blob: f5941e7327fe1729e1696865041a86234deb3b3b [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:
John McCall7cd7d1a2010-11-15 23:31:06 +0000163 case Stmt::OpaqueValueExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000164 K = CXCursor_UnexposedExpr;
165 break;
Douglas Gregor36897b02010-09-10 00:22:18 +0000166
Douglas Gregor97b98722010-01-19 23:20:36 +0000167 case Stmt::DeclRefExprClass:
168 case Stmt::BlockDeclRefExprClass:
169 // FIXME: UnresolvedLookupExpr?
170 // FIXME: DependentScopeDeclRefExpr?
171 K = CXCursor_DeclRefExpr;
172 break;
173
174 case Stmt::MemberExprClass:
175 case Stmt::ObjCIvarRefExprClass:
176 case Stmt::ObjCPropertyRefExprClass:
177 // FIXME: UnresolvedMemberExpr?
178 // FIXME: CXXDependentScopeMemberExpr?
179 K = CXCursor_MemberRefExpr;
180 break;
181
182 case Stmt::CallExprClass:
183 case Stmt::CXXOperatorCallExprClass:
184 case Stmt::CXXMemberCallExprClass:
185 case Stmt::CXXConstructExprClass:
186 case Stmt::CXXTemporaryObjectExprClass:
187 // FIXME: CXXUnresolvedConstructExpr
188 // FIXME: ObjCImplicitSetterGetterRefExpr?
189 K = CXCursor_CallExpr;
190 break;
191
192 case Stmt::ObjCMessageExprClass:
193 K = CXCursor_ObjCMessageExpr;
194 break;
195 }
196
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000197 CXCursor C = { K, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000198 return C;
199}
200
Douglas Gregor2e331b92010-01-16 14:00:32 +0000201CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000202 SourceLocation Loc,
203 ASTUnit *TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000204 assert(Super && TU && "Invalid arguments!");
Douglas Gregor2e331b92010-01-16 14:00:32 +0000205 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000206 CXCursor C = { CXCursor_ObjCSuperClassRef, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000207 return C;
208}
209
210std::pair<ObjCInterfaceDecl *, SourceLocation>
211cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
212 assert(C.kind == CXCursor_ObjCSuperClassRef);
213 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
214 SourceLocation::getFromRawEncoding(
215 reinterpret_cast<uintptr_t>(C.data[1])));
216}
217
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000218CXCursor cxcursor::MakeCursorObjCProtocolRef(ObjCProtocolDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000219 SourceLocation Loc,
220 ASTUnit *TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000221 assert(Super && TU && "Invalid arguments!");
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000222 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000223 CXCursor C = { CXCursor_ObjCProtocolRef, { Super, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000224 return C;
225}
226
227std::pair<ObjCProtocolDecl *, SourceLocation>
228cxcursor::getCursorObjCProtocolRef(CXCursor C) {
229 assert(C.kind == CXCursor_ObjCProtocolRef);
230 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
231 SourceLocation::getFromRawEncoding(
232 reinterpret_cast<uintptr_t>(C.data[1])));
233}
234
Douglas Gregor1adb0822010-01-16 17:14:40 +0000235CXCursor cxcursor::MakeCursorObjCClassRef(ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000236 SourceLocation Loc,
237 ASTUnit *TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000238 // 'Class' can be null for invalid code.
239 if (!Class)
240 return MakeCXCursorInvalid(CXCursor_InvalidCode);
241 assert(TU && "Invalid arguments!");
Douglas Gregor1adb0822010-01-16 17:14:40 +0000242 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000243 CXCursor C = { CXCursor_ObjCClassRef, { Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000244 return C;
245}
246
247std::pair<ObjCInterfaceDecl *, SourceLocation>
248cxcursor::getCursorObjCClassRef(CXCursor C) {
249 assert(C.kind == CXCursor_ObjCClassRef);
250 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
251 SourceLocation::getFromRawEncoding(
252 reinterpret_cast<uintptr_t>(C.data[1])));
253}
254
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000255CXCursor cxcursor::MakeCursorTypeRef(TypeDecl *Type, SourceLocation Loc,
256 ASTUnit *TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000257 assert(Type && TU && "Invalid arguments!");
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000258 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
259 CXCursor C = { CXCursor_TypeRef, { Type, RawLoc, TU } };
260 return C;
261}
262
263std::pair<TypeDecl *, SourceLocation>
264cxcursor::getCursorTypeRef(CXCursor C) {
265 assert(C.kind == CXCursor_TypeRef);
266 return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
267 SourceLocation::getFromRawEncoding(
268 reinterpret_cast<uintptr_t>(C.data[1])));
269}
270
Douglas Gregor0b36e612010-08-31 20:37:03 +0000271CXCursor cxcursor::MakeCursorTemplateRef(TemplateDecl *Template,
272 SourceLocation Loc, ASTUnit *TU) {
273 assert(Template && TU && "Invalid arguments!");
274 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
275 CXCursor C = { CXCursor_TemplateRef, { Template, RawLoc, TU } };
276 return C;
277}
278
279std::pair<TemplateDecl *, SourceLocation>
280cxcursor::getCursorTemplateRef(CXCursor C) {
281 assert(C.kind == CXCursor_TemplateRef);
282 return std::make_pair(static_cast<TemplateDecl *>(C.data[0]),
283 SourceLocation::getFromRawEncoding(
284 reinterpret_cast<uintptr_t>(C.data[1])));
285}
286
Douglas Gregor69319002010-08-31 23:48:11 +0000287CXCursor cxcursor::MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc,
288 ASTUnit *TU) {
289
290 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
291 "Invalid arguments!");
292 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
293 CXCursor C = { CXCursor_NamespaceRef, { NS, RawLoc, TU } };
294 return C;
295}
296
297std::pair<NamedDecl *, SourceLocation>
298cxcursor::getCursorNamespaceRef(CXCursor C) {
299 assert(C.kind == CXCursor_NamespaceRef);
300 return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
301 SourceLocation::getFromRawEncoding(
302 reinterpret_cast<uintptr_t>(C.data[1])));
303}
304
Douglas Gregora67e03f2010-09-09 21:42:20 +0000305CXCursor cxcursor::MakeCursorMemberRef(FieldDecl *Field, SourceLocation Loc,
306 ASTUnit *TU) {
307
308 assert(Field && TU && "Invalid arguments!");
309 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
310 CXCursor C = { CXCursor_MemberRef, { Field, RawLoc, TU } };
311 return C;
312}
313
314std::pair<FieldDecl *, SourceLocation>
315cxcursor::getCursorMemberRef(CXCursor C) {
316 assert(C.kind == CXCursor_MemberRef);
317 return std::make_pair(static_cast<FieldDecl *>(C.data[0]),
318 SourceLocation::getFromRawEncoding(
319 reinterpret_cast<uintptr_t>(C.data[1])));
320}
321
Ted Kremenek3064ef92010-08-27 21:34:58 +0000322CXCursor cxcursor::MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B, ASTUnit *TU){
323 CXCursor C = { CXCursor_CXXBaseSpecifier, { B, 0, TU } };
324 return C;
325}
326
327CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
328 assert(C.kind == CXCursor_CXXBaseSpecifier);
329 return static_cast<CXXBaseSpecifier*>(C.data[0]);
330}
331
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000332CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
333 ASTUnit *TU) {
334 CXCursor C = { CXCursor_PreprocessingDirective,
335 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
336 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
337 TU }
338 };
339 return C;
340}
341
342SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
343 assert(C.kind == CXCursor_PreprocessingDirective);
344 return SourceRange(SourceLocation::getFromRawEncoding(
345 reinterpret_cast<uintptr_t> (C.data[0])),
346 SourceLocation::getFromRawEncoding(
347 reinterpret_cast<uintptr_t> (C.data[1])));
348}
349
Douglas Gregor572feb22010-03-18 18:04:21 +0000350CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI, ASTUnit *TU) {
351 CXCursor C = { CXCursor_MacroDefinition, { MI, 0, TU } };
352 return C;
353}
354
355MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
356 assert(C.kind == CXCursor_MacroDefinition);
357 return static_cast<MacroDefinition *>(C.data[0]);
358}
359
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000360CXCursor cxcursor::MakeMacroInstantiationCursor(MacroInstantiation *MI,
Douglas Gregor48072312010-03-18 15:23:44 +0000361 ASTUnit *TU) {
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000362 CXCursor C = { CXCursor_MacroInstantiation, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000363 return C;
364}
365
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000366MacroInstantiation *cxcursor::getCursorMacroInstantiation(CXCursor C) {
Douglas Gregor48072312010-03-18 15:23:44 +0000367 assert(C.kind == CXCursor_MacroInstantiation);
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000368 return static_cast<MacroInstantiation *>(C.data[0]);
Douglas Gregor48072312010-03-18 15:23:44 +0000369}
370
Douglas Gregorecdcb882010-10-20 22:00:55 +0000371CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
372 ASTUnit *TU) {
373 CXCursor C = { CXCursor_InclusionDirective, { ID, 0, TU } };
374 return C;
375}
376
377InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
378 assert(C.kind == CXCursor_InclusionDirective);
379 return static_cast<InclusionDirective *>(C.data[0]);
380}
381
Douglas Gregor36897b02010-09-10 00:22:18 +0000382CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
383 ASTUnit *TU) {
384
385 assert(Label && TU && "Invalid arguments!");
386 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
387 CXCursor C = { CXCursor_LabelRef, { Label, RawLoc, TU } };
388 return C;
389}
390
391std::pair<LabelStmt*, SourceLocation>
392cxcursor::getCursorLabelRef(CXCursor C) {
393 assert(C.kind == CXCursor_LabelRef);
394 return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
395 SourceLocation::getFromRawEncoding(
396 reinterpret_cast<uintptr_t>(C.data[1])));
397}
398
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000399CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E,
400 ASTUnit *TU) {
401 assert(E && TU && "Invalid arguments!");
402 OverloadedDeclRefStorage Storage(E);
403 void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding());
404 CXCursor C = {
405 CXCursor_OverloadedDeclRef,
406 { Storage.getOpaqueValue(), RawLoc, TU }
407 };
408 return C;
409}
410
411CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D,
412 SourceLocation Loc,
413 ASTUnit *TU) {
414 assert(D && TU && "Invalid arguments!");
415 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
416 OverloadedDeclRefStorage Storage(D);
417 CXCursor C = {
418 CXCursor_OverloadedDeclRef,
419 { Storage.getOpaqueValue(), RawLoc, TU }
420 };
421 return C;
422}
423
424CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
425 SourceLocation Loc,
426 ASTUnit *TU) {
427 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
428 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
429 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
430 CXCursor C = {
431 CXCursor_OverloadedDeclRef,
432 { Storage.getOpaqueValue(), RawLoc, TU }
433 };
434 return C;
435}
436
437std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
438cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
439 assert(C.kind == CXCursor_OverloadedDeclRef);
440 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]),
441 SourceLocation::getFromRawEncoding(
442 reinterpret_cast<uintptr_t>(C.data[1])));
443}
444
Douglas Gregor283cae32010-01-15 21:56:13 +0000445Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
446 return (Decl *)Cursor.data[0];
447}
448
449Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
450 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
451}
452
453Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000454 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000455 Cursor.kind == CXCursor_ObjCProtocolRef ||
456 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000457 return 0;
458
Douglas Gregor283cae32010-01-15 21:56:13 +0000459 return (Stmt *)Cursor.data[1];
460}
461
Ted Kremenek95f33552010-08-26 01:42:22 +0000462Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
463 return (Attr *)Cursor.data[1];
464}
465
Douglas Gregorf46034a2010-01-18 23:41:10 +0000466ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000467 return getCursorASTUnit(Cursor)->getASTContext();
468}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000469
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000470ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
471 return static_cast<ASTUnit *>(Cursor.data[2]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000472}
473
Douglas Gregor283cae32010-01-15 21:56:13 +0000474bool cxcursor::operator==(CXCursor X, CXCursor Y) {
475 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
476 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000477}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000478
479// FIXME: Remove once we can model DeclGroups and their appropriate ranges
480// properly in the ASTs.
481bool cxcursor::isFirstInDeclGroup(CXCursor C) {
482 assert(clang_isDeclaration(C.kind));
483 return ((uintptr_t) (C.data[1])) != 0;
484}
485