Bug Summary

File:tools/lldb/source/Symbol/ClangASTContext.cpp
Warning:line 138, column 3
Returning null reference

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