blob: b8c44aa8116fefe4074e094228775c77b3285fcf [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
Ted Kremenek0a90d322010-11-17 23:24:11 +000016#include "CXTranslationUnit.h"
Ted Kremenek16c440a2010-01-15 20:35:54 +000017#include "CXCursor.h"
Ted Kremeneked122732010-11-16 01:56:27 +000018#include "CXString.h"
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +000019#include "clang/Frontend/ASTUnit.h"
Ted Kremenek16c440a2010-01-15 20:35:54 +000020#include "clang/AST/Decl.h"
Douglas Gregor69319002010-08-31 23:48:11 +000021#include "clang/AST/DeclCXX.h"
Douglas Gregor283cae32010-01-15 21:56:13 +000022#include "clang/AST/DeclObjC.h"
23#include "clang/AST/Expr.h"
Douglas Gregor1f60d9e2010-09-13 22:52:57 +000024#include "clang/AST/ExprCXX.h"
Ted Kremenek007a7c92010-11-01 23:26:51 +000025#include "clang-c/Index.h"
Ted Kremenekedc8aa62010-01-16 00:36:30 +000026#include "llvm/Support/ErrorHandling.h"
Ted Kremenek16c440a2010-01-15 20:35:54 +000027
28using namespace clang;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +000029using namespace cxcursor;
Ted Kremenek16c440a2010-01-15 20:35:54 +000030
Douglas Gregor5bfb8c12010-01-20 23:34:41 +000031CXCursor cxcursor::MakeCXCursorInvalid(CXCursorKind K) {
32 assert(K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid);
33 CXCursor C = { K, { 0, 0, 0 } };
34 return C;
Ted Kremenek16c440a2010-01-15 20:35:54 +000035}
36
Ted Kremeneke77f4432010-02-18 03:09:07 +000037static CXCursorKind GetCursorKind(const Attr *A) {
38 assert(A && "Invalid arguments!");
39 switch (A->getKind()) {
40 default: break;
Sean Hunt387475d2010-06-16 23:43:53 +000041 case attr::IBAction: return CXCursor_IBActionAttr;
42 case attr::IBOutlet: return CXCursor_IBOutletAttr;
43 case attr::IBOutletCollection: return CXCursor_IBOutletCollectionAttr;
Ted Kremeneke77f4432010-02-18 03:09:07 +000044 }
45
46 return CXCursor_UnexposedAttr;
47}
48
Ted Kremeneka60ed472010-11-16 08:15:36 +000049CXCursor cxcursor::MakeCXCursor(const Attr *A, Decl *Parent,
50 CXTranslationUnit TU) {
Ted Kremeneke77f4432010-02-18 03:09:07 +000051 assert(A && Parent && TU && "Invalid arguments!");
52 CXCursor C = { GetCursorKind(A), { Parent, (void*)A, TU } };
53 return C;
54}
55
Ted Kremeneka60ed472010-11-16 08:15:36 +000056CXCursor cxcursor::MakeCXCursor(Decl *D, CXTranslationUnit TU,
Ted Kremenek007a7c92010-11-01 23:26:51 +000057 bool FirstInDeclGroup) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +000058 assert(D && TU && "Invalid arguments!");
Ted Kremenek007a7c92010-11-01 23:26:51 +000059 CXCursor C = { getCursorKindForDecl(D),
Jeffrey Yasskindec09842011-01-18 02:00:16 +000060 { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU }
Ted Kremenek007a7c92010-11-01 23:26:51 +000061 };
Douglas Gregor5bfb8c12010-01-20 23:34:41 +000062 return C;
Ted Kremenekedc8aa62010-01-16 00:36:30 +000063}
64
Ted Kremeneka60ed472010-11-16 08:15:36 +000065CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent,
66 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +000067 assert(S && TU && "Invalid arguments!");
Douglas Gregor97b98722010-01-19 23:20:36 +000068 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:
Douglas Gregor97b98722010-01-19 23:20:36 +000078 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:
Douglas Gregor97b98722010-01-19 23:20:36 +000089 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::ObjCForCollectionStmtClass:
96 case Stmt::CXXCatchStmtClass:
97 case Stmt::CXXTryStmtClass:
Richard Smithad762fc2011-04-14 22:09:26 +000098 case Stmt::CXXForRangeStmtClass:
Douglas Gregor97b98722010-01-19 23:20:36 +000099 K = CXCursor_UnexposedStmt;
100 break;
101
Douglas Gregor36897b02010-09-10 00:22:18 +0000102 case Stmt::LabelStmtClass:
103 K = CXCursor_LabelStmt;
104 break;
105
Douglas Gregor97b98722010-01-19 23:20:36 +0000106 case Stmt::PredefinedExprClass:
107 case Stmt::IntegerLiteralClass:
108 case Stmt::FloatingLiteralClass:
109 case Stmt::ImaginaryLiteralClass:
110 case Stmt::StringLiteralClass:
111 case Stmt::CharacterLiteralClass:
112 case Stmt::ParenExprClass:
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000113 case Stmt::UnaryOperatorClass:
114 case Stmt::OffsetOfExprClass:
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000115 case Stmt::UnaryExprOrTypeTraitExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000116 case Stmt::ArraySubscriptExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000117 case Stmt::BinaryOperatorClass:
118 case Stmt::CompoundAssignOperatorClass:
119 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000120 case Stmt::BinaryConditionalOperatorClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000121 case Stmt::ImplicitCastExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000122 case Stmt::CStyleCastExprClass:
123 case Stmt::CompoundLiteralExprClass:
124 case Stmt::ExtVectorElementExprClass:
125 case Stmt::InitListExprClass:
126 case Stmt::DesignatedInitExprClass:
127 case Stmt::ImplicitValueInitExprClass:
128 case Stmt::ParenListExprClass:
129 case Stmt::VAArgExprClass:
130 case Stmt::AddrLabelExprClass:
131 case Stmt::StmtExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000132 case Stmt::ChooseExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000133 case Stmt::GenericSelectionExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000134 case Stmt::GNUNullExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000135 case Stmt::CXXStaticCastExprClass:
136 case Stmt::CXXDynamicCastExprClass:
137 case Stmt::CXXReinterpretCastExprClass:
138 case Stmt::CXXConstCastExprClass:
139 case Stmt::CXXFunctionalCastExprClass:
140 case Stmt::CXXTypeidExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +0000141 case Stmt::CXXUuidofExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000142 case Stmt::CXXBoolLiteralExprClass:
143 case Stmt::CXXNullPtrLiteralExprClass:
144 case Stmt::CXXThisExprClass:
145 case Stmt::CXXThrowExprClass:
146 case Stmt::CXXDefaultArgExprClass:
Douglas Gregored8abf12010-07-08 06:14:04 +0000147 case Stmt::CXXScalarValueInitExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000148 case Stmt::CXXNewExprClass:
149 case Stmt::CXXDeleteExprClass:
150 case Stmt::CXXPseudoDestructorExprClass:
151 case Stmt::UnresolvedLookupExprClass:
152 case Stmt::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +0000153 case Stmt::BinaryTypeTraitExprClass:
John Wiegley21ff2e52011-04-28 00:16:57 +0000154 case Stmt::ArrayTypeTraitExprClass:
John Wiegley55262202011-04-25 06:54:41 +0000155 case Stmt::ExpressionTraitExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000156 case Stmt::DependentScopeDeclRefExprClass:
157 case Stmt::CXXBindTemporaryExprClass:
John McCall4765fa02010-12-06 08:20:24 +0000158 case Stmt::ExprWithCleanupsClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000159 case Stmt::CXXUnresolvedConstructExprClass:
160 case Stmt::CXXDependentScopeMemberExprClass:
161 case Stmt::UnresolvedMemberExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +0000162 case Stmt::CXXNoexceptExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000163 case Stmt::ObjCStringLiteralClass:
164 case Stmt::ObjCEncodeExprClass:
165 case Stmt::ObjCSelectorExprClass:
166 case Stmt::ObjCProtocolExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000167 case Stmt::ObjCIsaExprClass:
168 case Stmt::ShuffleVectorExprClass:
169 case Stmt::BlockExprClass:
John McCall7cd7d1a2010-11-15 23:31:06 +0000170 case Stmt::OpaqueValueExprClass:
Douglas Gregorbe230c32011-01-03 17:17:50 +0000171 case Stmt::PackExpansionExprClass:
Douglas Gregoree8aff02011-01-04 17:33:58 +0000172 case Stmt::SizeOfPackExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000173 K = CXCursor_UnexposedExpr;
174 break;
Douglas Gregor36897b02010-09-10 00:22:18 +0000175
Douglas Gregor97b98722010-01-19 23:20:36 +0000176 case Stmt::DeclRefExprClass:
177 case Stmt::BlockDeclRefExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +0000178 case Stmt::SubstNonTypeTemplateParmPackExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000179 // FIXME: UnresolvedLookupExpr?
180 // FIXME: DependentScopeDeclRefExpr?
181 K = CXCursor_DeclRefExpr;
182 break;
183
184 case Stmt::MemberExprClass:
185 case Stmt::ObjCIvarRefExprClass:
186 case Stmt::ObjCPropertyRefExprClass:
187 // FIXME: UnresolvedMemberExpr?
188 // FIXME: CXXDependentScopeMemberExpr?
189 K = CXCursor_MemberRefExpr;
190 break;
191
192 case Stmt::CallExprClass:
193 case Stmt::CXXOperatorCallExprClass:
194 case Stmt::CXXMemberCallExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +0000195 case Stmt::CUDAKernelCallExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000196 case Stmt::CXXConstructExprClass:
197 case Stmt::CXXTemporaryObjectExprClass:
198 // FIXME: CXXUnresolvedConstructExpr
Douglas Gregor97b98722010-01-19 23:20:36 +0000199 K = CXCursor_CallExpr;
200 break;
201
202 case Stmt::ObjCMessageExprClass:
203 K = CXCursor_ObjCMessageExpr;
204 break;
205 }
206
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000207 CXCursor C = { K, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000208 return C;
209}
210
Douglas Gregor2e331b92010-01-16 14:00:32 +0000211CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000212 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000213 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000214 assert(Super && TU && "Invalid arguments!");
Douglas Gregor2e331b92010-01-16 14:00:32 +0000215 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000216 CXCursor C = { CXCursor_ObjCSuperClassRef, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000217 return C;
218}
219
220std::pair<ObjCInterfaceDecl *, SourceLocation>
221cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
222 assert(C.kind == CXCursor_ObjCSuperClassRef);
223 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
224 SourceLocation::getFromRawEncoding(
225 reinterpret_cast<uintptr_t>(C.data[1])));
226}
227
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000228CXCursor cxcursor::MakeCursorObjCProtocolRef(ObjCProtocolDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000229 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000230 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000231 assert(Super && TU && "Invalid arguments!");
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000232 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000233 CXCursor C = { CXCursor_ObjCProtocolRef, { Super, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000234 return C;
235}
236
237std::pair<ObjCProtocolDecl *, SourceLocation>
238cxcursor::getCursorObjCProtocolRef(CXCursor C) {
239 assert(C.kind == CXCursor_ObjCProtocolRef);
240 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
241 SourceLocation::getFromRawEncoding(
242 reinterpret_cast<uintptr_t>(C.data[1])));
243}
244
Douglas Gregor1adb0822010-01-16 17:14:40 +0000245CXCursor cxcursor::MakeCursorObjCClassRef(ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000246 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000247 CXTranslationUnit TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000248 // 'Class' can be null for invalid code.
249 if (!Class)
250 return MakeCXCursorInvalid(CXCursor_InvalidCode);
251 assert(TU && "Invalid arguments!");
Douglas Gregor1adb0822010-01-16 17:14:40 +0000252 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000253 CXCursor C = { CXCursor_ObjCClassRef, { Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000254 return C;
255}
256
257std::pair<ObjCInterfaceDecl *, SourceLocation>
258cxcursor::getCursorObjCClassRef(CXCursor C) {
259 assert(C.kind == CXCursor_ObjCClassRef);
260 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
261 SourceLocation::getFromRawEncoding(
262 reinterpret_cast<uintptr_t>(C.data[1])));
263}
264
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000265CXCursor cxcursor::MakeCursorTypeRef(TypeDecl *Type, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000266 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000267 assert(Type && TU && "Invalid arguments!");
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000268 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
269 CXCursor C = { CXCursor_TypeRef, { Type, RawLoc, TU } };
270 return C;
271}
272
273std::pair<TypeDecl *, SourceLocation>
274cxcursor::getCursorTypeRef(CXCursor C) {
275 assert(C.kind == CXCursor_TypeRef);
276 return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
277 SourceLocation::getFromRawEncoding(
278 reinterpret_cast<uintptr_t>(C.data[1])));
279}
280
Douglas Gregor0b36e612010-08-31 20:37:03 +0000281CXCursor cxcursor::MakeCursorTemplateRef(TemplateDecl *Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000282 SourceLocation Loc,
283 CXTranslationUnit TU) {
Douglas Gregor0b36e612010-08-31 20:37:03 +0000284 assert(Template && TU && "Invalid arguments!");
285 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
286 CXCursor C = { CXCursor_TemplateRef, { Template, RawLoc, TU } };
287 return C;
288}
289
290std::pair<TemplateDecl *, SourceLocation>
291cxcursor::getCursorTemplateRef(CXCursor C) {
292 assert(C.kind == CXCursor_TemplateRef);
293 return std::make_pair(static_cast<TemplateDecl *>(C.data[0]),
294 SourceLocation::getFromRawEncoding(
295 reinterpret_cast<uintptr_t>(C.data[1])));
296}
297
Douglas Gregor69319002010-08-31 23:48:11 +0000298CXCursor cxcursor::MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000299 CXTranslationUnit TU) {
Douglas Gregor69319002010-08-31 23:48:11 +0000300
301 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
302 "Invalid arguments!");
303 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
304 CXCursor C = { CXCursor_NamespaceRef, { NS, RawLoc, TU } };
305 return C;
306}
307
308std::pair<NamedDecl *, SourceLocation>
309cxcursor::getCursorNamespaceRef(CXCursor C) {
310 assert(C.kind == CXCursor_NamespaceRef);
311 return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
312 SourceLocation::getFromRawEncoding(
313 reinterpret_cast<uintptr_t>(C.data[1])));
314}
315
Douglas Gregora67e03f2010-09-09 21:42:20 +0000316CXCursor cxcursor::MakeCursorMemberRef(FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000317 CXTranslationUnit TU) {
Douglas Gregora67e03f2010-09-09 21:42:20 +0000318
319 assert(Field && TU && "Invalid arguments!");
320 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
321 CXCursor C = { CXCursor_MemberRef, { Field, RawLoc, TU } };
322 return C;
323}
324
325std::pair<FieldDecl *, SourceLocation>
326cxcursor::getCursorMemberRef(CXCursor C) {
327 assert(C.kind == CXCursor_MemberRef);
328 return std::make_pair(static_cast<FieldDecl *>(C.data[0]),
329 SourceLocation::getFromRawEncoding(
330 reinterpret_cast<uintptr_t>(C.data[1])));
331}
332
Ted Kremeneka60ed472010-11-16 08:15:36 +0000333CXCursor cxcursor::MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B,
334 CXTranslationUnit TU){
Ted Kremenek3064ef92010-08-27 21:34:58 +0000335 CXCursor C = { CXCursor_CXXBaseSpecifier, { B, 0, TU } };
336 return C;
337}
338
339CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
340 assert(C.kind == CXCursor_CXXBaseSpecifier);
341 return static_cast<CXXBaseSpecifier*>(C.data[0]);
342}
343
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000344CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000345 CXTranslationUnit TU) {
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000346 CXCursor C = { CXCursor_PreprocessingDirective,
347 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
348 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
349 TU }
350 };
351 return C;
352}
353
354SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
355 assert(C.kind == CXCursor_PreprocessingDirective);
356 return SourceRange(SourceLocation::getFromRawEncoding(
357 reinterpret_cast<uintptr_t> (C.data[0])),
358 SourceLocation::getFromRawEncoding(
359 reinterpret_cast<uintptr_t> (C.data[1])));
360}
361
Ted Kremeneka60ed472010-11-16 08:15:36 +0000362CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI,
363 CXTranslationUnit TU) {
Douglas Gregor572feb22010-03-18 18:04:21 +0000364 CXCursor C = { CXCursor_MacroDefinition, { MI, 0, TU } };
365 return C;
366}
367
368MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
369 assert(C.kind == CXCursor_MacroDefinition);
370 return static_cast<MacroDefinition *>(C.data[0]);
371}
372
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000373CXCursor cxcursor::MakeMacroInstantiationCursor(MacroInstantiation *MI,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000374 CXTranslationUnit TU) {
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000375 CXCursor C = { CXCursor_MacroInstantiation, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000376 return C;
377}
378
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000379MacroInstantiation *cxcursor::getCursorMacroInstantiation(CXCursor C) {
Douglas Gregor48072312010-03-18 15:23:44 +0000380 assert(C.kind == CXCursor_MacroInstantiation);
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000381 return static_cast<MacroInstantiation *>(C.data[0]);
Douglas Gregor48072312010-03-18 15:23:44 +0000382}
383
Douglas Gregorecdcb882010-10-20 22:00:55 +0000384CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000385 CXTranslationUnit TU) {
Douglas Gregorecdcb882010-10-20 22:00:55 +0000386 CXCursor C = { CXCursor_InclusionDirective, { ID, 0, TU } };
387 return C;
388}
389
390InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
391 assert(C.kind == CXCursor_InclusionDirective);
392 return static_cast<InclusionDirective *>(C.data[0]);
393}
394
Douglas Gregor36897b02010-09-10 00:22:18 +0000395CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000396 CXTranslationUnit TU) {
Douglas Gregor36897b02010-09-10 00:22:18 +0000397
398 assert(Label && TU && "Invalid arguments!");
399 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
400 CXCursor C = { CXCursor_LabelRef, { Label, RawLoc, TU } };
401 return C;
402}
403
404std::pair<LabelStmt*, SourceLocation>
405cxcursor::getCursorLabelRef(CXCursor C) {
406 assert(C.kind == CXCursor_LabelRef);
407 return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
408 SourceLocation::getFromRawEncoding(
409 reinterpret_cast<uintptr_t>(C.data[1])));
410}
411
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000412CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000413 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000414 assert(E && TU && "Invalid arguments!");
415 OverloadedDeclRefStorage Storage(E);
416 void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding());
417 CXCursor C = {
418 CXCursor_OverloadedDeclRef,
419 { Storage.getOpaqueValue(), RawLoc, TU }
420 };
421 return C;
422}
423
424CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D,
425 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000426 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000427 assert(D && TU && "Invalid arguments!");
428 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
429 OverloadedDeclRefStorage Storage(D);
430 CXCursor C = {
431 CXCursor_OverloadedDeclRef,
432 { Storage.getOpaqueValue(), RawLoc, TU }
433 };
434 return C;
435}
436
437CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
438 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000439 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000440 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
441 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
442 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
443 CXCursor C = {
444 CXCursor_OverloadedDeclRef,
445 { Storage.getOpaqueValue(), RawLoc, TU }
446 };
447 return C;
448}
449
450std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
451cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
452 assert(C.kind == CXCursor_OverloadedDeclRef);
453 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]),
454 SourceLocation::getFromRawEncoding(
455 reinterpret_cast<uintptr_t>(C.data[1])));
456}
457
Douglas Gregor283cae32010-01-15 21:56:13 +0000458Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
459 return (Decl *)Cursor.data[0];
460}
461
462Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
463 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
464}
465
466Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000467 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000468 Cursor.kind == CXCursor_ObjCProtocolRef ||
469 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000470 return 0;
471
Douglas Gregor283cae32010-01-15 21:56:13 +0000472 return (Stmt *)Cursor.data[1];
473}
474
Ted Kremenek95f33552010-08-26 01:42:22 +0000475Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
476 return (Attr *)Cursor.data[1];
477}
478
Douglas Gregorf46034a2010-01-18 23:41:10 +0000479ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000480 return getCursorASTUnit(Cursor)->getASTContext();
481}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000482
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000483ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Ted Kremeneka60ed472010-11-16 08:15:36 +0000484 return static_cast<ASTUnit *>(static_cast<CXTranslationUnit>(Cursor.data[2])
485 ->TUData);
486}
487
488CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
489 return static_cast<CXTranslationUnit>(Cursor.data[2]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000490}
491
Douglas Gregor283cae32010-01-15 21:56:13 +0000492bool cxcursor::operator==(CXCursor X, CXCursor Y) {
493 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
494 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000495}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000496
497// FIXME: Remove once we can model DeclGroups and their appropriate ranges
498// properly in the ASTs.
499bool cxcursor::isFirstInDeclGroup(CXCursor C) {
500 assert(clang_isDeclaration(C.kind));
501 return ((uintptr_t) (C.data[1])) != 0;
502}
503
Ted Kremenekeca099b2010-12-08 23:43:14 +0000504//===----------------------------------------------------------------------===//
505// CXCursorSet.
506//===----------------------------------------------------------------------===//
507
508typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
509
510static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
511 return (CXCursorSet) setImpl;
512}
513static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
514 return (CXCursorSet_Impl*) set;
515}
516namespace llvm {
Ted Kremenekda6fb692010-12-09 00:33:41 +0000517template<> struct DenseMapInfo<CXCursor> {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000518public:
519 static inline CXCursor getEmptyKey() {
520 return MakeCXCursorInvalid(CXCursor_InvalidFile);
521 }
522 static inline CXCursor getTombstoneKey() {
523 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
524 }
525 static inline unsigned getHashValue(const CXCursor &cursor) {
526 return llvm::DenseMapInfo<std::pair<void*,void*> >
527 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
528 }
529 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
530 return x.kind == y.kind &&
531 x.data[0] == y.data[0] &&
532 x.data[1] == y.data[1];
533 }
534};
535}
536
537extern "C" {
538CXCursorSet clang_createCXCursorSet() {
539 return packCXCursorSet(new CXCursorSet_Impl());
540}
541
542void clang_disposeCXCursorSet(CXCursorSet set) {
543 delete unpackCXCursorSet(set);
544}
545
546unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
547 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
548 if (!setImpl)
549 return 0;
550 return setImpl->find(cursor) == setImpl->end();
551}
552
Anders Carlssone8b3de02010-12-09 01:00:12 +0000553unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000554 // Do not insert invalid cursors into the set.
555 if (cursor.kind >= CXCursor_FirstInvalid &&
556 cursor.kind <= CXCursor_LastInvalid)
557 return 1;
558
559 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
560 if (!setImpl)
561 return 1;
562 unsigned &entry = (*setImpl)[cursor];
563 unsigned flag = entry == 0 ? 1 : 0;
564 entry = 1;
565 return flag;
566}
567} // end: extern "C"