blob: 3aec4a2ea90f9dd2f00da3110f18e3e73ff1a706 [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;
Douglas Gregor42b29842011-10-05 19:00:14 +000075
Douglas Gregor97b98722010-01-19 23:20:36 +000076 case Stmt::CaseStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +000077 K = CXCursor_CaseStmt;
78 break;
79
Douglas Gregor97b98722010-01-19 23:20:36 +000080 case Stmt::DefaultStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +000081 K = CXCursor_DefaultStmt;
82 break;
83
84 case Stmt::IfStmtClass:
85 K = CXCursor_IfStmt;
86 break;
87
88 case Stmt::SwitchStmtClass:
89 K = CXCursor_SwitchStmt;
90 break;
91
92 case Stmt::WhileStmtClass:
93 K = CXCursor_WhileStmt;
94 break;
95
96 case Stmt::DoStmtClass:
97 K = CXCursor_DoStmt;
98 break;
99
100 case Stmt::ForStmtClass:
101 K = CXCursor_ForStmt;
102 break;
103
104 case Stmt::GotoStmtClass:
105 K = CXCursor_GotoStmt;
106 break;
107
Douglas Gregor97b98722010-01-19 23:20:36 +0000108 case Stmt::IndirectGotoStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000109 K = CXCursor_IndirectGotoStmt;
110 break;
111
112 case Stmt::ContinueStmtClass:
113 K = CXCursor_ContinueStmt;
114 break;
115
116 case Stmt::BreakStmtClass:
117 K = CXCursor_BreakStmt;
118 break;
119
120 case Stmt::ReturnStmtClass:
121 K = CXCursor_ReturnStmt;
122 break;
123
124 case Stmt::AsmStmtClass:
125 K = CXCursor_AsmStmt;
126 break;
127
128 case Stmt::ObjCAtTryStmtClass:
129 K = CXCursor_ObjCAtTryStmt;
130 break;
131
132 case Stmt::ObjCAtCatchStmtClass:
133 K = CXCursor_ObjCAtCatchStmt;
134 break;
135
136 case Stmt::ObjCAtFinallyStmtClass:
137 K = CXCursor_ObjCAtFinallyStmt;
138 break;
139
140 case Stmt::ObjCAtThrowStmtClass:
141 K = CXCursor_ObjCAtThrowStmt;
142 break;
143
144 case Stmt::ObjCAtSynchronizedStmtClass:
145 K = CXCursor_ObjCAtSynchronizedStmt;
146 break;
147
148 case Stmt::ObjCAutoreleasePoolStmtClass:
149 K = CXCursor_ObjCAutoreleasePoolStmt;
150 break;
151
Douglas Gregor97b98722010-01-19 23:20:36 +0000152 case Stmt::ObjCForCollectionStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000153 K = CXCursor_ObjCForCollectionStmt;
154 break;
155
Douglas Gregor97b98722010-01-19 23:20:36 +0000156 case Stmt::CXXCatchStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000157 K = CXCursor_CXXCatchStmt;
158 break;
159
160 case Stmt::CXXTryStmtClass:
161 K = CXCursor_CXXTryStmt;
162 break;
163
164 case Stmt::CXXForRangeStmtClass:
165 K = CXCursor_CXXForRangeStmt;
166 break;
167
John Wiegley28bbe4b2011-04-28 01:08:34 +0000168 case Stmt::SEHTryStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000169 K = CXCursor_SEHTryStmt;
170 break;
171
John Wiegley28bbe4b2011-04-28 01:08:34 +0000172 case Stmt::SEHExceptStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000173 K = CXCursor_SEHExceptStmt;
174 break;
175
John Wiegley28bbe4b2011-04-28 01:08:34 +0000176 case Stmt::SEHFinallyStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000177 K = CXCursor_SEHFinallyStmt;
Douglas Gregor97b98722010-01-19 23:20:36 +0000178 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000179
John Wiegley21ff2e52011-04-28 00:16:57 +0000180 case Stmt::ArrayTypeTraitExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +0000181 case Stmt::AsTypeExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000182 case Stmt::BinaryConditionalOperatorClass:
183 case Stmt::BinaryTypeTraitExprClass:
184 case Stmt::CXXBindTemporaryExprClass:
185 case Stmt::CXXDefaultArgExprClass:
186 case Stmt::CXXScalarValueInitExprClass:
187 case Stmt::CXXUuidofExprClass:
188 case Stmt::ChooseExprClass:
189 case Stmt::DesignatedInitExprClass:
190 case Stmt::ExprWithCleanupsClass:
191 case Stmt::ExpressionTraitExprClass:
192 case Stmt::ExtVectorElementExprClass:
193 case Stmt::ImplicitCastExprClass:
194 case Stmt::ImplicitValueInitExprClass:
195 case Stmt::MaterializeTemporaryExprClass:
196 case Stmt::ObjCIndirectCopyRestoreExprClass:
197 case Stmt::OffsetOfExprClass:
198 case Stmt::OpaqueValueExprClass:
199 case Stmt::ParenListExprClass:
200 case Stmt::PredefinedExprClass:
201 case Stmt::ShuffleVectorExprClass:
202 case Stmt::UnaryExprOrTypeTraitExprClass:
203 case Stmt::UnaryTypeTraitExprClass:
204 case Stmt::VAArgExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000205 K = CXCursor_UnexposedExpr;
206 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000207
208 case Stmt::CompoundStmtClass:
209 K = CXCursor_CompoundStmt;
210 break;
Douglas Gregor36897b02010-09-10 00:22:18 +0000211
Douglas Gregor42b29842011-10-05 19:00:14 +0000212 case Stmt::NullStmtClass:
213 K = CXCursor_NullStmt;
214 break;
215
216 case Stmt::LabelStmtClass:
217 K = CXCursor_LabelStmt;
218 break;
219
220 case Stmt::DeclStmtClass:
221 K = CXCursor_DeclStmt;
222 break;
223
224 case Stmt::IntegerLiteralClass:
225 K = CXCursor_IntegerLiteral;
226 break;
227
228 case Stmt::FloatingLiteralClass:
229 K = CXCursor_FloatingLiteral;
230 break;
231
232 case Stmt::ImaginaryLiteralClass:
233 K = CXCursor_ImaginaryLiteral;
234 break;
235
236 case Stmt::StringLiteralClass:
237 K = CXCursor_StringLiteral;
238 break;
239
240 case Stmt::CharacterLiteralClass:
241 K = CXCursor_CharacterLiteral;
242 break;
243
244 case Stmt::ParenExprClass:
245 K = CXCursor_ParenExpr;
246 break;
247
248 case Stmt::UnaryOperatorClass:
249 K = CXCursor_UnaryOperator;
250 break;
251
252 case Stmt::CXXNoexceptExprClass:
253 K = CXCursor_UnaryExpr;
254 break;
255
256 case Stmt::ArraySubscriptExprClass:
257 K = CXCursor_ArraySubscriptExpr;
258 break;
259
260 case Stmt::BinaryOperatorClass:
261 K = CXCursor_BinaryOperator;
262 break;
263
264 case Stmt::CompoundAssignOperatorClass:
265 K = CXCursor_CompoundAssignOperator;
266 break;
267
268 case Stmt::ConditionalOperatorClass:
269 K = CXCursor_ConditionalOperator;
270 break;
271
272 case Stmt::CStyleCastExprClass:
273 K = CXCursor_CStyleCastExpr;
274 break;
275
276 case Stmt::CompoundLiteralExprClass:
277 K = CXCursor_CompoundLiteralExpr;
278 break;
279
280 case Stmt::InitListExprClass:
281 K = CXCursor_InitListExpr;
282 break;
283
284 case Stmt::AddrLabelExprClass:
285 K = CXCursor_AddrLabelExpr;
286 break;
287
288 case Stmt::StmtExprClass:
289 K = CXCursor_StmtExpr;
290 break;
291
292 case Stmt::GenericSelectionExprClass:
293 K = CXCursor_GenericSelectionExpr;
294 break;
295
296 case Stmt::GNUNullExprClass:
297 K = CXCursor_GNUNullExpr;
298 break;
299
300 case Stmt::CXXStaticCastExprClass:
301 K = CXCursor_CXXStaticCastExpr;
302 break;
303
304 case Stmt::CXXDynamicCastExprClass:
305 K = CXCursor_CXXDynamicCastExpr;
306 break;
307
308 case Stmt::CXXReinterpretCastExprClass:
309 K = CXCursor_CXXReinterpretCastExpr;
310 break;
311
312 case Stmt::CXXConstCastExprClass:
313 K = CXCursor_CXXConstCastExpr;
314 break;
315
316 case Stmt::CXXFunctionalCastExprClass:
317 K = CXCursor_CXXFunctionalCastExpr;
318 break;
319
320 case Stmt::CXXTypeidExprClass:
321 K = CXCursor_CXXTypeidExpr;
322 break;
323
324 case Stmt::CXXBoolLiteralExprClass:
325 K = CXCursor_CXXBoolLiteralExpr;
326 break;
327
328 case Stmt::CXXNullPtrLiteralExprClass:
329 K = CXCursor_CXXNullPtrLiteralExpr;
330 break;
331
332 case Stmt::CXXThisExprClass:
333 K = CXCursor_CXXThisExpr;
334 break;
335
336 case Stmt::CXXThrowExprClass:
337 K = CXCursor_CXXThrowExpr;
338 break;
339
340 case Stmt::CXXNewExprClass:
341 K = CXCursor_CXXNewExpr;
342 break;
343
344 case Stmt::CXXDeleteExprClass:
345 K = CXCursor_CXXDeleteExpr;
346 break;
347
348 case Stmt::ObjCStringLiteralClass:
349 K = CXCursor_ObjCStringLiteral;
350 break;
351
352 case Stmt::ObjCEncodeExprClass:
353 K = CXCursor_ObjCEncodeExpr;
354 break;
355
356 case Stmt::ObjCSelectorExprClass:
357 K = CXCursor_ObjCSelectorExpr;
358 break;
359
360 case Stmt::ObjCProtocolExprClass:
361 K = CXCursor_ObjCProtocolExpr;
362 break;
363
364 case Stmt::ObjCBridgedCastExprClass:
365 K = CXCursor_ObjCBridgedCastExpr;
366 break;
367
368 case Stmt::BlockExprClass:
369 K = CXCursor_BlockExpr;
370 break;
371
372 case Stmt::PackExpansionExprClass:
373 K = CXCursor_PackExpansionExpr;
374 break;
375
376 case Stmt::SizeOfPackExprClass:
377 K = CXCursor_SizeOfPackExpr;
378 break;
379
Douglas Gregor97b98722010-01-19 23:20:36 +0000380 case Stmt::BlockDeclRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000381 case Stmt::DeclRefExprClass:
382 case Stmt::DependentScopeDeclRefExprClass:
John McCall91a57552011-07-15 05:09:51 +0000383 case Stmt::SubstNonTypeTemplateParmExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +0000384 case Stmt::SubstNonTypeTemplateParmPackExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000385 case Stmt::UnresolvedLookupExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000386 K = CXCursor_DeclRefExpr;
387 break;
388
Douglas Gregor42b29842011-10-05 19:00:14 +0000389 case Stmt::CXXDependentScopeMemberExprClass:
390 case Stmt::CXXPseudoDestructorExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000391 case Stmt::MemberExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000392 case Stmt::ObjCIsaExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000393 case Stmt::ObjCIvarRefExprClass:
394 case Stmt::ObjCPropertyRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000395 case Stmt::UnresolvedMemberExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000396 K = CXCursor_MemberRefExpr;
397 break;
398
399 case Stmt::CallExprClass:
400 case Stmt::CXXOperatorCallExprClass:
401 case Stmt::CXXMemberCallExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +0000402 case Stmt::CUDAKernelCallExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000403 case Stmt::CXXConstructExprClass:
404 case Stmt::CXXTemporaryObjectExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000405 case Stmt::CXXUnresolvedConstructExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000406 K = CXCursor_CallExpr;
407 break;
408
409 case Stmt::ObjCMessageExprClass:
410 K = CXCursor_ObjCMessageExpr;
411 break;
412 }
413
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000414 CXCursor C = { K, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000415 return C;
416}
417
Douglas Gregor2e331b92010-01-16 14:00:32 +0000418CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000419 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000420 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000421 assert(Super && TU && "Invalid arguments!");
Douglas Gregor2e331b92010-01-16 14:00:32 +0000422 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000423 CXCursor C = { CXCursor_ObjCSuperClassRef, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000424 return C;
425}
426
427std::pair<ObjCInterfaceDecl *, SourceLocation>
428cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
429 assert(C.kind == CXCursor_ObjCSuperClassRef);
430 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
431 SourceLocation::getFromRawEncoding(
432 reinterpret_cast<uintptr_t>(C.data[1])));
433}
434
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000435CXCursor cxcursor::MakeCursorObjCProtocolRef(ObjCProtocolDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000436 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000437 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000438 assert(Super && TU && "Invalid arguments!");
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000439 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000440 CXCursor C = { CXCursor_ObjCProtocolRef, { Super, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000441 return C;
442}
443
444std::pair<ObjCProtocolDecl *, SourceLocation>
445cxcursor::getCursorObjCProtocolRef(CXCursor C) {
446 assert(C.kind == CXCursor_ObjCProtocolRef);
447 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
448 SourceLocation::getFromRawEncoding(
449 reinterpret_cast<uintptr_t>(C.data[1])));
450}
451
Douglas Gregor1adb0822010-01-16 17:14:40 +0000452CXCursor cxcursor::MakeCursorObjCClassRef(ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000453 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000454 CXTranslationUnit TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000455 // 'Class' can be null for invalid code.
456 if (!Class)
457 return MakeCXCursorInvalid(CXCursor_InvalidCode);
458 assert(TU && "Invalid arguments!");
Douglas Gregor1adb0822010-01-16 17:14:40 +0000459 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000460 CXCursor C = { CXCursor_ObjCClassRef, { Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000461 return C;
462}
463
464std::pair<ObjCInterfaceDecl *, SourceLocation>
465cxcursor::getCursorObjCClassRef(CXCursor C) {
466 assert(C.kind == CXCursor_ObjCClassRef);
467 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
468 SourceLocation::getFromRawEncoding(
469 reinterpret_cast<uintptr_t>(C.data[1])));
470}
471
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000472CXCursor cxcursor::MakeCursorTypeRef(TypeDecl *Type, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000473 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000474 assert(Type && TU && "Invalid arguments!");
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000475 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
476 CXCursor C = { CXCursor_TypeRef, { Type, RawLoc, TU } };
477 return C;
478}
479
480std::pair<TypeDecl *, SourceLocation>
481cxcursor::getCursorTypeRef(CXCursor C) {
482 assert(C.kind == CXCursor_TypeRef);
483 return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
484 SourceLocation::getFromRawEncoding(
485 reinterpret_cast<uintptr_t>(C.data[1])));
486}
487
Douglas Gregor0b36e612010-08-31 20:37:03 +0000488CXCursor cxcursor::MakeCursorTemplateRef(TemplateDecl *Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000489 SourceLocation Loc,
490 CXTranslationUnit TU) {
Douglas Gregor0b36e612010-08-31 20:37:03 +0000491 assert(Template && TU && "Invalid arguments!");
492 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
493 CXCursor C = { CXCursor_TemplateRef, { Template, RawLoc, TU } };
494 return C;
495}
496
497std::pair<TemplateDecl *, SourceLocation>
498cxcursor::getCursorTemplateRef(CXCursor C) {
499 assert(C.kind == CXCursor_TemplateRef);
500 return std::make_pair(static_cast<TemplateDecl *>(C.data[0]),
501 SourceLocation::getFromRawEncoding(
502 reinterpret_cast<uintptr_t>(C.data[1])));
503}
504
Douglas Gregor69319002010-08-31 23:48:11 +0000505CXCursor cxcursor::MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000506 CXTranslationUnit TU) {
Douglas Gregor69319002010-08-31 23:48:11 +0000507
508 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
509 "Invalid arguments!");
510 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
511 CXCursor C = { CXCursor_NamespaceRef, { NS, RawLoc, TU } };
512 return C;
513}
514
515std::pair<NamedDecl *, SourceLocation>
516cxcursor::getCursorNamespaceRef(CXCursor C) {
517 assert(C.kind == CXCursor_NamespaceRef);
518 return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
519 SourceLocation::getFromRawEncoding(
520 reinterpret_cast<uintptr_t>(C.data[1])));
521}
522
Douglas Gregora67e03f2010-09-09 21:42:20 +0000523CXCursor cxcursor::MakeCursorMemberRef(FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000524 CXTranslationUnit TU) {
Douglas Gregora67e03f2010-09-09 21:42:20 +0000525
526 assert(Field && TU && "Invalid arguments!");
527 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
528 CXCursor C = { CXCursor_MemberRef, { Field, RawLoc, TU } };
529 return C;
530}
531
532std::pair<FieldDecl *, SourceLocation>
533cxcursor::getCursorMemberRef(CXCursor C) {
534 assert(C.kind == CXCursor_MemberRef);
535 return std::make_pair(static_cast<FieldDecl *>(C.data[0]),
536 SourceLocation::getFromRawEncoding(
537 reinterpret_cast<uintptr_t>(C.data[1])));
538}
539
Ted Kremeneka60ed472010-11-16 08:15:36 +0000540CXCursor cxcursor::MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B,
541 CXTranslationUnit TU){
Ted Kremenek3064ef92010-08-27 21:34:58 +0000542 CXCursor C = { CXCursor_CXXBaseSpecifier, { B, 0, TU } };
543 return C;
544}
545
546CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
547 assert(C.kind == CXCursor_CXXBaseSpecifier);
548 return static_cast<CXXBaseSpecifier*>(C.data[0]);
549}
550
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000551CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000552 CXTranslationUnit TU) {
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000553 CXCursor C = { CXCursor_PreprocessingDirective,
554 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
555 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
556 TU }
557 };
558 return C;
559}
560
561SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
562 assert(C.kind == CXCursor_PreprocessingDirective);
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000563 SourceRange Range = SourceRange(SourceLocation::getFromRawEncoding(
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000564 reinterpret_cast<uintptr_t> (C.data[0])),
565 SourceLocation::getFromRawEncoding(
566 reinterpret_cast<uintptr_t> (C.data[1])));
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000567 ASTUnit *TU = getCursorASTUnit(C);
568 return TU->mapRangeFromPreamble(Range);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000569}
570
Ted Kremeneka60ed472010-11-16 08:15:36 +0000571CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI,
572 CXTranslationUnit TU) {
Douglas Gregor572feb22010-03-18 18:04:21 +0000573 CXCursor C = { CXCursor_MacroDefinition, { MI, 0, TU } };
574 return C;
575}
576
577MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
578 assert(C.kind == CXCursor_MacroDefinition);
579 return static_cast<MacroDefinition *>(C.data[0]);
580}
581
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000582CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
583 CXTranslationUnit TU) {
Chandler Carruth9b2a0ac2011-07-14 08:41:15 +0000584 CXCursor C = { CXCursor_MacroExpansion, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000585 return C;
586}
587
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000588MacroExpansion *cxcursor::getCursorMacroExpansion(CXCursor C) {
Chandler Carruth9b2a0ac2011-07-14 08:41:15 +0000589 assert(C.kind == CXCursor_MacroExpansion);
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000590 return static_cast<MacroExpansion *>(C.data[0]);
Douglas Gregor48072312010-03-18 15:23:44 +0000591}
592
Douglas Gregorecdcb882010-10-20 22:00:55 +0000593CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000594 CXTranslationUnit TU) {
Douglas Gregorecdcb882010-10-20 22:00:55 +0000595 CXCursor C = { CXCursor_InclusionDirective, { ID, 0, TU } };
596 return C;
597}
598
599InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
600 assert(C.kind == CXCursor_InclusionDirective);
601 return static_cast<InclusionDirective *>(C.data[0]);
602}
603
Douglas Gregor36897b02010-09-10 00:22:18 +0000604CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000605 CXTranslationUnit TU) {
Douglas Gregor36897b02010-09-10 00:22:18 +0000606
607 assert(Label && TU && "Invalid arguments!");
608 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
609 CXCursor C = { CXCursor_LabelRef, { Label, RawLoc, TU } };
610 return C;
611}
612
613std::pair<LabelStmt*, SourceLocation>
614cxcursor::getCursorLabelRef(CXCursor C) {
615 assert(C.kind == CXCursor_LabelRef);
616 return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
617 SourceLocation::getFromRawEncoding(
618 reinterpret_cast<uintptr_t>(C.data[1])));
619}
620
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000621CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000622 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000623 assert(E && TU && "Invalid arguments!");
624 OverloadedDeclRefStorage Storage(E);
625 void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding());
626 CXCursor C = {
627 CXCursor_OverloadedDeclRef,
628 { Storage.getOpaqueValue(), RawLoc, TU }
629 };
630 return C;
631}
632
633CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D,
634 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000635 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000636 assert(D && TU && "Invalid arguments!");
637 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
638 OverloadedDeclRefStorage Storage(D);
639 CXCursor C = {
640 CXCursor_OverloadedDeclRef,
641 { Storage.getOpaqueValue(), RawLoc, TU }
642 };
643 return C;
644}
645
646CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
647 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000648 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000649 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
650 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
651 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
652 CXCursor C = {
653 CXCursor_OverloadedDeclRef,
654 { Storage.getOpaqueValue(), RawLoc, TU }
655 };
656 return C;
657}
658
659std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
660cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
661 assert(C.kind == CXCursor_OverloadedDeclRef);
662 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]),
663 SourceLocation::getFromRawEncoding(
664 reinterpret_cast<uintptr_t>(C.data[1])));
665}
666
Douglas Gregor283cae32010-01-15 21:56:13 +0000667Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
668 return (Decl *)Cursor.data[0];
669}
670
671Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
672 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
673}
674
675Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000676 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000677 Cursor.kind == CXCursor_ObjCProtocolRef ||
678 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000679 return 0;
680
Douglas Gregor283cae32010-01-15 21:56:13 +0000681 return (Stmt *)Cursor.data[1];
682}
683
Ted Kremenek95f33552010-08-26 01:42:22 +0000684Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
685 return (Attr *)Cursor.data[1];
686}
687
Argyrios Kyrtzidis8ccac3d2011-06-29 22:20:07 +0000688Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
689 return (Decl *)Cursor.data[0];
690}
691
Douglas Gregorf46034a2010-01-18 23:41:10 +0000692ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000693 return getCursorASTUnit(Cursor)->getASTContext();
694}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000695
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000696ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Ted Kremeneka60ed472010-11-16 08:15:36 +0000697 return static_cast<ASTUnit *>(static_cast<CXTranslationUnit>(Cursor.data[2])
698 ->TUData);
699}
700
701CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
702 return static_cast<CXTranslationUnit>(Cursor.data[2]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000703}
704
Argyrios Kyrtzidisb11be042011-10-06 07:00:46 +0000705static void CollectOverriddenMethods(CXTranslationUnit TU,
706 DeclContext *Ctx,
707 ObjCMethodDecl *Method,
708 SmallVectorImpl<CXCursor> &Methods) {
709 if (!Ctx)
710 return;
711
712 // If we have a class or category implementation, jump straight to the
713 // interface.
714 if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(Ctx))
715 return CollectOverriddenMethods(TU, Impl->getClassInterface(),
716 Method, Methods);
717
718 ObjCContainerDecl *Container = dyn_cast<ObjCContainerDecl>(Ctx);
719 if (!Container)
720 return;
721
722 // Check whether we have a matching method at this level.
723 if (ObjCMethodDecl *Overridden = Container->getMethod(Method->getSelector(),
724 Method->isInstanceMethod()))
725 if (Method != Overridden) {
726 // We found an override at this level; there is no need to look
727 // into other protocols or categories.
728 Methods.push_back(MakeCXCursor(Overridden, TU));
729 return;
730 }
731
732 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
733 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
734 PEnd = Protocol->protocol_end();
735 P != PEnd; ++P)
736 CollectOverriddenMethods(TU, *P, Method, Methods);
737 }
738
739 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
740 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
741 PEnd = Category->protocol_end();
742 P != PEnd; ++P)
743 CollectOverriddenMethods(TU, *P, Method, Methods);
744 }
745
746 if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
747 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
748 PEnd = Interface->protocol_end();
749 P != PEnd; ++P)
750 CollectOverriddenMethods(TU, *P, Method, Methods);
751
752 for (ObjCCategoryDecl *Category = Interface->getCategoryList();
753 Category; Category = Category->getNextClassCategory())
754 CollectOverriddenMethods(TU, Category, Method, Methods);
755
756 // We only look into the superclass if we haven't found anything yet.
757 if (Methods.empty())
758 if (ObjCInterfaceDecl *Super = Interface->getSuperClass())
759 return CollectOverriddenMethods(TU, Super, Method, Methods);
760 }
761}
762
763void cxcursor::getOverriddenCursors(CXCursor cursor,
764 SmallVectorImpl<CXCursor> &overridden) {
765 if (!clang_isDeclaration(cursor.kind))
766 return;
767
768 Decl *D = getCursorDecl(cursor);
769 if (!D)
770 return;
771
772 // Handle C++ member functions.
773 CXTranslationUnit TU = getCursorTU(cursor);
774 if (CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) {
775 for (CXXMethodDecl::method_iterator
776 M = CXXMethod->begin_overridden_methods(),
777 MEnd = CXXMethod->end_overridden_methods();
778 M != MEnd; ++M)
779 overridden.push_back(MakeCXCursor(const_cast<CXXMethodDecl*>(*M), TU));
780 return;
781 }
782
783 ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D);
784 if (!Method)
785 return;
786
787 // Handle Objective-C methods.
788 CollectOverriddenMethods(TU, Method->getDeclContext(), Method, overridden);
789}
790
Douglas Gregor283cae32010-01-15 21:56:13 +0000791bool cxcursor::operator==(CXCursor X, CXCursor Y) {
792 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
793 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000794}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000795
796// FIXME: Remove once we can model DeclGroups and their appropriate ranges
797// properly in the ASTs.
798bool cxcursor::isFirstInDeclGroup(CXCursor C) {
799 assert(clang_isDeclaration(C.kind));
800 return ((uintptr_t) (C.data[1])) != 0;
801}
802
Ted Kremenekeca099b2010-12-08 23:43:14 +0000803//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000804// libclang CXCursor APIs
805//===----------------------------------------------------------------------===//
806
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000807extern "C" {
808
809int clang_Cursor_isNull(CXCursor cursor) {
810 return clang_equalCursors(cursor, clang_getNullCursor());
811}
812
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000813CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
814 return getCursorTU(cursor);
815}
816
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000817} // end: extern "C"
818
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000819//===----------------------------------------------------------------------===//
Ted Kremenekeca099b2010-12-08 23:43:14 +0000820// CXCursorSet.
821//===----------------------------------------------------------------------===//
822
823typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
824
825static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
826 return (CXCursorSet) setImpl;
827}
828static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
829 return (CXCursorSet_Impl*) set;
830}
831namespace llvm {
Ted Kremenekda6fb692010-12-09 00:33:41 +0000832template<> struct DenseMapInfo<CXCursor> {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000833public:
834 static inline CXCursor getEmptyKey() {
835 return MakeCXCursorInvalid(CXCursor_InvalidFile);
836 }
837 static inline CXCursor getTombstoneKey() {
838 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
839 }
840 static inline unsigned getHashValue(const CXCursor &cursor) {
841 return llvm::DenseMapInfo<std::pair<void*,void*> >
842 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
843 }
844 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
845 return x.kind == y.kind &&
846 x.data[0] == y.data[0] &&
847 x.data[1] == y.data[1];
848 }
849};
850}
851
852extern "C" {
853CXCursorSet clang_createCXCursorSet() {
854 return packCXCursorSet(new CXCursorSet_Impl());
855}
856
857void clang_disposeCXCursorSet(CXCursorSet set) {
858 delete unpackCXCursorSet(set);
859}
860
861unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
862 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
863 if (!setImpl)
864 return 0;
865 return setImpl->find(cursor) == setImpl->end();
866}
867
Anders Carlssone8b3de02010-12-09 01:00:12 +0000868unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000869 // Do not insert invalid cursors into the set.
870 if (cursor.kind >= CXCursor_FirstInvalid &&
871 cursor.kind <= CXCursor_LastInvalid)
872 return 1;
873
874 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
875 if (!setImpl)
876 return 1;
877 unsigned &entry = (*setImpl)[cursor];
878 unsigned flag = entry == 0 ? 1 : 0;
879 entry = 1;
880 return flag;
881}
Douglas Gregor8fa0a802011-08-04 20:04:59 +0000882
883CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
884 enum CXCursorKind kind = clang_getCursorKind(cursor);
885 if (clang_isDeclaration(kind)) {
886 Decl *decl = getCursorDecl(cursor);
887 if (isa<NamedDecl>(decl)) {
888 NamedDecl *namedDecl = (NamedDecl *)decl;
889 ASTUnit *unit = getCursorASTUnit(cursor);
890 if (unit->hasSema()) {
891 Sema &S = unit->getSema();
892 CodeCompletionAllocator *Allocator
893 = unit->getCursorCompletionAllocator().getPtr();
894 CodeCompletionResult Result(namedDecl);
895 CodeCompletionString *String
896 = Result.CreateCodeCompletionString(S, *Allocator);
897 return String;
898 }
899 }
900 }
901 else if (kind == CXCursor_MacroDefinition) {
902 MacroDefinition *definition = getCursorMacroDefinition(cursor);
903 const IdentifierInfo *MacroInfo = definition->getName();
904 ASTUnit *unit = getCursorASTUnit(cursor);
905 if (unit->hasSema()) {
906 Sema &S = unit->getSema();
907 CodeCompletionAllocator *Allocator
908 = unit->getCursorCompletionAllocator().getPtr();
Douglas Gregoreaf4fba2011-08-10 16:34:38 +0000909 CodeCompletionResult Result(const_cast<IdentifierInfo *>(MacroInfo));
Douglas Gregor8fa0a802011-08-04 20:04:59 +0000910 CodeCompletionString *String
911 = Result.CreateCodeCompletionString(S, *Allocator);
912 return String;
913 }
914 }
915 return NULL;
916}
917
Ted Kremenekeca099b2010-12-08 23:43:14 +0000918} // end: extern "C"