Bug Summary

File:tools/lldb/source/Symbol/ClangASTContext.cpp
Warning:line 757, column 10
Potential leak of memory pointed to by 'ast_source_ap.Obj'

Annotated Source Code

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
141static bool 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-6.0~svn318882/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 // if (Opts.CPlusPlus)
439 // Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
440 //
441 // if (Args.hasArg(OPT_fobjc_gc_only))
442 // Opts.setGCMode(LangOptions::GCOnly);
443 // else if (Args.hasArg(OPT_fobjc_gc))
444 // Opts.setGCMode(LangOptions::HybridGC);
445 //
446 // if (Args.hasArg(OPT_print_ivar_layout))
447 // Opts.ObjCGCBitmapPrint = 1;
448 //
449 // if (Args.hasArg(OPT_faltivec))
450 // Opts.AltiVec = 1;
451 //
452 // if (Args.hasArg(OPT_pthread))
453 // Opts.POSIXThreads = 1;
454 //
455 // llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
456 // "default");
457 // if (Vis == "default")
458 Opts.setValueVisibilityMode(DefaultVisibility);
459 // else if (Vis == "hidden")
460 // Opts.setVisibilityMode(LangOptions::Hidden);
461 // else if (Vis == "protected")
462 // Opts.setVisibilityMode(LangOptions::Protected);
463 // else
464 // Diags.Report(diag::err_drv_invalid_value)
465 // << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
466
467 // Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
468
469 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
470 // is specified, or -std is set to a conforming mode.
471 Opts.Trigraphs = !Opts.GNUMode;
472 // if (Args.hasArg(OPT_trigraphs))
473 // Opts.Trigraphs = 1;
474 //
475 // Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
476 // OPT_fno_dollars_in_identifiers,
477 // !Opts.AsmPreprocessor);
478 // Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
479 // Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
480 // Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
481 // if (Args.hasArg(OPT_fno_lax_vector_conversions))
482 // Opts.LaxVectorConversions = 0;
483 // Opts.Exceptions = Args.hasArg(OPT_fexceptions);
484 // Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
485 // Opts.Blocks = Args.hasArg(OPT_fblocks);
486 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
487 // Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
488 // Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
489 // Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
490 // Opts.AssumeSaneOperatorNew =
491 // !Args.hasArg(OPT_fno_assume_sane_operator_new);
492 // Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
493 // Opts.AccessControl = Args.hasArg(OPT_faccess_control);
494 // Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
495 // Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
496 // Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth,
497 // 99,
498 // Diags);
499 // Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
500 // Opts.ObjCConstantStringClass = getLastArgValue(Args,
501 // OPT_fconstant_string_class);
502 // Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
503 // Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
504 // Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
505 // Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
506 // Opts.Static = Args.hasArg(OPT_static_define);
507 Opts.OptimizeSize = 0;
508
509 // FIXME: Eliminate this dependency.
510 // unsigned Opt =
511 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
512 // Opts.Optimize = Opt != 0;
513 unsigned Opt = 0;
514
515 // This is the __NO_INLINE__ define, which just depends on things like the
516 // optimization level and -fno-inline, not actually whether the backend has
517 // inlining enabled.
518 //
519 // FIXME: This is affected by other options (-fno-inline).
520 Opts.NoInlineDefine = !Opt;
521
522 // unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
523 // switch (SSP) {
524 // default:
525 // Diags.Report(diag::err_drv_invalid_value)
526 // << Args.getLastArg(OPT_stack_protector)->getAsString(Args) <<
527 // SSP;
528 // break;
529 // case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
530 // case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
531 // case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
532 // }
533}
534
535ClangASTContext::ClangASTContext(const char *target_triple)
536 : TypeSystem(TypeSystem::eKindClang), m_target_triple(), m_ast_ap(),
537 m_language_options_ap(), m_source_manager_ap(), m_diagnostics_engine_ap(),
538 m_target_options_rp(), m_target_info_ap(), m_identifier_table_ap(),
539 m_selector_table_ap(), m_builtins_ap(), m_callback_tag_decl(nullptr),
540 m_callback_objc_decl(nullptr), m_callback_baton(nullptr),
541 m_pointer_byte_size(0), m_ast_owned(false) {
542 if (target_triple && target_triple[0])
543 SetTargetTriple(target_triple);
544}
545
546//----------------------------------------------------------------------
547// Destructor
548//----------------------------------------------------------------------
549ClangASTContext::~ClangASTContext() { Finalize(); }
550
551ConstString ClangASTContext::GetPluginNameStatic() {
552 return ConstString("clang");
553}
554
555ConstString ClangASTContext::GetPluginName() {
556 return ClangASTContext::GetPluginNameStatic();
557}
558
559uint32_t ClangASTContext::GetPluginVersion() { return 1; }
560
561lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
562 lldb_private::Module *module,
563 Target *target) {
564 if (ClangASTContextSupportsLanguage(language)) {
565 ArchSpec arch;
566 if (module)
567 arch = module->GetArchitecture();
568 else if (target)
569 arch = target->GetArchitecture();
570
571 if (arch.IsValid()) {
572 ArchSpec fixed_arch = arch;
573 // LLVM wants this to be set to iOS or MacOSX; if we're working on
574 // a bare-boards type image, change the triple for llvm's benefit.
575 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
576 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) {
577 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
578 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
579 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) {
580 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
581 } else {
582 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
583 }
584 }
585
586 if (module) {
587 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
588 if (ast_sp) {
589 ast_sp->SetArchitecture(fixed_arch);
590 }
591 return ast_sp;
592 } else if (target && target->IsValid()) {
593 std::shared_ptr<ClangASTContextForExpressions> ast_sp(
594 new ClangASTContextForExpressions(*target));
595 if (ast_sp) {
596 ast_sp->SetArchitecture(fixed_arch);
597 ast_sp->m_scratch_ast_source_ap.reset(
598 new ClangASTSource(target->shared_from_this()));
599 lldbassert(ast_sp->getFileManager())lldb_private::lldb_assert(ast_sp->getFileManager(), "ast_sp->getFileManager()"
, __FUNCTION__, "/build/llvm-toolchain-snapshot-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 599)
;
600 ast_sp->m_scratch_ast_source_ap->InstallASTContext(
601 *ast_sp->getASTContext(), *ast_sp->getFileManager(), true);
602 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
603 ast_sp->m_scratch_ast_source_ap->CreateProxy());
604 ast_sp->SetExternalSource(proxy_ast_source);
605 return ast_sp;
606 }
607 }
608 }
609 }
610 return lldb::TypeSystemSP();
611}
612
613void ClangASTContext::EnumerateSupportedLanguages(
614 std::set<lldb::LanguageType> &languages_for_types,
615 std::set<lldb::LanguageType> &languages_for_expressions) {
616 static std::vector<lldb::LanguageType> s_supported_languages_for_types(
617 {lldb::eLanguageTypeC89, lldb::eLanguageTypeC, lldb::eLanguageTypeC11,
618 lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeC99,
619 lldb::eLanguageTypeObjC, lldb::eLanguageTypeObjC_plus_plus,
620 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
621 lldb::eLanguageTypeC11, lldb::eLanguageTypeC_plus_plus_14});
622
623 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions(
624 {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC_plus_plus,
625 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
626 lldb::eLanguageTypeC_plus_plus_14});
627
628 languages_for_types.insert(s_supported_languages_for_types.begin(),
629 s_supported_languages_for_types.end());
630 languages_for_expressions.insert(
631 s_supported_languages_for_expressions.begin(),
632 s_supported_languages_for_expressions.end());
633}
634
635void ClangASTContext::Initialize() {
636 PluginManager::RegisterPlugin(GetPluginNameStatic(),
637 "clang base AST context plug-in",
638 CreateInstance, EnumerateSupportedLanguages);
639}
640
641void ClangASTContext::Terminate() {
642 PluginManager::UnregisterPlugin(CreateInstance);
643}
644
645void ClangASTContext::Finalize() {
646 if (m_ast_ap.get()) {
647 GetASTMap().Erase(m_ast_ap.get());
648 if (!m_ast_owned)
649 m_ast_ap.release();
650 }
651
652 m_builtins_ap.reset();
653 m_selector_table_ap.reset();
654 m_identifier_table_ap.reset();
655 m_target_info_ap.reset();
656 m_target_options_rp.reset();
657 m_diagnostics_engine_ap.reset();
658 m_source_manager_ap.reset();
659 m_language_options_ap.reset();
660 m_ast_ap.reset();
661 m_scratch_ast_source_ap.reset();
662}
663
664void ClangASTContext::Clear() {
665 m_ast_ap.reset();
666 m_language_options_ap.reset();
667 m_source_manager_ap.reset();
668 m_diagnostics_engine_ap.reset();
669 m_target_options_rp.reset();
670 m_target_info_ap.reset();
671 m_identifier_table_ap.reset();
672 m_selector_table_ap.reset();
673 m_builtins_ap.reset();
674 m_pointer_byte_size = 0;
675}
676
677const char *ClangASTContext::GetTargetTriple() {
678 return m_target_triple.c_str();
679}
680
681void ClangASTContext::SetTargetTriple(const char *target_triple) {
682 Clear();
683 m_target_triple.assign(target_triple);
684}
685
686void ClangASTContext::SetArchitecture(const ArchSpec &arch) {
687 SetTargetTriple(arch.GetTriple().str().c_str());
688}
689
690bool ClangASTContext::HasExternalSource() {
691 ASTContext *ast = getASTContext();
692 if (ast)
693 return ast->getExternalSource() != nullptr;
694 return false;
695}
696
697void ClangASTContext::SetExternalSource(
698 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap) {
699 ASTContext *ast = getASTContext();
700 if (ast) {
701 ast->setExternalSource(ast_source_ap);
702 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
703 // ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
704 }
705}
706
707void ClangASTContext::RemoveExternalSource() {
708 ASTContext *ast = getASTContext();
709
710 if (ast) {
711 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
712 ast->setExternalSource(empty_ast_source_ap);
713 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
714 // ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
715 }
716}
717
718void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) {
719 if (!m_ast_owned) {
720 m_ast_ap.release();
721 }
722 m_ast_owned = false;
723 m_ast_ap.reset(ast_ctx);
724 GetASTMap().Insert(ast_ctx, this);
725}
726
727ASTContext *ClangASTContext::getASTContext() {
728 if (m_ast_ap.get() == nullptr) {
2
Assuming the condition is true
3
Taking true branch
729 m_ast_owned = true;
730 m_ast_ap.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
731 *getIdentifierTable(), *getSelectorTable(),
732 *getBuiltinContext()));
733
734 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
735
736 // This can be NULL if we don't know anything about the architecture or if
737 // the
738 // target for an architecture isn't enabled in the llvm/clang that we built
739 TargetInfo *target_info = getTargetInfo();
740 if (target_info)
4
Taking true branch
741 m_ast_ap->InitBuiltinTypes(*target_info);
742
743 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) {
5
Assuming the condition is false
6
Assuming the condition is false
744 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
745 // m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
746 }
747
748 GetASTMap().Insert(m_ast_ap.get(), this);
749
750 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap(
751 new ClangExternalASTSourceCallbacks(
7
Memory is allocated
752 ClangASTContext::CompleteTagDecl,
753 ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
754 ClangASTContext::LayoutRecordType, this));
755 SetExternalSource(ast_source_ap);
756 }
757 return m_ast_ap.get();
8
Potential leak of memory pointed to by 'ast_source_ap.Obj'
758}
759
760ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
761 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
762 return clang_ast;
763}
764
765Builtin::Context *ClangASTContext::getBuiltinContext() {
766 if (m_builtins_ap.get() == nullptr)
767 m_builtins_ap.reset(new Builtin::Context());
768 return m_builtins_ap.get();
769}
770
771IdentifierTable *ClangASTContext::getIdentifierTable() {
772 if (m_identifier_table_ap.get() == nullptr)
773 m_identifier_table_ap.reset(
774 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
775 return m_identifier_table_ap.get();
776}
777
778LangOptions *ClangASTContext::getLanguageOptions() {
779 if (m_language_options_ap.get() == nullptr) {
780 m_language_options_ap.reset(new LangOptions());
781 ParseLangArgs(*m_language_options_ap, InputKind::ObjCXX, GetTargetTriple());
782 // InitializeLangOptions(*m_language_options_ap, InputKind::ObjCXX);
783 }
784 return m_language_options_ap.get();
785}
786
787SelectorTable *ClangASTContext::getSelectorTable() {
788 if (m_selector_table_ap.get() == nullptr)
789 m_selector_table_ap.reset(new SelectorTable());
790 return m_selector_table_ap.get();
791}
792
793clang::FileManager *ClangASTContext::getFileManager() {
794 if (m_file_manager_ap.get() == nullptr) {
795 clang::FileSystemOptions file_system_options;
796 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
797 }
798 return m_file_manager_ap.get();
799}
800
801clang::SourceManager *ClangASTContext::getSourceManager() {
802 if (m_source_manager_ap.get() == nullptr)
803 m_source_manager_ap.reset(
804 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
805 return m_source_manager_ap.get();
806}
807
808clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() {
809 if (m_diagnostics_engine_ap.get() == nullptr) {
810 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
811 m_diagnostics_engine_ap.reset(
812 new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
813 }
814 return m_diagnostics_engine_ap.get();
815}
816
817clang::MangleContext *ClangASTContext::getMangleContext() {
818 if (m_mangle_ctx_ap.get() == nullptr)
819 m_mangle_ctx_ap.reset(getASTContext()->createMangleContext());
820 return m_mangle_ctx_ap.get();
821}
822
823class NullDiagnosticConsumer : public DiagnosticConsumer {
824public:
825 NullDiagnosticConsumer() {
826 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS(1u << 8));
827 }
828
829 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
830 const clang::Diagnostic &info) {
831 if (m_log) {
832 llvm::SmallVector<char, 32> diag_str(10);
833 info.FormatDiagnostic(diag_str);
834 diag_str.push_back('\0');
835 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
836 }
837 }
838
839 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
840 return new NullDiagnosticConsumer();
841 }
842
843private:
844 Log *m_log;
845};
846
847DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() {
848 if (m_diagnostic_consumer_ap.get() == nullptr)
849 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
850
851 return m_diagnostic_consumer_ap.get();
852}
853
854std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
855 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty()) {
856 m_target_options_rp = std::make_shared<clang::TargetOptions>();
857 if (m_target_options_rp.get() != nullptr)
858 m_target_options_rp->Triple = m_target_triple;
859 }
860 return m_target_options_rp;
861}
862
863TargetInfo *ClangASTContext::getTargetInfo() {
864 // target_triple should be something like "x86_64-apple-macosx"
865 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty())
866 m_target_info_ap.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
867 getTargetOptions()));
868 return m_target_info_ap.get();
869}
870
871#pragma mark Basic Types
872
873static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
874 ASTContext *ast, QualType qual_type) {
875 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
876 if (qual_type_bit_size == bit_size)
877 return true;
878 return false;
879}
880
881CompilerType
882ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
883 size_t bit_size) {
884 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
885 getASTContext(), encoding, bit_size);
886}
887
888CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
889 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
890 if (!ast)
891 return CompilerType();
892 switch (encoding) {
893 case eEncodingInvalid:
894 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
895 return CompilerType(ast, ast->VoidPtrTy);
896 break;
897
898 case eEncodingUint:
899 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
900 return CompilerType(ast, ast->UnsignedCharTy);
901 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
902 return CompilerType(ast, ast->UnsignedShortTy);
903 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
904 return CompilerType(ast, ast->UnsignedIntTy);
905 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
906 return CompilerType(ast, ast->UnsignedLongTy);
907 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
908 return CompilerType(ast, ast->UnsignedLongLongTy);
909 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
910 return CompilerType(ast, ast->UnsignedInt128Ty);
911 break;
912
913 case eEncodingSint:
914 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
915 return CompilerType(ast, ast->SignedCharTy);
916 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
917 return CompilerType(ast, ast->ShortTy);
918 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
919 return CompilerType(ast, ast->IntTy);
920 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
921 return CompilerType(ast, ast->LongTy);
922 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
923 return CompilerType(ast, ast->LongLongTy);
924 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
925 return CompilerType(ast, ast->Int128Ty);
926 break;
927
928 case eEncodingIEEE754:
929 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
930 return CompilerType(ast, ast->FloatTy);
931 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
932 return CompilerType(ast, ast->DoubleTy);
933 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
934 return CompilerType(ast, ast->LongDoubleTy);
935 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
936 return CompilerType(ast, ast->HalfTy);
937 break;
938
939 case eEncodingVector:
940 // Sanity check that bit_size is a multiple of 8's.
941 if (bit_size && !(bit_size & 0x7u))
942 return CompilerType(
943 ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8));
944 break;
945 }
946
947 return CompilerType();
948}
949
950lldb::BasicType
951ClangASTContext::GetBasicTypeEnumeration(const ConstString &name) {
952 if (name) {
953 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
954 static TypeNameToBasicTypeMap g_type_map;
955 static llvm::once_flag g_once_flag;
956 llvm::call_once(g_once_flag, []() {
957 // "void"
958 g_type_map.Append(ConstString("void"), eBasicTypeVoid);
959
960 // "char"
961 g_type_map.Append(ConstString("char"), eBasicTypeChar);
962 g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar);
963 g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar);
964 g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar);
965 g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar);
966 g_type_map.Append(ConstString("unsigned wchar_t"),
967 eBasicTypeUnsignedWChar);
968 // "short"
969 g_type_map.Append(ConstString("short"), eBasicTypeShort);
970 g_type_map.Append(ConstString("short int"), eBasicTypeShort);
971 g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort);
972 g_type_map.Append(ConstString("unsigned short int"),
973 eBasicTypeUnsignedShort);
974
975 // "int"
976 g_type_map.Append(ConstString("int"), eBasicTypeInt);
977 g_type_map.Append(ConstString("signed int"), eBasicTypeInt);
978 g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt);
979 g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt);
980
981 // "long"
982 g_type_map.Append(ConstString("long"), eBasicTypeLong);
983 g_type_map.Append(ConstString("long int"), eBasicTypeLong);
984 g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong);
985 g_type_map.Append(ConstString("unsigned long int"),
986 eBasicTypeUnsignedLong);
987
988 // "long long"
989 g_type_map.Append(ConstString("long long"), eBasicTypeLongLong);
990 g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong);
991 g_type_map.Append(ConstString("unsigned long long"),
992 eBasicTypeUnsignedLongLong);
993 g_type_map.Append(ConstString("unsigned long long int"),
994 eBasicTypeUnsignedLongLong);
995
996 // "int128"
997 g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128);
998 g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128);
999
1000 // Miscellaneous
1001 g_type_map.Append(ConstString("bool"), eBasicTypeBool);
1002 g_type_map.Append(ConstString("float"), eBasicTypeFloat);
1003 g_type_map.Append(ConstString("double"), eBasicTypeDouble);
1004 g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble);
1005 g_type_map.Append(ConstString("id"), eBasicTypeObjCID);
1006 g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel);
1007 g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr);
1008 g_type_map.Sort();
1009 });
1010
1011 return g_type_map.Find(name, eBasicTypeInvalid);
1012 }
1013 return eBasicTypeInvalid;
1014}
1015
1016CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1017 const ConstString &name) {
1018 if (ast) {
1019 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
1020 return ClangASTContext::GetBasicType(ast, basic_type);
1021 }
1022 return CompilerType();
1023}
1024
1025uint32_t ClangASTContext::GetPointerByteSize() {
1026 if (m_pointer_byte_size == 0)
1027 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid)
1028 .GetPointerType()
1029 .GetByteSize(nullptr);
1030 return m_pointer_byte_size;
1031}
1032
1033CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
1034 return GetBasicType(getASTContext(), basic_type);
1035}
1036
1037CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1038 lldb::BasicType basic_type) {
1039 if (!ast)
1040 return CompilerType();
1041 lldb::opaque_compiler_type_t clang_type =
1042 GetOpaqueCompilerType(ast, basic_type);
1043
1044 if (clang_type)
1045 return CompilerType(GetASTContext(ast), clang_type);
1046 return CompilerType();
1047}
1048
1049CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
1050 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
1051 ASTContext *ast = getASTContext();
1052
1053#define streq(a, b)strcmp(a, b) == 0 strcmp(a, b) == 0
1054 assert(ast != nullptr)(static_cast <bool> (ast != nullptr) ? void (0) : __assert_fail
("ast != nullptr", "/build/llvm-toolchain-snapshot-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1054, __extension__ __PRETTY_FUNCTION__))
;
1055 if (ast) {
1056 switch (dw_ate) {
1057 default:
1058 break;
1059
1060 case DW_ATE_address:
1061 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1062 return CompilerType(ast, ast->VoidPtrTy);
1063 break;
1064
1065 case DW_ATE_boolean:
1066 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
1067 return CompilerType(ast, ast->BoolTy);
1068 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1069 return CompilerType(ast, ast->UnsignedCharTy);
1070 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1071 return CompilerType(ast, ast->UnsignedShortTy);
1072 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1073 return CompilerType(ast, ast->UnsignedIntTy);
1074 break;
1075
1076 case DW_ATE_lo_user:
1077 // This has been seen to mean DW_AT_complex_integer
1078 if (type_name) {
1079 if (::strstr(type_name, "complex")) {
1080 CompilerType complex_int_clang_type =
1081 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1082 bit_size / 2);
1083 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1084 complex_int_clang_type)));
1085 }
1086 }
1087 break;
1088
1089 case DW_ATE_complex_float:
1090 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
1091 return CompilerType(ast, ast->FloatComplexTy);
1092 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
1093 return CompilerType(ast, ast->DoubleComplexTy);
1094 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
1095 return CompilerType(ast, ast->LongDoubleComplexTy);
1096 else {
1097 CompilerType complex_float_clang_type =
1098 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1099 bit_size / 2);
1100 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1101 complex_float_clang_type)));
1102 }
1103 break;
1104
1105 case DW_ATE_float:
1106 if (streq(type_name, "float")strcmp(type_name, "float") == 0 &&
1107 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1108 return CompilerType(ast, ast->FloatTy);
1109 if (streq(type_name, "double")strcmp(type_name, "double") == 0 &&
1110 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1111 return CompilerType(ast, ast->DoubleTy);
1112 if (streq(type_name, "long double")strcmp(type_name, "long double") == 0 &&
1113 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1114 return CompilerType(ast, ast->LongDoubleTy);
1115 // Fall back to not requiring a name match
1116 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1117 return CompilerType(ast, ast->FloatTy);
1118 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1119 return CompilerType(ast, ast->DoubleTy);
1120 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1121 return CompilerType(ast, ast->LongDoubleTy);
1122 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1123 return CompilerType(ast, ast->HalfTy);
1124 break;
1125
1126 case DW_ATE_signed:
1127 if (type_name) {
1128 if (streq(type_name, "wchar_t")strcmp(type_name, "wchar_t") == 0 &&
1129 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1130 (getTargetInfo() &&
1131 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1132 return CompilerType(ast, ast->WCharTy);
1133 if (streq(type_name, "void")strcmp(type_name, "void") == 0 &&
1134 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
1135 return CompilerType(ast, ast->VoidTy);
1136 if (strstr(type_name, "long long") &&
1137 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1138 return CompilerType(ast, ast->LongLongTy);
1139 if (strstr(type_name, "long") &&
1140 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1141 return CompilerType(ast, ast->LongTy);
1142 if (strstr(type_name, "short") &&
1143 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1144 return CompilerType(ast, ast->ShortTy);
1145 if (strstr(type_name, "char")) {
1146 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1147 return CompilerType(ast, ast->CharTy);
1148 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1149 return CompilerType(ast, ast->SignedCharTy);
1150 }
1151 if (strstr(type_name, "int")) {
1152 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1153 return CompilerType(ast, ast->IntTy);
1154 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1155 return CompilerType(ast, ast->Int128Ty);
1156 }
1157 }
1158 // We weren't able to match up a type name, just search by size
1159 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1160 return CompilerType(ast, ast->CharTy);
1161 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1162 return CompilerType(ast, ast->ShortTy);
1163 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1164 return CompilerType(ast, ast->IntTy);
1165 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1166 return CompilerType(ast, ast->LongTy);
1167 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1168 return CompilerType(ast, ast->LongLongTy);
1169 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1170 return CompilerType(ast, ast->Int128Ty);
1171 break;
1172
1173 case DW_ATE_signed_char:
1174 if (ast->getLangOpts().CharIsSigned && type_name &&
1175 streq(type_name, "char")strcmp(type_name, "char") == 0) {
1176 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1177 return CompilerType(ast, ast->CharTy);
1178 }
1179 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1180 return CompilerType(ast, ast->SignedCharTy);
1181 break;
1182
1183 case DW_ATE_unsigned:
1184 if (type_name) {
1185 if (streq(type_name, "wchar_t")strcmp(type_name, "wchar_t") == 0) {
1186 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1187 if (!(getTargetInfo() &&
1188 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1189 return CompilerType(ast, ast->WCharTy);
1190 }
1191 }
1192 if (strstr(type_name, "long long")) {
1193 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1194 return CompilerType(ast, ast->UnsignedLongLongTy);
1195 } else if (strstr(type_name, "long")) {
1196 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1197 return CompilerType(ast, ast->UnsignedLongTy);
1198 } else if (strstr(type_name, "short")) {
1199 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1200 return CompilerType(ast, ast->UnsignedShortTy);
1201 } else if (strstr(type_name, "char")) {
1202 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1203 return CompilerType(ast, ast->UnsignedCharTy);
1204 } else if (strstr(type_name, "int")) {
1205 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1206 return CompilerType(ast, ast->UnsignedIntTy);
1207 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1208 return CompilerType(ast, ast->UnsignedInt128Ty);
1209 }
1210 }
1211 // We weren't able to match up a type name, just search by size
1212 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1213 return CompilerType(ast, ast->UnsignedCharTy);
1214 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1215 return CompilerType(ast, ast->UnsignedShortTy);
1216 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1217 return CompilerType(ast, ast->UnsignedIntTy);
1218 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1219 return CompilerType(ast, ast->UnsignedLongTy);
1220 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1221 return CompilerType(ast, ast->UnsignedLongLongTy);
1222 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1223 return CompilerType(ast, ast->UnsignedInt128Ty);
1224 break;
1225
1226 case DW_ATE_unsigned_char:
1227 if (!ast->getLangOpts().CharIsSigned && type_name &&
1228 streq(type_name, "char")strcmp(type_name, "char") == 0) {
1229 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1230 return CompilerType(ast, ast->CharTy);
1231 }
1232 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1233 return CompilerType(ast, ast->UnsignedCharTy);
1234 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1235 return CompilerType(ast, ast->UnsignedShortTy);
1236 break;
1237
1238 case DW_ATE_imaginary_float:
1239 break;
1240
1241 case DW_ATE_UTF:
1242 if (type_name) {
1243 if (streq(type_name, "char16_t")strcmp(type_name, "char16_t") == 0) {
1244 return CompilerType(ast, ast->Char16Ty);
1245 } else if (streq(type_name, "char32_t")strcmp(type_name, "char32_t") == 0) {
1246 return CompilerType(ast, ast->Char32Ty);
1247 }
1248 }
1249 break;
1250 }
1251 }
1252 // This assert should fire for anything that we don't catch above so we know
1253 // to fix any issues we run into.
1254 if (type_name) {
1255 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1256 "DW_TAG_base_type '%s' encoded with "
1257 "DW_ATE = 0x%x, bit_size = %u\n",
1258 type_name, dw_ate, bit_size);
1259 } else {
1260 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1261 "DW_TAG_base_type encoded with "
1262 "DW_ATE = 0x%x, bit_size = %u\n",
1263 dw_ate, bit_size);
1264 }
1265 return CompilerType();
1266}
1267
1268CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) {
1269 if (ast)
1270 return CompilerType(ast, ast->UnknownAnyTy);
1271 return CompilerType();
1272}
1273
1274CompilerType ClangASTContext::GetCStringType(bool is_const) {
1275 ASTContext *ast = getASTContext();
1276 QualType char_type(ast->CharTy);
1277
1278 if (is_const)
1279 char_type.addConst();
1280
1281 return CompilerType(ast, ast->getPointerType(char_type));
1282}
1283
1284clang::DeclContext *
1285ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1286 return ast->getTranslationUnitDecl();
1287}
1288
1289clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1290 clang::Decl *source_decl) {
1291 FileSystemOptions file_system_options;
1292 FileManager file_manager(file_system_options);
1293 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1294
1295 return importer.Import(source_decl);
1296}
1297
1298bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1299 bool ignore_qualifiers) {
1300 ClangASTContext *ast =
1301 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1302 if (!ast || ast != type2.GetTypeSystem())
1303 return false;
1304
1305 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1306 return true;
1307
1308 QualType type1_qual = ClangUtil::GetQualType(type1);
1309 QualType type2_qual = ClangUtil::GetQualType(type2);
1310
1311 if (ignore_qualifiers) {
1312 type1_qual = type1_qual.getUnqualifiedType();
1313 type2_qual = type2_qual.getUnqualifiedType();
1314 }
1315
1316 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
1317}
1318
1319CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1320 if (clang::ObjCInterfaceDecl *interface_decl =
1321 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1322 return GetTypeForDecl(interface_decl);
1323 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1324 return GetTypeForDecl(tag_decl);
1325 return CompilerType();
1326}
1327
1328CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
1329 // No need to call the getASTContext() accessor (which can create the AST
1330 // if it isn't created yet, because we can't have created a decl in this
1331 // AST if our AST didn't already exist...
1332 ASTContext *ast = &decl->getASTContext();
1333 if (ast)
1334 return CompilerType(ast, ast->getTagDeclType(decl));
1335 return CompilerType();
1336}
1337
1338CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
1339 // No need to call the getASTContext() accessor (which can create the AST
1340 // if it isn't created yet, because we can't have created a decl in this
1341 // AST if our AST didn't already exist...
1342 ASTContext *ast = &decl->getASTContext();
1343 if (ast)
1344 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1345 return CompilerType();
1346}
1347
1348#pragma mark Structure, Unions, Classes
1349
1350CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx,
1351 AccessType access_type,
1352 const char *name, int kind,
1353 LanguageType language,
1354 ClangASTMetadata *metadata) {
1355 ASTContext *ast = getASTContext();
1356 assert(ast != nullptr)(static_cast <bool> (ast != nullptr) ? void (0) : __assert_fail
("ast != nullptr", "/build/llvm-toolchain-snapshot-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1356, __extension__ __PRETTY_FUNCTION__))
;
1357
1358 if (decl_ctx == nullptr)
1359 decl_ctx = ast->getTranslationUnitDecl();
1360
1361 if (language == eLanguageTypeObjC ||
1362 language == eLanguageTypeObjC_plus_plus) {
1363 bool isForwardDecl = true;
1364 bool isInternal = false;
1365 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1366 }
1367
1368 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1369 // we will need to update this code. I was told to currently always use
1370 // the CXXRecordDecl class since we often don't know from debug information
1371 // if something is struct or a class, so we default to always use the more
1372 // complete definition just in case.
1373
1374 bool is_anonymous = (!name) || (!name[0]);
1375
1376 CXXRecordDecl *decl = CXXRecordDecl::Create(
1377 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1378 SourceLocation(), is_anonymous ? nullptr : &ast->Idents.get(name));
1379
1380 if (is_anonymous)
1381 decl->setAnonymousStructOrUnion(true);
1382
1383 if (decl) {
1384 if (metadata)
1385 SetMetadata(ast, decl, *metadata);
1386
1387 if (access_type != eAccessNone)
1388 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1389
1390 if (decl_ctx)
1391 decl_ctx->addDecl(decl);
1392
1393 return CompilerType(ast, ast->getTagDeclType(decl));
1394 }
1395 return CompilerType();
1396}
1397
1398namespace {
1399 bool IsValueParam(const clang::TemplateArgument &argument) {
1400 return argument.getKind() == TemplateArgument::Integral;
1401 }
1402}
1403
1404static TemplateParameterList *CreateTemplateParameterList(
1405 ASTContext *ast,
1406 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1407 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1408 const bool parameter_pack = false;
1409 const bool is_typename = false;
1410 const unsigned depth = 0;
1411 const size_t num_template_params = template_param_infos.args.size();
1412 DeclContext *const decl_context =
1413 ast->getTranslationUnitDecl(); // Is this the right decl context?,
1414 for (size_t i = 0; i < num_template_params; ++i) {
1415 const char *name = template_param_infos.names[i];
1416
1417 IdentifierInfo *identifier_info = nullptr;
1418 if (name && name[0])
1419 identifier_info = &ast->Idents.get(name);
1420 if (IsValueParam(template_param_infos.args[i])) {
1421 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1422 *ast, decl_context,
1423 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1424 template_param_infos.args[i].getIntegralType(), parameter_pack,
1425 nullptr));
1426
1427 } else {
1428 template_param_decls.push_back(TemplateTypeParmDecl::Create(
1429 *ast, decl_context,
1430 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1431 is_typename, parameter_pack));
1432 }
1433 }
1434
1435 if (template_param_infos.packed_args &&
1436 template_param_infos.packed_args->args.size()) {
1437 IdentifierInfo *identifier_info = nullptr;
1438 if (template_param_infos.pack_name && template_param_infos.pack_name[0])
1439 identifier_info = &ast->Idents.get(template_param_infos.pack_name);
1440 const bool parameter_pack_true = true;
1441 if (IsValueParam(template_param_infos.packed_args->args[0])) {
1442 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1443 *ast, decl_context,
1444 SourceLocation(), SourceLocation(), depth, num_template_params,
1445 identifier_info,
1446 template_param_infos.packed_args->args[0].getIntegralType(),
1447 parameter_pack_true, nullptr));
1448 } else {
1449 template_param_decls.push_back(TemplateTypeParmDecl::Create(
1450 *ast, decl_context,
1451 SourceLocation(), SourceLocation(), depth, num_template_params,
1452 identifier_info,
1453 is_typename, parameter_pack_true));
1454 }
1455 }
1456 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1457 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1458 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1459 SourceLocation(), requires_clause);
1460 return template_param_list;
1461}
1462
1463clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1464 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1465 const char *name, const TemplateParameterInfos &template_param_infos) {
1466 // /// \brief Create a function template node.
1467 ASTContext *ast = getASTContext();
1468
1469 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1470
1471 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1472 ast, template_param_infos, template_param_decls);
1473 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1474 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1475 template_param_list, func_decl);
1476
1477 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1478 i < template_param_decl_count; ++i) {
1479 // TODO: verify which decl context we should put template_param_decls into..
1480 template_param_decls[i]->setDeclContext(func_decl);
1481 }
1482
1483 return func_tmpl_decl;
1484}
1485
1486void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1487 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1488 const TemplateParameterInfos &infos) {
1489 TemplateArgumentList template_args(TemplateArgumentList::OnStack, infos.args);
1490
1491 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, &template_args,
1492 nullptr);
1493}
1494
1495ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1496 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1497 int kind, const TemplateParameterInfos &template_param_infos) {
1498 ASTContext *ast = getASTContext();
1499
1500 ClassTemplateDecl *class_template_decl = nullptr;
1501 if (decl_ctx == nullptr)
1502 decl_ctx = ast->getTranslationUnitDecl();
1503
1504 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1505 DeclarationName decl_name(&identifier_info);
1506
1507 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1508
1509 for (NamedDecl *decl : result) {
1510 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
1511 if (class_template_decl)
1512 return class_template_decl;
1513 }
1514
1515 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1516
1517 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1518 ast, template_param_infos, template_param_decls);
1519
1520 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1521 *ast, (TagDecl::TagKind)kind,
1522 decl_ctx, // What decl context do we use here? TU? The actual decl
1523 // context?
1524 SourceLocation(), SourceLocation(), &identifier_info);
1525
1526 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1527 i < template_param_decl_count; ++i) {
1528 template_param_decls[i]->setDeclContext(template_cxx_decl);
1529 }
1530
1531 // With templated classes, we say that a class is templated with
1532 // specializations, but that the bare class has no functions.
1533 // template_cxx_decl->startDefinition();
1534 // template_cxx_decl->completeDefinition();
1535
1536 class_template_decl = ClassTemplateDecl::Create(
1537 *ast,
1538 decl_ctx, // What decl context do we use here? TU? The actual decl
1539 // context?
1540 SourceLocation(), decl_name, template_param_list, template_cxx_decl);
1541
1542 if (class_template_decl) {
1543 if (access_type != eAccessNone)
1544 class_template_decl->setAccess(
1545 ConvertAccessTypeToAccessSpecifier(access_type));
1546
1547 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1548 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1549
1550 decl_ctx->addDecl(class_template_decl);
1551
1552#ifdef LLDB_CONFIGURATION_DEBUG
1553 VerifyDecl(class_template_decl);
1554#endif
1555 }
1556
1557 return class_template_decl;
1558}
1559
1560ClassTemplateSpecializationDecl *
1561ClangASTContext::CreateClassTemplateSpecializationDecl(
1562 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1563 const TemplateParameterInfos &template_param_infos) {
1564 ASTContext *ast = getASTContext();
1565 llvm::SmallVector<clang::TemplateArgument, 2> args(
1566 template_param_infos.args.size() +
1567 (template_param_infos.packed_args ? 1 : 0));
1568 std::copy(template_param_infos.args.begin(), template_param_infos.args.end(),
1569 args.begin());
1570 if (template_param_infos.packed_args) {
1571 args[args.size() - 1] = TemplateArgument::CreatePackCopy(
1572 *ast, template_param_infos.packed_args->args);
1573 }
1574 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1575 ClassTemplateSpecializationDecl::Create(
1576 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1577 SourceLocation(), class_template_decl, args,
1578 nullptr);
1579
1580 class_template_specialization_decl->setSpecializationKind(
1581 TSK_ExplicitSpecialization);
1582
1583 return class_template_specialization_decl;
1584}
1585
1586CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1587 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1588 if (class_template_specialization_decl) {
1589 ASTContext *ast = getASTContext();
1590 if (ast)
1591 return CompilerType(
1592 ast, ast->getTagDeclType(class_template_specialization_decl));
1593 }
1594 return CompilerType();
1595}
1596
1597static inline bool check_op_param(bool is_method,
1598 clang::OverloadedOperatorKind op_kind,
1599 bool unary, bool binary,
1600 uint32_t num_params) {
1601 // Special-case call since it can take any number of operands
1602 if (op_kind == OO_Call)
1603 return true;
1604
1605 // The parameter count doesn't include "this"
1606 if (is_method)
1607 ++num_params;
1608 if (num_params == 1)
1609 return unary;
1610 if (num_params == 2)
1611 return binary;
1612 else
1613 return false;
1614}
1615
1616bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1617 bool is_method, clang::OverloadedOperatorKind op_kind,
1618 uint32_t num_params) {
1619 switch (op_kind) {
1620 default:
1621 break;
1622 // C++ standard allows any number of arguments to new/delete
1623 case OO_New:
1624 case OO_Array_New:
1625 case OO_Delete:
1626 case OO_Array_Delete:
1627 return true;
1628 }
1629
1630#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1631 case OO_##Name: \
1632 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1633 switch (op_kind) {
1634#include "clang/Basic/OperatorKinds.def"
1635 default:
1636 break;
1637 }
1638 return false;
1639}
1640
1641clang::AccessSpecifier
1642ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1643 clang::AccessSpecifier rhs) {
1644 // Make the access equal to the stricter of the field and the nested field's
1645 // access
1646 if (lhs == AS_none || rhs == AS_none)
1647 return AS_none;
1648 if (lhs == AS_private || rhs == AS_private)
1649 return AS_private;
1650 if (lhs == AS_protected || rhs == AS_protected)
1651 return AS_protected;
1652 return AS_public;
1653}
1654
1655bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1656 uint32_t &bitfield_bit_size) {
1657 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
1658}
1659
1660bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field,
1661 uint32_t &bitfield_bit_size) {
1662 if (ast == nullptr || field == nullptr)
1663 return false;
1664
1665 if (field->isBitField()) {
1666 Expr *bit_width_expr = field->getBitWidth();
1667 if (bit_width_expr) {
1668 llvm::APSInt bit_width_apsint;
1669 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1670 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX(4294967295U));
1671 return true;
1672 }
1673 }
1674 }
1675 return false;
1676}
1677
1678bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1679 if (record_decl == nullptr)
1680 return false;
1681
1682 if (!record_decl->field_empty())
1683 return true;
1684
1685 // No fields, lets check this is a CXX record and check the base classes
1686 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1687 if (cxx_record_decl) {
1688 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1689 for (base_class = cxx_record_decl->bases_begin(),
1690 base_class_end = cxx_record_decl->bases_end();
1691 base_class != base_class_end; ++base_class) {
1692 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1693 base_class->getType()->getAs<RecordType>()->getDecl());
1694 if (RecordHasFields(base_class_decl))
1695 return true;
1696 }
1697 }
1698 return false;
1699}
1700
1701#pragma mark Objective C Classes
1702
1703CompilerType ClangASTContext::CreateObjCClass(const char *name,
1704 DeclContext *decl_ctx,
1705 bool isForwardDecl,
1706 bool isInternal,
1707 ClangASTMetadata *metadata) {
1708 ASTContext *ast = getASTContext();
1709 assert(ast != nullptr)(static_cast <bool> (ast != nullptr) ? void (0) : __assert_fail
("ast != nullptr", "/build/llvm-toolchain-snapshot-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1709, __extension__ __PRETTY_FUNCTION__))
;
1710 assert(name && name[0])(static_cast <bool> (name && name[0]) ? void (0
) : __assert_fail ("name && name[0]", "/build/llvm-toolchain-snapshot-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1710, __extension__ __PRETTY_FUNCTION__))
;
1711 if (decl_ctx == nullptr)
1712 decl_ctx = ast->getTranslationUnitDecl();
1713
1714 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1715 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1716 nullptr, SourceLocation(),
1717 /*isForwardDecl,*/
1718 isInternal);
1719
1720 if (decl && metadata)
1721 SetMetadata(ast, decl, *metadata);
1722
1723 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1724}
1725
1726static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
1727 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) ==
1728 false;
1729}
1730
1731uint32_t
1732ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1733 bool omit_empty_base_classes) {
1734 uint32_t num_bases = 0;
1735 if (cxx_record_decl) {
1736 if (omit_empty_base_classes) {
1737 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1738 for (base_class = cxx_record_decl->bases_begin(),
1739 base_class_end = cxx_record_decl->bases_end();
1740 base_class != base_class_end; ++base_class) {
1741 // Skip empty base classes
1742 if (omit_empty_base_classes) {
1743 if (BaseSpecifierIsEmpty(base_class))
1744 continue;
1745 }
1746 ++num_bases;
1747 }
1748 } else
1749 num_bases = cxx_record_decl->getNumBases();
1750 }
1751 return num_bases;
1752}
1753
1754#pragma mark Namespace Declarations
1755
1756NamespaceDecl *
1757ClangASTContext::GetUniqueNamespaceDeclaration(const char *name,
1758 DeclContext *decl_ctx) {
1759 NamespaceDecl *namespace_decl = nullptr;
1760 ASTContext *ast = getASTContext();
1761 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1762 if (decl_ctx == nullptr)
1763 decl_ctx = translation_unit_decl;
1764
1765 if (name) {
1766 IdentifierInfo &identifier_info = ast->Idents.get(name);
1767 DeclarationName decl_name(&identifier_info);
1768 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1769 for (NamedDecl *decl : result) {
1770 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1771 if (namespace_decl)
1772 return namespace_decl;
1773 }
1774
1775 namespace_decl =
1776 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1777 SourceLocation(), &identifier_info, nullptr);
1778
1779 decl_ctx->addDecl(namespace_decl);
1780 } else {
1781 if (decl_ctx == translation_unit_decl) {
1782 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1783 if (namespace_decl)
1784 return namespace_decl;
1785
1786 namespace_decl =
1787 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1788 SourceLocation(), nullptr, nullptr);
1789 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1790 translation_unit_decl->addDecl(namespace_decl);
1791 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-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1791, __extension__ __PRETTY_FUNCTION__))
;
1792 } else {
1793 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1794 if (parent_namespace_decl) {
1795 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1796 if (namespace_decl)
1797 return namespace_decl;
1798 namespace_decl =
1799 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1800 SourceLocation(), nullptr, nullptr);
1801 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1802 parent_namespace_decl->addDecl(namespace_decl);
1803 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-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1804, __extension__ __PRETTY_FUNCTION__))
1804 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-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1804, __extension__ __PRETTY_FUNCTION__))
;
1805 } else {
1806 // BAD!!!
1807 }
1808 }
1809 }
1810#ifdef LLDB_CONFIGURATION_DEBUG
1811 VerifyDecl(namespace_decl);
1812#endif
1813 return namespace_decl;
1814}
1815
1816NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1817 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx) {
1818 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1819 if (ast_ctx == nullptr)
1820 return nullptr;
1821
1822 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx);
1823}
1824
1825clang::BlockDecl *
1826ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1827 if (ctx != nullptr) {
1828 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1829 clang::SourceLocation());
1830 ctx->addDecl(decl);
1831 return decl;
1832 }
1833 return nullptr;
1834}
1835
1836clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1837 clang::DeclContext *right,
1838 clang::DeclContext *root) {
1839 if (root == nullptr)
1840 return nullptr;
1841
1842 std::set<clang::DeclContext *> path_left;
1843 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1844 path_left.insert(d);
1845
1846 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1847 if (path_left.find(d) != path_left.end())
1848 return d;
1849
1850 return nullptr;
1851}
1852
1853clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
1854 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
1855 if (decl_ctx != nullptr && ns_decl != nullptr) {
1856 clang::TranslationUnitDecl *translation_unit =
1857 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
1858 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
1859 *getASTContext(), decl_ctx, clang::SourceLocation(),
1860 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
1861 clang::SourceLocation(), ns_decl,
1862 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
1863 decl_ctx->addDecl(using_decl);
1864 return using_decl;
1865 }
1866 return nullptr;
1867}
1868
1869clang::UsingDecl *
1870ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
1871 clang::NamedDecl *target) {
1872 if (current_decl_ctx != nullptr && target != nullptr) {
1873 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
1874 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
1875 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
1876 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
1877 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
1878 target);
1879 using_decl->addShadowDecl(shadow_decl);
1880 current_decl_ctx->addDecl(using_decl);
1881 return using_decl;
1882 }
1883 return nullptr;
1884}
1885
1886clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
1887 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
1888 if (decl_context != nullptr) {
1889 clang::VarDecl *var_decl = clang::VarDecl::Create(
1890 *getASTContext(), decl_context, clang::SourceLocation(),
1891 clang::SourceLocation(),
1892 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
1893 nullptr, clang::SC_None);
1894 var_decl->setAccess(clang::AS_public);
1895 decl_context->addDecl(var_decl);
1896 return var_decl;
1897 }
1898 return nullptr;
1899}
1900
1901lldb::opaque_compiler_type_t
1902ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
1903 lldb::BasicType basic_type) {
1904 switch (basic_type) {
1905 case eBasicTypeVoid:
1906 return ast->VoidTy.getAsOpaquePtr();
1907 case eBasicTypeChar:
1908 return ast->CharTy.getAsOpaquePtr();
1909 case eBasicTypeSignedChar:
1910 return ast->SignedCharTy.getAsOpaquePtr();
1911 case eBasicTypeUnsignedChar:
1912 return ast->UnsignedCharTy.getAsOpaquePtr();
1913 case eBasicTypeWChar:
1914 return ast->getWCharType().getAsOpaquePtr();
1915 case eBasicTypeSignedWChar:
1916 return ast->getSignedWCharType().getAsOpaquePtr();
1917 case eBasicTypeUnsignedWChar:
1918 return ast->getUnsignedWCharType().getAsOpaquePtr();
1919 case eBasicTypeChar16:
1920 return ast->Char16Ty.getAsOpaquePtr();
1921 case eBasicTypeChar32:
1922 return ast->Char32Ty.getAsOpaquePtr();
1923 case eBasicTypeShort:
1924 return ast->ShortTy.getAsOpaquePtr();
1925 case eBasicTypeUnsignedShort:
1926 return ast->UnsignedShortTy.getAsOpaquePtr();
1927 case eBasicTypeInt:
1928 return ast->IntTy.getAsOpaquePtr();
1929 case eBasicTypeUnsignedInt:
1930 return ast->UnsignedIntTy.getAsOpaquePtr();
1931 case eBasicTypeLong:
1932 return ast->LongTy.getAsOpaquePtr();
1933 case eBasicTypeUnsignedLong:
1934 return ast->UnsignedLongTy.getAsOpaquePtr();
1935 case eBasicTypeLongLong:
1936 return ast->LongLongTy.getAsOpaquePtr();
1937 case eBasicTypeUnsignedLongLong:
1938 return ast->UnsignedLongLongTy.getAsOpaquePtr();
1939 case eBasicTypeInt128:
1940 return ast->Int128Ty.getAsOpaquePtr();
1941 case eBasicTypeUnsignedInt128:
1942 return ast->UnsignedInt128Ty.getAsOpaquePtr();
1943 case eBasicTypeBool:
1944 return ast->BoolTy.getAsOpaquePtr();
1945 case eBasicTypeHalf:
1946 return ast->HalfTy.getAsOpaquePtr();
1947 case eBasicTypeFloat:
1948 return ast->FloatTy.getAsOpaquePtr();
1949 case eBasicTypeDouble:
1950 return ast->DoubleTy.getAsOpaquePtr();
1951 case eBasicTypeLongDouble:
1952 return ast->LongDoubleTy.getAsOpaquePtr();
1953 case eBasicTypeFloatComplex:
1954 return ast->FloatComplexTy.getAsOpaquePtr();
1955 case eBasicTypeDoubleComplex:
1956 return ast->DoubleComplexTy.getAsOpaquePtr();
1957 case eBasicTypeLongDoubleComplex:
1958 return ast->LongDoubleComplexTy.getAsOpaquePtr();
1959 case eBasicTypeObjCID:
1960 return ast->getObjCIdType().getAsOpaquePtr();
1961 case eBasicTypeObjCClass:
1962 return ast->getObjCClassType().getAsOpaquePtr();
1963 case eBasicTypeObjCSel:
1964 return ast->getObjCSelType().getAsOpaquePtr();
1965 case eBasicTypeNullPtr:
1966 return ast->NullPtrTy.getAsOpaquePtr();
1967 default:
1968 return nullptr;
1969 }
1970}
1971
1972#pragma mark Function Types
1973
1974clang::DeclarationName
1975ClangASTContext::GetDeclarationName(const char *name,
1976 const CompilerType &function_clang_type) {
1977 if (!name || !name[0])
1978 return clang::DeclarationName();
1979
1980 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
1981 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
1982 return DeclarationName(&getASTContext()->Idents.get(
1983 name)); // Not operator, but a regular function.
1984
1985 // Check the number of operator parameters. Sometimes we have
1986 // seen bad DWARF that doesn't correctly describe operators and
1987 // if we try to create a method and add it to the class, clang
1988 // will assert and crash, so we need to make sure things are
1989 // acceptable.
1990 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
1991 const clang::FunctionProtoType *function_type =
1992 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
1993 if (function_type == nullptr)
1994 return clang::DeclarationName();
1995
1996 const bool is_method = false;
1997 const unsigned int num_params = function_type->getNumParams();
1998 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1999 is_method, op_kind, num_params))
2000 return clang::DeclarationName();
2001
2002 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
2003}
2004
2005FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
2006 DeclContext *decl_ctx, const char *name,
2007 const CompilerType &function_clang_type, int storage, bool is_inline) {
2008 FunctionDecl *func_decl = nullptr;
2009 ASTContext *ast = getASTContext();
2010 if (decl_ctx == nullptr)
2011 decl_ctx = ast->getTranslationUnitDecl();
2012
2013 const bool hasWrittenPrototype = true;
2014 const bool isConstexprSpecified = false;
2015
2016 clang::DeclarationName declarationName =
2017 GetDeclarationName(name, function_clang_type);
2018 func_decl = FunctionDecl::Create(
2019 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
2020 ClangUtil::GetQualType(function_clang_type), nullptr,
2021 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
2022 isConstexprSpecified);
2023 if (func_decl)
2024 decl_ctx->addDecl(func_decl);
2025
2026#ifdef LLDB_CONFIGURATION_DEBUG
2027 VerifyDecl(func_decl);
2028#endif
2029
2030 return func_decl;
2031}
2032
2033CompilerType ClangASTContext::CreateFunctionType(
2034 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
2035 unsigned num_args, bool is_variadic, unsigned type_quals) {
2036 if (ast == nullptr)
2037 return CompilerType(); // invalid AST
2038
2039 if (!result_type || !ClangUtil::IsClangType(result_type))
2040 return CompilerType(); // invalid return type
2041
2042 std::vector<QualType> qual_type_args;
2043 if (num_args > 0 && args == nullptr)
2044 return CompilerType(); // invalid argument array passed in
2045
2046 // Verify that all arguments are valid and the right type
2047 for (unsigned i = 0; i < num_args; ++i) {
2048 if (args[i]) {
2049 // Make sure we have a clang type in args[i] and not a type from another
2050 // language whose name might match
2051 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2052 lldbassert(is_clang_type)lldb_private::lldb_assert(is_clang_type, "is_clang_type", __FUNCTION__
, "/build/llvm-toolchain-snapshot-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 2052)
;
2053 if (is_clang_type)
2054 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2055 else
2056 return CompilerType(); // invalid argument type (must be a clang type)
2057 } else
2058 return CompilerType(); // invalid argument type (empty)
2059 }
2060
2061 // TODO: Detect calling convention in DWARF?
2062 FunctionProtoType::ExtProtoInfo proto_info;
2063 proto_info.Variadic = is_variadic;
2064 proto_info.ExceptionSpec = EST_None;
2065 proto_info.TypeQuals = type_quals;
2066 proto_info.RefQualifier = RQ_None;
2067
2068 return CompilerType(ast,
2069 ast->getFunctionType(ClangUtil::GetQualType(result_type),
2070 qual_type_args, proto_info));
2071}
2072
2073ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
2074 const char *name, const CompilerType &param_type, int storage) {
2075 ASTContext *ast = getASTContext();
2076 assert(ast != nullptr)(static_cast <bool> (ast != nullptr) ? void (0) : __assert_fail
("ast != nullptr", "/build/llvm-toolchain-snapshot-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 2076, __extension__ __PRETTY_FUNCTION__))
;
2077 return ParmVarDecl::Create(*ast, ast->getTranslationUnitDecl(),
2078 SourceLocation(), SourceLocation(),
2079 name && name[0] ? &ast->Idents.get(name) : nullptr,
2080 ClangUtil::GetQualType(param_type), nullptr,
2081 (clang::StorageClass)storage, nullptr);
2082}
2083
2084void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2085 ParmVarDecl **params,
2086 unsigned num_params) {
2087 if (function_decl)
2088 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
2089}
2090
2091CompilerType
2092ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
2093 QualType block_type = m_ast_ap->getBlockPointerType(
2094 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
2095
2096 return CompilerType(this, block_type.getAsOpaquePtr());
2097}
2098
2099#pragma mark Array Types
2100
2101CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2102 size_t element_count,
2103 bool is_vector) {
2104 if (element_type.IsValid()) {
2105 ASTContext *ast = getASTContext();
2106 assert(ast != nullptr)(static_cast <bool> (ast != nullptr) ? void (0) : __assert_fail
("ast != nullptr", "/build/llvm-toolchain-snapshot-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 2106, __extension__ __PRETTY_FUNCTION__))
;
2107
2108 if (is_vector) {
2109 return CompilerType(
2110 ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2111 element_count));
2112 } else {
2113
2114 llvm::APInt ap_element_count(64, element_count);
2115 if (element_count == 0) {
2116 return CompilerType(ast, ast->getIncompleteArrayType(
2117 ClangUtil::GetQualType(element_type),
2118 clang::ArrayType::Normal, 0));
2119 } else {
2120 return CompilerType(
2121 ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2122 ap_element_count,
2123 clang::ArrayType::Normal, 0));
2124 }
2125 }
2126 }
2127 return CompilerType();
2128}
2129
2130CompilerType ClangASTContext::CreateStructForIdentifier(
2131 const ConstString &type_name,
2132 const std::initializer_list<std::pair<const char *, CompilerType>>
2133 &type_fields,
2134 bool packed) {
2135 CompilerType type;
2136 if (!type_name.IsEmpty() &&
2137 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2138 .IsValid()) {
2139 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-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 2139)
;
2140 return type;
2141 }
2142
2143 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2144 clang::TTK_Struct, lldb::eLanguageTypeC);
2145 StartTagDeclarationDefinition(type);
2146 for (const auto &field : type_fields)
2147 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2148 0);
2149 if (packed)
2150 SetIsPacked(type);
2151 CompleteTagDeclarationDefinition(type);
2152 return type;
2153}
2154
2155CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
2156 const ConstString &type_name,
2157 const std::initializer_list<std::pair<const char *, CompilerType>>
2158 &type_fields,
2159 bool packed) {
2160 CompilerType type;
2161 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2162 return type;
2163
2164 return CreateStructForIdentifier(type_name, type_fields, packed);
2165}
2166
2167#pragma mark Enumeration Types
2168
2169CompilerType
2170ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2171 const Declaration &decl,
2172 const CompilerType &integer_clang_type,
2173 bool is_scoped) {
2174 // TODO: Do something intelligent with the Declaration object passed in
2175 // like maybe filling in the SourceLocation with it...
2176 ASTContext *ast = getASTContext();
2177
2178 // TODO: ask about these...
2179 // const bool IsFixed = false;
2180
2181 EnumDecl *enum_decl = EnumDecl::Create(
2182 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2183 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
2184 is_scoped, // IsScoped
2185 is_scoped, // IsScopedUsingClassTag
2186 false); // IsFixed
2187
2188 if (enum_decl) {
2189 // TODO: check if we should be setting the promotion type too?
2190 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2191
2192 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2193
2194 return CompilerType(ast, ast->getTagDeclType(enum_decl));
2195 }
2196 return CompilerType();
2197}
2198
2199// Disable this for now since I can't seem to get a nicely formatted float
2200// out of the APFloat class without just getting the float, double or quad
2201// and then using a formatted print on it which defeats the purpose. We ideally
2202// would like to get perfect string values for any kind of float semantics
2203// so we can support remote targets. The code below also requires a patch to
2204// llvm::APInt.
2205// bool
2206// ClangASTContext::ConvertFloatValueToString (ASTContext *ast,
2207// lldb::opaque_compiler_type_t clang_type, const uint8_t* bytes, size_t
2208// byte_size, int apint_byte_order, std::string &float_str)
2209//{
2210// uint32_t count = 0;
2211// bool is_complex = false;
2212// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
2213// {
2214// unsigned num_bytes_per_float = byte_size / count;
2215// unsigned num_bits_per_float = num_bytes_per_float * 8;
2216//
2217// float_str.clear();
2218// uint32_t i;
2219// for (i=0; i<count; i++)
2220// {
2221// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float,
2222// (APInt::ByteOrder)apint_byte_order);
2223// bool is_ieee = false;
2224// APFloat ap_float(ap_int, is_ieee);
2225// char s[1024];
2226// unsigned int hex_digits = 0;
2227// bool upper_case = false;
2228//
2229// if (ap_float.convertToHexString(s, hex_digits, upper_case,
2230// APFloat::rmNearestTiesToEven) > 0)
2231// {
2232// if (i > 0)
2233// float_str.append(", ");
2234// float_str.append(s);
2235// if (i == 1 && is_complex)
2236// float_str.append(1, 'i');
2237// }
2238// }
2239// return !float_str.empty();
2240// }
2241// return false;
2242//}
2243
2244CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2245 size_t bit_size,
2246 bool is_signed) {
2247 if (ast) {
2248 if (is_signed) {
2249 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
2250 return CompilerType(ast, ast->SignedCharTy);
2251
2252 if (bit_size == ast->getTypeSize(ast->ShortTy))
2253 return CompilerType(ast, ast->ShortTy);
2254
2255 if (bit_size == ast->getTypeSize(ast->IntTy))
2256 return CompilerType(ast, ast->IntTy);
2257
2258 if (bit_size == ast->getTypeSize(ast->LongTy))
2259 return CompilerType(ast, ast->LongTy);
2260
2261 if (bit_size == ast->getTypeSize(ast->LongLongTy))
2262 return CompilerType(ast, ast->LongLongTy);
2263
2264 if (bit_size == ast->getTypeSize(ast->Int128Ty))
2265 return CompilerType(ast, ast->Int128Ty);
2266 } else {
2267 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
2268 return CompilerType(ast, ast->UnsignedCharTy);
2269
2270 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
2271 return CompilerType(ast, ast->UnsignedShortTy);
2272
2273 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
2274 return CompilerType(ast, ast->UnsignedIntTy);
2275
2276 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
2277 return CompilerType(ast, ast->UnsignedLongTy);
2278
2279 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
2280 return CompilerType(ast, ast->UnsignedLongLongTy);
2281
2282 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
2283 return CompilerType(ast, ast->UnsignedInt128Ty);
2284 }
2285 }
2286 return CompilerType();
2287}
2288
2289CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2290 bool is_signed) {
2291 if (ast)
2292 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2293 is_signed);
2294 return CompilerType();
2295}
2296
2297void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2298 if (decl_ctx) {
2299 DumpDeclContextHiearchy(decl_ctx->getParent());
2300
2301 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2302 if (named_decl) {
2303 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2304 named_decl->getDeclName().getAsString().c_str());
2305 } else {
2306 printf("%20s\n", decl_ctx->getDeclKindName());
2307 }
2308 }
2309}
2310
2311void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2312 if (decl == nullptr)
2313 return;
2314 DumpDeclContextHiearchy(decl->getDeclContext());
2315
2316 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2317 if (record_decl) {
2318 printf("%20s: %s%s\n", decl->getDeclKindName(),
2319 record_decl->getDeclName().getAsString().c_str(),
2320 record_decl->isInjectedClassName() ? " (injected class name)" : "");
2321
2322 } else {
2323 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2324 if (named_decl) {
2325 printf("%20s: %s\n", decl->getDeclKindName(),
2326 named_decl->getDeclName().getAsString().c_str());
2327 } else {
2328 printf("%20s\n", decl->getDeclKindName());
2329 }
2330 }
2331}
2332
2333bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2334 clang::Decl *rhs_decl) {
2335 if (lhs_decl && rhs_decl) {
2336 //----------------------------------------------------------------------
2337 // Make sure the decl kinds match first
2338 //----------------------------------------------------------------------
2339 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2340 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
2341
2342 if (lhs_decl_kind == rhs_decl_kind) {
2343 //------------------------------------------------------------------
2344 // Now check that the decl contexts kinds are all equivalent
2345 // before we have to check any names of the decl contexts...
2346 //------------------------------------------------------------------
2347 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2348 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2349 if (lhs_decl_ctx && rhs_decl_ctx) {
2350 while (1) {
2351 if (lhs_decl_ctx && rhs_decl_ctx) {
2352 const clang::Decl::Kind lhs_decl_ctx_kind =
2353 lhs_decl_ctx->getDeclKind();
2354 const clang::Decl::Kind rhs_decl_ctx_kind =
2355 rhs_decl_ctx->getDeclKind();
2356 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2357 lhs_decl_ctx = lhs_decl_ctx->getParent();
2358 rhs_decl_ctx = rhs_decl_ctx->getParent();
2359
2360 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2361 break;
2362 } else
2363 return false;
2364 } else
2365 return false;
2366 }
2367
2368 //--------------------------------------------------------------
2369 // Now make sure the name of the decls match
2370 //--------------------------------------------------------------
2371 clang::NamedDecl *lhs_named_decl =
2372 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2373 clang::NamedDecl *rhs_named_decl =
2374 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2375 if (lhs_named_decl && rhs_named_decl) {
2376 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2377 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2378 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2379 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2380 return false;
2381 } else
2382 return false;
2383 } else
2384 return false;
2385
2386 //--------------------------------------------------------------
2387 // We know that the decl context kinds all match, so now we need
2388 // to make sure the names match as well
2389 //--------------------------------------------------------------
2390 lhs_decl_ctx = lhs_decl->getDeclContext();
2391 rhs_decl_ctx = rhs_decl->getDeclContext();
2392 while (1) {
2393 switch (lhs_decl_ctx->getDeclKind()) {
2394 case clang::Decl::TranslationUnit:
2395 // We don't care about the translation unit names
2396 return true;
2397 default: {
2398 clang::NamedDecl *lhs_named_decl =
2399 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2400 clang::NamedDecl *rhs_named_decl =
2401 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2402 if (lhs_named_decl && rhs_named_decl) {
2403 clang::DeclarationName lhs_decl_name =
2404 lhs_named_decl->getDeclName();
2405 clang::DeclarationName rhs_decl_name =
2406 rhs_named_decl->getDeclName();
2407 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2408 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2409 return false;
2410 } else
2411 return false;
2412 } else
2413 return false;
2414 } break;
2415 }
2416 lhs_decl_ctx = lhs_decl_ctx->getParent();
2417 rhs_decl_ctx = rhs_decl_ctx->getParent();
2418 }
2419 }
2420 }
2421 }
2422 return false;
2423}
2424bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2425 clang::Decl *decl) {
2426 if (!decl)
2427 return false;
2428
2429 ExternalASTSource *ast_source = ast->getExternalSource();
2430
2431 if (!ast_source)
2432 return false;
2433
2434 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2435 if (tag_decl->isCompleteDefinition())
2436 return true;
2437
2438 if (!tag_decl->hasExternalLexicalStorage())
2439 return false;
2440
2441 ast_source->CompleteType(tag_decl);
2442
2443 return !tag_decl->getTypeForDecl()->isIncompleteType();
2444 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2445 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2446 if (objc_interface_decl->getDefinition())
2447 return true;
2448
2449 if (!objc_interface_decl->hasExternalLexicalStorage())
2450 return false;
2451
2452 ast_source->CompleteType(objc_interface_decl);
2453
2454 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2455 } else {
2456 return false;
2457 }
2458}
2459
2460void ClangASTContext::SetMetadataAsUserID(const void *object,
2461 user_id_t user_id) {
2462 ClangASTMetadata meta_data;
2463 meta_data.SetUserID(user_id);
2464 SetMetadata(object, meta_data);
2465}
2466
2467void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2468 ClangASTMetadata &metadata) {
2469 ClangExternalASTSourceCommon *external_source =
2470 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2471
2472 if (external_source)
2473 external_source->SetMetadata(object, metadata);
2474}
2475
2476ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2477 const void *object) {
2478 ClangExternalASTSourceCommon *external_source =
2479 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2480
2481 if (external_source && external_source->HasMetadata(object))
2482 return external_source->GetMetadata(object);
2483 else
2484 return nullptr;
2485}
2486
2487clang::DeclContext *
2488ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2489 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2490}
2491
2492clang::DeclContext *
2493ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2494 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2495}
2496
2497bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2498 int kind) const {
2499 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2500 if (clang_type) {
2501 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2502 if (tag_type) {
2503 clang::TagDecl *tag_decl =
2504 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2505 if (tag_decl) {
2506 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2507 return true;
2508 }
2509 }
2510 }
2511 return false;
2512}
2513
2514bool ClangASTContext::SetDefaultAccessForRecordFields(
2515 clang::RecordDecl *record_decl, int default_accessibility,
2516 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2517 if (record_decl) {
2518 uint32_t field_idx;
2519 clang::RecordDecl::field_iterator field, field_end;
2520 for (field = record_decl->field_begin(),
2521 field_end = record_decl->field_end(), field_idx = 0;
2522 field != field_end; ++field, ++field_idx) {
2523 // If no accessibility was assigned, assign the correct one
2524 if (field_idx < num_assigned_accessibilities &&
2525 assigned_accessibilities[field_idx] == clang::AS_none)
2526 field->setAccess((clang::AccessSpecifier)default_accessibility);
2527 }
2528 return true;
2529 }
2530 return false;
2531}
2532
2533clang::DeclContext *
2534ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2535 return GetDeclContextForType(ClangUtil::GetQualType(type));
2536}
2537
2538clang::DeclContext *
2539ClangASTContext::GetDeclContextForType(clang::QualType type) {
2540 if (type.isNull())
2541 return nullptr;
2542
2543 clang::QualType qual_type = type.getCanonicalType();
2544 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2545 switch (type_class) {
2546 case clang::Type::ObjCInterface:
2547 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2548 ->getInterface();
2549 case clang::Type::ObjCObjectPointer:
2550 return GetDeclContextForType(
2551 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2552 ->getPointeeType());
2553 case clang::Type::Record:
2554 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2555 case clang::Type::Enum:
2556 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2557 case clang::Type::Typedef:
2558 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2559 ->getDecl()
2560 ->getUnderlyingType());
2561 case clang::Type::Auto:
2562 return GetDeclContextForType(
2563 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2564 case clang::Type::Elaborated:
2565 return GetDeclContextForType(
2566 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2567 case clang::Type::Paren:
2568 return GetDeclContextForType(
2569 llvm::cast<clang::ParenType>(qual_type)->desugar());
2570 default:
2571 break;
2572 }
2573 // No DeclContext in this type...
2574 return nullptr;
2575}
2576
2577static bool GetCompleteQualType(clang::ASTContext *ast,
2578 clang::QualType qual_type,
2579 bool allow_completion = true) {
2580 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2581 switch (type_class) {
2582 case clang::Type::ConstantArray:
2583 case clang::Type::IncompleteArray:
2584 case clang::Type::VariableArray: {
2585 const clang::ArrayType *array_type =
2586 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2587
2588 if (array_type)
2589 return GetCompleteQualType(ast, array_type->getElementType(),
2590 allow_completion);
2591 } break;
2592 case clang::Type::Record: {
2593 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2594 if (cxx_record_decl) {
2595 if (cxx_record_decl->hasExternalLexicalStorage()) {
2596 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2597 const bool fields_loaded =
2598 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2599 if (is_complete && fields_loaded)
2600 return true;
2601
2602 if (!allow_completion)
2603 return false;
2604
2605 // Call the field_begin() accessor to for it to use the external source
2606 // to load the fields...
2607 clang::ExternalASTSource *external_ast_source =
2608 ast->getExternalSource();
2609 if (external_ast_source) {
2610 external_ast_source->CompleteType(cxx_record_decl);
2611 if (cxx_record_decl->isCompleteDefinition()) {
2612 cxx_record_decl->field_begin();
2613 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2614 }
2615 }
2616 }
2617 }
2618 const clang::TagType *tag_type =
2619 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2620 return !tag_type->isIncompleteType();
2621 } break;
2622
2623 case clang::Type::Enum: {
2624 const clang::TagType *tag_type =
2625 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2626 if (tag_type) {
2627 clang::TagDecl *tag_decl = tag_type->getDecl();
2628 if (tag_decl) {
2629 if (tag_decl->getDefinition())
2630 return true;
2631
2632 if (!allow_completion)
2633 return false;
2634
2635 if (tag_decl->hasExternalLexicalStorage()) {
2636 if (ast) {
2637 clang::ExternalASTSource *external_ast_source =
2638 ast->getExternalSource();
2639 if (external_ast_source) {
2640 external_ast_source->CompleteType(tag_decl);
2641 return !tag_type->isIncompleteType();
2642 }
2643 }
2644 }
2645 return false;
2646 }
2647 }
2648
2649 } break;
2650 case clang::Type::ObjCObject:
2651 case clang::Type::ObjCInterface: {
2652 const clang::ObjCObjectType *objc_class_type =
2653 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2654 if (objc_class_type) {
2655 clang::ObjCInterfaceDecl *class_interface_decl =
2656 objc_class_type->getInterface();
2657 // We currently can't complete objective C types through the newly added
2658 // ASTContext
2659 // because it only supports TagDecl objects right now...
2660 if (class_interface_decl) {
2661 if (class_interface_decl->getDefinition())
2662 return true;
2663
2664 if (!allow_completion)
2665 return false;
2666
2667 if (class_interface_decl->hasExternalLexicalStorage()) {
2668 if (ast) {
2669 clang::ExternalASTSource *external_ast_source =
2670 ast->getExternalSource();
2671 if (external_ast_source) {
2672 external_ast_source->CompleteType(class_interface_decl);
2673 return !objc_class_type->isIncompleteType();
2674 }
2675 }
2676 }
2677 return false;
2678 }
2679 }
2680 } break;
2681
2682 case clang::Type::Typedef:
2683 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2684 ->getDecl()
2685 ->getUnderlyingType(),
2686 allow_completion);
2687
2688 case clang::Type::Auto:
2689 return GetCompleteQualType(
2690 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2691 allow_completion);
2692
2693 case clang::Type::Elaborated:
2694 return GetCompleteQualType(
2695 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2696 allow_completion);
2697
2698 case clang::Type::Paren:
2699 return GetCompleteQualType(
2700 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2701 allow_completion);
2702
2703 case clang::Type::Attributed:
2704 return GetCompleteQualType(
2705 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2706 allow_completion);
2707
2708 default:
2709 break;
2710 }
2711
2712 return true;
2713}
2714
2715static clang::ObjCIvarDecl::AccessControl
2716ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2717 switch (access) {
2718 case eAccessNone:
2719 return clang::ObjCIvarDecl::None;
2720 case eAccessPublic:
2721 return clang::ObjCIvarDecl::Public;
2722 case eAccessPrivate:
2723 return clang::ObjCIvarDecl::Private;
2724 case eAccessProtected:
2725 return clang::ObjCIvarDecl::Protected;
2726 case eAccessPackage:
2727 return clang::ObjCIvarDecl::Package;
2728 }
2729 return clang::ObjCIvarDecl::None;
2730}
2731
2732//----------------------------------------------------------------------
2733// Tests
2734//----------------------------------------------------------------------
2735
2736bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2737 clang::QualType qual_type(GetCanonicalQualType(type));
2738
2739 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2740 switch (type_class) {
2741 case clang::Type::IncompleteArray:
2742 case clang::Type::VariableArray:
2743 case clang::Type::ConstantArray:
2744 case clang::Type::ExtVector:
2745 case clang::Type::Vector:
2746 case clang::Type::Record:
2747 case clang::Type::ObjCObject:
2748 case clang::Type::ObjCInterface:
2749 return true;
2750 case clang::Type::Auto:
2751 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2752 ->getDeducedType()
2753 .getAsOpaquePtr());
2754 case clang::Type::Elaborated:
2755 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2756 ->getNamedType()
2757 .getAsOpaquePtr());
2758 case clang::Type::Typedef:
2759 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2760 ->getDecl()
2761 ->getUnderlyingType()
2762 .getAsOpaquePtr());
2763 case clang::Type::Paren:
2764 return IsAggregateType(
2765 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2766 default:
2767 break;
2768 }
2769 // The clang type does have a value
2770 return false;
2771}
2772
2773bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2774 clang::QualType qual_type(GetCanonicalQualType(type));
2775
2776 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2777 switch (type_class) {
2778 case clang::Type::Record: {
2779 if (const clang::RecordType *record_type =
2780 llvm::dyn_cast_or_null<clang::RecordType>(
2781 qual_type.getTypePtrOrNull())) {
2782 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2783 return record_decl->isAnonymousStructOrUnion();
2784 }
2785 }
2786 break;
2787 }
2788 case clang::Type::Auto:
2789 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2790 ->getDeducedType()
2791 .getAsOpaquePtr());
2792 case clang::Type::Elaborated:
2793 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2794 ->getNamedType()
2795 .getAsOpaquePtr());
2796 case clang::Type::Typedef:
2797 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2798 ->getDecl()
2799 ->getUnderlyingType()
2800 .getAsOpaquePtr());
2801 case clang::Type::Paren:
2802 return IsAnonymousType(
2803 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2804 default:
2805 break;
2806 }
2807 // The clang type does have a value
2808 return false;
2809}
2810
2811bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2812 CompilerType *element_type_ptr,
2813 uint64_t *size, bool *is_incomplete) {
2814 clang::QualType qual_type(GetCanonicalQualType(type));
2815
2816 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2817 switch (type_class) {
2818 default:
2819 break;
2820
2821 case clang::Type::ConstantArray:
2822 if (element_type_ptr)
2823 element_type_ptr->SetCompilerType(
2824 getASTContext(),
2825 llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
2826 if (size)
2827 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2828 ->getSize()
2829 .getLimitedValue(ULLONG_MAX(9223372036854775807LL*2ULL+1ULL));
2830 if (is_incomplete)
2831 *is_incomplete = false;
2832 return true;
2833
2834 case clang::Type::IncompleteArray:
2835 if (element_type_ptr)
2836 element_type_ptr->SetCompilerType(
2837 getASTContext(),
2838 llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2839 if (size)
2840 *size = 0;
2841 if (is_incomplete)
2842 *is_incomplete = true;
2843 return true;
2844
2845 case clang::Type::VariableArray:
2846 if (element_type_ptr)
2847 element_type_ptr->SetCompilerType(
2848 getASTContext(),
2849 llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2850 if (size)
2851 *size = 0;
2852 if (is_incomplete)
2853 *is_incomplete = false;
2854 return true;
2855
2856 case clang::Type::DependentSizedArray:
2857 if (element_type_ptr)
2858 element_type_ptr->SetCompilerType(
2859 getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)
2860 ->getElementType());
2861 if (size)
2862 *size = 0;
2863 if (is_incomplete)
2864 *is_incomplete = false;
2865 return true;
2866
2867 case clang::Type::Typedef:
2868 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2869 ->getDecl()
2870 ->getUnderlyingType()
2871 .getAsOpaquePtr(),
2872 element_type_ptr, size, is_incomplete);
2873 case clang::Type::Auto:
2874 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2875 ->getDeducedType()
2876 .getAsOpaquePtr(),
2877 element_type_ptr, size, is_incomplete);
2878 case clang::Type::Elaborated:
2879 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2880 ->getNamedType()
2881 .getAsOpaquePtr(),
2882 element_type_ptr, size, is_incomplete);
2883 case clang::Type::Paren:
2884 return IsArrayType(
2885 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2886 element_type_ptr, size, is_incomplete);
2887 }
2888 if (element_type_ptr)
2889 element_type_ptr->Clear();
2890 if (size)
2891 *size = 0;
2892 if (is_incomplete)
2893 *is_incomplete = false;
2894 return false;
2895}
2896
2897bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
2898 CompilerType *element_type, uint64_t *size) {
2899 clang::QualType qual_type(GetCanonicalQualType(type));
2900
2901 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2902 switch (type_class) {
2903 case clang::Type::Vector: {
2904 const clang::VectorType *vector_type =
2905 qual_type->getAs<clang::VectorType>();
2906 if (vector_type) {
2907 if (size)
2908 *size = vector_type->getNumElements();
2909 if (element_type)
2910 *element_type =
2911 CompilerType(getASTContext(), vector_type->getElementType());
2912 }
2913 return true;
2914 } break;
2915 case clang::Type::ExtVector: {
2916 const clang::ExtVectorType *ext_vector_type =
2917 qual_type->getAs<clang::ExtVectorType>();
2918 if (ext_vector_type) {
2919 if (size)
2920 *size = ext_vector_type->getNumElements();
2921 if (element_type)
2922 *element_type =
2923 CompilerType(getASTContext(), ext_vector_type->getElementType());
2924 }
2925 return true;
2926 }
2927 default:
2928 break;
2929 }
2930 return false;
2931}
2932
2933bool ClangASTContext::IsRuntimeGeneratedType(
2934 lldb::opaque_compiler_type_t type) {
2935 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
1
Calling 'ClangASTContext::getASTContext'
2936 ->GetDeclContextForType(GetQualType(type));
2937 if (!decl_ctx)
2938 return false;
2939
2940 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2941 return false;
2942
2943 clang::ObjCInterfaceDecl *result_iface_decl =
2944 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2945
2946 ClangASTMetadata *ast_metadata =
2947 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2948 if (!ast_metadata)
2949 return false;
2950 return (ast_metadata->GetISAPtr() != 0);
2951}
2952
2953bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
2954 return GetQualType(type).getUnqualifiedType()->isCharType();
2955}
2956
2957bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
2958 const bool allow_completion = false;
2959 return GetCompleteQualType(getASTContext(), GetQualType(type),
2960 allow_completion);
2961}
2962
2963bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
2964 return GetQualType(type).isConstQualified();
2965}
2966
2967bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
2968 uint32_t &length) {
2969 CompilerType pointee_or_element_clang_type;
2970 length = 0;
2971 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
2972
2973 if (!pointee_or_element_clang_type.IsValid())
2974 return false;
2975
2976 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
2977 if (pointee_or_element_clang_type.IsCharType()) {
2978 if (type_flags.Test(eTypeIsArray)) {
2979 // We know the size of the array and it could be a C string
2980 // since it is an array of characters
2981 length = llvm::cast<clang::ConstantArrayType>(
2982 GetCanonicalQualType(type).getTypePtr())
2983 ->getSize()
2984 .getLimitedValue();
2985 }
2986 return true;
2987 }
2988 }
2989 return false;
2990}
2991
2992bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
2993 bool *is_variadic_ptr) {
2994 if (type) {
2995 clang::QualType qual_type(GetCanonicalQualType(type));
2996
2997 if (qual_type->isFunctionType()) {
2998 if (is_variadic_ptr) {
2999 const clang::FunctionProtoType *function_proto_type =
3000 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3001 if (function_proto_type)
3002 *is_variadic_ptr = function_proto_type->isVariadic();
3003 else
3004 *is_variadic_ptr = false;
3005 }
3006 return true;
3007 }
3008
3009 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3010 switch (type_class) {
3011 default:
3012 break;
3013 case clang::Type::Typedef:
3014 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
3015 ->getDecl()
3016 ->getUnderlyingType()
3017 .getAsOpaquePtr(),
3018 nullptr);
3019 case clang::Type::Auto:
3020 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
3021 ->getDeducedType()
3022 .getAsOpaquePtr(),
3023 nullptr);
3024 case clang::Type::Elaborated:
3025 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
3026 ->getNamedType()
3027 .getAsOpaquePtr(),
3028 nullptr);
3029 case clang::Type::Paren:
3030 return IsFunctionType(
3031 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3032 nullptr);
3033 case clang::Type::LValueReference:
3034 case clang::Type::RValueReference: {
3035 const clang::ReferenceType *reference_type =
3036 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3037 if (reference_type)
3038 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
3039 nullptr);
3040 } break;
3041 }
3042 }
3043 return false;
3044}
3045
3046// Used to detect "Homogeneous Floating-point Aggregates"
3047uint32_t
3048ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
3049 CompilerType *base_type_ptr) {
3050 if (!type)
3051 return 0;
3052
3053 clang::QualType qual_type(GetCanonicalQualType(type));
3054 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3055 switch (type_class) {
3056 case clang::Type::Record:
3057 if (GetCompleteType(type)) {
3058 const clang::CXXRecordDecl *cxx_record_decl =
3059 qual_type->getAsCXXRecordDecl();
3060 if (cxx_record_decl) {
3061 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3062 return 0;
3063 }
3064 const clang::RecordType *record_type =
3065 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3066 if (record_type) {
3067 const clang::RecordDecl *record_decl = record_type->getDecl();
3068 if (record_decl) {
3069 // We are looking for a structure that contains only floating point
3070 // types
3071 clang::RecordDecl::field_iterator field_pos,
3072 field_end = record_decl->field_end();
3073 uint32_t num_fields = 0;
3074 bool is_hva = false;
3075 bool is_hfa = false;
3076 clang::QualType base_qual_type;
3077 uint64_t base_bitwidth = 0;
3078 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3079 ++field_pos) {
3080 clang::QualType field_qual_type = field_pos->getType();
3081 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
3082 if (field_qual_type->isFloatingType()) {
3083 if (field_qual_type->isComplexType())
3084 return 0;
3085 else {
3086 if (num_fields == 0)
3087 base_qual_type = field_qual_type;
3088 else {
3089 if (is_hva)
3090 return 0;
3091 is_hfa = true;
3092 if (field_qual_type.getTypePtr() !=
3093 base_qual_type.getTypePtr())
3094 return 0;
3095 }
3096 }
3097 } else if (field_qual_type->isVectorType() ||
3098 field_qual_type->isExtVectorType()) {
3099 if (num_fields == 0) {
3100 base_qual_type = field_qual_type;
3101 base_bitwidth = field_bitwidth;
3102 } else {
3103 if (is_hfa)
3104 return 0;
3105 is_hva = true;
3106 if (base_bitwidth != field_bitwidth)
3107 return 0;
3108 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3109 return 0;
3110 }
3111 } else
3112 return 0;
3113 ++num_fields;
3114 }
3115 if (base_type_ptr)
3116 *base_type_ptr = CompilerType(getASTContext(), base_qual_type);
3117 return num_fields;
3118 }
3119 }
3120 }
3121 break;
3122
3123 case clang::Type::Typedef:
3124 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3125 ->getDecl()
3126 ->getUnderlyingType()
3127 .getAsOpaquePtr(),
3128 base_type_ptr);
3129
3130 case clang::Type::Auto:
3131 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3132 ->getDeducedType()
3133 .getAsOpaquePtr(),
3134 base_type_ptr);
3135
3136 case clang::Type::Elaborated:
3137 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3138 ->getNamedType()
3139 .getAsOpaquePtr(),
3140 base_type_ptr);
3141 default:
3142 break;
3143 }
3144 return 0;
3145}
3146
3147size_t ClangASTContext::GetNumberOfFunctionArguments(
3148 lldb::opaque_compiler_type_t type) {
3149 if (type) {
3150 clang::QualType qual_type(GetCanonicalQualType(type));
3151 const clang::FunctionProtoType *func =
3152 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3153 if (func)
3154 return func->getNumParams();
3155 }
3156 return 0;
3157}
3158
3159CompilerType
3160ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3161 const size_t index) {
3162 if (type) {
3163 clang::QualType qual_type(GetQualType(type));
3164 const clang::FunctionProtoType *func =
3165 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3166 if (func) {
3167 if (index < func->getNumParams())
3168 return CompilerType(getASTContext(), func->getParamType(index));
3169 }
3170 }
3171 return CompilerType();
3172}
3173
3174bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3175 if (type) {
3176 clang::QualType qual_type(GetCanonicalQualType(type));
3177
3178 if (qual_type->isFunctionPointerType())
3179 return true;
3180
3181 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3182 switch (type_class) {
3183 default:
3184 break;
3185 case clang::Type::Typedef:
3186 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3187 ->getDecl()
3188 ->getUnderlyingType()
3189 .getAsOpaquePtr());
3190 case clang::Type::Auto:
3191 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3192 ->getDeducedType()
3193 .getAsOpaquePtr());
3194 case clang::Type::Elaborated:
3195 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3196 ->getNamedType()
3197 .getAsOpaquePtr());
3198 case clang::Type::Paren:
3199 return IsFunctionPointerType(
3200 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3201
3202 case clang::Type::LValueReference:
3203 case clang::Type::RValueReference: {
3204 const clang::ReferenceType *reference_type =
3205 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3206 if (reference_type)
3207 return IsFunctionPointerType(
3208 reference_type->getPointeeType().getAsOpaquePtr());
3209 } break;
3210 }
3211 }
3212 return false;
3213}
3214
3215bool ClangASTContext::IsBlockPointerType(
3216 lldb::opaque_compiler_type_t type,
3217 CompilerType *function_pointer_type_ptr) {
3218 if (type) {
3219 clang::QualType qual_type(GetCanonicalQualType(type));
3220
3221 if (qual_type->isBlockPointerType()) {
3222 if (function_pointer_type_ptr) {
3223 const clang::BlockPointerType *block_pointer_type =
3224 qual_type->getAs<clang::BlockPointerType>();
3225 QualType pointee_type = block_pointer_type->getPointeeType();
3226 QualType function_pointer_type = m_ast_ap->getPointerType(pointee_type);
3227 *function_pointer_type_ptr =
3228 CompilerType(getASTContext(), function_pointer_type);
3229 }
3230 return true;
3231 }
3232
3233 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3234 switch (type_class) {
3235 default:
3236 break;
3237 case clang::Type::Typedef:
3238 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3239 ->getDecl()
3240 ->getUnderlyingType()
3241 .getAsOpaquePtr(),
3242 function_pointer_type_ptr);
3243 case clang::Type::Auto:
3244 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3245 ->getDeducedType()
3246 .getAsOpaquePtr(),
3247 function_pointer_type_ptr);
3248 case clang::Type::Elaborated:
3249 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3250 ->getNamedType()
3251 .getAsOpaquePtr(),
3252 function_pointer_type_ptr);
3253 case clang::Type::Paren:
3254 return IsBlockPointerType(
3255 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3256 function_pointer_type_ptr);
3257
3258 case clang::Type::LValueReference:
3259 case clang::Type::RValueReference: {
3260 const clang::ReferenceType *reference_type =
3261 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3262 if (reference_type)
3263 return IsBlockPointerType(
3264 reference_type->getPointeeType().getAsOpaquePtr(),
3265 function_pointer_type_ptr);
3266 } break;
3267 }
3268 }
3269 return false;
3270}
3271
3272bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3273 bool &is_signed) {
3274 if (!type)
3275 return false;
3276
3277 clang::QualType qual_type(GetCanonicalQualType(type));
3278 const clang::BuiltinType *builtin_type =
3279 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3280
3281 if (builtin_type) {
3282 if (builtin_type->isInteger()) {
3283 is_signed = builtin_type->isSignedInteger();
3284 return true;
3285 }
3286 }
3287
3288 return false;
3289}
3290
3291bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3292 bool &is_signed) {
3293 if (type) {
3294 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3295 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3296
3297 if (enum_type) {
3298 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3299 is_signed);
3300 return true;
3301 }
3302 }
3303
3304 return false;
3305}
3306
3307bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3308 CompilerType *pointee_type) {
3309 if (type) {
3310 clang::QualType qual_type(GetCanonicalQualType(type));
3311 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3312 switch (type_class) {
3313 case clang::Type::Builtin:
3314 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3315 default:
3316 break;
3317 case clang::BuiltinType::ObjCId:
3318 case clang::BuiltinType::ObjCClass:
3319 return true;
3320 }
3321 return false;
3322 case clang::Type::ObjCObjectPointer:
3323 if (pointee_type)
3324 pointee_type->SetCompilerType(
3325 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3326 ->getPointeeType());
3327 return true;
3328 case clang::Type::BlockPointer:
3329 if (pointee_type)
3330 pointee_type->SetCompilerType(
3331 getASTContext(),
3332 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3333 return true;
3334 case clang::Type::Pointer:
3335 if (pointee_type)
3336 pointee_type->SetCompilerType(
3337 getASTContext(),
3338 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3339 return true;
3340 case clang::Type::MemberPointer:
3341 if (pointee_type)
3342 pointee_type->SetCompilerType(
3343 getASTContext(),
3344 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3345 return true;
3346 case clang::Type::Typedef:
3347 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3348 ->getDecl()
3349 ->getUnderlyingType()
3350 .getAsOpaquePtr(),
3351 pointee_type);
3352 case clang::Type::Auto:
3353 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3354 ->getDeducedType()
3355 .getAsOpaquePtr(),
3356 pointee_type);
3357 case clang::Type::Elaborated:
3358 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3359 ->getNamedType()
3360 .getAsOpaquePtr(),
3361 pointee_type);
3362 case clang::Type::Paren:
3363 return IsPointerType(
3364 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3365 pointee_type);
3366 default:
3367 break;
3368 }
3369 }
3370 if (pointee_type)
3371 pointee_type->Clear();
3372 return false;
3373}
3374
3375bool ClangASTContext::IsPointerOrReferenceType(
3376 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3377 if (type) {
3378 clang::QualType qual_type(GetCanonicalQualType(type));
3379 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3380 switch (type_class) {
3381 case clang::Type::Builtin:
3382 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3383 default:
3384 break;
3385 case clang::BuiltinType::ObjCId:
3386 case clang::BuiltinType::ObjCClass:
3387 return true;
3388 }
3389 return false;
3390 case clang::Type::ObjCObjectPointer:
3391 if (pointee_type)
3392 pointee_type->SetCompilerType(
3393 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3394 ->getPointeeType());
3395 return true;
3396 case clang::Type::BlockPointer:
3397 if (pointee_type)
3398 pointee_type->SetCompilerType(
3399 getASTContext(),
3400 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3401 return true;
3402 case clang::Type::Pointer:
3403 if (pointee_type)
3404 pointee_type->SetCompilerType(
3405 getASTContext(),
3406 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3407 return true;
3408 case clang::Type::MemberPointer:
3409 if (pointee_type)
3410 pointee_type->SetCompilerType(
3411 getASTContext(),
3412 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3413 return true;
3414 case clang::Type::LValueReference:
3415 if (pointee_type)
3416 pointee_type->SetCompilerType(
3417 getASTContext(),
3418 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3419 return true;
3420 case clang::Type::RValueReference:
3421 if (pointee_type)
3422 pointee_type->SetCompilerType(
3423 getASTContext(),
3424 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3425 return true;
3426 case clang::Type::Typedef:
3427 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3428 ->getDecl()
3429 ->getUnderlyingType()
3430 .getAsOpaquePtr(),
3431 pointee_type);
3432 case clang::Type::Auto:
3433 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3434 ->getDeducedType()
3435 .getAsOpaquePtr(),
3436 pointee_type);
3437 case clang::Type::Elaborated:
3438 return IsPointerOrReferenceType(
3439 llvm::cast<clang::ElaboratedType>(qual_type)
3440 ->getNamedType()
3441 .getAsOpaquePtr(),
3442 pointee_type);
3443 case clang::Type::Paren:
3444 return IsPointerOrReferenceType(
3445 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3446 pointee_type);
3447 default:
3448 break;
3449 }
3450 }
3451 if (pointee_type)
3452 pointee_type->Clear();
3453 return false;
3454}
3455
3456bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3457 CompilerType *pointee_type,
3458 bool *is_rvalue) {
3459 if (type) {
3460 clang::QualType qual_type(GetCanonicalQualType(type));
3461 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3462
3463 switch (type_class) {
3464 case clang::Type::LValueReference:
3465 if (pointee_type)
3466 pointee_type->SetCompilerType(
3467 getASTContext(),
3468 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3469 if (is_rvalue)
3470 *is_rvalue = false;
3471 return true;
3472 case clang::Type::RValueReference:
3473 if (pointee_type)
3474 pointee_type->SetCompilerType(
3475 getASTContext(),
3476 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3477 if (is_rvalue)
3478 *is_rvalue = true;
3479 return true;
3480 case clang::Type::Typedef:
3481 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3482 ->getDecl()
3483 ->getUnderlyingType()
3484 .getAsOpaquePtr(),
3485 pointee_type, is_rvalue);
3486 case clang::Type::Auto:
3487 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3488 ->getDeducedType()
3489 .getAsOpaquePtr(),
3490 pointee_type, is_rvalue);
3491 case clang::Type::Elaborated:
3492 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3493 ->getNamedType()
3494 .getAsOpaquePtr(),
3495 pointee_type, is_rvalue);
3496 case clang::Type::Paren:
3497 return IsReferenceType(
3498 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3499 pointee_type, is_rvalue);
3500
3501 default:
3502 break;
3503 }
3504 }
3505 if (pointee_type)
3506 pointee_type->Clear();
3507 return false;
3508}
3509
3510bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3511 uint32_t &count, bool &is_complex) {
3512 if (type) {
3513 clang::QualType qual_type(GetCanonicalQualType(type));
3514
3515 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3516 qual_type->getCanonicalTypeInternal())) {
3517 clang::BuiltinType::Kind kind = BT->getKind();
3518 if (kind >= clang::BuiltinType::Float &&
3519 kind <= clang::BuiltinType::LongDouble) {
3520 count = 1;
3521 is_complex = false;
3522 return true;
3523 }
3524 } else if (const clang::ComplexType *CT =
3525 llvm::dyn_cast<clang::ComplexType>(
3526 qual_type->getCanonicalTypeInternal())) {
3527 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3528 is_complex)) {
3529 count = 2;
3530 is_complex = true;
3531 return true;
3532 }
3533 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3534 qual_type->getCanonicalTypeInternal())) {
3535 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3536 is_complex)) {
3537 count = VT->getNumElements();
3538 is_complex = false;
3539 return true;
3540 }
3541 }
3542 }
3543 count = 0;
3544 is_complex = false;
3545 return false;
3546}
3547
3548bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3549 if (!type)
3550 return false;
3551
3552 clang::QualType qual_type(GetQualType(type));
3553 const clang::TagType *tag_type =
3554 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3555 if (tag_type) {
3556 clang::TagDecl *tag_decl = tag_type->getDecl();
3557 if (tag_decl)
3558 return tag_decl->isCompleteDefinition();
3559 return false;
3560 } else {
3561 const clang::ObjCObjectType *objc_class_type =
3562 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3563 if (objc_class_type) {
3564 clang::ObjCInterfaceDecl *class_interface_decl =
3565 objc_class_type->getInterface();
3566 if (class_interface_decl)
3567 return class_interface_decl->getDefinition() != nullptr;
3568 return false;
3569 }
3570 }
3571 return true;
3572}
3573
3574bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
3575 if (type) {
3576 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3577
3578 const clang::ObjCObjectPointerType *obj_pointer_type =
3579 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3580
3581 if (obj_pointer_type)
3582 return obj_pointer_type->isObjCClassType();
3583 }
3584 return false;
3585}
3586
3587bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3588 if (ClangUtil::IsClangType(type))
3589 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3590 return false;
3591}
3592
3593bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3594 if (!type)
3595 return false;
3596 clang::QualType qual_type(GetCanonicalQualType(type));
3597 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3598 return (type_class == clang::Type::Record);
3599}
3600
3601bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3602 if (!type)
3603 return false;
3604 clang::QualType qual_type(GetCanonicalQualType(type));
3605 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3606 return (type_class == clang::Type::Enum);
3607}
3608
3609bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3610 if (type) {
3611 clang::QualType qual_type(GetCanonicalQualType(type));
3612 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3613 switch (type_class) {
3614 case clang::Type::Record:
3615 if (GetCompleteType(type)) {
3616 const clang::RecordType *record_type =
3617 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3618 const clang::RecordDecl *record_decl = record_type->getDecl();
3619 if (record_decl) {
3620 const clang::CXXRecordDecl *cxx_record_decl =
3621 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3622 if (cxx_record_decl)
3623 return cxx_record_decl->isPolymorphic();
3624 }
3625 }
3626 break;
3627
3628 default:
3629 break;
3630 }
3631 }
3632 return false;
3633}
3634
3635bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3636 CompilerType *dynamic_pointee_type,
3637 bool check_cplusplus,
3638 bool check_objc) {
3639 clang::QualType pointee_qual_type;
3640 if (type) {
3641 clang::QualType qual_type(GetCanonicalQualType(type));
3642 bool success = false;
3643 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3644 switch (type_class) {
3645 case clang::Type::Builtin:
3646 if (check_objc &&
3647 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3648 clang::BuiltinType::ObjCId) {
3649 if (dynamic_pointee_type)
3650 dynamic_pointee_type->SetCompilerType(this, type);
3651 return true;
3652 }
3653 break;
3654
3655 case clang::Type::ObjCObjectPointer:
3656 if (check_objc) {
3657 if (auto objc_pointee_type =
3658 qual_type->getPointeeType().getTypePtrOrNull()) {
3659 if (auto objc_object_type =
3660 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3661 objc_pointee_type)) {
3662 if (objc_object_type->isObjCClass())
3663 return false;
3664 }
3665 }
3666 if (dynamic_pointee_type)
3667 dynamic_pointee_type->SetCompilerType(
3668 getASTContext(),
3669 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3670 ->getPointeeType());
3671 return true;
3672 }
3673 break;
3674
3675 case clang::Type::Pointer:
3676 pointee_qual_type =
3677 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3678 success = true;
3679 break;
3680
3681 case clang::Type::LValueReference:
3682 case clang::Type::RValueReference:
3683 pointee_qual_type =
3684 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3685 success = true;
3686 break;
3687
3688 case clang::Type::Typedef:
3689 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3690 ->getDecl()
3691 ->getUnderlyingType()
3692 .getAsOpaquePtr(),
3693 dynamic_pointee_type, check_cplusplus,
3694 check_objc);
3695
3696 case clang::Type::Auto:
3697 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3698 ->getDeducedType()
3699 .getAsOpaquePtr(),
3700 dynamic_pointee_type, check_cplusplus,
3701 check_objc);
3702
3703 case clang::Type::Elaborated:
3704 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3705 ->getNamedType()
3706 .getAsOpaquePtr(),
3707 dynamic_pointee_type, check_cplusplus,
3708 check_objc);
3709
3710 case clang::Type::Paren:
3711 return IsPossibleDynamicType(
3712 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3713 dynamic_pointee_type, check_cplusplus, check_objc);
3714 default:
3715 break;
3716 }
3717
3718 if (success) {
3719 // Check to make sure what we are pointing too is a possible dynamic C++
3720 // type
3721 // We currently accept any "void *" (in case we have a class that has been
3722 // watered down to an opaque pointer) and virtual C++ classes.
3723 const clang::Type::TypeClass pointee_type_class =
3724 pointee_qual_type.getCanonicalType()->getTypeClass();
3725 switch (pointee_type_class) {
3726 case clang::Type::Builtin:
3727 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3728 case clang::BuiltinType::UnknownAny:
3729 case clang::BuiltinType::Void:
3730 if (dynamic_pointee_type)
3731 dynamic_pointee_type->SetCompilerType(getASTContext(),
3732 pointee_qual_type);
3733 return true;
3734 default:
3735 break;
3736 }
3737 break;
3738
3739 case clang::Type::Record:
3740 if (check_cplusplus) {
3741 clang::CXXRecordDecl *cxx_record_decl =
3742 pointee_qual_type->getAsCXXRecordDecl();
3743 if (cxx_record_decl) {
3744 bool is_complete = cxx_record_decl->isCompleteDefinition();
3745
3746 if (is_complete)
3747 success = cxx_record_decl->isDynamicClass();
3748 else {
3749 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3750 getASTContext(), cxx_record_decl);
3751 if (metadata)
3752 success = metadata->GetIsDynamicCXXType();
3753 else {
3754 is_complete = CompilerType(getASTContext(), pointee_qual_type)
3755 .GetCompleteType();
3756 if (is_complete)
3757 success = cxx_record_decl->isDynamicClass();
3758 else
3759 success = false;
3760 }
3761 }
3762
3763 if (success) {
3764 if (dynamic_pointee_type)
3765 dynamic_pointee_type->SetCompilerType(getASTContext(),
3766 pointee_qual_type);
3767 return true;
3768 }
3769 }
3770 }
3771 break;
3772
3773 case clang::Type::ObjCObject:
3774 case clang::Type::ObjCInterface:
3775 if (check_objc) {
3776 if (dynamic_pointee_type)
3777 dynamic_pointee_type->SetCompilerType(getASTContext(),
3778 pointee_qual_type);
3779 return true;
3780 }
3781 break;
3782
3783 default:
3784 break;
3785 }
3786 }
3787 }
3788 if (dynamic_pointee_type)
3789 dynamic_pointee_type->Clear();
3790 return false;
3791}
3792
3793bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3794 if (!type)
3795 return false;
3796
3797 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3798}
3799
3800bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3801 if (!type)
3802 return false;
3803 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3804}
3805
3806bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3807 if (!type)
3808 return false;
3809 return GetCanonicalQualType(type)->isVoidType();
3810}
3811
3812bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3813 return ClangASTContextSupportsLanguage(language);
3814}
3815
3816bool ClangASTContext::GetCXXClassName(const CompilerType &type,
3817 std::string &class_name) {
3818 if (type) {
3819 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3820 if (!qual_type.isNull()) {
3821 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3822 if (cxx_record_decl) {
3823 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3824 return true;
3825 }
3826 }
3827 }
3828 class_name.clear();
3829 return false;
3830}
3831
3832bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3833 if (!type)
3834 return false;
3835
3836 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3837 if (!qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr)
3838 return true;
3839 return false;
3840}
3841
3842bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3843 if (!type)
3844 return false;
3845 clang::QualType qual_type(GetCanonicalQualType(type));
3846 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3847 if (tag_type)
3848 return tag_type->isBeingDefined();
3849 return false;
3850}
3851
3852bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3853 CompilerType *class_type_ptr) {
3854 if (!type)
3855 return false;
3856
3857 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3858
3859 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3860 if (class_type_ptr) {
3861 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3862 const clang::ObjCObjectPointerType *obj_pointer_type =
3863 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3864 if (obj_pointer_type == nullptr)
3865 class_type_ptr->Clear();
3866 else
3867 class_type_ptr->SetCompilerType(
3868 type.GetTypeSystem(),
3869 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3870 .getAsOpaquePtr());
3871 }
3872 }
3873 return true;
3874 }
3875 if (class_type_ptr)
3876 class_type_ptr->Clear();
3877 return false;
3878}
3879
3880bool ClangASTContext::GetObjCClassName(const CompilerType &type,
3881 std::string &class_name) {
3882 if (!type)
3883 return false;
3884
3885 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3886
3887 const clang::ObjCObjectType *object_type =
3888 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3889 if (object_type) {
3890 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3891 if (interface) {
3892 class_name = interface->getNameAsString();
3893 return true;
3894 }
3895 }
3896 return false;
3897}
3898
3899//----------------------------------------------------------------------
3900// Type Completion
3901//----------------------------------------------------------------------
3902
3903bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
3904 if (!type)
3905 return false;
3906 const bool allow_completion = true;
3907 return GetCompleteQualType(getASTContext(), GetQualType(type),
3908 allow_completion);
3909}
3910
3911ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
3912 std::string type_name;
3913 if (type) {
3914 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
3915 clang::QualType qual_type(GetQualType(type));
3916 printing_policy.SuppressTagKeyword = true;
3917 const clang::TypedefType *typedef_type =
3918 qual_type->getAs<clang::TypedefType>();
3919 if (typedef_type) {
3920 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3921 type_name = typedef_decl->getQualifiedNameAsString();
3922 } else {
3923 type_name = qual_type.getAsString(printing_policy);
3924 }
3925 }
3926 return ConstString(type_name);
3927}
3928
3929uint32_t
3930ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
3931 CompilerType *pointee_or_element_clang_type) {
3932 if (!type)
3933 return 0;
3934
3935 if (pointee_or_element_clang_type)
3936 pointee_or_element_clang_type->Clear();
3937
3938 clang::QualType qual_type(GetQualType(type));
3939
3940 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3941 switch (type_class) {
3942 case clang::Type::Attributed:
3943 return GetTypeInfo(
3944 qual_type->getAs<clang::AttributedType>()
3945 ->getModifiedType().getAsOpaquePtr(),
3946 pointee_or_element_clang_type);
3947 case clang::Type::Builtin: {
3948 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
3949 qual_type->getCanonicalTypeInternal());
3950
3951 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3952 switch (builtin_type->getKind()) {
3953 case clang::BuiltinType::ObjCId:
3954 case clang::BuiltinType::ObjCClass:
3955 if (pointee_or_element_clang_type)
3956 pointee_or_element_clang_type->SetCompilerType(
3957 getASTContext(), getASTContext()->ObjCBuiltinClassTy);
3958 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3959 break;
3960
3961 case clang::BuiltinType::ObjCSel:
3962 if (pointee_or_element_clang_type)
3963 pointee_or_element_clang_type->SetCompilerType(getASTContext(),
3964 getASTContext()->CharTy);
3965 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3966 break;
3967
3968 case clang::BuiltinType::Bool:
3969 case clang::BuiltinType::Char_U:
3970 case clang::BuiltinType::UChar:
3971 case clang::BuiltinType::WChar_U:
3972 case clang::BuiltinType::Char16:
3973 case clang::BuiltinType::Char32:
3974 case clang::BuiltinType::UShort:
3975 case clang::BuiltinType::UInt:
3976 case clang::BuiltinType::ULong:
3977 case clang::BuiltinType::ULongLong:
3978 case clang::BuiltinType::UInt128:
3979 case clang::BuiltinType::Char_S:
3980 case clang::BuiltinType::SChar:
3981 case clang::BuiltinType::WChar_S:
3982 case clang::BuiltinType::Short:
3983 case clang::BuiltinType::Int:
3984 case clang::BuiltinType::Long:
3985 case clang::BuiltinType::LongLong:
3986 case clang::BuiltinType::Int128:
3987 case clang::BuiltinType::Float:
3988 case clang::BuiltinType::Double:
3989 case clang::BuiltinType::LongDouble:
3990 builtin_type_flags |= eTypeIsScalar;
3991 if (builtin_type->isInteger()) {
3992 builtin_type_flags |= eTypeIsInteger;
3993 if (builtin_type->isSignedInteger())
3994 builtin_type_flags |= eTypeIsSigned;
3995 } else if (builtin_type->isFloatingPoint())
3996 builtin_type_flags |= eTypeIsFloat;
3997 break;
3998 default:
3999 break;
4000 }
4001 return builtin_type_flags;
4002 }
4003
4004 case clang::Type::BlockPointer:
4005 if (pointee_or_element_clang_type)
4006 pointee_or_element_clang_type->SetCompilerType(
4007 getASTContext(), qual_type->getPointeeType());
4008 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
4009
4010 case clang::Type::Complex: {
4011 uint32_t complex_type_flags =
4012 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
4013 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
4014 qual_type->getCanonicalTypeInternal());
4015 if (complex_type) {
4016 clang::QualType complex_element_type(complex_type->getElementType());
4017 if (complex_element_type->isIntegerType())
4018 complex_type_flags |= eTypeIsFloat;
4019 else if (complex_element_type->isFloatingType())
4020 complex_type_flags |= eTypeIsInteger;
4021 }
4022 return complex_type_flags;
4023 } break;
4024
4025 case clang::Type::ConstantArray:
4026 case clang::Type::DependentSizedArray:
4027 case clang::Type::IncompleteArray:
4028 case clang::Type::VariableArray:
4029 if (pointee_or_element_clang_type)
4030 pointee_or_element_clang_type->SetCompilerType(
4031 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
4032 ->getElementType());
4033 return eTypeHasChildren | eTypeIsArray;
4034
4035 case clang::Type::DependentName:
4036 return 0;
4037 case clang::Type::DependentSizedExtVector:
4038 return eTypeHasChildren | eTypeIsVector;
4039 case clang::Type::DependentTemplateSpecialization:
4040 return eTypeIsTemplate;
4041 case clang::Type::Decltype:
4042 return 0;
4043
4044 case clang::Type::Enum:
4045 if (pointee_or_element_clang_type)
4046 pointee_or_element_clang_type->SetCompilerType(
4047 getASTContext(),
4048 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
4049 return eTypeIsEnumeration | eTypeHasValue;
4050
4051 case clang::Type::Auto:
4052 return CompilerType(
4053 getASTContext(),
4054 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4055 .GetTypeInfo(pointee_or_element_clang_type);
4056 case clang::Type::Elaborated:
4057 return CompilerType(
4058 getASTContext(),
4059 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4060 .GetTypeInfo(pointee_or_element_clang_type);
4061 case clang::Type::Paren:
4062 return CompilerType(getASTContext(),
4063 llvm::cast<clang::ParenType>(qual_type)->desugar())
4064 .GetTypeInfo(pointee_or_element_clang_type);
4065
4066 case clang::Type::FunctionProto:
4067 return eTypeIsFuncPrototype | eTypeHasValue;
4068 case clang::Type::FunctionNoProto:
4069 return eTypeIsFuncPrototype | eTypeHasValue;
4070 case clang::Type::InjectedClassName:
4071 return 0;
4072
4073 case clang::Type::LValueReference:
4074 case clang::Type::RValueReference:
4075 if (pointee_or_element_clang_type)
4076 pointee_or_element_clang_type->SetCompilerType(
4077 getASTContext(),
4078 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4079 ->getPointeeType());
4080 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4081
4082 case clang::Type::MemberPointer:
4083 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4084
4085 case clang::Type::ObjCObjectPointer:
4086 if (pointee_or_element_clang_type)
4087 pointee_or_element_clang_type->SetCompilerType(
4088 getASTContext(), qual_type->getPointeeType());
4089 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4090 eTypeHasValue;
4091
4092 case clang::Type::ObjCObject:
4093 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4094 case clang::Type::ObjCInterface:
4095 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4096
4097 case clang::Type::Pointer:
4098 if (pointee_or_element_clang_type)
4099 pointee_or_element_clang_type->SetCompilerType(
4100 getASTContext(), qual_type->getPointeeType());
4101 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4102
4103 case clang::Type::Record:
4104 if (qual_type->getAsCXXRecordDecl())
4105 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4106 else
4107 return eTypeHasChildren | eTypeIsStructUnion;
4108 break;
4109 case clang::Type::SubstTemplateTypeParm:
4110 return eTypeIsTemplate;
4111 case clang::Type::TemplateTypeParm:
4112 return eTypeIsTemplate;
4113 case clang::Type::TemplateSpecialization:
4114 return eTypeIsTemplate;
4115
4116 case clang::Type::Typedef:
4117 return eTypeIsTypedef |
4118 CompilerType(getASTContext(),
4119 llvm::cast<clang::TypedefType>(qual_type)
4120 ->getDecl()
4121 ->getUnderlyingType())
4122 .GetTypeInfo(pointee_or_element_clang_type);
4123 case clang::Type::TypeOfExpr:
4124 return 0;
4125 case clang::Type::TypeOf:
4126 return 0;
4127 case clang::Type::UnresolvedUsing:
4128 return 0;
4129
4130 case clang::Type::ExtVector:
4131 case clang::Type::Vector: {
4132 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4133 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4134 qual_type->getCanonicalTypeInternal());
4135 if (vector_type) {
4136 if (vector_type->isIntegerType())
4137 vector_type_flags |= eTypeIsFloat;
4138 else if (vector_type->isFloatingType())
4139 vector_type_flags |= eTypeIsInteger;
4140 }
4141 return vector_type_flags;
4142 }
4143 default:
4144 return 0;
4145 }
4146 return 0;
4147}
4148
4149lldb::LanguageType
4150ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4151 if (!type)
4152 return lldb::eLanguageTypeC;
4153
4154 // If the type is a reference, then resolve it to what it refers to first:
4155 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4156 if (qual_type->isAnyPointerType()) {
4157 if (qual_type->isObjCObjectPointerType())
4158 return lldb::eLanguageTypeObjC;
4159
4160 clang::QualType pointee_type(qual_type->getPointeeType());
4161 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
4162 return lldb::eLanguageTypeC_plus_plus;
4163 if (pointee_type->isObjCObjectOrInterfaceType())
4164 return lldb::eLanguageTypeObjC;
4165 if (pointee_type->isObjCClassType())
4166 return lldb::eLanguageTypeObjC;
4167 if (pointee_type.getTypePtr() ==
4168 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4169 return lldb::eLanguageTypeObjC;
4170 } else {
4171 if (qual_type->isObjCObjectOrInterfaceType())
4172 return lldb::eLanguageTypeObjC;
4173 if (qual_type->getAsCXXRecordDecl())
4174 return lldb::eLanguageTypeC_plus_plus;
4175 switch (qual_type->getTypeClass()) {
4176 default:
4177 break;
4178 case clang::Type::Builtin:
4179 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4180 default:
4181 case clang::BuiltinType::Void:
4182 case clang::BuiltinType::Bool:
4183 case clang::BuiltinType::Char_U:
4184 case clang::BuiltinType::UChar:
4185 case clang::BuiltinType::WChar_U:
4186 case clang::BuiltinType::Char16:
4187 case clang::BuiltinType::Char32:
4188 case clang::BuiltinType::UShort:
4189 case clang::BuiltinType::UInt:
4190 case clang::BuiltinType::ULong:
4191 case clang::BuiltinType::ULongLong:
4192 case clang::BuiltinType::UInt128:
4193 case clang::BuiltinType::Char_S:
4194 case clang::BuiltinType::SChar:
4195 case clang::BuiltinType::WChar_S:
4196 case clang::BuiltinType::Short:
4197 case clang::BuiltinType::Int:
4198 case clang::BuiltinType::Long:
4199 case clang::BuiltinType::LongLong:
4200 case clang::BuiltinType::Int128:
4201 case clang::BuiltinType::Float:
4202 case clang::BuiltinType::Double:
4203 case clang::BuiltinType::LongDouble:
4204 break;
4205
4206 case clang::BuiltinType::NullPtr:
4207 return eLanguageTypeC_plus_plus;
4208
4209 case clang::BuiltinType::ObjCId:
4210 case clang::BuiltinType::ObjCClass:
4211 case clang::BuiltinType::ObjCSel:
4212 return eLanguageTypeObjC;
4213
4214 case clang::BuiltinType::Dependent:
4215 case clang::BuiltinType::Overload:
4216 case clang::BuiltinType::BoundMember:
4217 case clang::BuiltinType::UnknownAny:
4218 break;
4219 }
4220 break;
4221 case clang::Type::Typedef:
4222 return CompilerType(getASTContext(),
4223 llvm::cast<clang::TypedefType>(qual_type)
4224 ->getDecl()
4225 ->getUnderlyingType())
4226 .GetMinimumLanguage();
4227 }
4228 }
4229 return lldb::eLanguageTypeC;
4230}
4231
4232lldb::TypeClass
4233ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4234 if (!type)
4235 return lldb::eTypeClassInvalid;
4236
4237 clang::QualType qual_type(GetQualType(type));
4238
4239 switch (qual_type->getTypeClass()) {
4240 case clang::Type::UnaryTransform:
4241 break;
4242 case clang::Type::FunctionNoProto:
4243 return lldb::eTypeClassFunction;
4244 case clang::Type::FunctionProto:
4245 return lldb::eTypeClassFunction;
4246 case clang::Type::IncompleteArray:
4247 return lldb::eTypeClassArray;
4248 case clang::Type::VariableArray:
4249 return lldb::eTypeClassArray;
4250 case clang::Type::ConstantArray:
4251 return lldb::eTypeClassArray;
4252 case clang::Type::DependentSizedArray:
4253 return lldb::eTypeClassArray;
4254 case clang::Type::DependentSizedExtVector:
4255 return lldb::eTypeClassVector;
4256 case clang::Type::ExtVector:
4257 return lldb::eTypeClassVector;
4258 case clang::Type::Vector:
4259 return lldb::eTypeClassVector;
4260 case clang::Type::Builtin:
4261 return lldb::eTypeClassBuiltin;
4262 case clang::Type::ObjCObjectPointer:
4263 return lldb::eTypeClassObjCObjectPointer;
4264 case clang::Type::BlockPointer:
4265 return lldb::eTypeClassBlockPointer;
4266 case clang::Type::Pointer:
4267 return lldb::eTypeClassPointer;
4268 case clang::Type::LValueReference:
4269 return lldb::eTypeClassReference;
4270 case clang::Type::RValueReference:
4271 return lldb::eTypeClassReference;
4272 case clang::Type::MemberPointer:
4273 return lldb::eTypeClassMemberPointer;
4274 case clang::Type::Complex:
4275 if (qual_type->isComplexType())
4276 return lldb::eTypeClassComplexFloat;
4277 else
4278 return lldb::eTypeClassComplexInteger;
4279 case clang::Type::ObjCObject:
4280 return lldb::eTypeClassObjCObject;
4281 case clang::Type::ObjCInterface:
4282 return lldb::eTypeClassObjCInterface;
4283 case clang::Type::Record: {
4284 const clang::RecordType *record_type =
4285 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4286 const clang::RecordDecl *record_decl = record_type->getDecl();
4287 if (record_decl->isUnion())
4288 return lldb::eTypeClassUnion;
4289 else if (record_decl->isStruct())
4290 return lldb::eTypeClassStruct;
4291 else
4292 return lldb::eTypeClassClass;
4293 } break;
4294 case clang::Type::Enum:
4295 return lldb::eTypeClassEnumeration;
4296 case clang::Type::Typedef:
4297 return lldb::eTypeClassTypedef;
4298 case clang::Type::UnresolvedUsing:
4299 break;
4300 case clang::Type::Paren:
4301 return CompilerType(getASTContext(),
4302 llvm::cast<clang::ParenType>(qual_type)->desugar())
4303 .GetTypeClass();
4304 case clang::Type::Auto:
4305 return CompilerType(
4306 getASTContext(),
4307 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4308 .GetTypeClass();
4309 case clang::Type::Elaborated:
4310 return CompilerType(
4311 getASTContext(),
4312 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4313 .GetTypeClass();
4314
4315 case clang::Type::Attributed:
4316 break;
4317 case clang::Type::TemplateTypeParm:
4318 break;
4319 case clang::Type::SubstTemplateTypeParm:
4320 break;
4321 case clang::Type::SubstTemplateTypeParmPack:
4322 break;
4323 case clang::Type::InjectedClassName:
4324 break;
4325 case clang::Type::DependentName:
4326 break;
4327 case clang::Type::DependentTemplateSpecialization:
4328 break;
4329 case clang::Type::PackExpansion:
4330 break;
4331
4332 case clang::Type::TypeOfExpr:
4333 break;
4334 case clang::Type::TypeOf:
4335 break;
4336 case clang::Type::Decltype:
4337 break;
4338 case clang::Type::TemplateSpecialization:
4339 break;
4340 case clang::Type::DeducedTemplateSpecialization:
4341 break;
4342 case clang::Type::Atomic:
4343 break;
4344 case clang::Type::Pipe:
4345 break;
4346
4347 // pointer type decayed from an array or function type.
4348 case clang::Type::Decayed:
4349 break;
4350 case clang::Type::Adjusted:
4351 break;
4352 case clang::Type::ObjCTypeParam:
4353 break;
4354
4355 case clang::Type::DependentAddressSpace:
4356 break;
4357 }
4358 // We don't know hot to display this type...
4359 return lldb::eTypeClassOther;
4360}
4361
4362unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4363 if (type)
4364 return GetQualType(type).getQualifiers().getCVRQualifiers();
4365 return 0;
4366}
4367
4368//----------------------------------------------------------------------
4369// Creating related types
4370//----------------------------------------------------------------------
4371
4372CompilerType
4373ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4374 uint64_t *stride) {
4375 if (type) {
4376 clang::QualType qual_type(GetCanonicalQualType(type));
4377
4378 const clang::Type *array_eletype =
4379 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4380
4381 if (!array_eletype)
4382 return CompilerType();
4383
4384 CompilerType element_type(getASTContext(),
4385 array_eletype->getCanonicalTypeUnqualified());
4386
4387 // TODO: the real stride will be >= this value.. find the real one!
4388 if (stride)
4389 *stride = element_type.GetByteSize(nullptr);
4390
4391 return element_type;
4392 }
4393 return CompilerType();
4394}
4395
4396CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4397 uint64_t size) {
4398 if (type) {
4399 clang::QualType qual_type(GetCanonicalQualType(type));
4400 if (clang::ASTContext *ast_ctx = getASTContext()) {
4401 if (size != 0)
4402 return CompilerType(
4403 ast_ctx, ast_ctx->getConstantArrayType(
4404 qual_type, llvm::APInt(64, size),
4405 clang::ArrayType::ArraySizeModifier::Normal, 0));
4406 else
4407 return CompilerType(
4408 ast_ctx,
4409 ast_ctx->getIncompleteArrayType(
4410 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
4411 }
4412 }
4413
4414 return CompilerType();
4415}
4416
4417CompilerType
4418ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4419 if (type)
4420 return CompilerType(getASTContext(), GetCanonicalQualType(type));
4421 return CompilerType();
4422}
4423
4424static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4425 clang::QualType qual_type) {
4426 if (qual_type->isPointerType())
4427 qual_type = ast->getPointerType(
4428 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4429 else
4430 qual_type = qual_type.getUnqualifiedType();
4431 qual_type.removeLocalConst();
4432 qual_type.removeLocalRestrict();
4433 qual_type.removeLocalVolatile();
4434 return qual_type;
4435}
4436
4437CompilerType
4438ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4439 if (type)
4440 return CompilerType(
4441 getASTContext(),
4442 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4443 return CompilerType();
4444}
4445
4446int ClangASTContext::GetFunctionArgumentCount(
4447 lldb::opaque_compiler_type_t type) {
4448 if (type) {
4449 const clang::FunctionProtoType *func =
4450 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4451 if (func)
4452 return func->getNumParams();
4453 }
4454 return -1;
4455}
4456
4457CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4458 lldb::opaque_compiler_type_t type, size_t idx) {
4459 if (type) {
4460 const clang::FunctionProtoType *func =
4461 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4462 if (func) {
4463 const uint32_t num_args = func->getNumParams();
4464 if (idx < num_args)
4465 return CompilerType(getASTContext(), func->getParamType(idx));
4466 }
4467 }
4468 return CompilerType();
4469}
4470
4471CompilerType
4472ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4473 if (type) {
4474 clang::QualType qual_type(GetQualType(type));
4475 const clang::FunctionProtoType *func =
4476 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4477 if (func)
4478 return CompilerType(getASTContext(), func->getReturnType());
4479 }
4480 return CompilerType();
4481}
4482
4483size_t
4484ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4485 size_t num_functions = 0;
4486 if (type) {
4487 clang::QualType qual_type(GetCanonicalQualType(type));
4488 switch (qual_type->getTypeClass()) {
4489 case clang::Type::Record:
4490 if (GetCompleteQualType(getASTContext(), qual_type)) {
4491 const clang::RecordType *record_type =
4492 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4493 const clang::RecordDecl *record_decl = record_type->getDecl();
4494 assert(record_decl)(static_cast <bool> (record_decl) ? void (0) : __assert_fail
("record_decl", "/build/llvm-toolchain-snapshot-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 4494, __extension__ __PRETTY_FUNCTION__))
;
4495 const clang::CXXRecordDecl *cxx_record_decl =
4496 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4497 if (cxx_record_decl)
4498 num_functions = std::distance(cxx_record_decl->method_begin(),
4499 cxx_record_decl->method_end());
4500 }
4501 break;
4502
4503 case clang::Type::ObjCObjectPointer: {
4504 const clang::ObjCObjectPointerType *objc_class_type =
4505 qual_type->getAs<clang::ObjCObjectPointerType>();
4506 const clang::ObjCInterfaceType *objc_interface_type =
4507 objc_class_type->getInterfaceType();
4508 if (objc_interface_type &&
4509 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4510 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
4511 clang::ObjCInterfaceDecl *class_interface_decl =
4512 objc_interface_type->getDecl();
4513 if (class_interface_decl) {
4514 num_functions = std::distance(class_interface_decl->meth_begin(),
4515 class_interface_decl->meth_end());
4516 }
4517 }
4518 break;
4519 }
4520
4521 case clang::Type::ObjCObject:
4522 case clang::Type::ObjCInterface:
4523 if (GetCompleteType(type)) {
4524 const clang::ObjCObjectType *objc_class_type =
4525 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4526 if (objc_class_type) {
4527 clang::ObjCInterfaceDecl *class_interface_decl =
4528 objc_class_type->getInterface();
4529 if (class_interface_decl)
4530 num_functions = std::distance(class_interface_decl->meth_begin(),
4531 class_interface_decl->meth_end());
4532 }
4533 }
4534 break;
4535
4536 case clang::Type::Typedef:
4537 return CompilerType(getASTContext(),
4538 llvm::cast<clang::TypedefType>(qual_type)
4539 ->getDecl()
4540 ->getUnderlyingType())
4541 .GetNumMemberFunctions();
4542
4543 case clang::Type::Auto:
4544 return CompilerType(
4545 getASTContext(),
4546 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4547 .GetNumMemberFunctions();
4548
4549 case clang::Type::Elaborated:
4550 return CompilerType(
4551 getASTContext(),
4552 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4553 .GetNumMemberFunctions();
4554
4555 case clang::Type::Paren:
4556 return CompilerType(getASTContext(),
4557 llvm::cast<clang::ParenType>(qual_type)->desugar())
4558 .GetNumMemberFunctions();
4559
4560 default:
4561 break;
4562 }
4563 }
4564 return num_functions;
4565}
4566
4567TypeMemberFunctionImpl
4568ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4569 size_t idx) {
4570 std::string name;
4571 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4572 CompilerType clang_type;
4573 CompilerDecl clang_decl;
4574 if (type) {
4575 clang::QualType qual_type(GetCanonicalQualType(type));
4576 switch (qual_type->getTypeClass()) {
4577 case clang::Type::Record:
4578 if (GetCompleteQualType(getASTContext(), qual_type)) {
4579 const clang::RecordType *record_type =
4580 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4581 const clang::RecordDecl *record_decl = record_type->getDecl();
4582 assert(record_decl)(static_cast <bool> (record_decl) ? void (0) : __assert_fail
("record_decl", "/build/llvm-toolchain-snapshot-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 4582, __extension__ __PRETTY_FUNCTION__))
;
4583 const clang::CXXRecordDecl *cxx_record_decl =
4584 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4585 if (cxx_record_decl) {
4586 auto method_iter = cxx_record_decl->method_begin();
4587 auto method_end = cxx_record_decl->method_end();
4588 if (idx <
4589 static_cast<size_t>(std::distance(method_iter, method_end))) {
4590 std::advance(method_iter, idx);
4591 clang::CXXMethodDecl *cxx_method_decl =
4592 method_iter->getCanonicalDecl();
4593 if (cxx_method_decl) {
4594 name = cxx_method_decl->getDeclName().getAsString();
4595 if (cxx_method_decl->isStatic())
4596 kind = lldb::eMemberFunctionKindStaticMethod;
4597 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4598 kind = lldb::eMemberFunctionKindConstructor;
4599 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4600 kind = lldb::eMemberFunctionKindDestructor;
4601 else
4602 kind = lldb::eMemberFunctionKindInstanceMethod;
4603 clang_type = CompilerType(
4604 this, cxx_method_decl->getType().getAsOpaquePtr());
4605 clang_decl = CompilerDecl(this, cxx_method_decl);
4606 }
4607 }
4608 }
4609 }
4610 break;
4611
4612 case clang::Type::ObjCObjectPointer: {
4613 const clang::ObjCObjectPointerType *objc_class_type =
4614 qual_type->getAs<clang::ObjCObjectPointerType>();
4615 const clang::ObjCInterfaceType *objc_interface_type =
4616 objc_class_type->getInterfaceType();
4617 if (objc_interface_type &&
4618 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4619 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
4620 clang::ObjCInterfaceDecl *class_interface_decl =
4621 objc_interface_type->getDecl();
4622 if (class_interface_decl) {
4623 auto method_iter = class_interface_decl->meth_begin();
4624 auto method_end = class_interface_decl->meth_end();
4625 if (idx <
4626 static_cast<size_t>(std::distance(method_iter, method_end))) {
4627 std::advance(method_iter, idx);
4628 clang::ObjCMethodDecl *objc_method_decl =
4629 method_iter->getCanonicalDecl();
4630 if (objc_method_decl) {
4631 clang_decl = CompilerDecl(this, objc_method_decl);
4632 name = objc_method_decl->getSelector().getAsString();
4633 if (objc_method_decl->isClassMethod())
4634 kind = lldb::eMemberFunctionKindStaticMethod;
4635 else
4636 kind = lldb::eMemberFunctionKindInstanceMethod;
4637 }
4638 }
4639 }
4640 }
4641 break;
4642 }
4643
4644 case clang::Type::ObjCObject:
4645 case clang::Type::ObjCInterface:
4646 if (GetCompleteType(type)) {
4647 const clang::ObjCObjectType *objc_class_type =
4648 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4649 if (objc_class_type) {
4650 clang::ObjCInterfaceDecl *class_interface_decl =
4651 objc_class_type->getInterface();
4652 if (class_interface_decl) {
4653 auto method_iter = class_interface_decl->meth_begin();
4654 auto method_end = class_interface_decl->meth_end();
4655 if (idx <
4656 static_cast<size_t>(std::distance(method_iter, method_end))) {
4657 std::advance(method_iter, idx);
4658 clang::ObjCMethodDecl *objc_method_decl =
4659 method_iter->getCanonicalDecl();
4660 if (objc_method_decl) {
4661 clang_decl = CompilerDecl(this, objc_method_decl);
4662 name = objc_method_decl->getSelector().getAsString();
4663 if (objc_method_decl->isClassMethod())
4664 kind = lldb::eMemberFunctionKindStaticMethod;
4665 else
4666 kind = lldb::eMemberFunctionKindInstanceMethod;
4667 }
4668 }
4669 }
4670 }
4671 }
4672 break;
4673
4674 case clang::Type::Typedef:
4675 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4676 ->getDecl()
4677 ->getUnderlyingType()
4678 .getAsOpaquePtr(),
4679 idx);
4680
4681 case clang::Type::Auto:
4682 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4683 ->getDeducedType()
4684 .getAsOpaquePtr(),
4685 idx);
4686
4687 case clang::Type::Elaborated:
4688 return GetMemberFunctionAtIndex(
4689 llvm::cast<clang::ElaboratedType>(qual_type)
4690 ->getNamedType()
4691 .getAsOpaquePtr(),
4692 idx);
4693
4694 case clang::Type::Paren:
4695 return GetMemberFunctionAtIndex(
4696 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4697 idx);
4698
4699 default:
4700 break;
4701 }
4702 }
4703
4704 if (kind == eMemberFunctionKindUnknown)
4705 return TypeMemberFunctionImpl();
4706 else
4707 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
4708}
4709
4710CompilerType
4711ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4712 if (type)
4713 return CompilerType(getASTContext(),
4714 GetQualType(type).getNonReferenceType());
4715 return CompilerType();
4716}
4717
4718CompilerType ClangASTContext::CreateTypedefType(
4719 const CompilerType &type, const char *typedef_name,
4720 const CompilerDeclContext &compiler_decl_ctx) {
4721 if (type && typedef_name && typedef_name[0]) {
4722 ClangASTContext *ast =
4723 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4724 if (!ast)
4725 return CompilerType();
4726 clang::ASTContext *clang_ast = ast->getASTContext();
4727 clang::QualType qual_type(ClangUtil::GetQualType(type));
4728
4729 clang::DeclContext *decl_ctx =
4730 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4731 if (decl_ctx == nullptr)
4732 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4733
4734 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4735 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4736 &clang_ast->Idents.get(typedef_name),
4737 clang_ast->getTrivialTypeSourceInfo(qual_type));
4738
4739 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4740
4741 // Get a uniqued clang::QualType for the typedef decl type
4742 return CompilerType(clang_ast, clang_ast->getTypedefType(decl));
4743 }
4744 return CompilerType();
4745}
4746
4747CompilerType
4748ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4749 if (type) {
4750 clang::QualType qual_type(GetQualType(type));
4751 return CompilerType(getASTContext(),
4752 qual_type.getTypePtr()->getPointeeType());
4753 }
4754 return CompilerType();
4755}
4756
4757CompilerType
4758ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4759 if (type) {
4760 clang::QualType qual_type(GetQualType(type));
4761
4762 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4763 switch (type_class) {
4764 case clang::Type::ObjCObject:
4765 case clang::Type::ObjCInterface:
4766 return CompilerType(getASTContext(),
4767 getASTContext()->getObjCObjectPointerType(qual_type));
4768
4769 default:
4770 return CompilerType(getASTContext(),
4771 getASTContext()->getPointerType(qual_type));
4772 }
4773 }
4774 return CompilerType();
4775}
4776
4777CompilerType
4778ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4779 if (type)
4780 return CompilerType(this, getASTContext()
4781 ->getLValueReferenceType(GetQualType(type))
4782 .getAsOpaquePtr());
4783 else
4784 return CompilerType();
4785}
4786
4787CompilerType
4788ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4789 if (type)
4790 return CompilerType(this, getASTContext()
4791 ->getRValueReferenceType(GetQualType(type))
4792 .getAsOpaquePtr());
4793 else
4794 return CompilerType();
4795}
4796
4797CompilerType
4798ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4799 if (type) {
4800 clang::QualType result(GetQualType(type));
4801 result.addConst();
4802 return CompilerType(this, result.getAsOpaquePtr());
4803 }
4804 return CompilerType();
4805}
4806
4807CompilerType
4808ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4809 if (type) {
4810 clang::QualType result(GetQualType(type));
4811 result.addVolatile();
4812 return CompilerType(this, result.getAsOpaquePtr());
4813 }
4814 return CompilerType();
4815}
4816
4817CompilerType
4818ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4819 if (type) {
4820 clang::QualType result(GetQualType(type));
4821 result.addRestrict();
4822 return CompilerType(this, result.getAsOpaquePtr());
4823 }
4824 return CompilerType();
4825}
4826
4827CompilerType
4828ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4829 const char *typedef_name,
4830 const CompilerDeclContext &compiler_decl_ctx) {
4831 if (type) {
4832 clang::ASTContext *clang_ast = getASTContext();
4833 clang::QualType qual_type(GetQualType(type));
4834
4835 clang::DeclContext *decl_ctx =
4836 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4837 if (decl_ctx == nullptr)
4838 decl_ctx = getASTContext()->getTranslationUnitDecl();
4839
4840 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4841 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4842 &clang_ast->Idents.get(typedef_name),
4843 clang_ast->getTrivialTypeSourceInfo(qual_type));
4844
4845 clang::TagDecl *tdecl = nullptr;
4846 if (!qual_type.isNull()) {
4847 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4848 tdecl = rt->getDecl();
4849 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4850 tdecl = et->getDecl();
4851 }
4852
4853 // Check whether this declaration is an anonymous struct, union, or enum,
4854 // hidden behind a typedef. If so, we
4855 // try to check whether we have a typedef tag to attach to the original
4856 // record declaration
4857 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4858 tdecl->setTypedefNameForAnonDecl(decl);
4859
4860 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4861
4862 // Get a uniqued clang::QualType for the typedef decl type
4863 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4864 }
4865 return CompilerType();
4866}
4867
4868CompilerType
4869ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4870 if (type) {
4871 const clang::TypedefType *typedef_type =
4872 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4873 if (typedef_type)
4874 return CompilerType(getASTContext(),
4875 typedef_type->getDecl()->getUnderlyingType());
4876 }
4877 return CompilerType();
4878}
4879
4880//----------------------------------------------------------------------
4881// Create related types using the current type's AST
4882//----------------------------------------------------------------------
4883
4884CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
4885 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
4886}
4887//----------------------------------------------------------------------
4888// Exploring the type
4889//----------------------------------------------------------------------
4890
4891uint64_t ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
4892 ExecutionContextScope *exe_scope) {
4893 if (GetCompleteType(type)) {
4894 clang::QualType qual_type(GetCanonicalQualType(type));
4895 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4896 switch (type_class) {
4897 case clang::Type::Record:
4898 if (GetCompleteType(type))
4899 return getASTContext()->getTypeSize(qual_type);
4900 else
4901 return 0;
4902 break;
4903
4904 case clang::Type::ObjCInterface:
4905 case clang::Type::ObjCObject: {
4906 ExecutionContext exe_ctx(exe_scope);
4907 Process *process = exe_ctx.GetProcessPtr();
4908 if (process) {
4909 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4910 if (objc_runtime) {
4911 uint64_t bit_size = 0;
4912 if (objc_runtime->GetTypeBitSize(
4913 CompilerType(getASTContext(), qual_type), bit_size))
4914 return bit_size;
4915 }
4916 } else {
4917 static bool g_printed = false;
4918 if (!g_printed) {
4919 StreamString s;
4920 DumpTypeDescription(type, &s);
4921
4922 llvm::outs() << "warning: trying to determine the size of type ";
4923 llvm::outs() << s.GetString() << "\n";
4924 llvm::outs() << "without a valid ExecutionContext. this is not "
4925 "reliable. please file a bug against LLDB.\n";
4926 llvm::outs() << "backtrace:\n";
4927 llvm::sys::PrintStackTrace(llvm::outs());
4928 llvm::outs() << "\n";
4929 g_printed = true;
4930 }
4931 }
4932 }
4933 LLVM_FALLTHROUGH[[clang::fallthrough]];
4934 default:
4935 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
4936 if (bit_size == 0) {
4937 if (qual_type->isIncompleteArrayType())
4938 return getASTContext()->getTypeSize(
4939 qual_type->getArrayElementTypeNoTypeQual()
4940 ->getCanonicalTypeUnqualified());
4941 }
4942 if (qual_type->isObjCObjectOrInterfaceType())
4943 return bit_size +
4944 getASTContext()->getTypeSize(
4945 getASTContext()->ObjCBuiltinClassTy);
4946 return bit_size;
4947 }
4948 }
4949 return 0;
4950}
4951
4952size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
4953 if (GetCompleteType(type))
4954 return getASTContext()->getTypeAlign(GetQualType(type));
4955 return 0;
4956}
4957
4958lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
4959 uint64_t &count) {
4960 if (!type)
4961 return lldb::eEncodingInvalid;
4962
4963 count = 1;
4964 clang::QualType qual_type(GetCanonicalQualType(type));
4965
4966 switch (qual_type->getTypeClass()) {
4967 case clang::Type::UnaryTransform:
4968 break;
4969
4970 case clang::Type::FunctionNoProto:
4971 case clang::Type::FunctionProto:
4972 break;
4973
4974 case clang::Type::IncompleteArray:
4975 case clang::Type::VariableArray:
4976 break;
4977
4978 case clang::Type::ConstantArray:
4979 break;
4980
4981 case clang::Type::ExtVector:
4982 case clang::Type::Vector:
4983 // TODO: Set this to more than one???
4984 break;
4985
4986 case clang::Type::Builtin:
4987 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4988 case clang::BuiltinType::Void:
4989 break;
4990
4991 case clang::BuiltinType::Bool:
4992 case clang::BuiltinType::Char_S:
4993 case clang::BuiltinType::SChar:
4994 case clang::BuiltinType::WChar_S:
4995 case clang::BuiltinType::Char16:
4996 case clang::BuiltinType::Char32:
4997 case clang::BuiltinType::Short:
4998 case clang::BuiltinType::Int:
4999 case clang::BuiltinType::Long:
5000 case clang::BuiltinType::LongLong:
5001 case clang::BuiltinType::Int128:
5002 return lldb::eEncodingSint;
5003
5004 case clang::BuiltinType::Char_U:
5005 case clang::BuiltinType::UChar:
5006 case clang::BuiltinType::WChar_U:
5007 case clang::BuiltinType::UShort:
5008 case clang::BuiltinType::UInt:
5009 case clang::BuiltinType::ULong:
5010 case clang::BuiltinType::ULongLong:
5011 case clang::BuiltinType::UInt128:
5012 return lldb::eEncodingUint;
5013
5014 case clang::BuiltinType::Half:
5015 case clang::BuiltinType::Float:
5016 case clang::BuiltinType::Float16:
5017 case clang::BuiltinType::Float128:
5018 case clang::BuiltinType::Double:
5019 case clang::BuiltinType::LongDouble:
5020 return lldb::eEncodingIEEE754;
5021
5022 case clang::BuiltinType::ObjCClass:
5023 case clang::BuiltinType::ObjCId:
5024 case clang::BuiltinType::ObjCSel:
5025 return lldb::eEncodingUint;
5026
5027 case clang::BuiltinType::NullPtr:
5028 return lldb::eEncodingUint;
5029
5030 case clang::BuiltinType::Kind::ARCUnbridgedCast:
5031 case clang::BuiltinType::Kind::BoundMember:
5032 case clang::BuiltinType::Kind::BuiltinFn:
5033 case clang::BuiltinType::Kind::Dependent:
5034 case clang::BuiltinType::Kind::OCLClkEvent:
5035 case clang::BuiltinType::Kind::OCLEvent:
5036 case clang::BuiltinType::Kind::OCLImage1dRO:
5037 case clang::BuiltinType::Kind::OCLImage1dWO:
5038 case clang::BuiltinType::Kind::OCLImage1dRW:
5039 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
5040 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
5041 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
5042 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
5043 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
5044 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
5045 case clang::BuiltinType::Kind::OCLImage2dRO:
5046 case clang::BuiltinType::Kind::OCLImage2dWO:
5047 case clang::BuiltinType::Kind::OCLImage2dRW:
5048 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
5049 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
5050 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
5051 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
5052 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
5053 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
5054 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5055 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5056 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5057 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5058 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5059 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5060 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5061 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5062 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5063 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5064 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5065 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5066 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5067 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5068 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5069 case clang::BuiltinType::Kind::OCLImage3dRO:
5070 case clang::BuiltinType::Kind::OCLImage3dWO:
5071 case clang::BuiltinType::Kind::OCLImage3dRW:
5072 case clang::BuiltinType::Kind::OCLQueue:
5073 case clang::BuiltinType::Kind::OCLReserveID:
5074 case clang::BuiltinType::Kind::OCLSampler:
5075 case clang::BuiltinType::Kind::OMPArraySection:
5076 case clang::BuiltinType::Kind::Overload:
5077 case clang::BuiltinType::Kind::PseudoObject:
5078 case clang::BuiltinType::Kind::UnknownAny:
5079 break;
5080 }
5081 break;
5082 // All pointer types are represented as unsigned integer encodings.
5083 // We may nee to add a eEncodingPointer if we ever need to know the
5084 // difference
5085 case clang::Type::ObjCObjectPointer:
5086 case clang::Type::BlockPointer:
5087 case clang::Type::Pointer:
5088 case clang::Type::LValueReference:
5089 case clang::Type::RValueReference:
5090 case clang::Type::MemberPointer:
5091 return lldb::eEncodingUint;
5092 case clang::Type::Complex: {
5093 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5094 if (qual_type->isComplexType())
5095 encoding = lldb::eEncodingIEEE754;
5096 else {
5097 const clang::ComplexType *complex_type =
5098 qual_type->getAsComplexIntegerType();
5099 if (complex_type)
5100 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5101 .GetEncoding(count);
5102 else
5103 encoding = lldb::eEncodingSint;
5104 }
5105 count = 2;
5106 return encoding;
5107 }
5108
5109 case clang::Type::ObjCInterface:
5110 break;
5111 case clang::Type::Record:
5112 break;
5113 case clang::Type::Enum:
5114 return lldb::eEncodingSint;
5115 case clang::Type::Typedef:
5116 return CompilerType(getASTContext(),
5117 llvm::cast<clang::TypedefType>(qual_type)
5118 ->getDecl()
5119 ->getUnderlyingType())
5120 .GetEncoding(count);
5121
5122 case clang::Type::Auto:
5123 return CompilerType(
5124 getASTContext(),
5125 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5126 .GetEncoding(count);
5127
5128 case clang::Type::Elaborated:
5129 return CompilerType(
5130 getASTContext(),
5131 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5132 .GetEncoding(count);
5133
5134 case clang::Type::Paren:
5135 return CompilerType(getASTContext(),
5136 llvm::cast<clang::ParenType>(qual_type)->desugar())
5137 .GetEncoding(count);
5138
5139 case clang::Type::DependentSizedArray:
5140 case clang::Type::DependentSizedExtVector:
5141 case clang::Type::UnresolvedUsing:
5142 case clang::Type::Attributed:
5143 case clang::Type::TemplateTypeParm:
5144 case clang::Type::SubstTemplateTypeParm:
5145 case clang::Type::SubstTemplateTypeParmPack:
5146 case clang::Type::InjectedClassName:
5147 case clang::Type::DependentName:
5148 case clang::Type::DependentTemplateSpecialization:
5149 case clang::Type::PackExpansion:
5150 case clang::Type::ObjCObject:
5151
5152 case clang::Type::TypeOfExpr:
5153 case clang::Type::TypeOf:
5154 case clang::Type::Decltype:
5155 case clang::Type::TemplateSpecialization:
5156 case clang::Type::DeducedTemplateSpecialization:
5157 case clang::Type::Atomic:
5158 case clang::Type::Adjusted:
5159 case clang::Type::Pipe:
5160 break;
5161
5162 // pointer type decayed from an array or function type.
5163 case clang::Type::Decayed:
5164 break;
5165 case clang::Type::ObjCTypeParam:
5166 break;
5167
5168 case clang::Type::DependentAddressSpace:
5169 break;
5170 }
5171 count = 0;
5172 return lldb::eEncodingInvalid;
5173}
5174
5175lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5176 if (!type)
5177 return lldb::eFormatDefault;
5178
5179 clang::QualType qual_type(GetCanonicalQualType(type));
5180
5181 switch (qual_type->getTypeClass()) {
5182 case clang::Type::UnaryTransform:
5183 break;
5184
5185 case clang::Type::FunctionNoProto:
5186 case clang::Type::FunctionProto:
5187 break;
5188
5189 case clang::Type::IncompleteArray:
5190 case clang::Type::VariableArray:
5191 break;
5192
5193 case clang::Type::ConstantArray:
5194 return lldb::eFormatVoid; // no value
5195
5196 case clang::Type::ExtVector:
5197 case clang::Type::Vector:
5198 break;
5199
5200 case clang::Type::Builtin:
5201 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5202 // default: assert(0 && "Unknown builtin type!");
5203 case clang::BuiltinType::UnknownAny:
5204 case clang::BuiltinType::Void:
5205 case clang::BuiltinType::BoundMember:
5206 break;
5207
5208 case clang::BuiltinType::Bool:
5209 return lldb::eFormatBoolean;
5210 case clang::BuiltinType::Char_S:
5211 case clang::BuiltinType::SChar:
5212 case clang::BuiltinType::WChar_S:
5213 case clang::BuiltinType::Char_U:
5214 case clang::BuiltinType::UChar:
5215 case clang::BuiltinType::WChar_U:
5216 return lldb::eFormatChar;
5217 case clang::BuiltinType::Char16:
5218 return lldb::eFormatUnicode16;
5219 case clang::BuiltinType::Char32:
5220 return lldb::eFormatUnicode32;
5221 case clang::BuiltinType::UShort:
5222 return lldb::eFormatUnsigned;
5223 case clang::BuiltinType::Short:
5224 return lldb::eFormatDecimal;
5225 case clang::BuiltinType::UInt:
5226 return lldb::eFormatUnsigned;
5227 case clang::BuiltinType::Int:
5228 return lldb::eFormatDecimal;
5229 case clang::BuiltinType::ULong:
5230 return lldb::eFormatUnsigned;
5231 case clang::BuiltinType::Long:
5232 return lldb::eFormatDecimal;
5233 case clang::BuiltinType::ULongLong:
5234 return lldb::eFormatUnsigned;
5235 case clang::BuiltinType::LongLong:
5236 return lldb::eFormatDecimal;
5237 case clang::BuiltinType::UInt128:
5238 return lldb::eFormatUnsigned;
5239 case clang::BuiltinType::Int128:
5240 return lldb::eFormatDecimal;
5241 case clang::BuiltinType::Half:
5242 case clang::BuiltinType::Float:
5243 case clang::BuiltinType::Double:
5244 case clang::BuiltinType::LongDouble:
5245 return lldb::eFormatFloat;
5246 default:
5247 return lldb::eFormatHex;
5248 }
5249 break;
5250 case clang::Type::ObjCObjectPointer:
5251 return lldb::eFormatHex;
5252 case clang::Type::BlockPointer:
5253 return lldb::eFormatHex;
5254 case clang::Type::Pointer:
5255 return lldb::eFormatHex;
5256 case clang::Type::LValueReference:
5257 case clang::Type::RValueReference:
5258 return lldb::eFormatHex;
5259 case clang::Type::MemberPointer:
5260 break;
5261 case clang::Type::Complex: {
5262 if (qual_type->isComplexType())
5263 return lldb::eFormatComplex;
5264 else
5265 return lldb::eFormatComplexInteger;
5266 }
5267 case clang::Type::ObjCInterface:
5268 break;
5269 case clang::Type::Record:
5270 break;
5271 case clang::Type::Enum:
5272 return lldb::eFormatEnum;
5273 case clang::Type::Typedef:
5274 return CompilerType(getASTContext(),
5275 llvm::cast<clang::TypedefType>(qual_type)
5276 ->getDecl()
5277 ->getUnderlyingType())
5278 .GetFormat();
5279 case clang::Type::Auto:
5280 return CompilerType(getASTContext(),
5281 llvm::cast<clang::AutoType>(qual_type)->desugar())
5282 .GetFormat();
5283 case clang::Type::Paren:
5284 return CompilerType(getASTContext(),
5285 llvm::cast<clang::ParenType>(qual_type)->desugar())
5286 .GetFormat();
5287 case clang::Type::Elaborated:
5288 return CompilerType(
5289 getASTContext(),
5290 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5291 .GetFormat();
5292 case clang::Type::DependentSizedArray:
5293 case clang::Type::DependentSizedExtVector:
5294 case clang::Type::UnresolvedUsing:
5295 case clang::Type::Attributed:
5296 case clang::Type::TemplateTypeParm:
5297 case clang::Type::SubstTemplateTypeParm:
5298 case clang::Type::SubstTemplateTypeParmPack:
5299 case clang::Type::InjectedClassName:
5300 case clang::Type::DependentName:
5301 case clang::Type::DependentTemplateSpecialization:
5302 case clang::Type::PackExpansion:
5303 case clang::Type::ObjCObject:
5304
5305 case clang::Type::TypeOfExpr:
5306 case clang::Type::TypeOf:
5307 case clang::Type::Decltype:
5308 case clang::Type::TemplateSpecialization:
5309 case clang::Type::DeducedTemplateSpecialization:
5310 case clang::Type::Atomic:
5311 case clang::Type::Adjusted:
5312 case clang::Type::Pipe:
5313 break;
5314
5315 // pointer type decayed from an array or function type.
5316 case clang::Type::Decayed:
5317 break;
5318 case clang::Type::ObjCTypeParam:
5319 break;
5320
5321 case clang::Type::DependentAddressSpace:
5322 break;
5323 }
5324 // We don't know hot to display this type...
5325 return lldb::eFormatBytes;
5326}
5327
5328static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5329 bool check_superclass) {
5330 while (class_interface_decl) {
5331 if (class_interface_decl->ivar_size() > 0)
5332 return true;
5333
5334 if (check_superclass)
5335 class_interface_decl = class_interface_decl->getSuperClass();
5336 else
5337 break;
5338 }
5339 return false;
5340}
5341
5342uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
5343 bool omit_empty_base_classes) {
5344 if (!type)
5345 return 0;
5346
5347 uint32_t num_children = 0;
5348 clang::QualType qual_type(GetQualType(type));
5349 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5350 switch (type_class) {
5351 case clang::Type::Builtin:
5352 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5353 case clang::BuiltinType::ObjCId: // child is Class
5354 case clang::BuiltinType::ObjCClass: // child is Class
5355 num_children = 1;
5356 break;
5357
5358 default:
5359 break;
5360 }
5361 break;
5362
5363 case clang::Type::Complex:
5364 return 0;
5365
5366 case clang::Type::Record:
5367 if (GetCompleteQualType(getASTContext(), qual_type)) {
5368 const clang::RecordType *record_type =
5369 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5370 const clang::RecordDecl *record_decl = record_type->getDecl();
5371 assert(record_decl)(static_cast <bool> (record_decl) ? void (0) : __assert_fail
("record_decl", "/build/llvm-toolchain-snapshot-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 5371, __extension__ __PRETTY_FUNCTION__))
;
5372 const clang::CXXRecordDecl *cxx_record_decl =
5373 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5374 if (cxx_record_decl) {
5375 if (omit_empty_base_classes) {
5376 // Check each base classes to see if it or any of its
5377 // base classes contain any fields. This can help
5378 // limit the noise in variable views by not having to
5379 // show base classes that contain no members.
5380 clang::CXXRecordDecl::base_class_const_iterator base_class,
5381 base_class_end;
5382 for (base_class = cxx_record_decl->bases_begin(),
5383 base_class_end = cxx_record_decl->bases_end();
5384 base_class != base_class_end; ++base_class) {
5385 const clang::CXXRecordDecl *base_class_decl =
5386 llvm::cast<clang::CXXRecordDecl>(
5387 base_class->getType()
5388 ->getAs<clang::RecordType>()
5389 ->getDecl());
5390
5391 // Skip empty base classes
5392 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5393 continue;
5394
5395 num_children++;
5396 }
5397 } else {
5398 // Include all base classes
5399 num_children += cxx_record_decl->getNumBases();
5400 }
5401 }
5402 clang::RecordDecl::field_iterator field, field_end;
5403 for (field = record_decl->field_begin(),
5404 field_end = record_decl->field_end();
5405 field != field_end; ++field)
5406 ++num_children;
5407 }
5408 break;
5409
5410 case clang::Type::ObjCObject:
5411 case clang::Type::ObjCInterface:
5412 if (GetCompleteQualType(getASTContext(), qual_type)) {
5413 const clang::ObjCObjectType *objc_class_type =
5414 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5415 assert(objc_class_type)(static_cast <bool> (objc_class_type) ? void (0) : __assert_fail
("objc_class_type", "/build/llvm-toolchain-snapshot-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 5415, __extension__ __PRETTY_FUNCTION__))
;
5416 if (objc_class_type) {
5417 clang::ObjCInterfaceDecl *class_interface_decl =
5418 objc_class_type->getInterface();
5419
5420 if (class_interface_decl) {
5421
5422 clang::ObjCInterfaceDecl *superclass_interface_decl =
5423 class_interface_decl->getSuperClass();
5424 if (superclass_interface_decl) {
5425 if (omit_empty_base_classes) {
5426 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5427 ++num_children;
5428 } else
5429 ++num_children;
5430 }
5431
5432 num_children += class_interface_decl->ivar_size();
5433 }
5434 }
5435 }
5436 break;
5437
5438 case clang::Type::ObjCObjectPointer: {
5439 const clang::ObjCObjectPointerType *pointer_type =
5440 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5441 clang::QualType pointee_type = pointer_type->getPointeeType();
5442 uint32_t num_pointee_children =
5443 CompilerType(getASTContext(), pointee_type)
5444 .GetNumChildren(omit_empty_base_classes);
5445 // If this type points to a simple type, then it has 1 child
5446 if (num_pointee_children == 0)
5447 num_children = 1;
5448 else
5449 num_children = num_pointee_children;
5450 } break;
5451
5452 case clang::Type::Vector:
5453 case clang::Type::ExtVector:
5454 num_children =
5455 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5456 break;
5457
5458 case clang::Type::ConstantArray:
5459 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5460 ->getSize()
5461 .getLimitedValue();
5462 break;
5463
5464 case clang::Type::Pointer: {
5465 const clang::PointerType *pointer_type =
5466 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5467 clang::QualType pointee_type(pointer_type->getPointeeType());
5468 uint32_t num_pointee_children =
5469 CompilerType(getASTContext(), pointee_type)
5470 .GetNumChildren(omit_empty_base_classes);
5471 if (num_pointee_children == 0) {
5472 // We have a pointer to a pointee type that claims it has no children.
5473 // We will want to look at
5474 num_children = GetNumPointeeChildren(pointee_type);
5475 } else
5476 num_children = num_pointee_children;
5477 } break;
5478
5479 case clang::Type::LValueReference:
5480 case clang::Type::RValueReference: {
5481 const clang::ReferenceType *reference_type =
5482 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5483 clang::QualType pointee_type = reference_type->getPointeeType();
5484 uint32_t num_pointee_children =
5485 CompilerType(getASTContext(), pointee_type)
5486 .GetNumChildren(omit_empty_base_classes);
5487 // If this type points to a simple type, then it has 1 child
5488 if (num_pointee_children == 0)
5489 num_children = 1;
5490 else
5491 num_children = num_pointee_children;
5492 } break;
5493
5494 case clang::Type::Typedef:
5495 num_children =
5496 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5497 ->getDecl()
5498 ->getUnderlyingType())
5499 .GetNumChildren(omit_empty_base_classes);
5500 break;
5501
5502 case clang::Type::Auto:
5503 num_children =
5504 CompilerType(getASTContext(),
5505 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5506 .GetNumChildren(omit_empty_base_classes);
5507 break;
5508
5509 case clang::Type::Elaborated:
5510 num_children =
5511 CompilerType(
5512 getASTContext(),
5513 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5514 .GetNumChildren(omit_empty_base_classes);
5515 break;
5516
5517 case clang::Type::Paren:
5518 num_children =
5519 CompilerType(getASTContext(),
5520 llvm::cast<clang::ParenType>(qual_type)->desugar())
5521 .GetNumChildren(omit_empty_base_classes);
5522 break;
5523 default:
5524 break;
5525 }
5526 return num_children;
5527}
5528
5529CompilerType ClangASTContext::GetBuiltinTypeByName(const ConstString &name) {
5530 return GetBasicType(GetBasicTypeEnumeration(name));
5531}
5532
5533lldb::BasicType
5534ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5535 if (type) {
5536 clang::QualType qual_type(GetQualType(type));
5537 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5538 if (type_class == clang::Type::Builtin) {
5539 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5540 case clang::BuiltinType::Void:
5541 return eBasicTypeVoid;
5542 case clang::BuiltinType::Bool:
5543 return eBasicTypeBool;
5544 case clang::BuiltinType::Char_S:
5545 return eBasicTypeSignedChar;
5546 case clang::BuiltinType::Char_U:
5547 return eBasicTypeUnsignedChar;
5548 case clang::BuiltinType::Char16:
5549 return eBasicTypeChar16;
5550 case clang::BuiltinType::Char32:
5551 return eBasicTypeChar32;
5552 case clang::BuiltinType::UChar:
5553 return eBasicTypeUnsignedChar;
5554 case clang::BuiltinType::SChar:
5555 return eBasicTypeSignedChar;
5556 case clang::BuiltinType::WChar_S:
5557 return eBasicTypeSignedWChar;
5558 case clang::BuiltinType::WChar_U:
5559 return eBasicTypeUnsignedWChar;
5560 case clang::BuiltinType::Short:
5561 return eBasicTypeShort;
5562 case clang::BuiltinType::UShort:
5563 return eBasicTypeUnsignedShort;
5564 case clang::BuiltinType::Int:
5565 return eBasicTypeInt;
5566 case clang::BuiltinType::UInt:
5567 return eBasicTypeUnsignedInt;
5568 case clang::BuiltinType::Long:
5569 return eBasicTypeLong;
5570 case clang::BuiltinType::ULong:
5571 return eBasicTypeUnsignedLong;
5572 case clang::BuiltinType::LongLong:
5573 return eBasicTypeLongLong;
5574 case clang::BuiltinType::ULongLong:
5575 return eBasicTypeUnsignedLongLong;
5576 case clang::BuiltinType::Int128:
5577 return eBasicTypeInt128;
5578 case clang::BuiltinType::UInt128:
5579 return eBasicTypeUnsignedInt128;
5580
5581 case clang::BuiltinType::Half:
5582 return eBasicTypeHalf;
5583 case clang::BuiltinType::Float:
5584 return eBasicTypeFloat;
5585 case clang::BuiltinType::Double:
5586 return eBasicTypeDouble;
5587 case clang::BuiltinType::LongDouble:
5588 return eBasicTypeLongDouble;
5589
5590 case clang::BuiltinType::NullPtr:
5591 return eBasicTypeNullPtr;
5592 case clang::BuiltinType::ObjCId:
5593 return eBasicTypeObjCID;
5594 case clang::BuiltinType::ObjCClass:
5595 return eBasicTypeObjCClass;
5596 case clang::BuiltinType::ObjCSel:
5597 return eBasicTypeObjCSel;
5598 default:
5599 return eBasicTypeOther;
5600 }
5601 }
5602 }
5603 return eBasicTypeInvalid;
5604}
5605
5606void ClangASTContext::ForEachEnumerator(
5607 lldb::opaque_compiler_type_t type,
5608 std::function<bool(const CompilerType &integer_type,
5609 const ConstString &name,
5610 const llvm::APSInt &value)> const &callback) {
5611 const clang::EnumType *enum_type =
5612 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5613 if (enum_type) {
5614 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5615 if (enum_decl) {
5616 CompilerType integer_type(this,
5617 enum_decl->getIntegerType().getAsOpaquePtr());
5618
5619 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5620 for (enum_pos = enum_decl->enumerator_begin(),
5621 enum_end_pos = enum_decl->enumerator_end();
5622 enum_pos != enum_end_pos; ++enum_pos) {
5623 ConstString name(enum_pos->getNameAsString().c_str());
5624 if (!callback(integer_type, name, enum_pos->getInitVal()))
5625 break;
5626 }
5627 }
5628 }
5629}
5630
5631#pragma mark Aggregate Types
5632
5633uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5634 if (!type)
5635 return 0;
5636
5637 uint32_t count = 0;
5638 clang::QualType qual_type(GetCanonicalQualType(type));
5639 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5640 switch (type_class) {
5641 case clang::Type::Record:
5642 if (GetCompleteType(type)) {
5643 const clang::RecordType *record_type =
5644 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5645 if (record_type) {
5646 clang::RecordDecl *record_decl = record_type->getDecl();
5647 if (record_decl) {
5648 uint32_t field_idx = 0;
5649 clang::RecordDecl::field_iterator field, field_end;
5650 for (field = record_decl->field_begin(),
5651 field_end = record_decl->field_end();
5652 field != field_end; ++field)
5653 ++field_idx;
5654 count = field_idx;
5655 }
5656 }
5657 }
5658 break;
5659
5660 case clang::Type::Typedef:
5661 count =
5662 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5663 ->getDecl()
5664 ->getUnderlyingType())
5665 .GetNumFields();
5666 break;
5667
5668 case clang::Type::Auto:
5669 count =
5670 CompilerType(getASTContext(),
5671 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5672 .GetNumFields();
5673 break;
5674
5675 case clang::Type::Elaborated:
5676 count = CompilerType(
5677 getASTContext(),
5678 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5679 .GetNumFields();
5680 break;
5681
5682 case clang::Type::Paren:
5683 count = CompilerType(getASTContext(),
5684 llvm::cast<clang::ParenType>(qual_type)->desugar())
5685 .GetNumFields();
5686 break;
5687
5688 case clang::Type::ObjCObjectPointer: {
5689 const clang::ObjCObjectPointerType *objc_class_type =
5690 qual_type->getAs<clang::ObjCObjectPointerType>();
5691 const clang::ObjCInterfaceType *objc_interface_type =
5692 objc_class_type->getInterfaceType();
5693 if (objc_interface_type &&
5694 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5695 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
5696 clang::ObjCInterfaceDecl *class_interface_decl =
5697 objc_interface_type->getDecl();
5698 if (class_interface_decl) {
5699 count = class_interface_decl->ivar_size();
5700 }
5701 }
5702 break;
5703 }
5704
5705 case clang::Type::ObjCObject:
5706 case clang::Type::ObjCInterface:
5707 if (GetCompleteType(type)) {
5708 const clang::ObjCObjectType *objc_class_type =
5709 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5710 if (objc_class_type) {
5711 clang::ObjCInterfaceDecl *class_interface_decl =
5712 objc_class_type->getInterface();
5713
5714 if (class_interface_decl)
5715 count = class_interface_decl->ivar_size();
5716 }
5717 }
5718 break;
5719
5720 default:
5721 break;
5722 }
5723 return count;
5724}
5725
5726static lldb::opaque_compiler_type_t
5727GetObjCFieldAtIndex(clang::ASTContext *ast,
5728 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5729 std::string &name, uint64_t *bit_offset_ptr,
5730 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5731 if (class_interface_decl) {
5732 if (idx < (class_interface_decl->ivar_size())) {
5733 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5734 ivar_end = class_interface_decl->ivar_end();
5735 uint32_t ivar_idx = 0;
5736
5737 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5738 ++ivar_pos, ++ivar_idx) {
5739 if (ivar_idx == idx) {
5740 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5741
5742 clang::QualType ivar_qual_type(ivar_decl->getType());
5743
5744 name.assign(ivar_decl->getNameAsString());
5745
5746 if (bit_offset_ptr) {
5747 const clang::ASTRecordLayout &interface_layout =
5748 ast->getASTObjCInterfaceLayout(class_interface_decl);
5749 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5750 }
5751
5752 const bool is_bitfield = ivar_pos->isBitField();
5753
5754 if (bitfield_bit_size_ptr) {
5755 *bitfield_bit_size_ptr = 0;
5756
5757 if (is_bitfield && ast) {
5758 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5759 llvm::APSInt bitfield_apsint;
5760 if (bitfield_bit_size_expr &&
5761 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5762 *ast)) {
5763 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5764 }
5765 }
5766 }
5767 if (is_bitfield_ptr)
5768 *is_bitfield_ptr = is_bitfield;
5769
5770 return ivar_qual_type.getAsOpaquePtr();
5771 }
5772 }
5773 }
5774 }
5775 return nullptr;
5776}
5777
5778CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5779 size_t idx, std::string &name,
5780 uint64_t *bit_offset_ptr,
5781 uint32_t *bitfield_bit_size_ptr,
5782 bool *is_bitfield_ptr) {
5783 if (!type)
5784 return CompilerType();
5785
5786 clang::QualType qual_type(GetCanonicalQualType(type));
5787 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5788 switch (type_class) {
5789 case clang::Type::Record:
5790 if (GetCompleteType(type)) {
5791 const clang::RecordType *record_type =
5792 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5793 const clang::RecordDecl *record_decl = record_type->getDecl();
5794 uint32_t field_idx = 0;
5795 clang::RecordDecl::field_iterator field, field_end;
5796 for (field = record_decl->field_begin(),
5797 field_end = record_decl->field_end();
5798 field != field_end; ++field, ++field_idx) {
5799 if (idx == field_idx) {
5800 // Print the member type if requested
5801 // Print the member name and equal sign
5802 name.assign(field->getNameAsString());
5803
5804 // Figure out the type byte size (field_type_info.first) and
5805 // alignment (field_type_info.second) from the AST context.
5806 if (bit_offset_ptr) {
5807 const clang::ASTRecordLayout &record_layout =
5808 getASTContext()->getASTRecordLayout(record_decl);
5809 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
5810 }
5811
5812 const bool is_bitfield = field->isBitField();
5813
5814 if (bitfield_bit_size_ptr) {
5815 *bitfield_bit_size_ptr = 0;
5816
5817 if (is_bitfield) {
5818 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5819 llvm::APSInt bitfield_apsint;
5820 if (bitfield_bit_size_expr &&
5821 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5822 *getASTContext())) {
5823 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5824 }
5825 }
5826 }
5827 if (is_bitfield_ptr)
5828 *is_bitfield_ptr = is_bitfield;
5829
5830 return CompilerType(getASTContext(), field->getType());
5831 }
5832 }
5833 }
5834 break;
5835
5836 case clang::Type::ObjCObjectPointer: {
5837 const clang::ObjCObjectPointerType *objc_class_type =
5838 qual_type->getAs<clang::ObjCObjectPointerType>();
5839 const clang::ObjCInterfaceType *objc_interface_type =
5840 objc_class_type->getInterfaceType();
5841 if (objc_interface_type &&
5842 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5843 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
5844 clang::ObjCInterfaceDecl *class_interface_decl =
5845 objc_interface_type->getDecl();
5846 if (class_interface_decl) {
5847 return CompilerType(
5848 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5849 idx, name, bit_offset_ptr,
5850 bitfield_bit_size_ptr, is_bitfield_ptr));
5851 }
5852 }
5853 break;
5854 }
5855
5856 case clang::Type::ObjCObject:
5857 case clang::Type::ObjCInterface:
5858 if (GetCompleteType(type)) {
5859 const clang::ObjCObjectType *objc_class_type =
5860 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5861 assert(objc_class_type)(static_cast <bool> (objc_class_type) ? void (0) : __assert_fail
("objc_class_type", "/build/llvm-toolchain-snapshot-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 5861, __extension__ __PRETTY_FUNCTION__))
;
5862 if (objc_class_type) {
5863 clang::ObjCInterfaceDecl *class_interface_decl =
5864 objc_class_type->getInterface();
5865 return CompilerType(
5866 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5867 idx, name, bit_offset_ptr,
5868 bitfield_bit_size_ptr, is_bitfield_ptr));
5869 }
5870 }
5871 break;
5872
5873 case clang::Type::Typedef:
5874 return CompilerType(getASTContext(),
5875 llvm::cast<clang::TypedefType>(qual_type)
5876 ->getDecl()
5877 ->getUnderlyingType())
5878 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5879 is_bitfield_ptr);
5880
5881 case clang::Type::Auto:
5882 return CompilerType(
5883 getASTContext(),
5884 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5885 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5886 is_bitfield_ptr);
5887
5888 case clang::Type::Elaborated:
5889 return CompilerType(
5890 getASTContext(),
5891 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5892 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5893 is_bitfield_ptr);
5894
5895 case clang::Type::Paren:
5896 return CompilerType(getASTContext(),
5897 llvm::cast<clang::ParenType>(qual_type)->desugar())
5898 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5899 is_bitfield_ptr);
5900
5901 default:
5902 break;
5903 }
5904 return CompilerType();
5905}
5906
5907uint32_t
5908ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
5909 uint32_t count = 0;
5910 clang::QualType qual_type(GetCanonicalQualType(type));
5911 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5912 switch (type_class) {
5913 case clang::Type::Record:
5914 if (GetCompleteType(type)) {
5915 const clang::CXXRecordDecl *cxx_record_decl =
5916 qual_type->getAsCXXRecordDecl();
5917 if (cxx_record_decl)
5918 count = cxx_record_decl->getNumBases();
5919 }
5920 break;
5921
5922 case clang::Type::ObjCObjectPointer:
5923 count = GetPointeeType(type).GetNumDirectBaseClasses();
5924 break;
5925
5926 case clang::Type::ObjCObject:
5927 if (GetCompleteType(type)) {
5928 const clang::ObjCObjectType *objc_class_type =
5929 qual_type->getAsObjCQualifiedInterfaceType();
5930 if (objc_class_type) {
5931 clang::ObjCInterfaceDecl *class_interface_decl =
5932 objc_class_type->getInterface();
5933
5934 if (class_interface_decl && class_interface_decl->getSuperClass())
5935 count = 1;
5936 }
5937 }
5938 break;
5939 case clang::Type::ObjCInterface:
5940 if (GetCompleteType(type)) {
5941 const clang::ObjCInterfaceType *objc_interface_type =
5942 qual_type->getAs<clang::ObjCInterfaceType>();
5943 if (objc_interface_type) {
5944 clang::ObjCInterfaceDecl *class_interface_decl =
5945 objc_interface_type->getInterface();
5946
5947 if (class_interface_decl && class_interface_decl->getSuperClass())
5948 count = 1;
5949 }
5950 }
5951 break;
5952
5953 case clang::Type::Typedef:
5954 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5955 ->getDecl()
5956 ->getUnderlyingType()
5957 .getAsOpaquePtr());
5958 break;
5959
5960 case clang::Type::Auto:
5961 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
5962 ->getDeducedType()
5963 .getAsOpaquePtr());
5964 break;
5965
5966 case clang::Type::Elaborated:
5967 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
5968 ->getNamedType()
5969 .getAsOpaquePtr());
5970 break;
5971
5972 case clang::Type::Paren:
5973 return GetNumDirectBaseClasses(
5974 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5975
5976 default:
5977 break;
5978 }
5979 return count;
5980}
5981
5982uint32_t
5983ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
5984 uint32_t count = 0;
5985 clang::QualType qual_type(GetCanonicalQualType(type));
5986 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5987 switch (type_class) {
5988 case clang::Type::Record:
5989 if (GetCompleteType(type)) {
5990 const clang::CXXRecordDecl *cxx_record_decl =
5991 qual_type->getAsCXXRecordDecl();
5992 if (cxx_record_decl)
5993 count = cxx_record_decl->getNumVBases();
5994 }
5995 break;
5996
5997 case clang::Type::Typedef:
5998 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5999 ->getDecl()
6000 ->getUnderlyingType()
6001 .getAsOpaquePtr());
6002 break;
6003
6004 case clang::Type::Auto:
6005 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6006 ->getDeducedType()
6007 .getAsOpaquePtr());
6008 break;
6009
6010 case clang::Type::Elaborated:
6011 count =
6012 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6013 ->getNamedType()
6014 .getAsOpaquePtr());
6015 break;
6016
6017 case clang::Type::Paren:
6018 count = GetNumVirtualBaseClasses(
6019 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6020 break;
6021
6022 default:
6023 break;
6024 }
6025 return count;
6026}
6027
6028CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
6029 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6030 clang::QualType qual_type(GetCanonicalQualType(type));
6031 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6032 switch (type_class) {
6033 case clang::Type::Record:
6034 if (GetCompleteType(type)) {
6035 const clang::CXXRecordDecl *cxx_record_decl =
6036 qual_type->getAsCXXRecordDecl();
6037 if (cxx_record_decl) {
6038 uint32_t curr_idx = 0;
6039 clang::CXXRecordDecl::base_class_const_iterator base_class,
6040 base_class_end;
6041 for (base_class = cxx_record_decl->bases_begin(),
6042 base_class_end = cxx_record_decl->bases_end();
6043 base_class != base_class_end; ++base_class, ++curr_idx) {
6044 if (curr_idx == idx) {
6045 if (bit_offset_ptr) {
6046 const clang::ASTRecordLayout &record_layout =
6047 getASTContext()->getASTRecordLayout(cxx_record_decl);
6048 const clang::CXXRecordDecl *base_class_decl =
6049 llvm::cast<clang::CXXRecordDecl>(
6050 base_class->getType()
6051 ->getAs<clang::RecordType>()
6052 ->getDecl());
6053 if (base_class->isVirtual())
6054 *bit_offset_ptr =
6055 record_layout.getVBaseClassOffset(base_class_decl)
6056 .getQuantity() *
6057 8;
6058 else
6059 *bit_offset_ptr =
6060 record_layout.getBaseClassOffset(base_class_decl)
6061 .getQuantity() *
6062 8;
6063 }
6064 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6065 }
6066 }
6067 }
6068 }
6069 break;
6070
6071 case clang::Type::ObjCObjectPointer:
6072 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6073
6074 case clang::Type::ObjCObject:
6075 if (idx == 0 && GetCompleteType(type)) {
6076 const clang::ObjCObjectType *objc_class_type =
6077 qual_type->getAsObjCQualifiedInterfaceType();
6078 if (objc_class_type) {
6079 clang::ObjCInterfaceDecl *class_interface_decl =
6080 objc_class_type->getInterface();
6081
6082 if (class_interface_decl) {
6083 clang::ObjCInterfaceDecl *superclass_interface_decl =
6084 class_interface_decl->getSuperClass();
6085 if (superclass_interface_decl) {
6086 if (bit_offset_ptr)
6087 *bit_offset_ptr = 0;
6088 return CompilerType(getASTContext(),
6089 getASTContext()->getObjCInterfaceType(
6090 superclass_interface_decl));
6091 }
6092 }
6093 }
6094 }
6095 break;
6096 case clang::Type::ObjCInterface:
6097 if (idx == 0 && GetCompleteType(type)) {
6098 const clang::ObjCObjectType *objc_interface_type =
6099 qual_type->getAs<clang::ObjCInterfaceType>();
6100 if (objc_interface_type) {
6101 clang::ObjCInterfaceDecl *class_interface_decl =
6102 objc_interface_type->getInterface();
6103
6104 if (class_interface_decl) {
6105 clang::ObjCInterfaceDecl *superclass_interface_decl =
6106 class_interface_decl->getSuperClass();
6107 if (superclass_interface_decl) {
6108 if (bit_offset_ptr)
6109 *bit_offset_ptr = 0;
6110 return CompilerType(getASTContext(),
6111 getASTContext()->getObjCInterfaceType(
6112 superclass_interface_decl));
6113 }
6114 }
6115 }
6116 }
6117 break;
6118
6119 case clang::Type::Typedef:
6120 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6121 ->getDecl()
6122 ->getUnderlyingType()
6123 .getAsOpaquePtr(),
6124 idx, bit_offset_ptr);
6125
6126 case clang::Type::Auto:
6127 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6128 ->getDeducedType()
6129 .getAsOpaquePtr(),
6130 idx, bit_offset_ptr);
6131
6132 case clang::Type::Elaborated:
6133 return GetDirectBaseClassAtIndex(
6134 llvm::cast<clang::ElaboratedType>(qual_type)
6135 ->getNamedType()
6136 .getAsOpaquePtr(),
6137 idx, bit_offset_ptr);
6138
6139 case clang::Type::Paren:
6140 return GetDirectBaseClassAtIndex(
6141 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6142 idx, bit_offset_ptr);
6143
6144 default:
6145 break;
6146 }
6147 return CompilerType();
6148}
6149
6150CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6151 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6152 clang::QualType qual_type(GetCanonicalQualType(type));
6153 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6154 switch (type_class) {
6155 case clang::Type::Record:
6156 if (GetCompleteType(type)) {
6157 const clang::CXXRecordDecl *cxx_record_decl =
6158 qual_type->getAsCXXRecordDecl();
6159 if (cxx_record_decl) {
6160 uint32_t curr_idx = 0;
6161 clang::CXXRecordDecl::base_class_const_iterator base_class,
6162 base_class_end;
6163 for (base_class = cxx_record_decl->vbases_begin(),
6164 base_class_end = cxx_record_decl->vbases_end();
6165 base_class != base_class_end; ++base_class, ++curr_idx) {
6166 if (curr_idx == idx) {
6167 if (bit_offset_ptr) {
6168 const clang::ASTRecordLayout &record_layout =
6169 getASTContext()->getASTRecordLayout(cxx_record_decl);
6170 const clang::CXXRecordDecl *base_class_decl =
6171 llvm::cast<clang::CXXRecordDecl>(
6172 base_class->getType()
6173 ->getAs<clang::RecordType>()
6174 ->getDecl());
6175 *bit_offset_ptr =
6176 record_layout.getVBaseClassOffset(base_class_decl)
6177 .getQuantity() *
6178 8;
6179 }
6180 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6181 }
6182 }
6183 }
6184 }
6185 break;
6186
6187 case clang::Type::Typedef:
6188 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6189 ->getDecl()
6190 ->getUnderlyingType()
6191 .getAsOpaquePtr(),
6192 idx, bit_offset_ptr);
6193
6194 case clang::Type::Auto:
6195 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6196 ->getDeducedType()
6197 .getAsOpaquePtr(),
6198 idx, bit_offset_ptr);
6199
6200 case clang::Type::Elaborated:
6201 return GetVirtualBaseClassAtIndex(
6202 llvm::cast<clang::ElaboratedType>(qual_type)
6203 ->getNamedType()
6204 .getAsOpaquePtr(),
6205 idx, bit_offset_ptr);
6206
6207 case clang::Type::Paren:
6208 return GetVirtualBaseClassAtIndex(
6209 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6210 idx, bit_offset_ptr);
6211
6212 default:
6213 break;
6214 }
6215 return CompilerType();
6216}
6217
6218// If a pointer to a pointee type (the clang_type arg) says that it has no
6219// children, then we either need to trust it, or override it and return a
6220// different result. For example, an "int *" has one child that is an integer,
6221// but a function pointer doesn't have any children. Likewise if a Record type
6222// claims it has no children, then there really is nothing to show.
6223uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6224 if (type.isNull())
6225 return 0;
6226
6227 clang::QualType qual_type(type.getCanonicalType());
6228 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6229 switch (type_class) {
6230 case clang::Type::Builtin:
6231 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6232 case clang::BuiltinType::UnknownAny:
6233 case clang::BuiltinType::Void:
6234 case clang::BuiltinType::NullPtr:
6235 case clang::BuiltinType::OCLEvent:
6236 case clang::BuiltinType::OCLImage1dRO:
6237 case clang::BuiltinType::OCLImage1dWO:
6238 case clang::BuiltinType::OCLImage1dRW:
6239 case clang::BuiltinType::OCLImage1dArrayRO:
6240 case clang::BuiltinType::OCLImage1dArrayWO:
6241 case clang::BuiltinType::OCLImage1dArrayRW:
6242 case clang::BuiltinType::OCLImage1dBufferRO:
6243 case clang::BuiltinType::OCLImage1dBufferWO:
6244 case clang::BuiltinType::OCLImage1dBufferRW:
6245 case clang::BuiltinType::OCLImage2dRO:
6246 case clang::BuiltinType::OCLImage2dWO:
6247 case clang::BuiltinType::OCLImage2dRW:
6248 case clang::BuiltinType::OCLImage2dArrayRO:
6249 case clang::BuiltinType::OCLImage2dArrayWO:
6250 case clang::BuiltinType::OCLImage2dArrayRW:
6251 case clang::BuiltinType::OCLImage3dRO:
6252 case clang::BuiltinType::OCLImage3dWO:
6253 case clang::BuiltinType::OCLImage3dRW:
6254 case clang::BuiltinType::OCLSampler:
6255 return 0;
6256 case clang::BuiltinType::Bool:
6257 case clang::BuiltinType::Char_U:
6258 case clang::BuiltinType::UChar:
6259 case clang::BuiltinType::WChar_U:
6260 case clang::BuiltinType::Char16:
6261 case clang::BuiltinType::Char32:
6262 case clang::BuiltinType::UShort:
6263 case clang::BuiltinType::UInt:
6264 case clang::BuiltinType::ULong:
6265 case clang::BuiltinType::ULongLong:
6266 case clang::BuiltinType::UInt128:
6267 case clang::BuiltinType::Char_S:
6268 case clang::BuiltinType::SChar:
6269 case clang::BuiltinType::WChar_S:
6270 case clang::BuiltinType::Short:
6271 case clang::BuiltinType::Int:
6272 case clang::BuiltinType::Long:
6273 case clang::BuiltinType::LongLong:
6274 case clang::BuiltinType::Int128:
6275 case clang::BuiltinType::Float:
6276 case clang::BuiltinType::Double:
6277 case clang::BuiltinType::LongDouble:
6278 case clang::BuiltinType::Dependent:
6279 case clang::BuiltinType::Overload:
6280 case clang::BuiltinType::ObjCId:
6281 case clang::BuiltinType::ObjCClass:
6282 case clang::BuiltinType::ObjCSel:
6283 case clang::BuiltinType::BoundMember:
6284 case clang::BuiltinType::Half:
6285 case clang::BuiltinType::ARCUnbridgedCast:
6286 case clang::BuiltinType::PseudoObject:
6287 case clang::BuiltinType::BuiltinFn:
6288 case clang::BuiltinType::OMPArraySection:
6289 return 1;
6290 default:
6291 return 0;
6292 }
6293 break;
6294
6295 case clang::Type::Complex:
6296 return 1;
6297 case clang::Type::Pointer:
6298 return 1;
6299 case clang::Type::BlockPointer:
6300 return 0; // If block pointers don't have debug info, then no children for
6301 // them
6302 case clang::Type::LValueReference:
6303 return 1;
6304 case clang::Type::RValueReference:
6305 return 1;
6306 case clang::Type::MemberPointer:
6307 return 0;
6308 case clang::Type::ConstantArray:
6309 return 0;
6310 case clang::Type::IncompleteArray:
6311 return 0;
6312 case clang::Type::VariableArray:
6313 return 0;
6314 case clang::Type::DependentSizedArray:
6315 return 0;
6316 case clang::Type::DependentSizedExtVector:
6317 return 0;
6318 case clang::Type::Vector:
6319 return 0;
6320 case clang::Type::ExtVector:
6321 return 0;
6322 case clang::Type::FunctionProto:
6323 return 0; // When we function pointers, they have no children...
6324 case clang::Type::FunctionNoProto:
6325 return 0; // When we function pointers, they have no children...
6326 case clang::Type::UnresolvedUsing:
6327 return 0;
6328 case clang::Type::Paren:
6329 return GetNumPointeeChildren(
6330 llvm::cast<clang::ParenType>(qual_type)->desugar());
6331 case clang::Type::Typedef:
6332 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6333 ->getDecl()
6334 ->getUnderlyingType());
6335 case clang::Type::Auto:
6336 return GetNumPointeeChildren(
6337 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6338 case clang::Type::Elaborated:
6339 return GetNumPointeeChildren(
6340 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6341 case clang::Type::TypeOfExpr:
6342 return 0;
6343 case clang::Type::TypeOf:
6344 return 0;
6345 case clang::Type::Decltype:
6346 return 0;
6347 case clang::Type::Record:
6348 return 0;
6349 case clang::Type::Enum:
6350 return 1;
6351 case clang::Type::TemplateTypeParm:
6352 return 1;
6353 case clang::Type::SubstTemplateTypeParm:
6354 return 1;
6355 case clang::Type::TemplateSpecialization:
6356 return 1;
6357 case clang::Type::InjectedClassName:
6358 return 0;
6359 case clang::Type::DependentName:
6360 return 1;
6361 case clang::Type::DependentTemplateSpecialization:
6362 return 1;
6363 case clang::Type::ObjCObject:
6364 return 0;
6365 case clang::Type::ObjCInterface:
6366 return 0;
6367 case clang::Type::ObjCObjectPointer:
6368 return 1;
6369 default:
6370 break;
6371 }
6372 return 0;
6373}
6374
6375CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6376 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6377 bool transparent_pointers, bool omit_empty_base_classes,
6378 bool ignore_array_bounds, std::string &child_name,
6379 uint32_t &child_byte_size, int32_t &child_byte_offset,
6380 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6381 bool &child_is_base_class, bool &child_is_deref_of_parent,
6382 ValueObject *valobj, uint64_t &language_flags) {
6383 if (!type)
6384 return CompilerType();
6385
6386 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6387 const clang::Type::TypeClass parent_type_class =
6388 parent_qual_type->getTypeClass();
6389 child_bitfield_bit_size = 0;
6390 child_bitfield_bit_offset = 0;
6391 child_is_base_class = false;
6392 language_flags = 0;
6393
6394 const bool idx_is_valid = idx < GetNumChildren(type, omit_empty_base_classes);
6395 uint32_t bit_offset;
6396 switch (parent_type_class) {
6397 case clang::Type::Builtin:
6398 if (idx_is_valid) {
6399 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6400 case clang::BuiltinType::ObjCId:
6401 case clang::BuiltinType::ObjCClass:
6402 child_name = "isa";
6403 child_byte_size =
6404 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6405 CHAR_BIT8;
6406 return CompilerType(getASTContext(),
6407 getASTContext()->ObjCBuiltinClassTy);
6408
6409 default:
6410 break;
6411 }
6412 }
6413 break;
6414
6415 case clang::Type::Record:
6416 if (idx_is_valid && GetCompleteType(type)) {
6417 const clang::RecordType *record_type =
6418 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6419 const clang::RecordDecl *record_decl = record_type->getDecl();
6420 assert(record_decl)(static_cast <bool> (record_decl) ? void (0) : __assert_fail
("record_decl", "/build/llvm-toolchain-snapshot-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 6420, __extension__ __PRETTY_FUNCTION__))
;
6421 const clang::ASTRecordLayout &record_layout =
6422 getASTContext()->getASTRecordLayout(record_decl);
6423 uint32_t child_idx = 0;
6424
6425 const clang::CXXRecordDecl *cxx_record_decl =
6426 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6427 if (cxx_record_decl) {
6428 // We might have base classes to print out first
6429 clang::CXXRecordDecl::base_class_const_iterator base_class,
6430 base_class_end;
6431 for (base_class = cxx_record_decl->bases_begin(),
6432 base_class_end = cxx_record_decl->bases_end();
6433 base_class != base_class_end; ++base_class) {
6434 const clang::CXXRecordDecl *base_class_decl = nullptr;
6435
6436 // Skip empty base classes
6437 if (omit_empty_base_classes) {
6438 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6439 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6440 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
6441 continue;
6442 }
6443
6444 if (idx == child_idx) {
6445 if (base_class_decl == nullptr)
6446 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6447 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6448
6449 if (base_class->isVirtual()) {
6450 bool handled = false;
6451 if (valobj) {
6452 Status err;
6453 AddressType addr_type = eAddressTypeInvalid;
6454 lldb::addr_t vtable_ptr_addr =
6455 valobj->GetCPPVTableAddress(addr_type);
6456
6457 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS(18446744073709551615UL) &&
6458 addr_type == eAddressTypeLoad) {
6459
6460 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
6461 Process *process = exe_ctx.GetProcessPtr();
6462 if (process) {
6463 clang::VTableContextBase *vtable_ctx =
6464 getASTContext()->getVTableContext();
6465 if (vtable_ctx) {
6466 if (vtable_ctx->isMicrosoft()) {
6467 clang::MicrosoftVTableContext *msoft_vtable_ctx =
6468 static_cast<clang::MicrosoftVTableContext *>(
6469 vtable_ctx);
6470
6471 if (vtable_ptr_addr) {
6472 const lldb::addr_t vbtable_ptr_addr =
6473 vtable_ptr_addr +
6474 record_layout.getVBPtrOffset().getQuantity();
6475
6476 const lldb::addr_t vbtable_ptr =
6477 process->ReadPointerFromMemory(vbtable_ptr_addr,
6478 err);
6479 if (vbtable_ptr != LLDB_INVALID_ADDRESS(18446744073709551615UL)) {
6480 // Get the index into the virtual base table. The
6481 // index is the index in uint32_t from vbtable_ptr
6482 const unsigned vbtable_index =
6483 msoft_vtable_ctx->getVBTableIndex(
6484 cxx_record_decl, base_class_decl);
6485 const lldb::addr_t base_offset_addr =
6486 vbtable_ptr + vbtable_index * 4;
6487 const uint32_t base_offset =
6488 process->ReadUnsignedIntegerFromMemory(
6489 base_offset_addr, 4, UINT32_MAX(4294967295U), err);
6490 if (base_offset != UINT32_MAX(4294967295U)) {
6491 handled = true;
6492 bit_offset = base_offset * 8;
6493 }
6494 }
6495 }
6496 } else {
6497 clang::ItaniumVTableContext *itanium_vtable_ctx =
6498 static_cast<clang::ItaniumVTableContext *>(
6499 vtable_ctx);
6500 if (vtable_ptr_addr) {
6501 const lldb::addr_t vtable_ptr =
6502 process->ReadPointerFromMemory(vtable_ptr_addr,
6503 err);
6504 if (vtable_ptr != LLDB_INVALID_ADDRESS(18446744073709551615UL)) {
6505 clang::CharUnits base_offset_offset =
6506 itanium_vtable_ctx->getVirtualBaseOffsetOffset(
6507 cxx_record_decl, base_class_decl);
6508 const lldb::addr_t base_offset_addr =
6509 vtable_ptr + base_offset_offset.getQuantity();
6510 const uint32_t base_offset_size =
6511 process->GetAddressByteSize();
6512 const uint64_t base_offset =
6513 process->ReadUnsignedIntegerFromMemory(
6514 base_offset_addr, base_offset_size,
6515 UINT32_MAX(4294967295U), err);
6516 if (base_offset < UINT32_MAX(4294967295U)) {
6517 handled = true;
6518 bit_offset = base_offset * 8;
6519 }
6520 }
6521 }
6522 }
6523 }
6524 }
6525 }
6526 }
6527 if (!handled)
6528 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6529 .getQuantity() *
6530 8;
6531 } else
6532 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6533 .getQuantity() *
6534 8;
6535
6536 // Base classes should be a multiple of 8 bits in size
6537 child_byte_offset = bit_offset / 8;
6538 CompilerType base_class_clang_type(getASTContext(),
6539 base_class->getType());
6540 child_name = base_class_clang_type.GetTypeName().AsCString("");
6541 uint64_t base_class_clang_type_bit_size =
6542 base_class_clang_type.GetBitSize(
6543 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL__null);
6544
6545 // Base classes bit sizes should be a multiple of 8 bits in size
6546 assert(base_class_clang_type_bit_size % 8 == 0)(static_cast <bool> (base_class_clang_type_bit_size % 8
== 0) ? void (0) : __assert_fail ("base_class_clang_type_bit_size % 8 == 0"
, "/build/llvm-toolchain-snapshot-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 6546, __extension__ __PRETTY_FUNCTION__))
;
6547 child_byte_size = base_class_clang_type_bit_size / 8;
6548 child_is_base_class = true;
6549 return base_class_clang_type;
6550 }
6551 // We don't increment the child index in the for loop since we might
6552 // be skipping empty base classes
6553 ++child_idx;
6554 }
6555 }
6556 // Make sure index is in range...
6557 uint32_t field_idx = 0;
6558 clang::RecordDecl::field_iterator field, field_end;
6559 for (field = record_decl->field_begin(),
6560 field_end = record_decl->field_end();
6561 field != field_end; ++field, ++field_idx, ++child_idx) {
6562 if (idx == child_idx) {
6563 // Print the member type if requested
6564 // Print the member name and equal sign
6565 child_name.assign(field->getNameAsString());
6566
6567 // Figure out the type byte size (field_type_info.first) and
6568 // alignment (field_type_info.second) from the AST context.
6569 CompilerType field_clang_type(getASTContext(), field->getType());
6570 assert(field_idx < record_layout.getFieldCount())(static_cast <bool> (field_idx < record_layout.getFieldCount
()) ? void (0) : __assert_fail ("field_idx < record_layout.getFieldCount()"
, "/build/llvm-toolchain-snapshot-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 6570, __extension__ __PRETTY_FUNCTION__))
;
6571 child_byte_size = field_clang_type.GetByteSize(
6572 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL__null);
6573 const uint32_t child_bit_size = child_byte_size * 8;
6574
6575 // Figure out the field offset within the current struct/union/class
6576 // type
6577 bit_offset = record_layout.getFieldOffset(field_idx);
6578 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6579 child_bitfield_bit_size)) {
6580 child_bitfield_bit_offset = bit_offset % child_bit_size;
6581 const uint32_t child_bit_offset =
6582 bit_offset - child_bitfield_bit_offset;
6583 child_byte_offset = child_bit_offset / 8;
6584 } else {
6585 child_byte_offset = bit_offset / 8;
6586 }
6587
6588 return field_clang_type;
6589 }
6590 }
6591 }
6592 break;
6593
6594 case clang::Type::ObjCObject:
6595 case clang::Type::ObjCInterface:
6596 if (idx_is_valid && GetCompleteType(type)) {
6597 const clang::ObjCObjectType *objc_class_type =
6598 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6599 assert(objc_class_type)(static_cast <bool> (objc_class_type) ? void (0) : __assert_fail
("objc_class_type", "/build/llvm-toolchain-snapshot-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 6599, __extension__ __PRETTY_FUNCTION__))
;
6600 if (objc_class_type) {
6601 uint32_t child_idx = 0;
6602 clang::ObjCInterfaceDecl *class_interface_decl =
6603 objc_class_type->getInterface();
6604
6605 if (class_interface_decl) {
6606
6607 const clang::ASTRecordLayout &interface_layout =
6608 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6609 clang::ObjCInterfaceDecl *superclass_interface_decl =
6610 class_interface_decl->getSuperClass();
6611 if (superclass_interface_decl) {
6612 if (omit_empty_base_classes) {
6613 CompilerType base_class_clang_type(
6614 getASTContext(), getASTContext()->getObjCInterfaceType(
6615 superclass_interface_decl));
6616 if (base_class_clang_type.GetNumChildren(
6617 omit_empty_base_classes) > 0) {
6618 if (idx == 0) {
6619 clang::QualType ivar_qual_type(
6620 getASTContext()->getObjCInterfaceType(
6621 superclass_interface_decl));
6622
6623 child_name.assign(
6624 superclass_interface_decl->getNameAsString());
6625
6626 clang::TypeInfo ivar_type_info =
6627 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6628
6629 child_byte_size = ivar_type_info.Width / 8;
6630 child_byte_offset = 0;
6631 child_is_base_class = true;
6632
6633 return CompilerType(getASTContext(), ivar_qual_type);
6634 }
6635
6636 ++child_idx;
6637 }
6638 } else
6639 ++child_idx;
6640 }
6641
6642 const uint32_t superclass_idx = child_idx;
6643
6644 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6645 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6646 ivar_end = class_interface_decl->ivar_end();
6647
6648 for (ivar_pos = class_interface_decl->ivar_begin();
6649 ivar_pos != ivar_end; ++ivar_pos) {
6650 if (child_idx == idx) {
6651 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6652
6653 clang::QualType ivar_qual_type(ivar_decl->getType());
6654
6655 child_name.assign(ivar_decl->getNameAsString());
6656
6657 clang::TypeInfo ivar_type_info =
6658 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6659
6660 child_byte_size = ivar_type_info.Width / 8;
6661
6662 // Figure out the field offset within the current
6663 // struct/union/class type
6664 // For ObjC objects, we can't trust the bit offset we get from
6665 // the Clang AST, since
6666 // that doesn't account for the space taken up by unbacked
6667 // properties, or from
6668 // the changing size of base classes that are newer than this
6669 // class.
6670 // So if we have a process around that we can ask about this
6671 // object, do so.
6672 child_byte_offset = LLDB_INVALID_IVAR_OFFSET(4294967295U);
6673 Process *process = nullptr;
6674 if (exe_ctx)
6675 process = exe_ctx->GetProcessPtr();
6676 if (process) {
6677 ObjCLanguageRuntime *objc_runtime =
6678 process->GetObjCLanguageRuntime();
6679 if (objc_runtime != nullptr) {
6680 CompilerType parent_ast_type(getASTContext(),
6681 parent_qual_type);
6682 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6683 parent_ast_type, ivar_decl->getNameAsString().c_str());
6684 }
6685 }
6686
6687 // Setting this to UINT32_MAX to make sure we don't compute it
6688 // twice...
6689 bit_offset = UINT32_MAX(4294967295U);
6690
6691 if (child_byte_offset ==
6692 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET(4294967295U))) {
6693 bit_offset = interface_layout.getFieldOffset(child_idx -
6694 superclass_idx);
6695 child_byte_offset = bit_offset / 8;
6696 }
6697
6698 // Note, the ObjC Ivar Byte offset is just that, it doesn't
6699 // account for the bit offset
6700 // of a bitfield within its containing object. So regardless of
6701 // where we get the byte
6702 // offset from, we still need to get the bit offset for
6703 // bitfields from the layout.
6704
6705 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6706 child_bitfield_bit_size)) {
6707 if (bit_offset == UINT32_MAX(4294967295U))
6708 bit_offset = interface_layout.getFieldOffset(
6709 child_idx - superclass_idx);
6710
6711 child_bitfield_bit_offset = bit_offset % 8;
6712 }
6713 return CompilerType(getASTContext(), ivar_qual_type);
6714 }
6715 ++child_idx;
6716 }
6717 }
6718 }
6719 }
6720 }
6721 break;
6722
6723 case clang::Type::ObjCObjectPointer:
6724 if (idx_is_valid) {
6725 CompilerType pointee_clang_type(GetPointeeType(type));
6726
6727 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6728 child_is_deref_of_parent = false;
6729 bool tmp_child_is_deref_of_parent = false;
6730 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6731 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6732 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6733 child_bitfield_bit_size, child_bitfield_bit_offset,
6734 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6735 language_flags);
6736 } else {
6737 child_is_deref_of_parent = true;
6738 const char *parent_name =
6739 valobj ? valobj->GetName().GetCString() : NULL__null;
6740 if (parent_name) {
6741 child_name.assign(1, '*');
6742 child_name += parent_name;
6743 }
6744
6745 // We have a pointer to an simple type
6746 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
6747 child_byte_size = pointee_clang_type.GetByteSize(
6748 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL__null);
6749 child_byte_offset = 0;
6750 return pointee_clang_type;
6751 }
6752 }
6753 }
6754 break;
6755
6756 case clang::Type::Vector:
6757 case clang::Type::ExtVector:
6758 if (idx_is_valid) {
6759 const clang::VectorType *array =
6760 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6761 if (array) {
6762 CompilerType element_type(getASTContext(), array->getElementType());
6763 if (element_type.GetCompleteType()) {
6764 char element_name[64];
6765 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64"l" "u" "]",
6766 static_cast<uint64_t>(idx));
6767 child_name.assign(element_name);
6768 child_byte_size = element_type.GetByteSize(
6769 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL__null);
6770 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6771 return element_type;
6772 }
6773 }
6774 }
6775 break;
6776
6777 case clang::Type::ConstantArray:
6778 case clang::Type::IncompleteArray:
6779 if (ignore_array_bounds || idx_is_valid) {
6780 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6781 if (array) {
6782 CompilerType element_type(getASTContext(), array->getElementType());
6783 if (element_type.GetCompleteType()) {
6784 child_name = llvm::formatv("[{0}]", idx);
6785 child_byte_size = element_type.GetByteSize(
6786 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL__null);
6787 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6788 return element_type;
6789 }
6790 }
6791 }
6792 break;
6793
6794 case clang::Type::Pointer: {
6795 CompilerType pointee_clang_type(GetPointeeType(type));
6796
6797 // Don't dereference "void *" pointers
6798 if (pointee_clang_type.IsVoidType())
6799 return CompilerType();
6800
6801 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6802 child_is_deref_of_parent = false;
6803 bool tmp_child_is_deref_of_parent = false;
6804 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6805 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6806 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6807 child_bitfield_bit_size, child_bitfield_bit_offset,
6808 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6809 language_flags);
6810 } else {
6811 child_is_deref_of_parent = true;
6812
6813 const char *parent_name =
6814 valobj ? valobj->GetName().GetCString() : NULL__null;
6815 if (parent_name) {
6816 child_name.assign(1, '*');
6817 child_name += parent_name;
6818 }
6819
6820 // We have a pointer to an simple type
6821 if (idx == 0) {
6822 child_byte_size = pointee_clang_type.GetByteSize(
6823 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL__null);
6824 child_byte_offset = 0;
6825 return pointee_clang_type;
6826 }
6827 }
6828 break;
6829 }
6830
6831 case clang::Type::LValueReference:
6832 case clang::Type::RValueReference:
6833 if (idx_is_valid) {
6834 const clang::ReferenceType *reference_type =
6835 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
6836 CompilerType pointee_clang_type(getASTContext(),
6837 reference_type->getPointeeType());
6838 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6839 child_is_deref_of_parent = false;
6840 bool tmp_child_is_deref_of_parent = false;
6841 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6842 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6843 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6844 child_bitfield_bit_size, child_bitfield_bit_offset,
6845 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6846 language_flags);
6847 } else {
6848 const char *parent_name =
6849 valobj ? valobj->GetName().GetCString() : NULL__null;
6850 if (parent_name) {
6851 child_name.assign(1, '&');
6852 child_name += parent_name;
6853 }
6854
6855 // We have a pointer to an simple type
6856 if (idx == 0) {
6857 child_byte_size = pointee_clang_type.GetByteSize(
6858 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL__null);
6859 child_byte_offset = 0;
6860 return pointee_clang_type;
6861 }
6862 }
6863 }
6864 break;
6865
6866 case clang::Type::Typedef: {
6867 CompilerType typedefed_clang_type(
6868 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)
6869 ->getDecl()
6870 ->getUnderlyingType());
6871 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
6872 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6873 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6874 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6875 child_is_deref_of_parent, valobj, language_flags);
6876 } break;
6877
6878 case clang::Type::Auto: {
6879 CompilerType elaborated_clang_type(
6880 getASTContext(),
6881 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
6882 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6883 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6884 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6885 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6886 child_is_deref_of_parent, valobj, language_flags);
6887 }
6888
6889 case clang::Type::Elaborated: {
6890 CompilerType elaborated_clang_type(
6891 getASTContext(),
6892 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
6893 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6894 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6895 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6896 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6897 child_is_deref_of_parent, valobj, language_flags);
6898 }
6899
6900 case clang::Type::Paren: {
6901 CompilerType paren_clang_type(
6902 getASTContext(),
6903 llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
6904 return paren_clang_type.GetChildCompilerTypeAtIndex(
6905 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6906 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6907 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6908 child_is_deref_of_parent, valobj, language_flags);
6909 }
6910
6911 default:
6912 break;
6913 }
6914 return CompilerType();
6915}
6916
6917static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
6918 const clang::CXXBaseSpecifier *base_spec,
6919 bool omit_empty_base_classes) {
6920 uint32_t child_idx = 0;
6921
6922 const clang::CXXRecordDecl *cxx_record_decl =
6923 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6924
6925 // const char *super_name = record_decl->getNameAsCString();
6926 // const char *base_name =
6927 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
6928 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
6929 //
6930 if (cxx_record_decl) {
6931 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6932 for (base_class = cxx_record_decl->bases_begin(),
6933 base_class_end = cxx_record_decl->bases_end();
6934 base_class != base_class_end; ++base_class) {
6935 if (omit_empty_base_classes) {
6936 if (BaseSpecifierIsEmpty(base_class))
6937 continue;
6938 }
6939
6940 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
6941 // super_name, base_name,
6942 // child_idx,
6943 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
6944 //
6945 //
6946 if (base_class == base_spec)
6947 return child_idx;
6948 ++child_idx;
6949 }
6950 }
6951
6952 return UINT32_MAX(4294967295U);
6953}
6954
6955static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
6956 clang::NamedDecl *canonical_decl,
6957 bool omit_empty_base_classes) {
6958 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
6959 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
6960 omit_empty_base_classes);
6961
6962 clang::RecordDecl::field_iterator field, field_end;
6963 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6964 field != field_end; ++field, ++child_idx) {
6965 if (field->getCanonicalDecl() == canonical_decl)
6966 return child_idx;
6967 }
6968
6969 return UINT32_MAX(4294967295U);
6970}
6971
6972// Look for a child member (doesn't include base classes, but it does include
6973// their members) in the type hierarchy. Returns an index path into "clang_type"
6974// on how to reach the appropriate member.
6975//
6976// class A
6977// {
6978// public:
6979// int m_a;
6980// int m_b;
6981// };
6982//
6983// class B
6984// {
6985// };
6986//
6987// class C :
6988// public B,
6989// public A
6990// {
6991// };
6992//
6993// If we have a clang type that describes "class C", and we wanted to looked
6994// "m_b" in it:
6995//
6996// With omit_empty_base_classes == false we would get an integer array back
6997// with:
6998// { 1, 1 }
6999// The first index 1 is the child index for "class A" within class C
7000// The second index 1 is the child index for "m_b" within class A
7001//
7002// With omit_empty_base_classes == true we would get an integer array back with:
7003// { 0, 1 }
7004// The first index 0 is the child index for "class A" within class C (since
7005// class B doesn't have any members it doesn't count)
7006// The second index 1 is the child index for "m_b" within class A
7007
7008size_t ClangASTContext::GetIndexOfChildMemberWithName(
7009 lldb::opaque_compiler_type_t type, const char *name,
7010 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
7011 if (type && name && name[0]) {
7012 clang::QualType qual_type(GetCanonicalQualType(type));
7013 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7014 switch (type_class) {
7015 case clang::Type::Record:
7016 if (GetCompleteType(type)) {
7017 const clang::RecordType *record_type =
7018 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7019 const clang::RecordDecl *record_decl = record_type->getDecl();
7020
7021 assert(record_decl)(static_cast <bool> (record_decl) ? void (0) : __assert_fail
("record_decl", "/build/llvm-toolchain-snapshot-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 7021, __extension__ __PRETTY_FUNCTION__))
;
7022 uint32_t child_idx = 0;
7023
7024 const clang::CXXRecordDecl *cxx_record_decl =
7025 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7026
7027 // Try and find a field that matches NAME
7028 clang::RecordDecl::field_iterator field, field_end;
7029 llvm::StringRef name_sref(name);
7030 for (field = record_decl->field_begin(),
7031 field_end = record_decl->field_end();
7032 field != field_end; ++field, ++child_idx) {
7033 llvm::StringRef field_name = field->getName();
7034 if (field_name.empty()) {
7035 CompilerType field_type(getASTContext(), field->getType());
7036 child_indexes.push_back(child_idx);
7037 if (field_type.GetIndexOfChildMemberWithName(
7038 name, omit_empty_base_classes, child_indexes))
7039 return child_indexes.size();
7040 child_indexes.pop_back();
7041
7042 } else if (field_name.equals(name_sref)) {
7043 // We have to add on the number of base classes to this index!
7044 child_indexes.push_back(
7045 child_idx + ClangASTContext::GetNumBaseClasses(
7046 cxx_record_decl, omit_empty_base_classes));
7047 return child_indexes.size();
7048 }
7049 }
7050
7051 if (cxx_record_decl) {
7052 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7053
7054 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7055
7056 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7057 // Didn't find things easily, lets let clang do its thang...
7058 clang::IdentifierInfo &ident_ref =
7059 getASTContext()->Idents.get(name_sref);
7060 clang::DeclarationName decl_name(&ident_ref);
7061
7062 clang::CXXBasePaths paths;
7063 if (cxx_record_decl->lookupInBases(
7064 [decl_name](const clang::CXXBaseSpecifier *specifier,
7065 clang::CXXBasePath &path) {
7066 return clang::CXXRecordDecl::FindOrdinaryMember(
7067 specifier, path, decl_name);
7068 },
7069 paths)) {
7070 clang::CXXBasePaths::const_paths_iterator path,
7071 path_end = paths.end();
7072 for (path = paths.begin(); path != path_end; ++path) {
7073 const size_t num_path_elements = path->size();
7074 for (size_t e = 0; e < num_path_elements; ++e) {
7075 clang::CXXBasePathElement elem = (*path)[e];
7076
7077 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7078 omit_empty_base_classes);
7079 if (child_idx == UINT32_MAX(4294967295U)) {
7080 child_indexes.clear();
7081 return 0;
7082 } else {
7083 child_indexes.push_back(child_idx);
7084 parent_record_decl = llvm::cast<clang::RecordDecl>(
7085 elem.Base->getType()
7086 ->getAs<clang::RecordType>()
7087 ->getDecl());
7088 }
7089 }
7090 for (clang::NamedDecl *path_decl : path->Decls) {
7091 child_idx = GetIndexForRecordChild(
7092 parent_record_decl, path_decl, omit_empty_base_classes);
7093 if (child_idx == UINT32_MAX(4294967295U)) {
7094 child_indexes.clear();
7095 return 0;
7096 } else {
7097 child_indexes.push_back(child_idx);
7098 }
7099 }
7100 }
7101 return child_indexes.size();
7102 }
7103 }
7104 }
7105 break;
7106
7107 case clang::Type::ObjCObject:
7108 case clang::Type::ObjCInterface:
7109 if (GetCompleteType(type)) {
7110 llvm::StringRef name_sref(name);
7111 const clang::ObjCObjectType *objc_class_type =
7112 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7113 assert(objc_class_type)(static_cast <bool> (objc_class_type) ? void (0) : __assert_fail
("objc_class_type", "/build/llvm-toolchain-snapshot-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 7113, __extension__ __PRETTY_FUNCTION__))
;
7114 if (objc_class_type) {
7115 uint32_t child_idx = 0;
7116 clang::ObjCInterfaceDecl *class_interface_decl =
7117 objc_class_type->getInterface();
7118
7119 if (class_interface_decl) {
7120 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7121 ivar_end = class_interface_decl->ivar_end();
7122 clang::ObjCInterfaceDecl *superclass_interface_decl =
7123 class_interface_decl->getSuperClass();
7124
7125 for (ivar_pos = class_interface_decl->ivar_begin();
7126 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7127 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7128
7129 if (ivar_decl->getName().equals(name_sref)) {
7130 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7131 (omit_empty_base_classes &&
7132 ObjCDeclHasIVars(superclass_interface_decl, true)))
7133 ++child_idx;
7134
7135 child_indexes.push_back(child_idx);
7136 return child_indexes.size();
7137 }
7138 }
7139
7140 if (superclass_interface_decl) {
7141 // The super class index is always zero for ObjC classes,
7142 // so we push it onto the child indexes in case we find
7143 // an ivar in our superclass...
7144 child_indexes.push_back(0);
7145
7146 CompilerType superclass_clang_type(
7147 getASTContext(), getASTContext()->getObjCInterfaceType(
7148 superclass_interface_decl));
7149 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7150 name, omit_empty_base_classes, child_indexes)) {
7151 // We did find an ivar in a superclass so just
7152 // return the results!
7153 return child_indexes.size();
7154 }
7155
7156 // We didn't find an ivar matching "name" in our
7157 // superclass, pop the superclass zero index that
7158 // we pushed on above.
7159 child_indexes.pop_back();
7160 }
7161 }
7162 }
7163 }
7164 break;
7165
7166 case clang::Type::ObjCObjectPointer: {
7167 CompilerType objc_object_clang_type(
7168 getASTContext(),
7169 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7170 ->getPointeeType());
7171 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7172 name, omit_empty_base_classes, child_indexes);
7173 } break;
7174
7175 case clang::Type::ConstantArray: {
7176 // const clang::ConstantArrayType *array =
7177 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7178 // const uint64_t element_count =
7179 // array->getSize().getLimitedValue();
7180 //
7181 // if (idx < element_count)
7182 // {
7183 // std::pair<uint64_t, unsigned> field_type_info =
7184 // ast->getTypeInfo(array->getElementType());
7185 //
7186 // char element_name[32];
7187 // ::snprintf (element_name, sizeof (element_name),
7188 // "%s[%u]", parent_name ? parent_name : "", idx);
7189 //
7190 // child_name.assign(element_name);
7191 // assert(field_type_info.first % 8 == 0);
7192 // child_byte_size = field_type_info.first / 8;
7193 // child_byte_offset = idx * child_byte_size;
7194 // return array->getElementType().getAsOpaquePtr();
7195 // }
7196 } break;
7197
7198 // case clang::Type::MemberPointerType:
7199 // {
7200 // MemberPointerType *mem_ptr_type =
7201 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7202 // clang::QualType pointee_type =
7203 // mem_ptr_type->getPointeeType();
7204 //
7205 // if (ClangASTContext::IsAggregateType
7206 // (pointee_type.getAsOpaquePtr()))
7207 // {
7208 // return GetIndexOfChildWithName (ast,
7209 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7210 // name);
7211 // }
7212 // }
7213 // break;
7214 //
7215 case clang::Type::LValueReference:
7216 case clang::Type::RValueReference: {
7217 const clang::ReferenceType *reference_type =
7218 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7219 clang::QualType pointee_type(reference_type->getPointeeType());
7220 CompilerType pointee_clang_type(getASTContext(), pointee_type);
7221
7222 if (pointee_clang_type.IsAggregateType()) {
7223 return pointee_clang_type.GetIndexOfChildMemberWithName(
7224 name, omit_empty_base_classes, child_indexes);
7225 }
7226 } break;
7227
7228 case clang::Type::Pointer: {
7229 CompilerType pointee_clang_type(GetPointeeType(type));
7230
7231 if (pointee_clang_type.IsAggregateType()) {
7232 return pointee_clang_type.GetIndexOfChildMemberWithName(
7233 name, omit_empty_base_classes, child_indexes);
7234 }
7235 } break;
7236
7237 case clang::Type::Typedef:
7238 return CompilerType(getASTContext(),
7239 llvm::cast<clang::TypedefType>(qual_type)
7240 ->getDecl()
7241 ->getUnderlyingType())
7242 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7243 child_indexes);
7244
7245 case clang::Type::Auto:
7246 return CompilerType(
7247 getASTContext(),
7248 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7249 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7250 child_indexes);
7251
7252 case clang::Type::Elaborated:
7253 return CompilerType(
7254 getASTContext(),
7255 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7256 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7257 child_indexes);
7258
7259 case clang::Type::Paren:
7260 return CompilerType(getASTContext(),
7261 llvm::cast<clang::ParenType>(qual_type)->desugar())
7262 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7263 child_indexes);
7264
7265 default:
7266 break;
7267 }
7268 }
7269 return 0;
7270}
7271
7272// Get the index of the child of "clang_type" whose name matches. This function
7273// doesn't descend into the children, but only looks one level deep and name
7274// matches can include base class names.
7275
7276uint32_t
7277ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7278 const char *name,
7279 bool omit_empty_base_classes) {
7280 if (type && name && name[0]) {
7281 clang::QualType qual_type(GetCanonicalQualType(type));
7282
7283 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7284
7285 switch (type_class) {
7286 case clang::Type::Record:
7287 if (GetCompleteType(type)) {
7288 const clang::RecordType *record_type =
7289 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7290 const clang::RecordDecl *record_decl = record_type->getDecl();
7291
7292 assert(record_decl)(static_cast <bool> (record_decl) ? void (0) : __assert_fail
("record_decl", "/build/llvm-toolchain-snapshot-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 7292, __extension__ __PRETTY_FUNCTION__))
;
7293 uint32_t child_idx = 0;
7294
7295 const clang::CXXRecordDecl *cxx_record_decl =
7296 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7297
7298 if (cxx_record_decl) {
7299 clang::CXXRecordDecl::base_class_const_iterator base_class,
7300 base_class_end;
7301 for (base_class = cxx_record_decl->bases_begin(),
7302 base_class_end = cxx_record_decl->bases_end();
7303 base_class != base_class_end; ++base_class) {
7304 // Skip empty base classes
7305 clang::CXXRecordDecl *base_class_decl =
7306 llvm::cast<clang::CXXRecordDecl>(
7307 base_class->getType()
7308 ->getAs<clang::RecordType>()
7309 ->getDecl());
7310 if (omit_empty_base_classes &&
7311 ClangASTContext::RecordHasFields(base_class_decl) == false)
7312 continue;
7313
7314 CompilerType base_class_clang_type(getASTContext(),
7315 base_class->getType());
7316 std::string base_class_type_name(
7317 base_class_clang_type.GetTypeName().AsCString(""));
7318 if (base_class_type_name.compare(name) == 0)
7319 return child_idx;
7320 ++child_idx;
7321 }
7322 }
7323
7324 // Try and find a field that matches NAME
7325 clang::RecordDecl::field_iterator field, field_end;
7326 llvm::StringRef name_sref(name);
7327 for (field = record_decl->field_begin(),
7328 field_end = record_decl->field_end();
7329 field != field_end; ++field, ++child_idx) {
7330 if (field->getName().equals(name_sref))
7331 return child_idx;
7332 }
7333 }
7334 break;
7335
7336 case clang::Type::ObjCObject:
7337 case clang::Type::ObjCInterface:
7338 if (GetCompleteType(type)) {
7339 llvm::StringRef name_sref(name);
7340 const clang::ObjCObjectType *objc_class_type =
7341 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7342 assert(objc_class_type)(static_cast <bool> (objc_class_type) ? void (0) : __assert_fail
("objc_class_type", "/build/llvm-toolchain-snapshot-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 7342, __extension__ __PRETTY_FUNCTION__))
;
7343 if (objc_class_type) {
7344 uint32_t child_idx = 0;
7345 clang::ObjCInterfaceDecl *class_interface_decl =
7346 objc_class_type->getInterface();
7347
7348 if (class_interface_decl) {
7349 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7350 ivar_end = class_interface_decl->ivar_end();
7351 clang::ObjCInterfaceDecl *superclass_interface_decl =
7352 class_interface_decl->getSuperClass();
7353
7354 for (ivar_pos = class_interface_decl->ivar_begin();
7355 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7356 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7357
7358 if (ivar_decl->getName().equals(name_sref)) {
7359 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7360 (omit_empty_base_classes &&
7361 ObjCDeclHasIVars(superclass_interface_decl, true)))
7362 ++child_idx;
7363
7364 return child_idx;
7365 }
7366 }
7367
7368 if (superclass_interface_decl) {
7369 if (superclass_interface_decl->getName().equals(name_sref))
7370 return 0;
7371 }
7372 }
7373 }
7374 }
7375 break;
7376
7377 case clang::Type::ObjCObjectPointer: {
7378 CompilerType pointee_clang_type(
7379 getASTContext(),
7380 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7381 ->getPointeeType());
7382 return pointee_clang_type.GetIndexOfChildWithName(
7383 name, omit_empty_base_classes);
7384 } break;
7385
7386 case clang::Type::ConstantArray: {
7387 // const clang::ConstantArrayType *array =
7388 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7389 // const uint64_t element_count =
7390 // array->getSize().getLimitedValue();
7391 //
7392 // if (idx < element_count)
7393 // {
7394 // std::pair<uint64_t, unsigned> field_type_info =
7395 // ast->getTypeInfo(array->getElementType());
7396 //
7397 // char element_name[32];
7398 // ::snprintf (element_name, sizeof (element_name),
7399 // "%s[%u]", parent_name ? parent_name : "", idx);
7400 //
7401 // child_name.assign(element_name);
7402 // assert(field_type_info.first % 8 == 0);
7403 // child_byte_size = field_type_info.first / 8;
7404 // child_byte_offset = idx * child_byte_size;
7405 // return array->getElementType().getAsOpaquePtr();
7406 // }
7407 } break;
7408
7409 // case clang::Type::MemberPointerType:
7410 // {
7411 // MemberPointerType *mem_ptr_type =
7412 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7413 // clang::QualType pointee_type =
7414 // mem_ptr_type->getPointeeType();
7415 //
7416 // if (ClangASTContext::IsAggregateType
7417 // (pointee_type.getAsOpaquePtr()))
7418 // {
7419 // return GetIndexOfChildWithName (ast,
7420 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7421 // name);
7422 // }
7423 // }
7424 // break;
7425 //
7426 case clang::Type::LValueReference:
7427 case clang::Type::RValueReference: {
7428 const clang::ReferenceType *reference_type =
7429 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7430 CompilerType pointee_type(getASTContext(),
7431 reference_type->getPointeeType());
7432
7433 if (pointee_type.IsAggregateType()) {
7434 return pointee_type.GetIndexOfChildWithName(name,
7435 omit_empty_base_classes);
7436 }
7437 } break;
7438
7439 case clang::Type::Pointer: {
7440 const clang::PointerType *pointer_type =
7441 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7442 CompilerType pointee_type(getASTContext(),
7443 pointer_type->getPointeeType());
7444
7445 if (pointee_type.IsAggregateType()) {
7446 return pointee_type.GetIndexOfChildWithName(name,
7447 omit_empty_base_classes);
7448 } else {
7449 // if (parent_name)
7450 // {
7451 // child_name.assign(1, '*');
7452 // child_name += parent_name;
7453 // }
7454 //
7455 // // We have a pointer to an simple type
7456 // if (idx == 0)
7457 // {
7458 // std::pair<uint64_t, unsigned> clang_type_info
7459 // = ast->getTypeInfo(pointee_type);
7460 // assert(clang_type_info.first % 8 == 0);
7461 // child_byte_size = clang_type_info.first / 8;
7462 // child_byte_offset = 0;
7463 // return pointee_type.getAsOpaquePtr();
7464 // }
7465 }
7466 } break;
7467
7468 case clang::Type::Auto:
7469 return CompilerType(
7470 getASTContext(),
7471 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7472 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7473
7474 case clang::Type::Elaborated:
7475 return CompilerType(
7476 getASTContext(),
7477 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7478 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7479
7480 case clang::Type::Paren:
7481 return CompilerType(getASTContext(),
7482 llvm::cast<clang::ParenType>(qual_type)->desugar())
7483 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7484
7485 case clang::Type::Typedef:
7486 return CompilerType(getASTContext(),
7487 llvm::cast<clang::TypedefType>(qual_type)
7488 ->getDecl()
7489 ->getUnderlyingType())
7490 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7491
7492 default:
7493 break;
7494 }
7495 }
7496 return UINT32_MAX(4294967295U);
7497}
7498
7499size_t
7500ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7501 if (!type)
7502 return 0;
7503
7504 clang::QualType qual_type(GetCanonicalQualType(type));
7505 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7506 switch (type_class) {
7507 case clang::Type::Record:
7508 if (GetCompleteType(type)) {
7509 const clang::CXXRecordDecl *cxx_record_decl =
7510 qual_type->getAsCXXRecordDecl();
7511 if (cxx_record_decl) {
7512 const clang::ClassTemplateSpecializationDecl *template_decl =
7513 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7514 cxx_record_decl);
7515 if (template_decl)
7516 return template_decl->getTemplateArgs().size();
7517 }
7518 }
7519 break;
7520
7521 case clang::Type::Typedef:
7522 return (CompilerType(getASTContext(),
7523 llvm::cast<clang::TypedefType>(qual_type)
7524 ->getDecl()
7525 ->getUnderlyingType()))
7526 .GetNumTemplateArguments();
7527
7528 case clang::Type::Auto:
7529 return (CompilerType(
7530 getASTContext(),
7531 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7532 .GetNumTemplateArguments();
7533
7534 case clang::Type::Elaborated:
7535 return (CompilerType(
7536 getASTContext(),
7537 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7538 .GetNumTemplateArguments();
7539
7540 case clang::Type::Paren:
7541 return (CompilerType(getASTContext(),
7542 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7543 .GetNumTemplateArguments();
7544
7545 default:
7546 break;
7547 }
7548
7549 return 0;
7550}
7551
7552const clang::ClassTemplateSpecializationDecl *
7553ClangASTContext::GetAsTemplateSpecialization(
7554 lldb::opaque_compiler_type_t type) {
7555 if (!type)
7556 return nullptr;
7557
7558 clang::QualType qual_type(GetCanonicalQualType(type));
7559 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7560 switch (type_class) {
7561 case clang::Type::Record: {
7562 if (! GetCompleteType(type))
7563 return nullptr;
7564 const clang::CXXRecordDecl *cxx_record_decl =
7565 qual_type->getAsCXXRecordDecl();
7566 if (!cxx_record_decl)
7567 return nullptr;
7568 return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7569 cxx_record_decl);
7570 }
7571
7572 case clang::Type::Typedef:
7573 return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type)
7574 ->getDecl()
7575 ->getUnderlyingType()
7576 .getAsOpaquePtr());
7577
7578 case clang::Type::Auto:
7579 return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type)
7580 ->getDeducedType()
7581 .getAsOpaquePtr());
7582
7583 case clang::Type::Elaborated:
7584 return GetAsTemplateSpecialization(
7585 llvm::cast<clang::ElaboratedType>(qual_type)
7586 ->getNamedType()
7587 .getAsOpaquePtr());
7588
7589 case clang::Type::Paren:
7590 return GetAsTemplateSpecialization(
7591 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
7592
7593 default:
7594 return nullptr;
7595 }
7596}
7597
7598lldb::TemplateArgumentKind
7599ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
7600 size_t arg_idx) {
7601 const clang::ClassTemplateSpecializationDecl *template_decl =
7602 GetAsTemplateSpecialization(type);
7603 if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size())
7604 return eTemplateArgumentKindNull;
7605
7606 switch (template_decl->getTemplateArgs()[arg_idx].getKind()) {
7607 case clang::TemplateArgument::Null:
7608 return eTemplateArgumentKindNull;
7609
7610 case clang::TemplateArgument::NullPtr:
7611 return eTemplateArgumentKindNullPtr;
7612
7613 case clang::TemplateArgument::Type:
7614 return eTemplateArgumentKindType;
7615
7616 case clang::TemplateArgument::Declaration:
7617 return eTemplateArgumentKindDeclaration;
7618
7619 case clang::TemplateArgument::Integral:
7620 return eTemplateArgumentKindIntegral;
7621
7622 case clang::TemplateArgument::Template:
7623 return eTemplateArgumentKindTemplate;
7624
7625 case clang::TemplateArgument::TemplateExpansion:
7626 return eTemplateArgumentKindTemplateExpansion;
7627
7628 case clang::TemplateArgument::Expression:
7629 return eTemplateArgumentKindExpression;
7630
7631 case clang::TemplateArgument::Pack:
7632 return eTemplateArgumentKindPack;
7633 }
7634 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind")::llvm::llvm_unreachable_internal("Unhandled clang::TemplateArgument::ArgKind"
, "/build/llvm-toolchain-snapshot-6.0~svn318882/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 7634)
;
7635}
7636
7637CompilerType
7638ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
7639 size_t idx) {
7640 const clang::ClassTemplateSpecializationDecl *template_decl =
7641 GetAsTemplateSpecialization(type);
7642 if (!template_decl || idx >= template_decl->getTemplateArgs().size())
7643 return CompilerType();
7644
7645 const clang::TemplateArgument &template_arg =
7646 template_decl->getTemplateArgs()[idx];
7647 if (template_arg.getKind() != clang::TemplateArgument::Type)
7648 return CompilerType();
7649
7650 return CompilerType(getASTContext(), template_arg.getAsType());
7651}
7652
7653std::pair<llvm::APSInt, CompilerType>
7654ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
7655 size_t idx) {
7656 const clang::ClassTemplateSpecializationDecl *template_decl =
7657 GetAsTemplateSpecialization(type);
7658 if (! template_decl || idx >= template_decl->getTemplateArgs().size())
7659 return {llvm::APSInt(0), CompilerType()};
7660
7661 const clang::TemplateArgument &template_arg =
7662 template_decl->getTemplateArgs()[idx];
7663 if (template_arg.getKind() != clang::TemplateArgument::Integral)
7664 return {llvm::APSInt(0), CompilerType()};
7665
7666 return {template_arg.getAsIntegral(),
7667 CompilerType(getASTContext(), template_arg.getIntegralType())};
7668}
7669
7670CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7671 if (type)
7672 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7673 return CompilerType();
7674}
7675
7676clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7677 const clang::EnumType *enutype =
7678 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7679 if (enutype)
7680 return enutype->getDecl();
7681 return NULL__null;
7682}
7683
7684clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7685 const clang::RecordType *record_type =
7686 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7687 if (record_type)
7688 return record_type->getDecl();
7689 return nullptr;
7690}
7691
7692clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
7693 clang::QualType qual_type = ClangUtil::GetCanonicalQualType(type);
7694 if (qual_type.isNull())
7695 return nullptr;
7696 else
7697 return qual_type->getAsTagDecl();
7698}
7699
7700clang::CXXRecordDecl *
7701ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7702 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
7703}
7704
7705clang::ObjCInterfaceDecl *
7706ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7707 const clang::ObjCObjectType *objc_class_type =
7708 llvm::dyn_cast<clang::ObjCObjectType>(
7709 ClangUtil::GetCanonicalQualType(type));
7710 if (objc_class_type)
7711 return objc_class_type->getInterface();
7712 return nullptr;
7713}
7714
7715clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
7716 const CompilerType &type, const char *name,
7717 const CompilerType &field_clang_type, AccessType access,
7718 uint32_t bitfield_bit_size) {
7719 if (!type.IsValid() || !field_clang_type.IsValid())
7720 return nullptr;
7721 ClangASTContext *ast =
7722 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7723 if (!ast)
7724 return nullptr;
7725 clang::ASTContext *clang_ast = ast->getASTContext();
7726
7727 clang::FieldDecl *field = nullptr;
7728
7729 clang::Expr *bit_width = nullptr;
7730 if (bitfield_bit_size != 0) {
7731 llvm::APInt bitfield_bit_size_apint(
7732 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7733 bit_width = new (*clang_ast)
7734 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7735 clang_ast->IntTy, clang::SourceLocation());
7736 }
7737
7738 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7739 if (record_decl) {
7740 field = clang::FieldDecl::Create(
7741 *clang_ast, record_decl, clang::SourceLocation(),
7742 clang::SourceLocation(),
7743 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7744 ClangUtil::GetQualType(field_clang_type), // Field type
7745 nullptr, // TInfo *
7746 bit_width, // BitWidth
7747 false, // Mutable
7748 clang::ICIS_NoInit); // HasInit
7749
7750 if (!name) {
7751 // Determine whether this field corresponds to an anonymous
7752 // struct or union.
7753 if (const clang::TagType *TagT =
7754 field->getType()->getAs<clang::TagType>()) {
7755 if (clang::RecordDecl *Rec =
7756 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7757 if (!Rec->getDeclName()) {
7758 Rec->setAnonymousStructOrUnion(true);
7759 field->setImplicit();
7760 }
7761 }
7762 }
7763
7764 if (field) {
7765 field->setAccess(
7766 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7767
7768 record_decl->addDecl(field);
7769
7770#ifdef LLDB_CONFIGURATION_DEBUG
7771 VerifyDecl(field);
7772#endif
7773 }
7774 } else {
7775 clang::ObjCInterfaceDecl *class_interface_decl =
7776 ast->GetAsObjCInterfaceDecl(type);
7777
7778 if (class_interface_decl) {
7779 const bool is_synthesized = false;
7780
7781 field_clang_type.GetCompleteType();
7782
7783 field = clang::ObjCIvarDecl::Create(
7784 *clang_ast, class_interface_decl, clang::SourceLocation(),
7785 clang::SourceLocation(),
7786 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7787 ClangUtil::GetQualType(field_clang_type), // Field type
7788 nullptr, // TypeSourceInfo *
7789 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7790 is_synthesized);
7791
7792 if (field) {
7793 class_interface_decl->addDecl(field);
7794
7795#ifdef LLDB_CONFIGURATION_DEBUG
7796 VerifyDecl(field);
7797#endif
7798 }
7799 }
7800 }
7801 return field;
7802}
7803
7804void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7805 if (!type)
7806 return;
7807
7808 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7809 if (!ast)
7810 return;
7811
7812 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7813
7814 if (!record_decl)
7815 return;
7816
7817 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7818
7819 IndirectFieldVector indirect_fields;
7820 clang::RecordDecl::field_iterator field_pos;
7821 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7822 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7823 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7824 last_field_pos = field_pos++) {
7825 if (field_pos->isAnonymousStructOrUnion()) {
7826 clang::QualType field_qual_type = field_pos->getType();
7827
7828 const clang::RecordType *field_record_type =
7829 field_qual_type->getAs<clang::RecordType>();
7830
7831 if (!field_record_type)
7832 continue;
7833
7834 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7835
7836 if (!field_record_decl)
7837 continue;
7838
7839 for (clang::RecordDecl::decl_iterator
7840 di = field_record_decl->decls_begin(),
7841 de = field_record_decl->decls_end();
7842 di != de; ++di) {
7843 if (clang::FieldDecl *nested_field_decl =
7844 llvm::dyn_cast<clang::FieldDecl>(*di)) {
7845 clang::NamedDecl **chain =
7846 new (*ast->getASTContext()) clang::NamedDecl *[2];
7847 chain[0] = *field_pos;
7848 chain[1] = nested_field_decl;
7849 clang::IndirectFieldDecl *indirect_field =
7850 clang::IndirectFieldDecl::Create(
7851 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7852 nested_field_decl->getIdentifier(),
7853 nested_field_decl->getType(), {chain, 2});
7854
7855 indirect_field->setImplicit();
7856
7857 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7858 field_pos->getAccess(), nested_field_decl->getAccess()));
7859
7860 indirect_fields.push_back(indirect_field);
7861 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
7862 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
7863 size_t nested_chain_size =
7864 nested_indirect_field_decl->getChainingSize();
7865 clang::NamedDecl **chain = new (*ast->getASTContext())
7866 clang::NamedDecl *[nested_chain_size + 1];
7867 chain[0] = *field_pos;
7868
7869 int chain_index = 1;
7870 for (clang::IndirectFieldDecl::chain_iterator
7871 nci = nested_indirect_field_decl->chain_begin(),
7872 nce = nested_indirect_field_decl->chain_end();
7873 nci < nce; ++nci) {
7874 chain[chain_index] = *nci;
7875 chain_index++;
7876 }
7877
7878 clang::IndirectFieldDecl *indirect_field =
7879 clang::IndirectFieldDecl::Create(
7880 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7881 nested_indirect_field_decl->getIdentifier(),
7882 nested_indirect_field_decl->getType(),
7883 {chain, nested_chain_size + 1});
7884
7885 indirect_field->setImplicit();
7886
7887 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7888 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
7889
7890 indirect_fields.push_back(indirect_field);
7891 }
7892 }
7893 }
7894 }
7895
7896 // Check the last field to see if it has an incomplete array type as its
7897 // last member and if it does, the tell the record decl about it
7898 if (last_field_pos != field_end_pos) {
7899 if (last_field_pos->getType()->isIncompleteArrayType())
7900 record_decl->hasFlexibleArrayMember();
7901 }
7902
7903 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
7904 ife = indirect_fields.end();
7905 ifi < ife; ++ifi) {
7906 record_decl->addDecl(*ifi);
7907 }
7908}
7909
7910void ClangASTContext::SetIsPacked(const CompilerType &type) {
7911 if (type) {
7912 ClangASTContext *ast =
7913 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7914 if (ast) {
7915 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
7916
7917 if (!record_decl)
7918 return;
7919
7920 record_decl->addAttr(
7921 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
7922 }
7923 }
7924}
7925
7926clang::VarDecl *ClangASTContext::AddVariableToRecordType(
7927 const CompilerType &type, const char *name, const CompilerType &var_type,
7928 AccessType access) {
7929 clang::VarDecl *var_decl = nullptr;
7930
7931 if (!type.IsValid() || !var_type.IsValid())
7932 return nullptr;
7933 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7934 if (!ast)
7935 return nullptr;
7936
7937 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7938 if (record_decl) {
7939 var_decl = clang::VarDecl::Create(
7940 *ast->getASTContext(), // ASTContext &
7941 record_decl, // DeclContext *
7942 clang::SourceLocation(), // clang::SourceLocation StartLoc
7943 clang::SourceLocation(), // clang::SourceLocation IdLoc
7944 name ? &ast->getASTContext()->Idents.get(name)
7945 : nullptr, // clang::IdentifierInfo *
7946 ClangUtil::GetQualType(var_type), // Variable clang::QualType
7947 nullptr, // TypeSourceInfo *
7948 clang::SC_Static); // StorageClass
7949 if (var_decl) {
7950 var_decl->setAccess(
7951 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7952 record_decl->addDecl(var_decl);
7953
7954#ifdef LLDB_CONFIGURATION_DEBUG
7955 VerifyDecl(var_decl);
7956#endif
7957 }
7958 }
7959 return var_decl;
7960}
7961
7962clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
7963 lldb::opaque_compiler_type_t type, const char *name,
7964 const CompilerType &method_clang_type, lldb::AccessType access,
7965 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
7966 bool is_attr_used, bool is_artificial) {
7967 if (!type || !method_clang_type.IsValid() || name == nullptr ||
7968 name[0] == '\0')
7969 return nullptr;
7970
7971 clang::QualType record_qual_type(GetCanonicalQualType(type));
7972
7973 clang::CXXRecordDecl *cxx_record_decl =
7974 record_qual_type->getAsCXXRecordDecl();
7975
7976 if (cxx_record_decl == nullptr)
7977 return nullptr;
7978
7979 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
7980
7981 clang::CXXMethodDecl *cxx_method_decl = nullptr;
7982
7983 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
7984
7985 const clang::FunctionType *function_type =
7986 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
7987
7988 if (function_type == nullptr)
7989 return nullptr;
7990
7991 const clang::FunctionProtoType *method_function_prototype(
7992 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
7993
7994 if (!method_function_prototype)
7995 return nullptr;
7996
7997 unsigned int num_params = method_function_prototype->getNumParams();
7998
7999 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
8000 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
8001
8002 if (is_artificial)
8003 return nullptr; // skip everything artificial
8004
8005 if (name[0] == '~') {
8006 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
8007 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8008 clang::DeclarationNameInfo(
8009 getASTContext()->DeclarationNames.getCXXDestructorName(
8010 getASTContext()->getCanonicalType(record_qual_type)),
8011 clang::SourceLocation()),
8012 method_qual_type, nullptr, is_inline, is_artificial);
8013 cxx_method_decl = cxx_dtor_decl;
8014 } else if (decl_name == cxx_record_decl->getDeclName()) {
8015 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
8016 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8017 clang::DeclarationNameInfo(
8018 getASTContext()->DeclarationNames.getCXXConstructorName(
8019 getASTContext()->getCanonicalType(record_qual_type)),
8020 clang::SourceLocation()),
8021 method_qual_type,
8022 nullptr, // TypeSourceInfo *
8023 is_explicit, is_inline, is_artificial, false /*is_constexpr*/);
8024 cxx_method_decl = cxx_ctor_decl;
8025 } else {
8026 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
8027 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
8028
8029 if (IsOperator(name, op_kind)) {
8030 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
8031 // Check the number of operator parameters. Sometimes we have
8032 // seen bad DWARF that doesn't correctly describe operators and
8033 // if we try to create a method and add it to the class, clang
8034 // will assert and crash, so we need to make sure things are
8035 // acceptable.
8036 const bool is_method = true;
8037 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
8038 is_method, op_kind, num_params))
8039 return nullptr;
8040 cxx_method_decl = clang::CXXMethodDecl::Create(
8041 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8042 clang::DeclarationNameInfo(
8043 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
8044 clang::SourceLocation()),
8045 method_qual_type,
8046 nullptr, // TypeSourceInfo *
8047 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
8048 } else if (num_params == 0) {
8049 // Conversion operators don't take params...
8050 cxx_method_decl = clang::CXXConversionDecl::Create(
8051 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8052 clang::DeclarationNameInfo(
8053 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
8054 getASTContext()->getCanonicalType(
8055 function_type->getReturnType())),
8056 clang::SourceLocation()),
8057 method_qual_type,
8058 nullptr, // TypeSourceInfo *
8059 is_inline, is_explicit, false /*is_constexpr*/,
8060 clang::SourceLocation());
8061 }
8062 }
8063
8064 if (cxx_method_decl == nullptr) {
8065 cxx_method_decl = clang::CXXMethodDecl::Create(
8066 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8067 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8068 method_qual_type,
8069 nullptr, // TypeSourceInfo *
8070 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
8071 }
8072 }
8073
8074 clang::AccessSpecifier access_specifier =
8075 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8076
8077 cxx_method_decl->setAccess(access_specifier);
8078 cxx_method_decl->setVirtualAsWritten(is_virtual);
8079
8080 if (is_attr_used)
8081 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8082
8083 // Populate the method decl with parameter decls
8084
8085 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8086
8087 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8088 params.push_back(clang::ParmVarDecl::Create(
8089 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8090 clang::SourceLocation(),
8091 nullptr, // anonymous
8092 method_function_prototype->getParamType(param_index), nullptr,
8093 clang::SC_None, nullptr));
8094 }
8095
8096 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8097
8098 cxx_record_decl->addDecl(cxx_method_decl);
8099
8100 // Sometimes the debug info will mention a constructor (default/copy/move),
8101 // destructor, or assignment operator (copy/move) but there won't be any
8102 // version of this in the code. So we check if the function was artificially
8103 // generated and if it is trivial and this lets the compiler/backend know
8104 // that it can inline the IR for these when it needs to and we can avoid a
8105 // "missing function" error when running expressions.
8106
8107 if (is_artificial) {
8108 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8109 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8110 (cxx_ctor_decl->isCopyConstructor() &&
8111 cxx_record_decl->hasTrivialCopyConstructor()) ||
8112 (cxx_ctor_decl->isMoveConstructor() &&
8113 cxx_record_decl->hasTrivialMoveConstructor()))) {
8114 cxx_ctor_decl->setDefaulted();
8115 cxx_ctor_decl->setTrivial(true);
8116 } else if (cxx_dtor_decl) {
8117 if (cxx_record_decl->hasTrivialDestructor()) {
8118 cxx_dtor_decl->setDefaulted();
8119 cxx_dtor_decl->setTrivial(true);
8120 }
8121 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8122 cxx_record_decl->hasTrivialCopyAssignment()) ||
8123 (cxx_method_decl->isMoveAssignmentOperator() &&
8124 cxx_record_decl->hasTrivialMoveAssignment())) {
8125 cxx_method_decl->setDefaulted();
8126 cxx_method_decl->setTrivial(true);
8127 }
8128 }
8129
8130#ifdef LLDB_CONFIGURATION_DEBUG
8131 VerifyDecl(cxx_method_decl);
8132#endif
8133
8134 // printf ("decl->isPolymorphic() = %i\n",
8135 // cxx_record_decl->isPolymorphic());
8136 // printf ("decl->isAggregate() = %i\n",
8137 // cxx_record_decl->isAggregate());
8138 // printf ("decl->isPOD() = %i\n",
8139 // cxx_record_decl->isPOD());
8140 // printf ("decl->isEmpty() = %i\n",
8141 // cxx_record_decl->isEmpty());
8142 // printf ("decl->isAbstract() = %i\n",
8143 // cxx_record_decl->isAbstract());
8144 // printf ("decl->hasTrivialConstructor() = %i\n",
8145 // cxx_record_decl->hasTrivialConstructor());
8146 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8147 // cxx_record_decl->hasTrivialCopyConstructor());
8148 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8149 // cxx_record_decl->hasTrivialCopyAssignment());
8150 // printf ("decl->hasTrivialDestructor() = %i\n",
8151 // cxx_record_decl->hasTrivialDestructor());
8152 return cxx_method_decl;
8153}
8154
8155#pragma mark C++ Base Classes
8156
8157clang::CXXBaseSpecifier *
8158ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8159 AccessType access, bool is_virtual,
8160 bool base_of_class) {
8161 if (type)
8162 return new clang::CXXBaseSpecifier(
8163 clang::SourceRange(), is_virtual, base_of_class,
8164 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8165 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8166 clang::SourceLocation());
8167 return nullptr;
8168}
8169
8170void ClangASTContext::DeleteBaseClassSpecifiers(
8171 clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes) {
8172 for (unsigned i = 0; i < num_base_classes; ++i) {
8173 delete base_classes[i];
8174 base_classes[i] = nullptr;
8175 }
8176}
8177
8178bool ClangASTContext::SetBaseClassesForClassType(
8179 lldb::opaque_compiler_type_t type,
8180 clang::CXXBaseSpecifier const *const *base_classes,
8181 unsigned num_base_classes) {
8182 if (type) {
8183 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8184 if (cxx_record_decl) {
8185 cxx_record_decl->setBases(base_classes, num_base_classes);
8186 return true;
8187 }
8188 }
8189 return false;
8190}
8191
8192bool ClangASTContext::SetObjCSuperClass(
8193 const CompilerType &type, const CompilerType &superclass_clang_type) {
8194 ClangASTContext *ast =
8195 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8196 if (!ast)
8197 return false;
8198 clang::ASTContext *clang_ast = ast->getASTContext();
8199
8200 if (type && superclass_clang_type.IsValid() &&
8201 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8202 clang::ObjCInterfaceDecl *class_interface_decl =
8203 GetAsObjCInterfaceDecl(type);
8204 clang::ObjCInterfaceDecl *super_interface_decl =
8205 GetAsObjCInterfaceDecl(superclass_clang_type);
8206 if (class_interface_decl && super_interface_decl) {
8207 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8208 clang_ast->getObjCInterfaceType(super_interface_decl)));
8209 return true;
8210 }
8211 }
8212 return false;
8213}
8214
8215bool ClangASTContext::AddObjCClassProperty(
8216 const CompilerType &type, const char *property_name,
8217 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8218 const char *property_setter_name, const char *property_getter_name,
8219 uint32_t property_attributes, ClangASTMetadata *metadata) {
8220 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8221 property_name[0] == '\0')
8222 return false;
8223 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8224 if (!ast)
8225 return false;
8226 clang::ASTContext *clang_ast = ast->getASTContext();
8227
8228 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8229
8230 if (class_interface_decl) {
8231 CompilerType property_clang_type_to_access;
8232
8233 if (property_clang_type.IsValid())
8234 property_clang_type_to_access = property_clang_type;
8235 else if (ivar_decl)
8236 property_clang_type_to_access =
8237 CompilerType(clang_ast, ivar_decl->getType());
8238
8239 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8240 clang::TypeSourceInfo *prop_type_source;
8241 if (ivar_decl)
8242 prop_type_source =
8243 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8244 else
8245 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8246 ClangUtil::GetQualType(property_clang_type));
8247
8248 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8249 *clang_ast, class_interface_decl,
8250 clang::SourceLocation(), // Source Location
8251 &clang_ast->Idents.get(property_name),
8252 clang::SourceLocation(), // Source Location for AT
8253 clang::SourceLocation(), // Source location for (
8254 ivar_decl ? ivar_decl->getType()
8255 : ClangUtil::GetQualType(property_clang_type),
8256 prop_type_source);
8257
8258 if (property_decl) {
8259 if (metadata)
8260 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8261
8262 class_interface_decl->addDecl(property_decl);
8263
8264 clang::Selector setter_sel, getter_sel;
8265
8266 if (property_setter_name != nullptr) {
8267 std::string property_setter_no_colon(
8268 property_setter_name, strlen(property_setter_name) - 1);
8269 clang::IdentifierInfo *setter_ident =
8270 &clang_ast->Idents.get(property_setter_no_colon);
8271 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8272 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8273 std::string setter_sel_string("set");
8274 setter_sel_string.push_back(::toupper(property_name[0]));
8275 setter_sel_string.append(&property_name[1]);
8276 clang::IdentifierInfo *setter_ident =
8277 &clang_ast->Idents.get(setter_sel_string);
8278 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8279 }
8280 property_decl->setSetterName(setter_sel);
8281 property_decl->setPropertyAttributes(
8282 clang::ObjCPropertyDecl::OBJC_PR_setter);
8283
8284 if (property_getter_name != nullptr) {
8285 clang::IdentifierInfo *getter_ident =
8286 &clang_ast->Idents.get(property_getter_name);
8287 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8288 } else {
8289 clang::IdentifierInfo *getter_ident =
8290 &clang_ast->Idents.get(property_name);
8291 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8292 }
8293 property_decl->setGetterName(getter_sel);
8294 property_decl->setPropertyAttributes(
8295 clang::ObjCPropertyDecl::OBJC_PR_getter);
8296
8297 if (ivar_decl)
8298 property_decl->setPropertyIvarDecl(ivar_decl);
8299
8300 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8301 property_decl->setPropertyAttributes(
8302 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8303 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8304 property_decl->setPropertyAttributes(
8305 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8306 if (property_attributes & DW_APPLE_PROPERTY_assign)
8307 property_decl->setPropertyAttributes(
8308 clang::ObjCPropertyDecl::OBJC_PR_assign);
8309 if (property_attributes & DW_APPLE_PROPERTY_retain)
8310 property_decl->setPropertyAttributes(
8311 clang::ObjCPropertyDecl::OBJC_PR_retain);
8312 if (property_attributes & DW_APPLE_PROPERTY_copy)
8313 property_decl->setPropertyAttributes(
8314 clang::ObjCPropertyDecl::OBJC_PR_copy);
8315 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8316 property_decl->setPropertyAttributes(
8317 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8318 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8319 property_decl->setPropertyAttributes(
8320 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8321 if (property_attributes &
8322 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8323 property_decl->setPropertyAttributes(
8324 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8325 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8326 property_decl->setPropertyAttributes(
8327 clang::ObjCPropertyDecl::OBJC_PR_class);
8328
8329 const bool isInstance =
8330 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8331
8332 if (!getter_sel.isNull() &&
8333 !(isInstance
8334 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8335 : class_interface_decl->lookupClassMethod(getter_sel))) {
8336 const bool isVariadic = false;
8337 const bool isSynthesized = false;
8338 const bool isImplicitlyDeclared = true;
8339 const bool isDefined = false;
8340 const clang::ObjCMethodDecl::ImplementationControl impControl =
8341 clang::ObjCMethodDecl::None;
8342 const bool HasRelatedResultType = false;
8343
8344 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8345 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8346 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8347 nullptr, class_interface_decl, isInstance, isVariadic,
8348 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8349 HasRelatedResultType);
8350
8351 if (getter && metadata)
8352 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8353
8354 if (getter) {
8355 getter->setMethodParams(*clang_ast,
8356 llvm::ArrayRef<clang::ParmVarDecl *>(),
8357 llvm::ArrayRef<clang::SourceLocation>());
8358
8359 class_interface_decl->addDecl(getter);
8360 }
8361 }
8362
8363 if (!setter_sel.isNull() &&
8364 !(isInstance
8365 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8366 : class_interface_decl->lookupClassMethod(setter_sel))) {
8367 clang::QualType result_type = clang_ast->VoidTy;
8368 const bool isVariadic = false;
8369 const bool isSynthesized = false;
8370 const bool isImplicitlyDeclared = true;
8371 const bool isDefined = false;
8372 const clang::ObjCMethodDecl::ImplementationControl impControl =
8373 clang::ObjCMethodDecl::None;
8374 const bool HasRelatedResultType = false;
8375
8376 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8377 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8378 setter_sel, result_type, nullptr, class_interface_decl,
8379 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8380 isDefined, impControl, HasRelatedResultType);
8381
8382 if (setter && metadata)
8383 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8384
8385 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8386
8387 params.push_back(clang::ParmVarDecl::Create(
8388 *clang_ast, setter, clang::SourceLocation(),
8389 clang::SourceLocation(),
8390 nullptr, // anonymous
8391 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8392 clang::SC_Auto, nullptr));
8393
8394