blob: 3178017e45be85b0eebf9b2bab39e8bec6e492ad [file] [log] [blame]
Ted Kremenekd2fa5662009-08-26 22:36:44 +00001/*===-- clang-c/Index.h - Indexing Public C Interface -------------*- C -*-===*\
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 header provides a public inferface to a Clang library for extracting *|
11|* high-level symbol information from source files without exposing the full *|
12|* Clang C++ API. *|
13|* *|
14\*===----------------------------------------------------------------------===*/
15
16#ifndef CLANG_C_INDEX_H
17#define CLANG_C_INDEX_H
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
Steve Naroff600866c2009-08-27 19:51:58 +000023/*
24 Clang indeX abstractions. The backing store for the following API's will be
Steve Naroffb7cd17c2009-09-01 17:13:31 +000025 clangs AST file (currently based on PCH). AST files are created as follows:
Steve Naroff600866c2009-08-27 19:51:58 +000026
Steve Naroffb7cd17c2009-09-01 17:13:31 +000027 "clang -emit-ast <sourcefile.langsuffix> -o <sourcefile.ast>".
Steve Naroff600866c2009-08-27 19:51:58 +000028
Steve Naroff600866c2009-08-27 19:51:58 +000029 Naming Conventions: To avoid namespace pollution, data types are prefixed
30 with "CX" and functions are prefixed with "clang_".
31*/
Steve Naroff50398192009-08-28 15:28:48 +000032typedef void *CXIndex; /* An indexing instance. */
Steve Naroff600866c2009-08-27 19:51:58 +000033
Steve Naroff50398192009-08-28 15:28:48 +000034typedef void *CXTranslationUnit; /* A translation unit instance. */
Steve Naroff600866c2009-08-27 19:51:58 +000035
Steve Naroff50398192009-08-28 15:28:48 +000036typedef void *CXDecl; /* A specific declaration within a translation unit. */
Steve Narofffb570422009-09-22 19:25:29 +000037typedef void *CXStmt; /* A specific statement within a function/method */
Steve Naroff600866c2009-08-27 19:51:58 +000038
Steve Naroffc857ea42009-09-02 13:28:54 +000039/* Cursors represent declarations, definitions, and references. */
Steve Naroff89922f82009-08-31 00:59:03 +000040enum CXCursorKind {
Steve Naroff89922f82009-08-31 00:59:03 +000041 /* Declarations */
42 CXCursor_FirstDecl = 1,
Steve Naroffc857ea42009-09-02 13:28:54 +000043 CXCursor_TypedefDecl = 2,
44 CXCursor_StructDecl = 3,
45 CXCursor_UnionDecl = 4,
46 CXCursor_ClassDecl = 5,
47 CXCursor_EnumDecl = 6,
48 CXCursor_FieldDecl = 7,
49 CXCursor_EnumConstantDecl = 8,
50 CXCursor_FunctionDecl = 9,
51 CXCursor_VarDecl = 10,
52 CXCursor_ParmDecl = 11,
53 CXCursor_ObjCInterfaceDecl = 12,
54 CXCursor_ObjCCategoryDecl = 13,
55 CXCursor_ObjCProtocolDecl = 14,
56 CXCursor_ObjCPropertyDecl = 15,
57 CXCursor_ObjCIvarDecl = 16,
58 CXCursor_ObjCInstanceMethodDecl = 17,
59 CXCursor_ObjCClassMethodDecl = 18,
60 CXCursor_LastDecl = 18,
Steve Naroff89922f82009-08-31 00:59:03 +000061
Steve Naroffc857ea42009-09-02 13:28:54 +000062 /* Definitions */
63 CXCursor_FirstDefn = 32,
64 CXCursor_FunctionDefn = 32,
65 CXCursor_ObjCClassDefn = 33,
66 CXCursor_ObjCCategoryDefn = 34,
67 CXCursor_ObjCInstanceMethodDefn = 35,
68 CXCursor_ObjCClassMethodDefn = 36,
69 CXCursor_LastDefn = 36,
70
Steve Naroff89922f82009-08-31 00:59:03 +000071 /* References */
Steve Narofffb570422009-09-22 19:25:29 +000072 CXCursor_FirstRef = 40, /* Decl references */
Steve Narofff334b4e2009-09-02 18:26:48 +000073 CXCursor_ObjCSuperClassRef = 40,
74 CXCursor_ObjCProtocolRef = 41,
Steve Narofffb570422009-09-22 19:25:29 +000075 CXCursor_ObjCClassRef = 42,
76
77 CXCursor_ObjCSelectorRef = 43, /* Expression references */
78 CXCursor_ObjCIvarRef = 44,
79 CXCursor_VarRef = 45,
80 CXCursor_FunctionRef = 46,
81 CXCursor_EnumConstantRef = 47,
82 CXCursor_MemberRef = 48,
83 CXCursor_LastRef = 48,
Steve Naroff77128dd2009-09-15 20:25:34 +000084
85 /* Error conditions */
86 CXCursor_FirstInvalid = 70,
87 CXCursor_InvalidFile = 70,
88 CXCursor_NoDeclFound = 71,
89 CXCursor_NotImplemented = 72,
90 CXCursor_LastInvalid = 72
Steve Naroff600866c2009-08-27 19:51:58 +000091};
92
Steve Naroff89922f82009-08-31 00:59:03 +000093/* A cursor into the CXTranslationUnit. */
Steve Narofffb570422009-09-22 19:25:29 +000094
Steve Naroff89922f82009-08-31 00:59:03 +000095typedef struct {
96 enum CXCursorKind kind;
97 CXDecl decl;
Steve Narofffb570422009-09-22 19:25:29 +000098 CXStmt stmt; /* expression reference */
Steve Naroff89922f82009-08-31 00:59:03 +000099} CXCursor;
100
Steve Naroff50398192009-08-28 15:28:48 +0000101/* A unique token for looking up "visible" CXDecls from a CXTranslationUnit. */
Steve Naroff600866c2009-08-27 19:51:58 +0000102typedef void *CXEntity;
103
104CXIndex clang_createIndex();
Steve Narofffb570422009-09-22 19:25:29 +0000105void clang_disposeIndex(CXIndex);
Steve Naroff600866c2009-08-27 19:51:58 +0000106
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000107const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit);
108
Steve Naroff50398192009-08-28 15:28:48 +0000109CXTranslationUnit clang_createTranslationUnit(
Steve Naroff600866c2009-08-27 19:51:58 +0000110 CXIndex, const char *ast_filename
111);
Steve Narofffb570422009-09-22 19:25:29 +0000112void clang_disposeTranslationUnit(CXTranslationUnit);
Steve Naroff600866c2009-08-27 19:51:58 +0000113
114/*
115 Usage: clang_loadTranslationUnit(). Will load the toplevel declarations
116 within a translation unit, issuing a 'callback' for each one.
117
118 void printObjCInterfaceNames(CXTranslationUnit X, CXCursor C) {
119 if (clang_getCursorKind(C) == Cursor_Declaration) {
120 CXDecl D = clang_getCursorDecl(C);
121 if (clang_getDeclKind(D) == CXDecl_ObjC_interface)
122 printf("@interface %s in file %s on line %d column %d\n",
123 clang_getDeclSpelling(D), clang_getCursorSource(C),
124 clang_getCursorLine(C), clang_getCursorColumn(C));
125 }
126 }
127 static void usage {
128 clang_loadTranslationUnit(CXTranslationUnit, printObjCInterfaceNames);
129 }
130*/
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000131typedef void *CXClientData;
132typedef void (*CXTranslationUnitIterator)(CXTranslationUnit, CXCursor,
133 CXClientData);
134void clang_loadTranslationUnit(CXTranslationUnit, CXTranslationUnitIterator,
135 CXClientData);
Steve Naroff600866c2009-08-27 19:51:58 +0000136
137/*
138 Usage: clang_loadDeclaration(). Will load the declaration, issuing a
139 'callback' for each declaration/reference within the respective declaration.
140
141 For interface declarations, this will index the super class, protocols,
142 ivars, methods, etc. For structure declarations, this will index the fields.
143 For functions, this will index the parameters (and body, for function
144 definitions), local declarations/references.
145
146 void getInterfaceDetails(CXDecl X, CXCursor C) {
147 switch (clang_getCursorKind(C)) {
148 case Cursor_ObjC_ClassRef:
149 CXDecl SuperClass = clang_getCursorDecl(C);
150 case Cursor_ObjC_ProtocolRef:
151 CXDecl AdoptsProtocol = clang_getCursorDecl(C);
152 case Cursor_Declaration:
153 CXDecl AnIvarOrMethod = clang_getCursorDecl(C);
154 }
155 }
156 static void usage() {
157 if (clang_getDeclKind(D) == CXDecl_ObjC_interface) {
158 clang_loadDeclaration(D, getInterfaceDetails);
159 }
160 }
161*/
Steve Naroffc857ea42009-09-02 13:28:54 +0000162typedef void (*CXDeclIterator)(CXDecl, CXCursor, CXClientData);
Steve Naroff89922f82009-08-31 00:59:03 +0000163
Steve Naroffc857ea42009-09-02 13:28:54 +0000164void clang_loadDeclaration(CXDecl, CXDeclIterator, CXClientData);
Steve Naroff600866c2009-08-27 19:51:58 +0000165
Steve Naroff50398192009-08-28 15:28:48 +0000166/*
167 * CXEntity Operations.
168 */
Steve Naroff600866c2009-08-27 19:51:58 +0000169const char *clang_getDeclarationName(CXEntity);
170const char *clang_getURI(CXEntity);
171CXEntity clang_getEntity(const char *URI);
Steve Naroff50398192009-08-28 15:28:48 +0000172/*
173 * CXDecl Operations.
174 */
Steve Naroff600866c2009-08-27 19:51:58 +0000175CXCursor clang_getCursorFromDecl(CXDecl);
176CXEntity clang_getEntityFromDecl(CXDecl);
Steve Naroff600866c2009-08-27 19:51:58 +0000177const char *clang_getDeclSpelling(CXDecl);
Steve Naroff699a07d2009-09-25 21:32:34 +0000178unsigned clang_getDeclLine(CXDecl);
179unsigned clang_getDeclColumn(CXDecl);
Steve Naroffee9405e2009-09-25 21:45:39 +0000180const char *clang_getDeclSource(CXDecl);
Steve Naroff699a07d2009-09-25 21:32:34 +0000181
Steve Naroff50398192009-08-28 15:28:48 +0000182/*
183 * CXCursor Operations.
184 */
Steve Naroff600866c2009-08-27 19:51:58 +0000185CXCursor clang_getCursor(CXTranslationUnit, const char *source_name,
186 unsigned line, unsigned column);
187
Steve Naroff50398192009-08-28 15:28:48 +0000188enum CXCursorKind clang_getCursorKind(CXCursor);
Steve Naroff89922f82009-08-31 00:59:03 +0000189unsigned clang_isDeclaration(enum CXCursorKind);
Steve Narofff334b4e2009-09-02 18:26:48 +0000190unsigned clang_isReference(enum CXCursorKind);
191unsigned clang_isDefinition(enum CXCursorKind);
Steve Naroff77128dd2009-09-15 20:25:34 +0000192unsigned clang_isInvalid(enum CXCursorKind);
Steve Naroff600866c2009-08-27 19:51:58 +0000193
194unsigned clang_getCursorLine(CXCursor);
195unsigned clang_getCursorColumn(CXCursor);
196const char *clang_getCursorSource(CXCursor);
Steve Narofff334b4e2009-09-02 18:26:48 +0000197const char *clang_getCursorSpelling(CXCursor);
198
Steve Naroff4ade6d62009-09-23 17:52:52 +0000199/* for debug/testing */
200const char *clang_getCursorKindSpelling(enum CXCursorKind Kind);
201void clang_getDefinitionSpellingAndExtent(CXCursor,
202 const char **startBuf,
203 const char **endBuf,
204 unsigned *startLine,
205 unsigned *startColumn,
206 unsigned *endLine,
207 unsigned *endColumn);
Steve Naroff600866c2009-08-27 19:51:58 +0000208
Steve Naroff50398192009-08-28 15:28:48 +0000209/*
Chris Lattner459789b2009-09-16 20:18:54 +0000210 * If CXCursorKind == Cursor_Reference, then this will return the referenced
211 * declaration.
Steve Naroff50398192009-08-28 15:28:48 +0000212 * If CXCursorKind == Cursor_Declaration, then this will return the declaration.
213 */
Steve Naroff600866c2009-08-27 19:51:58 +0000214CXDecl clang_getCursorDecl(CXCursor);
Ted Kremenekd2fa5662009-08-26 22:36:44 +0000215
216#ifdef __cplusplus
217}
218#endif
219#endif
220