blob: 74b3cfd8af9748665569b1052ecaf3461fdf7e6b [file] [log] [blame]
Ted Kremenekd2fa5662009-08-26 22:36:44 +00001//===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===//
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// This file implements the Clang-C Source Indexing library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang-c/Index.h"
Steve Naroff50398192009-08-28 15:28:48 +000015#include "clang/Index/Program.h"
16#include "clang/Index/Indexer.h"
Steve Naroff9efa7672009-09-04 15:44:05 +000017#include "clang/Index/ASTLocation.h"
18#include "clang/Index/Utils.h"
Steve Naroff50398192009-08-28 15:28:48 +000019#include "clang/AST/DeclVisitor.h"
Steve Narofffb570422009-09-22 19:25:29 +000020#include "clang/AST/StmtVisitor.h"
Steve Naroffc857ea42009-09-02 13:28:54 +000021#include "clang/AST/Decl.h"
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000022#include "clang/Basic/FileManager.h"
Steve Naroff2d4d6292009-08-31 14:26:51 +000023#include "clang/Basic/SourceManager.h"
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000024#include "clang/Frontend/ASTUnit.h"
25#include <cstdio>
Steve Naroff50398192009-08-28 15:28:48 +000026using namespace clang;
27using namespace idx;
28
Steve Naroff89922f82009-08-31 00:59:03 +000029namespace {
30
Steve Naroff4ade6d62009-09-23 17:52:52 +000031static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE)
32{
33 NamedDecl *D = DRE->getDecl();
34 if (isa<VarDecl>(D))
35 return CXCursor_VarRef;
36 else if (isa<FunctionDecl>(D))
37 return CXCursor_FunctionRef;
38 else if (isa<EnumConstantDecl>(D))
39 return CXCursor_EnumConstantRef;
40 else
41 return CXCursor_NotImplemented;
42}
43
44#if 0
45// Will be useful one day.
Steve Narofffb570422009-09-22 19:25:29 +000046class CRefVisitor : public StmtVisitor<CRefVisitor> {
47 CXDecl CDecl;
48 CXDeclIterator Callback;
49 CXClientData CData;
50
51 void Call(enum CXCursorKind CK, Stmt *SRef) {
52 CXCursor C = { CK, CDecl, SRef };
53 Callback(CDecl, C, CData);
54 }
55
56public:
57 CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
58 CDecl(C), Callback(cback), CData(D) {}
59
60 void VisitStmt(Stmt *S) {
61 for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
62 C != CEnd; ++C)
63 Visit(*C);
64 }
65 void VisitDeclRefExpr(DeclRefExpr *Node) {
Steve Naroff4ade6d62009-09-23 17:52:52 +000066 Call(TranslateDeclRefExpr(Node), Node);
Steve Narofffb570422009-09-22 19:25:29 +000067 }
68 void VisitMemberExpr(MemberExpr *Node) {
69 Call(CXCursor_MemberRef, Node);
70 }
71 void VisitObjCMessageExpr(ObjCMessageExpr *Node) {
72 Call(CXCursor_ObjCSelectorRef, Node);
73 }
74 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
75 Call(CXCursor_ObjCIvarRef, Node);
76 }
77};
Steve Naroff4ade6d62009-09-23 17:52:52 +000078#endif
Steve Narofffb570422009-09-22 19:25:29 +000079
Steve Naroff89922f82009-08-31 00:59:03 +000080// Translation Unit Visitor.
81class TUVisitor : public DeclVisitor<TUVisitor> {
82 CXTranslationUnit TUnit;
83 CXTranslationUnitIterator Callback;
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000084 CXClientData CData;
85
86 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Narofffb570422009-09-22 19:25:29 +000087 CXCursor C = { CK, ND, 0 };
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000088 Callback(TUnit, C, CData);
89 }
Steve Naroff89922f82009-08-31 00:59:03 +000090public:
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000091 TUVisitor(CXTranslationUnit CTU,
92 CXTranslationUnitIterator cback, CXClientData D) :
93 TUnit(CTU), Callback(cback), CData(D) {}
Steve Naroff89922f82009-08-31 00:59:03 +000094
95 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
96 VisitDeclContext(dyn_cast<DeclContext>(D));
97 }
98 void VisitDeclContext(DeclContext *DC) {
99 for (DeclContext::decl_iterator
100 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
101 Visit(*I);
102 }
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000103 void VisitTypedefDecl(TypedefDecl *ND) {
104 Call(CXCursor_TypedefDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000105 }
106 void VisitTagDecl(TagDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000107 switch (ND->getTagKind()) {
108 case TagDecl::TK_struct:
109 Call(CXCursor_StructDecl, ND);
110 break;
111 case TagDecl::TK_class:
112 Call(CXCursor_ClassDecl, ND);
113 break;
114 case TagDecl::TK_union:
115 Call(CXCursor_UnionDecl, ND);
116 break;
117 case TagDecl::TK_enum:
118 Call(CXCursor_EnumDecl, ND);
119 break;
120 }
Steve Naroff89922f82009-08-31 00:59:03 +0000121 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000122 void VisitVarDecl(VarDecl *ND) {
123 Call(CXCursor_VarDecl, ND);
124 }
Steve Naroff89922f82009-08-31 00:59:03 +0000125 void VisitFunctionDecl(FunctionDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000126 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
127 : CXCursor_FunctionDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000128 }
129 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000130 Call(CXCursor_ObjCInterfaceDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000131 }
132 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000133 Call(CXCursor_ObjCCategoryDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000134 }
135 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000136 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000137 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000138 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
139 Call(CXCursor_ObjCClassDefn, ND);
140 }
141 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
142 Call(CXCursor_ObjCCategoryDefn, ND);
143 }
Steve Naroff89922f82009-08-31 00:59:03 +0000144};
145
Steve Naroffc857ea42009-09-02 13:28:54 +0000146// Declaration visitor.
147class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
148 CXDecl CDecl;
149 CXDeclIterator Callback;
150 CXClientData CData;
151
152 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000153 // Disable the callback when the context is equal to the visiting decl.
154 if (CDecl == ND && !clang_isReference(CK))
155 return;
Steve Narofffb570422009-09-22 19:25:29 +0000156 CXCursor C = { CK, ND, 0 };
Steve Naroffc857ea42009-09-02 13:28:54 +0000157 Callback(CDecl, C, CData);
158 }
Steve Naroff89922f82009-08-31 00:59:03 +0000159public:
Steve Naroffc857ea42009-09-02 13:28:54 +0000160 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
161 CDecl(C), Callback(cback), CData(D) {}
162
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000163 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
164 // Issue callbacks for the containing class.
165 Call(CXCursor_ObjCClassRef, ND);
166 // FIXME: Issue callbacks for protocol refs.
167 VisitDeclContext(dyn_cast<DeclContext>(ND));
168 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000169 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000170 // Issue callbacks for super class.
Steve Narofff334b4e2009-09-02 18:26:48 +0000171 if (D->getSuperClass())
172 Call(CXCursor_ObjCSuperClassRef, D);
173
Steve Naroff9efa7672009-09-04 15:44:05 +0000174 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
175 E = D->protocol_end(); I != E; ++I)
176 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroffc857ea42009-09-02 13:28:54 +0000177 VisitDeclContext(dyn_cast<DeclContext>(D));
178 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000179 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
180 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
181 E = PID->protocol_end(); I != E; ++I)
182 Call(CXCursor_ObjCProtocolRef, *I);
183
184 VisitDeclContext(dyn_cast<DeclContext>(PID));
185 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000186 void VisitTagDecl(TagDecl *D) {
187 VisitDeclContext(dyn_cast<DeclContext>(D));
188 }
189 void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
190 VisitDeclContext(dyn_cast<DeclContext>(D));
191 }
192 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
193 VisitDeclContext(dyn_cast<DeclContext>(D));
194 }
Steve Naroff89922f82009-08-31 00:59:03 +0000195 void VisitDeclContext(DeclContext *DC) {
196 for (DeclContext::decl_iterator
197 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
198 Visit(*I);
199 }
200 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000201 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000202 }
203 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000204 Call(CXCursor_FieldDecl, ND);
205 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000206 void VisitVarDecl(VarDecl *ND) {
207 Call(CXCursor_VarDecl, ND);
208 }
209 void VisitParmVarDecl(ParmVarDecl *ND) {
210 Call(CXCursor_ParmDecl, ND);
211 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000212 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
213 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000214 }
215 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000216 Call(CXCursor_ObjCIvarDecl, ND);
217 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000218 void VisitFunctionDecl(FunctionDecl *ND) {
219 if (ND->isThisDeclarationADefinition()) {
220 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff4ade6d62009-09-23 17:52:52 +0000221#if 0
222 // Not currently needed.
223 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Narofffb570422009-09-22 19:25:29 +0000224 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000225 RVisit.Visit(Body);
226#endif
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000227 }
228 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000229 void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
230 if (ND->getBody()) {
231 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
232 : CXCursor_ObjCClassMethodDefn, ND);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000233 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroffc857ea42009-09-02 13:28:54 +0000234 } else
235 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
236 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000237 }
238};
239
240}
241
Steve Naroff600866c2009-08-27 19:51:58 +0000242extern "C" {
Ted Kremenekd2fa5662009-08-26 22:36:44 +0000243
Steve Naroff600866c2009-08-27 19:51:58 +0000244CXIndex clang_createIndex()
Steve Naroff50398192009-08-28 15:28:48 +0000245{
Daniel Dunbara3907592009-09-21 03:03:22 +0000246 // FIXME: Program is leaked.
247 return new Indexer(*new Program());
Steve Naroff600866c2009-08-27 19:51:58 +0000248}
249
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000250void clang_disposeIndex(CXIndex CIdx)
251{
252 assert(CIdx && "Passed null CXIndex");
253 delete static_cast<Indexer *>(CIdx);
254}
255
Steve Naroff50398192009-08-28 15:28:48 +0000256// FIXME: need to pass back error info.
257CXTranslationUnit clang_createTranslationUnit(
258 CXIndex CIdx, const char *ast_filename)
Steve Naroff600866c2009-08-27 19:51:58 +0000259{
Steve Naroff50398192009-08-28 15:28:48 +0000260 assert(CIdx && "Passed null CXIndex");
261 Indexer *CXXIdx = static_cast<Indexer *>(CIdx);
262 std::string astName(ast_filename);
263 std::string ErrMsg;
264
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000265 return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getDiagnostics(),
266 CXXIdx->getFileManager(), &ErrMsg);
Steve Naroff600866c2009-08-27 19:51:58 +0000267}
268
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000269void clang_disposeTranslationUnit(
270 CXTranslationUnit CTUnit)
271{
272 assert(CTUnit && "Passed null CXTranslationUnit");
273 delete static_cast<ASTUnit *>(CTUnit);
274}
275
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000276const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit)
277{
278 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000279 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
280 return CXXUnit->getOriginalSourceFileName().c_str();
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000281}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000282
Steve Naroffc857ea42009-09-02 13:28:54 +0000283void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
284 CXTranslationUnitIterator callback,
285 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000286{
Steve Naroff50398192009-08-28 15:28:48 +0000287 assert(CTUnit && "Passed null CXTranslationUnit");
288 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
289 ASTContext &Ctx = CXXUnit->getASTContext();
290
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000291 TUVisitor DVisit(CTUnit, callback, CData);
Steve Naroff50398192009-08-28 15:28:48 +0000292 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000293}
294
Steve Naroffc857ea42009-09-02 13:28:54 +0000295void clang_loadDeclaration(CXDecl Dcl,
296 CXDeclIterator callback,
297 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000298{
Steve Naroffc857ea42009-09-02 13:28:54 +0000299 assert(Dcl && "Passed null CXDecl");
300
301 CDeclVisitor DVisit(Dcl, callback, CData);
302 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000303}
304
Steve Naroff7e8f8182009-08-28 12:07:44 +0000305// Some notes on CXEntity:
306//
307// - Since the 'ordinary' namespace includes functions, data, typedefs,
308// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
309// entity for 2 different types). For example:
310//
311// module1.m: @interface Foo @end Foo *x;
312// module2.m: void Foo(int);
313//
314// - Since the unique name spans translation units, static data/functions
315// within a CXTranslationUnit are *not* currently represented by entities.
316// As a result, there will be no entity for the following:
317//
318// module.m: static void Foo() { }
319//
320
321
Steve Naroff600866c2009-08-27 19:51:58 +0000322const char *clang_getDeclarationName(CXEntity)
323{
324 return "";
325}
326const char *clang_getURI(CXEntity)
327{
328 return "";
329}
330
331CXEntity clang_getEntity(const char *URI)
332{
333 return 0;
334}
335
336//
337// CXDecl Operations.
338//
Steve Naroff600866c2009-08-27 19:51:58 +0000339CXEntity clang_getEntityFromDecl(CXDecl)
340{
341 return 0;
342}
Steve Naroff89922f82009-08-31 00:59:03 +0000343const char *clang_getDeclSpelling(CXDecl AnonDecl)
Steve Naroff600866c2009-08-27 19:51:58 +0000344{
Steve Naroff89922f82009-08-31 00:59:03 +0000345 assert(AnonDecl && "Passed null CXDecl");
346 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffc857ea42009-09-02 13:28:54 +0000347
348 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) {
349 return OMD->getSelector().getAsString().c_str();
350 }
Steve Naroff89922f82009-08-31 00:59:03 +0000351 if (ND->getIdentifier())
352 return ND->getIdentifier()->getName();
Steve Naroffc857ea42009-09-02 13:28:54 +0000353 else
Steve Naroff89922f82009-08-31 00:59:03 +0000354 return "";
Steve Naroff600866c2009-08-27 19:51:58 +0000355}
Steve Narofff334b4e2009-09-02 18:26:48 +0000356
Steve Naroff699a07d2009-09-25 21:32:34 +0000357unsigned clang_getDeclLine(CXDecl AnonDecl)
358{
359 assert(AnonDecl && "Passed null CXDecl");
360 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
361 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
362 return SourceMgr.getSpellingLineNumber(ND->getLocation());
363}
364
365unsigned clang_getDeclColumn(CXDecl AnonDecl)
366{
367 assert(AnonDecl && "Passed null CXDecl");
368 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
369 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
370 return SourceMgr.getSpellingLineNumber(ND->getLocation());
371}
372
Steve Narofff334b4e2009-09-02 18:26:48 +0000373const char *clang_getCursorSpelling(CXCursor C)
374{
375 assert(C.decl && "CXCursor has null decl");
376 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
377
378 if (clang_isReference(C.kind)) {
379 switch (C.kind) {
Steve Naroff1164d852009-09-02 18:58:52 +0000380 case CXCursor_ObjCSuperClassRef:
381 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000382 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
383 assert(OID && "clang_getCursorLine(): Missing interface decl");
384 return OID->getSuperClass()->getIdentifier()->getName();
Steve Naroff1164d852009-09-02 18:58:52 +0000385 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000386 case CXCursor_ObjCClassRef:
387 {
388 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
389 assert(OID && "clang_getCursorLine(): Missing category decl");
390 return OID->getClassInterface()->getIdentifier()->getName();
391 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000392 case CXCursor_ObjCProtocolRef:
393 {
394 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
395 assert(OID && "clang_getCursorLine(): Missing protocol decl");
396 return OID->getIdentifier()->getName();
397 }
Steve Narofffb570422009-09-22 19:25:29 +0000398 case CXCursor_ObjCSelectorRef:
399 {
400 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
401 static_cast<Stmt *>(C.stmt));
402 assert(OME && "clang_getCursorLine(): Missing message expr");
403 return OME->getSelector().getAsString().c_str();
404 }
405 case CXCursor_VarRef:
406 case CXCursor_FunctionRef:
407 case CXCursor_EnumConstantRef:
408 {
409 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
410 static_cast<Stmt *>(C.stmt));
411 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
412 return DRE->getDecl()->getIdentifier()->getName();
413 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000414 default:
415 return "<not implemented>";
416 }
417 }
418 return clang_getDeclSpelling(C.decl);
419}
420
421const char *clang_getCursorKindSpelling(enum CXCursorKind Kind)
Steve Naroff600866c2009-08-27 19:51:58 +0000422{
Steve Naroff89922f82009-08-31 00:59:03 +0000423 switch (Kind) {
424 case CXCursor_FunctionDecl: return "FunctionDecl";
425 case CXCursor_TypedefDecl: return "TypedefDecl";
426 case CXCursor_EnumDecl: return "EnumDecl";
427 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000428 case CXCursor_StructDecl: return "StructDecl";
429 case CXCursor_UnionDecl: return "UnionDecl";
430 case CXCursor_ClassDecl: return "ClassDecl";
Steve Naroff89922f82009-08-31 00:59:03 +0000431 case CXCursor_FieldDecl: return "FieldDecl";
432 case CXCursor_VarDecl: return "VarDecl";
433 case CXCursor_ParmDecl: return "ParmDecl";
434 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
435 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
436 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
437 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
438 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000439 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
440 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
441 case CXCursor_FunctionDefn: return "FunctionDefn";
442 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
443 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
444 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
445 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Steve Narofff334b4e2009-09-02 18:26:48 +0000446 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
Steve Naroff9efa7672009-09-04 15:44:05 +0000447 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000448 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Steve Narofffb570422009-09-22 19:25:29 +0000449 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
450
451 case CXCursor_VarRef: return "VarRef";
452 case CXCursor_FunctionRef: return "FunctionRef";
453 case CXCursor_EnumConstantRef: return "EnumConstantRef";
454 case CXCursor_MemberRef: return "MemberRef";
455
Steve Naroff77128dd2009-09-15 20:25:34 +0000456 case CXCursor_InvalidFile: return "InvalidFile";
457 case CXCursor_NoDeclFound: return "NoDeclFound";
458 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff89922f82009-08-31 00:59:03 +0000459 default: return "<not implemented>";
460 }
Steve Naroff600866c2009-08-27 19:51:58 +0000461}
Steve Naroff89922f82009-08-31 00:59:03 +0000462
Steve Naroff9efa7672009-09-04 15:44:05 +0000463static enum CXCursorKind TranslateKind(Decl *D) {
464 switch (D->getKind()) {
465 case Decl::Function: return CXCursor_FunctionDecl;
466 case Decl::Typedef: return CXCursor_TypedefDecl;
467 case Decl::Enum: return CXCursor_EnumDecl;
468 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
469 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
470 case Decl::Field: return CXCursor_FieldDecl;
471 case Decl::Var: return CXCursor_VarDecl;
472 case Decl::ParmVar: return CXCursor_ParmDecl;
473 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
Steve Narofffb570422009-09-22 19:25:29 +0000474 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
475 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Steve Naroff9efa7672009-09-04 15:44:05 +0000476 case Decl::ObjCMethod: {
477 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
478 if (MD->isInstanceMethod())
479 return CXCursor_ObjCInstanceMethodDecl;
480 return CXCursor_ObjCClassMethodDecl;
481 }
482 default: break;
483 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000484 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000485}
Steve Naroff600866c2009-08-27 19:51:58 +0000486//
487// CXCursor Operations.
488//
Steve Naroff9efa7672009-09-04 15:44:05 +0000489CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Steve Naroff600866c2009-08-27 19:51:58 +0000490 unsigned line, unsigned column)
491{
Steve Naroff9efa7672009-09-04 15:44:05 +0000492 assert(CTUnit && "Passed null CXTranslationUnit");
493 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
494
495 FileManager &FMgr = CXXUnit->getFileManager();
496 const FileEntry *File = FMgr.getFile(source_name,
Steve Naroff77128dd2009-09-15 20:25:34 +0000497 source_name+strlen(source_name));
498 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000499 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000500 return C;
501 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000502 SourceLocation SLoc =
503 CXXUnit->getSourceManager().getLocation(File, line, column);
504
505 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc);
506
507 Decl *Dcl = ALoc.getDecl();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000508 Stmt *Stm = ALoc.getStmt();
509 if (Dcl) {
510 if (Stm) {
511 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
512 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
513 return C;
514 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
515 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
516 return C;
517 }
518 // Fall through...treat as a decl, not a ref.
519 }
Steve Narofffb570422009-09-22 19:25:29 +0000520 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000521 return C;
522 }
Steve Narofffb570422009-09-22 19:25:29 +0000523 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000524 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000525}
526
Steve Naroff77128dd2009-09-15 20:25:34 +0000527CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
528{
529 assert(AnonDecl && "Passed null CXDecl");
530 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
531
Steve Narofffb570422009-09-22 19:25:29 +0000532 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000533 return C;
534}
535
536unsigned clang_isInvalid(enum CXCursorKind K)
537{
538 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
539}
540
Steve Naroff89922f82009-08-31 00:59:03 +0000541unsigned clang_isDeclaration(enum CXCursorKind K)
542{
543 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
544}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000545
Steve Narofff334b4e2009-09-02 18:26:48 +0000546unsigned clang_isReference(enum CXCursorKind K)
547{
548 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
549}
550
551unsigned clang_isDefinition(enum CXCursorKind K)
552{
553 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
554}
555
Steve Naroff9efa7672009-09-04 15:44:05 +0000556CXCursorKind clang_getCursorKind(CXCursor C)
557{
558 return C.kind;
559}
560
Steve Naroff699a07d2009-09-25 21:32:34 +0000561static Decl *getDeclFromExpr(Stmt *E) {
562 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
563 return RefExpr->getDecl();
564 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
565 return ME->getMemberDecl();
566 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
567 return RE->getDecl();
568
569 if (CallExpr *CE = dyn_cast<CallExpr>(E))
570 return getDeclFromExpr(CE->getCallee());
571 if (CastExpr *CE = dyn_cast<CastExpr>(E))
572 return getDeclFromExpr(CE->getSubExpr());
573 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
574 return OME->getMethodDecl();
575
576 return 0;
577}
578
Steve Naroff9efa7672009-09-04 15:44:05 +0000579CXDecl clang_getCursorDecl(CXCursor C)
580{
Steve Naroff699a07d2009-09-25 21:32:34 +0000581 if (clang_isDeclaration(C.kind))
582 return C.decl;
583
584 if (clang_isReference(C.kind)) {
585 if (C.stmt)
586 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
587 else
588 return C.decl;
589 }
590 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000591}
592
Steve Narofff334b4e2009-09-02 18:26:48 +0000593static SourceLocation getLocationFromCursor(CXCursor C,
594 SourceManager &SourceMgr,
595 NamedDecl *ND) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000596 if (clang_isReference(C.kind)) {
597 switch (C.kind) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000598 case CXCursor_ObjCSuperClassRef:
Steve Naroff1164d852009-09-02 18:58:52 +0000599 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000600 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
601 assert(OID && "clang_getCursorLine(): Missing interface decl");
Steve Naroff1164d852009-09-02 18:58:52 +0000602 return OID->getSuperClassLoc();
603 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000604 case CXCursor_ObjCProtocolRef:
605 {
606 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
607 assert(OID && "clang_getCursorLine(): Missing protocol decl");
608 return OID->getLocation();
609 }
Steve Narofffb570422009-09-22 19:25:29 +0000610 case CXCursor_ObjCSelectorRef:
611 {
612 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
613 static_cast<Stmt *>(C.stmt));
614 assert(OME && "clang_getCursorLine(): Missing message expr");
615 return OME->getLeftLoc(); /* FIXME: should be a range */
616 }
617 case CXCursor_VarRef:
618 case CXCursor_FunctionRef:
619 case CXCursor_EnumConstantRef:
620 {
621 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
622 static_cast<Stmt *>(C.stmt));
623 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
624 return DRE->getLocation();
625 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000626 default:
Steve Naroff1164d852009-09-02 18:58:52 +0000627 return SourceLocation();
Steve Narofff334b4e2009-09-02 18:26:48 +0000628 }
629 } else { // We have a declaration or a definition.
Steve Naroff9efa7672009-09-04 15:44:05 +0000630 SourceLocation SLoc;
631 switch (ND->getKind()) {
632 case Decl::ObjCInterface:
633 {
634 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
635 break;
636 }
637 case Decl::ObjCProtocol:
638 {
639 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
640 break;
641 }
642 default:
643 {
644 SLoc = ND->getLocation();
645 break;
646 }
647 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000648 if (SLoc.isInvalid())
649 return SourceLocation();
Steve Naroff1164d852009-09-02 18:58:52 +0000650 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
Steve Narofff334b4e2009-09-02 18:26:48 +0000651 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000652}
653
Steve Naroff2d4d6292009-08-31 14:26:51 +0000654unsigned clang_getCursorLine(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000655{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000656 assert(C.decl && "CXCursor has null decl");
657 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000658 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000659
660 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000661 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000662}
Steve Narofff334b4e2009-09-02 18:26:48 +0000663
Steve Naroff2d4d6292009-08-31 14:26:51 +0000664unsigned clang_getCursorColumn(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000665{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000666 assert(C.decl && "CXCursor has null decl");
667 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000668 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000669
670 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000671 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000672}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000673const char *clang_getCursorSource(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000674{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000675 assert(C.decl && "CXCursor has null decl");
676 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000677 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000678
679 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000680 return SourceMgr.getBufferName(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000681}
682
Steve Naroff4ade6d62009-09-23 17:52:52 +0000683void clang_getDefinitionSpellingAndExtent(CXCursor C,
684 const char **startBuf,
685 const char **endBuf,
686 unsigned *startLine,
687 unsigned *startColumn,
688 unsigned *endLine,
689 unsigned *endColumn)
690{
691 assert(C.decl && "CXCursor has null decl");
692 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
693 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
694 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
695
696 SourceManager &SM = FD->getASTContext().getSourceManager();
697 *startBuf = SM.getCharacterData(Body->getLBracLoc());
698 *endBuf = SM.getCharacterData(Body->getRBracLoc());
699 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
700 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
701 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
702 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
703}
704
705
Steve Naroff600866c2009-08-27 19:51:58 +0000706} // end extern "C"