Bug Summary

File:tools/lldb/source/Symbol/ClangASTContext.cpp
Warning:line 7935, column 20
Array access (from variable 'chain') results in a null pointer dereference

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ClangASTContext.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-8/lib/clang/8.0.0 -D HAVE_ROUND -D LLDB_CONFIGURATION_RELEASE -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/tools/lldb/source/Symbol -I /build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/Symbol -I /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/tools/lldb/include -I /build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/include -I /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/include -I /build/llvm-toolchain-snapshot-8~svn345461/include -I /usr/include/python2.7 -I /build/llvm-toolchain-snapshot-8~svn345461/tools/clang/include -I /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/tools/lldb/../clang/include -I /build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/. -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/include/clang/8.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-8/lib/clang/8.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -Wno-deprecated-declarations -Wno-unknown-pragmas -Wno-strict-aliasing -Wno-deprecated-register -Wno-vla-extension -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/tools/lldb/source/Symbol -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-10-27-211344-32123-1 -x c++ /build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/Symbol/ClangASTContext.cpp -faddrsig
1//===-- ClangASTContext.cpp -------------------------------------*- C++ -*-===//
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
10#include "lldb/Symbol/ClangASTContext.h"
11
12#include "llvm/Support/FormatAdapters.h"
13#include "llvm/Support/FormatVariadic.h"
14
15// C Includes
16// C++ Includes
17#include <mutex>
18#include <string>
19#include <vector>
20
21// Other libraries and framework includes
22
23// Clang headers like to use NDEBUG inside of them to enable/disable debug
24// related features using "#ifndef NDEBUG" preprocessor blocks to do one thing
25// or another. This is bad because it means that if clang was built in release
26// mode, it assumes that you are building in release mode which is not always
27// the case. You can end up with functions that are defined as empty in header
28// files when NDEBUG is not defined, and this can cause link errors with the
29// clang .a files that you have since you might be missing functions in the .a
30// file. So we have to define NDEBUG when including clang headers to avoid any
31// mismatches. This is covered by rdar://problem/8691220
32
33#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
34#define LLDB_DEFINED_NDEBUG_FOR_CLANG
35#define NDEBUG
36// Need to include assert.h so it is as clang would expect it to be (disabled)
37#include <assert.h>
38#endif
39
40#include "clang/AST/ASTContext.h"
41#include "clang/AST/ASTImporter.h"
42#include "clang/AST/Attr.h"
43#include "clang/AST/CXXInheritance.h"
44#include "clang/AST/DeclObjC.h"
45#include "clang/AST/DeclTemplate.h"
46#include "clang/AST/Mangle.h"
47#include "clang/AST/RecordLayout.h"
48#include "clang/AST/Type.h"
49#include "clang/AST/VTableBuilder.h"
50#include "clang/Basic/Builtins.h"
51#include "clang/Basic/Diagnostic.h"
52#include "clang/Basic/FileManager.h"
53#include "clang/Basic/FileSystemOptions.h"
54#include "clang/Basic/SourceManager.h"
55#include "clang/Basic/TargetInfo.h"
56#include "clang/Basic/TargetOptions.h"
57#include "clang/Frontend/FrontendOptions.h"
58#include "clang/Frontend/LangStandard.h"
59
60#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
61#undef NDEBUG
62#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
63// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
64#include <assert.h>
65#endif
66
67#include "llvm/Support/Signals.h"
68#include "llvm/Support/Threading.h"
69
70#include "Plugins/ExpressionParser/Clang/ClangFunctionCaller.h"
71#include "Plugins/ExpressionParser/Clang/ClangUserExpression.h"
72#include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h"
73#include "lldb/Utility/ArchSpec.h"
74#include "lldb/Utility/Flags.h"
75
76#include "lldb/Core/DumpDataExtractor.h"
77#include "lldb/Core/Module.h"
78#include "lldb/Core/PluginManager.h"
79#include "lldb/Core/StreamFile.h"
80#include "lldb/Core/ThreadSafeDenseMap.h"
81#include "lldb/Core/UniqueCStringMap.h"
82#include "lldb/Symbol/ClangASTContext.h"
83#include "lldb/Symbol/ClangASTImporter.h"
84#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
85#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
86#include "lldb/Symbol/ClangUtil.h"
87#include "lldb/Symbol/ObjectFile.h"
88#include "lldb/Symbol/SymbolFile.h"
89#include "lldb/Symbol/VerifyDecl.h"
90#include "lldb/Target/ExecutionContext.h"
91#include "lldb/Target/Language.h"
92#include "lldb/Target/ObjCLanguageRuntime.h"
93#include "lldb/Target/Process.h"
94#include "lldb/Target/Target.h"
95#include "lldb/Utility/DataExtractor.h"
96#include "lldb/Utility/LLDBAssert.h"
97#include "lldb/Utility/Log.h"
98#include "lldb/Utility/RegularExpression.h"
99#include "lldb/Utility/Scalar.h"
100
101#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
102#include "Plugins/SymbolFile/PDB/PDBASTParser.h"
103
104#include <stdio.h>
105
106#include <mutex>
107
108using namespace lldb;
109using namespace lldb_private;
110using namespace llvm;
111using namespace clang;
112
113namespace {
114static inline bool
115ClangASTContextSupportsLanguage(lldb::LanguageType language) {
116 return language == eLanguageTypeUnknown || // Clang is the default type system
117 Language::LanguageIsC(language) ||
118 Language::LanguageIsCPlusPlus(language) ||
119 Language::LanguageIsObjC(language) ||
120 Language::LanguageIsPascal(language) ||
121 // Use Clang for Rust until there is a proper language plugin for it
122 language == eLanguageTypeRust ||
123 language == eLanguageTypeExtRenderScript ||
124 // Use Clang for D until there is a proper language plugin for it
125 language == eLanguageTypeD;
126}
127
128// Checks whether m1 is an overload of m2 (as opposed to an override). This is
129// called by addOverridesForMethod to distinguish overrides (which share a
130// vtable entry) from overloads (which require distinct entries).
131bool isOverload(clang::CXXMethodDecl *m1, clang::CXXMethodDecl *m2) {
132 // FIXME: This should detect covariant return types, but currently doesn't.
133 lldbassert(&m1->getASTContext() == &m2->getASTContext() &&lldb_private::lldb_assert(static_cast<bool>(&m1->
getASTContext() == &m2->getASTContext() && "Methods should have the same AST context"
), "&m1->getASTContext() == &m2->getASTContext() && \"Methods should have the same AST context\""
, __FUNCTION__, "/build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 134)
134 "Methods should have the same AST context")lldb_private::lldb_assert(static_cast<bool>(&m1->
getASTContext() == &m2->getASTContext() && "Methods should have the same AST context"
), "&m1->getASTContext() == &m2->getASTContext() && \"Methods should have the same AST context\""
, __FUNCTION__, "/build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 134)
;
135 clang::ASTContext &context = m1->getASTContext();
136
137 const auto *m1Type = llvm::cast<clang::FunctionProtoType>(
138 context.getCanonicalType(m1->getType()));
139
140 const auto *m2Type = llvm::cast<clang::FunctionProtoType>(
141 context.getCanonicalType(m2->getType()));
142
143 auto compareArgTypes = [&context](const clang::QualType &m1p,
144 const clang::QualType &m2p) {
145 return context.hasSameType(m1p.getUnqualifiedType(),
146 m2p.getUnqualifiedType());
147 };
148
149 // FIXME: In C++14 and later, we can just pass m2Type->param_type_end()
150 // as a fourth parameter to std::equal().
151 return (m1->getNumParams() != m2->getNumParams()) ||
152 !std::equal(m1Type->param_type_begin(), m1Type->param_type_end(),
153 m2Type->param_type_begin(), compareArgTypes);
154}
155
156// If decl is a virtual method, walk the base classes looking for methods that
157// decl overrides. This table of overridden methods is used by IRGen to
158// determine the vtable layout for decl's parent class.
159void addOverridesForMethod(clang::CXXMethodDecl *decl) {
160 if (!decl->isVirtual())
161 return;
162
163 clang::CXXBasePaths paths;
164
165 auto find_overridden_methods =
166 [decl](const clang::CXXBaseSpecifier *specifier,
167 clang::CXXBasePath &path) {
168 if (auto *base_record = llvm::dyn_cast<clang::CXXRecordDecl>(
169 specifier->getType()->getAs<clang::RecordType>()->getDecl())) {
170
171 clang::DeclarationName name = decl->getDeclName();
172
173 // If this is a destructor, check whether the base class destructor is
174 // virtual.
175 if (name.getNameKind() == clang::DeclarationName::CXXDestructorName)
176 if (auto *baseDtorDecl = base_record->getDestructor()) {
177 if (baseDtorDecl->isVirtual()) {
178 path.Decls = baseDtorDecl;
179 return true;
180 } else
181 return false;
182 }
183
184 // Otherwise, search for name in the base class.
185 for (path.Decls = base_record->lookup(name); !path.Decls.empty();
186 path.Decls = path.Decls.slice(1)) {
187 if (auto *method_decl =
188 llvm::dyn_cast<clang::CXXMethodDecl>(path.Decls.front()))
189 if (method_decl->isVirtual() && !isOverload(decl, method_decl)) {
190 path.Decls = method_decl;
191 return true;
192 }
193 }
194 }
195
196 return false;
197 };
198
199 if (decl->getParent()->lookupInBases(find_overridden_methods, paths)) {
200 for (auto *overridden_decl : paths.found_decls())
201 decl->addOverriddenMethod(
202 llvm::cast<clang::CXXMethodDecl>(overridden_decl));
203 }
204}
205}
206
207typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext *>
208 ClangASTMap;
209
210static ClangASTMap &GetASTMap() {
211 static ClangASTMap *g_map_ptr = nullptr;
212 static llvm::once_flag g_once_flag;
213 llvm::call_once(g_once_flag, []() {
214 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
215 });
216 return *g_map_ptr;
217}
218
219bool ClangASTContext::IsOperator(const char *name,
220 clang::OverloadedOperatorKind &op_kind) {
221 if (name == nullptr || name[0] == '\0')
222 return false;
223
224#define OPERATOR_PREFIX "operator"
225#define OPERATOR_PREFIX_LENGTH (sizeof(OPERATOR_PREFIX) - 1)
226
227 const char *post_op_name = nullptr;
228
229 bool no_space = true;
230
231 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
232 return false;
233
234 post_op_name = name + OPERATOR_PREFIX_LENGTH;
235
236 if (post_op_name[0] == ' ') {
237 post_op_name++;
238 no_space = false;
239 }
240
241#undef OPERATOR_PREFIX
242#undef OPERATOR_PREFIX_LENGTH
243
244 // This is an operator, set the overloaded operator kind to invalid in case
245 // this is a conversion operator...
246 op_kind = clang::NUM_OVERLOADED_OPERATORS;
247
248 switch (post_op_name[0]) {
249 default:
250 if (no_space)
251 return false;
252 break;
253 case 'n':
254 if (no_space)
255 return false;
256 if (strcmp(post_op_name, "new") == 0)
257 op_kind = clang::OO_New;
258 else if (strcmp(post_op_name, "new[]") == 0)
259 op_kind = clang::OO_Array_New;
260 break;
261
262 case 'd':
263 if (no_space)
264 return false;
265 if (strcmp(post_op_name, "delete") == 0)
266 op_kind = clang::OO_Delete;
267 else if (strcmp(post_op_name, "delete[]") == 0)
268 op_kind = clang::OO_Array_Delete;
269 break;
270
271 case '+':
272 if (post_op_name[1] == '\0')
273 op_kind = clang::OO_Plus;
274 else if (post_op_name[2] == '\0') {
275 if (post_op_name[1] == '=')
276 op_kind = clang::OO_PlusEqual;
277 else if (post_op_name[1] == '+')
278 op_kind = clang::OO_PlusPlus;
279 }
280 break;
281
282 case '-':
283 if (post_op_name[1] == '\0')
284 op_kind = clang::OO_Minus;
285 else if (post_op_name[2] == '\0') {
286 switch (post_op_name[1]) {
287 case '=':
288 op_kind = clang::OO_MinusEqual;
289 break;
290 case '-':
291 op_kind = clang::OO_MinusMinus;
292 break;
293 case '>':
294 op_kind = clang::OO_Arrow;
295 break;
296 }
297 } else if (post_op_name[3] == '\0') {
298 if (post_op_name[2] == '*')
299 op_kind = clang::OO_ArrowStar;
300 break;
301 }
302 break;
303
304 case '*':
305 if (post_op_name[1] == '\0')
306 op_kind = clang::OO_Star;
307 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
308 op_kind = clang::OO_StarEqual;
309 break;
310
311 case '/':
312 if (post_op_name[1] == '\0')
313 op_kind = clang::OO_Slash;
314 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
315 op_kind = clang::OO_SlashEqual;
316 break;
317
318 case '%':
319 if (post_op_name[1] == '\0')
320 op_kind = clang::OO_Percent;
321 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
322 op_kind = clang::OO_PercentEqual;
323 break;
324
325 case '^':
326 if (post_op_name[1] == '\0')
327 op_kind = clang::OO_Caret;
328 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
329 op_kind = clang::OO_CaretEqual;
330 break;
331
332 case '&':
333 if (post_op_name[1] == '\0')
334 op_kind = clang::OO_Amp;
335 else if (post_op_name[2] == '\0') {
336 switch (post_op_name[1]) {
337 case '=':
338 op_kind = clang::OO_AmpEqual;
339 break;
340 case '&':
341 op_kind = clang::OO_AmpAmp;
342 break;
343 }
344 }
345 break;
346
347 case '|':
348 if (post_op_name[1] == '\0')
349 op_kind = clang::OO_Pipe;
350 else if (post_op_name[2] == '\0') {
351 switch (post_op_name[1]) {
352 case '=':
353 op_kind = clang::OO_PipeEqual;
354 break;
355 case '|':
356 op_kind = clang::OO_PipePipe;
357 break;
358 }
359 }
360 break;
361
362 case '~':
363 if (post_op_name[1] == '\0')
364 op_kind = clang::OO_Tilde;
365 break;
366
367 case '!':
368 if (post_op_name[1] == '\0')
369 op_kind = clang::OO_Exclaim;
370 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
371 op_kind = clang::OO_ExclaimEqual;
372 break;
373
374 case '=':
375 if (post_op_name[1] == '\0')
376 op_kind = clang::OO_Equal;
377 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
378 op_kind = clang::OO_EqualEqual;
379 break;
380
381 case '<':
382 if (post_op_name[1] == '\0')
383 op_kind = clang::OO_Less;
384 else if (post_op_name[2] == '\0') {
385 switch (post_op_name[1]) {
386 case '<':
387 op_kind = clang::OO_LessLess;
388 break;
389 case '=':
390 op_kind = clang::OO_LessEqual;
391 break;
392 }
393 } else if (post_op_name[3] == '\0') {
394 if (post_op_name[2] == '=')
395 op_kind = clang::OO_LessLessEqual;
396 }
397 break;
398
399 case '>':
400 if (post_op_name[1] == '\0')
401 op_kind = clang::OO_Greater;
402 else if (post_op_name[2] == '\0') {
403 switch (post_op_name[1]) {
404 case '>':
405 op_kind = clang::OO_GreaterGreater;
406 break;
407 case '=':
408 op_kind = clang::OO_GreaterEqual;
409 break;
410 }
411 } else if (post_op_name[1] == '>' && post_op_name[2] == '=' &&
412 post_op_name[3] == '\0') {
413 op_kind = clang::OO_GreaterGreaterEqual;
414 }
415 break;
416
417 case ',':
418 if (post_op_name[1] == '\0')
419 op_kind = clang::OO_Comma;
420 break;
421
422 case '(':
423 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
424 op_kind = clang::OO_Call;
425 break;
426
427 case '[':
428 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
429 op_kind = clang::OO_Subscript;
430 break;
431 }
432
433 return true;
434}
435
436clang::AccessSpecifier
437ClangASTContext::ConvertAccessTypeToAccessSpecifier(AccessType access) {
438 switch (access) {
439 default:
440 break;
441 case eAccessNone:
442 return AS_none;
443 case eAccessPublic:
444 return AS_public;
445 case eAccessPrivate:
446 return AS_private;
447 case eAccessProtected:
448 return AS_protected;
449 }
450 return AS_none;
451}
452
453static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) {
454 // FIXME: Cleanup per-file based stuff.
455
456 // Set some properties which depend solely on the input kind; it would be
457 // nice to move these to the language standard, and have the driver resolve
458 // the input kind + language standard.
459 if (IK.getLanguage() == InputKind::Asm) {
460 Opts.AsmPreprocessor = 1;
461 } else if (IK.isObjectiveC()) {
462 Opts.ObjC1 = Opts.ObjC2 = 1;
463 }
464
465 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
466
467 if (LangStd == LangStandard::lang_unspecified) {
468 // Based on the base language, pick one.
469 switch (IK.getLanguage()) {
470 case InputKind::Unknown:
471 case InputKind::LLVM_IR:
472 case InputKind::RenderScript:
473 llvm_unreachable("Invalid input kind!")::llvm::llvm_unreachable_internal("Invalid input kind!", "/build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 473)
;
474 case InputKind::OpenCL:
475 LangStd = LangStandard::lang_opencl10;
476 break;
477 case InputKind::CUDA:
478 LangStd = LangStandard::lang_cuda;
479 break;
480 case InputKind::Asm:
481 case InputKind::C:
482 case InputKind::ObjC:
483 LangStd = LangStandard::lang_gnu99;
484 break;
485 case InputKind::CXX:
486 case InputKind::ObjCXX:
487 LangStd = LangStandard::lang_gnucxx98;
488 break;
489 case InputKind::HIP:
490 LangStd = LangStandard::lang_hip;
491 break;
492 }
493 }
494
495 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
496 Opts.LineComment = Std.hasLineComments();
497 Opts.C99 = Std.isC99();
498 Opts.CPlusPlus = Std.isCPlusPlus();
499 Opts.CPlusPlus11 = Std.isCPlusPlus11();
500 Opts.Digraphs = Std.hasDigraphs();
501 Opts.GNUMode = Std.isGNUMode();
502 Opts.GNUInline = !Std.isC99();
503 Opts.HexFloats = Std.hasHexFloats();
504 Opts.ImplicitInt = Std.hasImplicitInt();
505
506 Opts.WChar = true;
507
508 // OpenCL has some additional defaults.
509 if (LangStd == LangStandard::lang_opencl10) {
510 Opts.OpenCL = 1;
511 Opts.AltiVec = 1;
512 Opts.CXXOperatorNames = 1;
513 Opts.LaxVectorConversions = 1;
514 }
515
516 // OpenCL and C++ both have bool, true, false keywords.
517 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
518
519 Opts.setValueVisibilityMode(DefaultVisibility);
520
521 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs is
522 // specified, or -std is set to a conforming mode.
523 Opts.Trigraphs = !Opts.GNUMode;
524 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
525 Opts.OptimizeSize = 0;
526
527 // FIXME: Eliminate this dependency.
528 // unsigned Opt =
529 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
530 // Opts.Optimize = Opt != 0;
531 unsigned Opt = 0;
532
533 // This is the __NO_INLINE__ define, which just depends on things like the
534 // optimization level and -fno-inline, not actually whether the backend has
535 // inlining enabled.
536 //
537 // FIXME: This is affected by other options (-fno-inline).
538 Opts.NoInlineDefine = !Opt;
539}
540
541ClangASTContext::ClangASTContext(const char *target_triple)
542 : TypeSystem(TypeSystem::eKindClang), m_target_triple(), m_ast_ap(),
543 m_language_options_ap(), m_source_manager_ap(), m_diagnostics_engine_ap(),
544 m_target_options_rp(), m_target_info_ap(), m_identifier_table_ap(),
545 m_selector_table_ap(), m_builtins_ap(), m_callback_tag_decl(nullptr),
546 m_callback_objc_decl(nullptr), m_callback_baton(nullptr),
547 m_pointer_byte_size(0), m_ast_owned(false) {
548 if (target_triple && target_triple[0])
549 SetTargetTriple(target_triple);
550}
551
552//----------------------------------------------------------------------
553// Destructor
554//----------------------------------------------------------------------
555ClangASTContext::~ClangASTContext() { Finalize(); }
556
557ConstString ClangASTContext::GetPluginNameStatic() {
558 return ConstString("clang");
559}
560
561ConstString ClangASTContext::GetPluginName() {
562 return ClangASTContext::GetPluginNameStatic();
563}
564
565uint32_t ClangASTContext::GetPluginVersion() { return 1; }
566
567lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
568 lldb_private::Module *module,
569 Target *target) {
570 if (ClangASTContextSupportsLanguage(language)) {
571 ArchSpec arch;
572 if (module)
573 arch = module->GetArchitecture();
574 else if (target)
575 arch = target->GetArchitecture();
576
577 if (arch.IsValid()) {
578 ArchSpec fixed_arch = arch;
579 // LLVM wants this to be set to iOS or MacOSX; if we're working on
580 // a bare-boards type image, change the triple for llvm's benefit.
581 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
582 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) {
583 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
584 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
585 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) {
586 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
587 } else {
588 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
589 }
590 }
591
592 if (module) {
593 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
594 if (ast_sp) {
595 ast_sp->SetArchitecture(fixed_arch);
596 }
597 return ast_sp;
598 } else if (target && target->IsValid()) {
599 std::shared_ptr<ClangASTContextForExpressions> ast_sp(
600 new ClangASTContextForExpressions(*target));
601 if (ast_sp) {
602 ast_sp->SetArchitecture(fixed_arch);
603 ast_sp->m_scratch_ast_source_ap.reset(
604 new ClangASTSource(target->shared_from_this()));
605 lldbassert(ast_sp->getFileManager())lldb_private::lldb_assert(static_cast<bool>(ast_sp->
getFileManager()), "ast_sp->getFileManager()", __FUNCTION__
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 605)
;
606 ast_sp->m_scratch_ast_source_ap->InstallASTContext(
607 *ast_sp->getASTContext(), *ast_sp->getFileManager(), true);
608 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
609 ast_sp->m_scratch_ast_source_ap->CreateProxy());
610 ast_sp->SetExternalSource(proxy_ast_source);
611 return ast_sp;
612 }
613 }
614 }
615 }
616 return lldb::TypeSystemSP();
617}
618
619void ClangASTContext::EnumerateSupportedLanguages(
620 std::set<lldb::LanguageType> &languages_for_types,
621 std::set<lldb::LanguageType> &languages_for_expressions) {
622 static std::vector<lldb::LanguageType> s_supported_languages_for_types(
623 {lldb::eLanguageTypeC89, lldb::eLanguageTypeC, lldb::eLanguageTypeC11,
624 lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeC99,
625 lldb::eLanguageTypeObjC, lldb::eLanguageTypeObjC_plus_plus,
626 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
627 lldb::eLanguageTypeC11, lldb::eLanguageTypeC_plus_plus_14});
628
629 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions(
630 {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC_plus_plus,
631 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
632 lldb::eLanguageTypeC_plus_plus_14});
633
634 languages_for_types.insert(s_supported_languages_for_types.begin(),
635 s_supported_languages_for_types.end());
636 languages_for_expressions.insert(
637 s_supported_languages_for_expressions.begin(),
638 s_supported_languages_for_expressions.end());
639}
640
641void ClangASTContext::Initialize() {
642 PluginManager::RegisterPlugin(GetPluginNameStatic(),
643 "clang base AST context plug-in",
644 CreateInstance, EnumerateSupportedLanguages);
645}
646
647void ClangASTContext::Terminate() {
648 PluginManager::UnregisterPlugin(CreateInstance);
649}
650
651void ClangASTContext::Finalize() {
652 if (m_ast_ap.get()) {
653 GetASTMap().Erase(m_ast_ap.get());
654 if (!m_ast_owned)
655 m_ast_ap.release();
656 }
657
658 m_builtins_ap.reset();
659 m_selector_table_ap.reset();
660 m_identifier_table_ap.reset();
661 m_target_info_ap.reset();
662 m_target_options_rp.reset();
663 m_diagnostics_engine_ap.reset();
664 m_source_manager_ap.reset();
665 m_language_options_ap.reset();
666 m_ast_ap.reset();
667 m_scratch_ast_source_ap.reset();
668}
669
670void ClangASTContext::Clear() {
671 m_ast_ap.reset();
672 m_language_options_ap.reset();
673 m_source_manager_ap.reset();
674 m_diagnostics_engine_ap.reset();
675 m_target_options_rp.reset();
676 m_target_info_ap.reset();
677 m_identifier_table_ap.reset();
678 m_selector_table_ap.reset();
679 m_builtins_ap.reset();
680 m_pointer_byte_size = 0;
681}
682
683const char *ClangASTContext::GetTargetTriple() {
684 return m_target_triple.c_str();
685}
686
687void ClangASTContext::SetTargetTriple(const char *target_triple) {
688 Clear();
689 m_target_triple.assign(target_triple);
690}
691
692void ClangASTContext::SetArchitecture(const ArchSpec &arch) {
693 SetTargetTriple(arch.GetTriple().str().c_str());
694}
695
696bool ClangASTContext::HasExternalSource() {
697 ASTContext *ast = getASTContext();
698 if (ast)
699 return ast->getExternalSource() != nullptr;
700 return false;
701}
702
703void ClangASTContext::SetExternalSource(
704 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap) {
705 ASTContext *ast = getASTContext();
706 if (ast) {
707 ast->setExternalSource(ast_source_ap);
708 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
709 }
710}
711
712void ClangASTContext::RemoveExternalSource() {
713 ASTContext *ast = getASTContext();
714
715 if (ast) {
716 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
717 ast->setExternalSource(empty_ast_source_ap);
718 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
719 }
720}
721
722void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) {
723 if (!m_ast_owned) {
724 m_ast_ap.release();
725 }
726 m_ast_owned = false;
727 m_ast_ap.reset(ast_ctx);
728 GetASTMap().Insert(ast_ctx, this);
729}
730
731ASTContext *ClangASTContext::getASTContext() {
732 if (m_ast_ap.get() == nullptr) {
733 m_ast_owned = true;
734 m_ast_ap.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
735 *getIdentifierTable(), *getSelectorTable(),
736 *getBuiltinContext()));
737
738 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
739
740 // This can be NULL if we don't know anything about the architecture or if
741 // the target for an architecture isn't enabled in the llvm/clang that we
742 // built
743 TargetInfo *target_info = getTargetInfo();
744 if (target_info)
745 m_ast_ap->InitBuiltinTypes(*target_info);
746
747 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) {
748 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
749 // m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
750 }
751
752 GetASTMap().Insert(m_ast_ap.get(), this);
753
754 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap(
755 new ClangExternalASTSourceCallbacks(
756 ClangASTContext::CompleteTagDecl,
757 ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
758 ClangASTContext::LayoutRecordType, this));
759 SetExternalSource(ast_source_ap);
760 }
761 return m_ast_ap.get();
762}
763
764ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
765 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
766 return clang_ast;
767}
768
769Builtin::Context *ClangASTContext::getBuiltinContext() {
770 if (m_builtins_ap.get() == nullptr)
771 m_builtins_ap.reset(new Builtin::Context());
772 return m_builtins_ap.get();
773}
774
775IdentifierTable *ClangASTContext::getIdentifierTable() {
776 if (m_identifier_table_ap.get() == nullptr)
777 m_identifier_table_ap.reset(
778 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
779 return m_identifier_table_ap.get();
780}
781
782LangOptions *ClangASTContext::getLanguageOptions() {
783 if (m_language_options_ap.get() == nullptr) {
784 m_language_options_ap.reset(new LangOptions());
785 ParseLangArgs(*m_language_options_ap, InputKind::ObjCXX, GetTargetTriple());
786 // InitializeLangOptions(*m_language_options_ap, InputKind::ObjCXX);
787 }
788 return m_language_options_ap.get();
789}
790
791SelectorTable *ClangASTContext::getSelectorTable() {
792 if (m_selector_table_ap.get() == nullptr)
793 m_selector_table_ap.reset(new SelectorTable());
794 return m_selector_table_ap.get();
795}
796
797clang::FileManager *ClangASTContext::getFileManager() {
798 if (m_file_manager_ap.get() == nullptr) {
799 clang::FileSystemOptions file_system_options;
800 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
801 }
802 return m_file_manager_ap.get();
803}
804
805clang::SourceManager *ClangASTContext::getSourceManager() {
806 if (m_source_manager_ap.get() == nullptr)
807 m_source_manager_ap.reset(
808 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
809 return m_source_manager_ap.get();
810}
811
812clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() {
813 if (m_diagnostics_engine_ap.get() == nullptr) {
814 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
815 m_diagnostics_engine_ap.reset(
816 new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
817 }
818 return m_diagnostics_engine_ap.get();
819}
820
821clang::MangleContext *ClangASTContext::getMangleContext() {
822 if (m_mangle_ctx_ap.get() == nullptr)
823 m_mangle_ctx_ap.reset(getASTContext()->createMangleContext());
824 return m_mangle_ctx_ap.get();
825}
826
827class NullDiagnosticConsumer : public DiagnosticConsumer {
828public:
829 NullDiagnosticConsumer() {
830 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS(1u << 8));
831 }
832
833 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
834 const clang::Diagnostic &info) {
835 if (m_log) {
836 llvm::SmallVector<char, 32> diag_str(10);
837 info.FormatDiagnostic(diag_str);
838 diag_str.push_back('\0');
839 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
840 }
841 }
842
843 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
844 return new NullDiagnosticConsumer();
845 }
846
847private:
848 Log *m_log;
849};
850
851DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() {
852 if (m_diagnostic_consumer_ap.get() == nullptr)
853 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
854
855 return m_diagnostic_consumer_ap.get();
856}
857
858std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
859 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty()) {
860 m_target_options_rp = std::make_shared<clang::TargetOptions>();
861 if (m_target_options_rp.get() != nullptr)
862 m_target_options_rp->Triple = m_target_triple;
863 }
864 return m_target_options_rp;
865}
866
867TargetInfo *ClangASTContext::getTargetInfo() {
868 // target_triple should be something like "x86_64-apple-macosx"
869 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty())
870 m_target_info_ap.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
871 getTargetOptions()));
872 return m_target_info_ap.get();
873}
874
875#pragma mark Basic Types
876
877static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
878 ASTContext *ast, QualType qual_type) {
879 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
880 if (qual_type_bit_size == bit_size)
881 return true;
882 return false;
883}
884
885CompilerType
886ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
887 size_t bit_size) {
888 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
889 getASTContext(), encoding, bit_size);
890}
891
892CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
893 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
894 if (!ast)
895 return CompilerType();
896 switch (encoding) {
897 case eEncodingInvalid:
898 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
899 return CompilerType(ast, ast->VoidPtrTy);
900 break;
901
902 case eEncodingUint:
903 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
904 return CompilerType(ast, ast->UnsignedCharTy);
905 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
906 return CompilerType(ast, ast->UnsignedShortTy);
907 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
908 return CompilerType(ast, ast->UnsignedIntTy);
909 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
910 return CompilerType(ast, ast->UnsignedLongTy);
911 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
912 return CompilerType(ast, ast->UnsignedLongLongTy);
913 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
914 return CompilerType(ast, ast->UnsignedInt128Ty);
915 break;
916
917 case eEncodingSint:
918 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
919 return CompilerType(ast, ast->SignedCharTy);
920 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
921 return CompilerType(ast, ast->ShortTy);
922 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
923 return CompilerType(ast, ast->IntTy);
924 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
925 return CompilerType(ast, ast->LongTy);
926 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
927 return CompilerType(ast, ast->LongLongTy);
928 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
929 return CompilerType(ast, ast->Int128Ty);
930 break;
931
932 case eEncodingIEEE754:
933 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
934 return CompilerType(ast, ast->FloatTy);
935 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
936 return CompilerType(ast, ast->DoubleTy);
937 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
938 return CompilerType(ast, ast->LongDoubleTy);
939 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
940 return CompilerType(ast, ast->HalfTy);
941 break;
942
943 case eEncodingVector:
944 // Sanity check that bit_size is a multiple of 8's.
945 if (bit_size && !(bit_size & 0x7u))
946 return CompilerType(
947 ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8));
948 break;
949 }
950
951 return CompilerType();
952}
953
954lldb::BasicType
955ClangASTContext::GetBasicTypeEnumeration(const ConstString &name) {
956 if (name) {
957 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
958 static TypeNameToBasicTypeMap g_type_map;
959 static llvm::once_flag g_once_flag;
960 llvm::call_once(g_once_flag, []() {
961 // "void"
962 g_type_map.Append(ConstString("void"), eBasicTypeVoid);
963
964 // "char"
965 g_type_map.Append(ConstString("char"), eBasicTypeChar);
966 g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar);
967 g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar);
968 g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar);
969 g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar);
970 g_type_map.Append(ConstString("unsigned wchar_t"),
971 eBasicTypeUnsignedWChar);
972 // "short"
973 g_type_map.Append(ConstString("short"), eBasicTypeShort);
974 g_type_map.Append(ConstString("short int"), eBasicTypeShort);
975 g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort);
976 g_type_map.Append(ConstString("unsigned short int"),
977 eBasicTypeUnsignedShort);
978
979 // "int"
980 g_type_map.Append(ConstString("int"), eBasicTypeInt);
981 g_type_map.Append(ConstString("signed int"), eBasicTypeInt);
982 g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt);
983 g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt);
984
985 // "long"
986 g_type_map.Append(ConstString("long"), eBasicTypeLong);
987 g_type_map.Append(ConstString("long int"), eBasicTypeLong);
988 g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong);
989 g_type_map.Append(ConstString("unsigned long int"),
990 eBasicTypeUnsignedLong);
991
992 // "long long"
993 g_type_map.Append(ConstString("long long"), eBasicTypeLongLong);
994 g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong);
995 g_type_map.Append(ConstString("unsigned long long"),
996 eBasicTypeUnsignedLongLong);
997 g_type_map.Append(ConstString("unsigned long long int"),
998 eBasicTypeUnsignedLongLong);
999
1000 // "int128"
1001 g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128);
1002 g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128);
1003
1004 // Miscellaneous
1005 g_type_map.Append(ConstString("bool"), eBasicTypeBool);
1006 g_type_map.Append(ConstString("float"), eBasicTypeFloat);
1007 g_type_map.Append(ConstString("double"), eBasicTypeDouble);
1008 g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble);
1009 g_type_map.Append(ConstString("id"), eBasicTypeObjCID);
1010 g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel);
1011 g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr);
1012 g_type_map.Sort();
1013 });
1014
1015 return g_type_map.Find(name, eBasicTypeInvalid);
1016 }
1017 return eBasicTypeInvalid;
1018}
1019
1020CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1021 const ConstString &name) {
1022 if (ast) {
1023 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
1024 return ClangASTContext::GetBasicType(ast, basic_type);
1025 }
1026 return CompilerType();
1027}
1028
1029uint32_t ClangASTContext::GetPointerByteSize() {
1030 if (m_pointer_byte_size == 0)
1031 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid)
1032 .GetPointerType()
1033 .GetByteSize(nullptr);
1034 return m_pointer_byte_size;
1035}
1036
1037CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
1038 return GetBasicType(getASTContext(), basic_type);
1039}
1040
1041CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1042 lldb::BasicType basic_type) {
1043 if (!ast)
1044 return CompilerType();
1045 lldb::opaque_compiler_type_t clang_type =
1046 GetOpaqueCompilerType(ast, basic_type);
1047
1048 if (clang_type)
1049 return CompilerType(GetASTContext(ast), clang_type);
1050 return CompilerType();
1051}
1052
1053CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
1054 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
1055 ASTContext *ast = getASTContext();
1056
1057#define streq(a, b)strcmp(a, b) == 0 strcmp(a, b) == 0
1058 assert(ast != nullptr)((ast != nullptr) ? static_cast<void> (0) : __assert_fail
("ast != nullptr", "/build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1058, __PRETTY_FUNCTION__))
;
1059 if (ast) {
1060 switch (dw_ate) {
1061 default:
1062 break;
1063
1064 case DW_ATE_address:
1065 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1066 return CompilerType(ast, ast->VoidPtrTy);
1067 break;
1068
1069 case DW_ATE_boolean:
1070 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
1071 return CompilerType(ast, ast->BoolTy);
1072 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1073 return CompilerType(ast, ast->UnsignedCharTy);
1074 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1075 return CompilerType(ast, ast->UnsignedShortTy);
1076 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1077 return CompilerType(ast, ast->UnsignedIntTy);
1078 break;
1079
1080 case DW_ATE_lo_user:
1081 // This has been seen to mean DW_AT_complex_integer
1082 if (type_name) {
1083 if (::strstr(type_name, "complex")) {
1084 CompilerType complex_int_clang_type =
1085 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1086 bit_size / 2);
1087 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1088 complex_int_clang_type)));
1089 }
1090 }
1091 break;
1092
1093 case DW_ATE_complex_float:
1094 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
1095 return CompilerType(ast, ast->FloatComplexTy);
1096 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
1097 return CompilerType(ast, ast->DoubleComplexTy);
1098 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
1099 return CompilerType(ast, ast->LongDoubleComplexTy);
1100 else {
1101 CompilerType complex_float_clang_type =
1102 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1103 bit_size / 2);
1104 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1105 complex_float_clang_type)));
1106 }
1107 break;
1108
1109 case DW_ATE_float:
1110 if (streq(type_name, "float")strcmp(type_name, "float") == 0 &&
1111 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1112 return CompilerType(ast, ast->FloatTy);
1113 if (streq(type_name, "double")strcmp(type_name, "double") == 0 &&
1114 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1115 return CompilerType(ast, ast->DoubleTy);
1116 if (streq(type_name, "long double")strcmp(type_name, "long double") == 0 &&
1117 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1118 return CompilerType(ast, ast->LongDoubleTy);
1119 // Fall back to not requiring a name match
1120 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1121 return CompilerType(ast, ast->FloatTy);
1122 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1123 return CompilerType(ast, ast->DoubleTy);
1124 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1125 return CompilerType(ast, ast->LongDoubleTy);
1126 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1127 return CompilerType(ast, ast->HalfTy);
1128 break;
1129
1130 case DW_ATE_signed:
1131 if (type_name) {
1132 if (streq(type_name, "wchar_t")strcmp(type_name, "wchar_t") == 0 &&
1133 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1134 (getTargetInfo() &&
1135 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1136 return CompilerType(ast, ast->WCharTy);
1137 if (streq(type_name, "void")strcmp(type_name, "void") == 0 &&
1138 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
1139 return CompilerType(ast, ast->VoidTy);
1140 if (strstr(type_name, "long long") &&
1141 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1142 return CompilerType(ast, ast->LongLongTy);
1143 if (strstr(type_name, "long") &&
1144 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1145 return CompilerType(ast, ast->LongTy);
1146 if (strstr(type_name, "short") &&
1147 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1148 return CompilerType(ast, ast->ShortTy);
1149 if (strstr(type_name, "char")) {
1150 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1151 return CompilerType(ast, ast->CharTy);
1152 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1153 return CompilerType(ast, ast->SignedCharTy);
1154 }
1155 if (strstr(type_name, "int")) {
1156 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1157 return CompilerType(ast, ast->IntTy);
1158 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1159 return CompilerType(ast, ast->Int128Ty);
1160 }
1161 }
1162 // We weren't able to match up a type name, just search by size
1163 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1164 return CompilerType(ast, ast->CharTy);
1165 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1166 return CompilerType(ast, ast->ShortTy);
1167 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1168 return CompilerType(ast, ast->IntTy);
1169 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1170 return CompilerType(ast, ast->LongTy);
1171 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1172 return CompilerType(ast, ast->LongLongTy);
1173 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1174 return CompilerType(ast, ast->Int128Ty);
1175 break;
1176
1177 case DW_ATE_signed_char:
1178 if (ast->getLangOpts().CharIsSigned && type_name &&
1179 streq(type_name, "char")strcmp(type_name, "char") == 0) {
1180 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1181 return CompilerType(ast, ast->CharTy);
1182 }
1183 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1184 return CompilerType(ast, ast->SignedCharTy);
1185 break;
1186
1187 case DW_ATE_unsigned:
1188 if (type_name) {
1189 if (streq(type_name, "wchar_t")strcmp(type_name, "wchar_t") == 0) {
1190 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1191 if (!(getTargetInfo() &&
1192 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1193 return CompilerType(ast, ast->WCharTy);
1194 }
1195 }
1196 if (strstr(type_name, "long long")) {
1197 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1198 return CompilerType(ast, ast->UnsignedLongLongTy);
1199 } else if (strstr(type_name, "long")) {
1200 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1201 return CompilerType(ast, ast->UnsignedLongTy);
1202 } else if (strstr(type_name, "short")) {
1203 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1204 return CompilerType(ast, ast->UnsignedShortTy);
1205 } else if (strstr(type_name, "char")) {
1206 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1207 return CompilerType(ast, ast->UnsignedCharTy);
1208 } else if (strstr(type_name, "int")) {
1209 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1210 return CompilerType(ast, ast->UnsignedIntTy);
1211 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1212 return CompilerType(ast, ast->UnsignedInt128Ty);
1213 }
1214 }
1215 // We weren't able to match up a type name, just search by size
1216 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1217 return CompilerType(ast, ast->UnsignedCharTy);
1218 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1219 return CompilerType(ast, ast->UnsignedShortTy);
1220 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1221 return CompilerType(ast, ast->UnsignedIntTy);
1222 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1223 return CompilerType(ast, ast->UnsignedLongTy);
1224 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1225 return CompilerType(ast, ast->UnsignedLongLongTy);
1226 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1227 return CompilerType(ast, ast->UnsignedInt128Ty);
1228 break;
1229
1230 case DW_ATE_unsigned_char:
1231 if (!ast->getLangOpts().CharIsSigned && type_name &&
1232 streq(type_name, "char")strcmp(type_name, "char") == 0) {
1233 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1234 return CompilerType(ast, ast->CharTy);
1235 }
1236 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1237 return CompilerType(ast, ast->UnsignedCharTy);
1238 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1239 return CompilerType(ast, ast->UnsignedShortTy);
1240 break;
1241
1242 case DW_ATE_imaginary_float:
1243 break;
1244
1245 case DW_ATE_UTF:
1246 if (type_name) {
1247 if (streq(type_name, "char16_t")strcmp(type_name, "char16_t") == 0) {
1248 return CompilerType(ast, ast->Char16Ty);
1249 } else if (streq(type_name, "char32_t")strcmp(type_name, "char32_t") == 0) {
1250 return CompilerType(ast, ast->Char32Ty);
1251 }
1252 }
1253 break;
1254 }
1255 }
1256 // This assert should fire for anything that we don't catch above so we know
1257 // to fix any issues we run into.
1258 if (type_name) {
1259 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1260 "DW_TAG_base_type '%s' encoded with "
1261 "DW_ATE = 0x%x, bit_size = %u\n",
1262 type_name, dw_ate, bit_size);
1263 } else {
1264 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1265 "DW_TAG_base_type encoded with "
1266 "DW_ATE = 0x%x, bit_size = %u\n",
1267 dw_ate, bit_size);
1268 }
1269 return CompilerType();
1270}
1271
1272CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) {
1273 if (ast)
1274 return CompilerType(ast, ast->UnknownAnyTy);
1275 return CompilerType();
1276}
1277
1278CompilerType ClangASTContext::GetCStringType(bool is_const) {
1279 ASTContext *ast = getASTContext();
1280 QualType char_type(ast->CharTy);
1281
1282 if (is_const)
1283 char_type.addConst();
1284
1285 return CompilerType(ast, ast->getPointerType(char_type));
1286}
1287
1288clang::DeclContext *
1289ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1290 return ast->getTranslationUnitDecl();
1291}
1292
1293clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1294 clang::Decl *source_decl) {
1295 FileSystemOptions file_system_options;
1296 FileManager file_manager(file_system_options);
1297 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1298
1299 return importer.Import(source_decl);
1300}
1301
1302bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1303 bool ignore_qualifiers) {
1304 ClangASTContext *ast =
1305 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1306 if (!ast || ast != type2.GetTypeSystem())
1307 return false;
1308
1309 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1310 return true;
1311
1312 QualType type1_qual = ClangUtil::GetQualType(type1);
1313 QualType type2_qual = ClangUtil::GetQualType(type2);
1314
1315 if (ignore_qualifiers) {
1316 type1_qual = type1_qual.getUnqualifiedType();
1317 type2_qual = type2_qual.getUnqualifiedType();
1318 }
1319
1320 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
1321}
1322
1323CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1324 if (clang::ObjCInterfaceDecl *interface_decl =
1325 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1326 return GetTypeForDecl(interface_decl);
1327 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1328 return GetTypeForDecl(tag_decl);
1329 return CompilerType();
1330}
1331
1332CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
1333 // No need to call the getASTContext() accessor (which can create the AST if
1334 // it isn't created yet, because we can't have created a decl in this
1335 // AST if our AST didn't already exist...
1336 ASTContext *ast = &decl->getASTContext();
1337 if (ast)
1338 return CompilerType(ast, ast->getTagDeclType(decl));
1339 return CompilerType();
1340}
1341
1342CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
1343 // No need to call the getASTContext() accessor (which can create the AST if
1344 // it isn't created yet, because we can't have created a decl in this
1345 // AST if our AST didn't already exist...
1346 ASTContext *ast = &decl->getASTContext();
1347 if (ast)
1348 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1349 return CompilerType();
1350}
1351
1352#pragma mark Structure, Unions, Classes
1353
1354CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx,
1355 AccessType access_type,
1356 const char *name, int kind,
1357 LanguageType language,
1358 ClangASTMetadata *metadata) {
1359 ASTContext *ast = getASTContext();
1360 assert(ast != nullptr)((ast != nullptr) ? static_cast<void> (0) : __assert_fail
("ast != nullptr", "/build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1360, __PRETTY_FUNCTION__))
;
1361
1362 if (decl_ctx == nullptr)
1363 decl_ctx = ast->getTranslationUnitDecl();
1364
1365 if (language == eLanguageTypeObjC ||
1366 language == eLanguageTypeObjC_plus_plus) {
1367 bool isForwardDecl = true;
1368 bool isInternal = false;
1369 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1370 }
1371
1372 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1373 // we will need to update this code. I was told to currently always use the
1374 // CXXRecordDecl class since we often don't know from debug information if
1375 // something is struct or a class, so we default to always use the more
1376 // complete definition just in case.
1377
1378 bool is_anonymous = (!name) || (!name[0]);
1379
1380 CXXRecordDecl *decl = CXXRecordDecl::Create(
1381 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1382 SourceLocation(), is_anonymous ? nullptr : &ast->Idents.get(name));
1383
1384 if (is_anonymous)
1385 decl->setAnonymousStructOrUnion(true);
1386
1387 if (decl) {
1388 if (metadata)
1389 SetMetadata(ast, decl, *metadata);
1390
1391 if (access_type != eAccessNone)
1392 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1393
1394 if (decl_ctx)
1395 decl_ctx->addDecl(decl);
1396
1397 return CompilerType(ast, ast->getTagDeclType(decl));
1398 }
1399 return CompilerType();
1400}
1401
1402namespace {
1403 bool IsValueParam(const clang::TemplateArgument &argument) {
1404 return argument.getKind() == TemplateArgument::Integral;
1405 }
1406}
1407
1408static TemplateParameterList *CreateTemplateParameterList(
1409 ASTContext *ast,
1410 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1411 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1412 const bool parameter_pack = false;
1413 const bool is_typename = false;
1414 const unsigned depth = 0;
1415 const size_t num_template_params = template_param_infos.args.size();
1416 DeclContext *const decl_context =
1417 ast->getTranslationUnitDecl(); // Is this the right decl context?,
1418 for (size_t i = 0; i < num_template_params; ++i) {
1419 const char *name = template_param_infos.names[i];
1420
1421 IdentifierInfo *identifier_info = nullptr;
1422 if (name && name[0])
1423 identifier_info = &ast->Idents.get(name);
1424 if (IsValueParam(template_param_infos.args[i])) {
1425 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1426 *ast, decl_context,
1427 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1428 template_param_infos.args[i].getIntegralType(), parameter_pack,
1429 nullptr));
1430
1431 } else {
1432 template_param_decls.push_back(TemplateTypeParmDecl::Create(
1433 *ast, decl_context,
1434 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1435 is_typename, parameter_pack));
1436 }
1437 }
1438
1439 if (template_param_infos.packed_args &&
1440 template_param_infos.packed_args->args.size()) {
1441 IdentifierInfo *identifier_info = nullptr;
1442 if (template_param_infos.pack_name && template_param_infos.pack_name[0])
1443 identifier_info = &ast->Idents.get(template_param_infos.pack_name);
1444 const bool parameter_pack_true = true;
1445 if (IsValueParam(template_param_infos.packed_args->args[0])) {
1446 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1447 *ast, decl_context,
1448 SourceLocation(), SourceLocation(), depth, num_template_params,
1449 identifier_info,
1450 template_param_infos.packed_args->args[0].getIntegralType(),
1451 parameter_pack_true, nullptr));
1452 } else {
1453 template_param_decls.push_back(TemplateTypeParmDecl::Create(
1454 *ast, decl_context,
1455 SourceLocation(), SourceLocation(), depth, num_template_params,
1456 identifier_info,
1457 is_typename, parameter_pack_true));
1458 }
1459 }
1460 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1461 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1462 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1463 SourceLocation(), requires_clause);
1464 return template_param_list;
1465}
1466
1467clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1468 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1469 const char *name, const TemplateParameterInfos &template_param_infos) {
1470 // /// Create a function template node.
1471 ASTContext *ast = getASTContext();
1472
1473 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1474
1475 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1476 ast, template_param_infos, template_param_decls);
1477 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1478 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1479 template_param_list, func_decl);
1480
1481 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1482 i < template_param_decl_count; ++i) {
1483 // TODO: verify which decl context we should put template_param_decls into..
1484 template_param_decls[i]->setDeclContext(func_decl);
1485 }
1486
1487 return func_tmpl_decl;
1488}
1489
1490void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1491 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1492 const TemplateParameterInfos &infos) {
1493 TemplateArgumentList template_args(TemplateArgumentList::OnStack, infos.args);
1494
1495 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, &template_args,
1496 nullptr);
1497}
1498
1499ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1500 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1501 int kind, const TemplateParameterInfos &template_param_infos) {
1502 ASTContext *ast = getASTContext();
1503
1504 ClassTemplateDecl *class_template_decl = nullptr;
1505 if (decl_ctx == nullptr)
1506 decl_ctx = ast->getTranslationUnitDecl();
1507
1508 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1509 DeclarationName decl_name(&identifier_info);
1510
1511 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1512
1513 for (NamedDecl *decl : result) {
1514 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
1515 if (class_template_decl)
1516 return class_template_decl;
1517 }
1518
1519 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1520
1521 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1522 ast, template_param_infos, template_param_decls);
1523
1524 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1525 *ast, (TagDecl::TagKind)kind,
1526 decl_ctx, // What decl context do we use here? TU? The actual decl
1527 // context?
1528 SourceLocation(), SourceLocation(), &identifier_info);
1529
1530 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1531 i < template_param_decl_count; ++i) {
1532 template_param_decls[i]->setDeclContext(template_cxx_decl);
1533 }
1534
1535 // With templated classes, we say that a class is templated with
1536 // specializations, but that the bare class has no functions.
1537 // template_cxx_decl->startDefinition();
1538 // template_cxx_decl->completeDefinition();
1539
1540 class_template_decl = ClassTemplateDecl::Create(
1541 *ast,
1542 decl_ctx, // What decl context do we use here? TU? The actual decl
1543 // context?
1544 SourceLocation(), decl_name, template_param_list, template_cxx_decl);
1545
1546 if (class_template_decl) {
1547 if (access_type != eAccessNone)
1548 class_template_decl->setAccess(
1549 ConvertAccessTypeToAccessSpecifier(access_type));
1550
1551 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1552 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1553
1554 decl_ctx->addDecl(class_template_decl);
1555
1556#ifdef LLDB_CONFIGURATION_DEBUG
1557 VerifyDecl(class_template_decl);
1558#endif
1559 }
1560
1561 return class_template_decl;
1562}
1563
1564TemplateTemplateParmDecl *
1565ClangASTContext::CreateTemplateTemplateParmDecl(const char *template_name) {
1566 ASTContext *ast = getASTContext();
1567
1568 auto *decl_ctx = ast->getTranslationUnitDecl();
1569
1570 IdentifierInfo &identifier_info = ast->Idents.get(template_name);
1571 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1572
1573 ClangASTContext::TemplateParameterInfos template_param_infos;
1574 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1575 ast, template_param_infos, template_param_decls);
1576
1577 // LLDB needs to create those decls only to be able to display a
1578 // type that includes a template template argument. Only the name matters for
1579 // this purpose, so we use dummy values for the other characterisitcs of the
1580 // type.
1581 return TemplateTemplateParmDecl::Create(
1582 *ast, decl_ctx, SourceLocation(),
1583 /*Depth*/ 0, /*Position*/ 0,
1584 /*IsParameterPack*/ false, &identifier_info, template_param_list);
1585}
1586
1587ClassTemplateSpecializationDecl *
1588ClangASTContext::CreateClassTemplateSpecializationDecl(
1589 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1590 const TemplateParameterInfos &template_param_infos) {
1591 ASTContext *ast = getASTContext();
1592 llvm::SmallVector<clang::TemplateArgument, 2> args(
1593 template_param_infos.args.size() +
1594 (template_param_infos.packed_args ? 1 : 0));
1595 std::copy(template_param_infos.args.begin(), template_param_infos.args.end(),
1596 args.begin());
1597 if (template_param_infos.packed_args) {
1598 args[args.size() - 1] = TemplateArgument::CreatePackCopy(
1599 *ast, template_param_infos.packed_args->args);
1600 }
1601 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1602 ClassTemplateSpecializationDecl::Create(
1603 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1604 SourceLocation(), class_template_decl, args,
1605 nullptr);
1606
1607 class_template_specialization_decl->setSpecializationKind(
1608 TSK_ExplicitSpecialization);
1609
1610 return class_template_specialization_decl;
1611}
1612
1613CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1614 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1615 if (class_template_specialization_decl) {
1616 ASTContext *ast = getASTContext();
1617 if (ast)
1618 return CompilerType(
1619 ast, ast->getTagDeclType(class_template_specialization_decl));
1620 }
1621 return CompilerType();
1622}
1623
1624static inline bool check_op_param(bool is_method,
1625 clang::OverloadedOperatorKind op_kind,
1626 bool unary, bool binary,
1627 uint32_t num_params) {
1628 // Special-case call since it can take any number of operands
1629 if (op_kind == OO_Call)
1630 return true;
1631
1632 // The parameter count doesn't include "this"
1633 if (is_method)
1634 ++num_params;
1635 if (num_params == 1)
1636 return unary;
1637 if (num_params == 2)
1638 return binary;
1639 else
1640 return false;
1641}
1642
1643bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1644 bool is_method, clang::OverloadedOperatorKind op_kind,
1645 uint32_t num_params) {
1646 switch (op_kind) {
1647 default:
1648 break;
1649 // C++ standard allows any number of arguments to new/delete
1650 case OO_New:
1651 case OO_Array_New:
1652 case OO_Delete:
1653 case OO_Array_Delete:
1654 return true;
1655 }
1656
1657#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1658 case OO_##Name: \
1659 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1660 switch (op_kind) {
1661#include "clang/Basic/OperatorKinds.def"
1662 default:
1663 break;
1664 }
1665 return false;
1666}
1667
1668clang::AccessSpecifier
1669ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1670 clang::AccessSpecifier rhs) {
1671 // Make the access equal to the stricter of the field and the nested field's
1672 // access
1673 if (lhs == AS_none || rhs == AS_none)
1674 return AS_none;
1675 if (lhs == AS_private || rhs == AS_private)
1676 return AS_private;
1677 if (lhs == AS_protected || rhs == AS_protected)
1678 return AS_protected;
1679 return AS_public;
1680}
1681
1682bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1683 uint32_t &bitfield_bit_size) {
1684 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
1685}
1686
1687bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field,
1688 uint32_t &bitfield_bit_size) {
1689 if (ast == nullptr || field == nullptr)
1690 return false;
1691
1692 if (field->isBitField()) {
1693 Expr *bit_width_expr = field->getBitWidth();
1694 if (bit_width_expr) {
1695 llvm::APSInt bit_width_apsint;
1696 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1697 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX(4294967295U));
1698 return true;
1699 }
1700 }
1701 }
1702 return false;
1703}
1704
1705bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1706 if (record_decl == nullptr)
1707 return false;
1708
1709 if (!record_decl->field_empty())
1710 return true;
1711
1712 // No fields, lets check this is a CXX record and check the base classes
1713 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1714 if (cxx_record_decl) {
1715 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1716 for (base_class = cxx_record_decl->bases_begin(),
1717 base_class_end = cxx_record_decl->bases_end();
1718 base_class != base_class_end; ++base_class) {
1719 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1720 base_class->getType()->getAs<RecordType>()->getDecl());
1721 if (RecordHasFields(base_class_decl))
1722 return true;
1723 }
1724 }
1725 return false;
1726}
1727
1728#pragma mark Objective-C Classes
1729
1730CompilerType ClangASTContext::CreateObjCClass(const char *name,
1731 DeclContext *decl_ctx,
1732 bool isForwardDecl,
1733 bool isInternal,
1734 ClangASTMetadata *metadata) {
1735 ASTContext *ast = getASTContext();
1736 assert(ast != nullptr)((ast != nullptr) ? static_cast<void> (0) : __assert_fail
("ast != nullptr", "/build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1736, __PRETTY_FUNCTION__))
;
1737 assert(name && name[0])((name && name[0]) ? static_cast<void> (0) : __assert_fail
("name && name[0]", "/build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1737, __PRETTY_FUNCTION__))
;
1738 if (decl_ctx == nullptr)
1739 decl_ctx = ast->getTranslationUnitDecl();
1740
1741 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1742 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1743 nullptr, SourceLocation(),
1744 /*isForwardDecl,*/
1745 isInternal);
1746
1747 if (decl && metadata)
1748 SetMetadata(ast, decl, *metadata);
1749
1750 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1751}
1752
1753static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
1754 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) ==
1755 false;
1756}
1757
1758uint32_t
1759ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1760 bool omit_empty_base_classes) {
1761 uint32_t num_bases = 0;
1762 if (cxx_record_decl) {
1763 if (omit_empty_base_classes) {
1764 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1765 for (base_class = cxx_record_decl->bases_begin(),
1766 base_class_end = cxx_record_decl->bases_end();
1767 base_class != base_class_end; ++base_class) {
1768 // Skip empty base classes
1769 if (omit_empty_base_classes) {
1770 if (BaseSpecifierIsEmpty(base_class))
1771 continue;
1772 }
1773 ++num_bases;
1774 }
1775 } else
1776 num_bases = cxx_record_decl->getNumBases();
1777 }
1778 return num_bases;
1779}
1780
1781#pragma mark Namespace Declarations
1782
1783NamespaceDecl *
1784ClangASTContext::GetUniqueNamespaceDeclaration(const char *name,
1785 DeclContext *decl_ctx) {
1786 NamespaceDecl *namespace_decl = nullptr;
1787 ASTContext *ast = getASTContext();
1788 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1789 if (decl_ctx == nullptr)
1790 decl_ctx = translation_unit_decl;
1791
1792 if (name) {
1793 IdentifierInfo &identifier_info = ast->Idents.get(name);
1794 DeclarationName decl_name(&identifier_info);
1795 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1796 for (NamedDecl *decl : result) {
1797 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1798 if (namespace_decl)
1799 return namespace_decl;
1800 }
1801
1802 namespace_decl =
1803 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1804 SourceLocation(), &identifier_info, nullptr);
1805
1806 decl_ctx->addDecl(namespace_decl);
1807 } else {
1808 if (decl_ctx == translation_unit_decl) {
1809 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1810 if (namespace_decl)
1811 return namespace_decl;
1812
1813 namespace_decl =
1814 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1815 SourceLocation(), nullptr, nullptr);
1816 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1817 translation_unit_decl->addDecl(namespace_decl);
1818 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace())((namespace_decl == translation_unit_decl->getAnonymousNamespace
()) ? static_cast<void> (0) : __assert_fail ("namespace_decl == translation_unit_decl->getAnonymousNamespace()"
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1818, __PRETTY_FUNCTION__))
;
1819 } else {
1820 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1821 if (parent_namespace_decl) {
1822 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1823 if (namespace_decl)
1824 return namespace_decl;
1825 namespace_decl =
1826 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1827 SourceLocation(), nullptr, nullptr);
1828 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1829 parent_namespace_decl->addDecl(namespace_decl);
1830 assert(namespace_decl ==((namespace_decl == parent_namespace_decl->getAnonymousNamespace
()) ? static_cast<void> (0) : __assert_fail ("namespace_decl == parent_namespace_decl->getAnonymousNamespace()"
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1831, __PRETTY_FUNCTION__))
1831 parent_namespace_decl->getAnonymousNamespace())((namespace_decl == parent_namespace_decl->getAnonymousNamespace
()) ? static_cast<void> (0) : __assert_fail ("namespace_decl == parent_namespace_decl->getAnonymousNamespace()"
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1831, __PRETTY_FUNCTION__))
;
1832 } else {
1833 // BAD!!!
1834 }
1835 }
1836 }
1837#ifdef LLDB_CONFIGURATION_DEBUG
1838 VerifyDecl(namespace_decl);
1839#endif
1840 return namespace_decl;
1841}
1842
1843NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1844 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx) {
1845 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1846 if (ast_ctx == nullptr)
1847 return nullptr;
1848
1849 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx);
1850}
1851
1852clang::BlockDecl *
1853ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1854 if (ctx != nullptr) {
1855 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1856 clang::SourceLocation());
1857 ctx->addDecl(decl);
1858 return decl;
1859 }
1860 return nullptr;
1861}
1862
1863clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1864 clang::DeclContext *right,
1865 clang::DeclContext *root) {
1866 if (root == nullptr)
1867 return nullptr;
1868
1869 std::set<clang::DeclContext *> path_left;
1870 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1871 path_left.insert(d);
1872
1873 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1874 if (path_left.find(d) != path_left.end())
1875 return d;
1876
1877 return nullptr;
1878}
1879
1880clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
1881 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
1882 if (decl_ctx != nullptr && ns_decl != nullptr) {
1883 clang::TranslationUnitDecl *translation_unit =
1884 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
1885 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
1886 *getASTContext(), decl_ctx, clang::SourceLocation(),
1887 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
1888 clang::SourceLocation(), ns_decl,
1889 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
1890 decl_ctx->addDecl(using_decl);
1891 return using_decl;
1892 }
1893 return nullptr;
1894}
1895
1896clang::UsingDecl *
1897ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
1898 clang::NamedDecl *target) {
1899 if (current_decl_ctx != nullptr && target != nullptr) {
1900 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
1901 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
1902 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
1903 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
1904 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
1905 target);
1906 using_decl->addShadowDecl(shadow_decl);
1907 current_decl_ctx->addDecl(using_decl);
1908 return using_decl;
1909 }
1910 return nullptr;
1911}
1912
1913clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
1914 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
1915 if (decl_context != nullptr) {
1916 clang::VarDecl *var_decl = clang::VarDecl::Create(
1917 *getASTContext(), decl_context, clang::SourceLocation(),
1918 clang::SourceLocation(),
1919 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
1920 nullptr, clang::SC_None);
1921 var_decl->setAccess(clang::AS_public);
1922 decl_context->addDecl(var_decl);
1923 return var_decl;
1924 }
1925 return nullptr;
1926}
1927
1928lldb::opaque_compiler_type_t
1929ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
1930 lldb::BasicType basic_type) {
1931 switch (basic_type) {
1932 case eBasicTypeVoid:
1933 return ast->VoidTy.getAsOpaquePtr();
1934 case eBasicTypeChar:
1935 return ast->CharTy.getAsOpaquePtr();
1936 case eBasicTypeSignedChar:
1937 return ast->SignedCharTy.getAsOpaquePtr();
1938 case eBasicTypeUnsignedChar:
1939 return ast->UnsignedCharTy.getAsOpaquePtr();
1940 case eBasicTypeWChar:
1941 return ast->getWCharType().getAsOpaquePtr();
1942 case eBasicTypeSignedWChar:
1943 return ast->getSignedWCharType().getAsOpaquePtr();
1944 case eBasicTypeUnsignedWChar:
1945 return ast->getUnsignedWCharType().getAsOpaquePtr();
1946 case eBasicTypeChar16:
1947 return ast->Char16Ty.getAsOpaquePtr();
1948 case eBasicTypeChar32:
1949 return ast->Char32Ty.getAsOpaquePtr();
1950 case eBasicTypeShort:
1951 return ast->ShortTy.getAsOpaquePtr();
1952 case eBasicTypeUnsignedShort:
1953 return ast->UnsignedShortTy.getAsOpaquePtr();
1954 case eBasicTypeInt:
1955 return ast->IntTy.getAsOpaquePtr();
1956 case eBasicTypeUnsignedInt:
1957 return ast->UnsignedIntTy.getAsOpaquePtr();
1958 case eBasicTypeLong:
1959 return ast->LongTy.getAsOpaquePtr();
1960 case eBasicTypeUnsignedLong:
1961 return ast->UnsignedLongTy.getAsOpaquePtr();
1962 case eBasicTypeLongLong:
1963 return ast->LongLongTy.getAsOpaquePtr();
1964 case eBasicTypeUnsignedLongLong:
1965 return ast->UnsignedLongLongTy.getAsOpaquePtr();
1966 case eBasicTypeInt128:
1967 return ast->Int128Ty.getAsOpaquePtr();
1968 case eBasicTypeUnsignedInt128:
1969 return ast->UnsignedInt128Ty.getAsOpaquePtr();
1970 case eBasicTypeBool:
1971 return ast->BoolTy.getAsOpaquePtr();
1972 case eBasicTypeHalf:
1973 return ast->HalfTy.getAsOpaquePtr();
1974 case eBasicTypeFloat:
1975 return ast->FloatTy.getAsOpaquePtr();
1976 case eBasicTypeDouble:
1977 return ast->DoubleTy.getAsOpaquePtr();
1978 case eBasicTypeLongDouble:
1979 return ast->LongDoubleTy.getAsOpaquePtr();
1980 case eBasicTypeFloatComplex:
1981 return ast->FloatComplexTy.getAsOpaquePtr();
1982 case eBasicTypeDoubleComplex:
1983 return ast->DoubleComplexTy.getAsOpaquePtr();
1984 case eBasicTypeLongDoubleComplex:
1985 return ast->LongDoubleComplexTy.getAsOpaquePtr();
1986 case eBasicTypeObjCID:
1987 return ast->getObjCIdType().getAsOpaquePtr();
1988 case eBasicTypeObjCClass:
1989 return ast->getObjCClassType().getAsOpaquePtr();
1990 case eBasicTypeObjCSel:
1991 return ast->getObjCSelType().getAsOpaquePtr();
1992 case eBasicTypeNullPtr:
1993 return ast->NullPtrTy.getAsOpaquePtr();
1994 default:
1995 return nullptr;
1996 }
1997}
1998
1999#pragma mark Function Types
2000
2001clang::DeclarationName
2002ClangASTContext::GetDeclarationName(const char *name,
2003 const CompilerType &function_clang_type) {
2004 if (!name || !name[0])
2005 return clang::DeclarationName();
2006
2007 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
2008 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
2009 return DeclarationName(&getASTContext()->Idents.get(
2010 name)); // Not operator, but a regular function.
2011
2012 // Check the number of operator parameters. Sometimes we have seen bad DWARF
2013 // that doesn't correctly describe operators and if we try to create a method
2014 // and add it to the class, clang will assert and crash, so we need to make
2015 // sure things are acceptable.
2016 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
2017 const clang::FunctionProtoType *function_type =
2018 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
2019 if (function_type == nullptr)
2020 return clang::DeclarationName();
2021
2022 const bool is_method = false;
2023 const unsigned int num_params = function_type->getNumParams();
2024 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
2025 is_method, op_kind, num_params))
2026 return clang::DeclarationName();
2027
2028 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
2029}
2030
2031FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
2032 DeclContext *decl_ctx, const char *name,
2033 const CompilerType &function_clang_type, int storage, bool is_inline) {
2034 FunctionDecl *func_decl = nullptr;
2035 ASTContext *ast = getASTContext();
2036 if (decl_ctx == nullptr)
2037 decl_ctx = ast->getTranslationUnitDecl();
2038
2039 const bool hasWrittenPrototype = true;
2040 const bool isConstexprSpecified = false;
2041
2042 clang::DeclarationName declarationName =
2043 GetDeclarationName(name, function_clang_type);
2044 func_decl = FunctionDecl::Create(
2045 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
2046 ClangUtil::GetQualType(function_clang_type), nullptr,
2047 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
2048 isConstexprSpecified);
2049 if (func_decl)
2050 decl_ctx->addDecl(func_decl);
2051
2052#ifdef LLDB_CONFIGURATION_DEBUG
2053 VerifyDecl(func_decl);
2054#endif
2055
2056 return func_decl;
2057}
2058
2059CompilerType ClangASTContext::CreateFunctionType(
2060 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
2061 unsigned num_args, bool is_variadic, unsigned type_quals,
2062 clang::CallingConv cc) {
2063 if (ast == nullptr)
2064 return CompilerType(); // invalid AST
2065
2066 if (!result_type || !ClangUtil::IsClangType(result_type))
2067 return CompilerType(); // invalid return type
2068
2069 std::vector<QualType> qual_type_args;
2070 if (num_args > 0 && args == nullptr)
2071 return CompilerType(); // invalid argument array passed in
2072
2073 // Verify that all arguments are valid and the right type
2074 for (unsigned i = 0; i < num_args; ++i) {
2075 if (args[i]) {
2076 // Make sure we have a clang type in args[i] and not a type from another
2077 // language whose name might match
2078 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2079 lldbassert(is_clang_type)lldb_private::lldb_assert(static_cast<bool>(is_clang_type
), "is_clang_type", __FUNCTION__, "/build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 2079)
;
2080 if (is_clang_type)
2081 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2082 else
2083 return CompilerType(); // invalid argument type (must be a clang type)
2084 } else
2085 return CompilerType(); // invalid argument type (empty)
2086 }
2087
2088 // TODO: Detect calling convention in DWARF?
2089 FunctionProtoType::ExtProtoInfo proto_info;
2090 proto_info.ExtInfo = cc;
2091 proto_info.Variadic = is_variadic;
2092 proto_info.ExceptionSpec = EST_None;
2093 proto_info.TypeQuals = type_quals;
2094 proto_info.RefQualifier = RQ_None;
2095
2096 return CompilerType(ast,
2097 ast->getFunctionType(ClangUtil::GetQualType(result_type),
2098 qual_type_args, proto_info));
2099}
2100
2101ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
2102 const char *name, const CompilerType &param_type, int storage) {
2103 ASTContext *ast = getASTContext();
2104 assert(ast != nullptr)((ast != nullptr) ? static_cast<void> (0) : __assert_fail
("ast != nullptr", "/build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 2104, __PRETTY_FUNCTION__))
;
2105 return ParmVarDecl::Create(*ast, ast->getTranslationUnitDecl(),
2106 SourceLocation(), SourceLocation(),
2107 name && name[0] ? &ast->Idents.get(name) : nullptr,
2108 ClangUtil::GetQualType(param_type), nullptr,
2109 (clang::StorageClass)storage, nullptr);
2110}
2111
2112void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2113 ParmVarDecl **params,
2114 unsigned num_params) {
2115 if (function_decl)
2116 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
2117}
2118
2119CompilerType
2120ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
2121 QualType block_type = m_ast_ap->getBlockPointerType(
2122 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
2123
2124 return CompilerType(this, block_type.getAsOpaquePtr());
2125}
2126
2127#pragma mark Array Types
2128
2129CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2130 size_t element_count,
2131 bool is_vector) {
2132 if (element_type.IsValid()) {
2133 ASTContext *ast = getASTContext();
2134 assert(ast != nullptr)((ast != nullptr) ? static_cast<void> (0) : __assert_fail
("ast != nullptr", "/build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 2134, __PRETTY_FUNCTION__))
;
2135
2136 if (is_vector) {
2137 return CompilerType(
2138 ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2139 element_count));
2140 } else {
2141
2142 llvm::APInt ap_element_count(64, element_count);
2143 if (element_count == 0) {
2144 return CompilerType(ast, ast->getIncompleteArrayType(
2145 ClangUtil::GetQualType(element_type),
2146 clang::ArrayType::Normal, 0));
2147 } else {
2148 return CompilerType(
2149 ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2150 ap_element_count,
2151 clang::ArrayType::Normal, 0));
2152 }
2153 }
2154 }
2155 return CompilerType();
2156}
2157
2158CompilerType ClangASTContext::CreateStructForIdentifier(
2159 const ConstString &type_name,
2160 const std::initializer_list<std::pair<const char *, CompilerType>>
2161 &type_fields,
2162 bool packed) {
2163 CompilerType type;
2164 if (!type_name.IsEmpty() &&
2165 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2166 .IsValid()) {
2167 lldbassert(0 && "Trying to create a type for an existing name")lldb_private::lldb_assert(static_cast<bool>(0 &&
"Trying to create a type for an existing name"), "0 && \"Trying to create a type for an existing name\""
, __FUNCTION__, "/build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 2167)
;
2168 return type;
2169 }
2170
2171 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2172 clang::TTK_Struct, lldb::eLanguageTypeC);
2173 StartTagDeclarationDefinition(type);
2174 for (const auto &field : type_fields)
2175 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2176 0);
2177 if (packed)
2178 SetIsPacked(type);
2179 CompleteTagDeclarationDefinition(type);
2180 return type;
2181}
2182
2183CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
2184 const ConstString &type_name,
2185 const std::initializer_list<std::pair<const char *, CompilerType>>
2186 &type_fields,
2187 bool packed) {
2188 CompilerType type;
2189 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2190 return type;
2191
2192 return CreateStructForIdentifier(type_name, type_fields, packed);
2193}
2194
2195#pragma mark Enumeration Types
2196
2197CompilerType
2198ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2199 const Declaration &decl,
2200 const CompilerType &integer_clang_type,
2201 bool is_scoped) {
2202 // TODO: Do something intelligent with the Declaration object passed in
2203 // like maybe filling in the SourceLocation with it...
2204 ASTContext *ast = getASTContext();
2205
2206 // TODO: ask about these...
2207 // const bool IsFixed = false;
2208
2209 EnumDecl *enum_decl = EnumDecl::Create(
2210 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2211 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
2212 is_scoped, // IsScoped
2213 is_scoped, // IsScopedUsingClassTag
2214 false); // IsFixed
2215
2216 if (enum_decl) {
2217 if (decl_ctx)
2218 decl_ctx->addDecl(enum_decl);
2219
2220 // TODO: check if we should be setting the promotion type too?
2221 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2222
2223 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2224
2225 return CompilerType(ast, ast->getTagDeclType(enum_decl));
2226 }
2227 return CompilerType();
2228}
2229
2230CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2231 size_t bit_size,
2232 bool is_signed) {
2233 if (ast) {
2234 if (is_signed) {
2235 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
2236 return CompilerType(ast, ast->SignedCharTy);
2237
2238 if (bit_size == ast->getTypeSize(ast->ShortTy))
2239 return CompilerType(ast, ast->ShortTy);
2240
2241 if (bit_size == ast->getTypeSize(ast->IntTy))
2242 return CompilerType(ast, ast->IntTy);
2243
2244 if (bit_size == ast->getTypeSize(ast->LongTy))
2245 return CompilerType(ast, ast->LongTy);
2246
2247 if (bit_size == ast->getTypeSize(ast->LongLongTy))
2248 return CompilerType(ast, ast->LongLongTy);
2249
2250 if (bit_size == ast->getTypeSize(ast->Int128Ty))
2251 return CompilerType(ast, ast->Int128Ty);
2252 } else {
2253 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
2254 return CompilerType(ast, ast->UnsignedCharTy);
2255
2256 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
2257 return CompilerType(ast, ast->UnsignedShortTy);
2258
2259 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
2260 return CompilerType(ast, ast->UnsignedIntTy);
2261
2262 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
2263 return CompilerType(ast, ast->UnsignedLongTy);
2264
2265 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
2266 return CompilerType(ast, ast->UnsignedLongLongTy);
2267
2268 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
2269 return CompilerType(ast, ast->UnsignedInt128Ty);
2270 }
2271 }
2272 return CompilerType();
2273}
2274
2275CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2276 bool is_signed) {
2277 if (ast)
2278 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2279 is_signed);
2280 return CompilerType();
2281}
2282
2283void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2284 if (decl_ctx) {
2285 DumpDeclContextHiearchy(decl_ctx->getParent());
2286
2287 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2288 if (named_decl) {
2289 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2290 named_decl->getDeclName().getAsString().c_str());
2291 } else {
2292 printf("%20s\n", decl_ctx->getDeclKindName());
2293 }
2294 }
2295}
2296
2297void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2298 if (decl == nullptr)
2299 return;
2300 DumpDeclContextHiearchy(decl->getDeclContext());
2301
2302 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2303 if (record_decl) {
2304 printf("%20s: %s%s\n", decl->getDeclKindName(),
2305 record_decl->getDeclName().getAsString().c_str(),
2306 record_decl->isInjectedClassName() ? " (injected class name)" : "");
2307
2308 } else {
2309 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2310 if (named_decl) {
2311 printf("%20s: %s\n", decl->getDeclKindName(),
2312 named_decl->getDeclName().getAsString().c_str());
2313 } else {
2314 printf("%20s\n", decl->getDeclKindName());
2315 }
2316 }
2317}
2318
2319bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2320 clang::Decl *rhs_decl) {
2321 if (lhs_decl && rhs_decl) {
2322 //----------------------------------------------------------------------
2323 // Make sure the decl kinds match first
2324 //----------------------------------------------------------------------
2325 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2326 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
2327
2328 if (lhs_decl_kind == rhs_decl_kind) {
2329 //------------------------------------------------------------------
2330 // Now check that the decl contexts kinds are all equivalent before we
2331 // have to check any names of the decl contexts...
2332 //------------------------------------------------------------------
2333 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2334 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2335 if (lhs_decl_ctx && rhs_decl_ctx) {
2336 while (1) {
2337 if (lhs_decl_ctx && rhs_decl_ctx) {
2338 const clang::Decl::Kind lhs_decl_ctx_kind =
2339 lhs_decl_ctx->getDeclKind();
2340 const clang::Decl::Kind rhs_decl_ctx_kind =
2341 rhs_decl_ctx->getDeclKind();
2342 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2343 lhs_decl_ctx = lhs_decl_ctx->getParent();
2344 rhs_decl_ctx = rhs_decl_ctx->getParent();
2345
2346 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2347 break;
2348 } else
2349 return false;
2350 } else
2351 return false;
2352 }
2353
2354 //--------------------------------------------------------------
2355 // Now make sure the name of the decls match
2356 //--------------------------------------------------------------
2357 clang::NamedDecl *lhs_named_decl =
2358 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2359 clang::NamedDecl *rhs_named_decl =
2360 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2361 if (lhs_named_decl && rhs_named_decl) {
2362 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2363 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2364 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2365 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2366 return false;
2367 } else
2368 return false;
2369 } else
2370 return false;
2371
2372 //--------------------------------------------------------------
2373 // We know that the decl context kinds all match, so now we need to
2374 // make sure the names match as well
2375 //--------------------------------------------------------------
2376 lhs_decl_ctx = lhs_decl->getDeclContext();
2377 rhs_decl_ctx = rhs_decl->getDeclContext();
2378 while (1) {
2379 switch (lhs_decl_ctx->getDeclKind()) {
2380 case clang::Decl::TranslationUnit:
2381 // We don't care about the translation unit names
2382 return true;
2383 default: {
2384 clang::NamedDecl *lhs_named_decl =
2385 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2386 clang::NamedDecl *rhs_named_decl =
2387 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2388 if (lhs_named_decl && rhs_named_decl) {
2389 clang::DeclarationName lhs_decl_name =
2390 lhs_named_decl->getDeclName();
2391 clang::DeclarationName rhs_decl_name =
2392 rhs_named_decl->getDeclName();
2393 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2394 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2395 return false;
2396 } else
2397 return false;
2398 } else
2399 return false;
2400 } break;
2401 }
2402 lhs_decl_ctx = lhs_decl_ctx->getParent();
2403 rhs_decl_ctx = rhs_decl_ctx->getParent();
2404 }
2405 }
2406 }
2407 }
2408 return false;
2409}
2410bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2411 clang::Decl *decl) {
2412 if (!decl)
2413 return false;
2414
2415 ExternalASTSource *ast_source = ast->getExternalSource();
2416
2417 if (!ast_source)
2418 return false;
2419
2420 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2421 if (tag_decl->isCompleteDefinition())
2422 return true;
2423
2424 if (!tag_decl->hasExternalLexicalStorage())
2425 return false;
2426
2427 ast_source->CompleteType(tag_decl);
2428
2429 return !tag_decl->getTypeForDecl()->isIncompleteType();
2430 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2431 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2432 if (objc_interface_decl->getDefinition())
2433 return true;
2434
2435 if (!objc_interface_decl->hasExternalLexicalStorage())
2436 return false;
2437
2438 ast_source->CompleteType(objc_interface_decl);
2439
2440 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2441 } else {
2442 return false;
2443 }
2444}
2445
2446void ClangASTContext::SetMetadataAsUserID(const void *object,
2447 user_id_t user_id) {
2448 ClangASTMetadata meta_data;
2449 meta_data.SetUserID(user_id);
2450 SetMetadata(object, meta_data);
2451}
2452
2453void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2454 ClangASTMetadata &metadata) {
2455 ClangExternalASTSourceCommon *external_source =
2456 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2457
2458 if (external_source)
2459 external_source->SetMetadata(object, metadata);
2460}
2461
2462ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2463 const void *object) {
2464 ClangExternalASTSourceCommon *external_source =
2465 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2466
2467 if (external_source && external_source->HasMetadata(object))
2468 return external_source->GetMetadata(object);
2469 else
2470 return nullptr;
2471}
2472
2473clang::DeclContext *
2474ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2475 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2476}
2477
2478clang::DeclContext *
2479ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2480 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2481}
2482
2483bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2484 int kind) const {
2485 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2486 if (clang_type) {
2487 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2488 if (tag_type) {
2489 clang::TagDecl *tag_decl =
2490 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2491 if (tag_decl) {
2492 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2493 return true;
2494 }
2495 }
2496 }
2497 return false;
2498}
2499
2500bool ClangASTContext::SetDefaultAccessForRecordFields(
2501 clang::RecordDecl *record_decl, int default_accessibility,
2502 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2503 if (record_decl) {
2504 uint32_t field_idx;
2505 clang::RecordDecl::field_iterator field, field_end;
2506 for (field = record_decl->field_begin(),
2507 field_end = record_decl->field_end(), field_idx = 0;
2508 field != field_end; ++field, ++field_idx) {
2509 // If no accessibility was assigned, assign the correct one
2510 if (field_idx < num_assigned_accessibilities &&
2511 assigned_accessibilities[field_idx] == clang::AS_none)
2512 field->setAccess((clang::AccessSpecifier)default_accessibility);
2513 }
2514 return true;
2515 }
2516 return false;
2517}
2518
2519clang::DeclContext *
2520ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2521 return GetDeclContextForType(ClangUtil::GetQualType(type));
2522}
2523
2524clang::DeclContext *
2525ClangASTContext::GetDeclContextForType(clang::QualType type) {
2526 if (type.isNull())
2527 return nullptr;
2528
2529 clang::QualType qual_type = type.getCanonicalType();
2530 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2531 switch (type_class) {
2532 case clang::Type::ObjCInterface:
2533 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2534 ->getInterface();
2535 case clang::Type::ObjCObjectPointer:
2536 return GetDeclContextForType(
2537 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2538 ->getPointeeType());
2539 case clang::Type::Record:
2540 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2541 case clang::Type::Enum:
2542 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2543 case clang::Type::Typedef:
2544 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2545 ->getDecl()
2546 ->getUnderlyingType());
2547 case clang::Type::Auto:
2548 return GetDeclContextForType(
2549 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2550 case clang::Type::Elaborated:
2551 return GetDeclContextForType(
2552 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2553 case clang::Type::Paren:
2554 return GetDeclContextForType(
2555 llvm::cast<clang::ParenType>(qual_type)->desugar());
2556 default:
2557 break;
2558 }
2559 // No DeclContext in this type...
2560 return nullptr;
2561}
2562
2563static bool GetCompleteQualType(clang::ASTContext *ast,
2564 clang::QualType qual_type,
2565 bool allow_completion = true) {
2566 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2567 switch (type_class) {
2568 case clang::Type::ConstantArray:
2569 case clang::Type::IncompleteArray:
2570 case clang::Type::VariableArray: {
2571 const clang::ArrayType *array_type =
2572 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2573
2574 if (array_type)
2575 return GetCompleteQualType(ast, array_type->getElementType(),
2576 allow_completion);
2577 } break;
2578 case clang::Type::Record: {
2579 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2580 if (cxx_record_decl) {
2581 if (cxx_record_decl->hasExternalLexicalStorage()) {
2582 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2583 const bool fields_loaded =
2584 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2585 if (is_complete && fields_loaded)
2586 return true;
2587
2588 if (!allow_completion)
2589 return false;
2590
2591 // Call the field_begin() accessor to for it to use the external source
2592 // to load the fields...
2593 clang::ExternalASTSource *external_ast_source =
2594 ast->getExternalSource();
2595 if (external_ast_source) {
2596 external_ast_source->CompleteType(cxx_record_decl);
2597 if (cxx_record_decl->isCompleteDefinition()) {
2598 cxx_record_decl->field_begin();
2599 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2600 }
2601 }
2602 }
2603 }
2604 const clang::TagType *tag_type =
2605 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2606 return !tag_type->isIncompleteType();
2607 } break;
2608
2609 case clang::Type::Enum: {
2610 const clang::TagType *tag_type =
2611 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2612 if (tag_type) {
2613 clang::TagDecl *tag_decl = tag_type->getDecl();
2614 if (tag_decl) {
2615 if (tag_decl->getDefinition())
2616 return true;
2617
2618 if (!allow_completion)
2619 return false;
2620
2621 if (tag_decl->hasExternalLexicalStorage()) {
2622 if (ast) {
2623 clang::ExternalASTSource *external_ast_source =
2624 ast->getExternalSource();
2625 if (external_ast_source) {
2626 external_ast_source->CompleteType(tag_decl);
2627 return !tag_type->isIncompleteType();
2628 }
2629 }
2630 }
2631 return false;
2632 }
2633 }
2634
2635 } break;
2636 case clang::Type::ObjCObject:
2637 case clang::Type::ObjCInterface: {
2638 const clang::ObjCObjectType *objc_class_type =
2639 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2640 if (objc_class_type) {
2641 clang::ObjCInterfaceDecl *class_interface_decl =
2642 objc_class_type->getInterface();
2643 // We currently can't complete objective C types through the newly added
2644 // ASTContext because it only supports TagDecl objects right now...
2645 if (class_interface_decl) {
2646 if (class_interface_decl->getDefinition())
2647 return true;
2648
2649 if (!allow_completion)
2650 return false;
2651
2652 if (class_interface_decl->hasExternalLexicalStorage()) {
2653 if (ast) {
2654 clang::ExternalASTSource *external_ast_source =
2655 ast->getExternalSource();
2656 if (external_ast_source) {
2657 external_ast_source->CompleteType(class_interface_decl);
2658 return !objc_class_type->isIncompleteType();
2659 }
2660 }
2661 }
2662 return false;
2663 }
2664 }
2665 } break;
2666
2667 case clang::Type::Typedef:
2668 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2669 ->getDecl()
2670 ->getUnderlyingType(),
2671 allow_completion);
2672
2673 case clang::Type::Auto:
2674 return GetCompleteQualType(
2675 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2676 allow_completion);
2677
2678 case clang::Type::Elaborated:
2679 return GetCompleteQualType(
2680 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2681 allow_completion);
2682
2683 case clang::Type::Paren:
2684 return GetCompleteQualType(
2685 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2686 allow_completion);
2687
2688 case clang::Type::Attributed:
2689 return GetCompleteQualType(
2690 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2691 allow_completion);
2692
2693 default:
2694 break;
2695 }
2696
2697 return true;
2698}
2699
2700static clang::ObjCIvarDecl::AccessControl
2701ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2702 switch (access) {
2703 case eAccessNone:
2704 return clang::ObjCIvarDecl::None;
2705 case eAccessPublic:
2706 return clang::ObjCIvarDecl::Public;
2707 case eAccessPrivate:
2708 return clang::ObjCIvarDecl::Private;
2709 case eAccessProtected:
2710 return clang::ObjCIvarDecl::Protected;
2711 case eAccessPackage:
2712 return clang::ObjCIvarDecl::Package;
2713 }
2714 return clang::ObjCIvarDecl::None;
2715}
2716
2717//----------------------------------------------------------------------
2718// Tests
2719//----------------------------------------------------------------------
2720
2721bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2722 clang::QualType qual_type(GetCanonicalQualType(type));
2723
2724 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2725 switch (type_class) {
2726 case clang::Type::IncompleteArray:
2727 case clang::Type::VariableArray:
2728 case clang::Type::ConstantArray:
2729 case clang::Type::ExtVector:
2730 case clang::Type::Vector:
2731 case clang::Type::Record:
2732 case clang::Type::ObjCObject:
2733 case clang::Type::ObjCInterface:
2734 return true;
2735 case clang::Type::Auto:
2736 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2737 ->getDeducedType()
2738 .getAsOpaquePtr());
2739 case clang::Type::Elaborated:
2740 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2741 ->getNamedType()
2742 .getAsOpaquePtr());
2743 case clang::Type::Typedef:
2744 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2745 ->getDecl()
2746 ->getUnderlyingType()
2747 .getAsOpaquePtr());
2748 case clang::Type::Paren:
2749 return IsAggregateType(
2750 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2751 default:
2752 break;
2753 }
2754 // The clang type does have a value
2755 return false;
2756}
2757
2758bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2759 clang::QualType qual_type(GetCanonicalQualType(type));
2760
2761 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2762 switch (type_class) {
2763 case clang::Type::Record: {
2764 if (const clang::RecordType *record_type =
2765 llvm::dyn_cast_or_null<clang::RecordType>(
2766 qual_type.getTypePtrOrNull())) {
2767 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2768 return record_decl->isAnonymousStructOrUnion();
2769 }
2770 }
2771 break;
2772 }
2773 case clang::Type::Auto:
2774 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2775 ->getDeducedType()
2776 .getAsOpaquePtr());
2777 case clang::Type::Elaborated:
2778 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2779 ->getNamedType()
2780 .getAsOpaquePtr());
2781 case clang::Type::Typedef:
2782 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2783 ->getDecl()
2784 ->getUnderlyingType()
2785 .getAsOpaquePtr());
2786 case clang::Type::Paren:
2787 return IsAnonymousType(
2788 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2789 default:
2790 break;
2791 }
2792 // The clang type does have a value
2793 return false;
2794}
2795
2796bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2797 CompilerType *element_type_ptr,
2798 uint64_t *size, bool *is_incomplete) {
2799 clang::QualType qual_type(GetCanonicalQualType(type));
2800
2801 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2802 switch (type_class) {
2803 default:
2804 break;
2805
2806 case clang::Type::ConstantArray:
2807 if (element_type_ptr)
2808 element_type_ptr->SetCompilerType(
2809 getASTContext(),
2810 llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
2811 if (size)
2812 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2813 ->getSize()
2814 .getLimitedValue(ULLONG_MAX(9223372036854775807LL*2ULL+1ULL));
2815 if (is_incomplete)
2816 *is_incomplete = false;
2817 return true;
2818
2819 case clang::Type::IncompleteArray:
2820 if (element_type_ptr)
2821 element_type_ptr->SetCompilerType(
2822 getASTContext(),
2823 llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2824 if (size)
2825 *size = 0;
2826 if (is_incomplete)
2827 *is_incomplete = true;
2828 return true;
2829
2830 case clang::Type::VariableArray:
2831 if (element_type_ptr)
2832 element_type_ptr->SetCompilerType(
2833 getASTContext(),
2834 llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2835 if (size)
2836 *size = 0;
2837 if (is_incomplete)
2838 *is_incomplete = false;
2839 return true;
2840
2841 case clang::Type::DependentSizedArray:
2842 if (element_type_ptr)
2843 element_type_ptr->SetCompilerType(
2844 getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)
2845 ->getElementType());
2846 if (size)
2847 *size = 0;
2848 if (is_incomplete)
2849 *is_incomplete = false;
2850 return true;
2851
2852 case clang::Type::Typedef:
2853 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2854 ->getDecl()
2855 ->getUnderlyingType()
2856 .getAsOpaquePtr(),
2857 element_type_ptr, size, is_incomplete);
2858 case clang::Type::Auto:
2859 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2860 ->getDeducedType()
2861 .getAsOpaquePtr(),
2862 element_type_ptr, size, is_incomplete);
2863 case clang::Type::Elaborated:
2864 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2865 ->getNamedType()
2866 .getAsOpaquePtr(),
2867 element_type_ptr, size, is_incomplete);
2868 case clang::Type::Paren:
2869 return IsArrayType(
2870 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2871 element_type_ptr, size, is_incomplete);
2872 }
2873 if (element_type_ptr)
2874 element_type_ptr->Clear();
2875 if (size)
2876 *size = 0;
2877 if (is_incomplete)
2878 *is_incomplete = false;
2879 return false;
2880}
2881
2882bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
2883 CompilerType *element_type, uint64_t *size) {
2884 clang::QualType qual_type(GetCanonicalQualType(type));
2885
2886 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2887 switch (type_class) {
2888 case clang::Type::Vector: {
2889 const clang::VectorType *vector_type =
2890 qual_type->getAs<clang::VectorType>();
2891 if (vector_type) {
2892 if (size)
2893 *size = vector_type->getNumElements();
2894 if (element_type)
2895 *element_type =
2896 CompilerType(getASTContext(), vector_type->getElementType());
2897 }
2898 return true;
2899 } break;
2900 case clang::Type::ExtVector: {
2901 const clang::ExtVectorType *ext_vector_type =
2902 qual_type->getAs<clang::ExtVectorType>();
2903 if (ext_vector_type) {
2904 if (size)
2905 *size = ext_vector_type->getNumElements();
2906 if (element_type)
2907 *element_type =
2908 CompilerType(getASTContext(), ext_vector_type->getElementType());
2909 }
2910 return true;
2911 }
2912 default:
2913 break;
2914 }
2915 return false;
2916}
2917
2918bool ClangASTContext::IsRuntimeGeneratedType(
2919 lldb::opaque_compiler_type_t type) {
2920 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
2921 ->GetDeclContextForType(GetQualType(type));
2922 if (!decl_ctx)
2923 return false;
2924
2925 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2926 return false;
2927
2928 clang::ObjCInterfaceDecl *result_iface_decl =
2929 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2930
2931 ClangASTMetadata *ast_metadata =
2932 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2933 if (!ast_metadata)
2934 return false;
2935 return (ast_metadata->GetISAPtr() != 0);
2936}
2937
2938bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
2939 return GetQualType(type).getUnqualifiedType()->isCharType();
2940}
2941
2942bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
2943 const bool allow_completion = false;
2944 return GetCompleteQualType(getASTContext(), GetQualType(type),
2945 allow_completion);
2946}
2947
2948bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
2949 return GetQualType(type).isConstQualified();
2950}
2951
2952bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
2953 uint32_t &length) {
2954 CompilerType pointee_or_element_clang_type;
2955 length = 0;
2956 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
2957
2958 if (!pointee_or_element_clang_type.IsValid())
2959 return false;
2960
2961 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
2962 if (pointee_or_element_clang_type.IsCharType()) {
2963 if (type_flags.Test(eTypeIsArray)) {
2964 // We know the size of the array and it could be a C string since it is
2965 // an array of characters
2966 length = llvm::cast<clang::ConstantArrayType>(
2967 GetCanonicalQualType(type).getTypePtr())
2968 ->getSize()
2969 .getLimitedValue();
2970 }
2971 return true;
2972 }
2973 }
2974 return false;
2975}
2976
2977bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
2978 bool *is_variadic_ptr) {
2979 if (type) {
2980 clang::QualType qual_type(GetCanonicalQualType(type));
2981
2982 if (qual_type->isFunctionType()) {
2983 if (is_variadic_ptr) {
2984 const clang::FunctionProtoType *function_proto_type =
2985 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2986 if (function_proto_type)
2987 *is_variadic_ptr = function_proto_type->isVariadic();
2988 else
2989 *is_variadic_ptr = false;
2990 }
2991 return true;
2992 }
2993
2994 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2995 switch (type_class) {
2996 default:
2997 break;
2998 case clang::Type::Typedef:
2999 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
3000 ->getDecl()
3001 ->getUnderlyingType()
3002 .getAsOpaquePtr(),
3003 nullptr);
3004 case clang::Type::Auto:
3005 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
3006 ->getDeducedType()
3007 .getAsOpaquePtr(),
3008 nullptr);
3009 case clang::Type::Elaborated:
3010 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
3011 ->getNamedType()
3012 .getAsOpaquePtr(),
3013 nullptr);
3014 case clang::Type::Paren:
3015 return IsFunctionType(
3016 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3017 nullptr);
3018 case clang::Type::LValueReference:
3019 case clang::Type::RValueReference: {
3020 const clang::ReferenceType *reference_type =
3021 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3022 if (reference_type)
3023 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
3024 nullptr);
3025 } break;
3026 }
3027 }
3028 return false;
3029}
3030
3031// Used to detect "Homogeneous Floating-point Aggregates"
3032uint32_t
3033ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
3034 CompilerType *base_type_ptr) {
3035 if (!type)
3036 return 0;
3037
3038 clang::QualType qual_type(GetCanonicalQualType(type));
3039 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3040 switch (type_class) {
3041 case clang::Type::Record:
3042 if (GetCompleteType(type)) {
3043 const clang::CXXRecordDecl *cxx_record_decl =
3044 qual_type->getAsCXXRecordDecl();
3045 if (cxx_record_decl) {
3046 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3047 return 0;
3048 }
3049 const clang::RecordType *record_type =
3050 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3051 if (record_type) {
3052 const clang::RecordDecl *record_decl = record_type->getDecl();
3053 if (record_decl) {
3054 // We are looking for a structure that contains only floating point
3055 // types
3056 clang::RecordDecl::field_iterator field_pos,
3057 field_end = record_decl->field_end();
3058 uint32_t num_fields = 0;
3059 bool is_hva = false;
3060 bool is_hfa = false;
3061 clang::QualType base_qual_type;
3062 uint64_t base_bitwidth = 0;
3063 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3064 ++field_pos) {
3065 clang::QualType field_qual_type = field_pos->getType();
3066 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
3067 if (field_qual_type->isFloatingType()) {
3068 if (field_qual_type->isComplexType())
3069 return 0;
3070 else {
3071 if (num_fields == 0)
3072 base_qual_type = field_qual_type;
3073 else {
3074 if (is_hva)
3075 return 0;
3076 is_hfa = true;
3077 if (field_qual_type.getTypePtr() !=
3078 base_qual_type.getTypePtr())
3079 return 0;
3080 }
3081 }
3082 } else if (field_qual_type->isVectorType() ||
3083 field_qual_type->isExtVectorType()) {
3084 if (num_fields == 0) {
3085 base_qual_type = field_qual_type;
3086 base_bitwidth = field_bitwidth;
3087 } else {
3088 if (is_hfa)
3089 return 0;
3090 is_hva = true;
3091 if (base_bitwidth != field_bitwidth)
3092 return 0;
3093 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3094 return 0;
3095 }
3096 } else
3097 return 0;
3098 ++num_fields;
3099 }
3100 if (base_type_ptr)
3101 *base_type_ptr = CompilerType(getASTContext(), base_qual_type);
3102 return num_fields;
3103 }
3104 }
3105 }
3106 break;
3107
3108 case clang::Type::Typedef:
3109 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3110 ->getDecl()
3111 ->getUnderlyingType()
3112 .getAsOpaquePtr(),
3113 base_type_ptr);
3114
3115 case clang::Type::Auto:
3116 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3117 ->getDeducedType()
3118 .getAsOpaquePtr(),
3119 base_type_ptr);
3120
3121 case clang::Type::Elaborated:
3122 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3123 ->getNamedType()
3124 .getAsOpaquePtr(),
3125 base_type_ptr);
3126 default:
3127 break;
3128 }
3129 return 0;
3130}
3131
3132size_t ClangASTContext::GetNumberOfFunctionArguments(
3133 lldb::opaque_compiler_type_t type) {
3134 if (type) {
3135 clang::QualType qual_type(GetCanonicalQualType(type));
3136 const clang::FunctionProtoType *func =
3137 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3138 if (func)
3139 return func->getNumParams();
3140 }
3141 return 0;
3142}
3143
3144CompilerType
3145ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3146 const size_t index) {
3147 if (type) {
3148 clang::QualType qual_type(GetQualType(type));
3149 const clang::FunctionProtoType *func =
3150 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3151 if (func) {
3152 if (index < func->getNumParams())
3153 return CompilerType(getASTContext(), func->getParamType(index));
3154 }
3155 }
3156 return CompilerType();
3157}
3158
3159bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3160 if (type) {
3161 clang::QualType qual_type(GetCanonicalQualType(type));
3162
3163 if (qual_type->isFunctionPointerType())
3164 return true;
3165
3166 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3167 switch (type_class) {
3168 default:
3169 break;
3170 case clang::Type::Typedef:
3171 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3172 ->getDecl()
3173 ->getUnderlyingType()
3174 .getAsOpaquePtr());
3175 case clang::Type::Auto:
3176 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3177 ->getDeducedType()
3178 .getAsOpaquePtr());
3179 case clang::Type::Elaborated:
3180 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3181 ->getNamedType()
3182 .getAsOpaquePtr());
3183 case clang::Type::Paren:
3184 return IsFunctionPointerType(
3185 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3186
3187 case clang::Type::LValueReference:
3188 case clang::Type::RValueReference: {
3189 const clang::ReferenceType *reference_type =
3190 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3191 if (reference_type)
3192 return IsFunctionPointerType(
3193 reference_type->getPointeeType().getAsOpaquePtr());
3194 } break;
3195 }
3196 }
3197 return false;
3198}
3199
3200bool ClangASTContext::IsBlockPointerType(
3201 lldb::opaque_compiler_type_t type,
3202 CompilerType *function_pointer_type_ptr) {
3203 if (type) {
3204 clang::QualType qual_type(GetCanonicalQualType(type));
3205
3206 if (qual_type->isBlockPointerType()) {
3207 if (function_pointer_type_ptr) {
3208 const clang::BlockPointerType *block_pointer_type =
3209 qual_type->getAs<clang::BlockPointerType>();
3210 QualType pointee_type = block_pointer_type->getPointeeType();
3211 QualType function_pointer_type = m_ast_ap->getPointerType(pointee_type);
3212 *function_pointer_type_ptr =
3213 CompilerType(getASTContext(), function_pointer_type);
3214 }
3215 return true;
3216 }
3217
3218 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3219 switch (type_class) {
3220 default:
3221 break;
3222 case clang::Type::Typedef:
3223 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3224 ->getDecl()
3225 ->getUnderlyingType()
3226 .getAsOpaquePtr(),
3227 function_pointer_type_ptr);
3228 case clang::Type::Auto:
3229 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3230 ->getDeducedType()
3231 .getAsOpaquePtr(),
3232 function_pointer_type_ptr);
3233 case clang::Type::Elaborated:
3234 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3235 ->getNamedType()
3236 .getAsOpaquePtr(),
3237 function_pointer_type_ptr);
3238 case clang::Type::Paren:
3239 return IsBlockPointerType(
3240 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3241 function_pointer_type_ptr);
3242
3243 case clang::Type::LValueReference:
3244 case clang::Type::RValueReference: {
3245 const clang::ReferenceType *reference_type =
3246 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3247 if (reference_type)
3248 return IsBlockPointerType(
3249 reference_type->getPointeeType().getAsOpaquePtr(),
3250 function_pointer_type_ptr);
3251 } break;
3252 }
3253 }
3254 return false;
3255}
3256
3257bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3258 bool &is_signed) {
3259 if (!type)
3260 return false;
3261
3262 clang::QualType qual_type(GetCanonicalQualType(type));
3263 const clang::BuiltinType *builtin_type =
3264 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3265
3266 if (builtin_type) {
3267 if (builtin_type->isInteger()) {
3268 is_signed = builtin_type->isSignedInteger();
3269 return true;
3270 }
3271 }
3272
3273 return false;
3274}
3275
3276bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3277 bool &is_signed) {
3278 if (type) {
3279 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3280 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3281
3282 if (enum_type) {
3283 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3284 is_signed);
3285 return true;
3286 }
3287 }
3288
3289 return false;
3290}
3291
3292bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3293 CompilerType *pointee_type) {
3294 if (type) {
3295 clang::QualType qual_type(GetCanonicalQualType(type));
3296 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3297 switch (type_class) {
3298 case clang::Type::Builtin:
3299 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3300 default:
3301 break;
3302 case clang::BuiltinType::ObjCId:
3303 case clang::BuiltinType::ObjCClass:
3304 return true;
3305 }
3306 return false;
3307 case clang::Type::ObjCObjectPointer:
3308 if (pointee_type)
3309 pointee_type->SetCompilerType(
3310 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3311 ->getPointeeType());
3312 return true;
3313 case clang::Type::BlockPointer:
3314 if (pointee_type)
3315 pointee_type->SetCompilerType(
3316 getASTContext(),
3317 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3318 return true;
3319 case clang::Type::Pointer:
3320 if (pointee_type)
3321 pointee_type->SetCompilerType(
3322 getASTContext(),
3323 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3324 return true;
3325 case clang::Type::MemberPointer:
3326 if (pointee_type)
3327 pointee_type->SetCompilerType(
3328 getASTContext(),
3329 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3330 return true;
3331 case clang::Type::Typedef:
3332 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3333 ->getDecl()
3334 ->getUnderlyingType()
3335 .getAsOpaquePtr(),
3336 pointee_type);
3337 case clang::Type::Auto:
3338 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3339 ->getDeducedType()
3340 .getAsOpaquePtr(),
3341 pointee_type);
3342 case clang::Type::Elaborated:
3343 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3344 ->getNamedType()
3345 .getAsOpaquePtr(),
3346 pointee_type);
3347 case clang::Type::Paren:
3348 return IsPointerType(
3349 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3350 pointee_type);
3351 default:
3352 break;
3353 }
3354 }
3355 if (pointee_type)
3356 pointee_type->Clear();
3357 return false;
3358}
3359
3360bool ClangASTContext::IsPointerOrReferenceType(
3361 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3362 if (type) {
3363 clang::QualType qual_type(GetCanonicalQualType(type));
3364 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3365 switch (type_class) {
3366 case clang::Type::Builtin:
3367 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3368 default:
3369 break;
3370 case clang::BuiltinType::ObjCId:
3371 case clang::BuiltinType::ObjCClass:
3372 return true;
3373 }
3374 return false;
3375 case clang::Type::ObjCObjectPointer:
3376 if (pointee_type)
3377 pointee_type->SetCompilerType(
3378 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3379 ->getPointeeType());
3380 return true;
3381 case clang::Type::BlockPointer:
3382 if (pointee_type)
3383 pointee_type->SetCompilerType(
3384 getASTContext(),
3385 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3386 return true;
3387 case clang::Type::Pointer:
3388 if (pointee_type)
3389 pointee_type->SetCompilerType(
3390 getASTContext(),
3391 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3392 return true;
3393 case clang::Type::MemberPointer:
3394 if (pointee_type)
3395 pointee_type->SetCompilerType(
3396 getASTContext(),
3397 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3398 return true;
3399 case clang::Type::LValueReference:
3400 if (pointee_type)
3401 pointee_type->SetCompilerType(
3402 getASTContext(),
3403 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3404 return true;
3405 case clang::Type::RValueReference:
3406 if (pointee_type)
3407 pointee_type->SetCompilerType(
3408 getASTContext(),
3409 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3410 return true;
3411 case clang::Type::Typedef:
3412 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3413 ->getDecl()
3414 ->getUnderlyingType()
3415 .getAsOpaquePtr(),
3416 pointee_type);
3417 case clang::Type::Auto:
3418 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3419 ->getDeducedType()
3420 .getAsOpaquePtr(),
3421 pointee_type);
3422 case clang::Type::Elaborated:
3423 return IsPointerOrReferenceType(
3424 llvm::cast<clang::ElaboratedType>(qual_type)
3425 ->getNamedType()
3426 .getAsOpaquePtr(),
3427 pointee_type);
3428 case clang::Type::Paren:
3429 return IsPointerOrReferenceType(
3430 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3431 pointee_type);
3432 default:
3433 break;
3434 }
3435 }
3436 if (pointee_type)
3437 pointee_type->Clear();
3438 return false;
3439}
3440
3441bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3442 CompilerType *pointee_type,
3443 bool *is_rvalue) {
3444 if (type) {
3445 clang::QualType qual_type(GetCanonicalQualType(type));
3446 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3447
3448 switch (type_class) {
3449 case clang::Type::LValueReference:
3450 if (pointee_type)
3451 pointee_type->SetCompilerType(
3452 getASTContext(),
3453 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3454 if (is_rvalue)
3455 *is_rvalue = false;
3456 return true;
3457 case clang::Type::RValueReference:
3458 if (pointee_type)
3459 pointee_type->SetCompilerType(
3460 getASTContext(),
3461 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3462 if (is_rvalue)
3463 *is_rvalue = true;
3464 return true;
3465 case clang::Type::Typedef:
3466 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3467 ->getDecl()
3468 ->getUnderlyingType()
3469 .getAsOpaquePtr(),
3470 pointee_type, is_rvalue);
3471 case clang::Type::Auto:
3472 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3473 ->getDeducedType()
3474 .getAsOpaquePtr(),
3475 pointee_type, is_rvalue);
3476 case clang::Type::Elaborated:
3477 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3478 ->getNamedType()
3479 .getAsOpaquePtr(),
3480 pointee_type, is_rvalue);
3481 case clang::Type::Paren:
3482 return IsReferenceType(
3483 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3484 pointee_type, is_rvalue);
3485
3486 default:
3487 break;
3488 }
3489 }
3490 if (pointee_type)
3491 pointee_type->Clear();
3492 return false;
3493}
3494
3495bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3496 uint32_t &count, bool &is_complex) {
3497 if (type) {
3498 clang::QualType qual_type(GetCanonicalQualType(type));
3499
3500 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3501 qual_type->getCanonicalTypeInternal())) {
3502 clang::BuiltinType::Kind kind = BT->getKind();
3503 if (kind >= clang::BuiltinType::Float &&
3504 kind <= clang::BuiltinType::LongDouble) {
3505 count = 1;
3506 is_complex = false;
3507 return true;
3508 }
3509 } else if (const clang::ComplexType *CT =
3510 llvm::dyn_cast<clang::ComplexType>(
3511 qual_type->getCanonicalTypeInternal())) {
3512 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3513 is_complex)) {
3514 count = 2;
3515 is_complex = true;
3516 return true;
3517 }
3518 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3519 qual_type->getCanonicalTypeInternal())) {
3520 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3521 is_complex)) {
3522 count = VT->getNumElements();
3523 is_complex = false;
3524 return true;
3525 }
3526 }
3527 }
3528 count = 0;
3529 is_complex = false;
3530 return false;
3531}
3532
3533bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3534 if (!type)
3535 return false;
3536
3537 clang::QualType qual_type(GetQualType(type));
3538 const clang::TagType *tag_type =
3539 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3540 if (tag_type) {
3541 clang::TagDecl *tag_decl = tag_type->getDecl();
3542 if (tag_decl)
3543 return tag_decl->isCompleteDefinition();
3544 return false;
3545 } else {
3546 const clang::ObjCObjectType *objc_class_type =
3547 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3548 if (objc_class_type) {
3549 clang::ObjCInterfaceDecl *class_interface_decl =
3550 objc_class_type->getInterface();
3551 if (class_interface_decl)
3552 return class_interface_decl->getDefinition() != nullptr;
3553 return false;
3554 }
3555 }
3556 return true;
3557}
3558
3559bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
3560 if (type) {
3561 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3562
3563 const clang::ObjCObjectPointerType *obj_pointer_type =
3564 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3565
3566 if (obj_pointer_type)
3567 return obj_pointer_type->isObjCClassType();
3568 }
3569 return false;
3570}
3571
3572bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3573 if (ClangUtil::IsClangType(type))
3574 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3575 return false;
3576}
3577
3578bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3579 if (!type)
3580 return false;
3581 clang::QualType qual_type(GetCanonicalQualType(type));
3582 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3583 return (type_class == clang::Type::Record);
3584}
3585
3586bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3587 if (!type)
3588 return false;
3589 clang::QualType qual_type(GetCanonicalQualType(type));
3590 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3591 return (type_class == clang::Type::Enum);
3592}
3593
3594bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3595 if (type) {
3596 clang::QualType qual_type(GetCanonicalQualType(type));
3597 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3598 switch (type_class) {
3599 case clang::Type::Record:
3600 if (GetCompleteType(type)) {
3601 const clang::RecordType *record_type =
3602 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3603 const clang::RecordDecl *record_decl = record_type->getDecl();
3604 if (record_decl) {
3605 const clang::CXXRecordDecl *cxx_record_decl =
3606 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3607 if (cxx_record_decl)
3608 return cxx_record_decl->isPolymorphic();
3609 }
3610 }
3611 break;
3612
3613 default:
3614 break;
3615 }
3616 }
3617 return false;
3618}
3619
3620bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3621 CompilerType *dynamic_pointee_type,
3622 bool check_cplusplus,
3623 bool check_objc) {
3624 clang::QualType pointee_qual_type;
3625 if (type) {
3626 clang::QualType qual_type(GetCanonicalQualType(type));
3627 bool success = false;
3628 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3629 switch (type_class) {
3630 case clang::Type::Builtin:
3631 if (check_objc &&
3632 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3633 clang::BuiltinType::ObjCId) {
3634 if (dynamic_pointee_type)
3635 dynamic_pointee_type->SetCompilerType(this, type);
3636 return true;
3637 }
3638 break;
3639
3640 case clang::Type::ObjCObjectPointer:
3641 if (check_objc) {
3642 if (auto objc_pointee_type =
3643 qual_type->getPointeeType().getTypePtrOrNull()) {
3644 if (auto objc_object_type =
3645 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3646 objc_pointee_type)) {
3647 if (objc_object_type->isObjCClass())
3648 return false;
3649 }
3650 }
3651 if (dynamic_pointee_type)
3652 dynamic_pointee_type->SetCompilerType(
3653 getASTContext(),
3654 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3655 ->getPointeeType());
3656 return true;
3657 }
3658 break;
3659
3660 case clang::Type::Pointer:
3661 pointee_qual_type =
3662 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3663 success = true;
3664 break;
3665
3666 case clang::Type::LValueReference:
3667 case clang::Type::RValueReference:
3668 pointee_qual_type =
3669 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3670 success = true;
3671 break;
3672
3673 case clang::Type::Typedef:
3674 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3675 ->getDecl()
3676 ->getUnderlyingType()
3677 .getAsOpaquePtr(),
3678 dynamic_pointee_type, check_cplusplus,
3679 check_objc);
3680
3681 case clang::Type::Auto:
3682 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3683 ->getDeducedType()
3684 .getAsOpaquePtr(),
3685 dynamic_pointee_type, check_cplusplus,
3686 check_objc);
3687
3688 case clang::Type::Elaborated:
3689 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3690 ->getNamedType()
3691 .getAsOpaquePtr(),
3692 dynamic_pointee_type, check_cplusplus,
3693 check_objc);
3694
3695 case clang::Type::Paren:
3696 return IsPossibleDynamicType(
3697 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3698 dynamic_pointee_type, check_cplusplus, check_objc);
3699 default:
3700 break;
3701 }
3702
3703 if (success) {
3704 // Check to make sure what we are pointing too is a possible dynamic C++
3705 // type We currently accept any "void *" (in case we have a class that
3706 // has been watered down to an opaque pointer) and virtual C++ classes.
3707 const clang::Type::TypeClass pointee_type_class =
3708 pointee_qual_type.getCanonicalType()->getTypeClass();
3709 switch (pointee_type_class) {
3710 case clang::Type::Builtin:
3711 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3712 case clang::BuiltinType::UnknownAny:
3713 case clang::BuiltinType::Void:
3714 if (dynamic_pointee_type)
3715 dynamic_pointee_type->SetCompilerType(getASTContext(),
3716 pointee_qual_type);
3717 return true;
3718 default:
3719 break;
3720 }
3721 break;
3722
3723 case clang::Type::Record:
3724 if (check_cplusplus) {
3725 clang::CXXRecordDecl *cxx_record_decl =
3726 pointee_qual_type->getAsCXXRecordDecl();
3727 if (cxx_record_decl) {
3728 bool is_complete = cxx_record_decl->isCompleteDefinition();
3729
3730 if (is_complete)
3731 success = cxx_record_decl->isDynamicClass();
3732 else {
3733 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3734 getASTContext(), cxx_record_decl);
3735 if (metadata)
3736 success = metadata->GetIsDynamicCXXType();
3737 else {
3738 is_complete = CompilerType(getASTContext(), pointee_qual_type)
3739 .GetCompleteType();
3740 if (is_complete)
3741 success = cxx_record_decl->isDynamicClass();
3742 else
3743 success = false;
3744 }
3745 }
3746
3747 if (success) {
3748 if (dynamic_pointee_type)
3749 dynamic_pointee_type->SetCompilerType(getASTContext(),
3750 pointee_qual_type);
3751 return true;
3752 }
3753 }
3754 }
3755 break;
3756
3757 case clang::Type::ObjCObject:
3758 case clang::Type::ObjCInterface:
3759 if (check_objc) {
3760 if (dynamic_pointee_type)
3761 dynamic_pointee_type->SetCompilerType(getASTContext(),
3762 pointee_qual_type);
3763 return true;
3764 }
3765 break;
3766
3767 default:
3768 break;
3769 }
3770 }
3771 }
3772 if (dynamic_pointee_type)
3773 dynamic_pointee_type->Clear();
3774 return false;
3775}
3776
3777bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3778 if (!type)
3779 return false;
3780
3781 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3782}
3783
3784bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3785 if (!type)
3786 return false;
3787 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3788}
3789
3790bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3791 if (!type)
3792 return false;
3793 return GetCanonicalQualType(type)->isVoidType();
3794}
3795
3796bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3797 return ClangASTContextSupportsLanguage(language);
3798}
3799
3800bool ClangASTContext::GetCXXClassName(const CompilerType &type,
3801 std::string &class_name) {
3802 if (type) {
3803 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3804 if (!qual_type.isNull()) {
3805 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3806 if (cxx_record_decl) {
3807 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3808 return true;
3809 }
3810 }
3811 }
3812 class_name.clear();
3813 return false;
3814}
3815
3816bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3817 if (!type)
3818 return false;
3819
3820 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3821 if (!qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr)
3822 return true;
3823 return false;
3824}
3825
3826bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3827 if (!type)
3828 return false;
3829 clang::QualType qual_type(GetCanonicalQualType(type));
3830 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3831 if (tag_type)
3832 return tag_type->isBeingDefined();
3833 return false;
3834}
3835
3836bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3837 CompilerType *class_type_ptr) {
3838 if (!type)
3839 return false;
3840
3841 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3842
3843 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3844 if (class_type_ptr) {
3845 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3846 const clang::ObjCObjectPointerType *obj_pointer_type =
3847 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3848 if (obj_pointer_type == nullptr)
3849 class_type_ptr->Clear();
3850 else
3851 class_type_ptr->SetCompilerType(
3852 type.GetTypeSystem(),
3853 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3854 .getAsOpaquePtr());
3855 }
3856 }
3857 return true;
3858 }
3859 if (class_type_ptr)
3860 class_type_ptr->Clear();
3861 return false;
3862}
3863
3864bool ClangASTContext::GetObjCClassName(const CompilerType &type,
3865 std::string &class_name) {
3866 if (!type)
3867 return false;
3868
3869 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3870
3871 const clang::ObjCObjectType *object_type =
3872 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3873 if (object_type) {
3874 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3875 if (interface) {
3876 class_name = interface->getNameAsString();
3877 return true;
3878 }
3879 }
3880 return false;
3881}
3882
3883//----------------------------------------------------------------------
3884// Type Completion
3885//----------------------------------------------------------------------
3886
3887bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
3888 if (!type)
3889 return false;
3890 const bool allow_completion = true;
3891 return GetCompleteQualType(getASTContext(), GetQualType(type),
3892 allow_completion);
3893}
3894
3895ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
3896 std::string type_name;
3897 if (type) {
3898 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
3899 clang::QualType qual_type(GetQualType(type));
3900 printing_policy.SuppressTagKeyword = true;
3901 const clang::TypedefType *typedef_type =
3902 qual_type->getAs<clang::TypedefType>();
3903 if (typedef_type) {
3904 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3905 type_name = typedef_decl->getQualifiedNameAsString();
3906 } else {
3907 type_name = qual_type.getAsString(printing_policy);
3908 }
3909 }
3910 return ConstString(type_name);
3911}
3912
3913uint32_t
3914ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
3915 CompilerType *pointee_or_element_clang_type) {
3916 if (!type)
3917 return 0;
3918
3919 if (pointee_or_element_clang_type)
3920 pointee_or_element_clang_type->Clear();
3921
3922 clang::QualType qual_type(GetQualType(type));
3923
3924 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3925 switch (type_class) {
3926 case clang::Type::Attributed:
3927 return GetTypeInfo(
3928 qual_type->getAs<clang::AttributedType>()
3929 ->getModifiedType().getAsOpaquePtr(),
3930 pointee_or_element_clang_type);
3931 case clang::Type::Builtin: {
3932 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
3933 qual_type->getCanonicalTypeInternal());
3934
3935 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3936 switch (builtin_type->getKind()) {
3937 case clang::BuiltinType::ObjCId:
3938 case clang::BuiltinType::ObjCClass:
3939 if (pointee_or_element_clang_type)
3940 pointee_or_element_clang_type->SetCompilerType(
3941 getASTContext(), getASTContext()->ObjCBuiltinClassTy);
3942 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3943 break;
3944
3945 case clang::BuiltinType::ObjCSel:
3946 if (pointee_or_element_clang_type)
3947 pointee_or_element_clang_type->SetCompilerType(getASTContext(),
3948 getASTContext()->CharTy);
3949 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3950 break;
3951
3952 case clang::BuiltinType::Bool:
3953 case clang::BuiltinType::Char_U:
3954 case clang::BuiltinType::UChar:
3955 case clang::BuiltinType::WChar_U:
3956 case clang::BuiltinType::Char16:
3957 case clang::BuiltinType::Char32:
3958 case clang::BuiltinType::UShort:
3959 case clang::BuiltinType::UInt:
3960 case clang::BuiltinType::ULong:
3961 case clang::BuiltinType::ULongLong:
3962 case clang::BuiltinType::UInt128:
3963 case clang::BuiltinType::Char_S:
3964 case clang::BuiltinType::SChar:
3965 case clang::BuiltinType::WChar_S:
3966 case clang::BuiltinType::Short:
3967 case clang::BuiltinType::Int:
3968 case clang::BuiltinType::Long:
3969 case clang::BuiltinType::LongLong:
3970 case clang::BuiltinType::Int128:
3971 case clang::BuiltinType::Float:
3972 case clang::BuiltinType::Double:
3973 case clang::BuiltinType::LongDouble:
3974 builtin_type_flags |= eTypeIsScalar;
3975 if (builtin_type->isInteger()) {
3976 builtin_type_flags |= eTypeIsInteger;
3977 if (builtin_type->isSignedInteger())
3978 builtin_type_flags |= eTypeIsSigned;
3979 } else if (builtin_type->isFloatingPoint())
3980 builtin_type_flags |= eTypeIsFloat;
3981 break;
3982 default:
3983 break;
3984 }
3985 return builtin_type_flags;
3986 }
3987
3988 case clang::Type::BlockPointer:
3989 if (pointee_or_element_clang_type)
3990 pointee_or_element_clang_type->SetCompilerType(
3991 getASTContext(), qual_type->getPointeeType());
3992 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3993
3994 case clang::Type::Complex: {
3995 uint32_t complex_type_flags =
3996 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3997 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
3998 qual_type->getCanonicalTypeInternal());
3999 if (complex_type) {
4000 clang::QualType complex_element_type(complex_type->getElementType());
4001 if (complex_element_type->isIntegerType())
4002 complex_type_flags |= eTypeIsFloat;
4003 else if (complex_element_type->isFloatingType())
4004 complex_type_flags |= eTypeIsInteger;
4005 }
4006 return complex_type_flags;
4007 } break;
4008
4009 case clang::Type::ConstantArray:
4010 case clang::Type::DependentSizedArray:
4011 case clang::Type::IncompleteArray:
4012 case clang::Type::VariableArray:
4013 if (pointee_or_element_clang_type)
4014 pointee_or_element_clang_type->SetCompilerType(
4015 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
4016 ->getElementType());
4017 return eTypeHasChildren | eTypeIsArray;
4018
4019 case clang::Type::DependentName:
4020 return 0;
4021 case clang::Type::DependentSizedExtVector:
4022 return eTypeHasChildren | eTypeIsVector;
4023 case clang::Type::DependentTemplateSpecialization:
4024 return eTypeIsTemplate;
4025 case clang::Type::Decltype:
4026 return CompilerType(
4027 getASTContext(),
4028 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
4029 .GetTypeInfo(pointee_or_element_clang_type);
4030
4031 case clang::Type::Enum:
4032 if (pointee_or_element_clang_type)
4033 pointee_or_element_clang_type->SetCompilerType(
4034 getASTContext(),
4035 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
4036 return eTypeIsEnumeration | eTypeHasValue;
4037
4038 case clang::Type::Auto:
4039 return CompilerType(
4040 getASTContext(),
4041 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4042 .GetTypeInfo(pointee_or_element_clang_type);
4043 case clang::Type::Elaborated:
4044 return CompilerType(
4045 getASTContext(),
4046 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4047 .GetTypeInfo(pointee_or_element_clang_type);
4048 case clang::Type::Paren:
4049 return CompilerType(getASTContext(),
4050 llvm::cast<clang::ParenType>(qual_type)->desugar())
4051 .GetTypeInfo(pointee_or_element_clang_type);
4052
4053 case clang::Type::FunctionProto:
4054 return eTypeIsFuncPrototype | eTypeHasValue;
4055 case clang::Type::FunctionNoProto:
4056 return eTypeIsFuncPrototype | eTypeHasValue;
4057 case clang::Type::InjectedClassName:
4058 return 0;
4059
4060 case clang::Type::LValueReference:
4061 case clang::Type::RValueReference:
4062 if (pointee_or_element_clang_type)
4063 pointee_or_element_clang_type->SetCompilerType(
4064 getASTContext(),
4065 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4066 ->getPointeeType());
4067 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4068
4069 case clang::Type::MemberPointer:
4070 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4071
4072 case clang::Type::ObjCObjectPointer:
4073 if (pointee_or_element_clang_type)
4074 pointee_or_element_clang_type->SetCompilerType(
4075 getASTContext(), qual_type->getPointeeType());
4076 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4077 eTypeHasValue;
4078
4079 case clang::Type::ObjCObject:
4080 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4081 case clang::Type::ObjCInterface:
4082 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4083
4084 case clang::Type::Pointer:
4085 if (pointee_or_element_clang_type)
4086 pointee_or_element_clang_type->SetCompilerType(
4087 getASTContext(), qual_type->getPointeeType());
4088 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4089
4090 case clang::Type::Record:
4091 if (qual_type->getAsCXXRecordDecl())
4092 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4093 else
4094 return eTypeHasChildren | eTypeIsStructUnion;
4095 break;
4096 case clang::Type::SubstTemplateTypeParm:
4097 return eTypeIsTemplate;
4098 case clang::Type::TemplateTypeParm:
4099 return eTypeIsTemplate;
4100 case clang::Type::TemplateSpecialization:
4101 return eTypeIsTemplate;
4102
4103 case clang::Type::Typedef:
4104 return eTypeIsTypedef |
4105 CompilerType(getASTContext(),
4106 llvm::cast<clang::TypedefType>(qual_type)
4107 ->getDecl()
4108 ->getUnderlyingType())
4109 .GetTypeInfo(pointee_or_element_clang_type);
4110 case clang::Type::TypeOfExpr:
4111 return CompilerType(getASTContext(),
4112 llvm::cast<clang::TypeOfExprType>(qual_type)
4113 ->getUnderlyingExpr()
4114 ->getType())
4115 .GetTypeInfo(pointee_or_element_clang_type);
4116 case clang::Type::TypeOf:
4117 return CompilerType(
4118 getASTContext(),
4119 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4120 .GetTypeInfo(pointee_or_element_clang_type);
4121 case clang::Type::UnresolvedUsing:
4122 return 0;
4123
4124 case clang::Type::ExtVector:
4125 case clang::Type::Vector: {
4126 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4127 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4128 qual_type->getCanonicalTypeInternal());
4129 if (vector_type) {
4130 if (vector_type->isIntegerType())
4131 vector_type_flags |= eTypeIsFloat;
4132 else if (vector_type->isFloatingType())
4133 vector_type_flags |= eTypeIsInteger;
4134 }
4135 return vector_type_flags;
4136 }
4137 default:
4138 return 0;
4139 }
4140 return 0;
4141}
4142
4143lldb::LanguageType
4144ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4145 if (!type)
4146 return lldb::eLanguageTypeC;
4147
4148 // If the type is a reference, then resolve it to what it refers to first:
4149 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4150 if (qual_type->isAnyPointerType()) {
4151 if (qual_type->isObjCObjectPointerType())
4152 return lldb::eLanguageTypeObjC;
4153
4154 clang::QualType pointee_type(qual_type->getPointeeType());
4155 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
4156 return lldb::eLanguageTypeC_plus_plus;
4157 if (pointee_type->isObjCObjectOrInterfaceType())
4158 return lldb::eLanguageTypeObjC;
4159 if (pointee_type->isObjCClassType())
4160 return lldb::eLanguageTypeObjC;
4161 if (pointee_type.getTypePtr() ==
4162 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4163 return lldb::eLanguageTypeObjC;
4164 } else {
4165 if (qual_type->isObjCObjectOrInterfaceType())
4166 return lldb::eLanguageTypeObjC;
4167 if (qual_type->getAsCXXRecordDecl())
4168 return lldb::eLanguageTypeC_plus_plus;
4169 switch (qual_type->getTypeClass()) {
4170 default:
4171 break;
4172 case clang::Type::Builtin:
4173 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4174 default:
4175 case clang::BuiltinType::Void:
4176 case clang::BuiltinType::Bool:
4177 case clang::BuiltinType::Char_U:
4178 case clang::BuiltinType::UChar:
4179 case clang::BuiltinType::WChar_U:
4180 case clang::BuiltinType::Char16:
4181 case clang::BuiltinType::Char32:
4182 case clang::BuiltinType::UShort:
4183 case clang::BuiltinType::UInt:
4184 case clang::BuiltinType::ULong:
4185 case clang::BuiltinType::ULongLong:
4186 case clang::BuiltinType::UInt128:
4187 case clang::BuiltinType::Char_S:
4188 case clang::BuiltinType::SChar:
4189 case clang::BuiltinType::WChar_S:
4190 case clang::BuiltinType::Short:
4191 case clang::BuiltinType::Int:
4192 case clang::BuiltinType::Long:
4193 case clang::BuiltinType::LongLong:
4194 case clang::BuiltinType::Int128:
4195 case clang::BuiltinType::Float:
4196 case clang::BuiltinType::Double:
4197 case clang::BuiltinType::LongDouble:
4198 break;
4199
4200 case clang::BuiltinType::NullPtr:
4201 return eLanguageTypeC_plus_plus;
4202
4203 case clang::BuiltinType::ObjCId:
4204 case clang::BuiltinType::ObjCClass:
4205 case clang::BuiltinType::ObjCSel:
4206 return eLanguageTypeObjC;
4207
4208 case clang::BuiltinType::Dependent:
4209 case clang::BuiltinType::Overload:
4210 case clang::BuiltinType::BoundMember:
4211 case clang::BuiltinType::UnknownAny:
4212 break;
4213 }
4214 break;
4215 case clang::Type::Typedef:
4216 return CompilerType(getASTContext(),
4217 llvm::cast<clang::TypedefType>(qual_type)
4218 ->getDecl()
4219 ->getUnderlyingType())
4220 .GetMinimumLanguage();
4221 }
4222 }
4223 return lldb::eLanguageTypeC;
4224}
4225
4226lldb::TypeClass
4227ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4228 if (!type)
4229 return lldb::eTypeClassInvalid;
4230
4231 clang::QualType qual_type(GetQualType(type));
4232
4233 switch (qual_type->getTypeClass()) {
4234 case clang::Type::UnaryTransform:
4235 break;
4236 case clang::Type::FunctionNoProto:
4237 return lldb::eTypeClassFunction;
4238 case clang::Type::FunctionProto:
4239 return lldb::eTypeClassFunction;
4240 case clang::Type::IncompleteArray:
4241 return lldb::eTypeClassArray;
4242 case clang::Type::VariableArray:
4243 return lldb::eTypeClassArray;
4244 case clang::Type::ConstantArray:
4245 return lldb::eTypeClassArray;
4246 case clang::Type::DependentSizedArray:
4247 return lldb::eTypeClassArray;
4248 case clang::Type::DependentSizedExtVector:
4249 return lldb::eTypeClassVector;
4250 case clang::Type::DependentVector:
4251 return lldb::eTypeClassVector;
4252 case clang::Type::ExtVector:
4253 return lldb::eTypeClassVector;
4254 case clang::Type::Vector:
4255 return lldb::eTypeClassVector;
4256 case clang::Type::Builtin:
4257 return lldb::eTypeClassBuiltin;
4258 case clang::Type::ObjCObjectPointer:
4259 return lldb::eTypeClassObjCObjectPointer;
4260 case clang::Type::BlockPointer:
4261 return lldb::eTypeClassBlockPointer;
4262 case clang::Type::Pointer:
4263 return lldb::eTypeClassPointer;
4264 case clang::Type::LValueReference:
4265 return lldb::eTypeClassReference;
4266 case clang::Type::RValueReference:
4267 return lldb::eTypeClassReference;
4268 case clang::Type::MemberPointer:
4269 return lldb::eTypeClassMemberPointer;
4270 case clang::Type::Complex:
4271 if (qual_type->isComplexType())
4272 return lldb::eTypeClassComplexFloat;
4273 else
4274 return lldb::eTypeClassComplexInteger;
4275 case clang::Type::ObjCObject:
4276 return lldb::eTypeClassObjCObject;
4277 case clang::Type::ObjCInterface:
4278 return lldb::eTypeClassObjCInterface;
4279 case clang::Type::Record: {
4280 const clang::RecordType *record_type =
4281 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4282 const clang::RecordDecl *record_decl = record_type->getDecl();
4283 if (record_decl->isUnion())
4284 return lldb::eTypeClassUnion;
4285 else if (record_decl->isStruct())
4286 return lldb::eTypeClassStruct;
4287 else
4288 return lldb::eTypeClassClass;
4289 } break;
4290 case clang::Type::Enum:
4291 return lldb::eTypeClassEnumeration;
4292 case clang::Type::Typedef:
4293 return lldb::eTypeClassTypedef;
4294 case clang::Type::UnresolvedUsing:
4295 break;
4296 case clang::Type::Paren:
4297 return CompilerType(getASTContext(),
4298 llvm::cast<clang::ParenType>(qual_type)->desugar())
4299 .GetTypeClass();
4300 case clang::Type::Auto:
4301 return CompilerType(
4302 getASTContext(),
4303 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4304 .GetTypeClass();
4305 case clang::Type::Elaborated:
4306 return CompilerType(
4307 getASTContext(),
4308 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4309 .GetTypeClass();
4310
4311 case clang::Type::Attributed:
4312 break;
4313 case clang::Type::TemplateTypeParm:
4314 break;
4315 case clang::Type::SubstTemplateTypeParm:
4316 break;
4317 case clang::Type::SubstTemplateTypeParmPack:
4318 break;
4319 case clang::Type::InjectedClassName:
4320 break;
4321 case clang::Type::DependentName:
4322 break;
4323 case clang::Type::DependentTemplateSpecialization:
4324 break;
4325 case clang::Type::PackExpansion:
4326 break;
4327
4328 case clang::Type::TypeOfExpr:
4329 return CompilerType(getASTContext(),
4330 llvm::cast<clang::TypeOfExprType>(qual_type)
4331 ->getUnderlyingExpr()
4332 ->getType())
4333 .GetTypeClass();
4334 case clang::Type::TypeOf:
4335 return CompilerType(
4336 getASTContext(),
4337 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4338 .GetTypeClass();
4339 case clang::Type::Decltype:
4340 return CompilerType(
4341 getASTContext(),
4342 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4343 .GetTypeClass();
4344 case clang::Type::TemplateSpecialization:
4345 break;
4346 case clang::Type::DeducedTemplateSpecialization:
4347 break;
4348 case clang::Type::Atomic:
4349 break;
4350 case clang::Type::Pipe:
4351 break;
4352
4353 // pointer type decayed from an array or function type.
4354 case clang::Type::Decayed:
4355 break;
4356 case clang::Type::Adjusted:
4357 break;
4358 case clang::Type::ObjCTypeParam:
4359 break;
4360
4361 case clang::Type::DependentAddressSpace:
4362 break;
4363 }
4364 // We don't know hot to display this type...
4365 return lldb::eTypeClassOther;
4366}
4367
4368unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4369 if (type)
4370 return GetQualType(type).getQualifiers().getCVRQualifiers();
4371 return 0;
4372}
4373
4374//----------------------------------------------------------------------
4375// Creating related types
4376//----------------------------------------------------------------------
4377
4378CompilerType
4379ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4380 uint64_t *stride) {
4381 if (type) {
4382 clang::QualType qual_type(GetCanonicalQualType(type));
4383
4384 const clang::Type *array_eletype =
4385 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4386
4387 if (!array_eletype)
4388 return CompilerType();
4389
4390 CompilerType element_type(getASTContext(),
4391 array_eletype->getCanonicalTypeUnqualified());
4392
4393 // TODO: the real stride will be >= this value.. find the real one!
4394 if (stride)
4395 *stride = element_type.GetByteSize(nullptr);
4396
4397 return element_type;
4398 }
4399 return CompilerType();
4400}
4401
4402CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4403 uint64_t size) {
4404 if (type) {
4405 clang::QualType qual_type(GetCanonicalQualType(type));
4406 if (clang::ASTContext *ast_ctx = getASTContext()) {
4407 if (size != 0)
4408 return CompilerType(
4409 ast_ctx, ast_ctx->getConstantArrayType(
4410 qual_type, llvm::APInt(64, size),
4411 clang::ArrayType::ArraySizeModifier::Normal, 0));
4412 else
4413 return CompilerType(
4414 ast_ctx,
4415 ast_ctx->getIncompleteArrayType(
4416 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
4417 }
4418 }
4419
4420 return CompilerType();
4421}
4422
4423CompilerType
4424ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4425 if (type)
4426 return CompilerType(getASTContext(), GetCanonicalQualType(type));
4427 return CompilerType();
4428}
4429
4430static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4431 clang::QualType qual_type) {
4432 if (qual_type->isPointerType())
4433 qual_type = ast->getPointerType(
4434 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4435 else
4436 qual_type = qual_type.getUnqualifiedType();
4437 qual_type.removeLocalConst();
4438 qual_type.removeLocalRestrict();
4439 qual_type.removeLocalVolatile();
4440 return qual_type;
4441}
4442
4443CompilerType
4444ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4445 if (type)
4446 return CompilerType(
4447 getASTContext(),
4448 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4449 return CompilerType();
4450}
4451
4452int ClangASTContext::GetFunctionArgumentCount(
4453 lldb::opaque_compiler_type_t type) {
4454 if (type) {
4455 const clang::FunctionProtoType *func =
4456 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4457 if (func)
4458 return func->getNumParams();
4459 }
4460 return -1;
4461}
4462
4463CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4464 lldb::opaque_compiler_type_t type, size_t idx) {
4465 if (type) {
4466 const clang::FunctionProtoType *func =
4467 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4468 if (func) {
4469 const uint32_t num_args = func->getNumParams();
4470 if (idx < num_args)
4471 return CompilerType(getASTContext(), func->getParamType(idx));
4472 }
4473 }
4474 return CompilerType();
4475}
4476
4477CompilerType
4478ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4479 if (type) {
4480 clang::QualType qual_type(GetQualType(type));
4481 const clang::FunctionProtoType *func =
4482 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4483 if (func)
4484 return CompilerType(getASTContext(), func->getReturnType());
4485 }
4486 return CompilerType();
4487}
4488
4489size_t
4490ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4491 size_t num_functions = 0;
4492 if (type) {
4493 clang::QualType qual_type(GetCanonicalQualType(type));
4494 switch (qual_type->getTypeClass()) {
4495 case clang::Type::Record:
4496 if (GetCompleteQualType(getASTContext(), qual_type)) {
4497 const clang::RecordType *record_type =
4498 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4499 const clang::RecordDecl *record_decl = record_type->getDecl();
4500 assert(record_decl)((record_decl) ? static_cast<void> (0) : __assert_fail (
"record_decl", "/build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 4500, __PRETTY_FUNCTION__))
;
4501 const clang::CXXRecordDecl *cxx_record_decl =
4502 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4503 if (cxx_record_decl)
4504 num_functions = std::distance(cxx_record_decl->method_begin(),
4505 cxx_record_decl->method_end());
4506 }
4507 break;
4508
4509 case clang::Type::ObjCObjectPointer: {
4510 const clang::ObjCObjectPointerType *objc_class_type =
4511 qual_type->getAs<clang::ObjCObjectPointerType>();
4512 const clang::ObjCInterfaceType *objc_interface_type =
4513 objc_class_type->getInterfaceType();
4514 if (objc_interface_type &&
4515 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4516 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
4517 clang::ObjCInterfaceDecl *class_interface_decl =
4518 objc_interface_type->getDecl();
4519 if (class_interface_decl) {
4520 num_functions = std::distance(class_interface_decl->meth_begin(),
4521 class_interface_decl->meth_end());
4522 }
4523 }
4524 break;
4525 }
4526
4527 case clang::Type::ObjCObject:
4528 case clang::Type::ObjCInterface:
4529 if (GetCompleteType(type)) {
4530 const clang::ObjCObjectType *objc_class_type =
4531 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4532 if (objc_class_type) {
4533 clang::ObjCInterfaceDecl *class_interface_decl =
4534 objc_class_type->getInterface();
4535 if (class_interface_decl)
4536 num_functions = std::distance(class_interface_decl->meth_begin(),
4537 class_interface_decl->meth_end());
4538 }
4539 }
4540 break;
4541
4542 case clang::Type::Typedef:
4543 return CompilerType(getASTContext(),
4544 llvm::cast<clang::TypedefType>(qual_type)
4545 ->getDecl()
4546 ->getUnderlyingType())
4547 .GetNumMemberFunctions();
4548
4549 case clang::Type::Auto:
4550 return CompilerType(
4551 getASTContext(),
4552 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4553 .GetNumMemberFunctions();
4554
4555 case clang::Type::Elaborated:
4556 return CompilerType(
4557 getASTContext(),
4558 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4559 .GetNumMemberFunctions();
4560
4561 case clang::Type::Paren:
4562 return CompilerType(getASTContext(),
4563 llvm::cast<clang::ParenType>(qual_type)->desugar())
4564 .GetNumMemberFunctions();
4565
4566 default:
4567 break;
4568 }
4569 }
4570 return num_functions;
4571}
4572
4573TypeMemberFunctionImpl
4574ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4575 size_t idx) {
4576 std::string name;
4577 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4578 CompilerType clang_type;
4579 CompilerDecl clang_decl;
4580 if (type) {
4581 clang::QualType qual_type(GetCanonicalQualType(type));
4582 switch (qual_type->getTypeClass()) {
4583 case clang::Type::Record:
4584 if (GetCompleteQualType(getASTContext(), qual_type)) {
4585 const clang::RecordType *record_type =
4586 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4587 const clang::RecordDecl *record_decl = record_type->getDecl();
4588 assert(record_decl)((record_decl) ? static_cast<void> (0) : __assert_fail (
"record_decl", "/build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 4588, __PRETTY_FUNCTION__))
;
4589 const clang::CXXRecordDecl *cxx_record_decl =
4590 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4591 if (cxx_record_decl) {
4592 auto method_iter = cxx_record_decl->method_begin();
4593 auto method_end = cxx_record_decl->method_end();
4594 if (idx <
4595 static_cast<size_t>(std::distance(method_iter, method_end))) {
4596 std::advance(method_iter, idx);
4597 clang::CXXMethodDecl *cxx_method_decl =
4598 method_iter->getCanonicalDecl();
4599 if (cxx_method_decl) {
4600 name = cxx_method_decl->getDeclName().getAsString();
4601 if (cxx_method_decl->isStatic())
4602 kind = lldb::eMemberFunctionKindStaticMethod;
4603 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4604 kind = lldb::eMemberFunctionKindConstructor;
4605 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4606 kind = lldb::eMemberFunctionKindDestructor;
4607 else
4608 kind = lldb::eMemberFunctionKindInstanceMethod;
4609 clang_type = CompilerType(
4610 this, cxx_method_decl->getType().getAsOpaquePtr());
4611 clang_decl = CompilerDecl(this, cxx_method_decl);
4612 }
4613 }
4614 }
4615 }
4616 break;
4617
4618 case clang::Type::ObjCObjectPointer: {
4619 const clang::ObjCObjectPointerType *objc_class_type =
4620 qual_type->getAs<clang::ObjCObjectPointerType>();
4621 const clang::ObjCInterfaceType *objc_interface_type =
4622 objc_class_type->getInterfaceType();
4623 if (objc_interface_type &&
4624 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4625 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
4626 clang::ObjCInterfaceDecl *class_interface_decl =
4627 objc_interface_type->getDecl();
4628 if (class_interface_decl) {
4629 auto method_iter = class_interface_decl->meth_begin();
4630 auto method_end = class_interface_decl->meth_end();
4631 if (idx <
4632 static_cast<size_t>(std::distance(method_iter, method_end))) {
4633 std::advance(method_iter, idx);
4634 clang::ObjCMethodDecl *objc_method_decl =
4635 method_iter->getCanonicalDecl();
4636 if (objc_method_decl) {
4637 clang_decl = CompilerDecl(this, objc_method_decl);
4638 name = objc_method_decl->getSelector().getAsString();
4639 if (objc_method_decl->isClassMethod())
4640 kind = lldb::eMemberFunctionKindStaticMethod;
4641 else
4642 kind = lldb::eMemberFunctionKindInstanceMethod;
4643 }
4644 }
4645 }
4646 }
4647 break;
4648 }
4649
4650 case clang::Type::ObjCObject:
4651 case clang::Type::ObjCInterface:
4652 if (GetCompleteType(type)) {
4653 const clang::ObjCObjectType *objc_class_type =
4654 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4655 if (objc_class_type) {
4656 clang::ObjCInterfaceDecl *class_interface_decl =
4657 objc_class_type->getInterface();
4658 if (class_interface_decl) {
4659 auto method_iter = class_interface_decl->meth_begin();
4660 auto method_end = class_interface_decl->meth_end();
4661 if (idx <
4662 static_cast<size_t>(std::distance(method_iter, method_end))) {
4663 std::advance(method_iter, idx);
4664 clang::ObjCMethodDecl *objc_method_decl =
4665 method_iter->getCanonicalDecl();
4666 if (objc_method_decl) {
4667 clang_decl = CompilerDecl(this, objc_method_decl);
4668 name = objc_method_decl->getSelector().getAsString();
4669 if (objc_method_decl->isClassMethod())
4670 kind = lldb::eMemberFunctionKindStaticMethod;
4671 else
4672 kind = lldb::eMemberFunctionKindInstanceMethod;
4673 }
4674 }
4675 }
4676 }
4677 }
4678 break;
4679
4680 case clang::Type::Typedef:
4681 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4682 ->getDecl()
4683 ->getUnderlyingType()
4684 .getAsOpaquePtr(),
4685 idx);
4686
4687 case clang::Type::Auto:
4688 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4689 ->getDeducedType()
4690 .getAsOpaquePtr(),
4691 idx);
4692
4693 case clang::Type::Elaborated:
4694 return GetMemberFunctionAtIndex(
4695 llvm::cast<clang::ElaboratedType>(qual_type)
4696 ->getNamedType()
4697 .getAsOpaquePtr(),
4698 idx);
4699
4700 case clang::Type::Paren:
4701 return GetMemberFunctionAtIndex(
4702 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4703 idx);
4704
4705 default:
4706 break;
4707 }
4708 }
4709
4710 if (kind == eMemberFunctionKindUnknown)
4711 return TypeMemberFunctionImpl();
4712 else
4713 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
4714}
4715
4716CompilerType
4717ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4718 if (type)
4719 return CompilerType(getASTContext(),
4720 GetQualType(type).getNonReferenceType());
4721 return CompilerType();
4722}
4723
4724CompilerType ClangASTContext::CreateTypedefType(
4725 const CompilerType &type, const char *typedef_name,
4726 const CompilerDeclContext &compiler_decl_ctx) {
4727 if (type && typedef_name && typedef_name[0]) {
4728 ClangASTContext *ast =
4729 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4730 if (!ast)
4731 return CompilerType();
4732 clang::ASTContext *clang_ast = ast->getASTContext();
4733 clang::QualType qual_type(ClangUtil::GetQualType(type));
4734
4735 clang::DeclContext *decl_ctx =
4736 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4737 if (decl_ctx == nullptr)
4738 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4739
4740 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4741 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4742 &clang_ast->Idents.get(typedef_name),
4743 clang_ast->getTrivialTypeSourceInfo(qual_type));
4744
4745 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4746
4747 decl_ctx->addDecl(decl);
4748
4749 // Get a uniqued clang::QualType for the typedef decl type
4750 return CompilerType(clang_ast, clang_ast->getTypedefType(decl));
4751 }
4752 return CompilerType();
4753}
4754
4755CompilerType
4756ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4757 if (type) {
4758 clang::QualType qual_type(GetQualType(type));
4759 return CompilerType(getASTContext(),
4760 qual_type.getTypePtr()->getPointeeType());
4761 }
4762 return CompilerType();
4763}
4764
4765CompilerType
4766ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4767 if (type) {
4768 clang::QualType qual_type(GetQualType(type));
4769
4770 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4771 switch (type_class) {
4772 case clang::Type::ObjCObject:
4773 case clang::Type::ObjCInterface:
4774 return CompilerType(getASTContext(),
4775 getASTContext()->getObjCObjectPointerType(qual_type));
4776
4777 default:
4778 return CompilerType(getASTContext(),
4779 getASTContext()->getPointerType(qual_type));
4780 }
4781 }
4782 return CompilerType();
4783}
4784
4785CompilerType
4786ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4787 if (type)
4788 return CompilerType(this, getASTContext()
4789 ->getLValueReferenceType(GetQualType(type))
4790 .getAsOpaquePtr());
4791 else
4792 return CompilerType();
4793}
4794
4795CompilerType
4796ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4797 if (type)
4798 return CompilerType(this, getASTContext()
4799 ->getRValueReferenceType(GetQualType(type))
4800 .getAsOpaquePtr());
4801 else
4802 return CompilerType();
4803}
4804
4805CompilerType
4806ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4807 if (type) {
4808 clang::QualType result(GetQualType(type));
4809 result.addConst();
4810 return CompilerType(this, result.getAsOpaquePtr());
4811 }
4812 return CompilerType();
4813}
4814
4815CompilerType
4816ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4817 if (type) {
4818 clang::QualType result(GetQualType(type));
4819 result.addVolatile();
4820 return CompilerType(this, result.getAsOpaquePtr());
4821 }
4822 return CompilerType();
4823}
4824
4825CompilerType
4826ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4827 if (type) {
4828 clang::QualType result(GetQualType(type));
4829 result.addRestrict();
4830 return CompilerType(this, result.getAsOpaquePtr());
4831 }
4832 return CompilerType();
4833}
4834
4835CompilerType
4836ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4837 const char *typedef_name,
4838 const CompilerDeclContext &compiler_decl_ctx) {
4839 if (type) {
4840 clang::ASTContext *clang_ast = getASTContext();
4841 clang::QualType qual_type(GetQualType(type));
4842
4843 clang::DeclContext *decl_ctx =
4844 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4845 if (decl_ctx == nullptr)
4846 decl_ctx = getASTContext()->getTranslationUnitDecl();
4847
4848 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4849 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4850 &clang_ast->Idents.get(typedef_name),
4851 clang_ast->getTrivialTypeSourceInfo(qual_type));
4852
4853 clang::TagDecl *tdecl = nullptr;
4854 if (!qual_type.isNull()) {
4855 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4856 tdecl = rt->getDecl();
4857 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4858 tdecl = et->getDecl();
4859 }
4860
4861 // Check whether this declaration is an anonymous struct, union, or enum,
4862 // hidden behind a typedef. If so, we try to check whether we have a
4863 // typedef tag to attach to the original record declaration
4864 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4865 tdecl->setTypedefNameForAnonDecl(decl);
4866
4867 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4868
4869 // Get a uniqued clang::QualType for the typedef decl type
4870 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4871 }
4872 return CompilerType();
4873}
4874
4875CompilerType
4876ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4877 if (type) {
4878 const clang::TypedefType *typedef_type =
4879 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4880 if (typedef_type)
4881 return CompilerType(getASTContext(),
4882 typedef_type->getDecl()->getUnderlyingType());
4883 }
4884 return CompilerType();
4885}
4886
4887//----------------------------------------------------------------------
4888// Create related types using the current type's AST
4889//----------------------------------------------------------------------
4890
4891CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
4892 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
4893}
4894//----------------------------------------------------------------------
4895// Exploring the type
4896//----------------------------------------------------------------------
4897
4898uint64_t ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
4899 ExecutionContextScope *exe_scope) {
4900 if (GetCompleteType(type)) {
4901 clang::QualType qual_type(GetCanonicalQualType(type));
4902 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4903 switch (type_class) {
4904 case clang::Type::Record:
4905 if (GetCompleteType(type))
4906 return getASTContext()->getTypeSize(qual_type);
4907 else
4908 return 0;
4909 break;
4910
4911 case clang::Type::ObjCInterface:
4912 case clang::Type::ObjCObject: {
4913 ExecutionContext exe_ctx(exe_scope);
4914 Process *process = exe_ctx.GetProcessPtr();
4915 if (process) {
4916 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4917 if (objc_runtime) {
4918 uint64_t bit_size = 0;
4919 if (objc_runtime->GetTypeBitSize(
4920 CompilerType(getASTContext(), qual_type), bit_size))
4921 return bit_size;
4922 }
4923 } else {
4924 static bool g_printed = false;
4925 if (!g_printed) {
4926 StreamString s;
4927 DumpTypeDescription(type, &s);
4928
4929 llvm::outs() << "warning: trying to determine the size of type ";
4930 llvm::outs() << s.GetString() << "\n";
4931 llvm::outs() << "without a valid ExecutionContext. this is not "
4932 "reliable. please file a bug against LLDB.\n";
4933 llvm::outs() << "backtrace:\n";
4934 llvm::sys::PrintStackTrace(llvm::outs());
4935 llvm::outs() << "\n";
4936 g_printed = true;
4937 }
4938 }
4939 }
4940 LLVM_FALLTHROUGH[[clang::fallthrough]];
4941 default:
4942 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
4943 if (bit_size == 0) {
4944 if (qual_type->isIncompleteArrayType())
4945 return getASTContext()->getTypeSize(
4946 qual_type->getArrayElementTypeNoTypeQual()
4947 ->getCanonicalTypeUnqualified());
4948 }
4949 if (qual_type->isObjCObjectOrInterfaceType())
4950 return bit_size +
4951 getASTContext()->getTypeSize(
4952 getASTContext()->ObjCBuiltinClassTy);
4953 return bit_size;
4954 }
4955 }
4956 return 0;
4957}
4958
4959size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
4960 if (GetCompleteType(type))
4961 return getASTContext()->getTypeAlign(GetQualType(type));
4962 return 0;
4963}
4964
4965lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
4966 uint64_t &count) {
4967 if (!type)
4968 return lldb::eEncodingInvalid;
4969
4970 count = 1;
4971 clang::QualType qual_type(GetCanonicalQualType(type));
4972
4973 switch (qual_type->getTypeClass()) {
4974 case clang::Type::UnaryTransform:
4975 break;
4976
4977 case clang::Type::FunctionNoProto:
4978 case clang::Type::FunctionProto:
4979 break;
4980
4981 case clang::Type::IncompleteArray:
4982 case clang::Type::VariableArray:
4983 break;
4984
4985 case clang::Type::ConstantArray:
4986 break;
4987
4988 case clang::Type::DependentVector:
4989 case clang::Type::ExtVector:
4990 case clang::Type::Vector:
4991 // TODO: Set this to more than one???
4992 break;
4993
4994 case clang::Type::Builtin:
4995 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4996 case clang::BuiltinType::Void:
4997 break;
4998
4999 case clang::BuiltinType::Bool:
5000 case clang::BuiltinType::Char_S:
5001 case clang::BuiltinType::SChar:
5002 case clang::BuiltinType::WChar_S:
5003 case clang::BuiltinType::Short:
5004 case clang::BuiltinType::Int:
5005 case clang::BuiltinType::Long:
5006 case clang::BuiltinType::LongLong:
5007 case clang::BuiltinType::Int128:
5008 return lldb::eEncodingSint;
5009
5010 case clang::BuiltinType::Char_U:
5011 case clang::BuiltinType::UChar:
5012 case clang::BuiltinType::WChar_U:
5013 case clang::BuiltinType::Char8:
5014 case clang::BuiltinType::Char16:
5015 case clang::BuiltinType::Char32:
5016 case clang::BuiltinType::UShort:
5017 case clang::BuiltinType::UInt:
5018 case clang::BuiltinType::ULong:
5019 case clang::BuiltinType::ULongLong:
5020 case clang::BuiltinType::UInt128:
5021 return lldb::eEncodingUint;
5022
5023 // Fixed point types. Note that they are currently ignored.
5024 case clang::BuiltinType::ShortAccum:
5025 case clang::BuiltinType::Accum:
5026 case clang::BuiltinType::LongAccum:
5027 case clang::BuiltinType::UShortAccum:
5028 case clang::BuiltinType::UAccum:
5029 case clang::BuiltinType::ULongAccum:
5030 case clang::BuiltinType::ShortFract:
5031 case clang::BuiltinType::Fract:
5032 case clang::BuiltinType::LongFract:
5033 case clang::BuiltinType::UShortFract:
5034 case clang::BuiltinType::UFract:
5035 case clang::BuiltinType::ULongFract:
5036 case clang::BuiltinType::SatShortAccum:
5037 case clang::BuiltinType::SatAccum:
5038 case clang::BuiltinType::SatLongAccum:
5039 case clang::BuiltinType::SatUShortAccum:
5040 case clang::BuiltinType::SatUAccum:
5041 case clang::BuiltinType::SatULongAccum:
5042 case clang::BuiltinType::SatShortFract:
5043 case clang::BuiltinType::SatFract:
5044 case clang::BuiltinType::SatLongFract:
5045 case clang::BuiltinType::SatUShortFract:
5046 case clang::BuiltinType::SatUFract:
5047 case clang::BuiltinType::SatULongFract:
5048 break;
5049
5050 case clang::BuiltinType::Half:
5051 case clang::BuiltinType::Float:
5052 case clang::BuiltinType::Float16:
5053 case clang::BuiltinType::Float128:
5054 case clang::BuiltinType::Double:
5055 case clang::BuiltinType::LongDouble:
5056 return lldb::eEncodingIEEE754;
5057
5058 case clang::BuiltinType::ObjCClass:
5059 case clang::BuiltinType::ObjCId:
5060 case clang::BuiltinType::ObjCSel:
5061 return lldb::eEncodingUint;
5062
5063 case clang::BuiltinType::NullPtr:
5064 return lldb::eEncodingUint;
5065
5066 case clang::BuiltinType::Kind::ARCUnbridgedCast:
5067 case clang::BuiltinType::Kind::BoundMember:
5068 case clang::BuiltinType::Kind::BuiltinFn:
5069 case clang::BuiltinType::Kind::Dependent:
5070 case clang::BuiltinType::Kind::OCLClkEvent:
5071 case clang::BuiltinType::Kind::OCLEvent:
5072 case clang::BuiltinType::Kind::OCLImage1dRO:
5073 case clang::BuiltinType::Kind::OCLImage1dWO:
5074 case clang::BuiltinType::Kind::OCLImage1dRW:
5075 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
5076 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
5077 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
5078 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
5079 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
5080 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
5081 case clang::BuiltinType::Kind::OCLImage2dRO:
5082 case clang::BuiltinType::Kind::OCLImage2dWO:
5083 case clang::BuiltinType::Kind::OCLImage2dRW:
5084 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
5085 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
5086 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
5087 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
5088 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
5089 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
5090 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5091 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5092 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5093 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5094 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5095 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5096 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5097 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5098 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5099 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5100 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5101 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5102 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5103 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5104 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5105 case clang::BuiltinType::Kind::OCLImage3dRO:
5106 case clang::BuiltinType::Kind::OCLImage3dWO:
5107 case clang::BuiltinType::Kind::OCLImage3dRW:
5108 case clang::BuiltinType::Kind::OCLQueue:
5109 case clang::BuiltinType::Kind::OCLReserveID:
5110 case clang::BuiltinType::Kind::OCLSampler:
5111 case clang::BuiltinType::Kind::OMPArraySection:
5112 case clang::BuiltinType::Kind::Overload:
5113 case clang::BuiltinType::Kind::PseudoObject:
5114 case clang::BuiltinType::Kind::UnknownAny:
5115 break;
5116 }
5117 break;
5118 // All pointer types are represented as unsigned integer encodings. We may
5119 // nee to add a eEncodingPointer if we ever need to know the difference
5120 case clang::Type::ObjCObjectPointer:
5121 case clang::Type::BlockPointer:
5122 case clang::Type::Pointer:
5123 case clang::Type::LValueReference:
5124 case clang::Type::RValueReference:
5125 case clang::Type::MemberPointer:
5126 return lldb::eEncodingUint;
5127 case clang::Type::Complex: {
5128 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5129 if (qual_type->isComplexType())
5130 encoding = lldb::eEncodingIEEE754;
5131 else {
5132 const clang::ComplexType *complex_type =
5133 qual_type->getAsComplexIntegerType();
5134 if (complex_type)
5135 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5136 .GetEncoding(count);
5137 else
5138 encoding = lldb::eEncodingSint;
5139 }
5140 count = 2;
5141 return encoding;
5142 }
5143
5144 case clang::Type::ObjCInterface:
5145 break;
5146 case clang::Type::Record:
5147 break;
5148 case clang::Type::Enum:
5149 return lldb::eEncodingSint;
5150 case clang::Type::Typedef:
5151 return CompilerType(getASTContext(),
5152 llvm::cast<clang::TypedefType>(qual_type)
5153 ->getDecl()
5154 ->getUnderlyingType())
5155 .GetEncoding(count);
5156
5157 case clang::Type::Auto:
5158 return CompilerType(
5159 getASTContext(),
5160 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5161 .GetEncoding(count);
5162
5163 case clang::Type::Elaborated:
5164 return CompilerType(
5165 getASTContext(),
5166 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5167 .GetEncoding(count);
5168
5169 case clang::Type::Paren:
5170 return CompilerType(getASTContext(),
5171 llvm::cast<clang::ParenType>(qual_type)->desugar())
5172 .GetEncoding(count);
5173 case clang::Type::TypeOfExpr:
5174 return CompilerType(getASTContext(),
5175 llvm::cast<clang::TypeOfExprType>(qual_type)
5176 ->getUnderlyingExpr()
5177 ->getType())
5178 .GetEncoding(count);
5179 case clang::Type::TypeOf:
5180 return CompilerType(
5181 getASTContext(),
5182 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5183 .GetEncoding(count);
5184 case clang::Type::Decltype:
5185 return CompilerType(
5186 getASTContext(),
5187 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5188 .GetEncoding(count);
5189 case clang::Type::DependentSizedArray:
5190 case clang::Type::DependentSizedExtVector:
5191 case clang::Type::UnresolvedUsing:
5192 case clang::Type::Attributed:
5193 case clang::Type::TemplateTypeParm:
5194 case clang::Type::SubstTemplateTypeParm:
5195 case clang::Type::SubstTemplateTypeParmPack:
5196 case clang::Type::InjectedClassName:
5197 case clang::Type::DependentName:
5198 case clang::Type::DependentTemplateSpecialization:
5199 case clang::Type::PackExpansion:
5200 case clang::Type::ObjCObject:
5201
5202 case clang::Type::TemplateSpecialization:
5203 case clang::Type::DeducedTemplateSpecialization:
5204 case clang::Type::Atomic:
5205 case clang::Type::Adjusted:
5206 case clang::Type::Pipe:
5207 break;
5208
5209 // pointer type decayed from an array or function type.
5210 case clang::Type::Decayed:
5211 break;
5212 case clang::Type::ObjCTypeParam:
5213 break;
5214
5215 case clang::Type::DependentAddressSpace:
5216 break;
5217 }
5218 count = 0;
5219 return lldb::eEncodingInvalid;
5220}
5221
5222lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5223 if (!type)
5224 return lldb::eFormatDefault;
5225
5226 clang::QualType qual_type(GetCanonicalQualType(type));
5227
5228 switch (qual_type->getTypeClass()) {
5229 case clang::Type::UnaryTransform:
5230 break;
5231
5232 case clang::Type::FunctionNoProto:
5233 case clang::Type::FunctionProto:
5234 break;
5235
5236 case clang::Type::IncompleteArray:
5237 case clang::Type::VariableArray:
5238 break;
5239
5240 case clang::Type::ConstantArray:
5241 return lldb::eFormatVoid; // no value
5242
5243 case clang::Type::DependentVector:
5244 case clang::Type::ExtVector:
5245 case clang::Type::Vector:
5246 break;
5247
5248 case clang::Type::Builtin:
5249 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5250 // default: assert(0 && "Unknown builtin type!");
5251 case clang::BuiltinType::UnknownAny:
5252 case clang::BuiltinType::Void:
5253 case clang::BuiltinType::BoundMember:
5254 break;
5255
5256 case clang::BuiltinType::Bool:
5257 return lldb::eFormatBoolean;
5258 case clang::BuiltinType::Char_S:
5259 case clang::BuiltinType::SChar:
5260 case clang::BuiltinType::WChar_S:
5261 case clang::BuiltinType::Char_U:
5262 case clang::BuiltinType::UChar:
5263 case clang::BuiltinType::WChar_U:
5264 return lldb::eFormatChar;
5265 case clang::BuiltinType::Char16:
5266 return lldb::eFormatUnicode16;
5267 case clang::BuiltinType::Char32:
5268 return lldb::eFormatUnicode32;
5269 case clang::BuiltinType::UShort:
5270 return lldb::eFormatUnsigned;
5271 case clang::BuiltinType::Short:
5272 return lldb::eFormatDecimal;
5273 case clang::BuiltinType::UInt:
5274 return lldb::eFormatUnsigned;
5275 case clang::BuiltinType::Int:
5276 return lldb::eFormatDecimal;
5277 case clang::BuiltinType::ULong:
5278 return lldb::eFormatUnsigned;
5279 case clang::BuiltinType::Long:
5280 return lldb::eFormatDecimal;
5281 case clang::BuiltinType::ULongLong:
5282 return lldb::eFormatUnsigned;
5283 case clang::BuiltinType::LongLong:
5284 return lldb::eFormatDecimal;
5285 case clang::BuiltinType::UInt128:
5286 return lldb::eFormatUnsigned;
5287 case clang::BuiltinType::Int128:
5288 return lldb::eFormatDecimal;
5289 case clang::BuiltinType::Half:
5290 case clang::BuiltinType::Float:
5291 case clang::BuiltinType::Double:
5292 case clang::BuiltinType::LongDouble:
5293 return lldb::eFormatFloat;
5294 default:
5295 return lldb::eFormatHex;
5296 }
5297 break;
5298 case clang::Type::ObjCObjectPointer:
5299 return lldb::eFormatHex;
5300 case clang::Type::BlockPointer:
5301 return lldb::eFormatHex;
5302 case clang::Type::Pointer:
5303 return lldb::eFormatHex;
5304 case clang::Type::LValueReference:
5305 case clang::Type::RValueReference:
5306 return lldb::eFormatHex;
5307 case clang::Type::MemberPointer:
5308 break;
5309 case clang::Type::Complex: {
5310 if (qual_type->isComplexType())
5311 return lldb::eFormatComplex;
5312 else
5313 return lldb::eFormatComplexInteger;
5314 }
5315 case clang::Type::ObjCInterface:
5316 break;
5317 case clang::Type::Record:
5318 break;
5319 case clang::Type::Enum:
5320 return lldb::eFormatEnum;
5321 case clang::Type::Typedef:
5322 return CompilerType(getASTContext(),
5323 llvm::cast<clang::TypedefType>(qual_type)
5324 ->getDecl()
5325 ->getUnderlyingType())
5326 .GetFormat();
5327 case clang::Type::Auto:
5328 return CompilerType(getASTContext(),
5329 llvm::cast<clang::AutoType>(qual_type)->desugar())
5330 .GetFormat();
5331 case clang::Type::Paren:
5332 return CompilerType(getASTContext(),
5333 llvm::cast<clang::ParenType>(qual_type)->desugar())
5334 .GetFormat();
5335 case clang::Type::Elaborated:
5336 return CompilerType(
5337 getASTContext(),
5338 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5339 .GetFormat();
5340 case clang::Type::TypeOfExpr:
5341 return CompilerType(getASTContext(),
5342 llvm::cast<clang::TypeOfExprType>(qual_type)
5343 ->getUnderlyingExpr()
5344 ->getType())
5345 .GetFormat();
5346 case clang::Type::TypeOf:
5347 return CompilerType(
5348 getASTContext(),
5349 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5350 .GetFormat();
5351 case clang::Type::Decltype:
5352 return CompilerType(
5353 getASTContext(),
5354 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5355 .GetFormat();
5356 case clang::Type::DependentSizedArray:
5357 case clang::Type::DependentSizedExtVector:
5358 case clang::Type::UnresolvedUsing:
5359 case clang::Type::Attributed:
5360 case clang::Type::TemplateTypeParm:
5361 case clang::Type::SubstTemplateTypeParm:
5362 case clang::Type::SubstTemplateTypeParmPack:
5363 case clang::Type::InjectedClassName:
5364 case clang::Type::DependentName:
5365 case clang::Type::DependentTemplateSpecialization:
5366 case clang::Type::PackExpansion:
5367 case clang::Type::ObjCObject:
5368
5369 case clang::Type::TemplateSpecialization:
5370 case clang::Type::DeducedTemplateSpecialization:
5371 case clang::Type::Atomic:
5372 case clang::Type::Adjusted:
5373 case clang::Type::Pipe:
5374 break;
5375
5376 // pointer type decayed from an array or function type.
5377 case clang::Type::Decayed:
5378 break;
5379 case clang::Type::ObjCTypeParam:
5380 break;
5381
5382 case clang::Type::DependentAddressSpace:
5383 break;
5384 }
5385 // We don't know hot to display this type...
5386 return lldb::eFormatBytes;
5387}
5388
5389static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5390 bool check_superclass) {
5391 while (class_interface_decl) {
5392 if (class_interface_decl->ivar_size() > 0)
5393 return true;
5394
5395 if (check_superclass)
5396 class_interface_decl = class_interface_decl->getSuperClass();
5397 else
5398 break;
5399 }
5400 return false;
5401}
5402
5403uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
5404 bool omit_empty_base_classes) {
5405 if (!type)
5406 return 0;
5407
5408 uint32_t num_children = 0;
5409 clang::QualType qual_type(GetQualType(type));
5410 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5411 switch (type_class) {
5412 case clang::Type::Builtin:
5413 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5414 case clang::BuiltinType::ObjCId: // child is Class
5415 case clang::BuiltinType::ObjCClass: // child is Class
5416 num_children = 1;
5417 break;
5418
5419 default:
5420 break;
5421 }
5422 break;
5423
5424 case clang::Type::Complex:
5425 return 0;
5426
5427 case clang::Type::Record:
5428 if (GetCompleteQualType(getASTContext(), qual_type)) {
5429 const clang::RecordType *record_type =
5430 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5431 const clang::RecordDecl *record_decl = record_type->getDecl();
5432 assert(record_decl)((record_decl) ? static_cast<void> (0) : __assert_fail (
"record_decl", "/build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 5432, __PRETTY_FUNCTION__))
;
5433 const clang::CXXRecordDecl *cxx_record_decl =
5434 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5435 if (cxx_record_decl) {
5436 if (omit_empty_base_classes) {
5437 // Check each base classes to see if it or any of its base classes
5438 // contain any fields. This can help limit the noise in variable
5439 // views by not having to show base classes that contain no members.
5440 clang::CXXRecordDecl::base_class_const_iterator base_class,
5441 base_class_end;
5442 for (base_class = cxx_record_decl->bases_begin(),
5443 base_class_end = cxx_record_decl->bases_end();
5444 base_class != base_class_end; ++base_class) {
5445 const clang::CXXRecordDecl *base_class_decl =
5446 llvm::cast<clang::CXXRecordDecl>(
5447 base_class->getType()
5448 ->getAs<clang::RecordType>()
5449 ->getDecl());
5450
5451 // Skip empty base classes
5452 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5453 continue;
5454
5455 num_children++;
5456 }
5457 } else {
5458 // Include all base classes
5459 num_children += cxx_record_decl->getNumBases();
5460 }
5461 }
5462 clang::RecordDecl::field_iterator field, field_end;
5463 for (field = record_decl->field_begin(),
5464 field_end = record_decl->field_end();
5465 field != field_end; ++field)
5466 ++num_children;
5467 }
5468 break;
5469
5470 case clang::Type::ObjCObject:
5471 case clang::Type::ObjCInterface:
5472 if (GetCompleteQualType(getASTContext(), qual_type)) {
5473 const clang::ObjCObjectType *objc_class_type =
5474 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5475 assert(objc_class_type)((objc_class_type) ? static_cast<void> (0) : __assert_fail
("objc_class_type", "/build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 5475, __PRETTY_FUNCTION__))
;
5476 if (objc_class_type) {
5477 clang::ObjCInterfaceDecl *class_interface_decl =
5478 objc_class_type->getInterface();
5479
5480 if (class_interface_decl) {
5481
5482 clang::ObjCInterfaceDecl *superclass_interface_decl =
5483 class_interface_decl->getSuperClass();
5484 if (superclass_interface_decl) {
5485 if (omit_empty_base_classes) {
5486 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5487 ++num_children;
5488 } else
5489 ++num_children;
5490 }
5491
5492 num_children += class_interface_decl->ivar_size();
5493 }
5494 }
5495 }
5496 break;
5497
5498 case clang::Type::ObjCObjectPointer: {
5499 const clang::ObjCObjectPointerType *pointer_type =
5500 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5501 clang::QualType pointee_type = pointer_type->getPointeeType();
5502 uint32_t num_pointee_children =
5503 CompilerType(getASTContext(), pointee_type)
5504 .GetNumChildren(omit_empty_base_classes);
5505 // If this type points to a simple type, then it has 1 child
5506 if (num_pointee_children == 0)
5507 num_children = 1;
5508 else
5509 num_children = num_pointee_children;
5510 } break;
5511
5512 case clang::Type::Vector:
5513 case clang::Type::ExtVector:
5514 num_children =
5515 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5516 break;
5517
5518 case clang::Type::ConstantArray:
5519 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5520 ->getSize()
5521 .getLimitedValue();
5522 break;
5523
5524 case clang::Type::Pointer: {
5525 const clang::PointerType *pointer_type =
5526 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5527 clang::QualType pointee_type(pointer_type->getPointeeType());
5528 uint32_t num_pointee_children =
5529 CompilerType(getASTContext(), pointee_type)
5530 .GetNumChildren(omit_empty_base_classes);
5531 if (num_pointee_children == 0) {
5532 // We have a pointer to a pointee type that claims it has no children. We
5533 // will want to look at
5534 num_children = GetNumPointeeChildren(pointee_type);
5535 } else
5536 num_children = num_pointee_children;
5537 } break;
5538
5539 case clang::Type::LValueReference:
5540 case clang::Type::RValueReference: {
5541 const clang::ReferenceType *reference_type =
5542 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5543 clang::QualType pointee_type = reference_type->getPointeeType();
5544 uint32_t num_pointee_children =
5545 CompilerType(getASTContext(), pointee_type)
5546 .GetNumChildren(omit_empty_base_classes);
5547 // If this type points to a simple type, then it has 1 child
5548 if (num_pointee_children == 0)
5549 num_children = 1;
5550 else
5551 num_children = num_pointee_children;
5552 } break;
5553
5554 case clang::Type::Typedef:
5555 num_children =
5556 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5557 ->getDecl()
5558 ->getUnderlyingType())
5559 .GetNumChildren(omit_empty_base_classes);
5560 break;
5561
5562 case clang::Type::Auto:
5563 num_children =
5564 CompilerType(getASTContext(),
5565 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5566 .GetNumChildren(omit_empty_base_classes);
5567 break;
5568
5569 case clang::Type::Elaborated:
5570 num_children =
5571 CompilerType(
5572 getASTContext(),
5573 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5574 .GetNumChildren(omit_empty_base_classes);
5575 break;
5576
5577 case clang::Type::Paren:
5578 num_children =
5579 CompilerType(getASTContext(),
5580 llvm::cast<clang::ParenType>(qual_type)->desugar())
5581 .GetNumChildren(omit_empty_base_classes);
5582 break;
5583 default:
5584 break;
5585 }
5586 return num_children;
5587}
5588
5589CompilerType ClangASTContext::GetBuiltinTypeByName(const ConstString &name) {
5590 return GetBasicType(GetBasicTypeEnumeration(name));
5591}
5592
5593lldb::BasicType
5594ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5595 if (type) {
5596 clang::QualType qual_type(GetQualType(type));
5597 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5598 if (type_class == clang::Type::Builtin) {
5599 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5600 case clang::BuiltinType::Void:
5601 return eBasicTypeVoid;
5602 case clang::BuiltinType::Bool:
5603 return eBasicTypeBool;
5604 case clang::BuiltinType::Char_S:
5605 return eBasicTypeSignedChar;
5606 case clang::BuiltinType::Char_U:
5607 return eBasicTypeUnsignedChar;
5608 case clang::BuiltinType::Char16:
5609 return eBasicTypeChar16;
5610 case clang::BuiltinType::Char32:
5611 return eBasicTypeChar32;
5612 case clang::BuiltinType::UChar:
5613 return eBasicTypeUnsignedChar;
5614 case clang::BuiltinType::SChar:
5615 return eBasicTypeSignedChar;
5616 case clang::BuiltinType::WChar_S:
5617 return eBasicTypeSignedWChar;
5618 case clang::BuiltinType::WChar_U:
5619 return eBasicTypeUnsignedWChar;
5620 case clang::BuiltinType::Short:
5621 return eBasicTypeShort;
5622 case clang::BuiltinType::UShort:
5623 return eBasicTypeUnsignedShort;
5624 case clang::BuiltinType::Int:
5625 return eBasicTypeInt;
5626 case clang::BuiltinType::UInt:
5627 return eBasicTypeUnsignedInt;
5628 case clang::BuiltinType::Long:
5629 return eBasicTypeLong;
5630 case clang::BuiltinType::ULong:
5631 return eBasicTypeUnsignedLong;
5632 case clang::BuiltinType::LongLong:
5633 return eBasicTypeLongLong;
5634 case clang::BuiltinType::ULongLong:
5635 return eBasicTypeUnsignedLongLong;
5636 case clang::BuiltinType::Int128:
5637 return eBasicTypeInt128;
5638 case clang::BuiltinType::UInt128:
5639 return eBasicTypeUnsignedInt128;
5640
5641 case clang::BuiltinType::Half:
5642 return eBasicTypeHalf;
5643 case clang::BuiltinType::Float:
5644 return eBasicTypeFloat;
5645 case clang::BuiltinType::Double:
5646 return eBasicTypeDouble;
5647 case clang::BuiltinType::LongDouble:
5648 return eBasicTypeLongDouble;
5649
5650 case clang::BuiltinType::NullPtr:
5651 return eBasicTypeNullPtr;
5652 case clang::BuiltinType::ObjCId:
5653 return eBasicTypeObjCID;
5654 case clang::BuiltinType::ObjCClass:
5655 return eBasicTypeObjCClass;
5656 case clang::BuiltinType::ObjCSel:
5657 return eBasicTypeObjCSel;
5658 default:
5659 return eBasicTypeOther;
5660 }
5661 }
5662 }
5663 return eBasicTypeInvalid;
5664}
5665
5666void ClangASTContext::ForEachEnumerator(
5667 lldb::opaque_compiler_type_t type,
5668 std::function<bool(const CompilerType &integer_type,
5669 const ConstString &name,
5670 const llvm::APSInt &value)> const &callback) {
5671 const clang::EnumType *enum_type =
5672 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5673 if (enum_type) {
5674 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5675 if (enum_decl) {
5676 CompilerType integer_type(this,
5677 enum_decl->getIntegerType().getAsOpaquePtr());
5678
5679 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5680 for (enum_pos = enum_decl->enumerator_begin(),
5681 enum_end_pos = enum_decl->enumerator_end();
5682 enum_pos != enum_end_pos; ++enum_pos) {
5683 ConstString name(enum_pos->getNameAsString().c_str());
5684 if (!callback(integer_type, name, enum_pos->getInitVal()))
5685 break;
5686 }
5687 }
5688 }
5689}
5690
5691#pragma mark Aggregate Types
5692
5693uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5694 if (!type)
5695 return 0;
5696
5697 uint32_t count = 0;
5698 clang::QualType qual_type(GetCanonicalQualType(type));
5699 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5700 switch (type_class) {
5701 case clang::Type::Record:
5702 if (GetCompleteType(type)) {
5703 const clang::RecordType *record_type =
5704 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5705 if (record_type) {
5706 clang::RecordDecl *record_decl = record_type->getDecl();
5707 if (record_decl) {
5708 uint32_t field_idx = 0;
5709 clang::RecordDecl::field_iterator field, field_end;
5710 for (field = record_decl->field_begin(),
5711 field_end = record_decl->field_end();
5712 field != field_end; ++field)
5713 ++field_idx;
5714 count = field_idx;
5715 }
5716 }
5717 }
5718 break;
5719
5720 case clang::Type::Typedef:
5721 count =
5722 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5723 ->getDecl()
5724 ->getUnderlyingType())
5725 .GetNumFields();
5726 break;
5727
5728 case clang::Type::Auto:
5729 count =
5730 CompilerType(getASTContext(),
5731 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5732 .GetNumFields();
5733 break;
5734
5735 case clang::Type::Elaborated:
5736 count = CompilerType(
5737 getASTContext(),
5738 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5739 .GetNumFields();
5740 break;
5741
5742 case clang::Type::Paren:
5743 count = CompilerType(getASTContext(),
5744 llvm::cast<clang::ParenType>(qual_type)->desugar())
5745 .GetNumFields();
5746 break;
5747
5748 case clang::Type::ObjCObjectPointer: {
5749 const clang::ObjCObjectPointerType *objc_class_type =
5750 qual_type->getAs<clang::ObjCObjectPointerType>();
5751 const clang::ObjCInterfaceType *objc_interface_type =
5752 objc_class_type->getInterfaceType();
5753 if (objc_interface_type &&
5754 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5755 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
5756 clang::ObjCInterfaceDecl *class_interface_decl =
5757 objc_interface_type->getDecl();
5758 if (class_interface_decl) {
5759 count = class_interface_decl->ivar_size();
5760 }
5761 }
5762 break;
5763 }
5764
5765 case clang::Type::ObjCObject:
5766 case clang::Type::ObjCInterface:
5767 if (GetCompleteType(type)) {
5768 const clang::ObjCObjectType *objc_class_type =
5769 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5770 if (objc_class_type) {
5771 clang::ObjCInterfaceDecl *class_interface_decl =
5772 objc_class_type->getInterface();
5773
5774 if (class_interface_decl)
5775 count = class_interface_decl->ivar_size();
5776 }
5777 }
5778 break;
5779
5780 default:
5781 break;
5782 }
5783 return count;
5784}
5785
5786static lldb::opaque_compiler_type_t
5787GetObjCFieldAtIndex(clang::ASTContext *ast,
5788 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5789 std::string &name, uint64_t *bit_offset_ptr,
5790 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5791 if (class_interface_decl) {
5792 if (idx < (class_interface_decl->ivar_size())) {
5793 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5794 ivar_end = class_interface_decl->ivar_end();
5795 uint32_t ivar_idx = 0;
5796
5797 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5798 ++ivar_pos, ++ivar_idx) {
5799 if (ivar_idx == idx) {
5800 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5801
5802 clang::QualType ivar_qual_type(ivar_decl->getType());
5803
5804 name.assign(ivar_decl->getNameAsString());
5805
5806 if (bit_offset_ptr) {
5807 const clang::ASTRecordLayout &interface_layout =
5808 ast->getASTObjCInterfaceLayout(class_interface_decl);
5809 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5810 }
5811
5812 const bool is_bitfield = ivar_pos->isBitField();
5813
5814 if (bitfield_bit_size_ptr) {
5815 *bitfield_bit_size_ptr = 0;
5816
5817 if (is_bitfield && ast) {
5818 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5819 llvm::APSInt bitfield_apsint;
5820 if (bitfield_bit_size_expr &&
5821 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5822 *ast)) {
5823 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5824 }
5825 }
5826 }
5827 if (is_bitfield_ptr)
5828 *is_bitfield_ptr = is_bitfield;
5829
5830 return ivar_qual_type.getAsOpaquePtr();
5831 }
5832 }
5833 }
5834 }
5835 return nullptr;
5836}
5837
5838CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5839 size_t idx, std::string &name,
5840 uint64_t *bit_offset_ptr,
5841 uint32_t *bitfield_bit_size_ptr,
5842 bool *is_bitfield_ptr) {
5843 if (!type)
5844 return CompilerType();
5845
5846 clang::QualType qual_type(GetCanonicalQualType(type));
5847 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5848 switch (type_class) {
5849 case clang::Type::Record:
5850 if (GetCompleteType(type)) {
5851 const clang::RecordType *record_type =
5852 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5853 const clang::RecordDecl *record_decl = record_type->getDecl();
5854 uint32_t field_idx = 0;
5855 clang::RecordDecl::field_iterator field, field_end;
5856 for (field = record_decl->field_begin(),
5857 field_end = record_decl->field_end();
5858 field != field_end; ++field, ++field_idx) {
5859 if (idx == field_idx) {
5860 // Print the member type if requested
5861 // Print the member name and equal sign
5862 name.assign(field->getNameAsString());
5863
5864 // Figure out the type byte size (field_type_info.first) and
5865 // alignment (field_type_info.second) from the AST context.
5866 if (bit_offset_ptr) {
5867 const clang::ASTRecordLayout &record_layout =
5868 getASTContext()->getASTRecordLayout(record_decl);
5869 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
5870 }
5871
5872 const bool is_bitfield = field->isBitField();
5873
5874 if (bitfield_bit_size_ptr) {
5875 *bitfield_bit_size_ptr = 0;
5876
5877 if (is_bitfield) {
5878 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5879 llvm::APSInt bitfield_apsint;
5880 if (bitfield_bit_size_expr &&
5881 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5882 *getASTContext())) {
5883 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5884 }
5885 }
5886 }
5887 if (is_bitfield_ptr)
5888 *is_bitfield_ptr = is_bitfield;
5889
5890 return CompilerType(getASTContext(), field->getType());
5891 }
5892 }
5893 }
5894 break;
5895
5896 case clang::Type::ObjCObjectPointer: {
5897 const clang::ObjCObjectPointerType *objc_class_type =
5898 qual_type->getAs<clang::ObjCObjectPointerType>();
5899 const clang::ObjCInterfaceType *objc_interface_type =
5900 objc_class_type->getInterfaceType();
5901 if (objc_interface_type &&
5902 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5903 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
5904 clang::ObjCInterfaceDecl *class_interface_decl =
5905 objc_interface_type->getDecl();
5906 if (class_interface_decl) {
5907 return CompilerType(
5908 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5909 idx, name, bit_offset_ptr,
5910 bitfield_bit_size_ptr, is_bitfield_ptr));
5911 }
5912 }
5913 break;
5914 }
5915
5916 case clang::Type::ObjCObject:
5917 case clang::Type::ObjCInterface:
5918 if (GetCompleteType(type)) {
5919 const clang::ObjCObjectType *objc_class_type =
5920 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5921 assert(objc_class_type)((objc_class_type) ? static_cast<void> (0) : __assert_fail
("objc_class_type", "/build/llvm-toolchain-snapshot-8~svn345461/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 5921, __PRETTY_FUNCTION__))
;
5922 if (objc_class_type) {
5923 clang::ObjCInterfaceDecl *class_interface_decl =
5924 objc_class_type->getInterface();
5925 return CompilerType(
5926 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5927 idx, name, bit_offset_ptr,
5928 bitfield_bit_size_ptr, is_bitfield_ptr));
5929 }
5930 }
5931 break;
5932
5933 case clang::Type::Typedef:
5934 return CompilerType(getASTContext(),
5935 llvm::cast<clang::TypedefType>(qual_type)
5936 ->getDecl()
5937 ->getUnderlyingType())
5938 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5939 is_bitfield_ptr);
5940
5941 case clang::Type::Auto:
5942 return CompilerType(
5943 getASTContext(),
5944 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5945 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5946 is_bitfield_ptr);
5947
5948 case clang::Type::Elaborated:
5949 return CompilerType(
5950 getASTContext(),
5951 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5952 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5953 is_bitfield_ptr);
5954
5955 case clang::Type::Paren:
5956 return CompilerType(getASTContext(),
5957 llvm::cast<clang::ParenType>(qual_type)->desugar())
5958 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5959 is_bitfield_ptr);
5960
5961 default:
5962 break;
5963 }
5964 return CompilerType();
5965}
5966
5967uint32_t
5968ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
5969 uint32_t count = 0;
5970 clang::QualType qual_type(GetCanonicalQualType(type));
5971 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5972 switch (type_class) {
5973 case clang::Type::Record:
5974 if (GetCompleteType(type)) {
5975 const clang::CXXRecordDecl *cxx_record_decl =
5976 qual_type->getAsCXXRecordDecl();
5977 if (cxx_record_decl)
5978 count = cxx_record_decl->getNumBases();
5979 }
5980 break;
5981
5982 case clang::Type::ObjCObjectPointer:
5983 count = GetPointeeType(type).GetNumDirectBaseClasses();
5984 break;
5985
5986 case clang::Type::ObjCObject:
5987 if (GetCompleteType(type)) {
5988 const clang::ObjCObjectType *objc_class_type =
5989 qual_type->getAsObjCQualifiedInterfaceType();
5990 if (objc_class_type) {
5991 clang::ObjCInterfaceDecl *class_interface_decl =
5992 objc_class_type->getInterface();
5993
5994 if (class_interface_decl && class_interface_decl->getSuperClass())
5995 count = 1;
5996 }
5997 }
5998 break;
5999 case clang::Type::ObjCInterface:
6000 if (GetCompleteType(type)) {
6001 const clang::ObjCInterfaceType *objc_interface_type =
6002 qual_type->getAs<clang::ObjCInterfaceType>();
6003 if (objc_interface_type) {
6004 clang::ObjCInterfaceDecl *class_interface_decl =
6005 objc_interface_type->getInterface();
6006
6007 if (class_interface_decl && class_interface_decl->getSuperClass())
6008 count = 1;
6009 }
6010 }
6011 break;
6012
6013 case clang::Type::Typedef:
6014 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6015 ->getDecl()
6016 ->getUnderlyingType()
6017 .getAsOpaquePtr());
6018 break;
6019
6020 case clang::Type::Auto:
6021 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6022 ->getDeducedType()
6023 .getAsOpaquePtr());
6024 break;
6025
6026 case clang::Type::Elaborated:
6027 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6028 ->getNamedType()
6029 .getAsOpaquePtr());
6030 break;
6031
6032 case clang::Type::Paren:
6033 return GetNumDirectBaseClasses(
6034 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6035
6036 default:
6037 break;
6038 }
6039 return count;
6040}
6041
6042uint32_t
6043ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
6044 uint32_t count = 0;
6045 clang::QualType qual_type(GetCanonicalQualType(type));
6046 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6047 switch (type_class) {
6048 case clang::Type::Record:
6049 if (GetCompleteType(type)) {
6050 const clang::CXXRecordDecl *cxx_record_decl =
6051 qual_type->getAsCXXRecordDecl();
6052 if (cxx_record_decl)
6053 count = cxx_record_decl->getNumVBases();
6054 }
6055 break;
6056
6057 case clang::Type::Typedef:
6058 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6059 ->getDecl()
6060 ->getUnderlyingType()
6061 .getAsOpaquePtr());
6062 break;
6063
6064 case clang::Type::Auto:
6065 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6066 ->getDeducedType()
6067 .getAsOpaquePtr());
6068 break;
6069
6070 case clang::Type::Elaborated:
6071 count =
6072 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6073 ->getNamedType()
6074 .getAsOpaquePtr());
6075 break;
6076
6077 case clang::Type::Paren:
6078 count = GetNumVirtualBaseClasses(
6079 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6080 break;
6081
6082 default:
6083 break;
6084 }
6085 return count;
6086}
6087
6088CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
6089 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6090 clang::QualType qual_type(GetCanonicalQualType(type));
6091 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6092 switch (type_class) {
6093 case clang::Type::Record:
6094 if (GetCompleteType(type)) {
6095 const clang::CXXRecordDecl *cxx_record_decl =
6096 qual_type->getAsCXXRecordDecl();
6097 if (cxx_record_decl) {
6098 uint32_t curr_idx = 0;
6099 clang::CXXRecordDecl::base_class_const_iterator base_class,
6100 base_class_end;
6101 for (base_class = cxx_record_decl->bases_begin(),
6102 base_class_end = cxx_record_decl->bases_end();
6103 base_class != base_class_end; ++base_class, ++curr_idx) {
6104 if (curr_idx == idx) {
6105 if (bit_offset_ptr) {
6106 const clang::ASTRecordLayout &record_layout =
6107 getASTContext()->getASTRecordLayout(cxx_record_decl);
6108 const clang::CXXRecordDecl *base_class_decl =
6109 llvm::cast<clang::CXXRecordDecl>(
6110 base_class->getType()
6111 ->getAs<clang::RecordType>()
6112 ->getDecl());
6113 if (base_class->isVirtual())
6114 *bit_offset_ptr =
6115 record_layout.getVBaseClassOffset(base_class_decl)
6116 .getQuantity() *
6117 8;
6118 else
6119 *bit_offset_ptr =
6120 record_layout.getBaseClassOffset(base_class_decl)
6121 .getQuantity() *
6122 8;
6123 }
6124 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6125 }
6126 }
6127 }
6128 }
6129 break;
6130
6131 case clang::Type::ObjCObjectPointer:
6132 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6133
6134 case clang::Type::ObjCObject:
6135 if (idx == 0 && GetCompleteType(type)) {
6136 const clang::ObjCObjectType *objc_class_type =
6137 qual_type->getAsObjCQualifiedInterfaceType();
6138 if (objc_class_type) {
6139 clang::ObjCInterfaceDecl *class_interface_decl =
6140 objc_class_type->getInterface();
6141
6142 if (class_interface_decl) {
6143 clang::ObjCInterfaceDecl *superclass_interface_decl =
6144 class_interface_decl->getSuperClass();
6145 if (superclass_interface_decl) {
6146 if (bit_offset_ptr)
6147 *bit_offset_ptr = 0;
6148 return CompilerType(getASTContext(),
6149 getASTContext()->getObjCInterfaceType(
6150 superclass_interface_decl));
6151 }
6152 }
6153 }
6154 }
6155 break;
6156 case clang::Type::ObjCInterface:
6157 if (idx == 0 && GetCompleteType(type)) {
6158 const clang::ObjCObjectType *objc_interface_type =
6159 qual_type->getAs<clang::ObjCInterfaceType>();
6160 if (objc_interface_type) {
6161 clang::ObjCInterfaceDecl *class_interface_decl =
6162 objc_interface_type->getInterface();
6163
6164 if (class_interface_decl) {
6165 clang::ObjCInterfaceDecl *superclass_interface_decl =
6166 class_interface_decl->getSuperClass();
6167 if (superclass_interface_decl) {
6168 if (bit_offset_ptr)
6169 *bit_offset_ptr = 0;
6170 return CompilerType(getASTContext(),
6171 getASTContext()->getObjCInterfaceType(
6172 superclass_interface_decl));
6173 }
6174 }
6175 }
6176 }
6177 break;
6178
6179 case clang::Type::Typedef:
6180 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6181 ->getDecl()
6182 ->getUnderlyingType()
6183 .getAsOpaquePtr(),
6184 idx, bit_offset_ptr);
6185
6186 case clang::Type::Auto:
6187 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6188 ->getDeducedType()
6189 .getAsOpaquePtr(),
6190 idx, bit_offset_ptr);
6191
6192 case clang::Type::Elaborated:
6193 return GetDirectBaseClassAtIndex(
6194 llvm::cast<clang::ElaboratedType>(qual_type)
6195 ->getNamedType()
6196 .getAsOpaquePtr(),
6197 idx, bit_offset_ptr);
6198
6199 case clang::Type::Paren:
6200 return GetDirectBaseClassAtIndex(
6201 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6202 idx, bit_offset_ptr);
6203
6204 default:
6205 break;
6206 }
6207 return CompilerType();
6208}
6209
6210CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6211 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6212 clang::QualType qual_type(GetCanonicalQualType(type));
6213 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6214 switch (type_class) {
6215 case clang::Type::Record:
6216 if (GetCompleteType(type)) {
6217 const clang::CXXRecordDecl *cxx_record_decl =
6218 qual_type->getAsCXXRecordDecl();
6219 if (cxx_record_decl) {
6220 uint32_t curr_idx = 0;
6221 clang::CXXRecordDecl::base_class_const_iterator base_class,
6222 base_class_end;
6223 for (base_class = cxx_record_decl->vbases_begin(),
6224 base_class_end = cxx_record_decl->vbases_end();
6225 base_class != base_class_end; ++base_class, ++curr_idx) {
6226 if (curr_idx == idx) {
6227 if (bit_offset_ptr) {
6228 const clang::ASTRecordLayout &record_layout =
6229 getASTContext()->getASTRecordLayout(cxx_record_decl);
6230 const clang::CXXRecordDecl *base_class_decl =
6231 llvm::cast<clang::CXXRecordDecl>(
6232 base_class->getType()
6233 ->getAs<clang::RecordType>()
6234 ->getDecl());
6235 *bit_offset_ptr =
6236 record_layout.getVBaseClassOffset(base_class_decl)
6237 .getQuantity() *
6238 8;
6239 }
6240 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6241 }
6242 }
6243 }
6244 }
6245 break;
6246
6247 case clang::Type::Typedef:
6248 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6249 ->getDecl()
6250 ->getUnderlyingType()
6251 .getAsOpaquePtr(),
6252 idx, bit_offset_ptr);
6253
6254 case clang::Type::Auto:
6255 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6256 ->getDeducedType()
6257 .getAsOpaquePtr(),
6258 idx, bit_offset_ptr);
6259
6260 case clang::Type::Elaborated:
6261 return GetVirtualBaseClassAtIndex(
6262 llvm::cast<clang::ElaboratedType>(qual_type)
6263 ->getNamedType()
6264 .getAsOpaquePtr(),
6265 idx, bit_offset_ptr);
6266
6267 case clang::Type::Paren:
6268 return GetVirtualBaseClassAtIndex(
6269 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6270 idx, bit_offset_ptr);
6271
6272 default:
6273 break;
6274 }
6275 return CompilerType();
6276}
6277
6278// If a pointer to a pointee type (the clang_type arg) says that it has no
6279// children, then we either need to trust it, or override it and return a
6280// different result. For example, an "int *" has one child that is an integer,
6281// but a function pointer doesn't have any children. Likewise if a Record type
6282// claims it has no children, then there really is nothing to show.
6283uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6284 if (type.isNull())
6285 return 0;
6286
6287 clang::QualType qual_type(type.getCanonicalType());
6288 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6289 switch (type_class) {
6290 case clang::Type::Builtin:
6291 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6292 case clang::BuiltinType::UnknownAny:
6293 case clang::BuiltinType::Void:
6294 case clang::BuiltinType::NullPtr:
6295 case clang::BuiltinType::OCLEvent:
6296 case clang::BuiltinType::OCLImage1dRO:
6297 case clang::BuiltinType::OCLImage1dWO:
6298 case clang::BuiltinType::OCLImage1dRW:
6299 case clang::BuiltinType::OCLImage1dArrayRO:
6300 case clang::BuiltinType::OCLImage1dArrayWO:
6301 case clang::BuiltinType::OCLImage1dArrayRW:
6302 case clang::BuiltinType::OCLImage1dBufferRO:
6303 case clang::BuiltinType::OCLImage1dBufferWO:
6304 case clang::BuiltinType::OCLImage1dBufferRW:
6305 case clang::BuiltinType::OCLImage2dRO:
6306 case clang::BuiltinType::OCLImage2dWO:
6307 case clang::BuiltinType::OCLImage2dRW:
6308 case clang::BuiltinType::OCLImage2dArrayRO:
6309 case clang::BuiltinType::OCLImage2dArrayWO:
6310 case clang::BuiltinType::OCLImage2dArrayRW:
6311 case clang::BuiltinType::OCLImage3dRO:
6312 case clang::BuiltinType::OCLImage3dWO:
6313 case clang::BuiltinType::OCLImage3dRW:
6314 case clang::BuiltinType::OCLSampler:
6315 return 0;
6316 case clang::BuiltinType::Bool:
6317 case clang::BuiltinType::Char_U:
6318 case clang::BuiltinType::UChar:
6319 case clang::BuiltinType::WChar_U:
6320 case clang::BuiltinType::Char16:
6321 case clang::BuiltinType::Char32:
6322 case clang::BuiltinType::UShort:
6323 case clang::BuiltinType::UInt:
6324 case clang::BuiltinType::ULong:
6325 case clang::BuiltinType::ULongLong:
6326 case clang::BuiltinType::UInt128:
6327 case clang::BuiltinType::Char_S:
6328 case clang::BuiltinType::SChar:
6329 case clang::BuiltinType::WChar_S:
6330 case clang::BuiltinType::Short:
6331 case clang::BuiltinType::Int:
6332 case clang::BuiltinType::Long:
6333 case clang::BuiltinType::LongLong:
6334 case clang::BuiltinType::Int128:
6335 case clang::BuiltinType::Float:
6336 case clang::BuiltinType::Double:
6337 case clang::BuiltinType::LongDouble:
6338 case clang::BuiltinType::Dependent:
6339 case clang::BuiltinType::Overload:
6340 case clang::BuiltinType::ObjCId: