• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- 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 //
10 // 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.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "CXTranslationUnit.h"
17 #include "CXCursor.h"
18 #include "CXString.h"
19 #include "clang/Frontend/ASTUnit.h"
20 #include "clang/AST/Decl.h"
21 #include "clang/AST/DeclCXX.h"
22 #include "clang/AST/DeclObjC.h"
23 #include "clang/AST/Expr.h"
24 #include "clang/AST/ExprCXX.h"
25 #include "clang-c/Index.h"
26 #include "llvm/Support/ErrorHandling.h"
27 
28 using namespace clang;
29 using namespace cxcursor;
30 
MakeCXCursorInvalid(CXCursorKind K)31 CXCursor cxcursor::MakeCXCursorInvalid(CXCursorKind K) {
32   assert(K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid);
33   CXCursor C = { K, { 0, 0, 0 } };
34   return C;
35 }
36 
GetCursorKind(const Attr * A)37 static CXCursorKind GetCursorKind(const Attr *A) {
38   assert(A && "Invalid arguments!");
39   switch (A->getKind()) {
40     default: break;
41     case attr::IBAction: return CXCursor_IBActionAttr;
42     case attr::IBOutlet: return CXCursor_IBOutletAttr;
43     case attr::IBOutletCollection: return CXCursor_IBOutletCollectionAttr;
44   }
45 
46   return CXCursor_UnexposedAttr;
47 }
48 
MakeCXCursor(const Attr * A,Decl * Parent,CXTranslationUnit TU)49 CXCursor cxcursor::MakeCXCursor(const Attr *A, Decl *Parent,
50                                 CXTranslationUnit TU) {
51   assert(A && Parent && TU && "Invalid arguments!");
52   CXCursor C = { GetCursorKind(A), { Parent, (void*)A, TU } };
53   return C;
54 }
55 
MakeCXCursor(Decl * D,CXTranslationUnit TU,bool FirstInDeclGroup)56 CXCursor cxcursor::MakeCXCursor(Decl *D, CXTranslationUnit TU,
57                                 bool FirstInDeclGroup) {
58   assert(D && TU && "Invalid arguments!");
59   CXCursor C = { getCursorKindForDecl(D),
60                  { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU }
61                };
62   return C;
63 }
64 
MakeCXCursor(Stmt * S,Decl * Parent,CXTranslationUnit TU)65 CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent,
66                                 CXTranslationUnit TU) {
67   assert(S && TU && "Invalid arguments!");
68   CXCursorKind K = CXCursor_NotImplemented;
69 
70   switch (S->getStmtClass()) {
71   case Stmt::NoStmtClass:
72     break;
73 
74   case Stmt::NullStmtClass:
75   case Stmt::CompoundStmtClass:
76   case Stmt::CaseStmtClass:
77   case Stmt::DefaultStmtClass:
78   case Stmt::IfStmtClass:
79   case Stmt::SwitchStmtClass:
80   case Stmt::WhileStmtClass:
81   case Stmt::DoStmtClass:
82   case Stmt::ForStmtClass:
83   case Stmt::GotoStmtClass:
84   case Stmt::IndirectGotoStmtClass:
85   case Stmt::ContinueStmtClass:
86   case Stmt::BreakStmtClass:
87   case Stmt::ReturnStmtClass:
88   case Stmt::DeclStmtClass:
89   case Stmt::AsmStmtClass:
90   case Stmt::ObjCAtTryStmtClass:
91   case Stmt::ObjCAtCatchStmtClass:
92   case Stmt::ObjCAtFinallyStmtClass:
93   case Stmt::ObjCAtThrowStmtClass:
94   case Stmt::ObjCAtSynchronizedStmtClass:
95   case Stmt::ObjCAutoreleasePoolStmtClass:
96   case Stmt::ObjCForCollectionStmtClass:
97   case Stmt::CXXCatchStmtClass:
98   case Stmt::CXXTryStmtClass:
99   case Stmt::CXXForRangeStmtClass:
100   case Stmt::SEHTryStmtClass:
101   case Stmt::SEHExceptStmtClass:
102   case Stmt::SEHFinallyStmtClass:
103   case Stmt::MaterializeTemporaryExprClass:
104     K = CXCursor_UnexposedStmt;
105     break;
106 
107   case Stmt::LabelStmtClass:
108     K = CXCursor_LabelStmt;
109     break;
110 
111   case Stmt::PredefinedExprClass:
112   case Stmt::IntegerLiteralClass:
113   case Stmt::FloatingLiteralClass:
114   case Stmt::ImaginaryLiteralClass:
115   case Stmt::StringLiteralClass:
116   case Stmt::CharacterLiteralClass:
117   case Stmt::ParenExprClass:
118   case Stmt::UnaryOperatorClass:
119   case Stmt::OffsetOfExprClass:
120   case Stmt::UnaryExprOrTypeTraitExprClass:
121   case Stmt::ArraySubscriptExprClass:
122   case Stmt::BinaryOperatorClass:
123   case Stmt::CompoundAssignOperatorClass:
124   case Stmt::ConditionalOperatorClass:
125   case Stmt::BinaryConditionalOperatorClass:
126   case Stmt::ImplicitCastExprClass:
127   case Stmt::CStyleCastExprClass:
128   case Stmt::CompoundLiteralExprClass:
129   case Stmt::ExtVectorElementExprClass:
130   case Stmt::InitListExprClass:
131   case Stmt::DesignatedInitExprClass:
132   case Stmt::ImplicitValueInitExprClass:
133   case Stmt::ParenListExprClass:
134   case Stmt::VAArgExprClass:
135   case Stmt::AddrLabelExprClass:
136   case Stmt::StmtExprClass:
137   case Stmt::ChooseExprClass:
138   case Stmt::GenericSelectionExprClass:
139   case Stmt::GNUNullExprClass:
140   case Stmt::CXXStaticCastExprClass:
141   case Stmt::CXXDynamicCastExprClass:
142   case Stmt::CXXReinterpretCastExprClass:
143   case Stmt::CXXConstCastExprClass:
144   case Stmt::CXXFunctionalCastExprClass:
145   case Stmt::CXXTypeidExprClass:
146   case Stmt::CXXUuidofExprClass:
147   case Stmt::CXXBoolLiteralExprClass:
148   case Stmt::CXXNullPtrLiteralExprClass:
149   case Stmt::CXXThisExprClass:
150   case Stmt::CXXThrowExprClass:
151   case Stmt::CXXDefaultArgExprClass:
152   case Stmt::CXXScalarValueInitExprClass:
153   case Stmt::CXXNewExprClass:
154   case Stmt::CXXDeleteExprClass:
155   case Stmt::CXXPseudoDestructorExprClass:
156   case Stmt::UnresolvedLookupExprClass:
157   case Stmt::UnaryTypeTraitExprClass:
158   case Stmt::BinaryTypeTraitExprClass:
159   case Stmt::ArrayTypeTraitExprClass:
160   case Stmt::ExpressionTraitExprClass:
161   case Stmt::DependentScopeDeclRefExprClass:
162   case Stmt::CXXBindTemporaryExprClass:
163   case Stmt::ExprWithCleanupsClass:
164   case Stmt::CXXUnresolvedConstructExprClass:
165   case Stmt::CXXDependentScopeMemberExprClass:
166   case Stmt::UnresolvedMemberExprClass:
167   case Stmt::CXXNoexceptExprClass:
168   case Stmt::ObjCStringLiteralClass:
169   case Stmt::ObjCEncodeExprClass:
170   case Stmt::ObjCSelectorExprClass:
171   case Stmt::ObjCProtocolExprClass:
172   case Stmt::ObjCIsaExprClass:
173   case Stmt::ObjCIndirectCopyRestoreExprClass:
174   case Stmt::ObjCBridgedCastExprClass:
175   case Stmt::ShuffleVectorExprClass:
176   case Stmt::BlockExprClass:
177   case Stmt::OpaqueValueExprClass:
178   case Stmt::PackExpansionExprClass:
179   case Stmt::SizeOfPackExprClass:
180   case Stmt::AsTypeExprClass:
181     K = CXCursor_UnexposedExpr;
182     break;
183 
184   case Stmt::DeclRefExprClass:
185   case Stmt::BlockDeclRefExprClass:
186   case Stmt::SubstNonTypeTemplateParmExprClass:
187   case Stmt::SubstNonTypeTemplateParmPackExprClass:
188     // FIXME: UnresolvedLookupExpr?
189     // FIXME: DependentScopeDeclRefExpr?
190     K = CXCursor_DeclRefExpr;
191     break;
192 
193   case Stmt::MemberExprClass:
194   case Stmt::ObjCIvarRefExprClass:
195   case Stmt::ObjCPropertyRefExprClass:
196     // FIXME: UnresolvedMemberExpr?
197     // FIXME: CXXDependentScopeMemberExpr?
198     K = CXCursor_MemberRefExpr;
199     break;
200 
201   case Stmt::CallExprClass:
202   case Stmt::CXXOperatorCallExprClass:
203   case Stmt::CXXMemberCallExprClass:
204   case Stmt::CUDAKernelCallExprClass:
205   case Stmt::CXXConstructExprClass:
206   case Stmt::CXXTemporaryObjectExprClass:
207     // FIXME: CXXUnresolvedConstructExpr
208     K = CXCursor_CallExpr;
209     break;
210 
211   case Stmt::ObjCMessageExprClass:
212     K = CXCursor_ObjCMessageExpr;
213     break;
214   }
215 
216   CXCursor C = { K, { Parent, S, TU } };
217   return C;
218 }
219 
MakeCursorObjCSuperClassRef(ObjCInterfaceDecl * Super,SourceLocation Loc,CXTranslationUnit TU)220 CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
221                                                SourceLocation Loc,
222                                                CXTranslationUnit TU) {
223   assert(Super && TU && "Invalid arguments!");
224   void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
225   CXCursor C = { CXCursor_ObjCSuperClassRef, { Super, RawLoc, TU } };
226   return C;
227 }
228 
229 std::pair<ObjCInterfaceDecl *, SourceLocation>
getCursorObjCSuperClassRef(CXCursor C)230 cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
231   assert(C.kind == CXCursor_ObjCSuperClassRef);
232   return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
233            SourceLocation::getFromRawEncoding(
234                                       reinterpret_cast<uintptr_t>(C.data[1])));
235 }
236 
MakeCursorObjCProtocolRef(ObjCProtocolDecl * Super,SourceLocation Loc,CXTranslationUnit TU)237 CXCursor cxcursor::MakeCursorObjCProtocolRef(ObjCProtocolDecl *Super,
238                                              SourceLocation Loc,
239                                              CXTranslationUnit TU) {
240   assert(Super && TU && "Invalid arguments!");
241   void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
242   CXCursor C = { CXCursor_ObjCProtocolRef, { Super, RawLoc, TU } };
243   return C;
244 }
245 
246 std::pair<ObjCProtocolDecl *, SourceLocation>
getCursorObjCProtocolRef(CXCursor C)247 cxcursor::getCursorObjCProtocolRef(CXCursor C) {
248   assert(C.kind == CXCursor_ObjCProtocolRef);
249   return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
250            SourceLocation::getFromRawEncoding(
251                                       reinterpret_cast<uintptr_t>(C.data[1])));
252 }
253 
MakeCursorObjCClassRef(ObjCInterfaceDecl * Class,SourceLocation Loc,CXTranslationUnit TU)254 CXCursor cxcursor::MakeCursorObjCClassRef(ObjCInterfaceDecl *Class,
255                                           SourceLocation Loc,
256                                           CXTranslationUnit TU) {
257   // 'Class' can be null for invalid code.
258   if (!Class)
259     return MakeCXCursorInvalid(CXCursor_InvalidCode);
260   assert(TU && "Invalid arguments!");
261   void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
262   CXCursor C = { CXCursor_ObjCClassRef, { Class, RawLoc, TU } };
263   return C;
264 }
265 
266 std::pair<ObjCInterfaceDecl *, SourceLocation>
getCursorObjCClassRef(CXCursor C)267 cxcursor::getCursorObjCClassRef(CXCursor C) {
268   assert(C.kind == CXCursor_ObjCClassRef);
269   return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
270            SourceLocation::getFromRawEncoding(
271                                       reinterpret_cast<uintptr_t>(C.data[1])));
272 }
273 
MakeCursorTypeRef(TypeDecl * Type,SourceLocation Loc,CXTranslationUnit TU)274 CXCursor cxcursor::MakeCursorTypeRef(TypeDecl *Type, SourceLocation Loc,
275                                      CXTranslationUnit TU) {
276   assert(Type && TU && "Invalid arguments!");
277   void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
278   CXCursor C = { CXCursor_TypeRef, { Type, RawLoc, TU } };
279   return C;
280 }
281 
282 std::pair<TypeDecl *, SourceLocation>
getCursorTypeRef(CXCursor C)283 cxcursor::getCursorTypeRef(CXCursor C) {
284   assert(C.kind == CXCursor_TypeRef);
285   return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
286            SourceLocation::getFromRawEncoding(
287                                       reinterpret_cast<uintptr_t>(C.data[1])));
288 }
289 
MakeCursorTemplateRef(TemplateDecl * Template,SourceLocation Loc,CXTranslationUnit TU)290 CXCursor cxcursor::MakeCursorTemplateRef(TemplateDecl *Template,
291                                          SourceLocation Loc,
292                                          CXTranslationUnit TU) {
293   assert(Template && TU && "Invalid arguments!");
294   void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
295   CXCursor C = { CXCursor_TemplateRef, { Template, RawLoc, TU } };
296   return C;
297 }
298 
299 std::pair<TemplateDecl *, SourceLocation>
getCursorTemplateRef(CXCursor C)300 cxcursor::getCursorTemplateRef(CXCursor C) {
301   assert(C.kind == CXCursor_TemplateRef);
302   return std::make_pair(static_cast<TemplateDecl *>(C.data[0]),
303                         SourceLocation::getFromRawEncoding(
304                                        reinterpret_cast<uintptr_t>(C.data[1])));
305 }
306 
MakeCursorNamespaceRef(NamedDecl * NS,SourceLocation Loc,CXTranslationUnit TU)307 CXCursor cxcursor::MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc,
308                                           CXTranslationUnit TU) {
309 
310   assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
311          "Invalid arguments!");
312   void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
313   CXCursor C = { CXCursor_NamespaceRef, { NS, RawLoc, TU } };
314   return C;
315 }
316 
317 std::pair<NamedDecl *, SourceLocation>
getCursorNamespaceRef(CXCursor C)318 cxcursor::getCursorNamespaceRef(CXCursor C) {
319   assert(C.kind == CXCursor_NamespaceRef);
320   return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
321                         SourceLocation::getFromRawEncoding(
322                                        reinterpret_cast<uintptr_t>(C.data[1])));
323 }
324 
MakeCursorMemberRef(FieldDecl * Field,SourceLocation Loc,CXTranslationUnit TU)325 CXCursor cxcursor::MakeCursorMemberRef(FieldDecl *Field, SourceLocation Loc,
326                                        CXTranslationUnit TU) {
327 
328   assert(Field && TU && "Invalid arguments!");
329   void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
330   CXCursor C = { CXCursor_MemberRef, { Field, RawLoc, TU } };
331   return C;
332 }
333 
334 std::pair<FieldDecl *, SourceLocation>
getCursorMemberRef(CXCursor C)335 cxcursor::getCursorMemberRef(CXCursor C) {
336   assert(C.kind == CXCursor_MemberRef);
337   return std::make_pair(static_cast<FieldDecl *>(C.data[0]),
338                         SourceLocation::getFromRawEncoding(
339                                        reinterpret_cast<uintptr_t>(C.data[1])));
340 }
341 
MakeCursorCXXBaseSpecifier(CXXBaseSpecifier * B,CXTranslationUnit TU)342 CXCursor cxcursor::MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B,
343                                               CXTranslationUnit TU){
344   CXCursor C = { CXCursor_CXXBaseSpecifier, { B, 0, TU } };
345   return C;
346 }
347 
getCursorCXXBaseSpecifier(CXCursor C)348 CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
349   assert(C.kind == CXCursor_CXXBaseSpecifier);
350   return static_cast<CXXBaseSpecifier*>(C.data[0]);
351 }
352 
MakePreprocessingDirectiveCursor(SourceRange Range,CXTranslationUnit TU)353 CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
354                                                     CXTranslationUnit TU) {
355   CXCursor C = { CXCursor_PreprocessingDirective,
356                  { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
357                    reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
358                    TU }
359                };
360   return C;
361 }
362 
getCursorPreprocessingDirective(CXCursor C)363 SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
364   assert(C.kind == CXCursor_PreprocessingDirective);
365   return SourceRange(SourceLocation::getFromRawEncoding(
366                                       reinterpret_cast<uintptr_t> (C.data[0])),
367                      SourceLocation::getFromRawEncoding(
368                                       reinterpret_cast<uintptr_t> (C.data[1])));
369 }
370 
MakeMacroDefinitionCursor(MacroDefinition * MI,CXTranslationUnit TU)371 CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI,
372                                              CXTranslationUnit TU) {
373   CXCursor C = { CXCursor_MacroDefinition, { MI, 0, TU } };
374   return C;
375 }
376 
getCursorMacroDefinition(CXCursor C)377 MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
378   assert(C.kind == CXCursor_MacroDefinition);
379   return static_cast<MacroDefinition *>(C.data[0]);
380 }
381 
MakeMacroExpansionCursor(MacroExpansion * MI,CXTranslationUnit TU)382 CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
383                                             CXTranslationUnit TU) {
384   CXCursor C = { CXCursor_MacroExpansion, { MI, 0, TU } };
385   return C;
386 }
387 
getCursorMacroExpansion(CXCursor C)388 MacroExpansion *cxcursor::getCursorMacroExpansion(CXCursor C) {
389   assert(C.kind == CXCursor_MacroExpansion);
390   return static_cast<MacroExpansion *>(C.data[0]);
391 }
392 
MakeInclusionDirectiveCursor(InclusionDirective * ID,CXTranslationUnit TU)393 CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
394                                                 CXTranslationUnit TU) {
395   CXCursor C = { CXCursor_InclusionDirective, { ID, 0, TU } };
396   return C;
397 }
398 
getCursorInclusionDirective(CXCursor C)399 InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
400   assert(C.kind == CXCursor_InclusionDirective);
401   return static_cast<InclusionDirective *>(C.data[0]);
402 }
403 
MakeCursorLabelRef(LabelStmt * Label,SourceLocation Loc,CXTranslationUnit TU)404 CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
405                                       CXTranslationUnit TU) {
406 
407   assert(Label && TU && "Invalid arguments!");
408   void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
409   CXCursor C = { CXCursor_LabelRef, { Label, RawLoc, TU } };
410   return C;
411 }
412 
413 std::pair<LabelStmt*, SourceLocation>
getCursorLabelRef(CXCursor C)414 cxcursor::getCursorLabelRef(CXCursor C) {
415   assert(C.kind == CXCursor_LabelRef);
416   return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
417                         SourceLocation::getFromRawEncoding(
418                                        reinterpret_cast<uintptr_t>(C.data[1])));
419 }
420 
MakeCursorOverloadedDeclRef(OverloadExpr * E,CXTranslationUnit TU)421 CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E,
422                                                CXTranslationUnit TU) {
423   assert(E && TU && "Invalid arguments!");
424   OverloadedDeclRefStorage Storage(E);
425   void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding());
426   CXCursor C = {
427                  CXCursor_OverloadedDeclRef,
428                  { Storage.getOpaqueValue(), RawLoc, TU }
429                };
430   return C;
431 }
432 
MakeCursorOverloadedDeclRef(Decl * D,SourceLocation Loc,CXTranslationUnit TU)433 CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D,
434                                                SourceLocation Loc,
435                                                CXTranslationUnit TU) {
436   assert(D && TU && "Invalid arguments!");
437   void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
438   OverloadedDeclRefStorage Storage(D);
439   CXCursor C = {
440     CXCursor_OverloadedDeclRef,
441     { Storage.getOpaqueValue(), RawLoc, TU }
442   };
443   return C;
444 }
445 
MakeCursorOverloadedDeclRef(TemplateName Name,SourceLocation Loc,CXTranslationUnit TU)446 CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
447                                                SourceLocation Loc,
448                                                CXTranslationUnit TU) {
449   assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
450   void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
451   OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
452   CXCursor C = {
453     CXCursor_OverloadedDeclRef,
454     { Storage.getOpaqueValue(), RawLoc, TU }
455   };
456   return C;
457 }
458 
459 std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
getCursorOverloadedDeclRef(CXCursor C)460 cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
461   assert(C.kind == CXCursor_OverloadedDeclRef);
462   return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]),
463                         SourceLocation::getFromRawEncoding(
464                                        reinterpret_cast<uintptr_t>(C.data[1])));
465 }
466 
getCursorDecl(CXCursor Cursor)467 Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
468   return (Decl *)Cursor.data[0];
469 }
470 
getCursorExpr(CXCursor Cursor)471 Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
472   return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
473 }
474 
getCursorStmt(CXCursor Cursor)475 Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
476   if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
477       Cursor.kind == CXCursor_ObjCProtocolRef ||
478       Cursor.kind == CXCursor_ObjCClassRef)
479     return 0;
480 
481   return (Stmt *)Cursor.data[1];
482 }
483 
getCursorAttr(CXCursor Cursor)484 Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
485   return (Attr *)Cursor.data[1];
486 }
487 
getCursorParentDecl(CXCursor Cursor)488 Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
489   return (Decl *)Cursor.data[0];
490 }
491 
getCursorContext(CXCursor Cursor)492 ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
493   return getCursorASTUnit(Cursor)->getASTContext();
494 }
495 
getCursorASTUnit(CXCursor Cursor)496 ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
497   return static_cast<ASTUnit *>(static_cast<CXTranslationUnit>(Cursor.data[2])
498                                   ->TUData);
499 }
500 
getCursorTU(CXCursor Cursor)501 CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
502   return static_cast<CXTranslationUnit>(Cursor.data[2]);
503 }
504 
operator ==(CXCursor X,CXCursor Y)505 bool cxcursor::operator==(CXCursor X, CXCursor Y) {
506   return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
507          X.data[2] == Y.data[2];
508 }
509 
510 // FIXME: Remove once we can model DeclGroups and their appropriate ranges
511 // properly in the ASTs.
isFirstInDeclGroup(CXCursor C)512 bool cxcursor::isFirstInDeclGroup(CXCursor C) {
513   assert(clang_isDeclaration(C.kind));
514   return ((uintptr_t) (C.data[1])) != 0;
515 }
516 
517 //===----------------------------------------------------------------------===//
518 // CXCursorSet.
519 //===----------------------------------------------------------------------===//
520 
521 typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
522 
packCXCursorSet(CXCursorSet_Impl * setImpl)523 static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
524   return (CXCursorSet) setImpl;
525 }
unpackCXCursorSet(CXCursorSet set)526 static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
527   return (CXCursorSet_Impl*) set;
528 }
529 namespace llvm {
530 template<> struct DenseMapInfo<CXCursor> {
531 public:
getEmptyKeyllvm::DenseMapInfo532   static inline CXCursor getEmptyKey() {
533     return MakeCXCursorInvalid(CXCursor_InvalidFile);
534   }
getTombstoneKeyllvm::DenseMapInfo535   static inline CXCursor getTombstoneKey() {
536     return MakeCXCursorInvalid(CXCursor_NoDeclFound);
537   }
getHashValuellvm::DenseMapInfo538   static inline unsigned getHashValue(const CXCursor &cursor) {
539     return llvm::DenseMapInfo<std::pair<void*,void*> >
540       ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
541   }
isEqualllvm::DenseMapInfo542   static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
543     return x.kind == y.kind &&
544            x.data[0] == y.data[0] &&
545            x.data[1] == y.data[1];
546   }
547 };
548 }
549 
550 extern "C" {
clang_createCXCursorSet()551 CXCursorSet clang_createCXCursorSet() {
552   return packCXCursorSet(new CXCursorSet_Impl());
553 }
554 
clang_disposeCXCursorSet(CXCursorSet set)555 void clang_disposeCXCursorSet(CXCursorSet set) {
556   delete unpackCXCursorSet(set);
557 }
558 
clang_CXCursorSet_contains(CXCursorSet set,CXCursor cursor)559 unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
560   CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
561   if (!setImpl)
562     return 0;
563   return setImpl->find(cursor) == setImpl->end();
564 }
565 
clang_CXCursorSet_insert(CXCursorSet set,CXCursor cursor)566 unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
567   // Do not insert invalid cursors into the set.
568   if (cursor.kind >= CXCursor_FirstInvalid &&
569       cursor.kind <= CXCursor_LastInvalid)
570     return 1;
571 
572   CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
573   if (!setImpl)
574     return 1;
575   unsigned &entry = (*setImpl)[cursor];
576   unsigned flag = entry == 0 ? 1 : 0;
577   entry = 1;
578   return flag;
579 }
580 } // end: extern "C"
581