Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1 | //===- CIndexHigh.cpp - 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 "IndexingContext.h" |
| 11 | |
| 12 | #include "clang/AST/DeclVisitor.h" |
| 13 | |
| 14 | using namespace clang; |
| 15 | using namespace cxindex; |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | class IndexingDeclVisitor : public DeclVisitor<IndexingDeclVisitor, bool> { |
| 20 | IndexingContext &IndexCtx; |
| 21 | |
| 22 | public: |
| 23 | explicit IndexingDeclVisitor(IndexingContext &indexCtx) |
| 24 | : IndexCtx(indexCtx) { } |
| 25 | |
Argyrios Kyrtzidis | e422e45 | 2011-12-13 18:47:41 +0000 | [diff] [blame] | 26 | void handleDeclarator(DeclaratorDecl *D, const NamedDecl *Parent = 0) { |
| 27 | if (!Parent) Parent = D; |
Argyrios Kyrtzidis | db4d7a5 | 2012-01-14 02:05:51 +0000 | [diff] [blame] | 28 | |
| 29 | if (!IndexCtx.indexFunctionLocalSymbols()) { |
| 30 | IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), Parent); |
| 31 | IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent); |
| 32 | } else { |
| 33 | if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) { |
| 34 | IndexCtx.handleVar(Parm); |
| 35 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 36 | for (FunctionDecl::param_iterator |
| 37 | PI = FD->param_begin(), PE = FD->param_end(); PI != PE; ++PI) { |
| 38 | IndexCtx.handleVar(*PI); |
| 39 | } |
| 40 | } |
| 41 | } |
Argyrios Kyrtzidis | e422e45 | 2011-12-13 18:47:41 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 44 | bool VisitFunctionDecl(FunctionDecl *D) { |
| 45 | IndexCtx.handleFunction(D); |
Argyrios Kyrtzidis | e422e45 | 2011-12-13 18:47:41 +0000 | [diff] [blame] | 46 | handleDeclarator(D); |
Argyrios Kyrtzidis | 8818d45 | 2012-01-23 16:58:36 +0000 | [diff] [blame] | 47 | |
| 48 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) { |
| 49 | // Constructor initializers. |
| 50 | for (CXXConstructorDecl::init_iterator I = Ctor->init_begin(), |
| 51 | E = Ctor->init_end(); |
| 52 | I != E; ++I) { |
| 53 | CXXCtorInitializer *Init = *I; |
| 54 | if (Init->isWritten()) { |
| 55 | IndexCtx.indexTypeSourceInfo(Init->getTypeSourceInfo(), D); |
| 56 | if (const FieldDecl *Member = Init->getAnyMember()) |
| 57 | IndexCtx.handleReference(Member, Init->getMemberLocation(), D, D); |
| 58 | IndexCtx.indexBody(Init->getInit(), D, D); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 63 | if (D->isThisDeclarationADefinition()) { |
| 64 | const Stmt *Body = D->getBody(); |
| 65 | if (Body) { |
Argyrios Kyrtzidis | e422e45 | 2011-12-13 18:47:41 +0000 | [diff] [blame] | 66 | IndexCtx.indexBody(Body, D, D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | bool VisitVarDecl(VarDecl *D) { |
| 73 | IndexCtx.handleVar(D); |
Argyrios Kyrtzidis | e422e45 | 2011-12-13 18:47:41 +0000 | [diff] [blame] | 74 | handleDeclarator(D); |
| 75 | IndexCtx.indexBody(D->getInit(), D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 76 | return true; |
| 77 | } |
| 78 | |
| 79 | bool VisitFieldDecl(FieldDecl *D) { |
| 80 | IndexCtx.handleField(D); |
Argyrios Kyrtzidis | e422e45 | 2011-12-13 18:47:41 +0000 | [diff] [blame] | 81 | handleDeclarator(D); |
| 82 | if (D->isBitField()) |
| 83 | IndexCtx.indexBody(D->getBitWidth(), D); |
| 84 | else if (D->hasInClassInitializer()) |
| 85 | IndexCtx.indexBody(D->getInClassInitializer(), D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 86 | return true; |
| 87 | } |
| 88 | |
| 89 | bool VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 90 | IndexCtx.handleEnumerator(D); |
Argyrios Kyrtzidis | e422e45 | 2011-12-13 18:47:41 +0000 | [diff] [blame] | 91 | IndexCtx.indexBody(D->getInitExpr(), D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 92 | return true; |
| 93 | } |
| 94 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 95 | bool VisitTypedefDecl(TypedefNameDecl *D) { |
| 96 | IndexCtx.handleTypedefName(D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 97 | IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D); |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | bool VisitTagDecl(TagDecl *D) { |
| 102 | // Non-free standing tags are handled in indexTypeSourceInfo. |
| 103 | if (D->isFreeStanding()) |
| 104 | IndexCtx.indexTagDecl(D); |
| 105 | return true; |
| 106 | } |
| 107 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 108 | bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 109 | IndexCtx.handleObjCInterface(D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 110 | |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 111 | if (D->isThisDeclarationADefinition()) { |
| 112 | IndexCtx.indexTUDeclsInObjCContainer(); |
| 113 | IndexCtx.indexDeclContext(D); |
| 114 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 115 | return true; |
| 116 | } |
| 117 | |
| 118 | bool VisitObjCProtocolDecl(ObjCProtocolDecl *D) { |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 119 | IndexCtx.handleObjCProtocol(D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 120 | |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 121 | if (D->isThisDeclarationADefinition()) { |
| 122 | IndexCtx.indexTUDeclsInObjCContainer(); |
| 123 | IndexCtx.indexDeclContext(D); |
| 124 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 125 | return true; |
| 126 | } |
| 127 | |
| 128 | bool VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
Argyrios Kyrtzidis | e7bbab9 | 2011-11-15 06:20:24 +0000 | [diff] [blame] | 129 | const ObjCInterfaceDecl *Class = D->getClassInterface(); |
Argyrios Kyrtzidis | 37f4057 | 2011-11-23 20:27:26 +0000 | [diff] [blame] | 130 | if (!Class) |
| 131 | return true; |
| 132 | |
Argyrios Kyrtzidis | e7bbab9 | 2011-11-15 06:20:24 +0000 | [diff] [blame] | 133 | if (Class->isImplicitInterfaceDecl()) |
| 134 | IndexCtx.handleObjCInterface(Class); |
| 135 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 136 | IndexCtx.handleObjCImplementation(D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 137 | |
| 138 | IndexCtx.indexTUDeclsInObjCContainer(); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 139 | IndexCtx.indexDeclContext(D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 140 | return true; |
| 141 | } |
| 142 | |
| 143 | bool VisitObjCCategoryDecl(ObjCCategoryDecl *D) { |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 144 | IndexCtx.handleObjCCategory(D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 145 | |
| 146 | IndexCtx.indexTUDeclsInObjCContainer(); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 147 | IndexCtx.indexDeclContext(D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 148 | return true; |
| 149 | } |
| 150 | |
| 151 | bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
Argyrios Kyrtzidis | 37f4057 | 2011-11-23 20:27:26 +0000 | [diff] [blame] | 152 | const ObjCCategoryDecl *Cat = D->getCategoryDecl(); |
| 153 | if (!Cat) |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 154 | return true; |
| 155 | |
| 156 | IndexCtx.handleObjCCategoryImpl(D); |
| 157 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 158 | IndexCtx.indexTUDeclsInObjCContainer(); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 159 | IndexCtx.indexDeclContext(D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 160 | return true; |
| 161 | } |
| 162 | |
| 163 | bool VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 164 | IndexCtx.handleObjCMethod(D); |
| 165 | IndexCtx.indexTypeSourceInfo(D->getResultTypeSourceInfo(), D); |
| 166 | for (ObjCMethodDecl::param_iterator |
| 167 | I = D->param_begin(), E = D->param_end(); I != E; ++I) |
Argyrios Kyrtzidis | e422e45 | 2011-12-13 18:47:41 +0000 | [diff] [blame] | 168 | handleDeclarator(*I, D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 169 | |
| 170 | if (D->isThisDeclarationADefinition()) { |
| 171 | const Stmt *Body = D->getBody(); |
| 172 | if (Body) { |
Argyrios Kyrtzidis | e422e45 | 2011-12-13 18:47:41 +0000 | [diff] [blame] | 173 | IndexCtx.indexBody(Body, D, D); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | bool VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
| 180 | IndexCtx.handleObjCProperty(D); |
| 181 | IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D); |
| 182 | return true; |
| 183 | } |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 184 | |
| 185 | bool VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { |
| 186 | ObjCPropertyDecl *PD = D->getPropertyDecl(); |
| 187 | IndexCtx.handleSynthesizedObjCProperty(D); |
| 188 | |
| 189 | if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 190 | return true; |
| 191 | assert(D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize); |
| 192 | |
| 193 | if (ObjCIvarDecl *IvarD = D->getPropertyIvarDecl()) { |
| 194 | if (!IvarD->getSynthesize()) |
| 195 | IndexCtx.handleReference(IvarD, D->getPropertyIvarDeclLoc(), 0, |
| 196 | D->getDeclContext()); |
| 197 | } |
| 198 | |
| 199 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) { |
| 200 | if (MD->isSynthesized()) |
| 201 | IndexCtx.handleSynthesizedObjCMethod(MD, D->getLocation()); |
| 202 | } |
| 203 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) { |
| 204 | if (MD->isSynthesized()) |
| 205 | IndexCtx.handleSynthesizedObjCMethod(MD, D->getLocation()); |
| 206 | } |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 207 | return true; |
| 208 | } |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 209 | |
Argyrios Kyrtzidis | 68478b0 | 2011-12-07 05:52:06 +0000 | [diff] [blame] | 210 | bool VisitNamespaceDecl(NamespaceDecl *D) { |
| 211 | IndexCtx.handleNamespace(D); |
| 212 | IndexCtx.indexDeclContext(D); |
| 213 | return true; |
| 214 | } |
| 215 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 216 | bool VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 217 | IndexCtx.handleClassTemplate(D); |
| 218 | if (D->isThisDeclarationADefinition()) |
| 219 | IndexCtx.indexDeclContext(D->getTemplatedDecl()); |
| 220 | return true; |
| 221 | } |
| 222 | |
| 223 | bool VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 224 | IndexCtx.handleFunctionTemplate(D); |
| 225 | FunctionDecl *FD = D->getTemplatedDecl(); |
Argyrios Kyrtzidis | e422e45 | 2011-12-13 18:47:41 +0000 | [diff] [blame] | 226 | handleDeclarator(FD, D); |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 227 | if (FD->isThisDeclarationADefinition()) { |
| 228 | const Stmt *Body = FD->getBody(); |
| 229 | if (Body) { |
Argyrios Kyrtzidis | e422e45 | 2011-12-13 18:47:41 +0000 | [diff] [blame] | 230 | IndexCtx.indexBody(Body, D, FD); |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | return true; |
| 234 | } |
| 235 | |
| 236 | bool VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { |
| 237 | IndexCtx.handleTypeAliasTemplate(D); |
| 238 | IndexCtx.indexTypeSourceInfo(D->getTemplatedDecl()->getTypeSourceInfo(), D); |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 239 | return true; |
| 240 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | } // anonymous namespace |
| 244 | |
| 245 | void IndexingContext::indexDecl(const Decl *D) { |
Argyrios Kyrtzidis | d089008 | 2012-02-07 22:46:16 +0000 | [diff] [blame] | 246 | if (D->isImplicit() && shouldIgnoreIfImplicit(D)) |
| 247 | return; |
| 248 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 249 | bool Handled = IndexingDeclVisitor(*this).Visit(const_cast<Decl*>(D)); |
| 250 | if (!Handled && isa<DeclContext>(D)) |
| 251 | indexDeclContext(cast<DeclContext>(D)); |
| 252 | } |
| 253 | |
| 254 | void IndexingContext::indexDeclContext(const DeclContext *DC) { |
| 255 | for (DeclContext::decl_iterator |
| 256 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) { |
| 257 | indexDecl(*I); |
| 258 | } |
| 259 | } |
| 260 | |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 261 | void IndexingContext::indexTopLevelDecl(Decl *D) { |
| 262 | if (isNotFromSourceFile(D->getLocation())) |
| 263 | return; |
| 264 | |
| 265 | if (isa<ObjCMethodDecl>(D)) |
| 266 | return; // Wait for the objc container. |
| 267 | |
| 268 | indexDecl(D); |
| 269 | } |
| 270 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 271 | void IndexingContext::indexDeclGroupRef(DeclGroupRef DG) { |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 272 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 273 | indexTopLevelDecl(*I); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | void IndexingContext::indexTUDeclsInObjCContainer() { |
| 277 | for (unsigned i = 0, e = TUDeclsInObjCContainer.size(); i != e; ++i) |
| 278 | indexDeclGroupRef(TUDeclsInObjCContainer[i]); |
| 279 | TUDeclsInObjCContainer.clear(); |
| 280 | } |