Ted Kremenek | 1ca68fb | 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 | a6fc61b | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 23 | /* |
| 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 Naroff | 52833ba | 2009-08-31 14:26:51 +0000 | [diff] [blame^] | 28 | "clang -S -Xclang -emit-pch <sourcefile.langsuffix> -o <sourcefile.ast>". |
Steve Naroff | a6fc61b | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 29 | |
| 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 Naroff | 3ba83e9 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 36 | typedef void *CXIndex; /* An indexing instance. */ |
Steve Naroff | a6fc61b | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 37 | |
Steve Naroff | 3ba83e9 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 38 | typedef void *CXTranslationUnit; /* A translation unit instance. */ |
Steve Naroff | a6fc61b | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 39 | |
Steve Naroff | 3ba83e9 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 40 | typedef void *CXDecl; /* A specific declaration within a translation unit. */ |
Steve Naroff | a6fc61b | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 41 | |
Steve Naroff | c674c2d | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 42 | /* Cursors represent declarations and references (provides line/column info). */ |
| 43 | enum 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 Naroff | a6fc61b | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
Steve Naroff | c674c2d | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 73 | /* A cursor into the CXTranslationUnit. */ |
| 74 | typedef struct { |
| 75 | enum CXCursorKind kind; |
| 76 | CXDecl decl; |
| 77 | |
| 78 | /* FIXME: Handle references. */ |
| 79 | } CXCursor; |
| 80 | |
Steve Naroff | 3ba83e9 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 81 | /* A unique token for looking up "visible" CXDecls from a CXTranslationUnit. */ |
Steve Naroff | a6fc61b | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 82 | typedef void *CXEntity; |
| 83 | |
| 84 | CXIndex clang_createIndex(); |
| 85 | |
Steve Naroff | 3ba83e9 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 86 | CXTranslationUnit clang_createTranslationUnit( |
Steve Naroff | a6fc61b | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 87 | 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 Naroff | c674c2d | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 107 | typedef void (*CXTranslationUnitIterator)(CXTranslationUnit, CXCursor); |
| 108 | void clang_loadTranslationUnit(CXTranslationUnit, CXTranslationUnitIterator); |
Steve Naroff | a6fc61b | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 109 | |
| 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 Naroff | c674c2d | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 135 | typedef void (*CXDeclIterator)(CXTranslationUnit, CXDecl, void *clientData); |
| 136 | |
| 137 | void clang_loadDeclaration(CXDecl, CXDeclIterator); |
Steve Naroff | a6fc61b | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 138 | |
Steve Naroff | 3ba83e9 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 139 | /* |
| 140 | * CXEntity Operations. |
| 141 | */ |
Steve Naroff | a6fc61b | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 142 | const char *clang_getDeclarationName(CXEntity); |
| 143 | const char *clang_getURI(CXEntity); |
| 144 | CXEntity clang_getEntity(const char *URI); |
Steve Naroff | 3ba83e9 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 145 | /* |
| 146 | * CXDecl Operations. |
| 147 | */ |
Steve Naroff | a6fc61b | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 148 | CXCursor clang_getCursorFromDecl(CXDecl); |
| 149 | CXEntity clang_getEntityFromDecl(CXDecl); |
Steve Naroff | a6fc61b | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 150 | const char *clang_getDeclSpelling(CXDecl); |
Steve Naroff | 3ba83e9 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 151 | /* |
| 152 | * CXCursor Operations. |
| 153 | */ |
Steve Naroff | a6fc61b | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 154 | CXCursor clang_getCursor(CXTranslationUnit, const char *source_name, |
| 155 | unsigned line, unsigned column); |
| 156 | |
Steve Naroff | 3ba83e9 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 157 | enum CXCursorKind clang_getCursorKind(CXCursor); |
Steve Naroff | c674c2d | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 158 | unsigned clang_isDeclaration(enum CXCursorKind); |
Steve Naroff | a6fc61b | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 159 | |
| 160 | unsigned clang_getCursorLine(CXCursor); |
| 161 | unsigned clang_getCursorColumn(CXCursor); |
| 162 | const char *clang_getCursorSource(CXCursor); |
Steve Naroff | c674c2d | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 163 | const char *clang_getKindSpelling(enum CXCursorKind Kind); |
Steve Naroff | a6fc61b | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 164 | |
Steve Naroff | 3ba83e9 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 165 | /* |
| 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 Naroff | a6fc61b | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 169 | CXDecl clang_getCursorDecl(CXCursor); |
Ted Kremenek | 1ca68fb | 2009-08-26 22:36:44 +0000 | [diff] [blame] | 170 | |
| 171 | #ifdef __cplusplus |
| 172 | } |
| 173 | #endif |
| 174 | #endif |
| 175 | |