cursor.h 6.49 KB
Newer Older
Jørgen Lien Sellæg's avatar
Jørgen Lien Sellæg committed
1 2
#ifndef CURSOR_H_
#define CURSOR_H_
3
#include <clang-c/Index.h>
4 5
#include "source_location.h"
#include "source_range.h"
6
#include <string>
7
#include <vector>
8
#include <unordered_set>
Jørgen Lien Sellæg's avatar
Jørgen Lien Sellæg committed
9

10
namespace clangmm {
Jørgen Lien Sellæg's avatar
Jørgen Lien Sellæg committed
11 12
  class Cursor {
  public:
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    enum class Kind {
      UnexposedDecl = 1,
      StructDecl = 2,
      UnionDecl = 3,
      ClassDecl = 4,
      EnumDecl = 5,
      FieldDecl = 6,
      EnumConstantDecl = 7,
      FunctionDecl = 8,
      VarDecl = 9,
      ParmDecl = 10,
      ObjCInterfaceDecl = 11,
      ObjCCategoryDecl = 12,
      ObjCProtocolDecl = 13,
      ObjCPropertyDecl = 14,
      ObjCIvarDecl = 15,
      ObjCInstanceMethodDecl = 16,
      ObjCClassMethodDecl = 17,
      ObjCImplementationDecl = 18,
      ObjCCategoryImplDecl = 19,
      TypedefDecl = 20,
      CXXMethod = 21,
      Namespace = 22,
      LinkageSpec = 23,
      Constructor = 24,
      Destructor = 25,
      ConversionFunction = 26,
      TemplateTypeParameter = 27,
      NonTypeTemplateParameter = 28,
      TemplateTemplateParameter = 29,
      FunctionTemplate = 30,
      ClassTemplate = 31,
      ClassTemplatePartialSpecialization = 32,
      NamespaceAlias = 33,
      UsingDirective = 34,
      UsingDeclaration = 35,
      TypeAliasDecl = 36,
      ObjCSynthesizeDecl = 37,
      ObjCDynamicDecl = 38,
      CXXAccessSpecifier = 39,
      FirstDecl = UnexposedDecl,
      LastDecl = CXXAccessSpecifier,
      FirstRef = 40,
      ObjCSuperClassRef = 40,
      ObjCProtocolRef = 41,
      ObjCClassRef = 42,
      TypeRef = 43,
      CXXBaseSpecifier = 44,
      TemplateRef = 45,
      NamespaceRef = 46,
      MemberRef = 47,
      LabelRef = 48,
      OverloadedDeclRef = 49,
      VariableRef = 50,
      LastRef = VariableRef,
      FirstInvalid = 70,
      InvalidFile = 70,
      NoDeclFound = 71,
      NotImplemented = 72,
      InvalidCode = 73,
      LastInvalid = InvalidCode,
      FirstExpr = 100,
      UnexposedExpr = 100,
      DeclRefExpr = 101,
      MemberRefExpr = 102,
      CallExpr = 103,
      ObjCMessageExpr = 104,
      BlockExpr = 105,
      IntegerLiteral = 106,
      FloatingLiteral = 107,
      ImaginaryLiteral = 108,
      StringLiteral = 109,
      CharacterLiteral = 110,
      ParenExpr = 111,
      UnaryOperator = 112,
      ArraySubscriptExpr = 113,
      BinaryOperator = 114,
      CompoundAssignOperator = 115,
      ConditionalOperator = 116,
      CStyleCastExpr = 117,
      CompoundLiteralExpr = 118,
      InitListExpr = 119,
      AddrLabelExpr = 120,
      StmtExpr = 121,
      GenericSelectionExpr = 122,
      GNUNullExpr = 123,
      CXXStaticCastExpr = 124,
      CXXDynamicCastExpr = 125,
      CXXReinterpretCastExpr = 126,
      CXXConstCastExpr = 127,
      CXXFunctionalCastExpr = 128,
      CXXTypeidExpr = 129,
      CXXBoolLiteralExpr = 130,
      CXXNullPtrLiteralExpr = 131,
      CXXThisExpr = 132,
      CXXThrowExpr = 133,
      CXXNewExpr = 134,
      CXXDeleteExpr = 135,
      UnaryExpr = 136,
      ObjCStringLiteral = 137,
      ObjCEncodeExpr = 138,
      ObjCSelectorExpr = 139,
      ObjCProtocolExpr = 140,
      ObjCBridgedCastExpr = 141,
      PackExpansionExpr = 142,
      SizeOfPackExpr = 143,
      LambdaExpr = 144,
      ObjCBoolLiteralExpr = 145,
      ObjCSelfExpr = 146,
      LastExpr = ObjCSelfExpr,
      FirstStmt = 200,
      UnexposedStmt = 200,
      LabelStmt = 201,
      CompoundStmt = 202,
      CaseStmt = 203,
      DefaultStmt = 204,
      IfStmt = 205,
      SwitchStmt = 206,
      WhileStmt = 207,
      DoStmt = 208,
      ForStmt = 209,
      GotoStmt = 210,
      IndirectGotoStmt = 211,
      ContinueStmt = 212,
      BreakStmt = 213,
      ReturnStmt = 214,
      GCCAsmStmt = 215,
      AsmStmt = GCCAsmStmt,
      ObjCAtTryStmt = 216,
      ObjCAtCatchStmt = 217,
      ObjCAtFinallyStmt = 218,
      ObjCAtThrowStmt = 219,
      ObjCAtSynchronizedStmt = 220,
      ObjCAutoreleasePoolStmt = 221,
      ObjCForCollectionStmt = 222,
      CXXCatchStmt = 223,
      CXXTryStmt = 224,
      CXXForRangeStmt = 225,
      SEHTryStmt = 226,
      SEHExceptStmt = 227,
      SEHFinallyStmt = 228,
      MSAsmStmt = 229,
      NullStmt = 230,
      DeclStmt = 231,
      LastStmt = DeclStmt,
      TranslationUnit = 300,
      FirstAttr = 400,
      UnexposedAttr = 400,
      IBActionAttr = 401,
      IBOutletAttr = 402,
      IBOutletCollectionAttr = 403,
      CXXFinalAttr = 404,
      CXXOverrideAttr = 405,
      AnnotateAttr = 406,
      AsmLabelAttr = 407,
      LastAttr = AsmLabelAttr,
      PreprocessingDirective = 500,
      MacroDefinition = 501,
      MacroExpansion = 502,
      MacroInstantiation = MacroExpansion,
      InclusionDirective = 503,
      FirstPreprocessing = PreprocessingDirective,
      LastPreprocessing = InclusionDirective,
      ModuleImportDecl = 600,
      FirstExtraDecl = ModuleImportDecl,
      LastExtraDecl = ModuleImportDecl,
    };
    class Type {
    public:
      Type(const CXType &cx_type) : cx_type(cx_type) {}
      std::string get_spelling() const;
      Type get_result() const;
185
      Cursor get_cursor() const;
186 187 188 189 190
      bool operator==(const Cursor::Type& rhs) const;
      
