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