blob: 9ef51b9db9fa4c415a000d7b3664d170e4935eec [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 Naroff50398192009-08-28 15:28:48 +000017#include "clang/AST/DeclVisitor.h"
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000018#include "clang/Basic/FileManager.h"
19#include "clang/Frontend/ASTUnit.h"
20#include <cstdio>
Steve Naroff50398192009-08-28 15:28:48 +000021using namespace clang;
22using namespace idx;
23
Steve Naroff89922f82009-08-31 00:59:03 +000024namespace {
25
26// Translation Unit Visitor.
27class TUVisitor : public DeclVisitor<TUVisitor> {
28 CXTranslationUnit TUnit;
29 CXTranslationUnitIterator Callback;
30public:
31 TUVisitor(CXTranslationUnit CTU, CXTranslationUnitIterator cback) :
32 TUnit(CTU), Callback(cback) {}
33
34 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
35 VisitDeclContext(dyn_cast<DeclContext>(D));
36 }
37 void VisitDeclContext(DeclContext *DC) {
38 for (DeclContext::decl_iterator
39 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
40 Visit(*I);
41 }
42 void VisitTypedefDecl(TypedefDecl *ND) {
43 CXCursor C = { CXCursor_TypedefDecl, ND };
44 Callback(TUnit, C);
45 }
46 void VisitTagDecl(TagDecl *ND) {
47 CXCursor C = { ND->isEnum() ? CXCursor_EnumDecl : CXCursor_RecordDecl, ND };
48 Callback(TUnit, C);
49 }
50 void VisitFunctionDecl(FunctionDecl *ND) {
51 CXCursor C = { CXCursor_FunctionDecl, ND };
52 Callback(TUnit, C);
53 }
54 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
55 CXCursor C = { CXCursor_ObjCInterfaceDecl, ND };
56 Callback(TUnit, C);
57 }
58 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
59 CXCursor C = { CXCursor_ObjCCategoryDecl, ND };
60 Callback(TUnit, C);
61 }
62 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
63 CXCursor C = { CXCursor_ObjCProtocolDecl, ND };
64 Callback(TUnit, C);
65 }
66};
67
68// Top-level declaration visitor.
69class TLDeclVisitor : public DeclVisitor<TLDeclVisitor> {
70public:
71 void VisitDeclContext(DeclContext *DC) {
72 for (DeclContext::decl_iterator
73 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
74 Visit(*I);
75 }
76 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
77 }
78 void VisitFieldDecl(FieldDecl *ND) {
79 }
80 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
81 }
82};
83
84}
85
Steve Naroff600866c2009-08-27 19:51:58 +000086extern "C" {
Ted Kremenekd2fa5662009-08-26 22:36:44 +000087
Steve Naroff600866c2009-08-27 19:51:58 +000088CXIndex clang_createIndex()
Steve Naroff50398192009-08-28 15:28:48 +000089{
90 return new Indexer(*new Program(), *new FileManager());
Steve Naroff600866c2009-08-27 19:51:58 +000091}
92
Steve Naroff50398192009-08-28 15:28:48 +000093// FIXME: need to pass back error info.
94CXTranslationUnit clang_createTranslationUnit(
95 CXIndex CIdx, const char *ast_filename)
Steve Naroff600866c2009-08-27 19:51:58 +000096{
Steve Naroff50398192009-08-28 15:28:48 +000097 assert(CIdx && "Passed null CXIndex");
98 Indexer *CXXIdx = static_cast<Indexer *>(CIdx);
99 std::string astName(ast_filename);
100 std::string ErrMsg;
101
102 return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getFileManager(), &ErrMsg);
Steve Naroff600866c2009-08-27 19:51:58 +0000103}
104
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000105
Steve Naroff600866c2009-08-27 19:51:58 +0000106void clang_loadTranslationUnit(
Steve Naroff89922f82009-08-31 00:59:03 +0000107 CXTranslationUnit CTUnit, CXTranslationUnitIterator callback)
Steve Naroff600866c2009-08-27 19:51:58 +0000108{
Steve Naroff50398192009-08-28 15:28:48 +0000109 assert(CTUnit && "Passed null CXTranslationUnit");
110 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
111 ASTContext &Ctx = CXXUnit->getASTContext();
112
Steve Naroff89922f82009-08-31 00:59:03 +0000113 TUVisitor DVisit(CTUnit, callback);
Steve Naroff50398192009-08-28 15:28:48 +0000114 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000115}
116
Steve Naroff89922f82009-08-31 00:59:03 +0000117void clang_loadDeclaration(CXDecl, CXDeclIterator)
Steve Naroff600866c2009-08-27 19:51:58 +0000118{
119}
120
Steve Naroff7e8f8182009-08-28 12:07:44 +0000121// Some notes on CXEntity:
122//
123// - Since the 'ordinary' namespace includes functions, data, typedefs,
124// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
125// entity for 2 different types). For example:
126//
127// module1.m: @interface Foo @end Foo *x;
128// module2.m: void Foo(int);
129//
130// - Since the unique name spans translation units, static data/functions
131// within a CXTranslationUnit are *not* currently represented by entities.
132// As a result, there will be no entity for the following:
133//
134// module.m: static void Foo() { }
135//
136
137
Steve Naroff600866c2009-08-27 19:51:58 +0000138const char *clang_getDeclarationName(CXEntity)
139{
140 return "";
141}
142const char *clang_getURI(CXEntity)
143{
144 return "";
145}
146
147CXEntity clang_getEntity(const char *URI)
148{
149 return 0;
150}
151
152//
153// CXDecl Operations.
154//
155CXCursor clang_getCursorFromDecl(CXDecl)
156{
Steve Naroff89922f82009-08-31 00:59:03 +0000157 return CXCursor();
Steve Naroff600866c2009-08-27 19:51:58 +0000158}
159CXEntity clang_getEntityFromDecl(CXDecl)
160{
161 return 0;
162}
Steve Naroff89922f82009-08-31 00:59:03 +0000163const char *clang_getDeclSpelling(CXDecl AnonDecl)
Steve Naroff600866c2009-08-27 19:51:58 +0000164{
Steve Naroff89922f82009-08-31 00:59:03 +0000165 assert(AnonDecl && "Passed null CXDecl");
166 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
167 if (ND->getIdentifier())
168 return ND->getIdentifier()->getName();
169 else
170 return "";
Steve Naroff600866c2009-08-27 19:51:58 +0000171}
Steve Naroff89922f82009-08-31 00:59:03 +0000172const char *clang_getKindSpelling(enum CXCursorKind Kind)
Steve Naroff600866c2009-08-27 19:51:58 +0000173{
Steve Naroff89922f82009-08-31 00:59:03 +0000174 switch (Kind) {
175 case CXCursor_FunctionDecl: return "FunctionDecl";
176 case CXCursor_TypedefDecl: return "TypedefDecl";
177 case CXCursor_EnumDecl: return "EnumDecl";
178 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
179 case CXCursor_RecordDecl: return "RecordDecl";
180 case CXCursor_FieldDecl: return "FieldDecl";
181 case CXCursor_VarDecl: return "VarDecl";
182 case CXCursor_ParmDecl: return "ParmDecl";
183 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
184 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
185 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
186 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
187 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
188 case CXCursor_ObjCMethodDecl: return "ObjCMethodDecl";
189 default: return "<not implemented>";
190 }
Steve Naroff600866c2009-08-27 19:51:58 +0000191}
Steve Naroff89922f82009-08-31 00:59:03 +0000192
Steve Naroff600866c2009-08-27 19:51:58 +0000193//
194// CXCursor Operations.
195//
196CXCursor clang_getCursor(CXTranslationUnit, const char *source_name,
197 unsigned line, unsigned column)
198{
Steve Naroff89922f82009-08-31 00:59:03 +0000199 return CXCursor();
Steve Naroff600866c2009-08-27 19:51:58 +0000200}
201
202CXCursorKind clang_getCursorKind(CXCursor)
203{
Steve Naroff89922f82009-08-31 00:59:03 +0000204 return CXCursor_Invalid;
Steve Naroff600866c2009-08-27 19:51:58 +0000205}
206
Steve Naroff89922f82009-08-31 00:59:03 +0000207unsigned clang_isDeclaration(enum CXCursorKind K)
208{
209 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
210}
Steve Naroff600866c2009-08-27 19:51:58 +0000211unsigned clang_getCursorLine(CXCursor)
212{
213 return 0;
214}
215unsigned clang_getCursorColumn(CXCursor)
216{
217 return 0;
218}
219const char *clang_getCursorSource(CXCursor)
220{
221 return "";
222}
223
224// If CXCursorKind == Cursor_Reference, then this will return the referenced declaration.
225// If CXCursorKind == Cursor_Declaration, then this will return the declaration.
226CXDecl clang_getCursorDecl(CXCursor)
227{
228 return 0;
229}
230
231} // end extern "C"