Bug Summary

File:tools/lldb/source/Symbol/ClangASTContext.cpp
Warning:line 8265, column 5
Called C++ object pointer is null

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-eagerly-assume -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-7/lib/clang/7.0.0 -D HAVE_ROUND -D LLDB_CONFIGURATION_RELEASE -D LLDB_USE_BUILTIN_DEMANGLER -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/lldb/source/Symbol -I /build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/lldb/include -I /build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/include -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn329677/include -I /usr/include/python2.7 -I /build/llvm-toolchain-snapshot-7~svn329677/tools/clang/include -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/lldb/../clang/include -I /build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/. -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.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-7~svn329677/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-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-04-11-031539-24776-1 -x c++ /build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp

/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp

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