blob: d3e37837dff8d43e820ea9b82cb5aea51d72227f [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();
Steve Naroff74165242009-09-25 22:15:54 +0000370 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000371}
372
Steve Naroffee9405e2009-09-25 21:45:39 +0000373const char *clang_getDeclSource(CXDecl AnonDecl)
374{
375 assert(AnonDecl && "Passed null CXDecl");
376 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
377 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
378 return SourceMgr.getBufferName(ND->getLocation());
379}
380
Steve Narofff334b4e2009-09-02 18:26:48 +0000381const char *clang_getCursorSpelling(CXCursor C)
382{
383 assert(C.decl && "CXCursor has null decl");
384 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
385
386 if (clang_isReference(C.kind)) {
387 switch (C.kind) {
Steve Naroff1164d852009-09-02 18:58:52 +0000388 case CXCursor_ObjCSuperClassRef:
389 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000390 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
391 assert(OID && "clang_getCursorLine(): Missing interface decl");
392 return OID->getSuperClass()->getIdentifier()->getName();
Steve Naroff1164d852009-09-02 18:58:52 +0000393 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000394 case CXCursor_ObjCClassRef:
395 {
396 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
397 assert(OID && "clang_getCursorLine(): Missing category decl");
398 return OID->getClassInterface()->getIdentifier()->getName();
399 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000400 case CXCursor_ObjCProtocolRef:
401 {
402 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
403 assert(OID && "clang_getCursorLine(): Missing protocol decl");
404 return OID->getIdentifier()->getName();
405 }
Steve Narofffb570422009-09-22 19:25:29 +0000406 case CXCursor_ObjCSelectorRef:
407 {
408 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
409 static_cast<Stmt *>(C.stmt));
410 assert(OME && "clang_getCursorLine(): Missing message expr");
411 return OME->getSelector().getAsString().c_str();
412 }
413 case CXCursor_VarRef:
414 case CXCursor_FunctionRef:
415 case CXCursor_EnumConstantRef:
416 {
417 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
418 static_cast<Stmt *>(C.stmt));
419 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
420 return DRE->getDecl()->getIdentifier()->getName();
421 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000422 default:
423 return "<not implemented>";
424 }
425 }
426 return clang_getDeclSpelling(C.decl);
427}
428
429const char *clang_getCursorKindSpelling(enum CXCursorKind Kind)
Steve Naroff600866c2009-08-27 19:51:58 +0000430{
Steve Naroff89922f82009-08-31 00:59:03 +0000431 switch (Kind) {
432 case CXCursor_FunctionDecl: return "FunctionDecl";
433 case CXCursor_TypedefDecl: return "TypedefDecl";
434 case CXCursor_EnumDecl: return "EnumDecl";
435 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000436 case CXCursor_StructDecl: return "StructDecl";
437 case CXCursor_UnionDecl: return "UnionDecl";
438 case CXCursor_ClassDecl: return "ClassDecl";
Steve Naroff89922f82009-08-31 00:59:03 +0000439 case CXCursor_FieldDecl: return "FieldDecl";
440 case CXCursor_VarDecl: return "VarDecl";
441 case CXCursor_ParmDecl: return "ParmDecl";
442 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
443 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
444 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
445 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
446 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000447 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
448 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
449 case CXCursor_FunctionDefn: return "FunctionDefn";
450 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
451 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
452 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
453 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Steve Narofff334b4e2009-09-02 18:26:48 +0000454 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
Steve Naroff9efa7672009-09-04 15:44:05 +0000455 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000456 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Steve Narofffb570422009-09-22 19:25:29 +0000457 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
458
459 case CXCursor_VarRef: return "VarRef";
460 case CXCursor_FunctionRef: return "FunctionRef";
461 case CXCursor_EnumConstantRef: return "EnumConstantRef";
462 case CXCursor_MemberRef: return "MemberRef";
463
Steve Naroff77128dd2009-09-15 20:25:34 +0000464 case CXCursor_InvalidFile: return "InvalidFile";
465 case CXCursor_NoDeclFound: return "NoDeclFound";
466 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff89922f82009-08-31 00:59:03 +0000467 default: return "<not implemented>";
468 }
Steve Naroff600866c2009-08-27 19:51:58 +0000469}
Steve Naroff89922f82009-08-31 00:59:03 +0000470
Steve Naroff9efa7672009-09-04 15:44:05 +0000471static enum CXCursorKind TranslateKind(Decl *D) {
472 switch (D->getKind()) {
473 case Decl::Function: return CXCursor_FunctionDecl;
474 case Decl::Typedef: return CXCursor_TypedefDecl;
475 case Decl::Enum: return CXCursor_EnumDecl;
476 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
477 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
478 case Decl::Field: return CXCursor_FieldDecl;
479 case Decl::Var: return CXCursor_VarDecl;
480 case Decl::ParmVar: return CXCursor_ParmDecl;
481 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
Steve Narofffb570422009-09-22 19:25:29 +0000482 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
483 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Steve Naroff9efa7672009-09-04 15:44:05 +0000484 case Decl::ObjCMethod: {
485 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
486 if (MD->isInstanceMethod())
487 return CXCursor_ObjCInstanceMethodDecl;
488 return CXCursor_ObjCClassMethodDecl;
489 }
490 default: break;
491 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000492 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000493}
Steve Naroff600866c2009-08-27 19:51:58 +0000494//
495// CXCursor Operations.
496//
Steve Naroff9efa7672009-09-04 15:44:05 +0000497CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Steve Naroff600866c2009-08-27 19:51:58 +0000498 unsigned line, unsigned column)
499{
Steve Naroff9efa7672009-09-04 15:44:05 +0000500 assert(CTUnit && "Passed null CXTranslationUnit");
501 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
502
503 FileManager &FMgr = CXXUnit->getFileManager();
504 const FileEntry *File = FMgr.getFile(source_name,
Steve Naroff77128dd2009-09-15 20:25:34 +0000505 source_name+strlen(source_name));
506 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000507 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000508 return C;
509 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000510 SourceLocation SLoc =
511 CXXUnit->getSourceManager().getLocation(File, line, column);
512
513 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc);
514
515 Decl *Dcl = ALoc.getDecl();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000516 Stmt *Stm = ALoc.getStmt();
517 if (Dcl) {
518 if (Stm) {
519 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
520 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
521 return C;
522 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
523 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
524 return C;
525 }
526 // Fall through...treat as a decl, not a ref.
527 }
Steve Narofffb570422009-09-22 19:25:29 +0000528 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000529 return C;
530 }
Steve Narofffb570422009-09-22 19:25:29 +0000531 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000532 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000533}
534
Steve Naroff77128dd2009-09-15 20:25:34 +0000535CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
536{
537 assert(AnonDecl && "Passed null CXDecl");
538 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
539
Steve Narofffb570422009-09-22 19:25:29 +0000540 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000541 return C;
542}
543
544unsigned clang_isInvalid(enum CXCursorKind K)
545{
546 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
547}
548
Steve Naroff89922f82009-08-31 00:59:03 +0000549unsigned clang_isDeclaration(enum CXCursorKind K)
550{
551 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
552}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000553
Steve Narofff334b4e2009-09-02 18:26:48 +0000554unsigned clang_isReference(enum CXCursorKind K)
555{
556 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
557}
558
559unsigned clang_isDefinition(enum CXCursorKind K)
560{
561 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
562}
563
Steve Naroff9efa7672009-09-04 15:44:05 +0000564CXCursorKind clang_getCursorKind(CXCursor C)
565{
566 return C.kind;
567}
568
Steve Naroff699a07d2009-09-25 21:32:34 +0000569static Decl *getDeclFromExpr(Stmt *E) {
570 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
571 return RefExpr->getDecl();
572 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
573 return ME->getMemberDecl();
574 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
575 return RE->getDecl();
576
577 if (CallExpr *CE = dyn_cast<CallExpr>(E))
578 return getDeclFromExpr(CE->getCallee());
579 if (CastExpr *CE = dyn_cast<CastExpr>(E))
580 return getDeclFromExpr(CE->getSubExpr());
581 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
582 return OME->getMethodDecl();
583
584 return 0;
585}
586
Steve Naroff9efa7672009-09-04 15:44:05 +0000587CXDecl clang_getCursorDecl(CXCursor C)
588{
Steve Naroff699a07d2009-09-25 21:32:34 +0000589 if (clang_isDeclaration(C.kind))
590 return C.decl;
591
592 if (clang_isReference(C.kind)) {
593 if (C.stmt)
594 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
595 else
596 return C.decl;
597 }
598 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000599}
600
Steve Narofff334b4e2009-09-02 18:26:48 +0000601static SourceLocation getLocationFromCursor(CXCursor C,
602 SourceManager &SourceMgr,
603 NamedDecl *ND) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000604 if (clang_isReference(C.kind)) {
605 switch (C.kind) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000606 case CXCursor_ObjCSuperClassRef:
Steve Naroff1164d852009-09-02 18:58:52 +0000607 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000608 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
609 assert(OID && "clang_getCursorLine(): Missing interface decl");
Steve Naroff1164d852009-09-02 18:58:52 +0000610 return OID->getSuperClassLoc();
611 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000612 case CXCursor_ObjCProtocolRef:
613 {
614 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
615 assert(OID && "clang_getCursorLine(): Missing protocol decl");
616 return OID->getLocation();
617 }
Steve Narofffb570422009-09-22 19:25:29 +0000618 case CXCursor_ObjCSelectorRef:
619 {
620 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
621 static_cast<Stmt *>(C.stmt));
622 assert(OME && "clang_getCursorLine(): Missing message expr");
623 return OME->getLeftLoc(); /* FIXME: should be a range */
624 }
625 case CXCursor_VarRef:
626 case CXCursor_FunctionRef:
627 case CXCursor_EnumConstantRef:
628 {
629 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
630 static_cast<Stmt *>(C.stmt));
631 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
632 return DRE->getLocation();
633 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000634 default:
Steve Naroff1164d852009-09-02 18:58:52 +0000635 return SourceLocation();
Steve Narofff334b4e2009-09-02 18:26:48 +0000636 }
637 } else { // We have a declaration or a definition.
Steve Naroff9efa7672009-09-04 15:44:05 +0000638 SourceLocation SLoc;
639 switch (ND->getKind()) {
640 case Decl::ObjCInterface:
641 {
642 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
643 break;
644 }
645 case Decl::ObjCProtocol:
646 {
647 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
648 break;
649 }
650 default:
651 {
652 SLoc = ND->getLocation();
653 break;
654 }
655 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000656 if (SLoc.isInvalid())
657 return SourceLocation();
Steve Naroff1164d852009-09-02 18:58:52 +0000658 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
Steve Narofff334b4e2009-09-02 18:26:48 +0000659 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000660}
661
Steve Naroff2d4d6292009-08-31 14:26:51 +0000662unsigned clang_getCursorLine(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000663{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000664 assert(C.decl && "CXCursor has null decl");
665 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000666 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000667
668 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000669 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000670}
Steve Narofff334b4e2009-09-02 18:26:48 +0000671
Steve Naroff2d4d6292009-08-31 14:26:51 +0000672unsigned clang_getCursorColumn(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000673{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000674 assert(C.decl && "CXCursor has null decl");
675 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000676 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000677
678 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000679 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000680}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000681const char *clang_getCursorSource(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000682{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000683 assert(C.decl && "CXCursor has null decl");
684 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000685 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000686
687 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000688 return SourceMgr.getBufferName(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000689}
690
Steve Naroff4ade6d62009-09-23 17:52:52 +0000691void clang_getDefinitionSpellingAndExtent(CXCursor C,
692 const char **startBuf,
693 const char **endBuf,
694 unsigned *startLine,
695 unsigned *startColumn,
696 unsigned *endLine,
697 unsigned *endColumn)
698{
699 assert(C.decl && "CXCursor has null decl");
700 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
701 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
702 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
703
704 SourceManager &SM = FD->getASTContext().getSourceManager();
705 *startBuf = SM.getCharacterData(Body->getLBracLoc());
706 *endBuf = SM.getCharacterData(Body->getRBracLoc());
707 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
708 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
709 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
710 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
711}
712
713
Steve Naroff600866c2009-08-27 19:51:58 +0000714} // end extern "C"