language/duchain
indexeditems.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef INDEXEDITEMS_H
00015 #define INDEXEDITEMS_H
00016
00017 #include <sys/types.h>
00018 #include "../languageexport.h"
00019
00020 namespace KDevelop {
00021
00022 class Declaration;
00023 class TopDUContext;
00024 class IndexedString;
00025
00027 class KDEVPLATFORMLANGUAGE_EXPORT IndexedTopDUContext {
00028 public:
00029 inline IndexedTopDUContext(uint index) : m_index(index) {
00030 if(!index)
00031 setIsDummy(true);
00032 }
00033 IndexedTopDUContext(TopDUContext* context = 0);
00034
00035 enum {
00036 DummyMask = 1u<<31u
00037 };
00038
00042 TopDUContext* data() const;
00043
00045 bool isLoaded() const;
00046
00047 inline bool operator==(const IndexedTopDUContext& rhs) const {
00048 return m_index == rhs.m_index;
00049 }
00050
00051 inline bool operator!=(const IndexedTopDUContext& rhs) const {
00052 return m_index != rhs.m_index;
00053 }
00054
00055 inline bool operator<(const IndexedTopDUContext& rhs) const {
00056 return m_index < rhs.m_index;
00057 }
00058
00059 inline bool isValid() const {
00060 return m_index && !isDummy();
00061 }
00062
00063 inline uint index() const {
00064 if(isDummy())
00065 return 0;
00066 else
00067 return m_index;
00068 }
00069
00070 inline bool isDummy() const {
00071 return m_index & DummyMask;
00072 }
00073
00074 void setIsDummy(bool isDummy) {
00075 if(isDummy)
00076 m_index |= DummyMask;
00077 else
00078 m_index &= ~((uint)DummyMask);
00079 }
00080
00084 void setDummyData(ushort first, ushort second) {
00085 Q_ASSERT(isDummy());
00086 m_index = ((((uint)first)<<16) + second) | DummyMask;
00087 }
00088
00090 QPair<ushort, ushort> dummyData() const {
00091 uint withoutMask = m_index & (~((uint)DummyMask));
00092 return qMakePair((ushort)(withoutMask >> 16), (ushort)withoutMask);
00093 }
00094
00095 IndexedString url() const;
00096 private:
00097 uint m_index;
00098 friend class IndexedTopDUContextEmbeddedTreeHandler;
00099 };
00100
00101 struct IndexedTopDUContextIndexConversion {
00102 inline static uint toIndex(const IndexedTopDUContext& top) {
00103 return top.index();
00104 }
00105
00106 inline static IndexedTopDUContext toItem(uint index) {
00107 return IndexedTopDUContext(index);
00108 }
00109 };
00110
00111 class IndexedTopDUContextEmbeddedTreeHandler {
00112 public:
00113 static int leftChild(const IndexedTopDUContext& m_data) {
00114 return int(m_data.dummyData().first)-1;
00115 }
00116 static void setLeftChild(IndexedTopDUContext& m_data, int child) {
00117 m_data.setDummyData((ushort)(child+1), m_data.dummyData().second);
00118 }
00119 static int rightChild(const IndexedTopDUContext& m_data) {
00120 return int(m_data.dummyData().second)-1;
00121 }
00122 static void setRightChild(IndexedTopDUContext& m_data, int child) {
00123 m_data.setDummyData(m_data.dummyData().first, (ushort)(child+1));
00124 }
00125 static void createFreeItem(IndexedTopDUContext& data) {
00126 data = IndexedTopDUContext();
00127 data.setIsDummy(true);
00128 data.setDummyData(0u, 0u);
00129 }
00130
00131 static void copyTo(const IndexedTopDUContext& m_data, IndexedTopDUContext& data) {
00132 data = m_data;
00133 }
00134 static bool isFree(const IndexedTopDUContext& m_data) {
00135 return m_data.isDummy();
00136 }
00137
00138 static bool equals(const IndexedTopDUContext& m_data, const IndexedTopDUContext& rhs) {
00139 return m_data == rhs;
00140 }
00141 };
00142
00143
00144
00146 class KDEVPLATFORMLANGUAGE_EXPORT IndexedDeclaration {
00147 public:
00148 IndexedDeclaration(Declaration* decl = 0);
00149 IndexedDeclaration(uint topContext, uint declarationIndex);
00150
00152 Declaration* declaration() const;
00153
00155 Declaration* data() const {
00156 return declaration();
00157 }
00158
00159 inline bool operator==(const IndexedDeclaration& rhs) const {
00160 return m_topContext == rhs.m_topContext && m_declarationIndex == rhs.m_declarationIndex;
00161 }
00162
00163 inline uint hash() const {
00164 if(isDummy())
00165 return 0;
00166 return (m_topContext * 53 + m_declarationIndex) * 23;
00167 }
00168
00170 inline bool isValid() const {
00171 return !isDummy() && declaration() != 0;
00172 }
00173
00174 inline bool operator<(const IndexedDeclaration& rhs) const {
00175 Q_ASSERT(!isDummy());
00176 return m_topContext < rhs.m_topContext || (m_topContext == rhs.m_topContext && m_declarationIndex < rhs.m_declarationIndex);
00177 }
00178
00180 inline uint localIndex() const {
00181 if(isDummy())
00182 return 0;
00183 else
00184 return m_declarationIndex;
00185 }
00186
00187 inline uint topContextIndex() const {
00188 if(isDummy())
00189 return 0;
00190 else
00191 return m_topContext;
00192 }
00193
00194 inline IndexedTopDUContext indexedTopContext() const {
00195 if(isDummy())
00196 return IndexedTopDUContext();
00197 else
00198 return IndexedTopDUContext(m_topContext);
00199 }
00200
00205 void setIsDummy(bool dummy) {
00206 if(isDummy() == dummy)
00207 return;
00208 if(dummy)
00209 m_topContext = 1u << 31u;
00210 else
00211 m_topContext = 0;
00212 m_declarationIndex = 0;
00213 }
00214
00215 inline bool isDummy() const {
00216
00217
00218 return (bool)(m_topContext & static_cast<uint>(1u << 31u));
00219 }
00220
00221 inline QPair<uint, uint> dummyData() const {
00222 Q_ASSERT(isDummy());
00223 return qMakePair(m_topContext & (~(1u<<31u)), m_declarationIndex);
00224 }
00225
00227 void setDummyData(QPair<uint, uint> data) {
00228 Q_ASSERT(isDummy());
00229
00230 m_topContext = data.first;
00231 m_declarationIndex = data.second;
00232 Q_ASSERT(!isDummy());
00233 m_topContext |= (1u << 31u);
00234 Q_ASSERT(isDummy());
00235 Q_ASSERT(dummyData() == data);
00236 }
00237
00238
00239 private:
00240 uint m_topContext;
00241 uint m_declarationIndex;
00242 };
00243
00245 class KDEVPLATFORMLANGUAGE_EXPORT LocalIndexedDeclaration {
00246 public:
00247 LocalIndexedDeclaration(Declaration* decl = 0);
00248 LocalIndexedDeclaration(uint declarationIndex);
00249
00250
00251 Declaration* data(TopDUContext* top) const;
00252
00253 bool operator==(const LocalIndexedDeclaration& rhs) const {
00254 return m_declarationIndex == rhs.m_declarationIndex;
00255 }
00256 uint hash() const {
00257 return m_declarationIndex * 23;
00258 }
00259
00260 bool isValid() const {
00261 return m_declarationIndex != 0;
00262 }
00263
00264 bool operator<(const LocalIndexedDeclaration& rhs) const {
00265 return m_declarationIndex < rhs.m_declarationIndex;
00266 }
00267
00269 uint localIndex() const {
00270 return m_declarationIndex;
00271 }
00272
00273 bool isLoaded(TopDUContext* top) const;
00274
00275 private:
00276 uint m_declarationIndex;
00277 };
00278
00279 }
00280
00281 #endif // INDEXEDITEMS_H