blob: de2b5d68987bc087daa0021b92256dd751fedf54 [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 Naroffc674c2d2009-08-31 00:59:03 +0000107typedef void (*CXTranslationUnitIterator)(CXTranslationUnit, CXCursor);
108void clang_loadTranslationUnit(CXTranslationUnit, CXTranslationUnitIterator);
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000109
110/*
111 Usage: clang_loadDeclaration(). Will load the declaration, issuing a
112 'callback' for each declaration/reference within the respective declaration.
113
114 For interface declarations, this will index the super class, protocols,
115 ivars, methods, etc. For structure declarations, this will index the fields.
116 For functions, this will index the parameters (and body, for function
117 definitions), local declarations/references.
118
119 void getInterfaceDetails(CXDecl X, CXCursor C) {
120 switch (clang_getCursorKind(C)) {
121 case Cursor_ObjC_ClassRef:
122 CXDecl SuperClass = clang_getCursorDecl(C);
123 case Cursor_ObjC_ProtocolRef:
124 CXDecl AdoptsProtocol = clang_getCursorDecl(C);
125 case Cursor_Declaration:
126 CXDecl AnIvarOrMethod = clang_getCursorDecl(C);
127 }
128 }
129 static void usage() {
130 if (clang_getDeclKind(D) == CXDecl_ObjC_interface) {
131 clang_loadDeclaration(D, getInterfaceDetails);
132 }
133 }
134*/
Steve Naroffc674c2d2009-08-31 00:59:03 +0000135typedef void (*CXDeclIterator)(CXTranslationUnit, CXDecl, void *clientData);
136
137void clang_loadDeclaration(CXDecl, CXDeclIterator);
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000138
Steve Naroff3ba83e92009-08-28 15:28:48 +0000139/*
140 * CXEntity Operations.
141 */
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000142const char *clang_getDeclarationName(CXEntity);
143const char *clang_getURI(CXEntity);
144CXEntity clang_getEntity(const char *URI);
Steve Naroff3ba83e92009-08-28 15:28:48 +0000145/*
146 * CXDecl Operations.
147 */
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000148CXCursor clang_getCursorFromDecl(CXDecl);
149CXEntity clang_getEntityFromDecl(CXDecl);
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000150const char *clang_getDeclSpelling(CXDecl);
Steve Naroff3ba83e92009-08-28 15:28:48 +0000151/*
152 * CXCursor Operations.
153 */
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000154CXCursor clang_getCursor(CXTranslationUnit, const char *source_name,
155 unsigned line, unsigned column);
156
Steve Naroff3ba83e92009-08-28 15:28:48 +0000157enum CXCursorKind clang_getCursorKind(CXCursor);
Steve Naroffc674c2d2009-08-31 00:59:03 +0000158unsigned clang_isDeclaration(enum CXCursorKind);
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000159
160unsigned clang_getCursorLine(CXCursor);
161unsigned clang_getCursorColumn(CXCursor);
162const char *clang_getCursorSource(CXCursor);
Steve Naroffc674c2d2009-08-31 00:59:03 +0000163const char *clang_getKindSpelling(enum CXCursorKind Kind);
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000164
Steve Naroff3ba83e92009-08-28 15:28:48 +0000165/*
166 * If CXCursorKind == Cursor_Reference, then this will return the referenced declaration.
167 * If CXCursorKind == Cursor_Declaration, then this will return the declaration.
168 */
Steve Naroffa6fc61b2009-08-27 19:51:58 +0000169CXDecl clang_getCursorDecl(CXCursor);
Ted Kremenek1ca68fb2009-08-26 22:36:44 +0000170
171#ifdef __cplusplus
172}
173#endif
174#endif
175