blob: 1b9ba4c4f7c25fdd9e46ac0bff85335eb07cfa9f [file] [log] [blame]
Ted Kremenek1ca68fb2009-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 Naroffa6fc61b2009-08-27 19:51:58 +000023/*
24 Clang indeX abstractions. The backing store for the following API's will be
25 clangs PCH file (which contains AST's, or Abstract Syntax Trees). PCH files
26 are created by the following command:
27
Steve Naroff52833ba2009-08-31 14:26:51 +000028 "clang -S -Xclang -emit-pch <sourcefile.langsuffix> -o <sourcefile.ast>".
Steve Naroffa6fc61b2009-08-27 19:51:58 +000029
30 If the ast file format ends up diverging from the pch file format, we will
31 need to add a new switch (-emit-ast). For now, the contents are identical.
32
33 Naming Conventions: To avoid namespace pollution, data types are prefixed
34 with "CX" and functions are prefixed with "clang_".
35*/
Steve Naroff3ba83e92009-08-28 15:28:48 +000036typedef void *CXIndex; /* An indexing instance. */
Steve Naroffa6fc61b2009-08-27 19:51:58 +000037
Steve Naroff3ba83e92009-08-28 15:28:48 +000038typedef void *CXTranslationUnit; /* A translation unit instance. */
Steve Naroffa6fc61b2009-08-27 19:51:58 +000039
Steve Naroff3ba83e92009-08-28 15:28:48 +000040typedef void *CXDecl; /* A specific declaration within a translation unit. */
Steve Naroffa6fc61b2009-08-27 19:51:58 +000041
Steve Naroffc674c2d2009-08-31 00:59:03 +000042/* Cursors represent declarations and references (provides line/column info). */
43enum CXCursorKind {
44 CXCursor_Invalid = 0,
45
46 /* Declarations */
47 CXCursor_FirstDecl = 1,
48 CXCursor_TypedefDecl = 1,
49 CXCursor_EnumDecl = 2,
50 CXCursor_EnumConstantDecl = 3,
51 CXCursor_RecordDecl = 4, /* struct/union/class */
52 CXCursor_FieldDecl = 5,
53 CXCursor_FunctionDecl = 6,
54 CXCursor_VarDecl = 7,
55 CXCursor_ParmDecl = 8,
56 CXCursor_ObjCInterfaceDecl = 9,
57 CXCursor_ObjCCategoryDecl = 10,
58 CXCursor_ObjCProtocolDecl = 11,
59 CXCursor_ObjCPropertyDecl = 12,
60 CXCursor_ObjCIvarDecl = 13,
61 CXCursor_ObjCMethodDecl = 14,
62 CXCursor_LastDecl = 14,
63
64 /* References */
65 CXCursor_FirstRef = 19,
66 CXCursor_ObjCClassRef = 19,
67 CXCursor_ObjCProtocolRef = 20,
68 CXCursor_ObjCMessageRef = 21,
69 CXCursor_ObjCSelectorRef = 22,
70 CXCursor_LastRef = 23
Steve Naroffa6fc61b2009-08-27 19:51:58 +000071};
72
Steve Naroffc674c2d2009-08-31 00:59:03 +000073/* A cursor into the CXTranslationUnit. */
74typedef struct {
75 enum CXCursorKind kind;
76 CXDecl decl;
77
78 /* FIXME: Handle references. */
79} CXCursor;
80
Steve Naroff3ba83e92009-08-28 15:28:48 +000081/* A unique token for looking up "visible" CXDecls from a CXTranslationUnit. */
Steve Naroffa6fc61b2009-08-27 19:51:58 +000082typedef void *CXEntity;
83
84CXIndex clang_createIndex();
85
Steve Naroff3ba83e92009-08-28 15:28:48 +000086CXTranslationUnit clang_createTranslationUnit(
Steve Naroffa6fc61b2009-08-27 19:51:58 +000087 CXIndex, const char *ast_filename
88);
89
90/*
91 Usage: clang_loadTranslationUnit(). Will load the toplevel declarations
92 within a translation unit, issuing a 'callback' for each one.
93
94 void printObjCInterfaceNames(CXTranslationUnit X, CXCursor C) {
95 if (clang_getCursorKind(C) == Cursor_Declaration) {
96 CXDecl D = clang_getCursorDecl(C);
97 if (clang_getDeclKind(D) == CXDecl_ObjC_interface)
98 printf("@interface %s in file %s on line %d column %d\n",
99 clang_getDeclSpelling(D), clang_getCursorSource(C),
100 clang_getCursorLine(C), clang_getCursorColumn(C));
101 }
102 }
103 static void usage {
104 clang_loadTranslationUnit(CXTranslationUnit, printObjCInterfaceNames);
105 }
106*/
Steve Naroffb836cb72009-09-01 15:55:40 +0000107typedef void *CXClientData;
108typedef void (*CXTranslationUnitIterator)(CXTranslationUnit, CXCursor,
109 CXClientData);
110void clang_loadTranslationUnit(CXTranslationUnit, CXTranslationUnitIterator,
111 CXClientData);
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000112
113/*
114 Usage: clang_loadDeclaration(). Will load the declaration, issuing a
115 'callback' for each declaration/reference within the respective declaration.
116
117 For interface declarations, this will index the super class, protocols,
118 ivars, methods, etc. For structure declarations, this will index the fields.
119 For functions, this will index the parameters (and body, for function
120 definitions), local declarations/references.
121
122 void getInterfaceDetails(CXDecl X, CXCursor C) {
123 switch (clang_getCursorKind(C)) {
124 case Cursor_ObjC_ClassRef:
125 CXDecl SuperClass = clang_getCursorDecl(C);
126 case Cursor_ObjC_ProtocolRef:
127 CXDecl AdoptsProtocol = clang_getCursorDecl(C);
128 case Cursor_Declaration:
129 CXDecl AnIvarOrMethod = clang_getCursorDecl(C);
130 }
131 }
132 static void usage() {
133 if (clang_getDeclKind(D) == CXDecl_ObjC_interface) {
134 clang_loadDeclaration(D, getInterfaceDetails);
135 }
136 }
137*/
Steve Naroffc674c2d2009-08-31 00:59:03 +0000138typedef void (*CXDeclIterator)(CXTranslationUnit, CXDecl, void *clientData);
139
140void clang_loadDeclaration(CXDecl, CXDeclIterator);
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000141
Steve Naroff3ba83e92009-08-28 15:28:48 +0000142/*
143 * CXEntity Operations.
144 */
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000145const char *clang_getDeclarationName(CXEntity);
146const char *clang_getURI(CXEntity);
147CXEntity clang_getEntity(const char *URI);
Steve Naroff3ba83e92009-08-28 15:28:48 +0000148/*
149 * CXDecl Operations.
150 */
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000151CXCursor clang_getCursorFromDecl(CXDecl);
152CXEntity clang_getEntityFromDecl(CXDecl);
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000153const char *clang_getDeclSpelling(CXDecl);
Steve Naroff3ba83e92009-08-28 15:28:48 +0000154/*
155 * CXCursor Operations.
156 */
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000157CXCursor clang_getCursor(CXTranslationUnit, const char *source_name,
158 unsigned line, unsigned column);
159
Steve Naroff3ba83e92009-08-28 15:28:48 +0000160enum CXCursorKind clang_getCursorKind(CXCursor);
Steve Naroffc674c2d2009-08-31 00:59:03 +0000161unsigned clang_isDeclaration(enum CXCursorKind);
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000162
163unsigned clang_getCursorLine(CXCursor);
164unsigned clang_getCursorColumn(CXCursor);
165const char *clang_getCursorSource(CXCursor);
Steve Naroffc674c2d2009-08-31 00:59:03 +0000166const char *clang_getKindSpelling(enum CXCursorKind Kind);
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000167
Steve Naroff3ba83e92009-08-28 15:28:48 +0000168/*
169 * If CXCursorKind == Cursor_Reference, then this will return the referenced declaration.
170 * If CXCursorKind == Cursor_Declaration, then this will return the declaration.
171 */
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000172CXDecl clang_getCursorDecl(CXCursor);
Ted Kremenek1ca68fb2009-08-26 22:36:44 +0000173
174#ifdef __cplusplus
175}
176#endif
177#endif
178