blob: cb870c9da4a65514035c3c62ecd4dac5f33bbab7 [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
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000515 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000516 if (ALoc.isNamedRef())
517 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000518 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000519 if (Dcl) {
520 if (Stm) {
521 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
522 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
523 return C;
524 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
525 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
526 return C;
527 }
528 // Fall through...treat as a decl, not a ref.
529 }
Steve Narofffb570422009-09-22 19:25:29 +0000530 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000531 return C;
532 }
Steve Narofffb570422009-09-22 19:25:29 +0000533 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000534 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000535}
536
Steve Naroff77128dd2009-09-15 20:25:34 +0000537CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
538{
539 assert(AnonDecl && "Passed null CXDecl");
540 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
541
Steve Narofffb570422009-09-22 19:25:29 +0000542 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000543 return C;
544}
545
546unsigned clang_isInvalid(enum CXCursorKind K)
547{
548 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
549}
550
Steve Naroff89922f82009-08-31 00:59:03 +0000551unsigned clang_isDeclaration(enum CXCursorKind K)
552{
553 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
554}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000555
Steve Narofff334b4e2009-09-02 18:26:48 +0000556unsigned clang_isReference(enum CXCursorKind K)
557{
558 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
559}
560
561unsigned clang_isDefinition(enum CXCursorKind K)
562{
563 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
564}
565
Steve Naroff9efa7672009-09-04 15:44:05 +0000566CXCursorKind clang_getCursorKind(CXCursor C)
567{
568 return C.kind;
569}
570
Steve Naroff699a07d2009-09-25 21:32:34 +0000571static Decl *getDeclFromExpr(Stmt *E) {
572 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
573 return RefExpr->getDecl();
574 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
575 return ME->getMemberDecl();
576 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
577 return RE->getDecl();
578
579 if (CallExpr *CE = dyn_cast<CallExpr>(E))
580 return getDeclFromExpr(CE->getCallee());
581 if (CastExpr *CE = dyn_cast<CastExpr>(E))
582 return getDeclFromExpr(CE->getSubExpr());
583 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
584 return OME->getMethodDecl();
585
586 return 0;
587}
588
Steve Naroff9efa7672009-09-04 15:44:05 +0000589CXDecl clang_getCursorDecl(CXCursor C)
590{
Steve Naroff699a07d2009-09-25 21:32:34 +0000591 if (clang_isDeclaration(C.kind))
592 return C.decl;
593
594 if (clang_isReference(C.kind)) {
595 if (C.stmt)
596 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
597 else
598 return C.decl;
599 }
600 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000601}
602
Steve Narofff334b4e2009-09-02 18:26:48 +0000603static SourceLocation getLocationFromCursor(CXCursor C,
604 SourceManager &SourceMgr,
605 NamedDecl *ND) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000606 if (clang_isReference(C.kind)) {
607 switch (C.kind) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000608 case CXCursor_ObjCSuperClassRef:
Steve Naroff1164d852009-09-02 18:58:52 +0000609 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000610 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
611 assert(OID && "clang_getCursorLine(): Missing interface decl");
Steve Naroff1164d852009-09-02 18:58:52 +0000612 return OID->getSuperClassLoc();
613 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000614 case CXCursor_ObjCProtocolRef:
615 {
616 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
617 assert(OID && "clang_getCursorLine(): Missing protocol decl");
618 return OID->getLocation();
619 }
Steve Narofffb570422009-09-22 19:25:29 +0000620 case CXCursor_ObjCSelectorRef:
621 {
622 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
623 static_cast<Stmt *>(C.stmt));
624 assert(OME && "clang_getCursorLine(): Missing message expr");
625 return OME->getLeftLoc(); /* FIXME: should be a range */
626 }
627 case CXCursor_VarRef:
628 case CXCursor_FunctionRef:
629 case CXCursor_EnumConstantRef:
630 {
631 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
632 static_cast<Stmt *>(C.stmt));
633 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
634 return DRE->getLocation();
635 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000636 default:
Steve Naroff1164d852009-09-02 18:58:52 +0000637 return SourceLocation();
Steve Narofff334b4e2009-09-02 18:26:48 +0000638 }
639 } else { // We have a declaration or a definition.
Steve Naroff9efa7672009-09-04 15:44:05 +0000640 SourceLocation SLoc;
641 switch (ND->getKind()) {
642 case Decl::ObjCInterface:
643 {
644 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
645 break;
646 }
647 case Decl::ObjCProtocol:
648 {
649 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
650 break;
651 }
652 default:
653 {
654 SLoc = ND->getLocation();
655 break;
656 }
657 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000658 if (SLoc.isInvalid())
659 return SourceLocation();
Steve Naroff1164d852009-09-02 18:58:52 +0000660 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
Steve Narofff334b4e2009-09-02 18:26:48 +0000661 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000662}
663
Steve Naroff2d4d6292009-08-31 14:26:51 +0000664unsigned clang_getCursorLine(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.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000672}
Steve Narofff334b4e2009-09-02 18:26:48 +0000673
Steve Naroff2d4d6292009-08-31 14:26:51 +0000674unsigned clang_getCursorColumn(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000675{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000676 assert(C.decl && "CXCursor has null decl");
677 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000678 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000679
680 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000681 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000682}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000683const char *clang_getCursorSource(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000684{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000685 assert(C.decl && "CXCursor has null decl");
686 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000687 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000688
689 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000690 return SourceMgr.getBufferName(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000691}
692
Steve Naroff4ade6d62009-09-23 17:52:52 +0000693void clang_getDefinitionSpellingAndExtent(CXCursor C,
694 const char **startBuf,
695 const char **endBuf,
696 unsigned *startLine,
697 unsigned *startColumn,
698 unsigned *endLine,
699 unsigned *endColumn)
700{
701 assert(C.decl && "CXCursor has null decl");
702 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
703 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
704 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
705
706 SourceManager &SM = FD->getASTContext().getSourceManager();
707 *startBuf = SM.getCharacterData(Body->getLBracLoc());
708 *endBuf = SM.getCharacterData(Body->getRBracLoc());
709 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
710 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
711 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
712 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
713}
714
715
Steve Naroff600866c2009-08-27 19:51:58 +0000716} // end extern "C"