blob: 6e5f61f4495bbc4e84e1ff50014bc4e85f7e112a [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:
Douglas Gregor97b98722010-01-19 23:20:36 +000068 case Stmt::IfStmtClass:
69 case Stmt::SwitchStmtClass:
70 case Stmt::WhileStmtClass:
71 case Stmt::DoStmtClass:
72 case Stmt::ForStmtClass:
73 case Stmt::GotoStmtClass:
74 case Stmt::IndirectGotoStmtClass:
75 case Stmt::ContinueStmtClass:
76 case Stmt::BreakStmtClass:
77 case Stmt::ReturnStmtClass:
78 case Stmt::DeclStmtClass:
79 case Stmt::SwitchCaseClass:
80 case Stmt::AsmStmtClass:
81 case Stmt::ObjCAtTryStmtClass:
82 case Stmt::ObjCAtCatchStmtClass:
83 case Stmt::ObjCAtFinallyStmtClass:
84 case Stmt::ObjCAtThrowStmtClass:
85 case Stmt::ObjCAtSynchronizedStmtClass:
86 case Stmt::ObjCForCollectionStmtClass:
87 case Stmt::CXXCatchStmtClass:
88 case Stmt::CXXTryStmtClass:
89 K = CXCursor_UnexposedStmt;
90 break;
91
Douglas Gregor36897b02010-09-10 00:22:18 +000092 case Stmt::LabelStmtClass:
93 K = CXCursor_LabelStmt;
94 break;
95
Douglas Gregor97b98722010-01-19 23:20:36 +000096 case Stmt::PredefinedExprClass:
97 case Stmt::IntegerLiteralClass:
98 case Stmt::FloatingLiteralClass:
99 case Stmt::ImaginaryLiteralClass:
100 case Stmt::StringLiteralClass:
101 case Stmt::CharacterLiteralClass:
102 case Stmt::ParenExprClass:
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000103 case Stmt::UnaryOperatorClass:
104 case Stmt::OffsetOfExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000105 case Stmt::SizeOfAlignOfExprClass:
106 case Stmt::ArraySubscriptExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000107 case Stmt::BinaryOperatorClass:
108 case Stmt::CompoundAssignOperatorClass:
109 case Stmt::ConditionalOperatorClass:
110 case Stmt::ImplicitCastExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000111 case Stmt::CStyleCastExprClass:
112 case Stmt::CompoundLiteralExprClass:
113 case Stmt::ExtVectorElementExprClass:
114 case Stmt::InitListExprClass:
115 case Stmt::DesignatedInitExprClass:
116 case Stmt::ImplicitValueInitExprClass:
117 case Stmt::ParenListExprClass:
118 case Stmt::VAArgExprClass:
119 case Stmt::AddrLabelExprClass:
120 case Stmt::StmtExprClass:
121 case Stmt::TypesCompatibleExprClass:
122 case Stmt::ChooseExprClass:
123 case Stmt::GNUNullExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000124 case Stmt::CXXStaticCastExprClass:
125 case Stmt::CXXDynamicCastExprClass:
126 case Stmt::CXXReinterpretCastExprClass:
127 case Stmt::CXXConstCastExprClass:
128 case Stmt::CXXFunctionalCastExprClass:
129 case Stmt::CXXTypeidExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +0000130 case Stmt::CXXUuidofExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000131 case Stmt::CXXBoolLiteralExprClass:
132 case Stmt::CXXNullPtrLiteralExprClass:
133 case Stmt::CXXThisExprClass:
134 case Stmt::CXXThrowExprClass:
135 case Stmt::CXXDefaultArgExprClass:
Douglas Gregored8abf12010-07-08 06:14:04 +0000136 case Stmt::CXXScalarValueInitExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000137 case Stmt::CXXNewExprClass:
138 case Stmt::CXXDeleteExprClass:
139 case Stmt::CXXPseudoDestructorExprClass:
140 case Stmt::UnresolvedLookupExprClass:
141 case Stmt::UnaryTypeTraitExprClass:
142 case Stmt::DependentScopeDeclRefExprClass:
143 case Stmt::CXXBindTemporaryExprClass:
144 case Stmt::CXXExprWithTemporariesClass:
145 case Stmt::CXXUnresolvedConstructExprClass:
146 case Stmt::CXXDependentScopeMemberExprClass:
147 case Stmt::UnresolvedMemberExprClass:
148 case Stmt::ObjCStringLiteralClass:
149 case Stmt::ObjCEncodeExprClass:
150 case Stmt::ObjCSelectorExprClass:
151 case Stmt::ObjCProtocolExprClass:
152 case Stmt::ObjCImplicitSetterGetterRefExprClass:
153 case Stmt::ObjCSuperExprClass:
154 case Stmt::ObjCIsaExprClass:
155 case Stmt::ShuffleVectorExprClass:
156 case Stmt::BlockExprClass:
157 K = CXCursor_UnexposedExpr;
158 break;
Douglas Gregor36897b02010-09-10 00:22:18 +0000159
Douglas Gregor97b98722010-01-19 23:20:36 +0000160 case Stmt::DeclRefExprClass:
161 case Stmt::BlockDeclRefExprClass:
162 // FIXME: UnresolvedLookupExpr?
163 // FIXME: DependentScopeDeclRefExpr?
164 K = CXCursor_DeclRefExpr;
165 break;
166
167 case Stmt::MemberExprClass:
168 case Stmt::ObjCIvarRefExprClass:
169 case Stmt::ObjCPropertyRefExprClass:
170 // FIXME: UnresolvedMemberExpr?
171 // FIXME: CXXDependentScopeMemberExpr?
172 K = CXCursor_MemberRefExpr;
173 break;
174
175 case Stmt::CallExprClass:
176 case Stmt::CXXOperatorCallExprClass:
177 case Stmt::CXXMemberCallExprClass:
178 case Stmt::CXXConstructExprClass:
179 case Stmt::CXXTemporaryObjectExprClass:
180 // FIXME: CXXUnresolvedConstructExpr
181 // FIXME: ObjCImplicitSetterGetterRefExpr?
182 K = CXCursor_CallExpr;
183 break;
184
185 case Stmt::ObjCMessageExprClass:
186 K = CXCursor_ObjCMessageExpr;
187 break;
188 }
189
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000190 CXCursor C = { K, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000191 return C;
192}
193
Douglas Gregor2e331b92010-01-16 14:00:32 +0000194CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000195 SourceLocation Loc,
196 ASTUnit *TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000197 assert(Super && TU && "Invalid arguments!");
Douglas Gregor2e331b92010-01-16 14:00:32 +0000198 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000199 CXCursor C = { CXCursor_ObjCSuperClassRef, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000200 return C;
201}
202
203std::pair<ObjCInterfaceDecl *, SourceLocation>
204cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
205 assert(C.kind == CXCursor_ObjCSuperClassRef);
206 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
207 SourceLocation::getFromRawEncoding(
208 reinterpret_cast<uintptr_t>(C.data[1])));
209}
210
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000211CXCursor cxcursor::MakeCursorObjCProtocolRef(ObjCProtocolDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000212 SourceLocation Loc,
213 ASTUnit *TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000214 assert(Super && TU && "Invalid arguments!");
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000215 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000216 CXCursor C = { CXCursor_ObjCProtocolRef, { Super, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000217 return C;
218}
219
220std::pair<ObjCProtocolDecl *, SourceLocation>
221cxcursor::getCursorObjCProtocolRef(CXCursor C) {
222 assert(C.kind == CXCursor_ObjCProtocolRef);
223 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
224 SourceLocation::getFromRawEncoding(
225 reinterpret_cast<uintptr_t>(C.data[1])));
226}
227
Douglas Gregor1adb0822010-01-16 17:14:40 +0000228CXCursor cxcursor::MakeCursorObjCClassRef(ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000229 SourceLocation Loc,
230 ASTUnit *TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000231 // 'Class' can be null for invalid code.
232 if (!Class)
233 return MakeCXCursorInvalid(CXCursor_InvalidCode);
234 assert(TU && "Invalid arguments!");
Douglas Gregor1adb0822010-01-16 17:14:40 +0000235 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000236 CXCursor C = { CXCursor_ObjCClassRef, { Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000237 return C;
238}
239
240std::pair<ObjCInterfaceDecl *, SourceLocation>
241cxcursor::getCursorObjCClassRef(CXCursor C) {
242 assert(C.kind == CXCursor_ObjCClassRef);
243 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
244 SourceLocation::getFromRawEncoding(
245 reinterpret_cast<uintptr_t>(C.data[1])));
246}
247
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000248CXCursor cxcursor::MakeCursorTypeRef(TypeDecl *Type, SourceLocation Loc,
249 ASTUnit *TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000250 assert(Type && TU && "Invalid arguments!");
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000251 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
252 CXCursor C = { CXCursor_TypeRef, { Type, RawLoc, TU } };
253 return C;
254}
255
256std::pair<TypeDecl *, SourceLocation>
257cxcursor::getCursorTypeRef(CXCursor C) {
258 assert(C.kind == CXCursor_TypeRef);
259 return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
260 SourceLocation::getFromRawEncoding(
261 reinterpret_cast<uintptr_t>(C.data[1])));
262}
263
Douglas Gregor0b36e612010-08-31 20:37:03 +0000264CXCursor cxcursor::MakeCursorTemplateRef(TemplateDecl *Template,
265 SourceLocation Loc, ASTUnit *TU) {
266 assert(Template && TU && "Invalid arguments!");
267 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
268 CXCursor C = { CXCursor_TemplateRef, { Template, RawLoc, TU } };
269 return C;
270}
271
272std::pair<TemplateDecl *, SourceLocation>
273cxcursor::getCursorTemplateRef(CXCursor C) {
274 assert(C.kind == CXCursor_TemplateRef);
275 return std::make_pair(static_cast<TemplateDecl *>(C.data[0]),
276 SourceLocation::getFromRawEncoding(
277 reinterpret_cast<uintptr_t>(C.data[1])));
278}
279
Douglas Gregor69319002010-08-31 23:48:11 +0000280CXCursor cxcursor::MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc,
281 ASTUnit *TU) {
282
283 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
284 "Invalid arguments!");
285 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
286 CXCursor C = { CXCursor_NamespaceRef, { NS, RawLoc, TU } };
287 return C;
288}
289
290std::pair<NamedDecl *, SourceLocation>
291cxcursor::getCursorNamespaceRef(CXCursor C) {
292 assert(C.kind == CXCursor_NamespaceRef);
293 return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
294 SourceLocation::getFromRawEncoding(
295 reinterpret_cast<uintptr_t>(C.data[1])));
296}
297
Douglas Gregora67e03f2010-09-09 21:42:20 +0000298CXCursor cxcursor::MakeCursorMemberRef(FieldDecl *Field, SourceLocation Loc,
299 ASTUnit *TU) {
300
301 assert(Field && TU && "Invalid arguments!");
302 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
303 CXCursor C = { CXCursor_MemberRef, { Field, RawLoc, TU } };
304 return C;
305}
306
307std::pair<FieldDecl *, SourceLocation>
308cxcursor::getCursorMemberRef(CXCursor C) {
309 assert(C.kind == CXCursor_MemberRef);
310 return std::make_pair(static_cast<FieldDecl *>(C.data[0]),
311 SourceLocation::getFromRawEncoding(
312 reinterpret_cast<uintptr_t>(C.data[1])));
313}
314
Ted Kremenek3064ef92010-08-27 21:34:58 +0000315CXCursor cxcursor::MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B, ASTUnit *TU){
316 CXCursor C = { CXCursor_CXXBaseSpecifier, { B, 0, TU } };
317 return C;
318}
319
320CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
321 assert(C.kind == CXCursor_CXXBaseSpecifier);
322 return static_cast<CXXBaseSpecifier*>(C.data[0]);
323}
324
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000325CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
326 ASTUnit *TU) {
327 CXCursor C = { CXCursor_PreprocessingDirective,
328 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
329 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
330 TU }
331 };
332 return C;
333}
334
335SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
336 assert(C.kind == CXCursor_PreprocessingDirective);
337 return SourceRange(SourceLocation::getFromRawEncoding(
338 reinterpret_cast<uintptr_t> (C.data[0])),
339 SourceLocation::getFromRawEncoding(
340 reinterpret_cast<uintptr_t> (C.data[1])));
341}
342
Douglas Gregor572feb22010-03-18 18:04:21 +0000343CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI, ASTUnit *TU) {
344 CXCursor C = { CXCursor_MacroDefinition, { MI, 0, TU } };
345 return C;
346}
347
348MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
349 assert(C.kind == CXCursor_MacroDefinition);
350 return static_cast<MacroDefinition *>(C.data[0]);
351}
352
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000353CXCursor cxcursor::MakeMacroInstantiationCursor(MacroInstantiation *MI,
Douglas Gregor48072312010-03-18 15:23:44 +0000354 ASTUnit *TU) {
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000355 CXCursor C = { CXCursor_MacroInstantiation, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000356 return C;
357}
358
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000359MacroInstantiation *cxcursor::getCursorMacroInstantiation(CXCursor C) {
Douglas Gregor48072312010-03-18 15:23:44 +0000360 assert(C.kind == CXCursor_MacroInstantiation);
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000361 return static_cast<MacroInstantiation *>(C.data[0]);
Douglas Gregor48072312010-03-18 15:23:44 +0000362}
363
Douglas Gregor36897b02010-09-10 00:22:18 +0000364CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
365 ASTUnit *TU) {
366
367 assert(Label && TU && "Invalid arguments!");
368 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
369 CXCursor C = { CXCursor_LabelRef, { Label, RawLoc, TU } };
370 return C;
371}
372
373std::pair<LabelStmt*, SourceLocation>
374cxcursor::getCursorLabelRef(CXCursor C) {
375 assert(C.kind == CXCursor_LabelRef);
376 return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
377 SourceLocation::getFromRawEncoding(
378 reinterpret_cast<uintptr_t>(C.data[1])));
379}
380
Douglas Gregor283cae32010-01-15 21:56:13 +0000381Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
382 return (Decl *)Cursor.data[0];
383}
384
385Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
386 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
387}
388
389Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000390 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000391 Cursor.kind == CXCursor_ObjCProtocolRef ||
392 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000393 return 0;
394
Douglas Gregor283cae32010-01-15 21:56:13 +0000395 return (Stmt *)Cursor.data[1];
396}
397
Ted Kremenek95f33552010-08-26 01:42:22 +0000398Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
399 return (Attr *)Cursor.data[1];
400}
401
Douglas Gregorf46034a2010-01-18 23:41:10 +0000402ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000403 return getCursorASTUnit(Cursor)->getASTContext();
404}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000405
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000406ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
407 return static_cast<ASTUnit *>(Cursor.data[2]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000408}
409
Douglas Gregor283cae32010-01-15 21:56:13 +0000410bool cxcursor::operator==(CXCursor X, CXCursor Y) {
411 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
412 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000413}