blob: d9e8f4a826b07a93694b2d6838f0cd9a73f1d756 [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;
Argyrios Kyrtzidis6639e922011-09-13 17:39:31 +000044 case attr::Final: return CXCursor_CXXFinalAttr;
45 case attr::Override: return CXCursor_CXXOverrideAttr;
Ted Kremeneke77f4432010-02-18 03:09:07 +000046 }
47
48 return CXCursor_UnexposedAttr;
49}
50
Ted Kremeneka60ed472010-11-16 08:15:36 +000051CXCursor cxcursor::MakeCXCursor(const Attr *A, Decl *Parent,
52 CXTranslationUnit TU) {
Ted Kremeneke77f4432010-02-18 03:09:07 +000053 assert(A && Parent && TU && "Invalid arguments!");
54 CXCursor C = { GetCursorKind(A), { Parent, (void*)A, TU } };
55 return C;
56}
57
Ted Kremeneka60ed472010-11-16 08:15:36 +000058CXCursor cxcursor::MakeCXCursor(Decl *D, CXTranslationUnit TU,
Ted Kremenek007a7c92010-11-01 23:26:51 +000059 bool FirstInDeclGroup) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +000060 assert(D && TU && "Invalid arguments!");
Ted Kremenek007a7c92010-11-01 23:26:51 +000061 CXCursor C = { getCursorKindForDecl(D),
Jeffrey Yasskindec09842011-01-18 02:00:16 +000062 { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU }
Ted Kremenek007a7c92010-11-01 23:26:51 +000063 };
Douglas Gregor5bfb8c12010-01-20 23:34:41 +000064 return C;
Ted Kremenekedc8aa62010-01-16 00:36:30 +000065}
66
Ted Kremeneka60ed472010-11-16 08:15:36 +000067CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent,
68 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +000069 assert(S && TU && "Invalid arguments!");
Douglas Gregor97b98722010-01-19 23:20:36 +000070 CXCursorKind K = CXCursor_NotImplemented;
71
72 switch (S->getStmtClass()) {
73 case Stmt::NoStmtClass:
74 break;
75
76 case Stmt::NullStmtClass:
77 case Stmt::CompoundStmtClass:
78 case Stmt::CaseStmtClass:
79 case Stmt::DefaultStmtClass:
Douglas Gregor97b98722010-01-19 23:20:36 +000080 case Stmt::IfStmtClass:
81 case Stmt::SwitchStmtClass:
82 case Stmt::WhileStmtClass:
83 case Stmt::DoStmtClass:
84 case Stmt::ForStmtClass:
85 case Stmt::GotoStmtClass:
86 case Stmt::IndirectGotoStmtClass:
87 case Stmt::ContinueStmtClass:
88 case Stmt::BreakStmtClass:
89 case Stmt::ReturnStmtClass:
90 case Stmt::DeclStmtClass:
Douglas Gregor97b98722010-01-19 23:20:36 +000091 case Stmt::AsmStmtClass:
92 case Stmt::ObjCAtTryStmtClass:
93 case Stmt::ObjCAtCatchStmtClass:
94 case Stmt::ObjCAtFinallyStmtClass:
95 case Stmt::ObjCAtThrowStmtClass:
96 case Stmt::ObjCAtSynchronizedStmtClass:
John McCallf85e1932011-06-15 23:02:42 +000097 case Stmt::ObjCAutoreleasePoolStmtClass:
Douglas Gregor97b98722010-01-19 23:20:36 +000098 case Stmt::ObjCForCollectionStmtClass:
99 case Stmt::CXXCatchStmtClass:
100 case Stmt::CXXTryStmtClass:
Richard Smithad762fc2011-04-14 22:09:26 +0000101 case Stmt::CXXForRangeStmtClass:
John Wiegley28bbe4b2011-04-28 01:08:34 +0000102 case Stmt::SEHTryStmtClass:
103 case Stmt::SEHExceptStmtClass:
104 case Stmt::SEHFinallyStmtClass:
Douglas Gregor03e80032011-06-21 17:03:29 +0000105 case Stmt::MaterializeTemporaryExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000106 K = CXCursor_UnexposedStmt;
107 break;
108
Douglas Gregor36897b02010-09-10 00:22:18 +0000109 case Stmt::LabelStmtClass:
110 K = CXCursor_LabelStmt;
111 break;
112
Douglas Gregor97b98722010-01-19 23:20:36 +0000113 case Stmt::PredefinedExprClass:
114 case Stmt::IntegerLiteralClass:
115 case Stmt::FloatingLiteralClass:
116 case Stmt::ImaginaryLiteralClass:
117 case Stmt::StringLiteralClass:
118 case Stmt::CharacterLiteralClass:
119 case Stmt::ParenExprClass:
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000120 case Stmt::UnaryOperatorClass:
121 case Stmt::OffsetOfExprClass:
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000122 case Stmt::UnaryExprOrTypeTraitExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000123 case Stmt::ArraySubscriptExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000124 case Stmt::BinaryOperatorClass:
125 case Stmt::CompoundAssignOperatorClass:
126 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000127 case Stmt::BinaryConditionalOperatorClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000128 case Stmt::ImplicitCastExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000129 case Stmt::CStyleCastExprClass:
130 case Stmt::CompoundLiteralExprClass:
131 case Stmt::ExtVectorElementExprClass:
132 case Stmt::InitListExprClass:
133 case Stmt::DesignatedInitExprClass:
134 case Stmt::ImplicitValueInitExprClass:
135 case Stmt::ParenListExprClass:
136 case Stmt::VAArgExprClass:
137 case Stmt::AddrLabelExprClass:
138 case Stmt::StmtExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000139 case Stmt::ChooseExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +0000140 case Stmt::GenericSelectionExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000141 case Stmt::GNUNullExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000142 case Stmt::CXXStaticCastExprClass:
143 case Stmt::CXXDynamicCastExprClass:
144 case Stmt::CXXReinterpretCastExprClass:
145 case Stmt::CXXConstCastExprClass:
146 case Stmt::CXXFunctionalCastExprClass:
147 case Stmt::CXXTypeidExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +0000148 case Stmt::CXXUuidofExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000149 case Stmt::CXXBoolLiteralExprClass:
150 case Stmt::CXXNullPtrLiteralExprClass:
151 case Stmt::CXXThisExprClass:
152 case Stmt::CXXThrowExprClass:
153 case Stmt::CXXDefaultArgExprClass:
Douglas Gregored8abf12010-07-08 06:14:04 +0000154 case Stmt::CXXScalarValueInitExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000155 case Stmt::CXXNewExprClass:
156 case Stmt::CXXDeleteExprClass:
157 case Stmt::CXXPseudoDestructorExprClass:
158 case Stmt::UnresolvedLookupExprClass:
159 case Stmt::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +0000160 case Stmt::BinaryTypeTraitExprClass:
John Wiegley21ff2e52011-04-28 00:16:57 +0000161 case Stmt::ArrayTypeTraitExprClass:
John Wiegley55262202011-04-25 06:54:41 +0000162 case Stmt::ExpressionTraitExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000163 case Stmt::DependentScopeDeclRefExprClass:
164 case Stmt::CXXBindTemporaryExprClass:
John McCall4765fa02010-12-06 08:20:24 +0000165 case Stmt::ExprWithCleanupsClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000166 case Stmt::CXXUnresolvedConstructExprClass:
167 case Stmt::CXXDependentScopeMemberExprClass:
168 case Stmt::UnresolvedMemberExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +0000169 case Stmt::CXXNoexceptExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000170 case Stmt::ObjCStringLiteralClass:
171 case Stmt::ObjCEncodeExprClass:
172 case Stmt::ObjCSelectorExprClass:
173 case Stmt::ObjCProtocolExprClass:
John McCallf85e1932011-06-15 23:02:42 +0000174 case Stmt::ObjCIsaExprClass:
175 case Stmt::ObjCIndirectCopyRestoreExprClass:
176 case Stmt::ObjCBridgedCastExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000177 case Stmt::ShuffleVectorExprClass:
178 case Stmt::BlockExprClass:
John McCall7cd7d1a2010-11-15 23:31:06 +0000179 case Stmt::OpaqueValueExprClass:
Douglas Gregorbe230c32011-01-03 17:17:50 +0000180 case Stmt::PackExpansionExprClass:
Douglas Gregoree8aff02011-01-04 17:33:58 +0000181 case Stmt::SizeOfPackExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +0000182 case Stmt::AsTypeExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000183 K = CXCursor_UnexposedExpr;
184 break;
Douglas Gregor36897b02010-09-10 00:22:18 +0000185
Douglas Gregor97b98722010-01-19 23:20:36 +0000186 case Stmt::DeclRefExprClass:
187 case Stmt::BlockDeclRefExprClass:
John McCall91a57552011-07-15 05:09:51 +0000188 case Stmt::SubstNonTypeTemplateParmExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +0000189 case Stmt::SubstNonTypeTemplateParmPackExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000190 // FIXME: UnresolvedLookupExpr?
191 // FIXME: DependentScopeDeclRefExpr?
192 K = CXCursor_DeclRefExpr;
193 break;
194
195 case Stmt::MemberExprClass:
196 case Stmt::ObjCIvarRefExprClass:
197 case Stmt::ObjCPropertyRefExprClass:
198 // FIXME: UnresolvedMemberExpr?
199 // FIXME: CXXDependentScopeMemberExpr?
200 K = CXCursor_MemberRefExpr;
201 break;
202
203 case Stmt::CallExprClass:
204 case Stmt::CXXOperatorCallExprClass:
205 case Stmt::CXXMemberCallExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +0000206 case Stmt::CUDAKernelCallExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000207 case Stmt::CXXConstructExprClass:
208 case Stmt::CXXTemporaryObjectExprClass:
209 // FIXME: CXXUnresolvedConstructExpr
Douglas Gregor97b98722010-01-19 23:20:36 +0000210 K = CXCursor_CallExpr;
211 break;
212
213 case Stmt::ObjCMessageExprClass:
214 K = CXCursor_ObjCMessageExpr;
215 break;
216 }
217
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000218 CXCursor C = { K, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000219 return C;
220}
221
Douglas Gregor2e331b92010-01-16 14:00:32 +0000222CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000223 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000224 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000225 assert(Super && TU && "Invalid arguments!");
Douglas Gregor2e331b92010-01-16 14:00:32 +0000226 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000227 CXCursor C = { CXCursor_ObjCSuperClassRef, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000228 return C;
229}
230
231std::pair<ObjCInterfaceDecl *, SourceLocation>
232cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
233 assert(C.kind == CXCursor_ObjCSuperClassRef);
234 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
235 SourceLocation::getFromRawEncoding(
236 reinterpret_cast<uintptr_t>(C.data[1])));
237}
238
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000239CXCursor cxcursor::MakeCursorObjCProtocolRef(ObjCProtocolDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000240 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000241 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000242 assert(Super && TU && "Invalid arguments!");
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000243 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000244 CXCursor C = { CXCursor_ObjCProtocolRef, { Super, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000245 return C;
246}
247
248std::pair<ObjCProtocolDecl *, SourceLocation>
249cxcursor::getCursorObjCProtocolRef(CXCursor C) {
250 assert(C.kind == CXCursor_ObjCProtocolRef);
251 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
252 SourceLocation::getFromRawEncoding(
253 reinterpret_cast<uintptr_t>(C.data[1])));
254}
255
Douglas Gregor1adb0822010-01-16 17:14:40 +0000256CXCursor cxcursor::MakeCursorObjCClassRef(ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000257 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000258 CXTranslationUnit TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000259 // 'Class' can be null for invalid code.
260 if (!Class)
261 return MakeCXCursorInvalid(CXCursor_InvalidCode);
262 assert(TU && "Invalid arguments!");
Douglas Gregor1adb0822010-01-16 17:14:40 +0000263 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000264 CXCursor C = { CXCursor_ObjCClassRef, { Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000265 return C;
266}
267
268std::pair<ObjCInterfaceDecl *, SourceLocation>
269cxcursor::getCursorObjCClassRef(CXCursor C) {
270 assert(C.kind == CXCursor_ObjCClassRef);
271 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
272 SourceLocation::getFromRawEncoding(
273 reinterpret_cast<uintptr_t>(C.data[1])));
274}
275
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000276CXCursor cxcursor::MakeCursorTypeRef(TypeDecl *Type, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000277 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000278 assert(Type && TU && "Invalid arguments!");
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000279 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
280 CXCursor C = { CXCursor_TypeRef, { Type, RawLoc, TU } };
281 return C;
282}
283
284std::pair<TypeDecl *, SourceLocation>
285cxcursor::getCursorTypeRef(CXCursor C) {
286 assert(C.kind == CXCursor_TypeRef);
287 return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
288 SourceLocation::getFromRawEncoding(
289 reinterpret_cast<uintptr_t>(C.data[1])));
290}
291
Douglas Gregor0b36e612010-08-31 20:37:03 +0000292CXCursor cxcursor::MakeCursorTemplateRef(TemplateDecl *Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000293 SourceLocation Loc,
294 CXTranslationUnit TU) {
Douglas Gregor0b36e612010-08-31 20:37:03 +0000295 assert(Template && TU && "Invalid arguments!");
296 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
297 CXCursor C = { CXCursor_TemplateRef, { Template, RawLoc, TU } };
298 return C;
299}
300
301std::pair<TemplateDecl *, SourceLocation>
302cxcursor::getCursorTemplateRef(CXCursor C) {
303 assert(C.kind == CXCursor_TemplateRef);
304 return std::make_pair(static_cast<TemplateDecl *>(C.data[0]),
305 SourceLocation::getFromRawEncoding(
306 reinterpret_cast<uintptr_t>(C.data[1])));
307}
308
Douglas Gregor69319002010-08-31 23:48:11 +0000309CXCursor cxcursor::MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000310 CXTranslationUnit TU) {
Douglas Gregor69319002010-08-31 23:48:11 +0000311
312 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
313 "Invalid arguments!");
314 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
315 CXCursor C = { CXCursor_NamespaceRef, { NS, RawLoc, TU } };
316 return C;
317}
318
319std::pair<NamedDecl *, SourceLocation>
320cxcursor::getCursorNamespaceRef(CXCursor C) {
321 assert(C.kind == CXCursor_NamespaceRef);
322 return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
323 SourceLocation::getFromRawEncoding(
324 reinterpret_cast<uintptr_t>(C.data[1])));
325}
326
Douglas Gregora67e03f2010-09-09 21:42:20 +0000327CXCursor cxcursor::MakeCursorMemberRef(FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000328 CXTranslationUnit TU) {
Douglas Gregora67e03f2010-09-09 21:42:20 +0000329
330 assert(Field && TU && "Invalid arguments!");
331 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
332 CXCursor C = { CXCursor_MemberRef, { Field, RawLoc, TU } };
333 return C;
334}
335
336std::pair<FieldDecl *, SourceLocation>
337cxcursor::getCursorMemberRef(CXCursor C) {
338 assert(C.kind == CXCursor_MemberRef);
339 return std::make_pair(static_cast<FieldDecl *>(C.data[0]),
340 SourceLocation::getFromRawEncoding(
341 reinterpret_cast<uintptr_t>(C.data[1])));
342}
343
Ted Kremeneka60ed472010-11-16 08:15:36 +0000344CXCursor cxcursor::MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B,
345 CXTranslationUnit TU){
Ted Kremenek3064ef92010-08-27 21:34:58 +0000346 CXCursor C = { CXCursor_CXXBaseSpecifier, { B, 0, TU } };
347 return C;
348}
349
350CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
351 assert(C.kind == CXCursor_CXXBaseSpecifier);
352 return static_cast<CXXBaseSpecifier*>(C.data[0]);
353}
354
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000355CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000356 CXTranslationUnit TU) {
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000357 CXCursor C = { CXCursor_PreprocessingDirective,
358 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
359 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
360 TU }
361 };
362 return C;
363}
364
365SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
366 assert(C.kind == CXCursor_PreprocessingDirective);
367 return SourceRange(SourceLocation::getFromRawEncoding(
368 reinterpret_cast<uintptr_t> (C.data[0])),
369 SourceLocation::getFromRawEncoding(
370 reinterpret_cast<uintptr_t> (C.data[1])));
371}
372
Ted Kremeneka60ed472010-11-16 08:15:36 +0000373CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI,
374 CXTranslationUnit TU) {
Douglas Gregor572feb22010-03-18 18:04:21 +0000375 CXCursor C = { CXCursor_MacroDefinition, { MI, 0, TU } };
376 return C;
377}
378
379MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
380 assert(C.kind == CXCursor_MacroDefinition);
381 return static_cast<MacroDefinition *>(C.data[0]);
382}
383
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000384CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
385 CXTranslationUnit TU) {
Chandler Carruth9b2a0ac2011-07-14 08:41:15 +0000386 CXCursor C = { CXCursor_MacroExpansion, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000387 return C;
388}
389
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000390MacroExpansion *cxcursor::getCursorMacroExpansion(CXCursor C) {
Chandler Carruth9b2a0ac2011-07-14 08:41:15 +0000391 assert(C.kind == CXCursor_MacroExpansion);
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000392 return static_cast<MacroExpansion *>(C.data[0]);
Douglas Gregor48072312010-03-18 15:23:44 +0000393}
394
Douglas Gregorecdcb882010-10-20 22:00:55 +0000395CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000396 CXTranslationUnit TU) {
Douglas Gregorecdcb882010-10-20 22:00:55 +0000397 CXCursor C = { CXCursor_InclusionDirective, { ID, 0, TU } };
398 return C;
399}
400
401InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
402 assert(C.kind == CXCursor_InclusionDirective);
403 return static_cast<InclusionDirective *>(C.data[0]);
404}
405
Douglas Gregor36897b02010-09-10 00:22:18 +0000406CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000407 CXTranslationUnit TU) {
Douglas Gregor36897b02010-09-10 00:22:18 +0000408
409 assert(Label && TU && "Invalid arguments!");
410 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
411 CXCursor C = { CXCursor_LabelRef, { Label, RawLoc, TU } };
412 return C;
413}
414
415std::pair<LabelStmt*, SourceLocation>
416cxcursor::getCursorLabelRef(CXCursor C) {
417 assert(C.kind == CXCursor_LabelRef);
418 return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
419 SourceLocation::getFromRawEncoding(
420 reinterpret_cast<uintptr_t>(C.data[1])));
421}
422
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000423CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000424 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000425 assert(E && TU && "Invalid arguments!");
426 OverloadedDeclRefStorage Storage(E);
427 void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding());
428 CXCursor C = {
429 CXCursor_OverloadedDeclRef,
430 { Storage.getOpaqueValue(), RawLoc, TU }
431 };
432 return C;
433}
434
435CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D,
436 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000437 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000438 assert(D && TU && "Invalid arguments!");
439 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
440 OverloadedDeclRefStorage Storage(D);
441 CXCursor C = {
442 CXCursor_OverloadedDeclRef,
443 { Storage.getOpaqueValue(), RawLoc, TU }
444 };
445 return C;
446}
447
448CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
449 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000450 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000451 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
452 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
453 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
454 CXCursor C = {
455 CXCursor_OverloadedDeclRef,
456 { Storage.getOpaqueValue(), RawLoc, TU }
457 };
458 return C;
459}
460
461std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
462cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
463 assert(C.kind == CXCursor_OverloadedDeclRef);
464 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]),
465 SourceLocation::getFromRawEncoding(
466 reinterpret_cast<uintptr_t>(C.data[1])));
467}
468
Douglas Gregor283cae32010-01-15 21:56:13 +0000469Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
470 return (Decl *)Cursor.data[0];
471}
472
473Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
474 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
475}
476
477Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000478 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000479 Cursor.kind == CXCursor_ObjCProtocolRef ||
480 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000481 return 0;
482
Douglas Gregor283cae32010-01-15 21:56:13 +0000483 return (Stmt *)Cursor.data[1];
484}
485
Ted Kremenek95f33552010-08-26 01:42:22 +0000486Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
487 return (Attr *)Cursor.data[1];
488}
489
Argyrios Kyrtzidis8ccac3d2011-06-29 22:20:07 +0000490Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
491 return (Decl *)Cursor.data[0];
492}
493
Douglas Gregorf46034a2010-01-18 23:41:10 +0000494ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000495 return getCursorASTUnit(Cursor)->getASTContext();
496}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000497
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000498ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Ted Kremeneka60ed472010-11-16 08:15:36 +0000499 return static_cast<ASTUnit *>(static_cast<CXTranslationUnit>(Cursor.data[2])
500 ->TUData);
501}
502
503CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
504 return static_cast<CXTranslationUnit>(Cursor.data[2]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000505}
506
Douglas Gregor283cae32010-01-15 21:56:13 +0000507bool cxcursor::operator==(CXCursor X, CXCursor Y) {
508 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
509 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000510}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000511
512// FIXME: Remove once we can model DeclGroups and their appropriate ranges
513// properly in the ASTs.
514bool cxcursor::isFirstInDeclGroup(CXCursor C) {
515 assert(clang_isDeclaration(C.kind));
516 return ((uintptr_t) (C.data[1])) != 0;
517}
518
Ted Kremenekeca099b2010-12-08 23:43:14 +0000519//===----------------------------------------------------------------------===//
520// CXCursorSet.
521//===----------------------------------------------------------------------===//
522
523typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
524
525static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
526 return (CXCursorSet) setImpl;
527}
528static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
529 return (CXCursorSet_Impl*) set;
530}
531namespace llvm {
Ted Kremenekda6fb692010-12-09 00:33:41 +0000532template<> struct DenseMapInfo<CXCursor> {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000533public:
534 static inline CXCursor getEmptyKey() {
535 return MakeCXCursorInvalid(CXCursor_InvalidFile);
536 }
537 static inline CXCursor getTombstoneKey() {
538 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
539 }
540 static inline unsigned getHashValue(const CXCursor &cursor) {
541 return llvm::DenseMapInfo<std::pair<void*,void*> >
542 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
543 }
544 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
545 return x.kind == y.kind &&
546 x.data[0] == y.data[0] &&
547 x.data[1] == y.data[1];
548 }
549};
550}
551
552extern "C" {
553CXCursorSet clang_createCXCursorSet() {
554 return packCXCursorSet(new CXCursorSet_Impl());
555}
556
557void clang_disposeCXCursorSet(CXCursorSet set) {
558 delete unpackCXCursorSet(set);
559}
560
561unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
562 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
563 if (!setImpl)
564 return 0;
565 return setImpl->find(cursor) == setImpl->end();
566}
567
Anders Carlssone8b3de02010-12-09 01:00:12 +0000568unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000569 // Do not insert invalid cursors into the set.
570 if (cursor.kind >= CXCursor_FirstInvalid &&
571 cursor.kind <= CXCursor_LastInvalid)
572 return 1;
573
574 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
575 if (!setImpl)
576 return 1;
577 unsigned &entry = (*setImpl)[cursor];
578 unsigned flag = entry == 0 ? 1 : 0;
579 entry = 1;
580 return flag;
581}
Douglas Gregor8fa0a802011-08-04 20:04:59 +0000582
583CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
584 enum CXCursorKind kind = clang_getCursorKind(cursor);
585 if (clang_isDeclaration(kind)) {
586 Decl *decl = getCursorDecl(cursor);
587 if (isa<NamedDecl>(decl)) {
588 NamedDecl *namedDecl = (NamedDecl *)decl;
589 ASTUnit *unit = getCursorASTUnit(cursor);
590 if (unit->hasSema()) {
591 Sema &S = unit->getSema();
592 CodeCompletionAllocator *Allocator
593 = unit->getCursorCompletionAllocator().getPtr();
594 CodeCompletionResult Result(namedDecl);
595 CodeCompletionString *String
596 = Result.CreateCodeCompletionString(S, *Allocator);
597 return String;
598 }
599 }
600 }
601 else if (kind == CXCursor_MacroDefinition) {
602 MacroDefinition *definition = getCursorMacroDefinition(cursor);
603 const IdentifierInfo *MacroInfo = definition->getName();
604 ASTUnit *unit = getCursorASTUnit(cursor);
605 if (unit->hasSema()) {
606 Sema &S = unit->getSema();
607 CodeCompletionAllocator *Allocator
608 = unit->getCursorCompletionAllocator().getPtr();
Douglas Gregoreaf4fba2011-08-10 16:34:38 +0000609 CodeCompletionResult Result(const_cast<IdentifierInfo *>(MacroInfo));
Douglas Gregor8fa0a802011-08-04 20:04:59 +0000610 CodeCompletionString *String
611 = Result.CreateCodeCompletionString(S, *Allocator);
612 return String;
613 }
614 }
615 return NULL;
616}
617
Ted Kremenekeca099b2010-12-08 23:43:14 +0000618} // end: extern "C"