blob: 9b3ecbf0d9348637e1be1390833c75f775ec414c [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:
Francois Pichet9be88402010-09-08 23:47:05 +0000127 case Stmt::CXXUuidofExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000128 case Stmt::CXXBoolLiteralExprClass:
129 case Stmt::CXXNullPtrLiteralExprClass:
130 case Stmt::CXXThisExprClass:
131 case Stmt::CXXThrowExprClass:
132 case Stmt::CXXDefaultArgExprClass:
Douglas Gregored8abf12010-07-08 06:14:04 +0000133 case Stmt::CXXScalarValueInitExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000134 case Stmt::CXXNewExprClass:
135 case Stmt::CXXDeleteExprClass:
136 case Stmt::CXXPseudoDestructorExprClass:
137 case Stmt::UnresolvedLookupExprClass:
138 case Stmt::UnaryTypeTraitExprClass:
139 case Stmt::DependentScopeDeclRefExprClass:
140 case Stmt::CXXBindTemporaryExprClass:
141 case Stmt::CXXExprWithTemporariesClass:
142 case Stmt::CXXUnresolvedConstructExprClass:
143 case Stmt::CXXDependentScopeMemberExprClass:
144 case Stmt::UnresolvedMemberExprClass:
145 case Stmt::ObjCStringLiteralClass:
146 case Stmt::ObjCEncodeExprClass:
147 case Stmt::ObjCSelectorExprClass:
148 case Stmt::ObjCProtocolExprClass:
149 case Stmt::ObjCImplicitSetterGetterRefExprClass:
150 case Stmt::ObjCSuperExprClass:
151 case Stmt::ObjCIsaExprClass:
152 case Stmt::ShuffleVectorExprClass:
153 case Stmt::BlockExprClass:
154 K = CXCursor_UnexposedExpr;
155 break;
156 case Stmt::DeclRefExprClass:
157 case Stmt::BlockDeclRefExprClass:
158 // FIXME: UnresolvedLookupExpr?
159 // FIXME: DependentScopeDeclRefExpr?
160 K = CXCursor_DeclRefExpr;
161 break;
162
163 case Stmt::MemberExprClass:
164 case Stmt::ObjCIvarRefExprClass:
165 case Stmt::ObjCPropertyRefExprClass:
166 // FIXME: UnresolvedMemberExpr?
167 // FIXME: CXXDependentScopeMemberExpr?
168 K = CXCursor_MemberRefExpr;
169 break;
170
171 case Stmt::CallExprClass:
172 case Stmt::CXXOperatorCallExprClass:
173 case Stmt::CXXMemberCallExprClass:
174 case Stmt::CXXConstructExprClass:
175 case Stmt::CXXTemporaryObjectExprClass:
176 // FIXME: CXXUnresolvedConstructExpr
177 // FIXME: ObjCImplicitSetterGetterRefExpr?
178 K = CXCursor_CallExpr;
179 break;
180
181 case Stmt::ObjCMessageExprClass:
182 K = CXCursor_ObjCMessageExpr;
183 break;
184 }
185
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000186 CXCursor C = { K, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000187 return C;
188}
189
Douglas Gregor2e331b92010-01-16 14:00:32 +0000190CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000191 SourceLocation Loc,
192 ASTUnit *TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000193 assert(Super && TU && "Invalid arguments!");
Douglas Gregor2e331b92010-01-16 14:00:32 +0000194 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000195 CXCursor C = { CXCursor_ObjCSuperClassRef, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000196 return C;
197}
198
199std::pair<ObjCInterfaceDecl *, SourceLocation>
200cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
201 assert(C.kind == CXCursor_ObjCSuperClassRef);
202 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
203 SourceLocation::getFromRawEncoding(
204 reinterpret_cast<uintptr_t>(C.data[1])));
205}
206
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000207CXCursor cxcursor::MakeCursorObjCProtocolRef(ObjCProtocolDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000208 SourceLocation Loc,
209 ASTUnit *TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000210 assert(Super && TU && "Invalid arguments!");
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000211 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000212 CXCursor C = { CXCursor_ObjCProtocolRef, { Super, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000213 return C;
214}
215
216std::pair<ObjCProtocolDecl *, SourceLocation>
217cxcursor::getCursorObjCProtocolRef(CXCursor C) {
218 assert(C.kind == CXCursor_ObjCProtocolRef);
219 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
220 SourceLocation::getFromRawEncoding(
221 reinterpret_cast<uintptr_t>(C.data[1])));
222}
223
Douglas Gregor1adb0822010-01-16 17:14:40 +0000224CXCursor cxcursor::MakeCursorObjCClassRef(ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000225 SourceLocation Loc,
226 ASTUnit *TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000227 // 'Class' can be null for invalid code.
228 if (!Class)
229 return MakeCXCursorInvalid(CXCursor_InvalidCode);
230 assert(TU && "Invalid arguments!");
Douglas Gregor1adb0822010-01-16 17:14:40 +0000231 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000232 CXCursor C = { CXCursor_ObjCClassRef, { Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000233 return C;
234}
235
236std::pair<ObjCInterfaceDecl *, SourceLocation>
237cxcursor::getCursorObjCClassRef(CXCursor C) {
238 assert(C.kind == CXCursor_ObjCClassRef);
239 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
240 SourceLocation::getFromRawEncoding(
241 reinterpret_cast<uintptr_t>(C.data[1])));
242}
243
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000244CXCursor cxcursor::MakeCursorTypeRef(TypeDecl *Type, SourceLocation Loc,
245 ASTUnit *TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000246 assert(Type && TU && "Invalid arguments!");
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000247 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
248 CXCursor C = { CXCursor_TypeRef, { Type, RawLoc, TU } };
249 return C;
250}
251
252std::pair<TypeDecl *, SourceLocation>
253cxcursor::getCursorTypeRef(CXCursor C) {
254 assert(C.kind == CXCursor_TypeRef);
255 return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
256 SourceLocation::getFromRawEncoding(
257 reinterpret_cast<uintptr_t>(C.data[1])));
258}
259
Douglas Gregor0b36e612010-08-31 20:37:03 +0000260CXCursor cxcursor::MakeCursorTemplateRef(TemplateDecl *Template,
261 SourceLocation Loc, ASTUnit *TU) {
262 assert(Template && TU && "Invalid arguments!");
263 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
264 CXCursor C = { CXCursor_TemplateRef, { Template, RawLoc, TU } };
265 return C;
266}
267
268std::pair<TemplateDecl *, SourceLocation>
269cxcursor::getCursorTemplateRef(CXCursor C) {
270 assert(C.kind == CXCursor_TemplateRef);
271 return std::make_pair(static_cast<TemplateDecl *>(C.data[0]),
272 SourceLocation::getFromRawEncoding(
273 reinterpret_cast<uintptr_t>(C.data[1])));
274}
275
Douglas Gregor69319002010-08-31 23:48:11 +0000276CXCursor cxcursor::MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc,
277 ASTUnit *TU) {
278
279 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
280 "Invalid arguments!");
281 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
282 CXCursor C = { CXCursor_NamespaceRef, { NS, RawLoc, TU } };
283 return C;
284}
285
286std::pair<NamedDecl *, SourceLocation>
287cxcursor::getCursorNamespaceRef(CXCursor C) {
288 assert(C.kind == CXCursor_NamespaceRef);
289 return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
290 SourceLocation::getFromRawEncoding(
291 reinterpret_cast<uintptr_t>(C.data[1])));
292}
293
Ted Kremenek3064ef92010-08-27 21:34:58 +0000294CXCursor cxcursor::MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B, ASTUnit *TU){
295 CXCursor C = { CXCursor_CXXBaseSpecifier, { B, 0, TU } };
296 return C;
297}
298
299CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
300 assert(C.kind == CXCursor_CXXBaseSpecifier);
301 return static_cast<CXXBaseSpecifier*>(C.data[0]);
302}
303
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000304CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
305 ASTUnit *TU) {
306 CXCursor C = { CXCursor_PreprocessingDirective,
307 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
308 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
309 TU }
310 };
311 return C;
312}
313
314SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
315 assert(C.kind == CXCursor_PreprocessingDirective);
316 return SourceRange(SourceLocation::getFromRawEncoding(
317 reinterpret_cast<uintptr_t> (C.data[0])),
318 SourceLocation::getFromRawEncoding(
319 reinterpret_cast<uintptr_t> (C.data[1])));
320}
321
Douglas Gregor572feb22010-03-18 18:04:21 +0000322CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI, ASTUnit *TU) {
323 CXCursor C = { CXCursor_MacroDefinition, { MI, 0, TU } };
324 return C;
325}
326
327MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
328 assert(C.kind == CXCursor_MacroDefinition);
329 return static_cast<MacroDefinition *>(C.data[0]);
330}
331
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000332CXCursor cxcursor::MakeMacroInstantiationCursor(MacroInstantiation *MI,
Douglas Gregor48072312010-03-18 15:23:44 +0000333 ASTUnit *TU) {
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000334 CXCursor C = { CXCursor_MacroInstantiation, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000335 return C;
336}
337
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000338MacroInstantiation *cxcursor::getCursorMacroInstantiation(CXCursor C) {
Douglas Gregor48072312010-03-18 15:23:44 +0000339 assert(C.kind == CXCursor_MacroInstantiation);
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000340 return static_cast<MacroInstantiation *>(C.data[0]);
Douglas Gregor48072312010-03-18 15:23:44 +0000341}
342
Douglas Gregor283cae32010-01-15 21:56:13 +0000343Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
344 return (Decl *)Cursor.data[0];
345}
346
347Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
348 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
349}
350
351Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000352 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000353 Cursor.kind == CXCursor_ObjCProtocolRef ||
354 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000355 return 0;
356
Douglas Gregor283cae32010-01-15 21:56:13 +0000357 return (Stmt *)Cursor.data[1];
358}
359
Ted Kremenek95f33552010-08-26 01:42:22 +0000360Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
361 return (Attr *)Cursor.data[1];
362}
363
Douglas Gregorf46034a2010-01-18 23:41:10 +0000364ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000365 return getCursorASTUnit(Cursor)->getASTContext();
366}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000367
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000368ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
369 return static_cast<ASTUnit *>(Cursor.data[2]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000370}
371
Douglas Gregor283cae32010-01-15 21:56:13 +0000372bool cxcursor::operator==(CXCursor X, CXCursor Y) {
373 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
374 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000375}