CXCursor.cpp revision ed122735639d83c10f18c28c7fd117bfcd0f62cb
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===- CXCursor.cpp - Routines for manipulating CXCursors -----------------===//
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                     The LLVM Compiler Infrastructure
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// License. See LICENSE.TXT for details.
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===//
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file defines routines for manipulating CXCursors. It should be the
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// only file that has internal knowledge of the encoding of the data in
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// CXCursor.
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===//
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "CXCursor.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "CXString.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Frontend/ASTUnit.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/Decl.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/DeclCXX.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/DeclObjC.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/Expr.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/ExprCXX.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang-c/Index.h"
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Support/ErrorHandling.h"
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using namespace clang;
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using namespace cxcursor;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)CXCursor cxcursor::MakeCXCursorInvalid(CXCursorKind K) {
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  assert(K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid);
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CXCursor C = { K, { 0, 0, 0 } };
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return C;
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static CXCursorKind GetCursorKind(const Attr *A) {
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  assert(A && "Invalid arguments!");
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (A->getKind()) {
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default: break;
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case attr::IBAction: return CXCursor_IBActionAttr;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case attr::IBOutlet: return CXCursor_IBOutletAttr;
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case attr::IBOutletCollection: return CXCursor_IBOutletCollectionAttr;
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return CXCursor_UnexposedAttr;
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)CXCursor cxcursor::MakeCXCursor(const Attr *A, Decl *Parent, ASTUnit *TU) {
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  assert(A && Parent && TU && "Invalid arguments!");
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CXCursor C = { GetCursorKind(A), { Parent, (void*)A, TU } };
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return C;
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
54CXCursor cxcursor::MakeCXCursor(Decl *D, ASTUnit *TU,
55                                bool FirstInDeclGroup) {
56  assert(D && TU && "Invalid arguments!");
57  CXCursor C = { getCursorKindForDecl(D),
58                 { D, (void*) (FirstInDeclGroup ? 1 : 0), TU }
59               };
60  return C;
61}
62
63CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, ASTUnit *TU) {
64  assert(S && TU && "Invalid arguments!");
65  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:
75  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
99  case Stmt::LabelStmtClass:
100    K = CXCursor_LabelStmt;
101    break;
102
103  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:
110  case Stmt::UnaryOperatorClass:
111  case Stmt::OffsetOfExprClass:
112  case Stmt::SizeOfAlignOfExprClass:
113  case Stmt::ArraySubscriptExprClass:
114  case Stmt::BinaryOperatorClass:
115  case Stmt::CompoundAssignOperatorClass:
116  case Stmt::ConditionalOperatorClass:
117  case Stmt::ImplicitCastExprClass:
118  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:
131  case Stmt::CXXStaticCastExprClass:
132  case Stmt::CXXDynamicCastExprClass:
133  case Stmt::CXXReinterpretCastExprClass:
134  case Stmt::CXXConstCastExprClass:
135  case Stmt::CXXFunctionalCastExprClass:
136  case Stmt::CXXTypeidExprClass:
137  case Stmt::CXXUuidofExprClass:
138  case Stmt::CXXBoolLiteralExprClass:
139  case Stmt::CXXNullPtrLiteralExprClass:
140  case Stmt::CXXThisExprClass:
141  case Stmt::CXXThrowExprClass:
142  case Stmt::CXXDefaultArgExprClass:
143  case Stmt::CXXScalarValueInitExprClass:
144  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:
155  case Stmt::CXXNoexceptExprClass:
156  case Stmt::ObjCStringLiteralClass:
157  case Stmt::ObjCEncodeExprClass:
158  case Stmt::ObjCSelectorExprClass:
159  case Stmt::ObjCProtocolExprClass:
160  case Stmt::ObjCImplicitSetterGetterRefExprClass:
161  case Stmt::ObjCIsaExprClass:
162  case Stmt::ShuffleVectorExprClass:
163  case Stmt::BlockExprClass:
164  case Stmt::OpaqueValueExprClass:
165    K = CXCursor_UnexposedExpr;
166    break;
167
168  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
198  CXCursor C = { K, { Parent, S, TU } };
199  return C;
200}
201
202CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
203                                               SourceLocation Loc,
204                                               ASTUnit *TU) {
205  assert(Super && TU && "Invalid arguments!");
206  void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
207  CXCursor C = { CXCursor_ObjCSuperClassRef, { Super, RawLoc, TU } };
208  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
219CXCursor cxcursor::MakeCursorObjCProtocolRef(ObjCProtocolDecl *Super,
220                                             SourceLocation Loc,
221                                             ASTUnit *TU) {
222  assert(Super && TU && "Invalid arguments!");
223  void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
224  CXCursor C = { CXCursor_ObjCProtocolRef, { Super, RawLoc, TU } };
225  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
236CXCursor cxcursor::MakeCursorObjCClassRef(ObjCInterfaceDecl *Class,
237                                          SourceLocation Loc,
238                                          ASTUnit *TU) {
239  // 'Class' can be null for invalid code.
240  if (!Class)
241    return MakeCXCursorInvalid(CXCursor_InvalidCode);
242  assert(TU && "Invalid arguments!");
243  void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
244  CXCursor C = { CXCursor_ObjCClassRef, { Class, RawLoc, TU } };
245  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
256CXCursor cxcursor::MakeCursorTypeRef(TypeDecl *Type, SourceLocation Loc,
257                                     ASTUnit *TU) {
258  assert(Type && TU && "Invalid arguments!");
259  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
272CXCursor 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
288CXCursor 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
306CXCursor 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
323CXCursor 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
333CXCursor 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
351CXCursor 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
361CXCursor cxcursor::MakeMacroInstantiationCursor(MacroInstantiation *MI,
362                                                ASTUnit *TU) {
363  CXCursor C = { CXCursor_MacroInstantiation, { MI, 0, TU } };
364  return C;
365}
366
367MacroInstantiation *cxcursor::getCursorMacroInstantiation(CXCursor C) {
368  assert(C.kind == CXCursor_MacroInstantiation);
369  return static_cast<MacroInstantiation *>(C.data[0]);
370}
371
372CXCursor 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
383CXCursor 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
400CXCursor 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
446Decl *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) {
455  if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
456      Cursor.kind == CXCursor_ObjCProtocolRef ||
457      Cursor.kind == CXCursor_ObjCClassRef)
458    return 0;
459
460  return (Stmt *)Cursor.data[1];
461}
462
463Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
464  return (Attr *)Cursor.data[1];
465}
466
467ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
468  return getCursorASTUnit(Cursor)->getASTContext();
469}
470
471ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
472  return static_cast<ASTUnit *>(Cursor.data[2]);
473}
474
475bool 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];
478}
479
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
487