blob: a8fd049c9965744244678c6ec77f69ce444eae91 [file] [log] [blame]
Ted Kremenek16c440a2010-01-15 20:35:54 +00001//===- CXCursor.cpp - Routines for manipulating CXCursors -----------------===//
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//
Douglas Gregor2e331b92010-01-16 14:00:32 +000010// This file defines routines for manipulating CXCursors. It should be the
11// only file that has internal knowledge of the encoding of the data in
12// CXCursor.
Ted Kremenek16c440a2010-01-15 20:35:54 +000013//
14//===----------------------------------------------------------------------===//
15
16#include "CXCursor.h"
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +000017#include "clang/Frontend/ASTUnit.h"
Ted Kremenek16c440a2010-01-15 20:35:54 +000018#include "clang/AST/Decl.h"
Douglas Gregor69319002010-08-31 23:48:11 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregor283cae32010-01-15 21:56:13 +000020#include "clang/AST/DeclObjC.h"
21#include "clang/AST/Expr.h"
Ted Kremenekedc8aa62010-01-16 00:36:30 +000022#include "llvm/Support/ErrorHandling.h"
Ted Kremenek16c440a2010-01-15 20:35:54 +000023
24using namespace clang;
25
Douglas Gregor5bfb8c12010-01-20 23:34:41 +000026CXCursor cxcursor::MakeCXCursorInvalid(CXCursorKind K) {
27 assert(K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid);
28 CXCursor C = { K, { 0, 0, 0 } };
29 return C;
Ted Kremenek16c440a2010-01-15 20:35:54 +000030}
31
Ted Kremeneke77f4432010-02-18 03:09:07 +000032static CXCursorKind GetCursorKind(const Attr *A) {
33 assert(A && "Invalid arguments!");
34 switch (A->getKind()) {
35 default: break;
Sean Hunt387475d2010-06-16 23:43:53 +000036 case attr::IBAction: return CXCursor_IBActionAttr;
37 case attr::IBOutlet: return CXCursor_IBOutletAttr;
38 case attr::IBOutletCollection: return CXCursor_IBOutletCollectionAttr;
Ted Kremeneke77f4432010-02-18 03:09:07 +000039 }
40
41 return CXCursor_UnexposedAttr;
42}
43
44CXCursor cxcursor::MakeCXCursor(const Attr *A, Decl *Parent, ASTUnit *TU) {
45 assert(A && Parent && TU && "Invalid arguments!");
46 CXCursor C = { GetCursorKind(A), { Parent, (void*)A, TU } };
47 return C;
48}
49
Douglas Gregorb2cd4872010-01-20 23:57:43 +000050CXCursor cxcursor::MakeCXCursor(Decl *D, ASTUnit *TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +000051 assert(D && TU && "Invalid arguments!");
Douglas Gregore8d7beb2010-09-03 23:30:36 +000052 CXCursor C = { getCursorKindForDecl(D), { D, 0, TU } };
Douglas Gregor5bfb8c12010-01-20 23:34:41 +000053 return C;
Ted Kremenekedc8aa62010-01-16 00:36:30 +000054}
55
Douglas Gregorb2cd4872010-01-20 23:57:43 +000056CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, ASTUnit *TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +000057 assert(S && TU && "Invalid arguments!");
Douglas Gregor97b98722010-01-19 23:20:36 +000058 CXCursorKind K = CXCursor_NotImplemented;
59
60 switch (S->getStmtClass()) {
61 case Stmt::NoStmtClass:
62 break;
63
64 case Stmt::NullStmtClass:
65 case Stmt::CompoundStmtClass:
66 case Stmt::CaseStmtClass:
67 case Stmt::DefaultStmtClass:
68 case Stmt::LabelStmtClass:
69 case Stmt::IfStmtClass:
70 case Stmt::SwitchStmtClass:
71 case Stmt::WhileStmtClass:
72 case Stmt::DoStmtClass:
73 case Stmt::ForStmtClass:
74 case Stmt::GotoStmtClass:
75 case Stmt::IndirectGotoStmtClass:
76 case Stmt::ContinueStmtClass:
77 case Stmt::BreakStmtClass:
78 case Stmt::ReturnStmtClass:
79 case Stmt::DeclStmtClass:
80 case Stmt::SwitchCaseClass:
81 case Stmt::AsmStmtClass:
82 case Stmt::ObjCAtTryStmtClass:
83 case Stmt::ObjCAtCatchStmtClass:
84 case Stmt::ObjCAtFinallyStmtClass:
85 case Stmt::ObjCAtThrowStmtClass:
86 case Stmt::ObjCAtSynchronizedStmtClass:
87 case Stmt::ObjCForCollectionStmtClass:
88 case Stmt::CXXCatchStmtClass:
89 case Stmt::CXXTryStmtClass:
90 K = CXCursor_UnexposedStmt;
91 break;
92
Douglas Gregor97b98722010-01-19 23:20:36 +000093 case Stmt::PredefinedExprClass:
94 case Stmt::IntegerLiteralClass:
95 case Stmt::FloatingLiteralClass:
96 case Stmt::ImaginaryLiteralClass:
97 case Stmt::StringLiteralClass:
98 case Stmt::CharacterLiteralClass:
99 case Stmt::ParenExprClass:
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000100 case Stmt::UnaryOperatorClass:
101 case Stmt::OffsetOfExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000102 case Stmt::SizeOfAlignOfExprClass:
103 case Stmt::ArraySubscriptExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000104 case Stmt::BinaryOperatorClass:
105 case Stmt::CompoundAssignOperatorClass:
106 case Stmt::ConditionalOperatorClass:
107 case Stmt::ImplicitCastExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000108 case Stmt::CStyleCastExprClass:
109 case Stmt::CompoundLiteralExprClass:
110 case Stmt::ExtVectorElementExprClass:
111 case Stmt::InitListExprClass:
112 case Stmt::DesignatedInitExprClass:
113 case Stmt::ImplicitValueInitExprClass:
114 case Stmt::ParenListExprClass:
115 case Stmt::VAArgExprClass:
116 case Stmt::AddrLabelExprClass:
117 case Stmt::StmtExprClass:
118 case Stmt::TypesCompatibleExprClass:
119 case Stmt::ChooseExprClass:
120 case Stmt::GNUNullExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000121 case Stmt::CXXStaticCastExprClass:
122 case Stmt::CXXDynamicCastExprClass:
123 case Stmt::CXXReinterpretCastExprClass:
124 case Stmt::CXXConstCastExprClass:
125 case Stmt::CXXFunctionalCastExprClass:
126 case Stmt::CXXTypeidExprClass:
127 case Stmt::CXXBoolLiteralExprClass:
128 case Stmt::CXXNullPtrLiteralExprClass:
129 case Stmt::CXXThisExprClass:
130 case Stmt::CXXThrowExprClass:
131 case Stmt::CXXDefaultArgExprClass:
Douglas Gregored8abf12010-07-08 06:14:04 +0000132 case Stmt::CXXScalarValueInitExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000133 case Stmt::CXXNewExprClass:
134 case Stmt::CXXDeleteExprClass:
135 case Stmt::CXXPseudoDestructorExprClass:
136 case Stmt::UnresolvedLookupExprClass:
137 case Stmt::UnaryTypeTraitExprClass:
138 case Stmt::DependentScopeDeclRefExprClass:
139 case Stmt::CXXBindTemporaryExprClass:
140 case Stmt::CXXExprWithTemporariesClass:
141 case Stmt::CXXUnresolvedConstructExprClass:
142 case Stmt::CXXDependentScopeMemberExprClass:
143 case Stmt::UnresolvedMemberExprClass:
144 case Stmt::ObjCStringLiteralClass:
145 case Stmt::ObjCEncodeExprClass:
146 case Stmt::ObjCSelectorExprClass:
147 case Stmt::ObjCProtocolExprClass:
148 case Stmt::ObjCImplicitSetterGetterRefExprClass:
149 case Stmt::ObjCSuperExprClass:
150 case Stmt::ObjCIsaExprClass:
151 case Stmt::ShuffleVectorExprClass:
152 case Stmt::BlockExprClass:
153 K = CXCursor_UnexposedExpr;
154 break;
155 case Stmt::DeclRefExprClass:
156 case Stmt::BlockDeclRefExprClass:
157 // FIXME: UnresolvedLookupExpr?
158 // FIXME: DependentScopeDeclRefExpr?
159 K = CXCursor_DeclRefExpr;
160 break;
161
162 case Stmt::MemberExprClass:
163 case Stmt::ObjCIvarRefExprClass:
164 case Stmt::ObjCPropertyRefExprClass:
165 // FIXME: UnresolvedMemberExpr?
166 // FIXME: CXXDependentScopeMemberExpr?
167 K = CXCursor_MemberRefExpr;
168 break;
169
170 case Stmt::CallExprClass:
171 case Stmt::CXXOperatorCallExprClass:
172 case Stmt::CXXMemberCallExprClass:
173 case Stmt::CXXConstructExprClass:
174 case Stmt::CXXTemporaryObjectExprClass:
175 // FIXME: CXXUnresolvedConstructExpr
176 // FIXME: ObjCImplicitSetterGetterRefExpr?
177 K = CXCursor_CallExpr;
178 break;
179
180 case Stmt::ObjCMessageExprClass:
181 K = CXCursor_ObjCMessageExpr;
182 break;
183 }
184
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000185 CXCursor C = { K, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000186 return C;
187}
188
Douglas Gregor2e331b92010-01-16 14:00:32 +0000189CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000190 SourceLocation Loc,
191 ASTUnit *TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000192 assert(Super && TU && "Invalid arguments!");
Douglas Gregor2e331b92010-01-16 14:00:32 +0000193 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000194 CXCursor C = { CXCursor_ObjCSuperClassRef, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000195 return C;
196}
197
198std::pair<ObjCInterfaceDecl *, SourceLocation>
199cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
200 assert(C.kind == CXCursor_ObjCSuperClassRef);
201 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
202 SourceLocation::getFromRawEncoding(
203 reinterpret_cast<uintptr_t>(C.data[1])));
204}
205
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000206CXCursor cxcursor::MakeCursorObjCProtocolRef(ObjCProtocolDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000207 SourceLocation Loc,
208 ASTUnit *TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000209 assert(Super && TU && "Invalid arguments!");
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000210 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000211 CXCursor C = { CXCursor_ObjCProtocolRef, { Super, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000212 return C;
213}
214
215std::pair<ObjCProtocolDecl *, SourceLocation>
216cxcursor::getCursorObjCProtocolRef(CXCursor C) {
217 assert(C.kind == CXCursor_ObjCProtocolRef);
218 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
219 SourceLocation::getFromRawEncoding(
220 reinterpret_cast<uintptr_t>(C.data[1])));
221}
222
Douglas Gregor1adb0822010-01-16 17:14:40 +0000223CXCursor cxcursor::MakeCursorObjCClassRef(ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000224 SourceLocation Loc,
225 ASTUnit *TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000226 // 'Class' can be null for invalid code.
227 if (!Class)
228 return MakeCXCursorInvalid(CXCursor_InvalidCode);
229 assert(TU && "Invalid arguments!");
Douglas Gregor1adb0822010-01-16 17:14:40 +0000230 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000231 CXCursor C = { CXCursor_ObjCClassRef, { Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000232 return C;
233}
234
235std::pair<ObjCInterfaceDecl *, SourceLocation>
236cxcursor::getCursorObjCClassRef(CXCursor C) {
237 assert(C.kind == CXCursor_ObjCClassRef);
238 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
239 SourceLocation::getFromRawEncoding(
240 reinterpret_cast<uintptr_t>(C.data[1])));
241}
242
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000243CXCursor cxcursor::MakeCursorTypeRef(TypeDecl *Type, SourceLocation Loc,
244 ASTUnit *TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000245 assert(Type && TU && "Invalid arguments!");
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000246 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
247 CXCursor C = { CXCursor_TypeRef, { Type, RawLoc, TU } };
248 return C;
249}
250
251std::pair<TypeDecl *, SourceLocation>
252cxcursor::getCursorTypeRef(CXCursor C) {
253 assert(C.kind == CXCursor_TypeRef);
254 return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
255 SourceLocation::getFromRawEncoding(
256 reinterpret_cast<uintptr_t>(C.data[1])));
257}
258
Douglas Gregor0b36e612010-08-31 20:37:03 +0000259CXCursor cxcursor::MakeCursorTemplateRef(TemplateDecl *Template,
260 SourceLocation Loc, ASTUnit *TU) {
261 assert(Template && TU && "Invalid arguments!");
262 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
263 CXCursor C = { CXCursor_TemplateRef, { Template, RawLoc, TU } };
264 return C;
265}
266
267std::pair<TemplateDecl *, SourceLocation>
268cxcursor::getCursorTemplateRef(CXCursor C) {
269 assert(C.kind == CXCursor_TemplateRef);
270 return std::make_pair(static_cast<TemplateDecl *>(C.data[0]),
271 SourceLocation::getFromRawEncoding(
272 reinterpret_cast<uintptr_t>(C.data[1])));
273}
274
Douglas Gregor69319002010-08-31 23:48:11 +0000275CXCursor cxcursor::MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc,
276 ASTUnit *TU) {
277
278 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
279 "Invalid arguments!");
280 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
281 CXCursor C = { CXCursor_NamespaceRef, { NS, RawLoc, TU } };
282 return C;
283}
284
285std::pair<NamedDecl *, SourceLocation>
286cxcursor::getCursorNamespaceRef(CXCursor C) {
287 assert(C.kind == CXCursor_NamespaceRef);
288 return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
289 SourceLocation::getFromRawEncoding(
290 reinterpret_cast<uintptr_t>(C.data[1])));
291}
292
Ted Kremenek3064ef92010-08-27 21:34:58 +0000293CXCursor cxcursor::MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B, ASTUnit *TU){
294 CXCursor C = { CXCursor_CXXBaseSpecifier, { B, 0, TU } };
295 return C;
296}
297
298CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
299 assert(C.kind == CXCursor_CXXBaseSpecifier);
300 return static_cast<CXXBaseSpecifier*>(C.data[0]);
301}
302
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000303CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
304 ASTUnit *TU) {
305 CXCursor C = { CXCursor_PreprocessingDirective,
306 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
307 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
308 TU }
309 };
310 return C;
311}
312
313SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
314 assert(C.kind == CXCursor_PreprocessingDirective);
315 return SourceRange(SourceLocation::getFromRawEncoding(
316 reinterpret_cast<uintptr_t> (C.data[0])),
317 SourceLocation::getFromRawEncoding(
318 reinterpret_cast<uintptr_t> (C.data[1])));
319}
320
Douglas Gregor572feb22010-03-18 18:04:21 +0000321CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI, ASTUnit *TU) {
322 CXCursor C = { CXCursor_MacroDefinition, { MI, 0, TU } };
323 return C;
324}
325
326MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
327 assert(C.kind == CXCursor_MacroDefinition);
328 return static_cast<MacroDefinition *>(C.data[0]);
329}
330
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000331CXCursor cxcursor::MakeMacroInstantiationCursor(MacroInstantiation *MI,
Douglas Gregor48072312010-03-18 15:23:44 +0000332 ASTUnit *TU) {
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000333 CXCursor C = { CXCursor_MacroInstantiation, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000334 return C;
335}
336
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000337MacroInstantiation *cxcursor::getCursorMacroInstantiation(CXCursor C) {
Douglas Gregor48072312010-03-18 15:23:44 +0000338 assert(C.kind == CXCursor_MacroInstantiation);
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000339 return static_cast<MacroInstantiation *>(C.data[0]);
Douglas Gregor48072312010-03-18 15:23:44 +0000340}
341
Douglas Gregor283cae32010-01-15 21:56:13 +0000342Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
343 return (Decl *)Cursor.data[0];
344}
345
346Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
347 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
348}
349
350Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000351 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000352 Cursor.kind == CXCursor_ObjCProtocolRef ||
353 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000354 return 0;
355
Douglas Gregor283cae32010-01-15 21:56:13 +0000356 return (Stmt *)Cursor.data[1];
357}
358
Ted Kremenek95f33552010-08-26 01:42:22 +0000359Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
360 return (Attr *)Cursor.data[1];
361}
362
Douglas Gregorf46034a2010-01-18 23:41:10 +0000363ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000364 return getCursorASTUnit(Cursor)->getASTContext();
365}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000366
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000367ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
368 return static_cast<ASTUnit *>(Cursor.data[2]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000369}
370
Douglas Gregor283cae32010-01-15 21:56:13 +0000371bool cxcursor::operator==(CXCursor X, CXCursor Y) {
372 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
373 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000374}