blob: 0b605d512ed946787258111736b2603736456fa0 [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
Steve Naroff291065d2009-09-01 17:13:31 +000025 clangs AST file (currently based on PCH). AST files are created as follows:
Steve Naroffa6fc61b2009-08-27 19:51:58 +000026
Steve Naroff291065d2009-09-01 17:13:31 +000027 "clang -emit-ast <sourcefile.langsuffix> -o <sourcefile.ast>".
Steve Naroffa6fc61b2009-08-27 19:51:58 +000028
Steve Naroffa6fc61b2009-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 Naroff3ba83e92009-08-28 15:28:48 +000032typedef void *CXIndex; /* An indexing instance. */
Steve Naroffa6fc61b2009-08-27 19:51:58 +000033
Steve Naroff3ba83e92009-08-28 15:28:48 +000034typedef void *CXTranslationUnit; /* A translation unit instance. */
Steve Naroffa6fc61b2009-08-27 19:51:58 +000035
Steve Naroff3ba83e92009-08-28 15:28:48 +000036typedef void *CXDecl; /* A specific declaration within a translation unit. */
Steve Naroffa6fc61b2009-08-27 19:51:58 +000037
Steve Naroff51366e92009-09-02 13:28:54 +000038/* Cursors represent declarations, definitions, and references. */
Steve Naroffc674c2d2009-08-31 00:59:03 +000039enum CXCursorKind {
40 CXCursor_Invalid = 0,
41
42 /* Declarations */
43 CXCursor_FirstDecl = 1,
Steve Naroff51366e92009-09-02 13:28:54 +000044 CXCursor_TypedefDecl = 2,
45 CXCursor_StructDecl = 3,
46 CXCursor_UnionDecl = 4,
47 CXCursor_ClassDecl = 5,
48 CXCursor_EnumDecl = 6,
49 CXCursor_FieldDecl = 7,
50 CXCursor_EnumConstantDecl = 8,
51 CXCursor_FunctionDecl = 9,
52 CXCursor_VarDecl = 10,
53 CXCursor_ParmDecl = 11,
54 CXCursor_ObjCInterfaceDecl = 12,
55 CXCursor_ObjCCategoryDecl = 13,
56 CXCursor_ObjCProtocolDecl = 14,
57 CXCursor_ObjCPropertyDecl = 15,
58 CXCursor_ObjCIvarDecl = 16,
59 CXCursor_ObjCInstanceMethodDecl = 17,
60 CXCursor_ObjCClassMethodDecl = 18,
61 CXCursor_LastDecl = 18,
Steve Naroffc674c2d2009-08-31 00:59:03 +000062
Steve Naroff51366e92009-09-02 13:28:54 +000063 /* Definitions */
64 CXCursor_FirstDefn = 32,
65 CXCursor_FunctionDefn = 32,
66 CXCursor_ObjCClassDefn = 33,
67 CXCursor_ObjCCategoryDefn = 34,
68 CXCursor_ObjCInstanceMethodDefn = 35,
69 CXCursor_ObjCClassMethodDefn = 36,
70 CXCursor_LastDefn = 36,
71
Steve Naroffc674c2d2009-08-31 00:59:03 +000072 /* References */
Steve Naroff51366e92009-09-02 13:28:54 +000073 CXCursor_FirstRef = 40,
74 CXCursor_ObjCClassRef = 41,
75 CXCursor_ObjCProtocolRef = 42,
76 CXCursor_ObjCMessageRef = 43,
77 CXCursor_ObjCSelectorRef = 44,
78 CXCursor_LastRef = 44
Steve Naroffa6fc61b2009-08-27 19:51:58 +000079};
80
Steve Naroffc674c2d2009-08-31 00:59:03 +000081/* A cursor into the CXTranslationUnit. */
82typedef struct {
83 enum CXCursorKind kind;
84 CXDecl decl;
85
86 /* FIXME: Handle references. */
87} CXCursor;
88
Steve Naroff3ba83e92009-08-28 15:28:48 +000089/* A unique token for looking up "visible" CXDecls from a CXTranslationUnit. */
Steve Naroffa6fc61b2009-08-27 19:51:58 +000090typedef void *CXEntity;
91
92CXIndex clang_createIndex();
93
Steve Naroff3ba83e92009-08-28 15:28:48 +000094CXTranslationUnit clang_createTranslationUnit(
Steve Naroffa6fc61b2009-08-27 19:51:58 +000095 CXIndex, const char *ast_filename
96);
97
98/*
99 Usage: clang_loadTranslationUnit(). Will load the toplevel declarations
100 within a translation unit, issuing a 'callback' for each one.
101
102 void printObjCInterfaceNames(CXTranslationUnit X, CXCursor C) {
103 if (clang_getCursorKind(C) == Cursor_Declaration) {
104 CXDecl D = clang_getCursorDecl(C);
105 if (clang_getDeclKind(D) == CXDecl_ObjC_interface)
106 printf("@interface %s in file %s on line %d column %d\n",
107 clang_getDeclSpelling(D), clang_getCursorSource(C),
108 clang_getCursorLine(C), clang_getCursorColumn(C));
109 }
110 }
111 static void usage {
112 clang_loadTranslationUnit(CXTranslationUnit, printObjCInterfaceNames);
113 }
114*/
Steve Naroffb836cb72009-09-01 15:55:40 +0000115typedef void *CXClientData;
116typedef void (*CXTranslationUnitIterator)(CXTranslationUnit, CXCursor,
117 CXClientData);
118void clang_loadTranslationUnit(CXTranslationUnit, CXTranslationUnitIterator,
119 CXClientData);
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000120
121/*
122 Usage: clang_loadDeclaration(). Will load the declaration, issuing a
123 'callback' for each declaration/reference within the respective declaration.
124
125 For interface declarations, this will index the super class, protocols,
126 ivars, methods, etc. For structure declarations, this will index the fields.
127 For functions, this will index the parameters (and body, for function
128 definitions), local declarations/references.
129
130 void getInterfaceDetails(CXDecl X, CXCursor C) {
131 switch (clang_getCursorKind(C)) {
132 case Cursor_ObjC_ClassRef:
133 CXDecl SuperClass = clang_getCursorDecl(C);
134 case Cursor_ObjC_ProtocolRef:
135 CXDecl AdoptsProtocol = clang_getCursorDecl(C);
136 case Cursor_Declaration:
137 CXDecl AnIvarOrMethod = clang_getCursorDecl(C);
138 }
139 }
140 static void usage() {
141 if (clang_getDeclKind(D) == CXDecl_ObjC_interface) {
142 clang_loadDeclaration(D, getInterfaceDetails);
143 }
144 }
145*/
Steve Naroff51366e92009-09-02 13:28:54 +0000146typedef void (*CXDeclIterator)(CXDecl, CXCursor, CXClientData);
Steve Naroffc674c2d2009-08-31 00:59:03 +0000147
Steve Naroff51366e92009-09-02 13:28:54 +0000148void clang_loadDeclaration(CXDecl, CXDeclIterator, CXClientData);
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000149
Steve Naroff3ba83e92009-08-28 15:28:48 +0000150/*
151 * CXEntity Operations.
152 */
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000153const char *clang_getDeclarationName(CXEntity);
154const char *clang_getURI(CXEntity);
155CXEntity clang_getEntity(const char *URI);
Steve Naroff3ba83e92009-08-28 15:28:48 +0000156/*
157 * CXDecl Operations.
158 */
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000159CXCursor clang_getCursorFromDecl(CXDecl);
160CXEntity clang_getEntityFromDecl(CXDecl);
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000161const char *clang_getDeclSpelling(CXDecl);
Steve Naroff3ba83e92009-08-28 15:28:48 +0000162/*
163 * CXCursor Operations.
164 */
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000165CXCursor clang_getCursor(CXTranslationUnit, const char *source_name,
166 unsigned line, unsigned column);
167
Steve Naroff3ba83e92009-08-28 15:28:48 +0000168enum CXCursorKind clang_getCursorKind(CXCursor);
Steve Naroffc674c2d2009-08-31 00:59:03 +0000169unsigned clang_isDeclaration(enum CXCursorKind);
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000170
171unsigned clang_getCursorLine(CXCursor);
172unsigned clang_getCursorColumn(CXCursor);
173const char *clang_getCursorSource(CXCursor);
Steve Naroffc674c2d2009-08-31 00:59:03 +0000174const char *clang_getKindSpelling(enum CXCursorKind Kind);
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000175
Steve Naroff3ba83e92009-08-28 15:28:48 +0000176/*
177 * If CXCursorKind == Cursor_Reference, then this will return the referenced declaration.
178 * If CXCursorKind == Cursor_Declaration, then this will return the declaration.
179 */
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000180CXDecl clang_getCursorDecl(CXCursor);
Ted Kremenek1ca68fb2009-08-26 22:36:44 +0000181
182#ifdef __cplusplus
183}
184#endif
185#endif
186