Ted Kremenek | d2fa566 | 2009-08-26 22:36:44 +0000 | [diff] [blame] | 1 | /*===-- 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 |
| 20 | extern "C" { |
| 21 | #endif |
| 22 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 23 | /* |
| 24 | Clang indeX abstractions. The backing store for the following API's will be |
Steve Naroff | b7cd17c | 2009-09-01 17:13:31 +0000 | [diff] [blame] | 25 | clangs AST file (currently based on PCH). AST files are created as follows: |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 26 | |
Steve Naroff | b7cd17c | 2009-09-01 17:13:31 +0000 | [diff] [blame] | 27 | "clang -emit-ast <sourcefile.langsuffix> -o <sourcefile.ast>". |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 28 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 29 | Naming Conventions: To avoid namespace pollution, data types are prefixed |
| 30 | with "CX" and functions are prefixed with "clang_". |
| 31 | */ |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 32 | typedef void *CXIndex; /* An indexing instance. */ |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 33 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 34 | typedef void *CXTranslationUnit; /* A translation unit instance. */ |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 35 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 36 | typedef void *CXDecl; /* A specific declaration within a translation unit. */ |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 37 | typedef void *CXStmt; /* A specific statement within a function/method */ |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 38 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 39 | /* Cursors represent declarations, definitions, and references. */ |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 40 | enum CXCursorKind { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 41 | /* Declarations */ |
| 42 | CXCursor_FirstDecl = 1, |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 43 | 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 Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 61 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 62 | /* 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 Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 71 | /* References */ |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 72 | CXCursor_FirstRef = 40, /* Decl references */ |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 73 | CXCursor_ObjCSuperClassRef = 40, |
| 74 | CXCursor_ObjCProtocolRef = 41, |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 75 | 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 Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 84 | |
| 85 | /* Error conditions */ |
| 86 | CXCursor_FirstInvalid = 70, |
| 87 | CXCursor_InvalidFile = 70, |
| 88 | CXCursor_NoDeclFound = 71, |
| 89 | CXCursor_NotImplemented = 72, |
| 90 | CXCursor_LastInvalid = 72 |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 93 | /* A cursor into the CXTranslationUnit. */ |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 94 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 95 | typedef struct { |
| 96 | enum CXCursorKind kind; |
| 97 | CXDecl decl; |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 98 | CXStmt stmt; /* expression reference */ |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 99 | } CXCursor; |
| 100 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 101 | /* A unique token for looking up "visible" CXDecls from a CXTranslationUnit. */ |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 102 | typedef void *CXEntity; |
| 103 | |
| 104 | CXIndex clang_createIndex(); |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 105 | void clang_disposeIndex(CXIndex); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 106 | |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 107 | const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit); |
| 108 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 109 | CXTranslationUnit clang_createTranslationUnit( |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 110 | CXIndex, const char *ast_filename |
| 111 | ); |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 112 | void clang_disposeTranslationUnit(CXTranslationUnit); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 113 | |
| 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 Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 131 | typedef void *CXClientData; |
| 132 | typedef void (*CXTranslationUnitIterator)(CXTranslationUnit, CXCursor, |
| 133 | CXClientData); |
| 134 | void clang_loadTranslationUnit(CXTranslationUnit, CXTranslationUnitIterator, |
| 135 | CXClientData); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 136 | |
| 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 Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 162 | typedef void (*CXDeclIterator)(CXDecl, CXCursor, CXClientData); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 163 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 164 | void clang_loadDeclaration(CXDecl, CXDeclIterator, CXClientData); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 165 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 166 | /* |
| 167 | * CXEntity Operations. |
| 168 | */ |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 169 | const char *clang_getDeclarationName(CXEntity); |
| 170 | const char *clang_getURI(CXEntity); |
| 171 | CXEntity clang_getEntity(const char *URI); |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 172 | /* |
| 173 | * CXDecl Operations. |
| 174 | */ |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 175 | CXCursor clang_getCursorFromDecl(CXDecl); |
| 176 | CXEntity clang_getEntityFromDecl(CXDecl); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 177 | const char *clang_getDeclSpelling(CXDecl); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 178 | unsigned clang_getDeclLine(CXDecl); |
| 179 | unsigned clang_getDeclColumn(CXDecl); |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 180 | const char *clang_getDeclSource(CXDecl); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 181 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 182 | /* |
| 183 | * CXCursor Operations. |
| 184 | */ |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 185 | CXCursor clang_getCursor(CXTranslationUnit, const char *source_name, |
| 186 | unsigned line, unsigned column); |
| 187 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 188 | enum CXCursorKind clang_getCursorKind(CXCursor); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 189 | unsigned clang_isDeclaration(enum CXCursorKind); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 190 | unsigned clang_isReference(enum CXCursorKind); |
| 191 | unsigned clang_isDefinition(enum CXCursorKind); |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 192 | unsigned clang_isInvalid(enum CXCursorKind); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 193 | |
| 194 | unsigned clang_getCursorLine(CXCursor); |
| 195 | unsigned clang_getCursorColumn(CXCursor); |
| 196 | const char *clang_getCursorSource(CXCursor); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 197 | const char *clang_getCursorSpelling(CXCursor); |
| 198 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 199 | /* for debug/testing */ |
| 200 | const char *clang_getCursorKindSpelling(enum CXCursorKind Kind); |
| 201 | void clang_getDefinitionSpellingAndExtent(CXCursor, |
| 202 | const char **startBuf, |
| 203 | const char **endBuf, |
| 204 | unsigned *startLine, |
| 205 | unsigned *startColumn, |
| 206 | unsigned *endLine, |
| 207 | unsigned *endColumn); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 208 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 209 | /* |
Chris Lattner | 459789b | 2009-09-16 20:18:54 +0000 | [diff] [blame] | 210 | * If CXCursorKind == Cursor_Reference, then this will return the referenced |
| 211 | * declaration. |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 212 | * If CXCursorKind == Cursor_Declaration, then this will return the declaration. |
| 213 | */ |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 214 | CXDecl clang_getCursorDecl(CXCursor); |
Ted Kremenek | d2fa566 | 2009-08-26 22:36:44 +0000 | [diff] [blame] | 215 | |
| 216 | #ifdef __cplusplus |
| 217 | } |
| 218 | #endif |
| 219 | #endif |
| 220 | |