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 | |
| 28 | "clang -emit-pch <sourcefile.langsuffix> -o <sourcefile.ast>". |
| 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 | */ |
| 36 | typedef void *CXIndex; // An indexing instance. |
| 37 | |
| 38 | typedef void *CXTranslationUnit; // A translation unit instance. |
| 39 | |
| 40 | typedef void *CXCursor; // An opaque cursor into the CXTranslationUnit. |
| 41 | |
| 42 | // Cursors represent declarations and references (provides line/column info). |
| 43 | enum CXCursorKind { |
Steve Naroff | 00eb51c | 2009-08-28 12:07:44 +0000 | [diff] [blame^] | 44 | CXCursor_Declaration, |
| 45 | CXCursor_Reference, |
| 46 | CXCursor_ObjC_ClassRef, |
| 47 | CXCursor_ObjC_ProtocolRef, |
| 48 | CXCursor_ObjC_MessageRef, |
| 49 | CXCursor_ObjC_SelectorRef |
Steve Naroff | a6fc61b | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | typedef void *CXDecl; // A specific declaration within a translation unit. |
| 53 | |
| 54 | enum CXDeclKind { // The various kinds of declarations. |
| 55 | CXDecl_any, |
| 56 | CXDecl_typedef, |
| 57 | CXDecl_enum, |
| 58 | CXDecl_enum_constant, |
| 59 | CXDecl_record, |
| 60 | CXDecl_field, |
| 61 | CXDecl_function, |
| 62 | CXDecl_variable, |
| 63 | CXDecl_parameter, |
| 64 | CXDecl_ObjC_interface, |
| 65 | CXDecl_ObjC_category, |
| 66 | CXDecl_ObjC_protocol, |
| 67 | CXDecl_ObjC_property, |
| 68 | CXDecl_ObjC_instance_variable, |
| 69 | CXDecl_ObjC_instance_method, |
| 70 | CXDecl_ObjC_class_method, |
| 71 | CXDecl_ObjC_category_implementation, |
| 72 | CXDecl_ObjC_class_implementation, |
| 73 | CXDecl_ObjC_property_implementation |
| 74 | }; |
| 75 | |
| 76 | // A unique token for looking up "visible" CXDecls from a CXTranslationUnit. |
| 77 | typedef void *CXEntity; |
| 78 | |
| 79 | CXIndex clang_createIndex(); |
| 80 | |
| 81 | CXTranslationUnit clang_loadTranslationUnitFromASTFile( |
| 82 | CXIndex, const char *ast_filename |
| 83 | ); |
| 84 | |
| 85 | /* |
| 86 | Usage: clang_loadTranslationUnit(). Will load the toplevel declarations |
| 87 | within a translation unit, issuing a 'callback' for each one. |
| 88 | |
| 89 | void printObjCInterfaceNames(CXTranslationUnit X, CXCursor C) { |
| 90 | if (clang_getCursorKind(C) == Cursor_Declaration) { |
| 91 | CXDecl D = clang_getCursorDecl(C); |
| 92 | if (clang_getDeclKind(D) == CXDecl_ObjC_interface) |
| 93 | printf("@interface %s in file %s on line %d column %d\n", |
| 94 | clang_getDeclSpelling(D), clang_getCursorSource(C), |
| 95 | clang_getCursorLine(C), clang_getCursorColumn(C)); |
| 96 | } |
| 97 | } |
| 98 | static void usage { |
| 99 | clang_loadTranslationUnit(CXTranslationUnit, printObjCInterfaceNames); |
| 100 | } |
| 101 | */ |
| 102 | void clang_loadTranslationUnit( |
| 103 | CXTranslationUnit, void (*callback)(CXTranslationUnit, CXCursor) |
| 104 | ); |
| 105 | |
| 106 | /* |
| 107 | Usage: clang_loadDeclaration(). Will load the declaration, issuing a |
| 108 | 'callback' for each declaration/reference within the respective declaration. |
| 109 | |
| 110 | For interface declarations, this will index the super class, protocols, |
| 111 | ivars, methods, etc. For structure declarations, this will index the fields. |
| 112 | For functions, this will index the parameters (and body, for function |
| 113 | definitions), local declarations/references. |
| 114 | |
| 115 | void getInterfaceDetails(CXDecl X, CXCursor C) { |
| 116 | switch (clang_getCursorKind(C)) { |
| 117 | case Cursor_ObjC_ClassRef: |
| 118 | CXDecl SuperClass = clang_getCursorDecl(C); |
| 119 | case Cursor_ObjC_ProtocolRef: |
| 120 | CXDecl AdoptsProtocol = clang_getCursorDecl(C); |
| 121 | case Cursor_Declaration: |
| 122 | CXDecl AnIvarOrMethod = clang_getCursorDecl(C); |
| 123 | } |
| 124 | } |
| 125 | static void usage() { |
| 126 | if (clang_getDeclKind(D) == CXDecl_ObjC_interface) { |
| 127 | clang_loadDeclaration(D, getInterfaceDetails); |
| 128 | } |
| 129 | } |
| 130 | */ |
| 131 | void clang_loadDeclaration(CXDecl, void (*callback)(CXDecl, CXCursor)); |
| 132 | |
| 133 | // |
| 134 | // CXEntity Operations. |
| 135 | // |
| 136 | const char *clang_getDeclarationName(CXEntity); |
| 137 | const char *clang_getURI(CXEntity); |
| 138 | CXEntity clang_getEntity(const char *URI); |
| 139 | // |
| 140 | // CXDecl Operations. |
| 141 | // |
| 142 | CXCursor clang_getCursorFromDecl(CXDecl); |
| 143 | CXEntity clang_getEntityFromDecl(CXDecl); |
| 144 | enum CXDeclKind clang_getDeclKind(CXDecl); |
| 145 | const char *clang_getDeclSpelling(CXDecl); |
| 146 | // |
| 147 | // CXCursor Operations. |
| 148 | // |
| 149 | CXCursor clang_getCursor(CXTranslationUnit, const char *source_name, |
| 150 | unsigned line, unsigned column); |
| 151 | |
| 152 | CXCursorKind clang_getCursorKind(CXCursor); |
| 153 | |
| 154 | unsigned clang_getCursorLine(CXCursor); |
| 155 | unsigned clang_getCursorColumn(CXCursor); |
| 156 | const char *clang_getCursorSource(CXCursor); |
| 157 | |
| 158 | // If CXCursorKind == Cursor_Reference, then this will return the referenced declaration. |
| 159 | // If CXCursorKind == Cursor_Declaration, then this will return the declaration. |
| 160 | CXDecl clang_getCursorDecl(CXCursor); |
Ted Kremenek | 1ca68fb | 2009-08-26 22:36:44 +0000 | [diff] [blame] | 161 | |
| 162 | #ifdef __cplusplus |
| 163 | } |
| 164 | #endif |
| 165 | #endif |
| 166 | |