blob: 282f535d860ca0a9e933be5e9be140e04bb7b31c [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:
John McCall91a57552011-07-15 05:09:51 +0000186 case Stmt::SubstNonTypeTemplateParmExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +0000187 case Stmt::SubstNonTypeTemplateParmPackExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000188 // FIXME: UnresolvedLookupExpr?
189 // FIXME: DependentScopeDeclRefExpr?
190 K = CXCursor_DeclRefExpr;
191 break;
192
193 case Stmt::MemberExprClass:
194 case Stmt::ObjCIvarRefExprClass:
195 case Stmt::ObjCPropertyRefExprClass:
196 // FIXME: UnresolvedMemberExpr?
197 // FIXME: CXXDependentScopeMemberExpr?
198 K = CXCursor_MemberRefExpr;
199 break;
200
201 case Stmt::CallExprClass:
202 case Stmt::CXXOperatorCallExprClass:
203 case Stmt::CXXMemberCallExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +0000204 case Stmt::CUDAKernelCallExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000205 case Stmt::CXXConstructExprClass:
206 case Stmt::CXXTemporaryObjectExprClass:
207 // FIXME: CXXUnresolvedConstructExpr
Douglas Gregor97b98722010-01-19 23:20:36 +0000208 K = CXCursor_CallExpr;
209 break;
210
211 case Stmt::ObjCMessageExprClass:
212 K = CXCursor_ObjCMessageExpr;
213 break;
214 }
215
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000216 CXCursor C = { K, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000217 return C;
218}
219
Douglas Gregor2e331b92010-01-16 14:00:32 +0000220CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000221 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000222 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000223 assert(Super && TU && "Invalid arguments!");
Douglas Gregor2e331b92010-01-16 14:00:32 +0000224 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000225 CXCursor C = { CXCursor_ObjCSuperClassRef, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000226 return C;
227}
228
229std::pair<ObjCInterfaceDecl *, SourceLocation>
230cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
231 assert(C.kind == CXCursor_ObjCSuperClassRef);
232 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
233 SourceLocation::getFromRawEncoding(
234 reinterpret_cast<uintptr_t>(C.data[1])));
235}
236
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000237CXCursor cxcursor::MakeCursorObjCProtocolRef(ObjCProtocolDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000238 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000239 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000240 assert(Super && TU && "Invalid arguments!");
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000241 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000242 CXCursor C = { CXCursor_ObjCProtocolRef, { Super, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000243 return C;
244}
245
246std::pair<ObjCProtocolDecl *, SourceLocation>
247cxcursor::getCursorObjCProtocolRef(CXCursor C) {
248 assert(C.kind == CXCursor_ObjCProtocolRef);
249 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
250 SourceLocation::getFromRawEncoding(
251 reinterpret_cast<uintptr_t>(C.data[1])));
252}
253
Douglas Gregor1adb0822010-01-16 17:14:40 +0000254CXCursor cxcursor::MakeCursorObjCClassRef(ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000255 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000256 CXTranslationUnit TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000257 // 'Class' can be null for invalid code.
258 if (!Class)
259 return MakeCXCursorInvalid(CXCursor_InvalidCode);
260 assert(TU && "Invalid arguments!");
Douglas Gregor1adb0822010-01-16 17:14:40 +0000261 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000262 CXCursor C = { CXCursor_ObjCClassRef, { Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000263 return C;
264}
265
266std::pair<ObjCInterfaceDecl *, SourceLocation>
267cxcursor::getCursorObjCClassRef(CXCursor C) {
268 assert(C.kind == CXCursor_ObjCClassRef);
269 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
270 SourceLocation::getFromRawEncoding(
271 reinterpret_cast<uintptr_t>(C.data[1])));
272}
273
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000274CXCursor cxcursor::MakeCursorTypeRef(TypeDecl *Type, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000275 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000276 assert(Type && TU && "Invalid arguments!");
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000277 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
278 CXCursor C = { CXCursor_TypeRef, { Type, RawLoc, TU } };
279 return C;
280}
281
282std::pair<TypeDecl *, SourceLocation>
283cxcursor::getCursorTypeRef(CXCursor C) {
284 assert(C.kind == CXCursor_TypeRef);
285 return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
286 SourceLocation::getFromRawEncoding(
287 reinterpret_cast<uintptr_t>(C.data[1])));
288}
289
Douglas Gregor0b36e612010-08-31 20:37:03 +0000290CXCursor cxcursor::MakeCursorTemplateRef(TemplateDecl *Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000291 SourceLocation Loc,
292 CXTranslationUnit TU) {
Douglas Gregor0b36e612010-08-31 20:37:03 +0000293 assert(Template && TU && "Invalid arguments!");
294 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
295 CXCursor C = { CXCursor_TemplateRef, { Template, RawLoc, TU } };
296 return C;
297}
298
299std::pair<TemplateDecl *, SourceLocation>
300cxcursor::getCursorTemplateRef(CXCursor C) {
301 assert(C.kind == CXCursor_TemplateRef);
302 return std::make_pair(static_cast<TemplateDecl *>(C.data[0]),
303 SourceLocation::getFromRawEncoding(
304 reinterpret_cast<uintptr_t>(C.data[1])));
305}
306
Douglas Gregor69319002010-08-31 23:48:11 +0000307CXCursor cxcursor::MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000308 CXTranslationUnit TU) {
Douglas Gregor69319002010-08-31 23:48:11 +0000309
310 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
311 "Invalid arguments!");
312 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
313 CXCursor C = { CXCursor_NamespaceRef, { NS, RawLoc, TU } };
314 return C;
315}
316
317std::pair<NamedDecl *, SourceLocation>
318cxcursor::getCursorNamespaceRef(CXCursor C) {
319 assert(C.kind == CXCursor_NamespaceRef);
320 return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
321 SourceLocation::getFromRawEncoding(
322 reinterpret_cast<uintptr_t>(C.data[1])));
323}
324
Douglas Gregora67e03f2010-09-09 21:42:20 +0000325CXCursor cxcursor::MakeCursorMemberRef(FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000326 CXTranslationUnit TU) {
Douglas Gregora67e03f2010-09-09 21:42:20 +0000327
328 assert(Field && TU && "Invalid arguments!");
329 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
330 CXCursor C = { CXCursor_MemberRef, { Field, RawLoc, TU } };
331 return C;
332}
333
334std::pair<FieldDecl *, SourceLocation>
335cxcursor::getCursorMemberRef(CXCursor C) {
336 assert(C.kind == CXCursor_MemberRef);
337 return std::make_pair(static_cast<FieldDecl *>(C.data[0]),
338 SourceLocation::getFromRawEncoding(
339 reinterpret_cast<uintptr_t>(C.data[1])));
340}
341
Ted Kremeneka60ed472010-11-16 08:15:36 +0000342CXCursor cxcursor::MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B,
343 CXTranslationUnit TU){
Ted Kremenek3064ef92010-08-27 21:34:58 +0000344 CXCursor C = { CXCursor_CXXBaseSpecifier, { B, 0, TU } };
345 return C;
346}
347
348CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
349 assert(C.kind == CXCursor_CXXBaseSpecifier);
350 return static_cast<CXXBaseSpecifier*>(C.data[0]);
351}
352
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000353CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000354 CXTranslationUnit TU) {
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000355 CXCursor C = { CXCursor_PreprocessingDirective,
356 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
357 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
358 TU }
359 };
360 return C;
361}
362
363SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
364 assert(C.kind == CXCursor_PreprocessingDirective);
365 return SourceRange(SourceLocation::getFromRawEncoding(
366 reinterpret_cast<uintptr_t> (C.data[0])),
367 SourceLocation::getFromRawEncoding(
368 reinterpret_cast<uintptr_t> (C.data[1])));
369}
370
Ted Kremeneka60ed472010-11-16 08:15:36 +0000371CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI,
372 CXTranslationUnit TU) {
Douglas Gregor572feb22010-03-18 18:04:21 +0000373 CXCursor C = { CXCursor_MacroDefinition, { MI, 0, TU } };
374 return C;
375}
376
377MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
378 assert(C.kind == CXCursor_MacroDefinition);
379 return static_cast<MacroDefinition *>(C.data[0]);
380}
381
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000382CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
383 CXTranslationUnit TU) {
Chandler Carruth9b2a0ac2011-07-14 08:41:15 +0000384 CXCursor C = { CXCursor_MacroExpansion, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000385 return C;
386}
387
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000388MacroExpansion *cxcursor::getCursorMacroExpansion(CXCursor C) {
Chandler Carruth9b2a0ac2011-07-14 08:41:15 +0000389 assert(C.kind == CXCursor_MacroExpansion);
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000390 return static_cast<MacroExpansion *>(C.data[0]);
Douglas Gregor48072312010-03-18 15:23:44 +0000391}
392
Douglas Gregorecdcb882010-10-20 22:00:55 +0000393CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000394 CXTranslationUnit TU) {
Douglas Gregorecdcb882010-10-20 22:00:55 +0000395 CXCursor C = { CXCursor_InclusionDirective, { ID, 0, TU } };
396 return C;
397}
398
399InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
400 assert(C.kind == CXCursor_InclusionDirective);
401 return static_cast<InclusionDirective *>(C.data[0]);
402}
403
Douglas Gregor36897b02010-09-10 00:22:18 +0000404CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000405 CXTranslationUnit TU) {
Douglas Gregor36897b02010-09-10 00:22:18 +0000406
407 assert(Label && TU && "Invalid arguments!");
408 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
409 CXCursor C = { CXCursor_LabelRef, { Label, RawLoc, TU } };
410 return C;
411}
412
413std::pair<LabelStmt*, SourceLocation>
414cxcursor::getCursorLabelRef(CXCursor C) {
415 assert(C.kind == CXCursor_LabelRef);
416 return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
417 SourceLocation::getFromRawEncoding(
418 reinterpret_cast<uintptr_t>(C.data[1])));
419}
420
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000421CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000422 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000423 assert(E && TU && "Invalid arguments!");
424 OverloadedDeclRefStorage Storage(E);
425 void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding());
426 CXCursor C = {
427 CXCursor_OverloadedDeclRef,
428 { Storage.getOpaqueValue(), RawLoc, TU }
429 };
430 return C;
431}
432
433CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D,
434 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000435 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000436 assert(D && TU && "Invalid arguments!");
437 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
438 OverloadedDeclRefStorage Storage(D);
439 CXCursor C = {
440 CXCursor_OverloadedDeclRef,
441 { Storage.getOpaqueValue(), RawLoc, TU }
442 };
443 return C;
444}
445
446CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
447 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000448 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000449 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
450 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
451 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
452 CXCursor C = {
453 CXCursor_OverloadedDeclRef,
454 { Storage.getOpaqueValue(), RawLoc, TU }
455 };
456 return C;
457}
458
459std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
460cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
461 assert(C.kind == CXCursor_OverloadedDeclRef);
462 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]),
463 SourceLocation::getFromRawEncoding(
464 reinterpret_cast<uintptr_t>(C.data[1])));
465}
466
Douglas Gregor283cae32010-01-15 21:56:13 +0000467Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
468 return (Decl *)Cursor.data[0];
469}
470
471Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
472 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
473}
474
475Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000476 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000477 Cursor.kind == CXCursor_ObjCProtocolRef ||
478 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000479 return 0;
480
Douglas Gregor283cae32010-01-15 21:56:13 +0000481 return (Stmt *)Cursor.data[1];
482}
483
Ted Kremenek95f33552010-08-26 01:42:22 +0000484Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
485 return (Attr *)Cursor.data[1];
486}
487
Argyrios Kyrtzidis8ccac3d2011-06-29 22:20:07 +0000488Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
489 return (Decl *)Cursor.data[0];
490}
491
Douglas Gregorf46034a2010-01-18 23:41:10 +0000492ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000493 return getCursorASTUnit(Cursor)->getASTContext();
494}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000495
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000496ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Ted Kremeneka60ed472010-11-16 08:15:36 +0000497 return static_cast<ASTUnit *>(static_cast<CXTranslationUnit>(Cursor.data[2])
498 ->TUData);
499}
500
501CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
502 return static_cast<CXTranslationUnit>(Cursor.data[2]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000503}
504
Douglas Gregor283cae32010-01-15 21:56:13 +0000505bool cxcursor::operator==(CXCursor X, CXCursor Y) {
506 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
507 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000508}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000509
510// FIXME: Remove once we can model DeclGroups and their appropriate ranges
511// properly in the ASTs.
512bool cxcursor::isFirstInDeclGroup(CXCursor C) {
513 assert(clang_isDeclaration(C.kind));
514 return ((uintptr_t) (C.data[1])) != 0;
515}
516
Ted Kremenekeca099b2010-12-08 23:43:14 +0000517//===----------------------------------------------------------------------===//
518// CXCursorSet.
519//===----------------------------------------------------------------------===//
520
521typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
522
523static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
524 return (CXCursorSet) setImpl;
525}
526static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
527 return (CXCursorSet_Impl*) set;
528}
529namespace llvm {
Ted Kremenekda6fb692010-12-09 00:33:41 +0000530template<> struct DenseMapInfo<CXCursor> {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000531public:
532 static inline CXCursor getEmptyKey() {
533 return MakeCXCursorInvalid(CXCursor_InvalidFile);
534 }
535 static inline CXCursor getTombstoneKey() {
536 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
537 }
538 static inline unsigned getHashValue(const CXCursor &cursor) {
539 return llvm::DenseMapInfo<std::pair<void*,void*> >
540 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
541 }
542 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
543 return x.kind == y.kind &&
544 x.data[0] == y.data[0] &&
545 x.data[1] == y.data[1];
546 }
547};
548}
549
550extern "C" {
551CXCursorSet clang_createCXCursorSet() {
552 return packCXCursorSet(new CXCursorSet_Impl());
553}
554
555void clang_disposeCXCursorSet(CXCursorSet set) {
556 delete unpackCXCursorSet(set);
557}
558
559unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
560 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
561 if (!setImpl)
562 return 0;
563 return setImpl->find(cursor) == setImpl->end();
564}
565
Anders Carlssone8b3de02010-12-09 01:00:12 +0000566unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000567 // Do not insert invalid cursors into the set.
568 if (cursor.kind >= CXCursor_FirstInvalid &&
569 cursor.kind <= CXCursor_LastInvalid)
570 return 1;
571
572 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
573 if (!setImpl)
574 return 1;
575 unsigned &entry = (*setImpl)[cursor];
576 unsigned flag = entry == 0 ? 1 : 0;
577 entry = 1;
578 return flag;
579}
Douglas Gregor8fa0a802011-08-04 20:04:59 +0000580
581CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
582 enum CXCursorKind kind = clang_getCursorKind(cursor);
583 if (clang_isDeclaration(kind)) {
584 Decl *decl = getCursorDecl(cursor);
585 if (isa<NamedDecl>(decl)) {
586 NamedDecl *namedDecl = (NamedDecl *)decl;
587 ASTUnit *unit = getCursorASTUnit(cursor);
588 if (unit->hasSema()) {
589 Sema &S = unit->getSema();
590 CodeCompletionAllocator *Allocator
591 = unit->getCursorCompletionAllocator().getPtr();
592 CodeCompletionResult Result(namedDecl);
593 CodeCompletionString *String
594 = Result.CreateCodeCompletionString(S, *Allocator);
595 return String;
596 }
597 }
598 }
599 else if (kind == CXCursor_MacroDefinition) {
600 MacroDefinition *definition = getCursorMacroDefinition(cursor);
601 const IdentifierInfo *MacroInfo = definition->getName();
602 ASTUnit *unit = getCursorASTUnit(cursor);
603 if (unit->hasSema()) {
604 Sema &S = unit->getSema();
605 CodeCompletionAllocator *Allocator
606 = unit->getCursorCompletionAllocator().getPtr();
Douglas Gregoreaf4fba2011-08-10 16:34:38 +0000607 CodeCompletionResult Result(const_cast<IdentifierInfo *>(MacroInfo));
Douglas Gregor8fa0a802011-08-04 20:04:59 +0000608 CodeCompletionString *String
609 = Result.CreateCodeCompletionString(S, *Allocator);
610 return String;
611 }
612 }
613 return NULL;
614}
615
Ted Kremenekeca099b2010-12-08 23:43:14 +0000616} // end: extern "C"