Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1 | //===- IndexingContext.h - Higher level API functions ------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "Index_Internal.h" |
| 11 | #include "CXCursor.h" |
| 12 | |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 13 | #include "clang/AST/DeclObjC.h" |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclGroup.h" |
Argyrios Kyrtzidis | c6b4a50 | 2011-11-16 02:34:59 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/DenseSet.h" |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 16 | |
| 17 | namespace clang { |
| 18 | class FileEntry; |
| 19 | class ObjCPropertyDecl; |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 20 | class ClassTemplateDecl; |
| 21 | class FunctionTemplateDecl; |
| 22 | class TypeAliasTemplateDecl; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 23 | |
| 24 | namespace cxindex { |
| 25 | class IndexingContext; |
Argyrios Kyrtzidis | 643d3ce | 2011-12-15 00:05:00 +0000 | [diff] [blame] | 26 | class ScratchAlloc; |
| 27 | class AttrListInfo; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 28 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 29 | struct EntityInfo : public CXIdxEntityInfo { |
| 30 | const NamedDecl *Dcl; |
| 31 | IndexingContext *IndexCtx; |
Argyrios Kyrtzidis | 643d3ce | 2011-12-15 00:05:00 +0000 | [diff] [blame] | 32 | llvm::IntrusiveRefCntPtr<AttrListInfo> AttrList; |
Argyrios Kyrtzidis | 37f4057 | 2011-11-23 20:27:26 +0000 | [diff] [blame] | 33 | |
| 34 | EntityInfo() { |
| 35 | name = USR = 0; |
Argyrios Kyrtzidis | 643d3ce | 2011-12-15 00:05:00 +0000 | [diff] [blame] | 36 | attributes = 0; |
| 37 | numAttributes = 0; |
Argyrios Kyrtzidis | 37f4057 | 2011-11-23 20:27:26 +0000 | [diff] [blame] | 38 | } |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | struct ContainerInfo : public CXIdxContainerInfo { |
| 42 | const DeclContext *DC; |
| 43 | IndexingContext *IndexCtx; |
| 44 | }; |
| 45 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 46 | struct DeclInfo : public CXIdxDeclInfo { |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 47 | enum DInfoKind { |
| 48 | Info_Decl, |
| 49 | |
| 50 | Info_ObjCContainer, |
| 51 | Info_ObjCInterface, |
| 52 | Info_ObjCProtocol, |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 53 | Info_ObjCCategory, |
| 54 | |
| 55 | Info_CXXClass |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | DInfoKind Kind; |
| 59 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 60 | EntityInfo EntInfo; |
Argyrios Kyrtzidis | b1febb6 | 2011-12-07 20:44:19 +0000 | [diff] [blame] | 61 | ContainerInfo SemanticContainer; |
| 62 | ContainerInfo LexicalContainer; |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 63 | ContainerInfo DeclAsContainer; |
| 64 | |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 65 | DeclInfo(bool isRedeclaration, bool isDefinition, bool isContainer) |
| 66 | : Kind(Info_Decl) { |
| 67 | this->isRedeclaration = isRedeclaration; |
| 68 | this->isDefinition = isDefinition; |
| 69 | this->isContainer = isContainer; |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 70 | attributes = 0; |
| 71 | numAttributes = 0; |
Argyrios Kyrtzidis | b1febb6 | 2011-12-07 20:44:19 +0000 | [diff] [blame] | 72 | declAsContainer = semanticContainer = lexicalContainer = 0; |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 73 | } |
| 74 | DeclInfo(DInfoKind K, |
| 75 | bool isRedeclaration, bool isDefinition, bool isContainer) |
| 76 | : Kind(K) { |
| 77 | this->isRedeclaration = isRedeclaration; |
| 78 | this->isDefinition = isDefinition; |
| 79 | this->isContainer = isContainer; |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 80 | attributes = 0; |
| 81 | numAttributes = 0; |
Argyrios Kyrtzidis | b1febb6 | 2011-12-07 20:44:19 +0000 | [diff] [blame] | 82 | declAsContainer = semanticContainer = lexicalContainer = 0; |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static bool classof(const DeclInfo *) { return true; } |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 88 | struct ObjCContainerDeclInfo : public DeclInfo { |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 89 | CXIdxObjCContainerDeclInfo ObjCContDeclInfo; |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 90 | |
| 91 | ObjCContainerDeclInfo(bool isForwardRef, |
| 92 | bool isRedeclaration, |
| 93 | bool isImplementation) |
| 94 | : DeclInfo(Info_ObjCContainer, isRedeclaration, |
| 95 | /*isDefinition=*/!isForwardRef, /*isContainer=*/!isForwardRef) { |
| 96 | init(isForwardRef, isImplementation); |
| 97 | } |
| 98 | ObjCContainerDeclInfo(DInfoKind K, |
| 99 | bool isForwardRef, |
| 100 | bool isRedeclaration, |
| 101 | bool isImplementation) |
| 102 | : DeclInfo(K, isRedeclaration, /*isDefinition=*/!isForwardRef, |
| 103 | /*isContainer=*/!isForwardRef) { |
| 104 | init(isForwardRef, isImplementation); |
| 105 | } |
| 106 | |
| 107 | static bool classof(const DeclInfo *D) { |
| 108 | return Info_ObjCContainer <= D->Kind && D->Kind <= Info_ObjCCategory; |
| 109 | } |
| 110 | static bool classof(const ObjCContainerDeclInfo *D) { return true; } |
| 111 | |
| 112 | private: |
| 113 | void init(bool isForwardRef, bool isImplementation) { |
| 114 | if (isForwardRef) |
| 115 | ObjCContDeclInfo.kind = CXIdxObjCContainer_ForwardRef; |
| 116 | else if (isImplementation) |
| 117 | ObjCContDeclInfo.kind = CXIdxObjCContainer_Implementation; |
| 118 | else |
| 119 | ObjCContDeclInfo.kind = CXIdxObjCContainer_Interface; |
| 120 | } |
| 121 | }; |
| 122 | |
| 123 | struct ObjCInterfaceDeclInfo : public ObjCContainerDeclInfo { |
| 124 | CXIdxObjCInterfaceDeclInfo ObjCInterDeclInfo; |
| 125 | CXIdxObjCProtocolRefListInfo ObjCProtoListInfo; |
| 126 | |
| 127 | ObjCInterfaceDeclInfo(const ObjCInterfaceDecl *D) |
| 128 | : ObjCContainerDeclInfo(Info_ObjCInterface, |
| 129 | /*isForwardRef=*/false, |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 130 | /*isRedeclaration=*/D->getPreviousDecl() != 0, |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 131 | /*isImplementation=*/false) { } |
| 132 | |
| 133 | static bool classof(const DeclInfo *D) { |
| 134 | return D->Kind == Info_ObjCInterface; |
| 135 | } |
| 136 | static bool classof(const ObjCInterfaceDeclInfo *D) { return true; } |
| 137 | }; |
| 138 | |
| 139 | struct ObjCProtocolDeclInfo : public ObjCContainerDeclInfo { |
| 140 | CXIdxObjCProtocolRefListInfo ObjCProtoRefListInfo; |
| 141 | |
| 142 | ObjCProtocolDeclInfo(const ObjCProtocolDecl *D) |
| 143 | : ObjCContainerDeclInfo(Info_ObjCProtocol, |
| 144 | /*isForwardRef=*/false, |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 145 | /*isRedeclaration=*/D->getPreviousDecl(), |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 146 | /*isImplementation=*/false) { } |
| 147 | |
| 148 | static bool classof(const DeclInfo *D) { |
| 149 | return D->Kind == Info_ObjCProtocol; |
| 150 | } |
| 151 | static bool classof(const ObjCProtocolDeclInfo *D) { return true; } |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | struct ObjCCategoryDeclInfo : public ObjCContainerDeclInfo { |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 155 | CXIdxObjCCategoryDeclInfo ObjCCatDeclInfo; |
Argyrios Kyrtzidis | c10a4c8 | 2011-12-13 18:47:45 +0000 | [diff] [blame] | 156 | CXIdxObjCProtocolRefListInfo ObjCProtoListInfo; |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 157 | |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 158 | explicit ObjCCategoryDeclInfo(bool isImplementation) |
| 159 | : ObjCContainerDeclInfo(Info_ObjCCategory, |
| 160 | /*isForwardRef=*/false, |
| 161 | /*isRedeclaration=*/isImplementation, |
| 162 | /*isImplementation=*/isImplementation) { } |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 163 | |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 164 | static bool classof(const DeclInfo *D) { |
| 165 | return D->Kind == Info_ObjCCategory; |
| 166 | } |
| 167 | static bool classof(const ObjCCategoryDeclInfo *D) { return true; } |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 170 | struct CXXClassDeclInfo : public DeclInfo { |
| 171 | CXIdxCXXClassDeclInfo CXXClassInfo; |
| 172 | |
| 173 | CXXClassDeclInfo(bool isRedeclaration, bool isDefinition) |
| 174 | : DeclInfo(Info_CXXClass, isRedeclaration, isDefinition, isDefinition) { } |
| 175 | |
| 176 | static bool classof(const DeclInfo *D) { |
| 177 | return D->Kind == Info_CXXClass; |
| 178 | } |
| 179 | static bool classof(const CXXClassDeclInfo *D) { return true; } |
| 180 | }; |
| 181 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 182 | struct AttrInfo : public CXIdxAttrInfo { |
| 183 | const Attr *A; |
| 184 | |
| 185 | AttrInfo(CXIdxAttrKind Kind, CXCursor C, CXIdxLoc Loc, const Attr *A) { |
| 186 | kind = Kind; |
| 187 | cursor = C; |
| 188 | loc = Loc; |
| 189 | this->A = A; |
| 190 | } |
| 191 | |
| 192 | static bool classof(const AttrInfo *) { return true; } |
| 193 | }; |
| 194 | |
| 195 | struct IBOutletCollectionInfo : public AttrInfo { |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 196 | EntityInfo ClassInfo; |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 197 | CXIdxIBOutletCollectionAttrInfo IBCollInfo; |
| 198 | |
| 199 | IBOutletCollectionInfo(CXCursor C, CXIdxLoc Loc, const Attr *A) : |
| 200 | AttrInfo(CXIdxAttr_IBOutletCollection, C, Loc, A) { |
| 201 | assert(C.kind == CXCursor_IBOutletCollectionAttr); |
Argyrios Kyrtzidis | 643d3ce | 2011-12-15 00:05:00 +0000 | [diff] [blame] | 202 | IBCollInfo.objcClass = 0; |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Argyrios Kyrtzidis | 643d3ce | 2011-12-15 00:05:00 +0000 | [diff] [blame] | 205 | IBOutletCollectionInfo(const IBOutletCollectionInfo &other); |
| 206 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 207 | static bool classof(const AttrInfo *A) { |
| 208 | return A->kind == CXIdxAttr_IBOutletCollection; |
| 209 | } |
| 210 | static bool classof(const IBOutletCollectionInfo *D) { return true; } |
| 211 | }; |
| 212 | |
Argyrios Kyrtzidis | 643d3ce | 2011-12-15 00:05:00 +0000 | [diff] [blame] | 213 | class AttrListInfo { |
| 214 | SmallVector<AttrInfo, 2> Attrs; |
| 215 | SmallVector<IBOutletCollectionInfo, 2> IBCollAttrs; |
| 216 | SmallVector<CXIdxAttrInfo *, 2> CXAttrs; |
| 217 | unsigned ref_cnt; |
| 218 | |
| 219 | public: |
| 220 | AttrListInfo(const Decl *D, |
| 221 | IndexingContext &IdxCtx, |
| 222 | ScratchAlloc &SA); |
| 223 | AttrListInfo(const AttrListInfo &other); |
| 224 | |
| 225 | const CXIdxAttrInfo *const *getAttrs() const { |
| 226 | if (CXAttrs.empty()) |
| 227 | return 0; |
| 228 | return CXAttrs.data(); |
| 229 | } |
| 230 | unsigned getNumAttrs() const { return (unsigned)CXAttrs.size(); } |
| 231 | |
| 232 | /// \brief Retain/Release only useful when we allocate a AttrListInfo from the |
| 233 | /// BumpPtrAllocator, and not from the stack; so that we keep a pointer |
| 234 | // in the EntityInfo |
| 235 | void Retain() { ++ref_cnt; } |
| 236 | void Release() { |
| 237 | assert (ref_cnt > 0 && "Reference count is already zero."); |
| 238 | if (--ref_cnt == 0) { |
| 239 | // Memory is allocated from a BumpPtrAllocator, no need to delete it. |
| 240 | this->~AttrListInfo(); |
| 241 | } |
| 242 | } |
| 243 | }; |
| 244 | |
Argyrios Kyrtzidis | c6b4a50 | 2011-11-16 02:34:59 +0000 | [diff] [blame] | 245 | struct RefFileOccurence { |
| 246 | const FileEntry *File; |
| 247 | const Decl *Dcl; |
| 248 | |
| 249 | RefFileOccurence(const FileEntry *File, const Decl *Dcl) |
| 250 | : File(File), Dcl(Dcl) { } |
| 251 | }; |
| 252 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 253 | class IndexingContext { |
| 254 | ASTContext *Ctx; |
| 255 | CXClientData ClientData; |
| 256 | IndexerCallbacks &CB; |
| 257 | unsigned IndexOptions; |
| 258 | CXTranslationUnit CXTU; |
| 259 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 260 | typedef llvm::DenseMap<const FileEntry *, CXIdxClientFile> FileMapTy; |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 261 | typedef llvm::DenseMap<const DeclContext *, CXIdxClientContainer> |
| 262 | ContainerMapTy; |
| 263 | typedef llvm::DenseMap<const Decl *, CXIdxClientEntity> EntityMapTy; |
| 264 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 265 | FileMapTy FileMap; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 266 | ContainerMapTy ContainerMap; |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 267 | EntityMapTy EntityMap; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 268 | |
Argyrios Kyrtzidis | c6b4a50 | 2011-11-16 02:34:59 +0000 | [diff] [blame] | 269 | llvm::DenseSet<RefFileOccurence> RefFileOccurences; |
| 270 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 271 | SmallVector<DeclGroupRef, 8> TUDeclsInObjCContainer; |
| 272 | |
Argyrios Kyrtzidis | 37f4057 | 2011-11-23 20:27:26 +0000 | [diff] [blame] | 273 | llvm::BumpPtrAllocator StrScratch; |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 274 | unsigned StrAdapterCount; |
Argyrios Kyrtzidis | 643d3ce | 2011-12-15 00:05:00 +0000 | [diff] [blame] | 275 | friend class ScratchAlloc; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 276 | |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 277 | struct ObjCProtocolListInfo { |
| 278 | SmallVector<CXIdxObjCProtocolRefInfo, 4> ProtInfos; |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 279 | SmallVector<EntityInfo, 4> ProtEntities; |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 280 | SmallVector<CXIdxObjCProtocolRefInfo *, 4> Prots; |
| 281 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 282 | CXIdxObjCProtocolRefListInfo getListInfo() const { |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 283 | CXIdxObjCProtocolRefListInfo Info = { Prots.data(), |
| 284 | (unsigned)Prots.size() }; |
| 285 | return Info; |
| 286 | } |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 287 | |
| 288 | ObjCProtocolListInfo(const ObjCProtocolList &ProtList, |
| 289 | IndexingContext &IdxCtx, |
Argyrios Kyrtzidis | 643d3ce | 2011-12-15 00:05:00 +0000 | [diff] [blame] | 290 | ScratchAlloc &SA); |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | struct CXXBasesListInfo { |
| 294 | SmallVector<CXIdxBaseClassInfo, 4> BaseInfos; |
| 295 | SmallVector<EntityInfo, 4> BaseEntities; |
| 296 | SmallVector<CXIdxBaseClassInfo *, 4> CXBases; |
| 297 | |
| 298 | const CXIdxBaseClassInfo *const *getBases() const { |
| 299 | return CXBases.data(); |
| 300 | } |
| 301 | unsigned getNumBases() const { return (unsigned)CXBases.size(); } |
| 302 | |
| 303 | CXXBasesListInfo(const CXXRecordDecl *D, |
Argyrios Kyrtzidis | 643d3ce | 2011-12-15 00:05:00 +0000 | [diff] [blame] | 304 | IndexingContext &IdxCtx, ScratchAlloc &SA); |
Argyrios Kyrtzidis | b526a87 | 2011-12-07 20:44:15 +0000 | [diff] [blame] | 305 | |
| 306 | private: |
| 307 | SourceLocation getBaseLoc(const CXXBaseSpecifier &Base) const; |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 308 | }; |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 309 | |
Argyrios Kyrtzidis | 643d3ce | 2011-12-15 00:05:00 +0000 | [diff] [blame] | 310 | friend class AttrListInfo; |
| 311 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 312 | public: |
| 313 | IndexingContext(CXClientData clientData, IndexerCallbacks &indexCallbacks, |
| 314 | unsigned indexOptions, CXTranslationUnit cxTU) |
| 315 | : Ctx(0), ClientData(clientData), CB(indexCallbacks), |
Argyrios Kyrtzidis | 37f4057 | 2011-11-23 20:27:26 +0000 | [diff] [blame] | 316 | IndexOptions(indexOptions), CXTU(cxTU), |
| 317 | StrScratch(/*size=*/1024), StrAdapterCount(0) { } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 318 | |
| 319 | ASTContext &getASTContext() const { return *Ctx; } |
| 320 | |
| 321 | void setASTContext(ASTContext &ctx); |
| 322 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 323 | bool suppressRefs() const { |
| 324 | return IndexOptions & CXIndexOpt_SuppressRedundantRefs; |
Argyrios Kyrtzidis | c6b4a50 | 2011-11-16 02:34:59 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Argyrios Kyrtzidis | 2249074 | 2012-01-14 00:11:49 +0000 | [diff] [blame] | 327 | bool indexFunctionLocalSymbols() const { |
| 328 | return IndexOptions & CXIndexOpt_IndexFunctionLocalSymbols; |
| 329 | } |
| 330 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 331 | bool shouldAbort(); |
| 332 | |
Argyrios Kyrtzidis | 996e6e5 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 333 | bool hasDiagnosticCallback() const { return CB.diagnostic; } |
| 334 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 335 | void enteredMainFile(const FileEntry *File); |
| 336 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 337 | void ppIncludedFile(SourceLocation hashLoc, |
| 338 | StringRef filename, const FileEntry *File, |
| 339 | bool isImport, bool isAngled); |
| 340 | |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 341 | void startedTranslationUnit(); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 342 | |
| 343 | void indexDecl(const Decl *D); |
| 344 | |
| 345 | void indexTagDecl(const TagDecl *D); |
| 346 | |
| 347 | void indexTypeSourceInfo(TypeSourceInfo *TInfo, const NamedDecl *Parent, |
| 348 | const DeclContext *DC = 0); |
| 349 | |
| 350 | void indexTypeLoc(TypeLoc TL, const NamedDecl *Parent, |
Argyrios Kyrtzidis | e422e45 | 2011-12-13 18:47:41 +0000 | [diff] [blame] | 351 | const DeclContext *DC = 0); |
| 352 | |
| 353 | void indexNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS, |
| 354 | const NamedDecl *Parent, |
| 355 | const DeclContext *DC = 0); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 356 | |
| 357 | void indexDeclContext(const DeclContext *DC); |
| 358 | |
Argyrios Kyrtzidis | e422e45 | 2011-12-13 18:47:41 +0000 | [diff] [blame] | 359 | void indexBody(const Stmt *S, const NamedDecl *Parent, |
| 360 | const DeclContext *DC = 0); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 361 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 362 | void handleDiagnosticSet(CXDiagnosticSet CXDiagSet); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 363 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 364 | bool handleFunction(const FunctionDecl *FD); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 365 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 366 | bool handleVar(const VarDecl *D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 367 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 368 | bool handleField(const FieldDecl *D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 369 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 370 | bool handleEnumerator(const EnumConstantDecl *D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 371 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 372 | bool handleTagDecl(const TagDecl *D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 373 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 374 | bool handleTypedefName(const TypedefNameDecl *D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 375 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 376 | bool handleObjCInterface(const ObjCInterfaceDecl *D); |
| 377 | bool handleObjCImplementation(const ObjCImplementationDecl *D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 378 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 379 | bool handleObjCProtocol(const ObjCProtocolDecl *D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 380 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 381 | bool handleObjCCategory(const ObjCCategoryDecl *D); |
| 382 | bool handleObjCCategoryImpl(const ObjCCategoryImplDecl *D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 383 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 384 | bool handleObjCMethod(const ObjCMethodDecl *D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 385 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 386 | bool handleSynthesizedObjCProperty(const ObjCPropertyImplDecl *D); |
| 387 | bool handleSynthesizedObjCMethod(const ObjCMethodDecl *D, SourceLocation Loc); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 388 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 389 | bool handleObjCProperty(const ObjCPropertyDecl *D); |
| 390 | |
Argyrios Kyrtzidis | 68478b0 | 2011-12-07 05:52:06 +0000 | [diff] [blame] | 391 | bool handleNamespace(const NamespaceDecl *D); |
| 392 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 393 | bool handleClassTemplate(const ClassTemplateDecl *D); |
| 394 | bool handleFunctionTemplate(const FunctionTemplateDecl *D); |
| 395 | bool handleTypeAliasTemplate(const TypeAliasTemplateDecl *D); |
| 396 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 397 | bool handleReference(const NamedDecl *D, SourceLocation Loc, CXCursor Cursor, |
| 398 | const NamedDecl *Parent, |
| 399 | const DeclContext *DC, |
| 400 | const Expr *E = 0, |
| 401 | CXIdxEntityRefKind Kind = CXIdxEntityRef_Direct); |
| 402 | |
| 403 | bool handleReference(const NamedDecl *D, SourceLocation Loc, |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 404 | const NamedDecl *Parent, |
| 405 | const DeclContext *DC, |
Argyrios Kyrtzidis | aca19be | 2011-10-18 15:50:50 +0000 | [diff] [blame] | 406 | const Expr *E = 0, |
| 407 | CXIdxEntityRefKind Kind = CXIdxEntityRef_Direct); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 408 | |
| 409 | bool isNotFromSourceFile(SourceLocation Loc) const; |
| 410 | |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 411 | void indexTopLevelDecl(Decl *D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 412 | void indexTUDeclsInObjCContainer(); |
| 413 | void indexDeclGroupRef(DeclGroupRef DG); |
| 414 | |
| 415 | void addTUDeclInObjCContainer(DeclGroupRef DG) { |
| 416 | TUDeclsInObjCContainer.push_back(DG); |
| 417 | } |
| 418 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 419 | void translateLoc(SourceLocation Loc, CXIdxClientFile *indexFile, CXFile *file, |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 420 | unsigned *line, unsigned *column, unsigned *offset); |
| 421 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 422 | CXIdxClientContainer getClientContainerForDC(const DeclContext *DC) const; |
| 423 | void addContainerInMap(const DeclContext *DC, CXIdxClientContainer container); |
| 424 | |
| 425 | CXIdxClientEntity getClientEntity(const Decl *D) const; |
| 426 | void setClientEntity(const Decl *D, CXIdxClientEntity client); |
| 427 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 428 | private: |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 429 | bool handleDecl(const NamedDecl *D, |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 430 | SourceLocation Loc, CXCursor Cursor, |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 431 | DeclInfo &DInfo); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 432 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 433 | bool handleObjCContainer(const ObjCContainerDecl *D, |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 434 | SourceLocation Loc, CXCursor Cursor, |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 435 | ObjCContainerDeclInfo &ContDInfo); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 436 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 437 | bool handleCXXRecordDecl(const CXXRecordDecl *RD, const NamedDecl *OrigD); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 438 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 439 | bool markEntityOccurrenceInFile(const NamedDecl *D, SourceLocation Loc); |
| 440 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 441 | const NamedDecl *getEntityDecl(const NamedDecl *D) const; |
| 442 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 443 | const DeclContext *getEntityContainer(const Decl *D) const; |
| 444 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 445 | CXIdxClientFile getIndexFile(const FileEntry *File); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 446 | |
| 447 | CXIdxLoc getIndexLoc(SourceLocation Loc) const; |
| 448 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 449 | void getEntityInfo(const NamedDecl *D, |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 450 | EntityInfo &EntityInfo, |
Argyrios Kyrtzidis | 643d3ce | 2011-12-15 00:05:00 +0000 | [diff] [blame] | 451 | ScratchAlloc &SA); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 452 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 453 | void getContainerInfo(const DeclContext *DC, ContainerInfo &ContInfo); |
| 454 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 455 | CXCursor getCursor(const Decl *D) { |
| 456 | return cxcursor::MakeCXCursor(const_cast<Decl*>(D), CXTU); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | CXCursor getRefCursor(const NamedDecl *D, SourceLocation Loc); |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 460 | |
| 461 | static bool shouldIgnoreIfImplicit(const NamedDecl *D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 462 | }; |
| 463 | |
Argyrios Kyrtzidis | 643d3ce | 2011-12-15 00:05:00 +0000 | [diff] [blame] | 464 | class ScratchAlloc { |
| 465 | IndexingContext &IdxCtx; |
| 466 | |
| 467 | public: |
| 468 | explicit ScratchAlloc(IndexingContext &indexCtx) : IdxCtx(indexCtx) { |
| 469 | ++IdxCtx.StrAdapterCount; |
| 470 | } |
| 471 | |
| 472 | ~ScratchAlloc() { |
| 473 | --IdxCtx.StrAdapterCount; |
| 474 | if (IdxCtx.StrAdapterCount == 0) |
| 475 | IdxCtx.StrScratch.Reset(); |
| 476 | } |
| 477 | |
| 478 | const char *toCStr(StringRef Str); |
| 479 | const char *copyCStr(StringRef Str); |
| 480 | |
| 481 | template <typename T> |
| 482 | T *allocate() { |
| 483 | return IdxCtx.StrScratch.Allocate<T>(); |
| 484 | } |
| 485 | }; |
| 486 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 487 | }} // end clang::cxindex |
Argyrios Kyrtzidis | c6b4a50 | 2011-11-16 02:34:59 +0000 | [diff] [blame] | 488 | |
| 489 | namespace llvm { |
| 490 | /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and |
| 491 | /// DenseSets. |
| 492 | template <> |
| 493 | struct DenseMapInfo<clang::cxindex::RefFileOccurence> { |
| 494 | static inline clang::cxindex::RefFileOccurence getEmptyKey() { |
| 495 | return clang::cxindex::RefFileOccurence(0, 0); |
| 496 | } |
| 497 | |
| 498 | static inline clang::cxindex::RefFileOccurence getTombstoneKey() { |
| 499 | return clang::cxindex::RefFileOccurence((const clang::FileEntry *)~0, |
| 500 | (const clang::Decl *)~0); |
| 501 | } |
| 502 | |
| 503 | static unsigned getHashValue(clang::cxindex::RefFileOccurence S) { |
| 504 | llvm::FoldingSetNodeID ID; |
| 505 | ID.AddPointer(S.File); |
| 506 | ID.AddPointer(S.Dcl); |
| 507 | return ID.ComputeHash(); |
| 508 | } |
| 509 | |
| 510 | static bool isEqual(clang::cxindex::RefFileOccurence LHS, |
| 511 | clang::cxindex::RefFileOccurence RHS) { |
| 512 | return LHS.File == RHS.File && LHS.Dcl == RHS.Dcl; |
| 513 | } |
| 514 | }; |
| 515 | } |