blob: 8d5f56609f05d44186887cbe42cf8470731e80eb [file] [log] [blame]
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001//===- 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
17namespace clang {
18 class FileEntry;
19 class ObjCPropertyDecl;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000020 class ObjCClassDecl;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000021
22namespace cxindex {
23 class IndexingContext;
24
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000025struct DeclInfo : public CXIdxDeclInfo {
26 CXIdxEntityInfo CXEntInfo;
27};
28
29struct TagDeclInfo : public DeclInfo {
30 CXIdxTagDeclInfo CXTagDeclInfo;
31};
32
33struct ObjCContainerDeclInfo : public DeclInfo {
34 CXIdxObjCContainerDeclInfo CXObjCContDeclInfo;
35};
36
37struct ObjCCategoryDeclInfo : public ObjCContainerDeclInfo {
38 CXIdxObjCCategoryDeclInfo CXObjCCatDeclInfo;
39};
40
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000041class IndexingContext {
42 ASTContext *Ctx;
43 CXClientData ClientData;
44 IndexerCallbacks &CB;
45 unsigned IndexOptions;
46 CXTranslationUnit CXTU;
47
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000048 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 Kyrtzidis4e7064f2011-10-17 19:48:19 +000052 FileMapTy FileMap;
53 EntityMapTy EntityMap;
54 MacroMapTy MacroMap;
55 ContainerMapTy ContainerMap;
56
57 SmallVector<DeclGroupRef, 8> TUDeclsInObjCContainer;
58
59 llvm::SmallString<256> StrScratch;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000060 unsigned StrAdapterCount;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000061
62 class StrAdapter {
63 llvm::SmallString<256> &Scratch;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000064 IndexingContext &IdxCtx;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000065
66 public:
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000067 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 Kyrtzidis4e7064f2011-10-17 19:48:19 +000077
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
90public:
91 IndexingContext(CXClientData clientData, IndexerCallbacks &indexCallbacks,
92 unsigned indexOptions, CXTranslationUnit cxTU)
93 : Ctx(0), ClientData(clientData), CB(indexCallbacks),
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000094 IndexOptions(indexOptions), CXTU(cxTU), StrAdapterCount(0) { }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000095
96 ASTContext &getASTContext() const { return *Ctx; }
97
98 void setASTContext(ASTContext &ctx);
99
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000100 void enteredMainFile(const FileEntry *File);
101
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000102 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 Kyrtzidisdd93c592011-11-11 00:23:36 +0000148 void handleObjCClass(const ObjCClassDecl *D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000149 void handleObjCInterface(const ObjCInterfaceDecl *D);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000150 void handleObjCImplementation(const ObjCImplementationDecl *D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000151
152 void defineObjCInterface(const ObjCInterfaceDecl *D);
153
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000154 void handleObjCForwardProtocol(const ObjCProtocolDecl *D,
155 SourceLocation Loc,
156 bool isRedeclaration);
157
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000158 void handleObjCProtocol(const ObjCProtocolDecl *D);
159
160 void handleObjCCategory(const ObjCCategoryDecl *D);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000161 void handleObjCCategoryImpl(const ObjCCategoryImplDecl *D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000162
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 Kyrtzidisaca19be2011-10-18 15:50:50 +0000170 const Expr *E = 0,
171 CXIdxEntityRefKind Kind = CXIdxEntityRef_Direct);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000172
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000173 void startContainer(const NamedDecl *D, bool isStmtBody = false,
174 const DeclContext *DC = 0);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000175
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000176 void endContainer(const DeclContext *DC);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000177
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 Kyrtzidisdd93c592011-11-11 00:23:36 +0000187 void translateLoc(SourceLocation Loc, CXIdxClientFile *indexFile, CXFile *file,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000188 unsigned *line, unsigned *column, unsigned *offset);
189
190private:
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000191 void handleDecl(const NamedDecl *D,
192 SourceLocation Loc, CXCursor Cursor,
193 bool isRedeclaration, bool isDefinition,
194 DeclInfo &DInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000195
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000196 void handleObjCContainer(const ObjCContainerDecl *D,
197 SourceLocation Loc, CXCursor Cursor,
198 bool isForwardRef,
199 bool isRedeclaration,
200 bool isImplementation,
201 ObjCContainerDeclInfo &ContDInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000202
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000203 void addEntityInMap(const NamedDecl *D, CXIdxClientEntity entity);
204
205 void addContainerInMap(const DeclContext *DC, CXIdxClientContainer container);
206
207 CXIdxClientEntity getClientEntity(const NamedDecl *D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000208
209 const NamedDecl *getEntityDecl(const NamedDecl *D) const;
210
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000211 CXIdxClientContainer getIndexContainer(const NamedDecl *D) const {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000212 return getIndexContainerForDC(D->getDeclContext());
213 }
214
215 const DeclContext *getScopedContext(const DeclContext *DC) const;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000216 CXIdxClientContainer getIndexContainerForDC(const DeclContext *DC) const;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000217
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000218 CXIdxClientFile getIndexFile(const FileEntry *File);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000219
220 CXIdxLoc getIndexLoc(SourceLocation Loc) const;
221
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000222 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