blob: b34370d2e6527b9bc8b5768447ea025f2ac5b7f3 [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:
John Wiegley28bbe4b2011-04-28 01:08:34 +000099 case Stmt::SEHTryStmtClass:
100 case Stmt::SEHExceptStmtClass:
101 case Stmt::SEHFinallyStmtClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000102 K = CXCursor_UnexposedStmt;
103 break;
104
Douglas Gregor36897b02010-09-10 00:22:18 +0000105 case Stmt::LabelStmtClass:
106 K = CXCursor_LabelStmt;
107 break;
108
Douglas Gregor97b98722010-01-19 23:20:36 +0000109 case Stmt::PredefinedExprClass:
110 case Stmt::IntegerLiteralClass:
111 case Stmt::FloatingLiteralClass:
112 case Stmt::ImaginaryLiteralClass:
113 case Stmt::StringLiteralClass:
114 case Stmt::CharacterLiteralClass:
115 case Stmt::ParenExprClass:
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000116 case Stmt::UnaryOperatorClass:
117 case Stmt::OffsetOfExprClass:
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000118 case Stmt::UnaryExprOrTypeTraitExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000119 case Stmt::ArraySubscriptExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000120 case Stmt::BinaryOperatorClass:
121 case Stmt::CompoundAssignOperatorClass:
122 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000123 case Stmt::BinaryConditionalOperatorClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000124 case Stmt::ImplicitCastExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000125 case Stmt::CStyleCastExprClass:
126 case Stmt::CompoundLiteralExprClass:
127 case Stmt::ExtVectorElementExprClass:
128 case Stmt::InitListExprClass:
129 case Stmt::DesignatedInitExprClass:
130 case Stmt::ImplicitValueInitExprClass:
131 case Stmt::ParenListExprClass:
132 case Stmt::VAArgExprClass:
133 case Stmt::AddrLabelExprClass:
134 case Stmt::StmtExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000135 case Stmt::ChooseExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000136 case Stmt::GenericSelectionExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000137 case Stmt::GNUNullExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000138 case Stmt::CXXStaticCastExprClass:
139 case Stmt::CXXDynamicCastExprClass:
140 case Stmt::CXXReinterpretCastExprClass:
141 case Stmt::CXXConstCastExprClass:
142 case Stmt::CXXFunctionalCastExprClass:
143 case Stmt::CXXTypeidExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +0000144 case Stmt::CXXUuidofExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000145 case Stmt::CXXBoolLiteralExprClass:
146 case Stmt::CXXNullPtrLiteralExprClass:
147 case Stmt::CXXThisExprClass:
148 case Stmt::CXXThrowExprClass:
149 case Stmt::CXXDefaultArgExprClass:
Douglas Gregored8abf12010-07-08 06:14:04 +0000150 case Stmt::CXXScalarValueInitExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000151 case Stmt::CXXNewExprClass:
152 case Stmt::CXXDeleteExprClass:
153 case Stmt::CXXPseudoDestructorExprClass:
154 case Stmt::UnresolvedLookupExprClass:
155 case Stmt::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +0000156 case Stmt::BinaryTypeTraitExprClass:
John Wiegley21ff2e52011-04-28 00:16:57 +0000157 case Stmt::ArrayTypeTraitExprClass:
John Wiegley55262202011-04-25 06:54:41 +0000158 case Stmt::ExpressionTraitExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000159 case Stmt::DependentScopeDeclRefExprClass:
160 case Stmt::CXXBindTemporaryExprClass:
John McCall4765fa02010-12-06 08:20:24 +0000161 case Stmt::ExprWithCleanupsClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000162 case Stmt::CXXUnresolvedConstructExprClass:
163 case Stmt::CXXDependentScopeMemberExprClass:
164 case Stmt::UnresolvedMemberExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +0000165 case Stmt::CXXNoexceptExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000166 case Stmt::ObjCStringLiteralClass:
167 case Stmt::ObjCEncodeExprClass:
168 case Stmt::ObjCSelectorExprClass:
169 case Stmt::ObjCProtocolExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000170 case Stmt::ObjCIsaExprClass:
171 case Stmt::ShuffleVectorExprClass:
172 case Stmt::BlockExprClass:
John McCall7cd7d1a2010-11-15 23:31:06 +0000173 case Stmt::OpaqueValueExprClass:
Douglas Gregorbe230c32011-01-03 17:17:50 +0000174 case Stmt::PackExpansionExprClass:
Douglas Gregoree8aff02011-01-04 17:33:58 +0000175 case Stmt::SizeOfPackExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +0000176 case Stmt::AsTypeExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000177 K = CXCursor_UnexposedExpr;
178 break;
Douglas Gregor36897b02010-09-10 00:22:18 +0000179
Douglas Gregor97b98722010-01-19 23:20:36 +0000180 case Stmt::DeclRefExprClass:
181 case Stmt::BlockDeclRefExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +0000182 case Stmt::SubstNonTypeTemplateParmPackExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000183 // FIXME: UnresolvedLookupExpr?
184 // FIXME: DependentScopeDeclRefExpr?
185 K = CXCursor_DeclRefExpr;
186 break;
187
188 case Stmt::MemberExprClass:
189 case Stmt::ObjCIvarRefExprClass:
190 case Stmt::ObjCPropertyRefExprClass:
191 // FIXME: UnresolvedMemberExpr?
192 // FIXME: CXXDependentScopeMemberExpr?
193 K = CXCursor_MemberRefExpr;
194 break;
195
196 case Stmt::CallExprClass:
197 case Stmt::CXXOperatorCallExprClass:
198 case Stmt::CXXMemberCallExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +0000199 case Stmt::CUDAKernelCallExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000200 case Stmt::CXXConstructExprClass:
201 case Stmt::CXXTemporaryObjectExprClass:
202 // FIXME: CXXUnresolvedConstructExpr
Douglas Gregor97b98722010-01-19 23:20:36 +0000203 K = CXCursor_CallExpr;
204 break;
205
206 case Stmt::ObjCMessageExprClass:
207 K = CXCursor_ObjCMessageExpr;
208 break;
209 }
210
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000211 CXCursor C = { K, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000212 return C;
213}
214
Douglas Gregor2e331b92010-01-16 14:00:32 +0000215CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000216 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000217 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000218 assert(Super && TU && "Invalid arguments!");
Douglas Gregor2e331b92010-01-16 14:00:32 +0000219 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000220 CXCursor C = { CXCursor_ObjCSuperClassRef, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000221 return C;
222}
223
224std::pair<ObjCInterfaceDecl *, SourceLocation>
225cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
226 assert(C.kind == CXCursor_ObjCSuperClassRef);
227 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
228 SourceLocation::getFromRawEncoding(
229 reinterpret_cast<uintptr_t>(C.data[1])));
230}
231
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000232CXCursor cxcursor::MakeCursorObjCProtocolRef(ObjCProtocolDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000233 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000234 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000235 assert(Super && TU && "Invalid arguments!");
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000236 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000237 CXCursor C = { CXCursor_ObjCProtocolRef, { Super, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000238 return C;
239}
240
241std::pair<ObjCProtocolDecl *, SourceLocation>
242cxcursor::getCursorObjCProtocolRef(CXCursor C) {
243 assert(C.kind == CXCursor_ObjCProtocolRef);
244 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
245 SourceLocation::getFromRawEncoding(
246 reinterpret_cast<uintptr_t>(C.data[1])));
247}
248
Douglas Gregor1adb0822010-01-16 17:14:40 +0000249CXCursor cxcursor::MakeCursorObjCClassRef(ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000250 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000251 CXTranslationUnit TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000252 // 'Class' can be null for invalid code.
253 if (!Class)
254 return MakeCXCursorInvalid(CXCursor_InvalidCode);
255 assert(TU && "Invalid arguments!");
Douglas Gregor1adb0822010-01-16 17:14:40 +0000256 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000257 CXCursor C = { CXCursor_ObjCClassRef, { Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000258 return C;
259}
260
261std::pair<ObjCInterfaceDecl *, SourceLocation>
262cxcursor::getCursorObjCClassRef(CXCursor C) {
263 assert(C.kind == CXCursor_ObjCClassRef);
264 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
265 SourceLocation::getFromRawEncoding(
266 reinterpret_cast<uintptr_t>(C.data[1])));
267}
268
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000269CXCursor cxcursor::MakeCursorTypeRef(TypeDecl *Type, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000270 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000271 assert(Type && TU && "Invalid arguments!");
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000272 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
273 CXCursor C = { CXCursor_TypeRef, { Type, RawLoc, TU } };
274 return C;
275}
276
277std::pair<TypeDecl *, SourceLocation>
278cxcursor::getCursorTypeRef(CXCursor C) {
279 assert(C.kind == CXCursor_TypeRef);
280 return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
281 SourceLocation::getFromRawEncoding(
282 reinterpret_cast<uintptr_t>(C.data[1])));
283}
284
Douglas Gregor0b36e612010-08-31 20:37:03 +0000285CXCursor cxcursor::MakeCursorTemplateRef(TemplateDecl *Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000286 SourceLocation Loc,
287 CXTranslationUnit TU) {
Douglas Gregor0b36e612010-08-31 20:37:03 +0000288 assert(Template && TU && "Invalid arguments!");
289 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
290 CXCursor C = { CXCursor_TemplateRef, { Template, RawLoc, TU } };
291 return C;
292}
293
294std::pair<TemplateDecl *, SourceLocation>
295cxcursor::getCursorTemplateRef(CXCursor C) {
296 assert(C.kind == CXCursor_TemplateRef);
297 return std::make_pair(static_cast<TemplateDecl *>(C.data[0]),
298 SourceLocation::getFromRawEncoding(
299 reinterpret_cast<uintptr_t>(C.data[1])));
300}
301
Douglas Gregor69319002010-08-31 23:48:11 +0000302CXCursor cxcursor::MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000303 CXTranslationUnit TU) {
Douglas Gregor69319002010-08-31 23:48:11 +0000304
305 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
306 "Invalid arguments!");
307 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
308 CXCursor C = { CXCursor_NamespaceRef, { NS, RawLoc, TU } };
309 return C;
310}
311
312std::pair<NamedDecl *, SourceLocation>
313cxcursor::getCursorNamespaceRef(CXCursor C) {
314 assert(C.kind == CXCursor_NamespaceRef);
315 return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
316 SourceLocation::getFromRawEncoding(
317 reinterpret_cast<uintptr_t>(C.data[1])));
318}
319
Douglas Gregora67e03f2010-09-09 21:42:20 +0000320CXCursor cxcursor::MakeCursorMemberRef(FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000321 CXTranslationUnit TU) {
Douglas Gregora67e03f2010-09-09 21:42:20 +0000322
323 assert(Field && TU && "Invalid arguments!");
324 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
325 CXCursor C = { CXCursor_MemberRef, { Field, RawLoc, TU } };
326 return C;
327}
328
329std::pair<FieldDecl *, SourceLocation>
330cxcursor::getCursorMemberRef(CXCursor C) {
331 assert(C.kind == CXCursor_MemberRef);
332 return std::make_pair(static_cast<FieldDecl *>(C.data[0]),
333 SourceLocation::getFromRawEncoding(
334 reinterpret_cast<uintptr_t>(C.data[1])));
335}
336
Ted Kremeneka60ed472010-11-16 08:15:36 +0000337CXCursor cxcursor::MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B,
338 CXTranslationUnit TU){
Ted Kremenek3064ef92010-08-27 21:34:58 +0000339 CXCursor C = { CXCursor_CXXBaseSpecifier, { B, 0, TU } };
340 return C;
341}
342
343CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
344 assert(C.kind == CXCursor_CXXBaseSpecifier);
345 return static_cast<CXXBaseSpecifier*>(C.data[0]);
346}
347
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000348CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000349 CXTranslationUnit TU) {
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000350 CXCursor C = { CXCursor_PreprocessingDirective,
351 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
352 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
353 TU }
354 };
355 return C;
356}
357
358SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
359 assert(C.kind == CXCursor_PreprocessingDirective);
360 return SourceRange(SourceLocation::getFromRawEncoding(
361 reinterpret_cast<uintptr_t> (C.data[0])),
362 SourceLocation::getFromRawEncoding(
363 reinterpret_cast<uintptr_t> (C.data[1])));
364}
365
Ted Kremeneka60ed472010-11-16 08:15:36 +0000366CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI,
367 CXTranslationUnit TU) {
Douglas Gregor572feb22010-03-18 18:04:21 +0000368 CXCursor C = { CXCursor_MacroDefinition, { MI, 0, TU } };
369 return C;
370}
371
372MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
373 assert(C.kind == CXCursor_MacroDefinition);
374 return static_cast<MacroDefinition *>(C.data[0]);
375}
376
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000377CXCursor cxcursor::MakeMacroInstantiationCursor(MacroInstantiation *MI,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000378 CXTranslationUnit TU) {
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000379 CXCursor C = { CXCursor_MacroInstantiation, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000380 return C;
381}
382
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000383MacroInstantiation *cxcursor::getCursorMacroInstantiation(CXCursor C) {
Douglas Gregor48072312010-03-18 15:23:44 +0000384 assert(C.kind == CXCursor_MacroInstantiation);
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000385 return static_cast<MacroInstantiation *>(C.data[0]);
Douglas Gregor48072312010-03-18 15:23:44 +0000386}
387
Douglas Gregorecdcb882010-10-20 22:00:55 +0000388CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000389 CXTranslationUnit TU) {
Douglas Gregorecdcb882010-10-20 22:00:55 +0000390 CXCursor C = { CXCursor_InclusionDirective, { ID, 0, TU } };
391 return C;
392}
393
394InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
395 assert(C.kind == CXCursor_InclusionDirective);
396 return static_cast<InclusionDirective *>(C.data[0]);
397}
398
Douglas Gregor36897b02010-09-10 00:22:18 +0000399CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000400 CXTranslationUnit TU) {
Douglas Gregor36897b02010-09-10 00:22:18 +0000401
402 assert(Label && TU && "Invalid arguments!");
403 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
404 CXCursor C = { CXCursor_LabelRef, { Label, RawLoc, TU } };
405 return C;
406}
407
408std::pair<LabelStmt*, SourceLocation>
409cxcursor::getCursorLabelRef(CXCursor C) {
410 assert(C.kind == CXCursor_LabelRef);
411 return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
412 SourceLocation::getFromRawEncoding(
413 reinterpret_cast<uintptr_t>(C.data[1])));
414}
415
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000416CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000417 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000418 assert(E && TU && "Invalid arguments!");
419 OverloadedDeclRefStorage Storage(E);
420 void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding());
421 CXCursor C = {
422 CXCursor_OverloadedDeclRef,
423 { Storage.getOpaqueValue(), RawLoc, TU }
424 };
425 return C;
426}
427
428CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D,
429 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000430 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000431 assert(D && TU && "Invalid arguments!");
432 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
433 OverloadedDeclRefStorage Storage(D);
434 CXCursor C = {
435 CXCursor_OverloadedDeclRef,
436 { Storage.getOpaqueValue(), RawLoc, TU }
437 };
438 return C;
439}
440
441CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
442 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000443 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000444 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
445 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
446 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
447 CXCursor C = {
448 CXCursor_OverloadedDeclRef,
449 { Storage.getOpaqueValue(), RawLoc, TU }
450 };
451 return C;
452}
453
454std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
455cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
456 assert(C.kind == CXCursor_OverloadedDeclRef);
457 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]),
458 SourceLocation::getFromRawEncoding(
459 reinterpret_cast<uintptr_t>(C.data[1])));
460}
461
Douglas Gregor283cae32010-01-15 21:56:13 +0000462Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
463 return (Decl *)Cursor.data[0];
464}
465
466Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
467 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
468}
469
470Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000471 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000472 Cursor.kind == CXCursor_ObjCProtocolRef ||
473 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000474 return 0;
475
Douglas Gregor283cae32010-01-15 21:56:13 +0000476 return (Stmt *)Cursor.data[1];
477}
478
Ted Kremenek95f33552010-08-26 01:42:22 +0000479Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
480 return (Attr *)Cursor.data[1];
481}
482
Douglas Gregorf46034a2010-01-18 23:41:10 +0000483ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000484 return getCursorASTUnit(Cursor)->getASTContext();
485}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000486
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000487ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Ted Kremeneka60ed472010-11-16 08:15:36 +0000488 return static_cast<ASTUnit *>(static_cast<CXTranslationUnit>(Cursor.data[2])
489 ->TUData);
490}
491
492CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
493 return static_cast<CXTranslationUnit>(Cursor.data[2]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000494}
495
Douglas Gregor283cae32010-01-15 21:56:13 +0000496bool cxcursor::operator==(CXCursor X, CXCursor Y) {
497 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
498 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000499}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000500
501// FIXME: Remove once we can model DeclGroups and their appropriate ranges
502// properly in the ASTs.
503bool cxcursor::isFirstInDeclGroup(CXCursor C) {
504 assert(clang_isDeclaration(C.kind));
505 return ((uintptr_t) (C.data[1])) != 0;
506}
507
Ted Kremenekeca099b2010-12-08 23:43:14 +0000508//===----------------------------------------------------------------------===//
509// CXCursorSet.
510//===----------------------------------------------------------------------===//
511
512typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
513
514static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
515 return (CXCursorSet) setImpl;
516}
517static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
518 return (CXCursorSet_Impl*) set;
519}
520namespace llvm {
Ted Kremenekda6fb692010-12-09 00:33:41 +0000521template<> struct DenseMapInfo<CXCursor> {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000522public:
523 static inline CXCursor getEmptyKey() {
524 return MakeCXCursorInvalid(CXCursor_InvalidFile);
525 }
526 static inline CXCursor getTombstoneKey() {
527 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
528 }
529 static inline unsigned getHashValue(const CXCursor &cursor) {
530 return llvm::DenseMapInfo<std::pair<void*,void*> >
531 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
532 }
533 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
534 return x.kind == y.kind &&
535 x.data[0] == y.data[0] &&
536 x.data[1] == y.data[1];
537 }
538};
539}
540
541extern "C" {
542CXCursorSet clang_createCXCursorSet() {
543 return packCXCursorSet(new CXCursorSet_Impl());
544}
545
546void clang_disposeCXCursorSet(CXCursorSet set) {
547 delete unpackCXCursorSet(set);
548}
549
550unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
551 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
552 if (!setImpl)
553 return 0;
554 return setImpl->find(cursor) == setImpl->end();
555}
556
Anders Carlssone8b3de02010-12-09 01:00:12 +0000557unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000558 // Do not insert invalid cursors into the set.
559 if (cursor.kind >= CXCursor_FirstInvalid &&
560 cursor.kind <= CXCursor_LastInvalid)
561 return 1;
562
563 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
564 if (!setImpl)
565 return 1;
566 unsigned &entry = (*setImpl)[cursor];
567 unsigned flag = entry == 0 ? 1 : 0;
568 entry = 1;
569 return flag;
570}
571} // end: extern "C"