blob: 1efb50fab5cc7b6e3b21a4e8b1e87224e4f95849 [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 Naroffc857ea42009-09-02 13:28:54 +000020#include "clang/AST/Decl.h"
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000021#include "clang/Basic/FileManager.h"
Steve Naroff2d4d6292009-08-31 14:26:51 +000022#include "clang/Basic/SourceManager.h"
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000023#include "clang/Frontend/ASTUnit.h"
24#include <cstdio>
Steve Naroff50398192009-08-28 15:28:48 +000025using namespace clang;
26using namespace idx;
27
Steve Naroff89922f82009-08-31 00:59:03 +000028namespace {
29
30// Translation Unit Visitor.
31class TUVisitor : public DeclVisitor<TUVisitor> {
32 CXTranslationUnit TUnit;
33 CXTranslationUnitIterator Callback;
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000034 CXClientData CData;
35
36 void Call(enum CXCursorKind CK, NamedDecl *ND) {
37 CXCursor C = { CK, ND };
38 Callback(TUnit, C, CData);
39 }
Steve Naroff89922f82009-08-31 00:59:03 +000040public:
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000041 TUVisitor(CXTranslationUnit CTU,
42 CXTranslationUnitIterator cback, CXClientData D) :
43 TUnit(CTU), Callback(cback), CData(D) {}
Steve Naroff89922f82009-08-31 00:59:03 +000044
45 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
46 VisitDeclContext(dyn_cast<DeclContext>(D));
47 }
48 void VisitDeclContext(DeclContext *DC) {
49 for (DeclContext::decl_iterator
50 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
51 Visit(*I);
52 }
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000053 void VisitTypedefDecl(TypedefDecl *ND) {
54 Call(CXCursor_TypedefDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +000055 }
56 void VisitTagDecl(TagDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +000057 switch (ND->getTagKind()) {
58 case TagDecl::TK_struct:
59 Call(CXCursor_StructDecl, ND);
60 break;
61 case TagDecl::TK_class:
62 Call(CXCursor_ClassDecl, ND);
63 break;
64 case TagDecl::TK_union:
65 Call(CXCursor_UnionDecl, ND);
66 break;
67 case TagDecl::TK_enum:
68 Call(CXCursor_EnumDecl, ND);
69 break;
70 }
Steve Naroff89922f82009-08-31 00:59:03 +000071 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +000072 void VisitVarDecl(VarDecl *ND) {
73 Call(CXCursor_VarDecl, ND);
74 }
Steve Naroff89922f82009-08-31 00:59:03 +000075 void VisitFunctionDecl(FunctionDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +000076 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
77 : CXCursor_FunctionDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +000078 }
79 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000080 Call(CXCursor_ObjCInterfaceDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +000081 }
82 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000083 Call(CXCursor_ObjCCategoryDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +000084 }
85 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000086 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +000087 }
Steve Naroffc857ea42009-09-02 13:28:54 +000088 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
89 Call(CXCursor_ObjCClassDefn, ND);
90 }
91 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
92 Call(CXCursor_ObjCCategoryDefn, ND);
93 }
Steve Naroff89922f82009-08-31 00:59:03 +000094};
95
Steve Naroffc857ea42009-09-02 13:28:54 +000096// Declaration visitor.
97class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
98 CXDecl CDecl;
99 CXDeclIterator Callback;
100 CXClientData CData;
101
102 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000103 // Disable the callback when the context is equal to the visiting decl.
104 if (CDecl == ND && !clang_isReference(CK))
105 return;
Steve Naroffc857ea42009-09-02 13:28:54 +0000106 CXCursor C = { CK, ND };
107 Callback(CDecl, C, CData);
108 }
Steve Naroff89922f82009-08-31 00:59:03 +0000109public:
Steve Naroffc857ea42009-09-02 13:28:54 +0000110 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
111 CDecl(C), Callback(cback), CData(D) {}
112
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000113 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
114 // Issue callbacks for the containing class.
115 Call(CXCursor_ObjCClassRef, ND);
116 // FIXME: Issue callbacks for protocol refs.
117 VisitDeclContext(dyn_cast<DeclContext>(ND));
118 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000119 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000120 // Issue callbacks for super class.
Steve Narofff334b4e2009-09-02 18:26:48 +0000121 if (D->getSuperClass())
122 Call(CXCursor_ObjCSuperClassRef, D);
123
Steve Naroff9efa7672009-09-04 15:44:05 +0000124 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
125 E = D->protocol_end(); I != E; ++I)
126 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroffc857ea42009-09-02 13:28:54 +0000127 VisitDeclContext(dyn_cast<DeclContext>(D));
128 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000129 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
130 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
131 E = PID->protocol_end(); I != E; ++I)
132 Call(CXCursor_ObjCProtocolRef, *I);
133
134 VisitDeclContext(dyn_cast<DeclContext>(PID));
135 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000136 void VisitTagDecl(TagDecl *D) {
137 VisitDeclContext(dyn_cast<DeclContext>(D));
138 }
139 void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
140 VisitDeclContext(dyn_cast<DeclContext>(D));
141 }
142 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
143 VisitDeclContext(dyn_cast<DeclContext>(D));
144 }
Steve Naroff89922f82009-08-31 00:59:03 +0000145 void VisitDeclContext(DeclContext *DC) {
146 for (DeclContext::decl_iterator
147 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
148 Visit(*I);
149 }
150 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000151 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000152 }
153 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000154 Call(CXCursor_FieldDecl, ND);
155 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000156 void VisitVarDecl(VarDecl *ND) {
157 Call(CXCursor_VarDecl, ND);
158 }
159 void VisitParmVarDecl(ParmVarDecl *ND) {
160 Call(CXCursor_ParmDecl, ND);
161 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000162 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
163 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000164 }
165 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000166 Call(CXCursor_ObjCIvarDecl, ND);
167 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000168 void VisitFunctionDecl(FunctionDecl *ND) {
169 if (ND->isThisDeclarationADefinition()) {
170 VisitDeclContext(dyn_cast<DeclContext>(ND));
171 }
172 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000173 void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
174 if (ND->getBody()) {
175 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
176 : CXCursor_ObjCClassMethodDefn, ND);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000177 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroffc857ea42009-09-02 13:28:54 +0000178 } else
179 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
180 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000181 }
182};
183
184}
185
Steve Naroff600866c2009-08-27 19:51:58 +0000186extern "C" {
Ted Kremenekd2fa5662009-08-26 22:36:44 +0000187
Steve Naroff600866c2009-08-27 19:51:58 +0000188CXIndex clang_createIndex()
Steve Naroff50398192009-08-28 15:28:48 +0000189{
190 return new Indexer(*new Program(), *new FileManager());
Steve Naroff600866c2009-08-27 19:51:58 +0000191}
192
Steve Naroff50398192009-08-28 15:28:48 +0000193// FIXME: need to pass back error info.
194CXTranslationUnit clang_createTranslationUnit(
195 CXIndex CIdx, const char *ast_filename)
Steve Naroff600866c2009-08-27 19:51:58 +0000196{
Steve Naroff50398192009-08-28 15:28:48 +0000197 assert(CIdx && "Passed null CXIndex");
198 Indexer *CXXIdx = static_cast<Indexer *>(CIdx);
199 std::string astName(ast_filename);
200 std::string ErrMsg;
201
202 return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getFileManager(), &ErrMsg);
Steve Naroff600866c2009-08-27 19:51:58 +0000203}
204
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000205const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit)
206{
207 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000208 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
209 return CXXUnit->getOriginalSourceFileName().c_str();
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000210}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000211
Steve Naroffc857ea42009-09-02 13:28:54 +0000212void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
213 CXTranslationUnitIterator callback,
214 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000215{
Steve Naroff50398192009-08-28 15:28:48 +0000216 assert(CTUnit && "Passed null CXTranslationUnit");
217 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
218 ASTContext &Ctx = CXXUnit->getASTContext();
219
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000220 TUVisitor DVisit(CTUnit, callback, CData);
Steve Naroff50398192009-08-28 15:28:48 +0000221 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000222}
223
Steve Naroffc857ea42009-09-02 13:28:54 +0000224void clang_loadDeclaration(CXDecl Dcl,
225 CXDeclIterator callback,
226 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000227{
Steve Naroffc857ea42009-09-02 13:28:54 +0000228 assert(Dcl && "Passed null CXDecl");
229
230 CDeclVisitor DVisit(Dcl, callback, CData);
231 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000232}
233
Steve Naroff7e8f8182009-08-28 12:07:44 +0000234// Some notes on CXEntity:
235//
236// - Since the 'ordinary' namespace includes functions, data, typedefs,
237// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
238// entity for 2 different types). For example:
239//
240// module1.m: @interface Foo @end Foo *x;
241// module2.m: void Foo(int);
242//
243// - Since the unique name spans translation units, static data/functions
244// within a CXTranslationUnit are *not* currently represented by entities.
245// As a result, there will be no entity for the following:
246//
247// module.m: static void Foo() { }
248//
249
250
Steve Naroff600866c2009-08-27 19:51:58 +0000251const char *clang_getDeclarationName(CXEntity)
252{
253 return "";
254}
255const char *clang_getURI(CXEntity)
256{
257 return "";
258}
259
260CXEntity clang_getEntity(const char *URI)
261{
262 return 0;
263}
264
265//
266// CXDecl Operations.
267//
Steve Naroff600866c2009-08-27 19:51:58 +0000268CXEntity clang_getEntityFromDecl(CXDecl)
269{
270 return 0;
271}
Steve Naroff89922f82009-08-31 00:59:03 +0000272const char *clang_getDeclSpelling(CXDecl AnonDecl)
Steve Naroff600866c2009-08-27 19:51:58 +0000273{
Steve Naroff89922f82009-08-31 00:59:03 +0000274 assert(AnonDecl && "Passed null CXDecl");
275 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffc857ea42009-09-02 13:28:54 +0000276
277 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) {
278 return OMD->getSelector().getAsString().c_str();
279 }
Steve Naroff89922f82009-08-31 00:59:03 +0000280 if (ND->getIdentifier())
281 return ND->getIdentifier()->getName();
Steve Naroffc857ea42009-09-02 13:28:54 +0000282 else
Steve Naroff89922f82009-08-31 00:59:03 +0000283 return "";
Steve Naroff600866c2009-08-27 19:51:58 +0000284}
Steve Narofff334b4e2009-09-02 18:26:48 +0000285
286const char *clang_getCursorSpelling(CXCursor C)
287{
288 assert(C.decl && "CXCursor has null decl");
289 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
290
291 if (clang_isReference(C.kind)) {
292 switch (C.kind) {
Steve Naroff1164d852009-09-02 18:58:52 +0000293 case CXCursor_ObjCSuperClassRef:
294 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000295 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
296 assert(OID && "clang_getCursorLine(): Missing interface decl");
297 return OID->getSuperClass()->getIdentifier()->getName();
Steve Naroff1164d852009-09-02 18:58:52 +0000298 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000299 case CXCursor_ObjCClassRef:
300 {
301 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
302 assert(OID && "clang_getCursorLine(): Missing category decl");
303 return OID->getClassInterface()->getIdentifier()->getName();
304 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000305 case CXCursor_ObjCProtocolRef:
306 {
307 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
308 assert(OID && "clang_getCursorLine(): Missing protocol decl");
309 return OID->getIdentifier()->getName();
310 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000311 default:
312 return "<not implemented>";
313 }
314 }
315 return clang_getDeclSpelling(C.decl);
316}
317
318const char *clang_getCursorKindSpelling(enum CXCursorKind Kind)
Steve Naroff600866c2009-08-27 19:51:58 +0000319{
Steve Naroff89922f82009-08-31 00:59:03 +0000320 switch (Kind) {
321 case CXCursor_FunctionDecl: return "FunctionDecl";
322 case CXCursor_TypedefDecl: return "TypedefDecl";
323 case CXCursor_EnumDecl: return "EnumDecl";
324 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000325 case CXCursor_StructDecl: return "StructDecl";
326 case CXCursor_UnionDecl: return "UnionDecl";
327 case CXCursor_ClassDecl: return "ClassDecl";
Steve Naroff89922f82009-08-31 00:59:03 +0000328 case CXCursor_FieldDecl: return "FieldDecl";
329 case CXCursor_VarDecl: return "VarDecl";
330 case CXCursor_ParmDecl: return "ParmDecl";
331 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
332 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
333 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
334 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
335 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000336 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
337 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
338 case CXCursor_FunctionDefn: return "FunctionDefn";
339 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
340 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
341 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
342 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Steve Narofff334b4e2009-09-02 18:26:48 +0000343 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
Steve Naroff9efa7672009-09-04 15:44:05 +0000344 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000345 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Steve Naroff77128dd2009-09-15 20:25:34 +0000346 case CXCursor_InvalidFile: return "InvalidFile";
347 case CXCursor_NoDeclFound: return "NoDeclFound";
348 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff89922f82009-08-31 00:59:03 +0000349 default: return "<not implemented>";
350 }
Steve Naroff600866c2009-08-27 19:51:58 +0000351}
Steve Naroff89922f82009-08-31 00:59:03 +0000352
Steve Naroff9efa7672009-09-04 15:44:05 +0000353static enum CXCursorKind TranslateKind(Decl *D) {
354 switch (D->getKind()) {
355 case Decl::Function: return CXCursor_FunctionDecl;
356 case Decl::Typedef: return CXCursor_TypedefDecl;
357 case Decl::Enum: return CXCursor_EnumDecl;
358 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
359 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
360 case Decl::Field: return CXCursor_FieldDecl;
361 case Decl::Var: return CXCursor_VarDecl;
362 case Decl::ParmVar: return CXCursor_ParmDecl;
363 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
364 case Decl::ObjCMethod: {
365 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
366 if (MD->isInstanceMethod())
367 return CXCursor_ObjCInstanceMethodDecl;
368 return CXCursor_ObjCClassMethodDecl;
369 }
370 default: break;
371 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000372 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000373}
Steve Naroff600866c2009-08-27 19:51:58 +0000374//
375// CXCursor Operations.
376//
Steve Naroff9efa7672009-09-04 15:44:05 +0000377CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Steve Naroff600866c2009-08-27 19:51:58 +0000378 unsigned line, unsigned column)
379{
Steve Naroff9efa7672009-09-04 15:44:05 +0000380 assert(CTUnit && "Passed null CXTranslationUnit");
381 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
382
383 FileManager &FMgr = CXXUnit->getFileManager();
384 const FileEntry *File = FMgr.getFile(source_name,
Steve Naroff77128dd2009-09-15 20:25:34 +0000385 source_name+strlen(source_name));
386 if (!File) {
387 CXCursor C = { CXCursor_InvalidFile, 0 };
388 return C;
389 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000390 SourceLocation SLoc =
391 CXXUnit->getSourceManager().getLocation(File, line, column);
392
393 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc);
394
395 Decl *Dcl = ALoc.getDecl();
Steve Naroff77128dd2009-09-15 20:25:34 +0000396 if (Dcl) {
397 CXCursor C = { TranslateKind(Dcl), Dcl };
398 return C;
399 }
400 CXCursor C = { CXCursor_NoDeclFound, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000401 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000402}
403
Steve Naroff77128dd2009-09-15 20:25:34 +0000404CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
405{
406 assert(AnonDecl && "Passed null CXDecl");
407 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
408
409 CXCursor C = { TranslateKind(ND), ND };
410 return C;
411}
412
413unsigned clang_isInvalid(enum CXCursorKind K)
414{
415 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
416}
417
Steve Naroff89922f82009-08-31 00:59:03 +0000418unsigned clang_isDeclaration(enum CXCursorKind K)
419{
420 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
421}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000422
Steve Narofff334b4e2009-09-02 18:26:48 +0000423unsigned clang_isReference(enum CXCursorKind K)
424{
425 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
426}
427
428unsigned clang_isDefinition(enum CXCursorKind K)
429{
430 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
431}
432
Steve Naroff9efa7672009-09-04 15:44:05 +0000433CXCursorKind clang_getCursorKind(CXCursor C)
434{
435 return C.kind;
436}
437
438CXDecl clang_getCursorDecl(CXCursor C)
439{
440 return C.decl;
441}
442
Steve Narofff334b4e2009-09-02 18:26:48 +0000443static SourceLocation getLocationFromCursor(CXCursor C,
444 SourceManager &SourceMgr,
445 NamedDecl *ND) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000446 if (clang_isReference(C.kind)) {
447 switch (C.kind) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000448 case CXCursor_ObjCSuperClassRef:
Steve Naroff1164d852009-09-02 18:58:52 +0000449 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000450 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
451 assert(OID && "clang_getCursorLine(): Missing interface decl");
Steve Naroff1164d852009-09-02 18:58:52 +0000452 return OID->getSuperClassLoc();
453 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000454 case CXCursor_ObjCProtocolRef:
455 {
456 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
457 assert(OID && "clang_getCursorLine(): Missing protocol decl");
458 return OID->getLocation();
459 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000460 default:
Steve Naroff1164d852009-09-02 18:58:52 +0000461 return SourceLocation();
Steve Narofff334b4e2009-09-02 18:26:48 +0000462 }
463 } else { // We have a declaration or a definition.
Steve Naroff9efa7672009-09-04 15:44:05 +0000464 SourceLocation SLoc;
465 switch (ND->getKind()) {
466 case Decl::ObjCInterface:
467 {
468 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
469 break;
470 }
471 case Decl::ObjCProtocol:
472 {
473 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
474 break;
475 }
476 default:
477 {
478 SLoc = ND->getLocation();
479 break;
480 }
481 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000482 if (SLoc.isInvalid())
483 return SourceLocation();
Steve Naroff1164d852009-09-02 18:58:52 +0000484 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
Steve Narofff334b4e2009-09-02 18:26:48 +0000485 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000486}
487
Steve Naroff2d4d6292009-08-31 14:26:51 +0000488unsigned clang_getCursorLine(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000489{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000490 assert(C.decl && "CXCursor has null decl");
491 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000492 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000493
494 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000495 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000496}
Steve Narofff334b4e2009-09-02 18:26:48 +0000497
Steve Naroff2d4d6292009-08-31 14:26:51 +0000498unsigned clang_getCursorColumn(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000499{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000500 assert(C.decl && "CXCursor has null decl");
501 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000502 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000503
504 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000505 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000506}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000507const char *clang_getCursorSource(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000508{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000509 assert(C.decl && "CXCursor has null decl");
510 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000511 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000512
513 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000514 return SourceMgr.getBufferName(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000515}
516
Steve Naroff600866c2009-08-27 19:51:58 +0000517} // end extern "C"