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 | |
| 13 | #include "clang/AST/Decl.h" |
| 14 | #include "clang/AST/DeclGroup.h" |
| 15 | #include "llvm/ADT/DenseMap.h" |
| 16 | |
| 17 | namespace clang { |
| 18 | class FileEntry; |
| 19 | class ObjCPropertyDecl; |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 20 | class ObjCClassDecl; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 21 | |
| 22 | namespace cxindex { |
| 23 | class IndexingContext; |
| 24 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 25 | struct DeclInfo : public CXIdxDeclInfo { |
| 26 | CXIdxEntityInfo CXEntInfo; |
| 27 | }; |
| 28 | |
| 29 | struct TagDeclInfo : public DeclInfo { |
| 30 | CXIdxTagDeclInfo CXTagDeclInfo; |
| 31 | }; |
| 32 | |
| 33 | struct ObjCContainerDeclInfo : public DeclInfo { |
| 34 | CXIdxObjCContainerDeclInfo CXObjCContDeclInfo; |
| 35 | }; |
| 36 | |
| 37 | struct ObjCCategoryDeclInfo : public ObjCContainerDeclInfo { |
| 38 | CXIdxObjCCategoryDeclInfo CXObjCCatDeclInfo; |
| 39 | }; |
| 40 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 41 | class IndexingContext { |
| 42 | ASTContext *Ctx; |
| 43 | CXClientData ClientData; |
| 44 | IndexerCallbacks &CB; |
| 45 | unsigned IndexOptions; |
| 46 | CXTranslationUnit CXTU; |
| 47 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 48 | typedef llvm::DenseMap<const FileEntry *, CXIdxClientFile> FileMapTy; |
| 49 | typedef llvm::DenseMap<const NamedDecl *, CXIdxClientEntity> EntityMapTy; |
| 50 | typedef llvm::DenseMap<const void *, CXIdxClientMacro> MacroMapTy; |
| 51 | typedef llvm::DenseMap<const DeclContext *, CXIdxClientContainer> ContainerMapTy; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 52 | FileMapTy FileMap; |
| 53 | EntityMapTy EntityMap; |
| 54 | MacroMapTy MacroMap; |
| 55 | ContainerMapTy ContainerMap; |
| 56 | |
| 57 | SmallVector<DeclGroupRef, 8> TUDeclsInObjCContainer; |
| 58 | |
| 59 | llvm::SmallString<256> StrScratch; |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 60 | unsigned StrAdapterCount; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 61 | |
| 62 | class StrAdapter { |
| 63 | llvm::SmallString<256> &Scratch; |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 64 | IndexingContext &IdxCtx; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 65 | |
| 66 | public: |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 67 | StrAdapter(IndexingContext &indexCtx) |
| 68 | : Scratch(indexCtx.StrScratch), IdxCtx(indexCtx) { |
| 69 | ++IdxCtx.StrAdapterCount; |
| 70 | } |
| 71 | |
| 72 | ~StrAdapter() { |
| 73 | --IdxCtx.StrAdapterCount; |
| 74 | if (IdxCtx.StrAdapterCount == 0) |
| 75 | Scratch.clear(); |
| 76 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 77 | |
| 78 | const char *toCStr(StringRef Str); |
| 79 | |
| 80 | unsigned getCurSize() const { return Scratch.size(); } |
| 81 | |
| 82 | const char *getCStr(unsigned CharIndex) { |
| 83 | Scratch.push_back('\0'); |
| 84 | return Scratch.data() + CharIndex; |
| 85 | } |
| 86 | |
| 87 | SmallVectorImpl<char> &getBuffer() { return Scratch; } |
| 88 | }; |
| 89 | |
| 90 | public: |
| 91 | IndexingContext(CXClientData clientData, IndexerCallbacks &indexCallbacks, |
| 92 | unsigned indexOptions, CXTranslationUnit cxTU) |
| 93 | : Ctx(0), ClientData(clientData), CB(indexCallbacks), |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 94 | IndexOptions(indexOptions), CXTU(cxTU), StrAdapterCount(0) { } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 95 | |
| 96 | ASTContext &getASTContext() const { return *Ctx; } |
| 97 | |
| 98 | void setASTContext(ASTContext &ctx); |
| 99 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 100 | void enteredMainFile(const FileEntry *File); |
| 101 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 102 | void ppIncludedFile(SourceLocation hashLoc, |
| 103 | StringRef filename, const FileEntry *File, |
| 104 | bool isImport, bool isAngled); |
| 105 | |
| 106 | void ppMacroDefined(SourceLocation Loc, StringRef Name, |
| 107 | SourceLocation DefBegin, unsigned Length, |
| 108 | const void *OpaqueMacro); |
| 109 | |
| 110 | void ppMacroUndefined(SourceLocation Loc, StringRef Name, |
| 111 | const void *OpaqueMacro); |
| 112 | |
| 113 | void ppMacroExpanded(SourceLocation Loc, StringRef Name, |
| 114 | const void *OpaqueMacro); |
| 115 | |
| 116 | void invokeStartedTranslationUnit(); |
| 117 | |
| 118 | void invokeFinishedTranslationUnit(); |
| 119 | |
| 120 | void indexDecl(const Decl *D); |
| 121 | |
| 122 | void indexTagDecl(const TagDecl *D); |
| 123 | |
| 124 | void indexTypeSourceInfo(TypeSourceInfo *TInfo, const NamedDecl *Parent, |
| 125 | const DeclContext *DC = 0); |
| 126 | |
| 127 | void indexTypeLoc(TypeLoc TL, const NamedDecl *Parent, |
| 128 | const DeclContext *DC); |
| 129 | |
| 130 | void indexDeclContext(const DeclContext *DC); |
| 131 | |
| 132 | void indexBody(const Stmt *S, const DeclContext *DC); |
| 133 | |
| 134 | void handleDiagnostic(const StoredDiagnostic &StoredDiag); |
| 135 | |
| 136 | void handleFunction(const FunctionDecl *FD); |
| 137 | |
| 138 | void handleVar(const VarDecl *D); |
| 139 | |
| 140 | void handleField(const FieldDecl *D); |
| 141 | |
| 142 | void handleEnumerator(const EnumConstantDecl *D); |
| 143 | |
| 144 | void handleTagDecl(const TagDecl *D); |
| 145 | |
| 146 | void handleTypedef(const TypedefDecl *D); |
| 147 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 148 | void handleObjCClass(const ObjCClassDecl *D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 149 | void handleObjCInterface(const ObjCInterfaceDecl *D); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 150 | void handleObjCImplementation(const ObjCImplementationDecl *D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 151 | |
| 152 | void defineObjCInterface(const ObjCInterfaceDecl *D); |
| 153 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 154 | void handleObjCForwardProtocol(const ObjCProtocolDecl *D, |
| 155 | SourceLocation Loc, |
| 156 | bool isRedeclaration); |
| 157 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 158 | void handleObjCProtocol(const ObjCProtocolDecl *D); |
| 159 | |
| 160 | void handleObjCCategory(const ObjCCategoryDecl *D); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 161 | void handleObjCCategoryImpl(const ObjCCategoryImplDecl *D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 162 | |
| 163 | void handleObjCMethod(const ObjCMethodDecl *D); |
| 164 | |
| 165 | void handleObjCProperty(const ObjCPropertyDecl *D); |
| 166 | |
| 167 | void handleReference(const NamedDecl *D, SourceLocation Loc, |
| 168 | const NamedDecl *Parent, |
| 169 | const DeclContext *DC, |
Argyrios Kyrtzidis | aca19be | 2011-10-18 15:50:50 +0000 | [diff] [blame] | 170 | const Expr *E = 0, |
| 171 | CXIdxEntityRefKind Kind = CXIdxEntityRef_Direct); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 172 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 173 | void startContainer(const NamedDecl *D, bool isStmtBody = false, |
| 174 | const DeclContext *DC = 0); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 175 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 176 | void endContainer(const DeclContext *DC); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 177 | |
| 178 | bool isNotFromSourceFile(SourceLocation Loc) const; |
| 179 | |
| 180 | void indexTUDeclsInObjCContainer(); |
| 181 | void indexDeclGroupRef(DeclGroupRef DG); |
| 182 | |
| 183 | void addTUDeclInObjCContainer(DeclGroupRef DG) { |
| 184 | TUDeclsInObjCContainer.push_back(DG); |
| 185 | } |
| 186 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 187 | void translateLoc(SourceLocation Loc, CXIdxClientFile *indexFile, CXFile *file, |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 188 | unsigned *line, unsigned *column, unsigned *offset); |
| 189 | |
| 190 | private: |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 191 | void handleDecl(const NamedDecl *D, |
| 192 | SourceLocation Loc, CXCursor Cursor, |
| 193 | bool isRedeclaration, bool isDefinition, |
| 194 | DeclInfo &DInfo); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 195 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 196 | void handleObjCContainer(const ObjCContainerDecl *D, |
| 197 | SourceLocation Loc, CXCursor Cursor, |
| 198 | bool isForwardRef, |
| 199 | bool isRedeclaration, |
| 200 | bool isImplementation, |
| 201 | ObjCContainerDeclInfo &ContDInfo); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 202 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 203 | void addEntityInMap(const NamedDecl *D, CXIdxClientEntity entity); |
| 204 | |
| 205 | void addContainerInMap(const DeclContext *DC, CXIdxClientContainer container); |
| 206 | |
| 207 | CXIdxClientEntity getClientEntity(const NamedDecl *D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 208 | |
| 209 | const NamedDecl *getEntityDecl(const NamedDecl *D) const; |
| 210 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 211 | CXIdxClientContainer getIndexContainer(const NamedDecl *D) const { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 212 | return getIndexContainerForDC(D->getDeclContext()); |
| 213 | } |
| 214 | |
| 215 | const DeclContext *getScopedContext(const DeclContext *DC) const; |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 216 | CXIdxClientContainer getIndexContainerForDC(const DeclContext *DC) const; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 217 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame^] | 218 | CXIdxClientFile getIndexFile(const FileEntry *File); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 219 | |
| 220 | CXIdxLoc getIndexLoc(SourceLocation Loc) const; |
| 221 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 222 | void getEntityInfo(const NamedDecl *D, |
| 223 | CXIdxEntityInfo &EntityInfo, |
| 224 | StrAdapter &SA); |
| 225 | |
| 226 | CXCursor getCursor(const NamedDecl *D) { |
| 227 | return cxcursor::MakeCXCursor(const_cast<NamedDecl*>(D), CXTU); |
| 228 | } |
| 229 | |
| 230 | CXCursor getRefCursor(const NamedDecl *D, SourceLocation Loc); |
| 231 | }; |
| 232 | |
| 233 | }} // end clang::cxindex |