blob: 9dd94f972ef27dcb6ddb474f751cc462b34feb40 [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"
Douglas Gregor1f60d9e2010-09-13 22:52:57 +000022#include "clang/AST/ExprCXX.h"
Ted Kremenek007a7c92010-11-01 23:26:51 +000023#include "clang-c/Index.h"
Ted Kremenekedc8aa62010-01-16 00:36:30 +000024#include "llvm/Support/ErrorHandling.h"
Ted Kremenek16c440a2010-01-15 20:35:54 +000025
26using namespace clang;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +000027using namespace cxcursor;
Ted Kremenek16c440a2010-01-15 20:35:54 +000028
Douglas Gregor5bfb8c12010-01-20 23:34:41 +000029CXCursor cxcursor::MakeCXCursorInvalid(CXCursorKind K) {
30 assert(K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid);
31 CXCursor C = { K, { 0, 0, 0 } };
32 return C;
Ted Kremenek16c440a2010-01-15 20:35:54 +000033}
34
Ted Kremeneke77f4432010-02-18 03:09:07 +000035static CXCursorKind GetCursorKind(const Attr *A) {
36 assert(A && "Invalid arguments!");
37 switch (A->getKind()) {
38 default: break;
Sean Hunt387475d2010-06-16 23:43:53 +000039 case attr::IBAction: return CXCursor_IBActionAttr;
40 case attr::IBOutlet: return CXCursor_IBOutletAttr;
41 case attr::IBOutletCollection: return CXCursor_IBOutletCollectionAttr;
Ted Kremeneke77f4432010-02-18 03:09:07 +000042 }
43
44 return CXCursor_UnexposedAttr;
45}
46
47CXCursor cxcursor::MakeCXCursor(const Attr *A, Decl *Parent, ASTUnit *TU) {
48 assert(A && Parent && TU && "Invalid arguments!");
49 CXCursor C = { GetCursorKind(A), { Parent, (void*)A, TU } };
50 return C;
51}
52
Ted Kremenek007a7c92010-11-01 23:26:51 +000053CXCursor cxcursor::MakeCXCursor(Decl *D, ASTUnit *TU,
54 bool FirstInDeclGroup) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +000055 assert(D && TU && "Invalid arguments!");
Ted Kremenek007a7c92010-11-01 23:26:51 +000056 CXCursor C = { getCursorKindForDecl(D),
57 { D, (void*) (FirstInDeclGroup ? 1 : 0), TU }
58 };
Douglas Gregor5bfb8c12010-01-20 23:34:41 +000059 return C;
Ted Kremenekedc8aa62010-01-16 00:36:30 +000060}
61
Douglas Gregorb2cd4872010-01-20 23:57:43 +000062CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, ASTUnit *TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +000063 assert(S && TU && "Invalid arguments!");
Douglas Gregor97b98722010-01-19 23:20:36 +000064 CXCursorKind K = CXCursor_NotImplemented;
65
66 switch (S->getStmtClass()) {
67 case Stmt::NoStmtClass:
68 break;
69
70 case Stmt::NullStmtClass:
71 case Stmt::CompoundStmtClass:
72 case Stmt::CaseStmtClass:
73 case Stmt::DefaultStmtClass:
Douglas Gregor97b98722010-01-19 23:20:36 +000074 case Stmt::IfStmtClass:
75 case Stmt::SwitchStmtClass:
76 case Stmt::WhileStmtClass:
77 case Stmt::DoStmtClass:
78 case Stmt::ForStmtClass:
79 case Stmt::GotoStmtClass:
80 case Stmt::IndirectGotoStmtClass:
81 case Stmt::ContinueStmtClass:
82 case Stmt::BreakStmtClass:
83 case Stmt::ReturnStmtClass:
84 case Stmt::DeclStmtClass:
85 case Stmt::SwitchCaseClass:
86 case Stmt::AsmStmtClass:
87 case Stmt::ObjCAtTryStmtClass:
88 case Stmt::ObjCAtCatchStmtClass:
89 case Stmt::ObjCAtFinallyStmtClass:
90 case Stmt::ObjCAtThrowStmtClass:
91 case Stmt::ObjCAtSynchronizedStmtClass:
92 case Stmt::ObjCForCollectionStmtClass:
93 case Stmt::CXXCatchStmtClass:
94 case Stmt::CXXTryStmtClass:
95 K = CXCursor_UnexposedStmt;
96 break;
97
Douglas Gregor36897b02010-09-10 00:22:18 +000098 case Stmt::LabelStmtClass:
99 K = CXCursor_LabelStmt;
100 break;
101
Douglas Gregor97b98722010-01-19 23:20:36 +0000102 case Stmt::PredefinedExprClass:
103 case Stmt::IntegerLiteralClass:
104 case Stmt::FloatingLiteralClass:
105 case Stmt::ImaginaryLiteralClass:
106 case Stmt::StringLiteralClass:
107 case Stmt::CharacterLiteralClass:
108 case Stmt::ParenExprClass:
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000109 case Stmt::UnaryOperatorClass:
110 case Stmt::OffsetOfExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000111 case Stmt::SizeOfAlignOfExprClass:
112 case Stmt::ArraySubscriptExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000113 case Stmt::BinaryOperatorClass:
114 case Stmt::CompoundAssignOperatorClass:
115 case Stmt::ConditionalOperatorClass:
116 case Stmt::ImplicitCastExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000117 case Stmt::CStyleCastExprClass:
118 case Stmt::CompoundLiteralExprClass:
119 case Stmt::ExtVectorElementExprClass:
120 case Stmt::InitListExprClass:
121 case Stmt::DesignatedInitExprClass:
122 case Stmt::ImplicitValueInitExprClass:
123 case Stmt::ParenListExprClass:
124 case Stmt::VAArgExprClass:
125 case Stmt::AddrLabelExprClass:
126 case Stmt::StmtExprClass:
127 case Stmt::TypesCompatibleExprClass:
128 case Stmt::ChooseExprClass:
129 case Stmt::GNUNullExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000130 case Stmt::CXXStaticCastExprClass:
131 case Stmt::CXXDynamicCastExprClass:
132 case Stmt::CXXReinterpretCastExprClass:
133 case Stmt::CXXConstCastExprClass:
134 case Stmt::CXXFunctionalCastExprClass:
135 case Stmt::CXXTypeidExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +0000136 case Stmt::CXXUuidofExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000137 case Stmt::CXXBoolLiteralExprClass:
138 case Stmt::CXXNullPtrLiteralExprClass:
139 case Stmt::CXXThisExprClass:
140 case Stmt::CXXThrowExprClass:
141 case Stmt::CXXDefaultArgExprClass:
Douglas Gregored8abf12010-07-08 06:14:04 +0000142 case Stmt::CXXScalarValueInitExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000143 case Stmt::CXXNewExprClass:
144 case Stmt::CXXDeleteExprClass:
145 case Stmt::CXXPseudoDestructorExprClass:
146 case Stmt::UnresolvedLookupExprClass:
147 case Stmt::UnaryTypeTraitExprClass:
148 case Stmt::DependentScopeDeclRefExprClass:
149 case Stmt::CXXBindTemporaryExprClass:
150 case Stmt::CXXExprWithTemporariesClass:
151 case Stmt::CXXUnresolvedConstructExprClass:
152 case Stmt::CXXDependentScopeMemberExprClass:
153 case Stmt::UnresolvedMemberExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +0000154 case Stmt::CXXNoexceptExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000155 case Stmt::ObjCStringLiteralClass:
156 case Stmt::ObjCEncodeExprClass:
157 case Stmt::ObjCSelectorExprClass:
158 case Stmt::ObjCProtocolExprClass:
159 case Stmt::ObjCImplicitSetterGetterRefExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000160 case Stmt::ObjCIsaExprClass:
161 case Stmt::ShuffleVectorExprClass:
162 case Stmt::BlockExprClass:
163 K = CXCursor_UnexposedExpr;
164 break;
Douglas Gregor36897b02010-09-10 00:22:18 +0000165
Douglas Gregor97b98722010-01-19 23:20:36 +0000166 case Stmt::DeclRefExprClass:
167 case Stmt::BlockDeclRefExprClass:
168 // FIXME: UnresolvedLookupExpr?
169 // FIXME: DependentScopeDeclRefExpr?
170 K = CXCursor_DeclRefExpr;
171 break;
172
173 case Stmt::MemberExprClass:
174 case Stmt::ObjCIvarRefExprClass:
175 case Stmt::ObjCPropertyRefExprClass:
176 // FIXME: UnresolvedMemberExpr?
177 // FIXME: CXXDependentScopeMemberExpr?
178 K = CXCursor_MemberRefExpr;
179 break;
180
181 case Stmt::CallExprClass:
182 case Stmt::CXXOperatorCallExprClass:
183 case Stmt::CXXMemberCallExprClass:
184 case Stmt::CXXConstructExprClass:
185 case Stmt::CXXTemporaryObjectExprClass:
186 // FIXME: CXXUnresolvedConstructExpr
187 // FIXME: ObjCImplicitSetterGetterRefExpr?
188 K = CXCursor_CallExpr;
189 break;
190
191 case Stmt::ObjCMessageExprClass:
192 K = CXCursor_ObjCMessageExpr;
193 break;
194 }
195
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000196 CXCursor C = { K, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000197 return C;
198}
199
Douglas Gregor2e331b92010-01-16 14:00:32 +0000200CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000201 SourceLocation Loc,
202 ASTUnit *TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000203 assert(Super && TU && "Invalid arguments!");
Douglas Gregor2e331b92010-01-16 14:00:32 +0000204 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000205 CXCursor C = { CXCursor_ObjCSuperClassRef, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000206 return C;
207}
208
209std::pair<ObjCInterfaceDecl *, SourceLocation>
210cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
211 assert(C.kind == CXCursor_ObjCSuperClassRef);
212 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
213 SourceLocation::getFromRawEncoding(
214 reinterpret_cast<uintptr_t>(C.data[1])));
215}
216
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000217CXCursor cxcursor::MakeCursorObjCProtocolRef(ObjCProtocolDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000218 SourceLocation Loc,
219 ASTUnit *TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000220 assert(Super && TU && "Invalid arguments!");
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000221 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000222 CXCursor C = { CXCursor_ObjCProtocolRef, { Super, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000223 return C;
224}
225
226std::pair<ObjCProtocolDecl *, SourceLocation>
227cxcursor::getCursorObjCProtocolRef(CXCursor C) {
228 assert(C.kind == CXCursor_ObjCProtocolRef);
229 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
230 SourceLocation::getFromRawEncoding(
231 reinterpret_cast<uintptr_t>(C.data[1])));
232}
233
Douglas Gregor1adb0822010-01-16 17:14:40 +0000234CXCursor cxcursor::MakeCursorObjCClassRef(ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000235 SourceLocation Loc,
236 ASTUnit *TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000237 // 'Class' can be null for invalid code.
238 if (!Class)
239 return MakeCXCursorInvalid(CXCursor_InvalidCode);
240 assert(TU && "Invalid arguments!");
Douglas Gregor1adb0822010-01-16 17:14:40 +0000241 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000242 CXCursor C = { CXCursor_ObjCClassRef, { Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000243 return C;
244}
245
246std::pair<ObjCInterfaceDecl *, SourceLocation>
247cxcursor::getCursorObjCClassRef(CXCursor C) {
248 assert(C.kind == CXCursor_ObjCClassRef);
249 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
250 SourceLocation::getFromRawEncoding(
251 reinterpret_cast<uintptr_t>(C.data[1])));
252}
253
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000254CXCursor cxcursor::MakeCursorTypeRef(TypeDecl *Type, SourceLocation Loc,
255 ASTUnit *TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000256 assert(Type && TU && "Invalid arguments!");
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000257 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
258 CXCursor C = { CXCursor_TypeRef, { Type, RawLoc, TU } };
259 return C;
260}
261
262std::pair<TypeDecl *, SourceLocation>
263cxcursor::getCursorTypeRef(CXCursor C) {
264 assert(C.kind == CXCursor_TypeRef);
265 return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
266 SourceLocation::getFromRawEncoding(
267 reinterpret_cast<uintptr_t>(C.data[1])));
268}
269
Douglas Gregor0b36e612010-08-31 20:37:03 +0000270CXCursor cxcursor::MakeCursorTemplateRef(TemplateDecl *Template,
271 SourceLocation Loc, ASTUnit *TU) {
272 assert(Template && TU && "Invalid arguments!");
273 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
274 CXCursor C = { CXCursor_TemplateRef, { Template, RawLoc, TU } };
275 return C;
276}
277
278std::pair<TemplateDecl *, SourceLocation>
279cxcursor::getCursorTemplateRef(CXCursor C) {
280 assert(C.kind == CXCursor_TemplateRef);
281 return std::make_pair(static_cast<TemplateDecl *>(C.data[0]),
282 SourceLocation::getFromRawEncoding(
283 reinterpret_cast<uintptr_t>(C.data[1])));
284}
285
Douglas Gregor69319002010-08-31 23:48:11 +0000286CXCursor cxcursor::MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc,
287 ASTUnit *TU) {
288
289 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
290 "Invalid arguments!");
291 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
292 CXCursor C = { CXCursor_NamespaceRef, { NS, RawLoc, TU } };
293 return C;
294}
295
296std::pair<NamedDecl *, SourceLocation>
297cxcursor::getCursorNamespaceRef(CXCursor C) {
298 assert(C.kind == CXCursor_NamespaceRef);
299 return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
300 SourceLocation::getFromRawEncoding(
301 reinterpret_cast<uintptr_t>(C.data[1])));
302}
303
Douglas Gregora67e03f2010-09-09 21:42:20 +0000304CXCursor cxcursor::MakeCursorMemberRef(FieldDecl *Field, SourceLocation Loc,
305 ASTUnit *TU) {
306
307 assert(Field && TU && "Invalid arguments!");
308 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
309 CXCursor C = { CXCursor_MemberRef, { Field, RawLoc, TU } };
310 return C;
311}
312
313std::pair<FieldDecl *, SourceLocation>
314cxcursor::getCursorMemberRef(CXCursor C) {
315 assert(C.kind == CXCursor_MemberRef);
316 return std::make_pair(static_cast<FieldDecl *>(C.data[0]),
317 SourceLocation::getFromRawEncoding(
318 reinterpret_cast<uintptr_t>(C.data[1])));
319}
320
Ted Kremenek3064ef92010-08-27 21:34:58 +0000321CXCursor cxcursor::MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B, ASTUnit *TU){
322 CXCursor C = { CXCursor_CXXBaseSpecifier, { B, 0, TU } };
323 return C;
324}
325
326CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
327 assert(C.kind == CXCursor_CXXBaseSpecifier);
328 return static_cast<CXXBaseSpecifier*>(C.data[0]);
329}
330
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000331CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
332 ASTUnit *TU) {
333 CXCursor C = { CXCursor_PreprocessingDirective,
334 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
335 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
336 TU }
337 };
338 return C;
339}
340
341SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
342 assert(C.kind == CXCursor_PreprocessingDirective);
343 return SourceRange(SourceLocation::getFromRawEncoding(
344 reinterpret_cast<uintptr_t> (C.data[0])),
345 SourceLocation::getFromRawEncoding(
346 reinterpret_cast<uintptr_t> (C.data[1])));
347}
348
Douglas Gregor572feb22010-03-18 18:04:21 +0000349CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI, ASTUnit *TU) {
350 CXCursor C = { CXCursor_MacroDefinition, { MI, 0, TU } };
351 return C;
352}
353
354MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
355 assert(C.kind == CXCursor_MacroDefinition);
356 return static_cast<MacroDefinition *>(C.data[0]);
357}
358
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000359CXCursor cxcursor::MakeMacroInstantiationCursor(MacroInstantiation *MI,
Douglas Gregor48072312010-03-18 15:23:44 +0000360 ASTUnit *TU) {
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000361 CXCursor C = { CXCursor_MacroInstantiation, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000362 return C;
363}
364
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000365MacroInstantiation *cxcursor::getCursorMacroInstantiation(CXCursor C) {
Douglas Gregor48072312010-03-18 15:23:44 +0000366 assert(C.kind == CXCursor_MacroInstantiation);
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000367 return static_cast<MacroInstantiation *>(C.data[0]);
Douglas Gregor48072312010-03-18 15:23:44 +0000368}
369
Douglas Gregorecdcb882010-10-20 22:00:55 +0000370CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
371 ASTUnit *TU) {
372 CXCursor C = { CXCursor_InclusionDirective, { ID, 0, TU } };
373 return C;
374}
375
376InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
377 assert(C.kind == CXCursor_InclusionDirective);
378 return static_cast<InclusionDirective *>(C.data[0]);
379}
380
Douglas Gregor36897b02010-09-10 00:22:18 +0000381CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
382 ASTUnit *TU) {
383
384 assert(Label && TU && "Invalid arguments!");
385 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
386 CXCursor C = { CXCursor_LabelRef, { Label, RawLoc, TU } };
387 return C;
388}
389
390std::pair<LabelStmt*, SourceLocation>
391cxcursor::getCursorLabelRef(CXCursor C) {
392 assert(C.kind == CXCursor_LabelRef);
393 return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
394 SourceLocation::getFromRawEncoding(
395 reinterpret_cast<uintptr_t>(C.data[1])));
396}
397
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000398CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E,
399 ASTUnit *TU) {
400 assert(E && TU && "Invalid arguments!");
401 OverloadedDeclRefStorage Storage(E);
402 void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding());
403 CXCursor C = {
404 CXCursor_OverloadedDeclRef,
405 { Storage.getOpaqueValue(), RawLoc, TU }
406 };
407 return C;
408}
409
410CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D,
411 SourceLocation Loc,
412 ASTUnit *TU) {
413 assert(D && TU && "Invalid arguments!");
414 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
415 OverloadedDeclRefStorage Storage(D);
416 CXCursor C = {
417 CXCursor_OverloadedDeclRef,
418 { Storage.getOpaqueValue(), RawLoc, TU }
419 };
420 return C;
421}
422
423CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
424 SourceLocation Loc,
425 ASTUnit *TU) {
426 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
427 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
428 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
429 CXCursor C = {
430 CXCursor_OverloadedDeclRef,
431 { Storage.getOpaqueValue(), RawLoc, TU }
432 };
433 return C;
434}
435
436std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
437cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
438 assert(C.kind == CXCursor_OverloadedDeclRef);
439 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]),
440 SourceLocation::getFromRawEncoding(
441 reinterpret_cast<uintptr_t>(C.data[1])));
442}
443
Douglas Gregor283cae32010-01-15 21:56:13 +0000444Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
445 return (Decl *)Cursor.data[0];
446}
447
448Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
449 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
450}
451
452Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000453 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000454 Cursor.kind == CXCursor_ObjCProtocolRef ||
455 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000456 return 0;
457
Douglas Gregor283cae32010-01-15 21:56:13 +0000458 return (Stmt *)Cursor.data[1];
459}
460
Ted Kremenek95f33552010-08-26 01:42:22 +0000461Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
462 return (Attr *)Cursor.data[1];
463}
464
Douglas Gregorf46034a2010-01-18 23:41:10 +0000465ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000466 return getCursorASTUnit(Cursor)->getASTContext();
467}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000468
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000469ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
470 return static_cast<ASTUnit *>(Cursor.data[2]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000471}
472
Douglas Gregor283cae32010-01-15 21:56:13 +0000473bool cxcursor::operator==(CXCursor X, CXCursor Y) {
474 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
475 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000476}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000477
478// FIXME: Remove once we can model DeclGroups and their appropriate ranges
479// properly in the ASTs.
480bool cxcursor::isFirstInDeclGroup(CXCursor C) {
481 assert(clang_isDeclaration(C.kind));
482 return ((uintptr_t) (C.data[1])) != 0;
483}
484