blob: 37f2d8695c2a24aba6da9900824be71780da752b [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
357const char *clang_getCursorSpelling(CXCursor C)
358{
359 assert(C.decl && "CXCursor has null decl");
360 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
361
362 if (clang_isReference(C.kind)) {
363 switch (C.kind) {
Steve Naroff1164d852009-09-02 18:58:52 +0000364 case CXCursor_ObjCSuperClassRef:
365 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000366 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
367 assert(OID && "clang_getCursorLine(): Missing interface decl");
368 return OID->getSuperClass()->getIdentifier()->getName();
Steve Naroff1164d852009-09-02 18:58:52 +0000369 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000370 case CXCursor_ObjCClassRef:
371 {
372 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
373 assert(OID && "clang_getCursorLine(): Missing category decl");
374 return OID->getClassInterface()->getIdentifier()->getName();
375 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000376 case CXCursor_ObjCProtocolRef:
377 {
378 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
379 assert(OID && "clang_getCursorLine(): Missing protocol decl");
380 return OID->getIdentifier()->getName();
381 }
Steve Narofffb570422009-09-22 19:25:29 +0000382 case CXCursor_ObjCSelectorRef:
383 {
384 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
385 static_cast<Stmt *>(C.stmt));
386 assert(OME && "clang_getCursorLine(): Missing message expr");
387 return OME->getSelector().getAsString().c_str();
388 }
389 case CXCursor_VarRef:
390 case CXCursor_FunctionRef:
391 case CXCursor_EnumConstantRef:
392 {
393 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
394 static_cast<Stmt *>(C.stmt));
395 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
396 return DRE->getDecl()->getIdentifier()->getName();
397 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000398 default:
399 return "<not implemented>";
400 }
401 }
402 return clang_getDeclSpelling(C.decl);
403}
404
405const char *clang_getCursorKindSpelling(enum CXCursorKind Kind)
Steve Naroff600866c2009-08-27 19:51:58 +0000406{
Steve Naroff89922f82009-08-31 00:59:03 +0000407 switch (Kind) {
408 case CXCursor_FunctionDecl: return "FunctionDecl";
409 case CXCursor_TypedefDecl: return "TypedefDecl";
410 case CXCursor_EnumDecl: return "EnumDecl";
411 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000412 case CXCursor_StructDecl: return "StructDecl";
413 case CXCursor_UnionDecl: return "UnionDecl";
414 case CXCursor_ClassDecl: return "ClassDecl";
Steve Naroff89922f82009-08-31 00:59:03 +0000415 case CXCursor_FieldDecl: return "FieldDecl";
416 case CXCursor_VarDecl: return "VarDecl";
417 case CXCursor_ParmDecl: return "ParmDecl";
418 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
419 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
420 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
421 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
422 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000423 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
424 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
425 case CXCursor_FunctionDefn: return "FunctionDefn";
426 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
427 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
428 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
429 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Steve Narofff334b4e2009-09-02 18:26:48 +0000430 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
Steve Naroff9efa7672009-09-04 15:44:05 +0000431 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000432 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Steve Narofffb570422009-09-22 19:25:29 +0000433 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
434
435 case CXCursor_VarRef: return "VarRef";
436 case CXCursor_FunctionRef: return "FunctionRef";
437 case CXCursor_EnumConstantRef: return "EnumConstantRef";
438 case CXCursor_MemberRef: return "MemberRef";
439
Steve Naroff77128dd2009-09-15 20:25:34 +0000440 case CXCursor_InvalidFile: return "InvalidFile";
441 case CXCursor_NoDeclFound: return "NoDeclFound";
442 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff89922f82009-08-31 00:59:03 +0000443 default: return "<not implemented>";
444 }
Steve Naroff600866c2009-08-27 19:51:58 +0000445}
Steve Naroff89922f82009-08-31 00:59:03 +0000446
Steve Naroff9efa7672009-09-04 15:44:05 +0000447static enum CXCursorKind TranslateKind(Decl *D) {
448 switch (D->getKind()) {
449 case Decl::Function: return CXCursor_FunctionDecl;
450 case Decl::Typedef: return CXCursor_TypedefDecl;
451 case Decl::Enum: return CXCursor_EnumDecl;
452 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
453 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
454 case Decl::Field: return CXCursor_FieldDecl;
455 case Decl::Var: return CXCursor_VarDecl;
456 case Decl::ParmVar: return CXCursor_ParmDecl;
457 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
Steve Narofffb570422009-09-22 19:25:29 +0000458 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
459 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Steve Naroff9efa7672009-09-04 15:44:05 +0000460 case Decl::ObjCMethod: {
461 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
462 if (MD->isInstanceMethod())
463 return CXCursor_ObjCInstanceMethodDecl;
464 return CXCursor_ObjCClassMethodDecl;
465 }
466 default: break;
467 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000468 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000469}
Steve Naroff600866c2009-08-27 19:51:58 +0000470//
471// CXCursor Operations.
472//
Steve Naroff9efa7672009-09-04 15:44:05 +0000473CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Steve Naroff600866c2009-08-27 19:51:58 +0000474 unsigned line, unsigned column)
475{
Steve Naroff9efa7672009-09-04 15:44:05 +0000476 assert(CTUnit && "Passed null CXTranslationUnit");
477 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
478
479 FileManager &FMgr = CXXUnit->getFileManager();
480 const FileEntry *File = FMgr.getFile(source_name,
Steve Naroff77128dd2009-09-15 20:25:34 +0000481 source_name+strlen(source_name));
482 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000483 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000484 return C;
485 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000486 SourceLocation SLoc =
487 CXXUnit->getSourceManager().getLocation(File, line, column);
488
489 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc);
490
491 Decl *Dcl = ALoc.getDecl();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000492 Stmt *Stm = ALoc.getStmt();
493 if (Dcl) {
494 if (Stm) {
495 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
496 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
497 return C;
498 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
499 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
500 return C;
501 }
502 // Fall through...treat as a decl, not a ref.
503 }
Steve Narofffb570422009-09-22 19:25:29 +0000504 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000505 return C;
506 }
Steve Narofffb570422009-09-22 19:25:29 +0000507 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000508 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000509}
510
Steve Naroff77128dd2009-09-15 20:25:34 +0000511CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
512{
513 assert(AnonDecl && "Passed null CXDecl");
514 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
515
Steve Narofffb570422009-09-22 19:25:29 +0000516 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000517 return C;
518}
519
520unsigned clang_isInvalid(enum CXCursorKind K)
521{
522 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
523}
524
Steve Naroff89922f82009-08-31 00:59:03 +0000525unsigned clang_isDeclaration(enum CXCursorKind K)
526{
527 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
528}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000529
Steve Narofff334b4e2009-09-02 18:26:48 +0000530unsigned clang_isReference(enum CXCursorKind K)
531{
532 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
533}
534
535unsigned clang_isDefinition(enum CXCursorKind K)
536{
537 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
538}
539
Steve Naroff9efa7672009-09-04 15:44:05 +0000540CXCursorKind clang_getCursorKind(CXCursor C)
541{
542 return C.kind;
543}
544
545CXDecl clang_getCursorDecl(CXCursor C)
546{
547 return C.decl;
548}
549
Steve Narofff334b4e2009-09-02 18:26:48 +0000550static SourceLocation getLocationFromCursor(CXCursor C,
551 SourceManager &SourceMgr,
552 NamedDecl *ND) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000553 if (clang_isReference(C.kind)) {
554 switch (C.kind) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000555 case CXCursor_ObjCSuperClassRef:
Steve Naroff1164d852009-09-02 18:58:52 +0000556 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000557 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
558 assert(OID && "clang_getCursorLine(): Missing interface decl");
Steve Naroff1164d852009-09-02 18:58:52 +0000559 return OID->getSuperClassLoc();
560 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000561 case CXCursor_ObjCProtocolRef:
562 {
563 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
564 assert(OID && "clang_getCursorLine(): Missing protocol decl");
565 return OID->getLocation();
566 }
Steve Narofffb570422009-09-22 19:25:29 +0000567 case CXCursor_ObjCSelectorRef:
568 {
569 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
570 static_cast<Stmt *>(C.stmt));
571 assert(OME && "clang_getCursorLine(): Missing message expr");
572 return OME->getLeftLoc(); /* FIXME: should be a range */
573 }
574 case CXCursor_VarRef:
575 case CXCursor_FunctionRef:
576 case CXCursor_EnumConstantRef:
577 {
578 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
579 static_cast<Stmt *>(C.stmt));
580 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
581 return DRE->getLocation();
582 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000583 default:
Steve Naroff1164d852009-09-02 18:58:52 +0000584 return SourceLocation();
Steve Narofff334b4e2009-09-02 18:26:48 +0000585 }
586 } else { // We have a declaration or a definition.
Steve Naroff9efa7672009-09-04 15:44:05 +0000587 SourceLocation SLoc;
588 switch (ND->getKind()) {
589 case Decl::ObjCInterface:
590 {
591 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
592 break;
593 }
594 case Decl::ObjCProtocol:
595 {
596 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
597 break;
598 }
599 default:
600 {
601 SLoc = ND->getLocation();
602 break;
603 }
604 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000605 if (SLoc.isInvalid())
606 return SourceLocation();
Steve Naroff1164d852009-09-02 18:58:52 +0000607 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
Steve Narofff334b4e2009-09-02 18:26:48 +0000608 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000609}
610
Steve Naroff2d4d6292009-08-31 14:26:51 +0000611unsigned clang_getCursorLine(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000612{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000613 assert(C.decl && "CXCursor has null decl");
614 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000615 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000616
617 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000618 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000619}
Steve Narofff334b4e2009-09-02 18:26:48 +0000620
Steve Naroff2d4d6292009-08-31 14:26:51 +0000621unsigned clang_getCursorColumn(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000622{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000623 assert(C.decl && "CXCursor has null decl");
624 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000625 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000626
627 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000628 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000629}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000630const char *clang_getCursorSource(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000631{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000632 assert(C.decl && "CXCursor has null decl");
633 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000634 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000635
636 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000637 return SourceMgr.getBufferName(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000638}
639
Steve Naroff4ade6d62009-09-23 17:52:52 +0000640void clang_getDefinitionSpellingAndExtent(CXCursor C,
641 const char **startBuf,
642 const char **endBuf,
643 unsigned *startLine,
644 unsigned *startColumn,
645 unsigned *endLine,
646 unsigned *endColumn)
647{
648 assert(C.decl && "CXCursor has null decl");
649 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
650 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
651 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
652
653 SourceManager &SM = FD->getASTContext().getSourceManager();
654 *startBuf = SM.getCharacterData(Body->getLBracLoc());
655 *endBuf = SM.getCharacterData(Body->getRBracLoc());
656 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
657 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
658 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
659 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
660}
661
662
Steve Naroff600866c2009-08-27 19:51:58 +0000663} // end extern "C"