1
2
3
4
5
6
7
8
9
10
11
12
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
|
//===- CXCursor.cpp - Routines for manipulating CXCursors -----------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines routines for manipulating CXCursors. It should be the
// only file that has internal knowledge of the encoding of the data in
// CXCursor.
//
//===----------------------------------------------------------------------===//
#include "CXCursor.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/Expr.h"
#include "llvm/Support/ErrorHandling.h"
using namespace clang;
CXCursor cxcursor::MakeCXCursor(CXCursorKind K, Decl *D) {
CXCursor C = { K, { D, 0, 0 } };
return C;
}
CXCursor cxcursor::MakeCXCursor(CXCursorKind K, Decl *D, Stmt *S) {
assert(clang_isReference(K));
CXCursor C = { K, { D, S, 0 } };
return C;
}
static CXCursorKind GetCursorKind(Decl *D) {
switch (D->getKind()) {
case Decl::Enum: return CXCursor_EnumDecl;
case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
case Decl::Field: return CXCursor_FieldDecl;
case Decl::Function:
return cast<FunctionDecl>(D)->isThisDeclarationADefinition()
? CXCursor_FunctionDefn : CXCursor_FunctionDecl;
case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
case Decl::ObjCCategoryImpl: return CXCursor_ObjCCategoryDefn;
case Decl::ObjCClass:
// FIXME
return CXCursor_NotImplemented;
case Decl::ObjCImplementation: return CXCursor_ObjCClassDefn;
case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
case Decl::ObjCIvar: return CXCursor_ObjCIvarDecl;
case Decl::ObjCMethod:
return cast<ObjCMethodDecl>(D)->isInstanceMethod()
? CXCursor_ObjCInstanceMethodDecl : CXCursor_ObjCClassMethodDecl;
case Decl::ObjCProperty: return CXCursor_ObjCPropertyDecl;
case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
case Decl::ParmVar: return CXCursor_ParmDecl;
case Decl::Typedef: return CXCursor_TypedefDecl;
case Decl::Var: return CXCursor_VarDecl;
default:
if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
switch (TD->getTagKind()) {
case TagDecl::TK_struct: return CXCursor_StructDecl;
case TagDecl::TK_class: return CXCursor_ClassDecl;
case TagDecl::TK_union: return CXCursor_UnionDecl;
case TagDecl::TK_enum: return CXCursor_EnumDecl;
}
}
}
llvm_unreachable("Invalid Decl");
return CXCursor_NotImplemented;
}
CXCursor cxcursor::MakeCXCursor(Decl *D) {
return MakeCXCursor(GetCursorKind(D), D);
}
CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
SourceLocation Loc) {
void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
CXCursor C = { CXCursor_ObjCSuperClassRef, { Super, RawLoc, 0 } };
return C;
}
std::pair<ObjCInterfaceDecl *, SourceLocation>
cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
assert(C.kind == CXCursor_ObjCSuperClassRef);
return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
SourceLocation::getFromRawEncoding(
reinterpret_cast<uintptr_t>(C.data[1])));
}
CXCursor cxcursor::MakeCursorObjCProtocolRef(ObjCProtocolDecl *Super,
SourceLocation Loc) {
void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
CXCursor C = { CXCursor_ObjCProtocolRef, { Super, RawLoc, 0 } };
return C;
}
std::pair<ObjCProtocolDecl *, SourceLocation>
cxcursor::getCursorObjCProtocolRef(CXCursor C) {
assert(C.kind == CXCursor_ObjCProtocolRef);
return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
SourceLocation::getFromRawEncoding(
reinterpret_cast<uintptr_t>(C.data[1])));
}
Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
return (Decl *)Cursor.data[0];
}
Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
}
Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Cursor.kind == CXCursor_ObjCProtocolRef)
return 0;
return (Stmt *)Cursor.data[1];
}
Decl *cxcursor::getCursorReferringDecl(CXCursor Cursor) {
return (Decl *)Cursor.data[2];
}
NamedDecl *cxcursor::getCursorInterfaceParent(CXCursor Cursor) {
assert(Cursor.kind == CXCursor_ObjCClassRef);
assert(isa<ObjCInterfaceDecl>(getCursorDecl(Cursor)));
// FIXME: This is a hack (storing the parent decl in the stmt slot).
return static_cast<NamedDecl *>(Cursor.data[1]);
}
bool cxcursor::operator==(CXCursor X, CXCursor Y) {
return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
X.data[2] == Y.data[2];
}
|