blob: d0c1ea5c6711659aa7a92ad6812d008ecc8a1e01 [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 Narofffb570422009-09-22 19:25:29 +000031class CRefVisitor : public StmtVisitor<CRefVisitor> {
32 CXDecl CDecl;
33 CXDeclIterator Callback;
34 CXClientData CData;
35
36 void Call(enum CXCursorKind CK, Stmt *SRef) {
37 CXCursor C = { CK, CDecl, SRef };
38 Callback(CDecl, C, CData);
39 }
40
41public:
42 CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
43 CDecl(C), Callback(cback), CData(D) {}
44
45 void VisitStmt(Stmt *S) {
46 for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
47 C != CEnd; ++C)
48 Visit(*C);
49 }
50 void VisitDeclRefExpr(DeclRefExpr *Node) {
51 NamedDecl *D = Node->getDecl();
52 if (isa<VarDecl>(D))
53 Call(CXCursor_VarRef, Node);
54 else if (isa<FunctionDecl>(D))
55 Call(CXCursor_FunctionRef, Node);
56 else if (isa<EnumConstantDecl>(D))
57 Call(CXCursor_EnumConstantRef, Node);
58 }
59 void VisitMemberExpr(MemberExpr *Node) {
60 Call(CXCursor_MemberRef, Node);
61 }
62 void VisitObjCMessageExpr(ObjCMessageExpr *Node) {
63 Call(CXCursor_ObjCSelectorRef, Node);
64 }
65 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
66 Call(CXCursor_ObjCIvarRef, Node);
67 }
68};
69
Steve Naroff89922f82009-08-31 00:59:03 +000070// Translation Unit Visitor.
71class TUVisitor : public DeclVisitor<TUVisitor> {
72 CXTranslationUnit TUnit;
73 CXTranslationUnitIterator Callback;
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000074 CXClientData CData;
75
76 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Narofffb570422009-09-22 19:25:29 +000077 CXCursor C = { CK, ND, 0 };
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000078 Callback(TUnit, C, CData);
79 }
Steve Naroff89922f82009-08-31 00:59:03 +000080public:
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000081 TUVisitor(CXTranslationUnit CTU,
82 CXTranslationUnitIterator cback, CXClientData D) :
83 TUnit(CTU), Callback(cback), CData(D) {}
Steve Naroff89922f82009-08-31 00:59:03 +000084
85 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
86 VisitDeclContext(dyn_cast<DeclContext>(D));
87 }
88 void VisitDeclContext(DeclContext *DC) {
89 for (DeclContext::decl_iterator
90 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
91 Visit(*I);
92 }
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000093 void VisitTypedefDecl(TypedefDecl *ND) {
94 Call(CXCursor_TypedefDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +000095 }
96 void VisitTagDecl(TagDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +000097 switch (ND->getTagKind()) {
98 case TagDecl::TK_struct:
99 Call(CXCursor_StructDecl, ND);
100 break;
101 case TagDecl::TK_class:
102 Call(CXCursor_ClassDecl, ND);
103 break;
104 case TagDecl::TK_union:
105 Call(CXCursor_UnionDecl, ND);
106 break;
107 case TagDecl::TK_enum:
108 Call(CXCursor_EnumDecl, ND);
109 break;
110 }
Steve Naroff89922f82009-08-31 00:59:03 +0000111 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000112 void VisitVarDecl(VarDecl *ND) {
113 Call(CXCursor_VarDecl, ND);
114 }
Steve Naroff89922f82009-08-31 00:59:03 +0000115 void VisitFunctionDecl(FunctionDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000116 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
117 : CXCursor_FunctionDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000118 }
119 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000120 Call(CXCursor_ObjCInterfaceDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000121 }
122 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000123 Call(CXCursor_ObjCCategoryDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000124 }
125 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000126 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000127 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000128 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
129 Call(CXCursor_ObjCClassDefn, ND);
130 }
131 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
132 Call(CXCursor_ObjCCategoryDefn, ND);
133 }
Steve Naroff89922f82009-08-31 00:59:03 +0000134};
135
Steve Naroffc857ea42009-09-02 13:28:54 +0000136// Declaration visitor.
137class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
138 CXDecl CDecl;
139 CXDeclIterator Callback;
140 CXClientData CData;
141
142 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000143 // Disable the callback when the context is equal to the visiting decl.
144 if (CDecl == ND && !clang_isReference(CK))
145 return;
Steve Narofffb570422009-09-22 19:25:29 +0000146 CXCursor C = { CK, ND, 0 };
Steve Naroffc857ea42009-09-02 13:28:54 +0000147 Callback(CDecl, C, CData);
148 }
Steve Naroff89922f82009-08-31 00:59:03 +0000149public:
Steve Naroffc857ea42009-09-02 13:28:54 +0000150 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
151 CDecl(C), Callback(cback), CData(D) {}
152
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000153 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
154 // Issue callbacks for the containing class.
155 Call(CXCursor_ObjCClassRef, ND);
156 // FIXME: Issue callbacks for protocol refs.
157 VisitDeclContext(dyn_cast<DeclContext>(ND));
158 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000159 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000160 // Issue callbacks for super class.
Steve Narofff334b4e2009-09-02 18:26:48 +0000161 if (D->getSuperClass())
162 Call(CXCursor_ObjCSuperClassRef, D);
163
Steve Naroff9efa7672009-09-04 15:44:05 +0000164 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
165 E = D->protocol_end(); I != E; ++I)
166 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroffc857ea42009-09-02 13:28:54 +0000167 VisitDeclContext(dyn_cast<DeclContext>(D));
168 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000169 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
170 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
171 E = PID->protocol_end(); I != E; ++I)
172 Call(CXCursor_ObjCProtocolRef, *I);
173
174 VisitDeclContext(dyn_cast<DeclContext>(PID));
175 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000176 void VisitTagDecl(TagDecl *D) {
177 VisitDeclContext(dyn_cast<DeclContext>(D));
178 }
179 void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
180 VisitDeclContext(dyn_cast<DeclContext>(D));
181 }
182 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
183 VisitDeclContext(dyn_cast<DeclContext>(D));
184 }
Steve Naroff89922f82009-08-31 00:59:03 +0000185 void VisitDeclContext(DeclContext *DC) {
186 for (DeclContext::decl_iterator
187 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
188 Visit(*I);
189 }
190 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000191 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000192 }
193 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000194 Call(CXCursor_FieldDecl, ND);
195 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000196 void VisitVarDecl(VarDecl *ND) {
197 Call(CXCursor_VarDecl, ND);
198 }
199 void VisitParmVarDecl(ParmVarDecl *ND) {
200 Call(CXCursor_ParmDecl, ND);
201 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000202 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
203 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000204 }
205 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000206 Call(CXCursor_ObjCIvarDecl, ND);
207 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000208 void VisitFunctionDecl(FunctionDecl *ND) {
209 if (ND->isThisDeclarationADefinition()) {
210 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Narofffb570422009-09-22 19:25:29 +0000211
212 CRefVisitor RVisit(CDecl, Callback, CData);
213 RVisit.Visit(ND->getBody());
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000214 }
215 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000216 void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
217 if (ND->getBody()) {
218 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
219 : CXCursor_ObjCClassMethodDefn, ND);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000220 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroffc857ea42009-09-02 13:28:54 +0000221 } else
222 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
223 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000224 }
225};
226
227}
228
Steve Naroff600866c2009-08-27 19:51:58 +0000229extern "C" {
Ted Kremenekd2fa5662009-08-26 22:36:44 +0000230
Steve Naroff600866c2009-08-27 19:51:58 +0000231CXIndex clang_createIndex()
Steve Naroff50398192009-08-28 15:28:48 +0000232{
Daniel Dunbara3907592009-09-21 03:03:22 +0000233 // FIXME: Program is leaked.
234 return new Indexer(*new Program());
Steve Naroff600866c2009-08-27 19:51:58 +0000235}
236
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000237void clang_disposeIndex(CXIndex CIdx)
238{
239 assert(CIdx && "Passed null CXIndex");
240 delete static_cast<Indexer *>(CIdx);
241}
242
Steve Naroff50398192009-08-28 15:28:48 +0000243// FIXME: need to pass back error info.
244CXTranslationUnit clang_createTranslationUnit(
245 CXIndex CIdx, const char *ast_filename)
Steve Naroff600866c2009-08-27 19:51:58 +0000246{
Steve Naroff50398192009-08-28 15:28:48 +0000247 assert(CIdx && "Passed null CXIndex");
248 Indexer *CXXIdx = static_cast<Indexer *>(CIdx);
249 std::string astName(ast_filename);
250 std::string ErrMsg;
251
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000252 return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getDiagnostics(),
253 CXXIdx->getFileManager(), &ErrMsg);
Steve Naroff600866c2009-08-27 19:51:58 +0000254}
255
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000256void clang_disposeTranslationUnit(
257 CXTranslationUnit CTUnit)
258{
259 assert(CTUnit && "Passed null CXTranslationUnit");
260 delete static_cast<ASTUnit *>(CTUnit);
261}
262
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000263const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit)
264{
265 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000266 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
267 return CXXUnit->getOriginalSourceFileName().c_str();
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000268}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000269
Steve Naroffc857ea42009-09-02 13:28:54 +0000270void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
271 CXTranslationUnitIterator callback,
272 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000273{
Steve Naroff50398192009-08-28 15:28:48 +0000274 assert(CTUnit && "Passed null CXTranslationUnit");
275 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
276 ASTContext &Ctx = CXXUnit->getASTContext();
277
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000278 TUVisitor DVisit(CTUnit, callback, CData);
Steve Naroff50398192009-08-28 15:28:48 +0000279 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000280}
281
Steve Naroffc857ea42009-09-02 13:28:54 +0000282void clang_loadDeclaration(CXDecl Dcl,
283 CXDeclIterator callback,
284 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000285{
Steve Naroffc857ea42009-09-02 13:28:54 +0000286 assert(Dcl && "Passed null CXDecl");
287
288 CDeclVisitor DVisit(Dcl, callback, CData);
289 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000290}
291
Steve Naroff7e8f8182009-08-28 12:07:44 +0000292// Some notes on CXEntity:
293//
294// - Since the 'ordinary' namespace includes functions, data, typedefs,
295// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
296// entity for 2 different types). For example:
297//
298// module1.m: @interface Foo @end Foo *x;
299// module2.m: void Foo(int);
300//
301// - Since the unique name spans translation units, static data/functions
302// within a CXTranslationUnit are *not* currently represented by entities.
303// As a result, there will be no entity for the following:
304//
305// module.m: static void Foo() { }
306//
307
308
Steve Naroff600866c2009-08-27 19:51:58 +0000309const char *clang_getDeclarationName(CXEntity)
310{
311 return "";
312}
313const char *clang_getURI(CXEntity)
314{
315 return "";
316}
317
318CXEntity clang_getEntity(const char *URI)
319{
320 return 0;
321}
322
323//
324// CXDecl Operations.
325//
Steve Naroff600866c2009-08-27 19:51:58 +0000326CXEntity clang_getEntityFromDecl(CXDecl)
327{
328 return 0;
329}
Steve Naroff89922f82009-08-31 00:59:03 +0000330const char *clang_getDeclSpelling(CXDecl AnonDecl)
Steve Naroff600866c2009-08-27 19:51:58 +0000331{
Steve Naroff89922f82009-08-31 00:59:03 +0000332 assert(AnonDecl && "Passed null CXDecl");
333 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffc857ea42009-09-02 13:28:54 +0000334
335 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) {
336 return OMD->getSelector().getAsString().c_str();
337 }
Steve Naroff89922f82009-08-31 00:59:03 +0000338 if (ND->getIdentifier())
339 return ND->getIdentifier()->getName();
Steve Naroffc857ea42009-09-02 13:28:54 +0000340 else
Steve Naroff89922f82009-08-31 00:59:03 +0000341 return "";
Steve Naroff600866c2009-08-27 19:51:58 +0000342}
Steve Narofff334b4e2009-09-02 18:26:48 +0000343
344const char *clang_getCursorSpelling(CXCursor C)
345{
346 assert(C.decl && "CXCursor has null decl");
347 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
348
349 if (clang_isReference(C.kind)) {
350 switch (C.kind) {
Steve Naroff1164d852009-09-02 18:58:52 +0000351 case CXCursor_ObjCSuperClassRef:
352 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000353 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
354 assert(OID && "clang_getCursorLine(): Missing interface decl");
355 return OID->getSuperClass()->getIdentifier()->getName();
Steve Naroff1164d852009-09-02 18:58:52 +0000356 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000357 case CXCursor_ObjCClassRef:
358 {
359 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
360 assert(OID && "clang_getCursorLine(): Missing category decl");
361 return OID->getClassInterface()->getIdentifier()->getName();
362 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000363 case CXCursor_ObjCProtocolRef:
364 {
365 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
366 assert(OID && "clang_getCursorLine(): Missing protocol decl");
367 return OID->getIdentifier()->getName();
368 }
Steve Narofffb570422009-09-22 19:25:29 +0000369 case CXCursor_ObjCSelectorRef:
370 {
371 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
372 static_cast<Stmt *>(C.stmt));
373 assert(OME && "clang_getCursorLine(): Missing message expr");
374 return OME->getSelector().getAsString().c_str();
375 }
376 case CXCursor_VarRef:
377 case CXCursor_FunctionRef:
378 case CXCursor_EnumConstantRef:
379 {
380 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
381 static_cast<Stmt *>(C.stmt));
382 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
383 return DRE->getDecl()->getIdentifier()->getName();
384 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000385 default:
386 return "<not implemented>";
387 }
388 }
389 return clang_getDeclSpelling(C.decl);
390}
391
392const char *clang_getCursorKindSpelling(enum CXCursorKind Kind)
Steve Naroff600866c2009-08-27 19:51:58 +0000393{
Steve Naroff89922f82009-08-31 00:59:03 +0000394 switch (Kind) {
395 case CXCursor_FunctionDecl: return "FunctionDecl";
396 case CXCursor_TypedefDecl: return "TypedefDecl";
397 case CXCursor_EnumDecl: return "EnumDecl";
398 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000399 case CXCursor_StructDecl: return "StructDecl";
400 case CXCursor_UnionDecl: return "UnionDecl";
401 case CXCursor_ClassDecl: return "ClassDecl";
Steve Naroff89922f82009-08-31 00:59:03 +0000402 case CXCursor_FieldDecl: return "FieldDecl";
403 case CXCursor_VarDecl: return "VarDecl";
404 case CXCursor_ParmDecl: return "ParmDecl";
405 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
406 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
407 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
408 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
409 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000410 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
411 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
412 case CXCursor_FunctionDefn: return "FunctionDefn";
413 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
414 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
415 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
416 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Steve Narofff334b4e2009-09-02 18:26:48 +0000417 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
Steve Naroff9efa7672009-09-04 15:44:05 +0000418 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000419 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Steve Narofffb570422009-09-22 19:25:29 +0000420 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
421
422 case CXCursor_VarRef: return "VarRef";
423 case CXCursor_FunctionRef: return "FunctionRef";
424 case CXCursor_EnumConstantRef: return "EnumConstantRef";
425 case CXCursor_MemberRef: return "MemberRef";
426
Steve Naroff77128dd2009-09-15 20:25:34 +0000427 case CXCursor_InvalidFile: return "InvalidFile";
428 case CXCursor_NoDeclFound: return "NoDeclFound";
429 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff89922f82009-08-31 00:59:03 +0000430 default: return "<not implemented>";
431 }
Steve Naroff600866c2009-08-27 19:51:58 +0000432}
Steve Naroff89922f82009-08-31 00:59:03 +0000433
Steve Naroff9efa7672009-09-04 15:44:05 +0000434static enum CXCursorKind TranslateKind(Decl *D) {
435 switch (D->getKind()) {
436 case Decl::Function: return CXCursor_FunctionDecl;
437 case Decl::Typedef: return CXCursor_TypedefDecl;
438 case Decl::Enum: return CXCursor_EnumDecl;
439 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
440 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
441 case Decl::Field: return CXCursor_FieldDecl;
442 case Decl::Var: return CXCursor_VarDecl;
443 case Decl::ParmVar: return CXCursor_ParmDecl;
444 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
Steve Narofffb570422009-09-22 19:25:29 +0000445 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
446 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Steve Naroff9efa7672009-09-04 15:44:05 +0000447 case Decl::ObjCMethod: {
448 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
449 if (MD->isInstanceMethod())
450 return CXCursor_ObjCInstanceMethodDecl;
451 return CXCursor_ObjCClassMethodDecl;
452 }
453 default: break;
454 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000455 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000456}
Steve Naroff600866c2009-08-27 19:51:58 +0000457//
458// CXCursor Operations.
459//
Steve Naroff9efa7672009-09-04 15:44:05 +0000460CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Steve Naroff600866c2009-08-27 19:51:58 +0000461 unsigned line, unsigned column)
462{
Steve Naroff9efa7672009-09-04 15:44:05 +0000463 assert(CTUnit && "Passed null CXTranslationUnit");
464 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
465
466 FileManager &FMgr = CXXUnit->getFileManager();
467 const FileEntry *File = FMgr.getFile(source_name,
Steve Naroff77128dd2009-09-15 20:25:34 +0000468 source_name+strlen(source_name));
469 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000470 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000471 return C;
472 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000473 SourceLocation SLoc =
474 CXXUnit->getSourceManager().getLocation(File, line, column);
475
476 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc);
477
478 Decl *Dcl = ALoc.getDecl();
Steve Naroff77128dd2009-09-15 20:25:34 +0000479 if (Dcl) {
Steve Narofffb570422009-09-22 19:25:29 +0000480 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000481 return C;
482 }
Steve Narofffb570422009-09-22 19:25:29 +0000483 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000484 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000485}
486
Steve Naroff77128dd2009-09-15 20:25:34 +0000487CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
488{
489 assert(AnonDecl && "Passed null CXDecl");
490 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
491
Steve Narofffb570422009-09-22 19:25:29 +0000492 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000493 return C;
494}
495
496unsigned clang_isInvalid(enum CXCursorKind K)
497{
498 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
499}
500
Steve Naroff89922f82009-08-31 00:59:03 +0000501unsigned clang_isDeclaration(enum CXCursorKind K)
502{
503 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
504}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000505
Steve Narofff334b4e2009-09-02 18:26:48 +0000506unsigned clang_isReference(enum CXCursorKind K)
507{
508 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
509}
510
511unsigned clang_isDefinition(enum CXCursorKind K)
512{
513 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
514}
515
Steve Naroff9efa7672009-09-04 15:44:05 +0000516CXCursorKind clang_getCursorKind(CXCursor C)
517{
518 return C.kind;
519}
520
521CXDecl clang_getCursorDecl(CXCursor C)
522{
523 return C.decl;
524}
525
Steve Narofff334b4e2009-09-02 18:26:48 +0000526static SourceLocation getLocationFromCursor(CXCursor C,
527 SourceManager &SourceMgr,
528 NamedDecl *ND) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000529 if (clang_isReference(C.kind)) {
530 switch (C.kind) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000531 case CXCursor_ObjCSuperClassRef:
Steve Naroff1164d852009-09-02 18:58:52 +0000532 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000533 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
534 assert(OID && "clang_getCursorLine(): Missing interface decl");
Steve Naroff1164d852009-09-02 18:58:52 +0000535 return OID->getSuperClassLoc();
536 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000537 case CXCursor_ObjCProtocolRef:
538 {
539 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
540 assert(OID && "clang_getCursorLine(): Missing protocol decl");
541 return OID->getLocation();
542 }
Steve Narofffb570422009-09-22 19:25:29 +0000543 case CXCursor_ObjCSelectorRef:
544 {
545 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
546 static_cast<Stmt *>(C.stmt));
547 assert(OME && "clang_getCursorLine(): Missing message expr");
548 return OME->getLeftLoc(); /* FIXME: should be a range */
549 }
550 case CXCursor_VarRef:
551 case CXCursor_FunctionRef:
552 case CXCursor_EnumConstantRef:
553 {
554 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
555 static_cast<Stmt *>(C.stmt));
556 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
557 return DRE->getLocation();
558 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000559 default:
Steve Naroff1164d852009-09-02 18:58:52 +0000560 return SourceLocation();
Steve Narofff334b4e2009-09-02 18:26:48 +0000561 }
562 } else { // We have a declaration or a definition.
Steve Naroff9efa7672009-09-04 15:44:05 +0000563 SourceLocation SLoc;
564 switch (ND->getKind()) {
565 case Decl::ObjCInterface:
566 {
567 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
568 break;
569 }
570 case Decl::ObjCProtocol:
571 {
572 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
573 break;
574 }
575 default:
576 {
577 SLoc = ND->getLocation();
578 break;
579 }
580 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000581 if (SLoc.isInvalid())
582 return SourceLocation();
Steve Naroff1164d852009-09-02 18:58:52 +0000583 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
Steve Narofff334b4e2009-09-02 18:26:48 +0000584 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000585}
586
Steve Naroff2d4d6292009-08-31 14:26:51 +0000587unsigned clang_getCursorLine(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000588{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000589 assert(C.decl && "CXCursor has null decl");
590 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000591 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000592
593 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000594 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000595}
Steve Narofff334b4e2009-09-02 18:26:48 +0000596
Steve Naroff2d4d6292009-08-31 14:26:51 +0000597unsigned clang_getCursorColumn(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000598{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000599 assert(C.decl && "CXCursor has null decl");
600 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000601 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000602
603 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000604 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000605}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000606const char *clang_getCursorSource(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000607{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000608 assert(C.decl && "CXCursor has null decl");
609 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000610 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000611
612 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000613 return SourceMgr.getBufferName(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000614}
615
Steve Naroff600866c2009-08-27 19:51:58 +0000616} // end extern "C"