blob: 35134513419270ca4558329122063ccdcaf85241 [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:
John McCallf85e1932011-06-15 23:02:42 +000095 case Stmt::ObjCAutoreleasePoolStmtClass:
Douglas Gregor97b98722010-01-19 23:20:36 +000096 case Stmt::ObjCForCollectionStmtClass:
97 case Stmt::CXXCatchStmtClass:
98 case Stmt::CXXTryStmtClass:
Richard Smithad762fc2011-04-14 22:09:26 +000099 case Stmt::CXXForRangeStmtClass:
John Wiegley28bbe4b2011-04-28 01:08:34 +0000100 case Stmt::SEHTryStmtClass:
101 case Stmt::SEHExceptStmtClass:
102 case Stmt::SEHFinallyStmtClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000103 K = CXCursor_UnexposedStmt;
104 break;
105
Douglas Gregor36897b02010-09-10 00:22:18 +0000106 case Stmt::LabelStmtClass:
107 K = CXCursor_LabelStmt;
108 break;
109
Douglas Gregor97b98722010-01-19 23:20:36 +0000110 case Stmt::PredefinedExprClass:
111 case Stmt::IntegerLiteralClass:
112 case Stmt::FloatingLiteralClass:
113 case Stmt::ImaginaryLiteralClass:
114 case Stmt::StringLiteralClass:
115 case Stmt::CharacterLiteralClass:
116 case Stmt::ParenExprClass:
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000117 case Stmt::UnaryOperatorClass:
118 case Stmt::OffsetOfExprClass:
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000119 case Stmt::UnaryExprOrTypeTraitExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000120 case Stmt::ArraySubscriptExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000121 case Stmt::BinaryOperatorClass:
122 case Stmt::CompoundAssignOperatorClass:
123 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000124 case Stmt::BinaryConditionalOperatorClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000125 case Stmt::ImplicitCastExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000126 case Stmt::CStyleCastExprClass:
127 case Stmt::CompoundLiteralExprClass:
128 case Stmt::ExtVectorElementExprClass:
129 case Stmt::InitListExprClass:
130 case Stmt::DesignatedInitExprClass:
131 case Stmt::ImplicitValueInitExprClass:
132 case Stmt::ParenListExprClass:
133 case Stmt::VAArgExprClass:
134 case Stmt::AddrLabelExprClass:
135 case Stmt::StmtExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000136 case Stmt::ChooseExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000137 case Stmt::GenericSelectionExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000138 case Stmt::GNUNullExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000139 case Stmt::CXXStaticCastExprClass:
140 case Stmt::CXXDynamicCastExprClass:
141 case Stmt::CXXReinterpretCastExprClass:
142 case Stmt::CXXConstCastExprClass:
143 case Stmt::CXXFunctionalCastExprClass:
144 case Stmt::CXXTypeidExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +0000145 case Stmt::CXXUuidofExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000146 case Stmt::CXXBoolLiteralExprClass:
147 case Stmt::CXXNullPtrLiteralExprClass:
148 case Stmt::CXXThisExprClass:
149 case Stmt::CXXThrowExprClass:
150 case Stmt::CXXDefaultArgExprClass:
Douglas Gregored8abf12010-07-08 06:14:04 +0000151 case Stmt::CXXScalarValueInitExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000152 case Stmt::CXXNewExprClass:
153 case Stmt::CXXDeleteExprClass:
154 case Stmt::CXXPseudoDestructorExprClass:
155 case Stmt::UnresolvedLookupExprClass:
156 case Stmt::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +0000157 case Stmt::BinaryTypeTraitExprClass:
John Wiegley21ff2e52011-04-28 00:16:57 +0000158 case Stmt::ArrayTypeTraitExprClass:
John Wiegley55262202011-04-25 06:54:41 +0000159 case Stmt::ExpressionTraitExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000160 case Stmt::DependentScopeDeclRefExprClass:
161 case Stmt::CXXBindTemporaryExprClass:
John McCall4765fa02010-12-06 08:20:24 +0000162 case Stmt::ExprWithCleanupsClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000163 case Stmt::CXXUnresolvedConstructExprClass:
164 case Stmt::CXXDependentScopeMemberExprClass:
165 case Stmt::UnresolvedMemberExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +0000166 case Stmt::CXXNoexceptExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000167 case Stmt::ObjCStringLiteralClass:
168 case Stmt::ObjCEncodeExprClass:
169 case Stmt::ObjCSelectorExprClass:
170 case Stmt::ObjCProtocolExprClass:
John McCallf85e1932011-06-15 23:02:42 +0000171 case Stmt::ObjCIsaExprClass:
172 case Stmt::ObjCIndirectCopyRestoreExprClass:
173 case Stmt::ObjCBridgedCastExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000174 case Stmt::ShuffleVectorExprClass:
175 case Stmt::BlockExprClass:
John McCall7cd7d1a2010-11-15 23:31:06 +0000176 case Stmt::OpaqueValueExprClass:
Douglas Gregorbe230c32011-01-03 17:17:50 +0000177 case Stmt::PackExpansionExprClass:
Douglas Gregoree8aff02011-01-04 17:33:58 +0000178 case Stmt::SizeOfPackExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +0000179 case Stmt::AsTypeExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000180 K = CXCursor_UnexposedExpr;
181 break;
Douglas Gregor36897b02010-09-10 00:22:18 +0000182
Douglas Gregor97b98722010-01-19 23:20:36 +0000183 case Stmt::DeclRefExprClass:
184 case Stmt::BlockDeclRefExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +0000185 case Stmt::SubstNonTypeTemplateParmPackExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000186 // FIXME: UnresolvedLookupExpr?
187 // FIXME: DependentScopeDeclRefExpr?
188 K = CXCursor_DeclRefExpr;
189 break;
190
191 case Stmt::MemberExprClass:
192 case Stmt::ObjCIvarRefExprClass:
193 case Stmt::ObjCPropertyRefExprClass:
194 // FIXME: UnresolvedMemberExpr?
195 // FIXME: CXXDependentScopeMemberExpr?
196 K = CXCursor_MemberRefExpr;
197 break;
198
199 case Stmt::CallExprClass:
200 case Stmt::CXXOperatorCallExprClass:
201 case Stmt::CXXMemberCallExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +0000202 case Stmt::CUDAKernelCallExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000203 case Stmt::CXXConstructExprClass:
204 case Stmt::CXXTemporaryObjectExprClass:
205 // FIXME: CXXUnresolvedConstructExpr
Douglas Gregor97b98722010-01-19 23:20:36 +0000206 K = CXCursor_CallExpr;
207 break;
208
209 case Stmt::ObjCMessageExprClass:
210 K = CXCursor_ObjCMessageExpr;
211 break;
212 }
213
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000214 CXCursor C = { K, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000215 return C;
216}
217
Douglas Gregor2e331b92010-01-16 14:00:32 +0000218CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000219 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000220 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000221 assert(Super && TU && "Invalid arguments!");
Douglas Gregor2e331b92010-01-16 14:00:32 +0000222 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000223 CXCursor C = { CXCursor_ObjCSuperClassRef, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000224 return C;
225}
226
227std::pair<ObjCInterfaceDecl *, SourceLocation>
228cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
229 assert(C.kind == CXCursor_ObjCSuperClassRef);
230 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
231 SourceLocation::getFromRawEncoding(
232 reinterpret_cast<uintptr_t>(C.data[1])));
233}
234
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000235CXCursor cxcursor::MakeCursorObjCProtocolRef(ObjCProtocolDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000236 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000237 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000238 assert(Super && TU && "Invalid arguments!");
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000239 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000240 CXCursor C = { CXCursor_ObjCProtocolRef, { Super, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000241 return C;
242}
243
244std::pair<ObjCProtocolDecl *, SourceLocation>
245cxcursor::getCursorObjCProtocolRef(CXCursor C) {
246 assert(C.kind == CXCursor_ObjCProtocolRef);
247 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
248 SourceLocation::getFromRawEncoding(
249 reinterpret_cast<uintptr_t>(C.data[1])));
250}
251
Douglas Gregor1adb0822010-01-16 17:14:40 +0000252CXCursor cxcursor::MakeCursorObjCClassRef(ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000253 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000254 CXTranslationUnit TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000255 // 'Class' can be null for invalid code.
256 if (!Class)
257 return MakeCXCursorInvalid(CXCursor_InvalidCode);
258 assert(TU && "Invalid arguments!");
Douglas Gregor1adb0822010-01-16 17:14:40 +0000259 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000260 CXCursor C = { CXCursor_ObjCClassRef, { Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000261 return C;
262}
263
264std::pair<ObjCInterfaceDecl *, SourceLocation>
265cxcursor::getCursorObjCClassRef(CXCursor C) {
266 assert(C.kind == CXCursor_ObjCClassRef);
267 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
268 SourceLocation::getFromRawEncoding(
269 reinterpret_cast<uintptr_t>(C.data[1])));
270}
271
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000272CXCursor cxcursor::MakeCursorTypeRef(TypeDecl *Type, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000273 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000274 assert(Type && TU && "Invalid arguments!");
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000275 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
276 CXCursor C = { CXCursor_TypeRef, { Type, RawLoc, TU } };
277 return C;
278}
279
280std::pair<TypeDecl *, SourceLocation>
281cxcursor::getCursorTypeRef(CXCursor C) {
282 assert(C.kind == CXCursor_TypeRef);
283 return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
284 SourceLocation::getFromRawEncoding(
285 reinterpret_cast<uintptr_t>(C.data[1])));
286}
287
Douglas Gregor0b36e612010-08-31 20:37:03 +0000288CXCursor cxcursor::MakeCursorTemplateRef(TemplateDecl *Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000289 SourceLocation Loc,
290 CXTranslationUnit TU) {
Douglas Gregor0b36e612010-08-31 20:37:03 +0000291 assert(Template && TU && "Invalid arguments!");
292 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
293 CXCursor C = { CXCursor_TemplateRef, { Template, RawLoc, TU } };
294 return C;
295}
296
297std::pair<TemplateDecl *, SourceLocation>
298cxcursor::getCursorTemplateRef(CXCursor C) {
299 assert(C.kind == CXCursor_TemplateRef);
300 return std::make_pair(static_cast<TemplateDecl *>(C.data[0]),
301 SourceLocation::getFromRawEncoding(
302 reinterpret_cast<uintptr_t>(C.data[1])));
303}
304
Douglas Gregor69319002010-08-31 23:48:11 +0000305CXCursor cxcursor::MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000306 CXTranslationUnit TU) {
Douglas Gregor69319002010-08-31 23:48:11 +0000307
308 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
309 "Invalid arguments!");
310 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
311 CXCursor C = { CXCursor_NamespaceRef, { NS, RawLoc, TU } };
312 return C;
313}
314
315std::pair<NamedDecl *, SourceLocation>
316cxcursor::getCursorNamespaceRef(CXCursor C) {
317 assert(C.kind == CXCursor_NamespaceRef);
318 return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
319 SourceLocation::getFromRawEncoding(
320 reinterpret_cast<uintptr_t>(C.data[1])));
321}
322
Douglas Gregora67e03f2010-09-09 21:42:20 +0000323CXCursor cxcursor::MakeCursorMemberRef(FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000324 CXTranslationUnit TU) {
Douglas Gregora67e03f2010-09-09 21:42:20 +0000325
326 assert(Field && TU && "Invalid arguments!");
327 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
328 CXCursor C = { CXCursor_MemberRef, { Field, RawLoc, TU } };
329 return C;
330}
331
332std::pair<FieldDecl *, SourceLocation>
333cxcursor::getCursorMemberRef(CXCursor C) {
334 assert(C.kind == CXCursor_MemberRef);
335 return std::make_pair(static_cast<FieldDecl *>(C.data[0]),
336 SourceLocation::getFromRawEncoding(
337 reinterpret_cast<uintptr_t>(C.data[1])));
338}
339
Ted Kremeneka60ed472010-11-16 08:15:36 +0000340CXCursor cxcursor::MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B,
341 CXTranslationUnit TU){
Ted Kremenek3064ef92010-08-27 21:34:58 +0000342 CXCursor C = { CXCursor_CXXBaseSpecifier, { B, 0, TU } };
343 return C;
344}
345
346CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
347 assert(C.kind == CXCursor_CXXBaseSpecifier);
348 return static_cast<CXXBaseSpecifier*>(C.data[0]);
349}
350
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000351CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000352 CXTranslationUnit TU) {
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000353 CXCursor C = { CXCursor_PreprocessingDirective,
354 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
355 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
356 TU }
357 };
358 return C;
359}
360
361SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
362 assert(C.kind == CXCursor_PreprocessingDirective);
363 return SourceRange(SourceLocation::getFromRawEncoding(
364 reinterpret_cast<uintptr_t> (C.data[0])),
365 SourceLocation::getFromRawEncoding(
366 reinterpret_cast<uintptr_t> (C.data[1])));
367}
368
Ted Kremeneka60ed472010-11-16 08:15:36 +0000369CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI,
370 CXTranslationUnit TU) {
Douglas Gregor572feb22010-03-18 18:04:21 +0000371 CXCursor C = { CXCursor_MacroDefinition, { MI, 0, TU } };
372 return C;
373}
374
375MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
376 assert(C.kind == CXCursor_MacroDefinition);
377 return static_cast<MacroDefinition *>(C.data[0]);
378}
379
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000380CXCursor cxcursor::MakeMacroInstantiationCursor(MacroInstantiation *MI,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000381 CXTranslationUnit TU) {
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000382 CXCursor C = { CXCursor_MacroInstantiation, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000383 return C;
384}
385
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000386MacroInstantiation *cxcursor::getCursorMacroInstantiation(CXCursor C) {
Douglas Gregor48072312010-03-18 15:23:44 +0000387 assert(C.kind == CXCursor_MacroInstantiation);
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000388 return static_cast<MacroInstantiation *>(C.data[0]);
Douglas Gregor48072312010-03-18 15:23:44 +0000389}
390
Douglas Gregorecdcb882010-10-20 22:00:55 +0000391CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000392 CXTranslationUnit TU) {
Douglas Gregorecdcb882010-10-20 22:00:55 +0000393 CXCursor C = { CXCursor_InclusionDirective, { ID, 0, TU } };
394 return C;
395}
396
397InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
398 assert(C.kind == CXCursor_InclusionDirective);
399 return static_cast<InclusionDirective *>(C.data[0]);
400}
401
Douglas Gregor36897b02010-09-10 00:22:18 +0000402CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000403 CXTranslationUnit TU) {
Douglas Gregor36897b02010-09-10 00:22:18 +0000404
405 assert(Label && TU && "Invalid arguments!");
406 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
407 CXCursor C = { CXCursor_LabelRef, { Label, RawLoc, TU } };
408 return C;
409}
410
411std::pair<LabelStmt*, SourceLocation>
412cxcursor::getCursorLabelRef(CXCursor C) {
413 assert(C.kind == CXCursor_LabelRef);
414 return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
415 SourceLocation::getFromRawEncoding(
416 reinterpret_cast<uintptr_t>(C.data[1])));
417}
418
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000419CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000420 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000421 assert(E && TU && "Invalid arguments!");
422 OverloadedDeclRefStorage Storage(E);
423 void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding());
424 CXCursor C = {
425 CXCursor_OverloadedDeclRef,
426 { Storage.getOpaqueValue(), RawLoc, TU }
427 };
428 return C;
429}
430
431CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D,
432 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000433 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000434 assert(D && TU && "Invalid arguments!");
435 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
436 OverloadedDeclRefStorage Storage(D);
437 CXCursor C = {
438 CXCursor_OverloadedDeclRef,
439 { Storage.getOpaqueValue(), RawLoc, TU }
440 };
441 return C;
442}
443
444CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
445 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000446 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000447 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
448 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
449 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
450 CXCursor C = {
451 CXCursor_OverloadedDeclRef,
452 { Storage.getOpaqueValue(), RawLoc, TU }
453 };
454 return C;
455}
456
457std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
458cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
459 assert(C.kind == CXCursor_OverloadedDeclRef);
460 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]),
461 SourceLocation::getFromRawEncoding(
462 reinterpret_cast<uintptr_t>(C.data[1])));
463}
464
Douglas Gregor283cae32010-01-15 21:56:13 +0000465Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
466 return (Decl *)Cursor.data[0];
467}
468
469Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
470 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
471}
472
473Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000474 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000475 Cursor.kind == CXCursor_ObjCProtocolRef ||
476 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000477 return 0;
478
Douglas Gregor283cae32010-01-15 21:56:13 +0000479 return (Stmt *)Cursor.data[1];
480}
481
Ted Kremenek95f33552010-08-26 01:42:22 +0000482Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
483 return (Attr *)Cursor.data[1];
484}
485
Douglas Gregorf46034a2010-01-18 23:41:10 +0000486ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000487 return getCursorASTUnit(Cursor)->getASTContext();
488}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000489
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000490ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Ted Kremeneka60ed472010-11-16 08:15:36 +0000491 return static_cast<ASTUnit *>(static_cast<CXTranslationUnit>(Cursor.data[2])
492 ->TUData);
493}
494
495CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
496 return static_cast<CXTranslationUnit>(Cursor.data[2]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000497}
498
Douglas Gregor283cae32010-01-15 21:56:13 +0000499bool cxcursor::operator==(CXCursor X, CXCursor Y) {
500 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
501 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000502}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000503
504// FIXME: Remove once we can model DeclGroups and their appropriate ranges
505// properly in the ASTs.
506bool cxcursor::isFirstInDeclGroup(CXCursor C) {
507 assert(clang_isDeclaration(C.kind));
508 return ((uintptr_t) (C.data[1])) != 0;
509}
510
Ted Kremenekeca099b2010-12-08 23:43:14 +0000511//===----------------------------------------------------------------------===//
512// CXCursorSet.
513//===----------------------------------------------------------------------===//
514
515typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
516
517static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
518 return (CXCursorSet) setImpl;
519}
520static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
521 return (CXCursorSet_Impl*) set;
522}
523namespace llvm {
Ted Kremenekda6fb692010-12-09 00:33:41 +0000524template<> struct DenseMapInfo<CXCursor> {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000525public:
526 static inline CXCursor getEmptyKey() {
527 return MakeCXCursorInvalid(CXCursor_InvalidFile);
528 }
529 static inline CXCursor getTombstoneKey() {
530 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
531 }
532 static inline unsigned getHashValue(const CXCursor &cursor) {
533 return llvm::DenseMapInfo<std::pair<void*,void*> >
534 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
535 }
536 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
537 return x.kind == y.kind &&
538 x.data[0] == y.data[0] &&
539 x.data[1] == y.data[1];
540 }
541};
542}
543
544extern "C" {
545CXCursorSet clang_createCXCursorSet() {
546 return packCXCursorSet(new CXCursorSet_Impl());
547}
548
549void clang_disposeCXCursorSet(CXCursorSet set) {
550 delete unpackCXCursorSet(set);
551}
552
553unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
554 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
555 if (!setImpl)
556 return 0;
557 return setImpl->find(cursor) == setImpl->end();
558}
559
Anders Carlssone8b3de02010-12-09 01:00:12 +0000560unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000561 // Do not insert invalid cursors into the set.
562 if (cursor.kind >= CXCursor_FirstInvalid &&
563 cursor.kind <= CXCursor_LastInvalid)
564 return 1;
565
566 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
567 if (!setImpl)
568 return 1;
569 unsigned &entry = (*setImpl)[cursor];
570 unsigned flag = entry == 0 ? 1 : 0;
571 entry = 1;
572 return flag;
573}
574} // end: extern "C"