blob: 9330194ceecb01e04bd6015616c69ac311436061 [file] [log] [blame]
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001//===- 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/RecursiveASTVisitor.h"
13
14using namespace clang;
15using namespace cxindex;
16
17namespace {
18
19class BodyIndexer : public RecursiveASTVisitor<BodyIndexer> {
20 IndexingContext &IndexCtx;
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000021 const NamedDecl *Parent;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000022 const DeclContext *ParentDC;
23
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +000024 typedef RecursiveASTVisitor<BodyIndexer> base;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000025public:
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000026 BodyIndexer(IndexingContext &indexCtx,
27 const NamedDecl *Parent, const DeclContext *DC)
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +000028 : IndexCtx(indexCtx), Parent(Parent), ParentDC(DC) { }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000029
30 bool shouldWalkTypesOfTypeLocs() const { return false; }
31
32 bool TraverseTypeLoc(TypeLoc TL) {
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000033 IndexCtx.indexTypeLoc(TL, Parent, ParentDC);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000034 return true;
35 }
36
Argyrios Kyrtzidis55fa1d92012-01-23 16:58:38 +000037 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
38 IndexCtx.indexNestedNameSpecifierLoc(NNS, Parent, ParentDC);
39 return true;
40 }
41
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000042 bool VisitDeclRefExpr(DeclRefExpr *E) {
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000043 IndexCtx.handleReference(E->getDecl(), E->getLocation(),
44 Parent, ParentDC, E);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000045 return true;
46 }
47
48 bool VisitMemberExpr(MemberExpr *E) {
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000049 IndexCtx.handleReference(E->getMemberDecl(), E->getMemberLoc(),
50 Parent, ParentDC, E);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000051 return true;
52 }
53
54 bool VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000055 IndexCtx.handleReference(E->getDecl(), E->getLocation(),
56 Parent, ParentDC, E);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000057 return true;
58 }
Argyrios Kyrtzidis9fbbf142011-10-18 15:13:11 +000059
60 bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +000061 if (TypeSourceInfo *Cls = E->getClassReceiverTypeInfo())
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000062 IndexCtx.indexTypeSourceInfo(Cls, Parent, ParentDC);
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +000063
Argyrios Kyrtzidis9fbbf142011-10-18 15:13:11 +000064 if (ObjCMethodDecl *MD = E->getMethodDecl())
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000065 IndexCtx.handleReference(MD, E->getSelectorStartLoc(),
66 Parent, ParentDC, E,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +000067 E->isImplicit() ? CXIdxEntityRef_Implicit
68 : CXIdxEntityRef_Direct);
Argyrios Kyrtzidis9fbbf142011-10-18 15:13:11 +000069 return true;
70 }
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +000071
72 bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
73 if (E->isImplicitProperty()) {
74 if (ObjCMethodDecl *MD = E->getImplicitPropertyGetter())
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000075 IndexCtx.handleReference(MD, E->getLocation(), Parent, ParentDC, E,
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +000076 CXIdxEntityRef_Implicit);
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +000077 if (ObjCMethodDecl *MD = E->getImplicitPropertySetter())
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000078 IndexCtx.handleReference(MD, E->getLocation(), Parent, ParentDC, E,
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +000079 CXIdxEntityRef_Implicit);
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +000080 } else {
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000081 IndexCtx.handleReference(E->getExplicitProperty(), E->getLocation(),
82 Parent, ParentDC, E);
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +000083 }
84 return true;
85 }
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +000086
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +000087 bool VisitCXXConstructExpr(CXXConstructExpr *E) {
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000088 IndexCtx.handleReference(E->getConstructor(), E->getLocation(),
89 Parent, ParentDC, E);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +000090 return true;
91 }
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +000092
Argyrios Kyrtzidis46e75472012-02-08 01:21:13 +000093 bool TraverseCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
94 if (E->getOperatorLoc().isInvalid())
95 return true; // implicit.
96 return base::TraverseCXXOperatorCallExpr(E);
97 }
98
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +000099 bool VisitDeclStmt(DeclStmt *S) {
Argyrios Kyrtzidis58d2dbe2012-02-14 22:23:11 +0000100 if (IndexCtx.shouldIndexFunctionLocalSymbols())
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +0000101 IndexCtx.indexDeclGroupRef(S->getDeclGroup());
102 return true;
103 }
Richard Smith5677eaf2012-02-15 02:07:05 +0000104
Douglas Gregor011d8b92012-02-15 00:54:55 +0000105 bool TraverseLambdaCapture(LambdaExpr::Capture C) {
106 if (C.capturesThis())
107 return true;
Richard Smith5677eaf2012-02-15 02:07:05 +0000108
109 if (IndexCtx.shouldIndexFunctionLocalSymbols())
Douglas Gregor011d8b92012-02-15 00:54:55 +0000110 IndexCtx.handleReference(C.getCapturedVar(), C.getLocation(),
111 Parent, ParentDC);
112 return true;
113 }
114
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000115};
116
117} // anonymous namespace
118
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +0000119void IndexingContext::indexBody(const Stmt *S, const NamedDecl *Parent,
120 const DeclContext *DC) {
121 if (!S)
122 return;
123
124 if (DC == 0)
125 DC = Parent->getLexicalDeclContext();
126 BodyIndexer(*this, Parent, DC).TraverseStmt(const_cast<Stmt*>(S));
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000127}