      CXType cx_type;
    };
    
191
    Cursor() { cx_cursor=clang_getNullCursor(); }
192
    Cursor(const CXCursor &cx_cursor) : cx_cursor(cx_cursor) {}
193
    Kind get_kind() const;
Florian Jung's avatar
Florian Jung committed
194
    std::string get_kind_spelling() const;
195
    static bool is_similar_kind(Kind kind, Kind other_kind);
196
    Type get_type() const;
197 198
    SourceLocation get_source_location() const;
    SourceRange get_source_range() const;
199
    std::string get_spelling() const;
200
    std::string get_display_name() const;
201
    std::string get_token_spelling() const;
202
    std::string get_usr() const;
203 204
    /// Improved usr that is also template and argument invariant
    std::string get_usr_extended() const;
205 206
    /// Also get overridden cursors
    std::unordered_set<std::string> get_all_usr_extended() const;
207
    Cursor get_referenced() const;
208 209
    Cursor get_canonical() const;
    Cursor get_definition() const;
210
    Cursor get_semantic_parent() const;
211
    std::vector<Cursor> get_children() const;
212
    std::vector<Cursor> get_arguments() const;
213
    std::vector<Cursor> get_all_overridden_cursors() const;
214
    operator bool() const;
215
    bool operator==(const Cursor& rhs) const;
216
    
217 218 219
    bool is_valid_kind() const;
    std::string get_type_description() const;
    std::string get_brief_comments() const;
220
    
221
    friend std::ostream &operator<<(std::ostream &os, const Cursor &cursor) {
222
      os << cursor.get_source_range() << ' ' << cursor.get_spelling();
223 224 225
      return os;
    }
    
226
    CXCursor cx_cursor;
Jørgen Lien Sellæg's avatar
Jørgen Lien Sellæg committed
227
  };
228
}  // namespace clangmm
Jørgen Lien Sellæg's avatar
Jørgen Lien Sellæg committed
229
#endif  // CURSOR_H_