blob: a8cbca57dc65bf5797ded9592a90c6ea7280cf5e [file] [log] [blame]
Ted Kremenek87553c42010-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 Gregor6c8959b2010-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 Kremenek87553c42010-01-15 20:35:54 +000013//
14//===----------------------------------------------------------------------===//
15
16#include "CXCursor.h"
Ted Kremenek4b4f3692010-11-16 01:56:27 +000017#include "CXString.h"
Douglas Gregord2fc7272010-01-20 00:23:15 +000018#include "clang/Frontend/ASTUnit.h"
Ted Kremenek87553c42010-01-15 20:35:54 +000019#include "clang/AST/Decl.h"
Douglas Gregora89314e2010-08-31 23:48:11 +000020#include "clang/AST/DeclCXX.h"
Douglas Gregorc58d05b2010-01-15 21:56:13 +000021#include "clang/AST/DeclObjC.h"
22#include "clang/AST/Expr.h"
Douglas Gregor16a2bdd2010-09-13 22:52:57 +000023#include "clang/AST/ExprCXX.h"
Ted Kremenek818e5c12010-11-01 23:26:51 +000024#include "clang-c/Index.h"
Ted Kremenekc2aa0f12010-01-16 00:36:30 +000025#include "llvm/Support/ErrorHandling.h"
Ted Kremenek87553c42010-01-15 20:35:54 +000026
27using namespace clang;
Douglas Gregor16a2bdd2010-09-13 22:52:57 +000028using namespace cxcursor;
Ted Kremenek87553c42010-01-15 20:35:54 +000029
Douglas Gregor58552bc2010-01-20 23:34:41 +000030CXCursor cxcursor::MakeCXCursorInvalid(CXCursorKind K) {
31 assert(K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid);
32 CXCursor C = { K, { 0, 0, 0 } };
33 return C;
Ted Kremenek87553c42010-01-15 20:35:54 +000034}
35
Ted Kremenekbff31432010-02-18 03:09:07 +000036static CXCursorKind GetCursorKind(const Attr *A) {
37 assert(A && "Invalid arguments!");
38 switch (A->getKind()) {
39 default: break;
Alexis Hunt344393e2010-06-16 23:43:53 +000040 case attr::IBAction: return CXCursor_IBActionAttr;
41 case attr::IBOutlet: return CXCursor_IBOutletAttr;
42 case attr::IBOutletCollection: return CXCursor_IBOutletCollectionAttr;
Ted Kremenekbff31432010-02-18 03:09:07 +000043 }
44
45 return CXCursor_UnexposedAttr;
46}
47
48CXCursor cxcursor::MakeCXCursor(const Attr *A, Decl *Parent, ASTUnit *TU) {
49 assert(A && Parent && TU && "Invalid arguments!");
50 CXCursor C = { GetCursorKind(A), { Parent, (void*)A, TU } };
51 return C;
52}
53
Ted Kremenek818e5c12010-11-01 23:26:51 +000054CXCursor cxcursor::MakeCXCursor(Decl *D, ASTUnit *TU,
55 bool FirstInDeclGroup) {
Daniel Dunbar2def7eb2010-01-25 00:40:30 +000056 assert(D && TU && "Invalid arguments!");
Ted Kremenek818e5c12010-11-01 23:26:51 +000057 CXCursor C = { getCursorKindForDecl(D),
58 { D, (void*) (FirstInDeclGroup ? 1 : 0), TU }
59 };
Douglas Gregor58552bc2010-01-20 23:34:41 +000060 return C;
Ted Kremenekc2aa0f12010-01-16 00:36:30 +000061}
62
Douglas Gregorfed36b12010-01-20 23:57:43 +000063CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, ASTUnit *TU) {
Daniel Dunbar2def7eb2010-01-25 00:40:30 +000064 assert(S && TU && "Invalid arguments!");
Douglas Gregor8f40bbee2010-01-19 23:20:36 +000065 CXCursorKind K = CXCursor_NotImplemented;
66
67 switch (S->getStmtClass()) {
68 case Stmt::NoStmtClass:
69 break;
70
71 case Stmt::NullStmtClass:
72 case Stmt::CompoundStmtClass:
73 case Stmt::CaseStmtClass:
74 case Stmt::DefaultStmtClass:
Douglas Gregor8f40bbee2010-01-19 23:20:36 +000075 case Stmt::IfStmtClass:
76 case Stmt::SwitchStmtClass:
77 case Stmt::WhileStmtClass:
78 case Stmt::DoStmtClass:
79 case Stmt::ForStmtClass:
80 case Stmt::GotoStmtClass:
81 case Stmt::IndirectGotoStmtClass:
82 case Stmt::ContinueStmtClass:
83 case Stmt::BreakStmtClass:
84 case Stmt::ReturnStmtClass:
85 case Stmt::DeclStmtClass:
86 case Stmt::SwitchCaseClass:
87 case Stmt::AsmStmtClass:
88 case Stmt::ObjCAtTryStmtClass:
89 case Stmt::ObjCAtCatchStmtClass:
90 case Stmt::ObjCAtFinallyStmtClass:
91 case Stmt::ObjCAtThrowStmtClass:
92 case Stmt::ObjCAtSynchronizedStmtClass:
93 case Stmt::ObjCForCollectionStmtClass:
94 case Stmt::CXXCatchStmtClass:
95 case Stmt::CXXTryStmtClass:
96 K = CXCursor_UnexposedStmt;
97 break;
98
Douglas Gregora93ab662010-09-10 00:22:18 +000099 case Stmt::LabelStmtClass:
100 K = CXCursor_LabelStmt;
101 break;
102
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000103 case Stmt::PredefinedExprClass:
104 case Stmt::IntegerLiteralClass:
105 case Stmt::FloatingLiteralClass:
106 case Stmt::ImaginaryLiteralClass:
107 case Stmt::StringLiteralClass:
108 case Stmt::CharacterLiteralClass:
109 case Stmt::ParenExprClass:
Douglas Gregor882211c2010-04-28 22:16:22 +0000110 case Stmt::UnaryOperatorClass:
111 case Stmt::OffsetOfExprClass:
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000112 case Stmt::SizeOfAlignOfExprClass:
113 case Stmt::ArraySubscriptExprClass:
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000114 case Stmt::BinaryOperatorClass:
115 case Stmt::CompoundAssignOperatorClass:
116 case Stmt::ConditionalOperatorClass:
117 case Stmt::ImplicitCastExprClass:
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000118 case Stmt::CStyleCastExprClass:
119 case Stmt::CompoundLiteralExprClass:
120 case Stmt::ExtVectorElementExprClass:
121 case Stmt::InitListExprClass:
122 case Stmt::DesignatedInitExprClass:
123 case Stmt::ImplicitValueInitExprClass:
124 case Stmt::ParenListExprClass:
125 case Stmt::VAArgExprClass:
126 case Stmt::AddrLabelExprClass:
127 case Stmt::StmtExprClass:
128 case Stmt::TypesCompatibleExprClass:
129 case Stmt::ChooseExprClass:
130 case Stmt::GNUNullExprClass:
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000131 case Stmt::CXXStaticCastExprClass:
132 case Stmt::CXXDynamicCastExprClass:
133 case Stmt::CXXReinterpretCastExprClass:
134 case Stmt::CXXConstCastExprClass:
135 case Stmt::CXXFunctionalCastExprClass:
136 case Stmt::CXXTypeidExprClass:
Francois Pichet5cc0a672010-09-08 23:47:05 +0000137 case Stmt::CXXUuidofExprClass:
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000138 case Stmt::CXXBoolLiteralExprClass:
139 case Stmt::CXXNullPtrLiteralExprClass:
140 case Stmt::CXXThisExprClass:
141 case Stmt::CXXThrowExprClass:
142 case Stmt::CXXDefaultArgExprClass:
Douglas Gregor747eb782010-07-08 06:14:04 +0000143 case Stmt::CXXScalarValueInitExprClass:
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000144 case Stmt::CXXNewExprClass:
145 case Stmt::CXXDeleteExprClass:
146 case Stmt::CXXPseudoDestructorExprClass:
147 case Stmt::UnresolvedLookupExprClass:
148 case Stmt::UnaryTypeTraitExprClass:
149 case Stmt::DependentScopeDeclRefExprClass:
150 case Stmt::CXXBindTemporaryExprClass:
151 case Stmt::CXXExprWithTemporariesClass:
152 case Stmt::CXXUnresolvedConstructExprClass:
153 case Stmt::CXXDependentScopeMemberExprClass:
154 case Stmt::UnresolvedMemberExprClass:
Sebastian Redl4202c0f2010-09-10 20:55:43 +0000155 case Stmt::CXXNoexceptExprClass:
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000156 case Stmt::ObjCStringLiteralClass:
157 case Stmt::ObjCEncodeExprClass:
158 case Stmt::ObjCSelectorExprClass:
159 case Stmt::ObjCProtocolExprClass:
160 case Stmt::ObjCImplicitSetterGetterRefExprClass:
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000161 case Stmt::ObjCIsaExprClass:
162 case Stmt::ShuffleVectorExprClass:
163 case Stmt::BlockExprClass:
John McCall8d69a212010-11-15 23:31:06 +0000164 case Stmt::OpaqueValueExprClass:
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000165 K = CXCursor_UnexposedExpr;
166 break;
Douglas Gregora93ab662010-09-10 00:22:18 +0000167
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000168 case Stmt::DeclRefExprClass:
169 case Stmt::BlockDeclRefExprClass:
170 // FIXME: UnresolvedLookupExpr?
171 // FIXME: DependentScopeDeclRefExpr?
172 K = CXCursor_DeclRefExpr;
173 break;
174
175 case Stmt::MemberExprClass:
176 case Stmt::ObjCIvarRefExprClass:
177 case Stmt::ObjCPropertyRefExprClass:
178 // FIXME: UnresolvedMemberExpr?
179 // FIXME: CXXDependentScopeMemberExpr?
180 K = CXCursor_MemberRefExpr;
181 break;
182
183 case Stmt::CallExprClass:
184 case Stmt::CXXOperatorCallExprClass:
185 case Stmt::CXXMemberCallExprClass:
186 case Stmt::CXXConstructExprClass:
187 case Stmt::CXXTemporaryObjectExprClass:
188 // FIXME: CXXUnresolvedConstructExpr
189 // FIXME: ObjCImplicitSetterGetterRefExpr?
190 K = CXCursor_CallExpr;
191 break;
192
193 case Stmt::ObjCMessageExprClass:
194 K = CXCursor_ObjCMessageExpr;
195 break;
196 }
197
Douglas Gregorfed36b12010-01-20 23:57:43 +0000198 CXCursor C = { K, { Parent, S, TU } };
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000199 return C;
200}
201
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000202CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorfed36b12010-01-20 23:57:43 +0000203 SourceLocation Loc,
204 ASTUnit *TU) {
Daniel Dunbar2def7eb2010-01-25 00:40:30 +0000205 assert(Super && TU && "Invalid arguments!");
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000206 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorfed36b12010-01-20 23:57:43 +0000207 CXCursor C = { CXCursor_ObjCSuperClassRef, { Super, RawLoc, TU } };
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000208 return C;
209}
210
211std::pair<ObjCInterfaceDecl *, SourceLocation>
212cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
213 assert(C.kind == CXCursor_ObjCSuperClassRef);
214 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
215 SourceLocation::getFromRawEncoding(
216 reinterpret_cast<uintptr_t>(C.data[1])));
217}
218
Douglas Gregoref6eb842010-01-16 15:44:18 +0000219CXCursor cxcursor::MakeCursorObjCProtocolRef(ObjCProtocolDecl *Super,
Douglas Gregorfed36b12010-01-20 23:57:43 +0000220 SourceLocation Loc,
221 ASTUnit *TU) {
Daniel Dunbar2def7eb2010-01-25 00:40:30 +0000222 assert(Super && TU && "Invalid arguments!");
Douglas Gregoref6eb842010-01-16 15:44:18 +0000223 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorfed36b12010-01-20 23:57:43 +0000224 CXCursor C = { CXCursor_ObjCProtocolRef, { Super, RawLoc, TU } };
Douglas Gregoref6eb842010-01-16 15:44:18 +0000225 return C;
226}
227
228std::pair<ObjCProtocolDecl *, SourceLocation>
229cxcursor::getCursorObjCProtocolRef(CXCursor C) {
230 assert(C.kind == CXCursor_ObjCProtocolRef);
231 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
232 SourceLocation::getFromRawEncoding(
233 reinterpret_cast<uintptr_t>(C.data[1])));
234}
235
Douglas Gregor46d66142010-01-16 17:14:40 +0000236CXCursor cxcursor::MakeCursorObjCClassRef(ObjCInterfaceDecl *Class,
Douglas Gregorfed36b12010-01-20 23:57:43 +0000237 SourceLocation Loc,
238 ASTUnit *TU) {
Ted Kremeneke184ac52010-03-19 20:39:03 +0000239 // 'Class' can be null for invalid code.
240 if (!Class)
241 return MakeCXCursorInvalid(CXCursor_InvalidCode);
242 assert(TU && "Invalid arguments!");
Douglas Gregor46d66142010-01-16 17:14:40 +0000243 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorfed36b12010-01-20 23:57:43 +0000244 CXCursor C = { CXCursor_ObjCClassRef, { Class, RawLoc, TU } };
Douglas Gregor46d66142010-01-16 17:14:40 +0000245 return C;
246}
247
248std::pair<ObjCInterfaceDecl *, SourceLocation>
249cxcursor::getCursorObjCClassRef(CXCursor C) {
250 assert(C.kind == CXCursor_ObjCClassRef);
251 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
252 SourceLocation::getFromRawEncoding(
253 reinterpret_cast<uintptr_t>(C.data[1])));
254}
255
Douglas Gregor93f89952010-01-21 16:28:34 +0000256CXCursor cxcursor::MakeCursorTypeRef(TypeDecl *Type, SourceLocation Loc,
257 ASTUnit *TU) {
Daniel Dunbar2def7eb2010-01-25 00:40:30 +0000258 assert(Type && TU && "Invalid arguments!");
Douglas Gregor93f89952010-01-21 16:28:34 +0000259 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
260 CXCursor C = { CXCursor_TypeRef, { Type, RawLoc, TU } };
261 return C;
262}
263
264std::pair<TypeDecl *, SourceLocation>
265cxcursor::getCursorTypeRef(CXCursor C) {
266 assert(C.kind == CXCursor_TypeRef);
267 return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
268 SourceLocation::getFromRawEncoding(
269 reinterpret_cast<uintptr_t>(C.data[1])));
270}
271
Douglas Gregora23e8f72010-08-31 20:37:03 +0000272CXCursor cxcursor::MakeCursorTemplateRef(TemplateDecl *Template,
273 SourceLocation Loc, ASTUnit *TU) {
274 assert(Template && TU && "Invalid arguments!");
275 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
276 CXCursor C = { CXCursor_TemplateRef, { Template, RawLoc, TU } };
277 return C;
278}
279
280std::pair<TemplateDecl *, SourceLocation>
281cxcursor::getCursorTemplateRef(CXCursor C) {
282 assert(C.kind == CXCursor_TemplateRef);
283 return std::make_pair(static_cast<TemplateDecl *>(C.data[0]),
284 SourceLocation::getFromRawEncoding(
285 reinterpret_cast<uintptr_t>(C.data[1])));
286}
287
Douglas Gregora89314e2010-08-31 23:48:11 +0000288CXCursor cxcursor::MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc,
289 ASTUnit *TU) {
290
291 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
292 "Invalid arguments!");
293 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
294 CXCursor C = { CXCursor_NamespaceRef, { NS, RawLoc, TU } };
295 return C;
296}
297
298std::pair<NamedDecl *, SourceLocation>
299cxcursor::getCursorNamespaceRef(CXCursor C) {
300 assert(C.kind == CXCursor_NamespaceRef);
301 return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
302 SourceLocation::getFromRawEncoding(
303 reinterpret_cast<uintptr_t>(C.data[1])));
304}
305
Douglas Gregorf3af3112010-09-09 21:42:20 +0000306CXCursor cxcursor::MakeCursorMemberRef(FieldDecl *Field, SourceLocation Loc,
307 ASTUnit *TU) {
308
309 assert(Field && TU && "Invalid arguments!");
310 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
311 CXCursor C = { CXCursor_MemberRef, { Field, RawLoc, TU } };
312 return C;
313}
314
315std::pair<FieldDecl *, SourceLocation>
316cxcursor::getCursorMemberRef(CXCursor C) {
317 assert(C.kind == CXCursor_MemberRef);
318 return std::make_pair(static_cast<FieldDecl *>(C.data[0]),
319 SourceLocation::getFromRawEncoding(
320 reinterpret_cast<uintptr_t>(C.data[1])));
321}
322
Ted Kremenekae9e2212010-08-27 21:34:58 +0000323CXCursor cxcursor::MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B, ASTUnit *TU){
324 CXCursor C = { CXCursor_CXXBaseSpecifier, { B, 0, TU } };
325 return C;
326}
327
328CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
329 assert(C.kind == CXCursor_CXXBaseSpecifier);
330 return static_cast<CXXBaseSpecifier*>(C.data[0]);
331}
332
Douglas Gregor92a524f2010-03-18 00:42:48 +0000333CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
334 ASTUnit *TU) {
335 CXCursor C = { CXCursor_PreprocessingDirective,
336 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
337 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
338 TU }
339 };
340 return C;
341}
342
343SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
344 assert(C.kind == CXCursor_PreprocessingDirective);
345 return SourceRange(SourceLocation::getFromRawEncoding(
346 reinterpret_cast<uintptr_t> (C.data[0])),
347 SourceLocation::getFromRawEncoding(
348 reinterpret_cast<uintptr_t> (C.data[1])));
349}
350
Douglas Gregor06d6d322010-03-18 18:04:21 +0000351CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI, ASTUnit *TU) {
352 CXCursor C = { CXCursor_MacroDefinition, { MI, 0, TU } };
353 return C;
354}
355
356MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
357 assert(C.kind == CXCursor_MacroDefinition);
358 return static_cast<MacroDefinition *>(C.data[0]);
359}
360
Douglas Gregor065f8d12010-03-18 17:52:52 +0000361CXCursor cxcursor::MakeMacroInstantiationCursor(MacroInstantiation *MI,
Douglas Gregor02ded2a2010-03-18 15:23:44 +0000362 ASTUnit *TU) {
Douglas Gregor065f8d12010-03-18 17:52:52 +0000363 CXCursor C = { CXCursor_MacroInstantiation, { MI, 0, TU } };
Douglas Gregor02ded2a2010-03-18 15:23:44 +0000364 return C;
365}
366
Douglas Gregor065f8d12010-03-18 17:52:52 +0000367MacroInstantiation *cxcursor::getCursorMacroInstantiation(CXCursor C) {
Douglas Gregor02ded2a2010-03-18 15:23:44 +0000368 assert(C.kind == CXCursor_MacroInstantiation);
Douglas Gregor065f8d12010-03-18 17:52:52 +0000369 return static_cast<MacroInstantiation *>(C.data[0]);
Douglas Gregor02ded2a2010-03-18 15:23:44 +0000370}
371
Douglas Gregor796d76a2010-10-20 22:00:55 +0000372CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
373 ASTUnit *TU) {
374 CXCursor C = { CXCursor_InclusionDirective, { ID, 0, TU } };
375 return C;
376}
377
378InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
379 assert(C.kind == CXCursor_InclusionDirective);
380 return static_cast<InclusionDirective *>(C.data[0]);
381}
382
Douglas Gregora93ab662010-09-10 00:22:18 +0000383CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
384 ASTUnit *TU) {
385
386 assert(Label && TU && "Invalid arguments!");
387 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
388 CXCursor C = { CXCursor_LabelRef, { Label, RawLoc, TU } };
389 return C;
390}
391
392std::pair<LabelStmt*, SourceLocation>
393cxcursor::getCursorLabelRef(CXCursor C) {
394 assert(C.kind == CXCursor_LabelRef);
395 return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
396 SourceLocation::getFromRawEncoding(
397 reinterpret_cast<uintptr_t>(C.data[1])));
398}
399
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000400CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E,
401 ASTUnit *TU) {
402 assert(E && TU && "Invalid arguments!");
403 OverloadedDeclRefStorage Storage(E);
404 void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding());
405 CXCursor C = {
406 CXCursor_OverloadedDeclRef,
407 { Storage.getOpaqueValue(), RawLoc, TU }
408 };
409 return C;
410}
411
412CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D,
413 SourceLocation Loc,
414 ASTUnit *TU) {
415 assert(D && TU && "Invalid arguments!");
416 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
417 OverloadedDeclRefStorage Storage(D);
418 CXCursor C = {
419 CXCursor_OverloadedDeclRef,
420 { Storage.getOpaqueValue(), RawLoc, TU }
421 };
422 return C;
423}
424
425CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
426 SourceLocation Loc,
427 ASTUnit *TU) {
428 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
429 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
430 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
431 CXCursor C = {
432 CXCursor_OverloadedDeclRef,
433 { Storage.getOpaqueValue(), RawLoc, TU }
434 };
435 return C;
436}
437
438std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
439cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
440 assert(C.kind == CXCursor_OverloadedDeclRef);
441 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]),
442 SourceLocation::getFromRawEncoding(
443 reinterpret_cast<uintptr_t>(C.data[1])));
444}
445
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000446Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
447 return (Decl *)Cursor.data[0];
448}
449
450Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
451 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
452}
453
454Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregoref6eb842010-01-16 15:44:18 +0000455 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor46d66142010-01-16 17:14:40 +0000456 Cursor.kind == CXCursor_ObjCProtocolRef ||
457 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000458 return 0;
459
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000460 return (Stmt *)Cursor.data[1];
461}
462
Ted Kremeneka5940822010-08-26 01:42:22 +0000463Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
464 return (Attr *)Cursor.data[1];
465}
466
Douglas Gregor7ecd0202010-01-18 23:41:10 +0000467ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorfed36b12010-01-20 23:57:43 +0000468 return getCursorASTUnit(Cursor)->getASTContext();
469}
Douglas Gregor7ecd0202010-01-18 23:41:10 +0000470
Douglas Gregorfed36b12010-01-20 23:57:43 +0000471ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
472 return static_cast<ASTUnit *>(Cursor.data[2]);
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000473}
474
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000475bool cxcursor::operator==(CXCursor X, CXCursor Y) {
476 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
477 X.data[2] == Y.data[2];
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000478}
Ted Kremenek818e5c12010-11-01 23:26:51 +0000479
480// FIXME: Remove once we can model DeclGroups and their appropriate ranges
481// properly in the ASTs.
482bool cxcursor::isFirstInDeclGroup(CXCursor C) {
483 assert(clang_isDeclaration(C.kind));
484 return ((uintptr_t) (C.data[1])) != 0;
485}
486