blob: b48ad91098101655101c49025d8e4cada806c302 [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 Gregor03e80032011-06-21 17:03:29 +0000103 case Stmt::MaterializeTemporaryExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000104 K = CXCursor_UnexposedStmt;
105 break;
106
Douglas Gregor36897b02010-09-10 00:22:18 +0000107 case Stmt::LabelStmtClass:
108 K = CXCursor_LabelStmt;
109 break;
110
Douglas Gregor97b98722010-01-19 23:20:36 +0000111 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:
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000118 case Stmt::UnaryOperatorClass:
119 case Stmt::OffsetOfExprClass:
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000120 case Stmt::UnaryExprOrTypeTraitExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000121 case Stmt::ArraySubscriptExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000122 case Stmt::BinaryOperatorClass:
123 case Stmt::CompoundAssignOperatorClass:
124 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000125 case Stmt::BinaryConditionalOperatorClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000126 case Stmt::ImplicitCastExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000127 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:
Douglas Gregor97b98722010-01-19 23:20:36 +0000137 case Stmt::ChooseExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000138 case Stmt::GenericSelectionExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000139 case Stmt::GNUNullExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000140 case Stmt::CXXStaticCastExprClass:
141 case Stmt::CXXDynamicCastExprClass:
142 case Stmt::CXXReinterpretCastExprClass:
143 case Stmt::CXXConstCastExprClass:
144 case Stmt::CXXFunctionalCastExprClass:
145 case Stmt::CXXTypeidExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +0000146 case Stmt::CXXUuidofExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000147 case Stmt::CXXBoolLiteralExprClass:
148 case Stmt::CXXNullPtrLiteralExprClass:
149 case Stmt::CXXThisExprClass:
150 case Stmt::CXXThrowExprClass:
151 case Stmt::CXXDefaultArgExprClass:
Douglas Gregored8abf12010-07-08 06:14:04 +0000152 case Stmt::CXXScalarValueInitExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000153 case Stmt::CXXNewExprClass:
154 case Stmt::CXXDeleteExprClass:
155 case Stmt::CXXPseudoDestructorExprClass:
156 case Stmt::UnresolvedLookupExprClass:
157 case Stmt::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +0000158 case Stmt::BinaryTypeTraitExprClass:
John Wiegley21ff2e52011-04-28 00:16:57 +0000159 case Stmt::ArrayTypeTraitExprClass:
John Wiegley55262202011-04-25 06:54:41 +0000160 case Stmt::ExpressionTraitExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000161 case Stmt::DependentScopeDeclRefExprClass:
162 case Stmt::CXXBindTemporaryExprClass:
John McCall4765fa02010-12-06 08:20:24 +0000163 case Stmt::ExprWithCleanupsClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000164 case Stmt::CXXUnresolvedConstructExprClass:
165 case Stmt::CXXDependentScopeMemberExprClass:
166 case Stmt::UnresolvedMemberExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +0000167 case Stmt::CXXNoexceptExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000168 case Stmt::ObjCStringLiteralClass:
169 case Stmt::ObjCEncodeExprClass:
170 case Stmt::ObjCSelectorExprClass:
171 case Stmt::ObjCProtocolExprClass:
John McCallf85e1932011-06-15 23:02:42 +0000172 case Stmt::ObjCIsaExprClass:
173 case Stmt::ObjCIndirectCopyRestoreExprClass:
174 case Stmt::ObjCBridgedCastExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000175 case Stmt::ShuffleVectorExprClass:
176 case Stmt::BlockExprClass:
John McCall7cd7d1a2010-11-15 23:31:06 +0000177 case Stmt::OpaqueValueExprClass:
Douglas Gregorbe230c32011-01-03 17:17:50 +0000178 case Stmt::PackExpansionExprClass:
Douglas Gregoree8aff02011-01-04 17:33:58 +0000179 case Stmt::SizeOfPackExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +0000180 case Stmt::AsTypeExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000181 K = CXCursor_UnexposedExpr;
182 break;
Douglas Gregor36897b02010-09-10 00:22:18 +0000183
Douglas Gregor97b98722010-01-19 23:20:36 +0000184 case Stmt::DeclRefExprClass:
185 case Stmt::BlockDeclRefExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +0000186 case Stmt::SubstNonTypeTemplateParmPackExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000187 // FIXME: UnresolvedLookupExpr?
188 // FIXME: DependentScopeDeclRefExpr?
189 K = CXCursor_DeclRefExpr;
190 break;
191
192 case Stmt::MemberExprClass:
193 case Stmt::ObjCIvarRefExprClass:
194 case Stmt::ObjCPropertyRefExprClass:
195 // FIXME: UnresolvedMemberExpr?
196 // FIXME: CXXDependentScopeMemberExpr?
197 K = CXCursor_MemberRefExpr;
198 break;
199
200 case Stmt::CallExprClass:
201 case Stmt::CXXOperatorCallExprClass:
202 case Stmt::CXXMemberCallExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +0000203 case Stmt::CUDAKernelCallExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000204 case Stmt::CXXConstructExprClass:
205 case Stmt::CXXTemporaryObjectExprClass:
206 // FIXME: CXXUnresolvedConstructExpr
Douglas Gregor97b98722010-01-19 23:20:36 +0000207 K = CXCursor_CallExpr;
208 break;
209
210 case Stmt::ObjCMessageExprClass:
211 K = CXCursor_ObjCMessageExpr;
212 break;
213 }
214
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000215 CXCursor C = { K, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000216 return C;
217}
218
Douglas Gregor2e331b92010-01-16 14:00:32 +0000219CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000220 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000221 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000222 assert(Super && TU && "Invalid arguments!");
Douglas Gregor2e331b92010-01-16 14:00:32 +0000223 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000224 CXCursor C = { CXCursor_ObjCSuperClassRef, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000225 return C;
226}
227
228std::pair<ObjCInterfaceDecl *, SourceLocation>
229cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
230 assert(C.kind == CXCursor_ObjCSuperClassRef);
231 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
232 SourceLocation::getFromRawEncoding(
233 reinterpret_cast<uintptr_t>(C.data[1])));
234}
235
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000236CXCursor cxcursor::MakeCursorObjCProtocolRef(ObjCProtocolDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000237 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000238 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000239 assert(Super && TU && "Invalid arguments!");
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000240 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000241 CXCursor C = { CXCursor_ObjCProtocolRef, { Super, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000242 return C;
243}
244
245std::pair<ObjCProtocolDecl *, SourceLocation>
246cxcursor::getCursorObjCProtocolRef(CXCursor C) {
247 assert(C.kind == CXCursor_ObjCProtocolRef);
248 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
249 SourceLocation::getFromRawEncoding(
250 reinterpret_cast<uintptr_t>(C.data[1])));
251}
252
Douglas Gregor1adb0822010-01-16 17:14:40 +0000253CXCursor cxcursor::MakeCursorObjCClassRef(ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000254 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000255 CXTranslationUnit TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000256 // 'Class' can be null for invalid code.
257 if (!Class)
258 return MakeCXCursorInvalid(CXCursor_InvalidCode);
259 assert(TU && "Invalid arguments!");
Douglas Gregor1adb0822010-01-16 17:14:40 +0000260 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000261 CXCursor C = { CXCursor_ObjCClassRef, { Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000262 return C;
263}
264
265std::pair<ObjCInterfaceDecl *, SourceLocation>
266cxcursor::getCursorObjCClassRef(CXCursor C) {
267 assert(C.kind == CXCursor_ObjCClassRef);
268 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
269 SourceLocation::getFromRawEncoding(
270 reinterpret_cast<uintptr_t>(C.data[1])));
271}
272
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000273CXCursor cxcursor::MakeCursorTypeRef(TypeDecl *Type, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000274 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000275 assert(Type && TU && "Invalid arguments!");
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000276 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
277 CXCursor C = { CXCursor_TypeRef, { Type, RawLoc, TU } };
278 return C;
279}
280
281std::pair<TypeDecl *, SourceLocation>
282cxcursor::getCursorTypeRef(CXCursor C) {
283 assert(C.kind == CXCursor_TypeRef);
284 return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
285 SourceLocation::getFromRawEncoding(
286 reinterpret_cast<uintptr_t>(C.data[1])));
287}
288
Douglas Gregor0b36e612010-08-31 20:37:03 +0000289CXCursor cxcursor::MakeCursorTemplateRef(TemplateDecl *Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000290 SourceLocation Loc,
291 CXTranslationUnit TU) {
Douglas Gregor0b36e612010-08-31 20:37:03 +0000292 assert(Template && TU && "Invalid arguments!");
293 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
294 CXCursor C = { CXCursor_TemplateRef, { Template, RawLoc, TU } };
295 return C;
296}
297
298std::pair<TemplateDecl *, SourceLocation>
299cxcursor::getCursorTemplateRef(CXCursor C) {
300 assert(C.kind == CXCursor_TemplateRef);
301 return std::make_pair(static_cast<TemplateDecl *>(C.data[0]),
302 SourceLocation::getFromRawEncoding(
303 reinterpret_cast<uintptr_t>(C.data[1])));
304}
305
Douglas Gregor69319002010-08-31 23:48:11 +0000306CXCursor cxcursor::MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000307 CXTranslationUnit TU) {
Douglas Gregor69319002010-08-31 23:48:11 +0000308
309 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
310 "Invalid arguments!");
311 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
312 CXCursor C = { CXCursor_NamespaceRef, { NS, RawLoc, TU } };
313 return C;
314}
315
316std::pair<NamedDecl *, SourceLocation>
317cxcursor::getCursorNamespaceRef(CXCursor C) {
318 assert(C.kind == CXCursor_NamespaceRef);
319 return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
320 SourceLocation::getFromRawEncoding(
321 reinterpret_cast<uintptr_t>(C.data[1])));
322}
323
Douglas Gregora67e03f2010-09-09 21:42:20 +0000324CXCursor cxcursor::MakeCursorMemberRef(FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000325 CXTranslationUnit TU) {
Douglas Gregora67e03f2010-09-09 21:42:20 +0000326
327 assert(Field && TU && "Invalid arguments!");
328 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
329 CXCursor C = { CXCursor_MemberRef, { Field, RawLoc, TU } };
330 return C;
331}
332
333std::pair<FieldDecl *, SourceLocation>
334cxcursor::getCursorMemberRef(CXCursor C) {
335 assert(C.kind == CXCursor_MemberRef);
336 return std::make_pair(static_cast<FieldDecl *>(C.data[0]),
337 SourceLocation::getFromRawEncoding(
338 reinterpret_cast<uintptr_t>(C.data[1])));
339}
340
Ted Kremeneka60ed472010-11-16 08:15:36 +0000341CXCursor cxcursor::MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B,
342 CXTranslationUnit TU){
Ted Kremenek3064ef92010-08-27 21:34:58 +0000343 CXCursor C = { CXCursor_CXXBaseSpecifier, { B, 0, TU } };
344 return C;
345}
346
347CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
348 assert(C.kind == CXCursor_CXXBaseSpecifier);
349 return static_cast<CXXBaseSpecifier*>(C.data[0]);
350}
351
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000352CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000353 CXTranslationUnit TU) {
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000354 CXCursor C = { CXCursor_PreprocessingDirective,
355 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
356 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
357 TU }
358 };
359 return C;
360}
361
362SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
363 assert(C.kind == CXCursor_PreprocessingDirective);
364 return SourceRange(SourceLocation::getFromRawEncoding(
365 reinterpret_cast<uintptr_t> (C.data[0])),
366 SourceLocation::getFromRawEncoding(
367 reinterpret_cast<uintptr_t> (C.data[1])));
368}
369
Ted Kremeneka60ed472010-11-16 08:15:36 +0000370CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI,
371 CXTranslationUnit TU) {
Douglas Gregor572feb22010-03-18 18:04:21 +0000372 CXCursor C = { CXCursor_MacroDefinition, { MI, 0, TU } };
373 return C;
374}
375
376MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
377 assert(C.kind == CXCursor_MacroDefinition);
378 return static_cast<MacroDefinition *>(C.data[0]);
379}
380
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000381CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
382 CXTranslationUnit TU) {
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000383 CXCursor C = { CXCursor_MacroInstantiation, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000384 return C;
385}
386
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000387MacroExpansion *cxcursor::getCursorMacroExpansion(CXCursor C) {
Douglas Gregor48072312010-03-18 15:23:44 +0000388 assert(C.kind == CXCursor_MacroInstantiation);
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000389 return static_cast<MacroExpansion *>(C.data[0]);
Douglas Gregor48072312010-03-18 15:23:44 +0000390}
391
Douglas Gregorecdcb882010-10-20 22:00:55 +0000392CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000393 CXTranslationUnit TU) {
Douglas Gregorecdcb882010-10-20 22:00:55 +0000394 CXCursor C = { CXCursor_InclusionDirective, { ID, 0, TU } };
395 return C;
396}
397
398InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
399 assert(C.kind == CXCursor_InclusionDirective);
400 return static_cast<InclusionDirective *>(C.data[0]);
401}
402
Douglas Gregor36897b02010-09-10 00:22:18 +0000403CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000404 CXTranslationUnit TU) {
Douglas Gregor36897b02010-09-10 00:22:18 +0000405
406 assert(Label && TU && "Invalid arguments!");
407 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
408 CXCursor C = { CXCursor_LabelRef, { Label, RawLoc, TU } };
409 return C;
410}
411
412std::pair<LabelStmt*, SourceLocation>
413cxcursor::getCursorLabelRef(CXCursor C) {
414 assert(C.kind == CXCursor_LabelRef);
415 return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
416 SourceLocation::getFromRawEncoding(
417 reinterpret_cast<uintptr_t>(C.data[1])));
418}
419
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000420CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000421 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000422 assert(E && TU && "Invalid arguments!");
423 OverloadedDeclRefStorage Storage(E);
424 void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding());
425 CXCursor C = {
426 CXCursor_OverloadedDeclRef,
427 { Storage.getOpaqueValue(), RawLoc, TU }
428 };
429 return C;
430}
431
432CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D,
433 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000434 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000435 assert(D && TU && "Invalid arguments!");
436 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
437 OverloadedDeclRefStorage Storage(D);
438 CXCursor C = {
439 CXCursor_OverloadedDeclRef,
440 { Storage.getOpaqueValue(), RawLoc, TU }
441 };
442 return C;
443}
444
445CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
446 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000447 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000448 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
449 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
450 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
451 CXCursor C = {
452 CXCursor_OverloadedDeclRef,
453 { Storage.getOpaqueValue(), RawLoc, TU }
454 };
455 return C;
456}
457
458std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
459cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
460 assert(C.kind == CXCursor_OverloadedDeclRef);
461 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]),
462 SourceLocation::getFromRawEncoding(
463 reinterpret_cast<uintptr_t>(C.data[1])));
464}
465
Douglas Gregor283cae32010-01-15 21:56:13 +0000466Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
467 return (Decl *)Cursor.data[0];
468}
469
470Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
471 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
472}
473
474Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000475 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000476 Cursor.kind == CXCursor_ObjCProtocolRef ||
477 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000478 return 0;
479
Douglas Gregor283cae32010-01-15 21:56:13 +0000480 return (Stmt *)Cursor.data[1];
481}
482
Ted Kremenek95f33552010-08-26 01:42:22 +0000483Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
484 return (Attr *)Cursor.data[1];
485}
486
Argyrios Kyrtzidis8ccac3d2011-06-29 22:20:07 +0000487Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
488 return (Decl *)Cursor.data[0];
489}
490
Douglas Gregorf46034a2010-01-18 23:41:10 +0000491ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000492 return getCursorASTUnit(Cursor)->getASTContext();
493}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000494
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000495ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Ted Kremeneka60ed472010-11-16 08:15:36 +0000496 return static_cast<ASTUnit *>(static_cast<CXTranslationUnit>(Cursor.data[2])
497 ->TUData);
498}
499
500CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
501 return static_cast<CXTranslationUnit>(Cursor.data[2]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000502}
503
Douglas Gregor283cae32010-01-15 21:56:13 +0000504bool cxcursor::operator==(CXCursor X, CXCursor Y) {
505 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
506 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000507}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000508
509// FIXME: Remove once we can model DeclGroups and their appropriate ranges
510// properly in the ASTs.
511bool cxcursor::isFirstInDeclGroup(CXCursor C) {
512 assert(clang_isDeclaration(C.kind));
513 return ((uintptr_t) (C.data[1])) != 0;
514}
515
Ted Kremenekeca099b2010-12-08 23:43:14 +0000516//===----------------------------------------------------------------------===//
517// CXCursorSet.
518//===----------------------------------------------------------------------===//
519
520typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
521
522static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
523 return (CXCursorSet) setImpl;
524}
525static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
526 return (CXCursorSet_Impl*) set;
527}
528namespace llvm {
Ted Kremenekda6fb692010-12-09 00:33:41 +0000529template<> struct DenseMapInfo<CXCursor> {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000530public:
531 static inline CXCursor getEmptyKey() {
532 return MakeCXCursorInvalid(CXCursor_InvalidFile);
533 }
534 static inline CXCursor getTombstoneKey() {
535 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
536 }
537 static inline unsigned getHashValue(const CXCursor &cursor) {
538 return llvm::DenseMapInfo<std::pair<void*,void*> >
539 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
540 }
541 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
542 return x.kind == y.kind &&
543 x.data[0] == y.data[0] &&
544 x.data[1] == y.data[1];
545 }
546};
547}
548
549extern "C" {
550CXCursorSet clang_createCXCursorSet() {
551 return packCXCursorSet(new CXCursorSet_Impl());
552}
553
554void clang_disposeCXCursorSet(CXCursorSet set) {
555 delete unpackCXCursorSet(set);
556}
557
558unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
559 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
560 if (!setImpl)
561 return 0;
562 return setImpl->find(cursor) == setImpl->end();
563}
564
Anders Carlssone8b3de02010-12-09 01:00:12 +0000565unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000566 // Do not insert invalid cursors into the set.
567 if (cursor.kind >= CXCursor_FirstInvalid &&
568 cursor.kind <= CXCursor_LastInvalid)
569 return 1;
570
571 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
572 if (!setImpl)
573 return 1;
574 unsigned &entry = (*setImpl)[cursor];
575 unsigned flag = entry == 0 ? 1 : 0;
576 entry = 1;
577 return flag;
578}
579} // end: extern "C"