blob: b367b72b3a3773269716644761c21e1a43f91fc5 [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
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +000013#include "clang/AST/DeclObjC.h"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000014#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
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000029struct ObjCContainerDeclInfo : public DeclInfo {
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +000030 CXIdxObjCContainerDeclInfo ObjCContDeclInfo;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000031};
32
33struct ObjCCategoryDeclInfo : public ObjCContainerDeclInfo {
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +000034 CXIdxObjCCategoryDeclInfo ObjCCatDeclInfo;
35};
36
37struct ObjCInterfaceDeclInfo : public ObjCCategoryDeclInfo {
38 CXIdxObjCInterfaceDeclInfo ObjCInterDeclInfo;
39};
40
41struct ObjCProtocolDeclInfo : public ObjCCategoryDeclInfo {
42 CXIdxObjCProtocolDeclInfo ObjCProtoDeclInfo;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000043};
44
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000045class IndexingContext {
46 ASTContext *Ctx;
47 CXClientData ClientData;
48 IndexerCallbacks &CB;
49 unsigned IndexOptions;
50 CXTranslationUnit CXTU;
51
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000052 typedef llvm::DenseMap<const FileEntry *, CXIdxClientFile> FileMapTy;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000053 typedef llvm::DenseMap<const DeclContext *, CXIdxClientContainer> ContainerMapTy;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000054 FileMapTy FileMap;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000055 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
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +000090 struct ObjCProtocolListInfo {
91 SmallVector<CXIdxObjCProtocolRefInfo, 4> ProtInfos;
92 SmallVector<CXIdxEntityInfo, 4> ProtEntities;
93 SmallVector<CXIdxObjCProtocolRefInfo *, 4> Prots;
94
95 CXIdxObjCProtocolRefInfo **getProtocolRefs() { return Prots.data(); }
96 unsigned getNumProtocols() { return (unsigned)Prots.size(); }
97
98 ObjCProtocolListInfo(const ObjCProtocolList &ProtList,
99 IndexingContext &IdxCtx,
100 IndexingContext::StrAdapter &SA);
101 };
102
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000103public:
104 IndexingContext(CXClientData clientData, IndexerCallbacks &indexCallbacks,
105 unsigned indexOptions, CXTranslationUnit cxTU)
106 : Ctx(0), ClientData(clientData), CB(indexCallbacks),
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000107 IndexOptions(indexOptions), CXTU(cxTU), StrAdapterCount(0) { }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000108
109 ASTContext &getASTContext() const { return *Ctx; }
110
111 void setASTContext(ASTContext &ctx);
112
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000113 void enteredMainFile(const FileEntry *File);
114
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000115 void ppIncludedFile(SourceLocation hashLoc,
116 StringRef filename, const FileEntry *File,
117 bool isImport, bool isAngled);
118
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000119 void startedTranslationUnit();
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000120
121 void indexDecl(const Decl *D);
122
123 void indexTagDecl(const TagDecl *D);
124
125 void indexTypeSourceInfo(TypeSourceInfo *TInfo, const NamedDecl *Parent,
126 const DeclContext *DC = 0);
127
128 void indexTypeLoc(TypeLoc TL, const NamedDecl *Parent,
129 const DeclContext *DC);
130
131 void indexDeclContext(const DeclContext *DC);
132
133 void indexBody(const Stmt *S, const DeclContext *DC);
134
135 void handleDiagnostic(const StoredDiagnostic &StoredDiag);
136
137 void handleFunction(const FunctionDecl *FD);
138
139 void handleVar(const VarDecl *D);
140
141 void handleField(const FieldDecl *D);
142
143 void handleEnumerator(const EnumConstantDecl *D);
144
145 void handleTagDecl(const TagDecl *D);
146
147 void handleTypedef(const TypedefDecl *D);
148
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000149 void handleObjCClass(const ObjCClassDecl *D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000150 void handleObjCInterface(const ObjCInterfaceDecl *D);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000151 void handleObjCImplementation(const ObjCImplementationDecl *D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000152
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000153 void handleObjCForwardProtocol(const ObjCProtocolDecl *D,
154 SourceLocation Loc,
155 bool isRedeclaration);
156
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000157 void handleObjCProtocol(const ObjCProtocolDecl *D);
158
159 void handleObjCCategory(const ObjCCategoryDecl *D);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000160 void handleObjCCategoryImpl(const ObjCCategoryImplDecl *D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000161
162 void handleObjCMethod(const ObjCMethodDecl *D);
163
164 void handleObjCProperty(const ObjCPropertyDecl *D);
165
166 void handleReference(const NamedDecl *D, SourceLocation Loc,
167 const NamedDecl *Parent,
168 const DeclContext *DC,
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +0000169 const Expr *E = 0,
170 CXIdxEntityRefKind Kind = CXIdxEntityRef_Direct);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000171
172 bool isNotFromSourceFile(SourceLocation Loc) const;
173
174 void indexTUDeclsInObjCContainer();
175 void indexDeclGroupRef(DeclGroupRef DG);
176
177 void addTUDeclInObjCContainer(DeclGroupRef DG) {
178 TUDeclsInObjCContainer.push_back(DG);
179 }
180
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000181 void translateLoc(SourceLocation Loc, CXIdxClientFile *indexFile, CXFile *file,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000182 unsigned *line, unsigned *column, unsigned *offset);
183
184private:
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000185 void handleDecl(const NamedDecl *D,
186 SourceLocation Loc, CXCursor Cursor,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000187 bool isRedeclaration, bool isDefinition, bool isContainer,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000188 DeclInfo &DInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000189
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000190 void handleObjCContainer(const ObjCContainerDecl *D,
191 SourceLocation Loc, CXCursor Cursor,
192 bool isForwardRef,
193 bool isRedeclaration,
194 bool isImplementation,
195 ObjCContainerDeclInfo &ContDInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000196
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000197 void addContainerInMap(const DeclContext *DC, CXIdxClientContainer container);
198
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000199 const NamedDecl *getEntityDecl(const NamedDecl *D) const;
200
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000201 CXIdxClientContainer getIndexContainer(const NamedDecl *D) const {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000202 return getIndexContainerForDC(D->getDeclContext());
203 }
204
205 const DeclContext *getScopedContext(const DeclContext *DC) const;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000206 CXIdxClientContainer getIndexContainerForDC(const DeclContext *DC) const;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000207
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000208 CXIdxClientFile getIndexFile(const FileEntry *File);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000209
210 CXIdxLoc getIndexLoc(SourceLocation Loc) const;
211
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000212 void getEntityInfo(const NamedDecl *D,
213 CXIdxEntityInfo &EntityInfo,
214 StrAdapter &SA);
215
216 CXCursor getCursor(const NamedDecl *D) {
217 return cxcursor::MakeCXCursor(const_cast<NamedDecl*>(D), CXTU);
218 }
219
220 CXCursor getRefCursor(const NamedDecl *D, SourceLocation Loc);
221};
222
223}} // end clang::cxindex