Bug Summary

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

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ClangASTContext.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D HAVE_ROUND -D LLDB_CONFIGURATION_RELEASE -D LLDB_USE_BUILTIN_DEMANGLER -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/lldb/source/Symbol -I /build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/lldb/include -I /build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/include -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn329677/include -I /usr/include/python2.7 -I /build/llvm-toolchain-snapshot-7~svn329677/tools/clang/include -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/lldb/../clang/include -I /build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/. -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -Wno-deprecated-declarations -Wno-unknown-pragmas -Wno-strict-aliasing -Wno-deprecated-register -Wno-vla-extension -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/lldb/source/Symbol -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-04-11-031539-24776-1 -x c++ /build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp
1//===-- ClangASTContext.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Symbol/ClangASTContext.h"
11
12#include "llvm/Support/FormatAdapters.h"
13#include "llvm/Support/FormatVariadic.h"
14
15// C Includes
16// C++ Includes
17#include <mutex>
18#include <string>
19#include <vector>
20
21// Other libraries and framework includes
22
23// Clang headers like to use NDEBUG inside of them to enable/disable debug
24// related features using "#ifndef NDEBUG" preprocessor blocks to do one thing
25// or another. This is bad because it means that if clang was built in release
26// mode, it assumes that you are building in release mode which is not always
27// the case. You can end up with functions that are defined as empty in header
28// files when NDEBUG is not defined, and this can cause link errors with the
29// clang .a files that you have since you might be missing functions in the .a
30// file. So we have to define NDEBUG when including clang headers to avoid any
31// mismatches. This is covered by rdar://problem/8691220
32
33#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
34#define LLDB_DEFINED_NDEBUG_FOR_CLANG
35#define NDEBUG
36// Need to include assert.h so it is as clang would expect it to be (disabled)
37#include <assert.h>
38#endif
39
40#include "clang/AST/ASTContext.h"
41#include "clang/AST/ASTImporter.h"
42#include "clang/AST/Attr.h"
43#include "clang/AST/CXXInheritance.h"
44#include "clang/AST/DeclObjC.h"
45#include "clang/AST/DeclTemplate.h"
46#include "clang/AST/Mangle.h"
47#include "clang/AST/RecordLayout.h"
48#include "clang/AST/Type.h"
49#include "clang/AST/VTableBuilder.h"
50#include "clang/Basic/Builtins.h"
51#include "clang/Basic/Diagnostic.h"
52#include "clang/Basic/FileManager.h"
53#include "clang/Basic/FileSystemOptions.h"
54#include "clang/Basic/SourceManager.h"
55#include "clang/Basic/TargetInfo.h"
56#include "clang/Basic/TargetOptions.h"
57#include "clang/Frontend/FrontendOptions.h"
58#include "clang/Frontend/LangStandard.h"
59
60#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
61#undef NDEBUG
62#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
63// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
64#include <assert.h>
65#endif
66
67#include "llvm/Support/Signals.h"
68#include "llvm/Support/Threading.h"
69
70#include "Plugins/ExpressionParser/Clang/ClangFunctionCaller.h"
71#include "Plugins/ExpressionParser/Clang/ClangUserExpression.h"
72#include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h"
73#include "lldb/Utility/ArchSpec.h"
74#include "lldb/Utility/Flags.h"
75
76#include "lldb/Core/DumpDataExtractor.h"
77#include "lldb/Core/Module.h"
78#include "lldb/Core/PluginManager.h"
79#include "lldb/Core/Scalar.h"
80#include "lldb/Core/StreamFile.h"
81#include "lldb/Core/ThreadSafeDenseMap.h"
82#include "lldb/Core/UniqueCStringMap.h"
83#include "lldb/Symbol/ClangASTContext.h"
84#include "lldb/Symbol/ClangASTImporter.h"
85#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
86#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
87#include "lldb/Symbol/ClangUtil.h"
88#include "lldb/Symbol/ObjectFile.h"
89#include "lldb/Symbol/SymbolFile.h"
90#include "lldb/Symbol/VerifyDecl.h"
91#include "lldb/Target/ExecutionContext.h"
92#include "lldb/Target/Language.h"
93#include "lldb/Target/ObjCLanguageRuntime.h"
94#include "lldb/Target/Process.h"
95#include "lldb/Target/Target.h"
96#include "lldb/Utility/DataExtractor.h"
97#include "lldb/Utility/LLDBAssert.h"
98#include "lldb/Utility/Log.h"
99#include "lldb/Utility/RegularExpression.h"
100
101#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
102#include "Plugins/SymbolFile/PDB/PDBASTParser.h"
103
104#include <stdio.h>
105
106#include <mutex>
107
108using namespace lldb;
109using namespace lldb_private;
110using namespace llvm;
111using namespace clang;
112
113namespace {
114static inline bool
115ClangASTContextSupportsLanguage(lldb::LanguageType language) {
116 return language == eLanguageTypeUnknown || // Clang is the default type system
117 Language::LanguageIsC(language) ||
118 Language::LanguageIsCPlusPlus(language) ||
119 Language::LanguageIsObjC(language) ||
120 Language::LanguageIsPascal(language) ||
121 // Use Clang for Rust until there is a proper language plugin for it
122 language == eLanguageTypeRust ||
123 language == eLanguageTypeExtRenderScript ||
124 // Use Clang for D until there is a proper language plugin for it
125 language == eLanguageTypeD;
126}
127}
128
129typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext *>
130 ClangASTMap;
131
132static ClangASTMap &GetASTMap() {
133 static ClangASTMap *g_map_ptr = nullptr;
134 static llvm::once_flag g_once_flag;
135 llvm::call_once(g_once_flag, []() {
136 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
137 });
138 return *g_map_ptr;
139}
140
141bool ClangASTContext::IsOperator(const char *name,
142 clang::OverloadedOperatorKind &op_kind) {
143 if (name == nullptr || name[0] == '\0')
144 return false;
145
146#define OPERATOR_PREFIX "operator"
147#define OPERATOR_PREFIX_LENGTH (sizeof(OPERATOR_PREFIX) - 1)
148
149 const char *post_op_name = nullptr;
150
151 bool no_space = true;
152
153 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
154 return false;
155
156 post_op_name = name + OPERATOR_PREFIX_LENGTH;
157
158 if (post_op_name[0] == ' ') {
159 post_op_name++;
160 no_space = false;
161 }
162
163#undef OPERATOR_PREFIX
164#undef OPERATOR_PREFIX_LENGTH
165
166 // This is an operator, set the overloaded operator kind to invalid
167 // in case this is a conversion operator...
168 op_kind = clang::NUM_OVERLOADED_OPERATORS;
169
170 switch (post_op_name[0]) {
171 default:
172 if (no_space)
173 return false;
174 break;
175 case 'n':
176 if (no_space)
177 return false;
178 if (strcmp(post_op_name, "new") == 0)
179 op_kind = clang::OO_New;
180 else if (strcmp(post_op_name, "new[]") == 0)
181 op_kind = clang::OO_Array_New;
182 break;
183
184 case 'd':
185 if (no_space)
186 return false;
187 if (strcmp(post_op_name, "delete") == 0)
188 op_kind = clang::OO_Delete;
189 else if (strcmp(post_op_name, "delete[]") == 0)
190 op_kind = clang::OO_Array_Delete;
191 break;
192
193 case '+':
194 if (post_op_name[1] == '\0')
195 op_kind = clang::OO_Plus;
196 else if (post_op_name[2] == '\0') {
197 if (post_op_name[1] == '=')
198 op_kind = clang::OO_PlusEqual;
199 else if (post_op_name[1] == '+')
200 op_kind = clang::OO_PlusPlus;
201 }
202 break;
203
204 case '-':
205 if (post_op_name[1] == '\0')
206 op_kind = clang::OO_Minus;
207 else if (post_op_name[2] == '\0') {
208 switch (post_op_name[1]) {
209 case '=':
210 op_kind = clang::OO_MinusEqual;
211 break;
212 case '-':
213 op_kind = clang::OO_MinusMinus;
214 break;
215 case '>':
216 op_kind = clang::OO_Arrow;
217 break;
218 }
219 } else if (post_op_name[3] == '\0') {
220 if (post_op_name[2] == '*')
221 op_kind = clang::OO_ArrowStar;
222 break;
223 }
224 break;
225
226 case '*':
227 if (post_op_name[1] == '\0')
228 op_kind = clang::OO_Star;
229 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
230 op_kind = clang::OO_StarEqual;
231 break;
232
233 case '/':
234 if (post_op_name[1] == '\0')
235 op_kind = clang::OO_Slash;
236 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
237 op_kind = clang::OO_SlashEqual;
238 break;
239
240 case '%':
241 if (post_op_name[1] == '\0')
242 op_kind = clang::OO_Percent;
243 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
244 op_kind = clang::OO_PercentEqual;
245 break;
246
247 case '^':
248 if (post_op_name[1] == '\0')
249 op_kind = clang::OO_Caret;
250 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
251 op_kind = clang::OO_CaretEqual;
252 break;
253
254 case '&':
255 if (post_op_name[1] == '\0')
256 op_kind = clang::OO_Amp;
257 else if (post_op_name[2] == '\0') {
258 switch (post_op_name[1]) {
259 case '=':
260 op_kind = clang::OO_AmpEqual;
261 break;
262 case '&':
263 op_kind = clang::OO_AmpAmp;
264 break;
265 }
266 }
267 break;
268
269 case '|':
270 if (post_op_name[1] == '\0')
271 op_kind = clang::OO_Pipe;
272 else if (post_op_name[2] == '\0') {
273 switch (post_op_name[1]) {
274 case '=':
275 op_kind = clang::OO_PipeEqual;
276 break;
277 case '|':
278 op_kind = clang::OO_PipePipe;
279 break;
280 }
281 }
282 break;
283
284 case '~':
285 if (post_op_name[1] == '\0')
286 op_kind = clang::OO_Tilde;
287 break;
288
289 case '!':
290 if (post_op_name[1] == '\0')
291 op_kind = clang::OO_Exclaim;
292 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
293 op_kind = clang::OO_ExclaimEqual;
294 break;
295
296 case '=':
297 if (post_op_name[1] == '\0')
298 op_kind = clang::OO_Equal;
299 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
300 op_kind = clang::OO_EqualEqual;
301 break;
302
303 case '<':
304 if (post_op_name[1] == '\0')
305 op_kind = clang::OO_Less;
306 else if (post_op_name[2] == '\0') {
307 switch (post_op_name[1]) {
308 case '<':
309 op_kind = clang::OO_LessLess;
310 break;
311 case '=':
312 op_kind = clang::OO_LessEqual;
313 break;
314 }
315 } else if (post_op_name[3] == '\0') {
316 if (post_op_name[2] == '=')
317 op_kind = clang::OO_LessLessEqual;
318 }
319 break;
320
321 case '>':
322 if (post_op_name[1] == '\0')
323 op_kind = clang::OO_Greater;
324 else if (post_op_name[2] == '\0') {
325 switch (post_op_name[1]) {
326 case '>':
327 op_kind = clang::OO_GreaterGreater;
328 break;
329 case '=':
330 op_kind = clang::OO_GreaterEqual;
331 break;
332 }
333 } else if (post_op_name[1] == '>' && post_op_name[2] == '=' &&
334 post_op_name[3] == '\0') {
335 op_kind = clang::OO_GreaterGreaterEqual;
336 }
337 break;
338
339 case ',':
340 if (post_op_name[1] == '\0')
341 op_kind = clang::OO_Comma;
342 break;
343
344 case '(':
345 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
346 op_kind = clang::OO_Call;
347 break;
348
349 case '[':
350 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
351 op_kind = clang::OO_Subscript;
352 break;
353 }
354
355 return true;
356}
357
358clang::AccessSpecifier
359ClangASTContext::ConvertAccessTypeToAccessSpecifier(AccessType access) {
360 switch (access) {
361 default:
362 break;
363 case eAccessNone:
364 return AS_none;
365 case eAccessPublic:
366 return AS_public;
367 case eAccessPrivate:
368 return AS_private;
369 case eAccessProtected:
370 return AS_protected;
371 }
372 return AS_none;
373}
374
375static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) {
376 // FIXME: Cleanup per-file based stuff.
377
378 // Set some properties which depend solely on the input kind; it would be nice
379 // to move these to the language standard, and have the driver resolve the
380 // input kind + language standard.
381 if (IK.getLanguage() == InputKind::Asm) {
382 Opts.AsmPreprocessor = 1;
383 } else if (IK.isObjectiveC()) {
384 Opts.ObjC1 = Opts.ObjC2 = 1;
385 }
386
387 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
388
389 if (LangStd == LangStandard::lang_unspecified) {
390 // Based on the base language, pick one.
391 switch (IK.getLanguage()) {
392 case InputKind::Unknown:
393 case InputKind::LLVM_IR:
394 case InputKind::RenderScript:
395 llvm_unreachable("Invalid input kind!")::llvm::llvm_unreachable_internal("Invalid input kind!", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 395)
;
396 case InputKind::OpenCL:
397 LangStd = LangStandard::lang_opencl10;
398 break;
399 case InputKind::CUDA:
400 LangStd = LangStandard::lang_cuda;
401 break;
402 case InputKind::Asm:
403 case InputKind::C:
404 case InputKind::ObjC:
405 LangStd = LangStandard::lang_gnu99;
406 break;
407 case InputKind::CXX:
408 case InputKind::ObjCXX:
409 LangStd = LangStandard::lang_gnucxx98;
410 break;
411 }
412 }
413
414 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
415 Opts.LineComment = Std.hasLineComments();
416 Opts.C99 = Std.isC99();
417 Opts.CPlusPlus = Std.isCPlusPlus();
418 Opts.CPlusPlus11 = Std.isCPlusPlus11();
419 Opts.Digraphs = Std.hasDigraphs();
420 Opts.GNUMode = Std.isGNUMode();
421 Opts.GNUInline = !Std.isC99();
422 Opts.HexFloats = Std.hasHexFloats();
423 Opts.ImplicitInt = Std.hasImplicitInt();
424
425 Opts.WChar = true;
426
427 // OpenCL has some additional defaults.
428 if (LangStd == LangStandard::lang_opencl10) {
429 Opts.OpenCL = 1;
430 Opts.AltiVec = 1;
431 Opts.CXXOperatorNames = 1;
432 Opts.LaxVectorConversions = 1;
433 }
434
435 // OpenCL and C++ both have bool, true, false keywords.
436 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
437
438 Opts.setValueVisibilityMode(DefaultVisibility);
439
440 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
441 // is specified, or -std is set to a conforming mode.
442 Opts.Trigraphs = !Opts.GNUMode;
443 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
444 Opts.OptimizeSize = 0;
445
446 // FIXME: Eliminate this dependency.
447 // unsigned Opt =
448 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
449 // Opts.Optimize = Opt != 0;
450 unsigned Opt = 0;
451
452 // This is the __NO_INLINE__ define, which just depends on things like the
453 // optimization level and -fno-inline, not actually whether the backend has
454 // inlining enabled.
455 //
456 // FIXME: This is affected by other options (-fno-inline).
457 Opts.NoInlineDefine = !Opt;
458}
459
460ClangASTContext::ClangASTContext(const char *target_triple)
461 : TypeSystem(TypeSystem::eKindClang), m_target_triple(), m_ast_ap(),
462 m_language_options_ap(), m_source_manager_ap(), m_diagnostics_engine_ap(),
463 m_target_options_rp(), m_target_info_ap(), m_identifier_table_ap(),
464 m_selector_table_ap(), m_builtins_ap(), m_callback_tag_decl(nullptr),
465 m_callback_objc_decl(nullptr), m_callback_baton(nullptr),
466 m_pointer_byte_size(0), m_ast_owned(false) {
467 if (target_triple && target_triple[0])
468 SetTargetTriple(target_triple);
469}
470
471//----------------------------------------------------------------------
472// Destructor
473//----------------------------------------------------------------------
474ClangASTContext::~ClangASTContext() { Finalize(); }
475
476ConstString ClangASTContext::GetPluginNameStatic() {
477 return ConstString("clang");
478}
479
480ConstString ClangASTContext::GetPluginName() {
481 return ClangASTContext::GetPluginNameStatic();
482}
483
484uint32_t ClangASTContext::GetPluginVersion() { return 1; }
485
486lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
487 lldb_private::Module *module,
488 Target *target) {
489 if (ClangASTContextSupportsLanguage(language)) {
490 ArchSpec arch;
491 if (module)
492 arch = module->GetArchitecture();
493 else if (target)
494 arch = target->GetArchitecture();
495
496 if (arch.IsValid()) {
497 ArchSpec fixed_arch = arch;
498 // LLVM wants this to be set to iOS or MacOSX; if we're working on
499 // a bare-boards type image, change the triple for llvm's benefit.
500 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
501 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) {
502 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
503 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
504 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) {
505 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
506 } else {
507 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
508 }
509 }
510
511 if (module) {
512 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
513 if (ast_sp) {
514 ast_sp->SetArchitecture(fixed_arch);
515 }
516 return ast_sp;
517 } else if (target && target->IsValid()) {
518 std::shared_ptr<ClangASTContextForExpressions> ast_sp(
519 new ClangASTContextForExpressions(*target));
520 if (ast_sp) {
521 ast_sp->SetArchitecture(fixed_arch);
522 ast_sp->m_scratch_ast_source_ap.reset(
523 new ClangASTSource(target->shared_from_this()));
524 lldbassert(ast_sp->getFileManager())lldb_private::lldb_assert(ast_sp->getFileManager(), "ast_sp->getFileManager()"
, __FUNCTION__, "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 524)
;
525 ast_sp->m_scratch_ast_source_ap->InstallASTContext(
526 *ast_sp->getASTContext(), *ast_sp->getFileManager(), true);
527 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
528 ast_sp->m_scratch_ast_source_ap->CreateProxy());
529 ast_sp->SetExternalSource(proxy_ast_source);
530 return ast_sp;
531 }
532 }
533 }
534 }
535 return lldb::TypeSystemSP();
536}
537
538void ClangASTContext::EnumerateSupportedLanguages(
539 std::set<lldb::LanguageType> &languages_for_types,
540 std::set<lldb::LanguageType> &languages_for_expressions) {
541 static std::vector<lldb::LanguageType> s_supported_languages_for_types(
542 {lldb::eLanguageTypeC89, lldb::eLanguageTypeC, lldb::eLanguageTypeC11,
543 lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeC99,
544 lldb::eLanguageTypeObjC, lldb::eLanguageTypeObjC_plus_plus,
545 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
546 lldb::eLanguageTypeC11, lldb::eLanguageTypeC_plus_plus_14});
547
548 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions(
549 {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC_plus_plus,
550 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
551 lldb::eLanguageTypeC_plus_plus_14});
552
553 languages_for_types.insert(s_supported_languages_for_types.begin(),
554 s_supported_languages_for_types.end());
555 languages_for_expressions.insert(
556 s_supported_languages_for_expressions.begin(),
557 s_supported_languages_for_expressions.end());
558}
559
560void ClangASTContext::Initialize() {
561 PluginManager::RegisterPlugin(GetPluginNameStatic(),
562 "clang base AST context plug-in",
563 CreateInstance, EnumerateSupportedLanguages);
564}
565
566void ClangASTContext::Terminate() {
567 PluginManager::UnregisterPlugin(CreateInstance);
568}
569
570void ClangASTContext::Finalize() {
571 if (m_ast_ap.get()) {
572 GetASTMap().Erase(m_ast_ap.get());
573 if (!m_ast_owned)
574 m_ast_ap.release();
575 }
576
577 m_builtins_ap.reset();
578 m_selector_table_ap.reset();
579 m_identifier_table_ap.reset();
580 m_target_info_ap.reset();
581 m_target_options_rp.reset();
582 m_diagnostics_engine_ap.reset();
583 m_source_manager_ap.reset();
584 m_language_options_ap.reset();
585 m_ast_ap.reset();
586 m_scratch_ast_source_ap.reset();
587}
588
589void ClangASTContext::Clear() {
590 m_ast_ap.reset();
591 m_language_options_ap.reset();
592 m_source_manager_ap.reset();
593 m_diagnostics_engine_ap.reset();
594 m_target_options_rp.reset();
595 m_target_info_ap.reset();
596 m_identifier_table_ap.reset();
597 m_selector_table_ap.reset();
598 m_builtins_ap.reset();
599 m_pointer_byte_size = 0;
600}
601
602const char *ClangASTContext::GetTargetTriple() {
603 return m_target_triple.c_str();
604}
605
606void ClangASTContext::SetTargetTriple(const char *target_triple) {
607 Clear();
608 m_target_triple.assign(target_triple);
609}
610
611void ClangASTContext::SetArchitecture(const ArchSpec &arch) {
612 SetTargetTriple(arch.GetTriple().str().c_str());
613}
614
615bool ClangASTContext::HasExternalSource() {
616 ASTContext *ast = getASTContext();
617 if (ast)
618 return ast->getExternalSource() != nullptr;
619 return false;
620}
621
622void ClangASTContext::SetExternalSource(
623 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap) {
624 ASTContext *ast = getASTContext();
625 if (ast) {
626 ast->setExternalSource(ast_source_ap);
627 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
628 // ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
629 }
630}
631
632void ClangASTContext::RemoveExternalSource() {
633 ASTContext *ast = getASTContext();
634
635 if (ast) {
636 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
637 ast->setExternalSource(empty_ast_source_ap);
638 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
639 // ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
640 }
641}
642
643void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) {
644 if (!m_ast_owned) {
645 m_ast_ap.release();
646 }
647 m_ast_owned = false;
648 m_ast_ap.reset(ast_ctx);
649 GetASTMap().Insert(ast_ctx, this);
650}
651
652ASTContext *ClangASTContext::getASTContext() {
653 if (m_ast_ap.get() == nullptr) {
3
Assuming the condition is true
4
Taking true branch
654 m_ast_owned = true;
655 m_ast_ap.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
656 *getIdentifierTable(), *getSelectorTable(),
657 *getBuiltinContext()));
658
659 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
660
661 // This can be NULL if we don't know anything about the architecture or if
662 // the
663 // target for an architecture isn't enabled in the llvm/clang that we built
664 TargetInfo *target_info = getTargetInfo();
665 if (target_info)
5
Taking true branch
666 m_ast_ap->InitBuiltinTypes(*target_info);
667
668 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) {
6
Assuming the condition is false
7
Assuming the condition is false
669 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
670 // m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
671 }
672
673 GetASTMap().Insert(m_ast_ap.get(), this);
674
675 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap(
676 new ClangExternalASTSourceCallbacks(
8
Memory is allocated
677 ClangASTContext::CompleteTagDecl,
678 ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
679 ClangASTContext::LayoutRecordType, this));
680 SetExternalSource(ast_source_ap);
681 }
682 return m_ast_ap.get();
9
Potential leak of memory pointed to by field 'Obj'
683}
684
685ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
686 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
687 return clang_ast;
688}
689
690Builtin::Context *ClangASTContext::getBuiltinContext() {
691 if (m_builtins_ap.get() == nullptr)
692 m_builtins_ap.reset(new Builtin::Context());
693 return m_builtins_ap.get();
694}
695
696IdentifierTable *ClangASTContext::getIdentifierTable() {
697 if (m_identifier_table_ap.get() == nullptr)
698 m_identifier_table_ap.reset(
699 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
700 return m_identifier_table_ap.get();
701}
702
703LangOptions *ClangASTContext::getLanguageOptions() {
704 if (m_language_options_ap.get() == nullptr) {
705 m_language_options_ap.reset(new LangOptions());
706 ParseLangArgs(*m_language_options_ap, InputKind::ObjCXX, GetTargetTriple());
707 // InitializeLangOptions(*m_language_options_ap, InputKind::ObjCXX);
708 }
709 return m_language_options_ap.get();
710}
711
712SelectorTable *ClangASTContext::getSelectorTable() {
713 if (m_selector_table_ap.get() == nullptr)
714 m_selector_table_ap.reset(new SelectorTable());
715 return m_selector_table_ap.get();
716}
717
718clang::FileManager *ClangASTContext::getFileManager() {
719 if (m_file_manager_ap.get() == nullptr) {
720 clang::FileSystemOptions file_system_options;
721 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
722 }
723 return m_file_manager_ap.get();
724}
725
726clang::SourceManager *ClangASTContext::getSourceManager() {
727 if (m_source_manager_ap.get() == nullptr)
728 m_source_manager_ap.reset(
729 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
730 return m_source_manager_ap.get();
731}
732
733clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() {
734 if (m_diagnostics_engine_ap.get() == nullptr) {
735 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
736 m_diagnostics_engine_ap.reset(
737 new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
738 }
739 return m_diagnostics_engine_ap.get();
740}
741
742clang::MangleContext *ClangASTContext::getMangleContext() {
743 if (m_mangle_ctx_ap.get() == nullptr)
744 m_mangle_ctx_ap.reset(getASTContext()->createMangleContext());
745 return m_mangle_ctx_ap.get();
746}
747
748class NullDiagnosticConsumer : public DiagnosticConsumer {
749public:
750 NullDiagnosticConsumer() {
751 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS(1u << 8));
752 }
753
754 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
755 const clang::Diagnostic &info) {
756 if (m_log) {
757 llvm::SmallVector<char, 32> diag_str(10);
758 info.FormatDiagnostic(diag_str);
759 diag_str.push_back('\0');
760 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
761 }
762 }
763
764 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
765 return new NullDiagnosticConsumer();
766 }
767
768private:
769 Log *m_log;
770};
771
772DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() {
773 if (m_diagnostic_consumer_ap.get() == nullptr)
774 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
775
776 return m_diagnostic_consumer_ap.get();
777}
778
779std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
780 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty()) {
781 m_target_options_rp = std::make_shared<clang::TargetOptions>();
782 if (m_target_options_rp.get() != nullptr)
783 m_target_options_rp->Triple = m_target_triple;
784 }
785 return m_target_options_rp;
786}
787
788TargetInfo *ClangASTContext::getTargetInfo() {
789 // target_triple should be something like "x86_64-apple-macosx"
790 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty())
791 m_target_info_ap.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
792 getTargetOptions()));
793 return m_target_info_ap.get();
794}
795
796#pragma mark Basic Types
797
798static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
799 ASTContext *ast, QualType qual_type) {
800 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
801 if (qual_type_bit_size == bit_size)
802 return true;
803 return false;
804}
805
806CompilerType
807ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
808 size_t bit_size) {
809 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
810 getASTContext(), encoding, bit_size);
811}
812
813CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
814 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
815 if (!ast)
816 return CompilerType();
817 switch (encoding) {
818 case eEncodingInvalid:
819 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
820 return CompilerType(ast, ast->VoidPtrTy);
821 break;
822
823 case eEncodingUint:
824 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
825 return CompilerType(ast, ast->UnsignedCharTy);
826 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
827 return CompilerType(ast, ast->UnsignedShortTy);
828 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
829 return CompilerType(ast, ast->UnsignedIntTy);
830 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
831 return CompilerType(ast, ast->UnsignedLongTy);
832 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
833 return CompilerType(ast, ast->UnsignedLongLongTy);
834 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
835 return CompilerType(ast, ast->UnsignedInt128Ty);
836 break;
837
838 case eEncodingSint:
839 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
840 return CompilerType(ast, ast->SignedCharTy);
841 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
842 return CompilerType(ast, ast->ShortTy);
843 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
844 return CompilerType(ast, ast->IntTy);
845 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
846 return CompilerType(ast, ast->LongTy);
847 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
848 return CompilerType(ast, ast->LongLongTy);
849 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
850 return CompilerType(ast, ast->Int128Ty);
851 break;
852
853 case eEncodingIEEE754:
854 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
855 return CompilerType(ast, ast->FloatTy);
856 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
857 return CompilerType(ast, ast->DoubleTy);
858 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
859 return CompilerType(ast, ast->LongDoubleTy);
860 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
861 return CompilerType(ast, ast->HalfTy);
862 break;
863
864 case eEncodingVector:
865 // Sanity check that bit_size is a multiple of 8's.
866 if (bit_size && !(bit_size & 0x7u))
867 return CompilerType(
868 ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8));
869 break;
870 }
871
872 return CompilerType();
873}
874
875lldb::BasicType
876ClangASTContext::GetBasicTypeEnumeration(const ConstString &name) {
877 if (name) {
878 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
879 static TypeNameToBasicTypeMap g_type_map;
880 static llvm::once_flag g_once_flag;
881 llvm::call_once(g_once_flag, []() {
882 // "void"
883 g_type_map.Append(ConstString("void"), eBasicTypeVoid);
884
885 // "char"
886 g_type_map.Append(ConstString("char"), eBasicTypeChar);
887 g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar);
888 g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar);
889 g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar);
890 g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar);
891 g_type_map.Append(ConstString("unsigned wchar_t"),
892 eBasicTypeUnsignedWChar);
893 // "short"
894 g_type_map.Append(ConstString("short"), eBasicTypeShort);
895 g_type_map.Append(ConstString("short int"), eBasicTypeShort);
896 g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort);
897 g_type_map.Append(ConstString("unsigned short int"),
898 eBasicTypeUnsignedShort);
899
900 // "int"
901 g_type_map.Append(ConstString("int"), eBasicTypeInt);
902 g_type_map.Append(ConstString("signed int"), eBasicTypeInt);
903 g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt);
904 g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt);
905
906 // "long"
907 g_type_map.Append(ConstString("long"), eBasicTypeLong);
908 g_type_map.Append(ConstString("long int"), eBasicTypeLong);
909 g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong);
910 g_type_map.Append(ConstString("unsigned long int"),
911 eBasicTypeUnsignedLong);
912
913 // "long long"
914 g_type_map.Append(ConstString("long long"), eBasicTypeLongLong);
915 g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong);
916 g_type_map.Append(ConstString("unsigned long long"),
917 eBasicTypeUnsignedLongLong);
918 g_type_map.Append(ConstString("unsigned long long int"),
919 eBasicTypeUnsignedLongLong);
920
921 // "int128"
922 g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128);
923 g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128);
924
925 // Miscellaneous
926 g_type_map.Append(ConstString("bool"), eBasicTypeBool);
927 g_type_map.Append(ConstString("float"), eBasicTypeFloat);
928 g_type_map.Append(ConstString("double"), eBasicTypeDouble);
929 g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble);
930 g_type_map.Append(ConstString("id"), eBasicTypeObjCID);
931 g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel);
932 g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr);
933 g_type_map.Sort();
934 });
935
936 return g_type_map.Find(name, eBasicTypeInvalid);
937 }
938 return eBasicTypeInvalid;
939}
940
941CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
942 const ConstString &name) {
943 if (ast) {
944 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
945 return ClangASTContext::GetBasicType(ast, basic_type);
946 }
947 return CompilerType();
948}
949
950uint32_t ClangASTContext::GetPointerByteSize() {
951 if (m_pointer_byte_size == 0)
952 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid)
953 .GetPointerType()
954 .GetByteSize(nullptr);
955 return m_pointer_byte_size;
956}
957
958CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
959 return GetBasicType(getASTContext(), basic_type);
2
Calling 'ClangASTContext::getASTContext'
960}
961
962CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
963 lldb::BasicType basic_type) {
964 if (!ast)
965 return CompilerType();
966 lldb::opaque_compiler_type_t clang_type =
967 GetOpaqueCompilerType(ast, basic_type);
968
969 if (clang_type)
970 return CompilerType(GetASTContext(ast), clang_type);
971 return CompilerType();
972}
973
974CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
975 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
976 ASTContext *ast = getASTContext();
977
978#define streq(a, b)strcmp(a, b) == 0 strcmp(a, b) == 0
979 assert(ast != nullptr)(static_cast <bool> (ast != nullptr) ? void (0) : __assert_fail
("ast != nullptr", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 979, __extension__ __PRETTY_FUNCTION__))
;
980 if (ast) {
981 switch (dw_ate) {
982 default:
983 break;
984
985 case DW_ATE_address:
986 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
987 return CompilerType(ast, ast->VoidPtrTy);
988 break;
989
990 case DW_ATE_boolean:
991 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
992 return CompilerType(ast, ast->BoolTy);
993 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
994 return CompilerType(ast, ast->UnsignedCharTy);
995 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
996 return CompilerType(ast, ast->UnsignedShortTy);
997 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
998 return CompilerType(ast, ast->UnsignedIntTy);
999 break;
1000
1001 case DW_ATE_lo_user:
1002 // This has been seen to mean DW_AT_complex_integer
1003 if (type_name) {
1004 if (::strstr(type_name, "complex")) {
1005 CompilerType complex_int_clang_type =
1006 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1007 bit_size / 2);
1008 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1009 complex_int_clang_type)));
1010 }
1011 }
1012 break;
1013
1014 case DW_ATE_complex_float:
1015 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
1016 return CompilerType(ast, ast->FloatComplexTy);
1017 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
1018 return CompilerType(ast, ast->DoubleComplexTy);
1019 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
1020 return CompilerType(ast, ast->LongDoubleComplexTy);
1021 else {
1022 CompilerType complex_float_clang_type =
1023 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1024 bit_size / 2);
1025 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1026 complex_float_clang_type)));
1027 }
1028 break;
1029
1030 case DW_ATE_float:
1031 if (streq(type_name, "float")strcmp(type_name, "float") == 0 &&
1032 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1033 return CompilerType(ast, ast->FloatTy);
1034 if (streq(type_name, "double")strcmp(type_name, "double") == 0 &&
1035 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1036 return CompilerType(ast, ast->DoubleTy);
1037 if (streq(type_name, "long double")strcmp(type_name, "long double") == 0 &&
1038 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1039 return CompilerType(ast, ast->LongDoubleTy);
1040 // Fall back to not requiring a name match
1041 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1042 return CompilerType(ast, ast->FloatTy);
1043 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1044 return CompilerType(ast, ast->DoubleTy);
1045 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1046 return CompilerType(ast, ast->LongDoubleTy);
1047 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1048 return CompilerType(ast, ast->HalfTy);
1049 break;
1050
1051 case DW_ATE_signed:
1052 if (type_name) {
1053 if (streq(type_name, "wchar_t")strcmp(type_name, "wchar_t") == 0 &&
1054 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1055 (getTargetInfo() &&
1056 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1057 return CompilerType(ast, ast->WCharTy);
1058 if (streq(type_name, "void")strcmp(type_name, "void") == 0 &&
1059 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
1060 return CompilerType(ast, ast->VoidTy);
1061 if (strstr(type_name, "long long") &&
1062 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1063 return CompilerType(ast, ast->LongLongTy);
1064 if (strstr(type_name, "long") &&
1065 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1066 return CompilerType(ast, ast->LongTy);
1067 if (strstr(type_name, "short") &&
1068 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1069 return CompilerType(ast, ast->ShortTy);
1070 if (strstr(type_name, "char")) {
1071 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1072 return CompilerType(ast, ast->CharTy);
1073 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1074 return CompilerType(ast, ast->SignedCharTy);
1075 }
1076 if (strstr(type_name, "int")) {
1077 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1078 return CompilerType(ast, ast->IntTy);
1079 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1080 return CompilerType(ast, ast->Int128Ty);
1081 }
1082 }
1083 // We weren't able to match up a type name, just search by size
1084 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1085 return CompilerType(ast, ast->CharTy);
1086 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1087 return CompilerType(ast, ast->ShortTy);
1088 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1089 return CompilerType(ast, ast->IntTy);
1090 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1091 return CompilerType(ast, ast->LongTy);
1092 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1093 return CompilerType(ast, ast->LongLongTy);
1094 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1095 return CompilerType(ast, ast->Int128Ty);
1096 break;
1097
1098 case DW_ATE_signed_char:
1099 if (ast->getLangOpts().CharIsSigned && type_name &&
1100 streq(type_name, "char")strcmp(type_name, "char") == 0) {
1101 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1102 return CompilerType(ast, ast->CharTy);
1103 }
1104 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1105 return CompilerType(ast, ast->SignedCharTy);
1106 break;
1107
1108 case DW_ATE_unsigned:
1109 if (type_name) {
1110 if (streq(type_name, "wchar_t")strcmp(type_name, "wchar_t") == 0) {
1111 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1112 if (!(getTargetInfo() &&
1113 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1114 return CompilerType(ast, ast->WCharTy);
1115 }
1116 }
1117 if (strstr(type_name, "long long")) {
1118 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1119 return CompilerType(ast, ast->UnsignedLongLongTy);
1120 } else if (strstr(type_name, "long")) {
1121 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1122 return CompilerType(ast, ast->UnsignedLongTy);
1123 } else if (strstr(type_name, "short")) {
1124 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1125 return CompilerType(ast, ast->UnsignedShortTy);
1126 } else if (strstr(type_name, "char")) {
1127 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1128 return CompilerType(ast, ast->UnsignedCharTy);
1129 } else if (strstr(type_name, "int")) {
1130 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1131 return CompilerType(ast, ast->UnsignedIntTy);
1132 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1133 return CompilerType(ast, ast->UnsignedInt128Ty);
1134 }
1135 }
1136 // We weren't able to match up a type name, just search by size
1137 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1138 return CompilerType(ast, ast->UnsignedCharTy);
1139 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1140 return CompilerType(ast, ast->UnsignedShortTy);
1141 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1142 return CompilerType(ast, ast->UnsignedIntTy);
1143 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1144 return CompilerType(ast, ast->UnsignedLongTy);
1145 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1146 return CompilerType(ast, ast->UnsignedLongLongTy);
1147 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1148 return CompilerType(ast, ast->UnsignedInt128Ty);
1149 break;
1150
1151 case DW_ATE_unsigned_char:
1152 if (!ast->getLangOpts().CharIsSigned && type_name &&
1153 streq(type_name, "char")strcmp(type_name, "char") == 0) {
1154 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1155 return CompilerType(ast, ast->CharTy);
1156 }
1157 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1158 return CompilerType(ast, ast->UnsignedCharTy);
1159 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1160 return CompilerType(ast, ast->UnsignedShortTy);
1161 break;
1162
1163 case DW_ATE_imaginary_float:
1164 break;
1165
1166 case DW_ATE_UTF:
1167 if (type_name) {
1168 if (streq(type_name, "char16_t")strcmp(type_name, "char16_t") == 0) {
1169 return CompilerType(ast, ast->Char16Ty);
1170 } else if (streq(type_name, "char32_t")strcmp(type_name, "char32_t") == 0) {
1171 return CompilerType(ast, ast->Char32Ty);
1172 }
1173 }
1174 break;
1175 }
1176 }
1177 // This assert should fire for anything that we don't catch above so we know
1178 // to fix any issues we run into.
1179 if (type_name) {
1180 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1181 "DW_TAG_base_type '%s' encoded with "
1182 "DW_ATE = 0x%x, bit_size = %u\n",
1183 type_name, dw_ate, bit_size);
1184 } else {
1185 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1186 "DW_TAG_base_type encoded with "
1187 "DW_ATE = 0x%x, bit_size = %u\n",
1188 dw_ate, bit_size);
1189 }
1190 return CompilerType();
1191}
1192
1193CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) {
1194 if (ast)
1195 return CompilerType(ast, ast->UnknownAnyTy);
1196 return CompilerType();
1197}
1198
1199CompilerType ClangASTContext::GetCStringType(bool is_const) {
1200 ASTContext *ast = getASTContext();
1201 QualType char_type(ast->CharTy);
1202
1203 if (is_const)
1204 char_type.addConst();
1205
1206 return CompilerType(ast, ast->getPointerType(char_type));
1207}
1208
1209clang::DeclContext *
1210ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1211 return ast->getTranslationUnitDecl();
1212}
1213
1214clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1215 clang::Decl *source_decl) {
1216 FileSystemOptions file_system_options;
1217 FileManager file_manager(file_system_options);
1218 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1219
1220 return importer.Import(source_decl);
1221}
1222
1223bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1224 bool ignore_qualifiers) {
1225 ClangASTContext *ast =
1226 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1227 if (!ast || ast != type2.GetTypeSystem())
1228 return false;
1229
1230 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1231 return true;
1232
1233 QualType type1_qual = ClangUtil::GetQualType(type1);
1234 QualType type2_qual = ClangUtil::GetQualType(type2);
1235
1236 if (ignore_qualifiers) {
1237 type1_qual = type1_qual.getUnqualifiedType();
1238 type2_qual = type2_qual.getUnqualifiedType();
1239 }
1240
1241 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
1242}
1243
1244CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1245 if (clang::ObjCInterfaceDecl *interface_decl =
1246 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1247 return GetTypeForDecl(interface_decl);
1248 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1249 return GetTypeForDecl(tag_decl);
1250 return CompilerType();
1251}
1252
1253CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
1254 // No need to call the getASTContext() accessor (which can create the AST
1255 // if it isn't created yet, because we can't have created a decl in this
1256 // AST if our AST didn't already exist...
1257 ASTContext *ast = &decl->getASTContext();
1258 if (ast)
1259 return CompilerType(ast, ast->getTagDeclType(decl));
1260 return CompilerType();
1261}
1262
1263CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
1264 // No need to call the getASTContext() accessor (which can create the AST
1265 // if it isn't created yet, because we can't have created a decl in this
1266 // AST if our AST didn't already exist...
1267 ASTContext *ast = &decl->getASTContext();
1268 if (ast)
1269 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1270 return CompilerType();
1271}
1272
1273#pragma mark Structure, Unions, Classes
1274
1275CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx,
1276 AccessType access_type,
1277 const char *name, int kind,
1278 LanguageType language,
1279 ClangASTMetadata *metadata) {
1280 ASTContext *ast = getASTContext();
1281 assert(ast != nullptr)(static_cast <bool> (ast != nullptr) ? void (0) : __assert_fail
("ast != nullptr", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1281, __extension__ __PRETTY_FUNCTION__))
;
1282
1283 if (decl_ctx == nullptr)
1284 decl_ctx = ast->getTranslationUnitDecl();
1285
1286 if (language == eLanguageTypeObjC ||
1287 language == eLanguageTypeObjC_plus_plus) {
1288 bool isForwardDecl = true;
1289 bool isInternal = false;
1290 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1291 }
1292
1293 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1294 // we will need to update this code. I was told to currently always use
1295 // the CXXRecordDecl class since we often don't know from debug information
1296 // if something is struct or a class, so we default to always use the more
1297 // complete definition just in case.
1298
1299 bool is_anonymous = (!name) || (!name[0]);
1300
1301 CXXRecordDecl *decl = CXXRecordDecl::Create(
1302 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1303 SourceLocation(), is_anonymous ? nullptr : &ast->Idents.get(name));
1304
1305 if (is_anonymous)
1306 decl->setAnonymousStructOrUnion(true);
1307
1308 if (decl) {
1309 if (metadata)
1310 SetMetadata(ast, decl, *metadata);
1311
1312 if (access_type != eAccessNone)
1313 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1314
1315 if (decl_ctx)
1316 decl_ctx->addDecl(decl);
1317
1318 return CompilerType(ast, ast->getTagDeclType(decl));
1319 }
1320 return CompilerType();
1321}
1322
1323namespace {
1324 bool IsValueParam(const clang::TemplateArgument &argument) {
1325 return argument.getKind() == TemplateArgument::Integral;
1326 }
1327}
1328
1329static TemplateParameterList *CreateTemplateParameterList(
1330 ASTContext *ast,
1331 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1332 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1333 const bool parameter_pack = false;
1334 const bool is_typename = false;
1335 const unsigned depth = 0;
1336 const size_t num_template_params = template_param_infos.args.size();
1337 DeclContext *const decl_context =
1338 ast->getTranslationUnitDecl(); // Is this the right decl context?,
1339 for (size_t i = 0; i < num_template_params; ++i) {
1340 const char *name = template_param_infos.names[i];
1341
1342 IdentifierInfo *identifier_info = nullptr;
1343 if (name && name[0])
1344 identifier_info = &ast->Idents.get(name);
1345 if (IsValueParam(template_param_infos.args[i])) {
1346 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1347 *ast, decl_context,
1348 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1349 template_param_infos.args[i].getIntegralType(), parameter_pack,
1350 nullptr));
1351
1352 } else {
1353 template_param_decls.push_back(TemplateTypeParmDecl::Create(
1354 *ast, decl_context,
1355 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1356 is_typename, parameter_pack));
1357 }
1358 }
1359
1360 if (template_param_infos.packed_args &&
1361 template_param_infos.packed_args->args.size()) {
1362 IdentifierInfo *identifier_info = nullptr;
1363 if (template_param_infos.pack_name && template_param_infos.pack_name[0])
1364 identifier_info = &ast->Idents.get(template_param_infos.pack_name);
1365 const bool parameter_pack_true = true;
1366 if (IsValueParam(template_param_infos.packed_args->args[0])) {
1367 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1368 *ast, decl_context,
1369 SourceLocation(), SourceLocation(), depth, num_template_params,
1370 identifier_info,
1371 template_param_infos.packed_args->args[0].getIntegralType(),
1372 parameter_pack_true, nullptr));
1373 } else {
1374 template_param_decls.push_back(TemplateTypeParmDecl::Create(
1375 *ast, decl_context,
1376 SourceLocation(), SourceLocation(), depth, num_template_params,
1377 identifier_info,
1378 is_typename, parameter_pack_true));
1379 }
1380 }
1381 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1382 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1383 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1384 SourceLocation(), requires_clause);
1385 return template_param_list;
1386}
1387
1388clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1389 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1390 const char *name, const TemplateParameterInfos &template_param_infos) {
1391 // /// \brief Create a function template node.
1392 ASTContext *ast = getASTContext();
1393
1394 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1395
1396 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1397 ast, template_param_infos, template_param_decls);
1398 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1399 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1400 template_param_list, func_decl);
1401
1402 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1403 i < template_param_decl_count; ++i) {
1404 // TODO: verify which decl context we should put template_param_decls into..
1405 template_param_decls[i]->setDeclContext(func_decl);
1406 }
1407
1408 return func_tmpl_decl;
1409}
1410
1411void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1412 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1413 const TemplateParameterInfos &infos) {
1414 TemplateArgumentList template_args(TemplateArgumentList::OnStack, infos.args);
1415
1416 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, &template_args,
1417 nullptr);
1418}
1419
1420ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1421 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1422 int kind, const TemplateParameterInfos &template_param_infos) {
1423 ASTContext *ast = getASTContext();
1424
1425 ClassTemplateDecl *class_template_decl = nullptr;
1426 if (decl_ctx == nullptr)
1427 decl_ctx = ast->getTranslationUnitDecl();
1428
1429 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1430 DeclarationName decl_name(&identifier_info);
1431
1432 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1433
1434 for (NamedDecl *decl : result) {
1435 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
1436 if (class_template_decl)
1437 return class_template_decl;
1438 }
1439
1440 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1441
1442 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1443 ast, template_param_infos, template_param_decls);
1444
1445 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1446 *ast, (TagDecl::TagKind)kind,
1447 decl_ctx, // What decl context do we use here? TU? The actual decl
1448 // context?
1449 SourceLocation(), SourceLocation(), &identifier_info);
1450
1451 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1452 i < template_param_decl_count; ++i) {
1453 template_param_decls[i]->setDeclContext(template_cxx_decl);
1454 }
1455
1456 // With templated classes, we say that a class is templated with
1457 // specializations, but that the bare class has no functions.
1458 // template_cxx_decl->startDefinition();
1459 // template_cxx_decl->completeDefinition();
1460
1461 class_template_decl = ClassTemplateDecl::Create(
1462 *ast,
1463 decl_ctx, // What decl context do we use here? TU? The actual decl
1464 // context?
1465 SourceLocation(), decl_name, template_param_list, template_cxx_decl);
1466
1467 if (class_template_decl) {
1468 if (access_type != eAccessNone)
1469 class_template_decl->setAccess(
1470 ConvertAccessTypeToAccessSpecifier(access_type));
1471
1472 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1473 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1474
1475 decl_ctx->addDecl(class_template_decl);
1476
1477#ifdef LLDB_CONFIGURATION_DEBUG
1478 VerifyDecl(class_template_decl);
1479#endif
1480 }
1481
1482 return class_template_decl;
1483}
1484
1485TemplateTemplateParmDecl *
1486ClangASTContext::CreateTemplateTemplateParmDecl(const char *template_name) {
1487 ASTContext *ast = getASTContext();
1488
1489 auto *decl_ctx = ast->getTranslationUnitDecl();
1490
1491 IdentifierInfo &identifier_info = ast->Idents.get(template_name);
1492 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1493
1494 ClangASTContext::TemplateParameterInfos template_param_infos;
1495 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1496 ast, template_param_infos, template_param_decls);
1497
1498 // LLDB needs to create those decls only to be able to display a
1499 // type that includes a template template argument. Only the name
1500 // matters for this purpose, so we use dummy values for the other
1501 // characterisitcs of the type.
1502 return TemplateTemplateParmDecl::Create(
1503 *ast, decl_ctx, SourceLocation(),
1504 /*Depth*/ 0, /*Position*/ 0,
1505 /*IsParameterPack*/ false, &identifier_info, template_param_list);
1506}
1507
1508ClassTemplateSpecializationDecl *
1509ClangASTContext::CreateClassTemplateSpecializationDecl(
1510 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1511 const TemplateParameterInfos &template_param_infos) {
1512 ASTContext *ast = getASTContext();
1513 llvm::SmallVector<clang::TemplateArgument, 2> args(
1514 template_param_infos.args.size() +
1515 (template_param_infos.packed_args ? 1 : 0));
1516 std::copy(template_param_infos.args.begin(), template_param_infos.args.end(),
1517 args.begin());
1518 if (template_param_infos.packed_args) {
1519 args[args.size() - 1] = TemplateArgument::CreatePackCopy(
1520 *ast, template_param_infos.packed_args->args);
1521 }
1522 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1523 ClassTemplateSpecializationDecl::Create(
1524 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1525 SourceLocation(), class_template_decl, args,
1526 nullptr);
1527
1528 class_template_specialization_decl->setSpecializationKind(
1529 TSK_ExplicitSpecialization);
1530
1531 return class_template_specialization_decl;
1532}
1533
1534CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1535 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1536 if (class_template_specialization_decl) {
1537 ASTContext *ast = getASTContext();
1538 if (ast)
1539 return CompilerType(
1540 ast, ast->getTagDeclType(class_template_specialization_decl));
1541 }
1542 return CompilerType();
1543}
1544
1545static inline bool check_op_param(bool is_method,
1546 clang::OverloadedOperatorKind op_kind,
1547 bool unary, bool binary,
1548 uint32_t num_params) {
1549 // Special-case call since it can take any number of operands
1550 if (op_kind == OO_Call)
1551 return true;
1552
1553 // The parameter count doesn't include "this"
1554 if (is_method)
1555 ++num_params;
1556 if (num_params == 1)
1557 return unary;
1558 if (num_params == 2)
1559 return binary;
1560 else
1561 return false;
1562}
1563
1564bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1565 bool is_method, clang::OverloadedOperatorKind op_kind,
1566 uint32_t num_params) {
1567 switch (op_kind) {
1568 default:
1569 break;
1570 // C++ standard allows any number of arguments to new/delete
1571 case OO_New:
1572 case OO_Array_New:
1573 case OO_Delete:
1574 case OO_Array_Delete:
1575 return true;
1576 }
1577
1578#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1579 case OO_##Name: \
1580 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1581 switch (op_kind) {
1582#include "clang/Basic/OperatorKinds.def"
1583 default:
1584 break;
1585 }
1586 return false;
1587}
1588
1589clang::AccessSpecifier
1590ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1591 clang::AccessSpecifier rhs) {
1592 // Make the access equal to the stricter of the field and the nested field's
1593 // access
1594 if (lhs == AS_none || rhs == AS_none)
1595 return AS_none;
1596 if (lhs == AS_private || rhs == AS_private)
1597 return AS_private;
1598 if (lhs == AS_protected || rhs == AS_protected)
1599 return AS_protected;
1600 return AS_public;
1601}
1602
1603bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1604 uint32_t &bitfield_bit_size) {
1605 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
1606}
1607
1608bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field,
1609 uint32_t &bitfield_bit_size) {
1610 if (ast == nullptr || field == nullptr)
1611 return false;
1612
1613 if (field->isBitField()) {
1614 Expr *bit_width_expr = field->getBitWidth();
1615 if (bit_width_expr) {
1616 llvm::APSInt bit_width_apsint;
1617 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1618 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX(4294967295U));
1619 return true;
1620 }
1621 }
1622 }
1623 return false;
1624}
1625
1626bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1627 if (record_decl == nullptr)
1628 return false;
1629
1630 if (!record_decl->field_empty())
1631 return true;
1632
1633 // No fields, lets check this is a CXX record and check the base classes
1634 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1635 if (cxx_record_decl) {
1636 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1637 for (base_class = cxx_record_decl->bases_begin(),
1638 base_class_end = cxx_record_decl->bases_end();
1639 base_class != base_class_end; ++base_class) {
1640 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1641 base_class->getType()->getAs<RecordType>()->getDecl());
1642 if (RecordHasFields(base_class_decl))
1643 return true;
1644 }
1645 }
1646 return false;
1647}
1648
1649#pragma mark Objective C Classes
1650
1651CompilerType ClangASTContext::CreateObjCClass(const char *name,
1652 DeclContext *decl_ctx,
1653 bool isForwardDecl,
1654 bool isInternal,
1655 ClangASTMetadata *metadata) {
1656 ASTContext *ast = getASTContext();
1657 assert(ast != nullptr)(static_cast <bool> (ast != nullptr) ? void (0) : __assert_fail
("ast != nullptr", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1657, __extension__ __PRETTY_FUNCTION__))
;
1658 assert(name && name[0])(static_cast <bool> (name && name[0]) ? void (0
) : __assert_fail ("name && name[0]", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1658, __extension__ __PRETTY_FUNCTION__))
;
1659 if (decl_ctx == nullptr)
1660 decl_ctx = ast->getTranslationUnitDecl();
1661
1662 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1663 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1664 nullptr, SourceLocation(),
1665 /*isForwardDecl,*/
1666 isInternal);
1667
1668 if (decl && metadata)
1669 SetMetadata(ast, decl, *metadata);
1670
1671 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1672}
1673
1674static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
1675 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) ==
1676 false;
1677}
1678
1679uint32_t
1680ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1681 bool omit_empty_base_classes) {
1682 uint32_t num_bases = 0;
1683 if (cxx_record_decl) {
1684 if (omit_empty_base_classes) {
1685 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1686 for (base_class = cxx_record_decl->bases_begin(),
1687 base_class_end = cxx_record_decl->bases_end();
1688 base_class != base_class_end; ++base_class) {
1689 // Skip empty base classes
1690 if (omit_empty_base_classes) {
1691 if (BaseSpecifierIsEmpty(base_class))
1692 continue;
1693 }
1694 ++num_bases;
1695 }
1696 } else
1697 num_bases = cxx_record_decl->getNumBases();
1698 }
1699 return num_bases;
1700}
1701
1702#pragma mark Namespace Declarations
1703
1704NamespaceDecl *
1705ClangASTContext::GetUniqueNamespaceDeclaration(const char *name,
1706 DeclContext *decl_ctx) {
1707 NamespaceDecl *namespace_decl = nullptr;
1708 ASTContext *ast = getASTContext();
1709 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1710 if (decl_ctx == nullptr)
1711 decl_ctx = translation_unit_decl;
1712
1713 if (name) {
1714 IdentifierInfo &identifier_info = ast->Idents.get(name);
1715 DeclarationName decl_name(&identifier_info);
1716 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1717 for (NamedDecl *decl : result) {
1718 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1719 if (namespace_decl)
1720 return namespace_decl;
1721 }
1722
1723 namespace_decl =
1724 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1725 SourceLocation(), &identifier_info, nullptr);
1726
1727 decl_ctx->addDecl(namespace_decl);
1728 } else {
1729 if (decl_ctx == translation_unit_decl) {
1730 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1731 if (namespace_decl)
1732 return namespace_decl;
1733
1734 namespace_decl =
1735 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1736 SourceLocation(), nullptr, nullptr);
1737 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1738 translation_unit_decl->addDecl(namespace_decl);
1739 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace())(static_cast <bool> (namespace_decl == translation_unit_decl
->getAnonymousNamespace()) ? void (0) : __assert_fail ("namespace_decl == translation_unit_decl->getAnonymousNamespace()"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1739, __extension__ __PRETTY_FUNCTION__))
;
1740 } else {
1741 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1742 if (parent_namespace_decl) {
1743 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1744 if (namespace_decl)
1745 return namespace_decl;
1746 namespace_decl =
1747 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1748 SourceLocation(), nullptr, nullptr);
1749 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1750 parent_namespace_decl->addDecl(namespace_decl);
1751 assert(namespace_decl ==(static_cast <bool> (namespace_decl == parent_namespace_decl
->getAnonymousNamespace()) ? void (0) : __assert_fail ("namespace_decl == parent_namespace_decl->getAnonymousNamespace()"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1752, __extension__ __PRETTY_FUNCTION__))
1752 parent_namespace_decl->getAnonymousNamespace())(static_cast <bool> (namespace_decl == parent_namespace_decl
->getAnonymousNamespace()) ? void (0) : __assert_fail ("namespace_decl == parent_namespace_decl->getAnonymousNamespace()"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1752, __extension__ __PRETTY_FUNCTION__))
;
1753 } else {
1754 // BAD!!!
1755 }
1756 }
1757 }
1758#ifdef LLDB_CONFIGURATION_DEBUG
1759 VerifyDecl(namespace_decl);
1760#endif
1761 return namespace_decl;
1762}
1763
1764NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1765 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx) {
1766 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1767 if (ast_ctx == nullptr)
1768 return nullptr;
1769
1770 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx);
1771}
1772
1773clang::BlockDecl *
1774ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1775 if (ctx != nullptr) {
1776 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1777 clang::SourceLocation());
1778 ctx->addDecl(decl);
1779 return decl;
1780 }
1781 return nullptr;
1782}
1783
1784clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1785 clang::DeclContext *right,
1786 clang::DeclContext *root) {
1787 if (root == nullptr)
1788 return nullptr;
1789
1790 std::set<clang::DeclContext *> path_left;
1791 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1792 path_left.insert(d);
1793
1794 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1795 if (path_left.find(d) != path_left.end())
1796 return d;
1797
1798 return nullptr;
1799}
1800
1801clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
1802 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
1803 if (decl_ctx != nullptr && ns_decl != nullptr) {
1804 clang::TranslationUnitDecl *translation_unit =
1805 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
1806 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
1807 *getASTContext(), decl_ctx, clang::SourceLocation(),
1808 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
1809 clang::SourceLocation(), ns_decl,
1810 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
1811 decl_ctx->addDecl(using_decl);
1812 return using_decl;
1813 }
1814 return nullptr;
1815}
1816
1817clang::UsingDecl *
1818ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
1819 clang::NamedDecl *target) {
1820 if (current_decl_ctx != nullptr && target != nullptr) {
1821 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
1822 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
1823 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
1824 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
1825 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
1826 target);
1827 using_decl->addShadowDecl(shadow_decl);
1828 current_decl_ctx->addDecl(using_decl);
1829 return using_decl;
1830 }
1831 return nullptr;
1832}
1833
1834clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
1835 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
1836 if (decl_context != nullptr) {
1837 clang::VarDecl *var_decl = clang::VarDecl::Create(
1838 *getASTContext(), decl_context, clang::SourceLocation(),
1839 clang::SourceLocation(),
1840 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
1841 nullptr, clang::SC_None);
1842 var_decl->setAccess(clang::AS_public);
1843 decl_context->addDecl(var_decl);
1844 return var_decl;
1845 }
1846 return nullptr;
1847}
1848
1849lldb::opaque_compiler_type_t
1850ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
1851 lldb::BasicType basic_type) {
1852 switch (basic_type) {
1853 case eBasicTypeVoid:
1854 return ast->VoidTy.getAsOpaquePtr();
1855 case eBasicTypeChar:
1856 return ast->CharTy.getAsOpaquePtr();
1857 case eBasicTypeSignedChar:
1858 return ast->SignedCharTy.getAsOpaquePtr();
1859 case eBasicTypeUnsignedChar:
1860 return ast->UnsignedCharTy.getAsOpaquePtr();
1861 case eBasicTypeWChar:
1862 return ast->getWCharType().getAsOpaquePtr();
1863 case eBasicTypeSignedWChar:
1864 return ast->getSignedWCharType().getAsOpaquePtr();
1865 case eBasicTypeUnsignedWChar:
1866 return ast->getUnsignedWCharType().getAsOpaquePtr();
1867 case eBasicTypeChar16:
1868 return ast->Char16Ty.getAsOpaquePtr();
1869 case eBasicTypeChar32:
1870 return ast->Char32Ty.getAsOpaquePtr();
1871 case eBasicTypeShort:
1872 return ast->ShortTy.getAsOpaquePtr();
1873 case eBasicTypeUnsignedShort:
1874 return ast->UnsignedShortTy.getAsOpaquePtr();
1875 case eBasicTypeInt:
1876 return ast->IntTy.getAsOpaquePtr();
1877 case eBasicTypeUnsignedInt:
1878 return ast->UnsignedIntTy.getAsOpaquePtr();
1879 case eBasicTypeLong:
1880 return ast->LongTy.getAsOpaquePtr();
1881 case eBasicTypeUnsignedLong:
1882 return ast->UnsignedLongTy.getAsOpaquePtr();
1883 case eBasicTypeLongLong:
1884 return ast->LongLongTy.getAsOpaquePtr();
1885 case eBasicTypeUnsignedLongLong:
1886 return ast->UnsignedLongLongTy.getAsOpaquePtr();
1887 case eBasicTypeInt128:
1888 return ast->Int128Ty.getAsOpaquePtr();
1889 case eBasicTypeUnsignedInt128:
1890 return ast->UnsignedInt128Ty.getAsOpaquePtr();
1891 case eBasicTypeBool:
1892 return ast->BoolTy.getAsOpaquePtr();
1893 case eBasicTypeHalf:
1894 return ast->HalfTy.getAsOpaquePtr();
1895 case eBasicTypeFloat:
1896 return ast->FloatTy.getAsOpaquePtr();
1897 case eBasicTypeDouble:
1898 return ast->DoubleTy.getAsOpaquePtr();
1899 case eBasicTypeLongDouble:
1900 return ast->LongDoubleTy.getAsOpaquePtr();
1901 case eBasicTypeFloatComplex:
1902 return ast->FloatComplexTy.getAsOpaquePtr();
1903 case eBasicTypeDoubleComplex:
1904 return ast->DoubleComplexTy.getAsOpaquePtr();
1905 case eBasicTypeLongDoubleComplex:
1906 return ast->LongDoubleComplexTy.getAsOpaquePtr();
1907 case eBasicTypeObjCID:
1908 return ast->getObjCIdType().getAsOpaquePtr();
1909 case eBasicTypeObjCClass:
1910 return ast->getObjCClassType().getAsOpaquePtr();
1911 case eBasicTypeObjCSel:
1912 return ast->getObjCSelType().getAsOpaquePtr();
1913 case eBasicTypeNullPtr:
1914 return ast->NullPtrTy.getAsOpaquePtr();
1915 default:
1916 return nullptr;
1917 }
1918}
1919
1920#pragma mark Function Types
1921
1922clang::DeclarationName
1923ClangASTContext::GetDeclarationName(const char *name,
1924 const CompilerType &function_clang_type) {
1925 if (!name || !name[0])
1926 return clang::DeclarationName();
1927
1928 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
1929 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
1930 return DeclarationName(&getASTContext()->Idents.get(
1931 name)); // Not operator, but a regular function.
1932
1933 // Check the number of operator parameters. Sometimes we have
1934 // seen bad DWARF that doesn't correctly describe operators and
1935 // if we try to create a method and add it to the class, clang
1936 // will assert and crash, so we need to make sure things are
1937 // acceptable.
1938 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
1939 const clang::FunctionProtoType *function_type =
1940 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
1941 if (function_type == nullptr)
1942 return clang::DeclarationName();
1943
1944 const bool is_method = false;
1945 const unsigned int num_params = function_type->getNumParams();
1946 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1947 is_method, op_kind, num_params))
1948 return clang::DeclarationName();
1949
1950 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
1951}
1952
1953FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
1954 DeclContext *decl_ctx, const char *name,
1955 const CompilerType &function_clang_type, int storage, bool is_inline) {
1956 FunctionDecl *func_decl = nullptr;
1957 ASTContext *ast = getASTContext();
1958 if (decl_ctx == nullptr)
1959 decl_ctx = ast->getTranslationUnitDecl();
1960
1961 const bool hasWrittenPrototype = true;
1962 const bool isConstexprSpecified = false;
1963
1964 clang::DeclarationName declarationName =
1965 GetDeclarationName(name, function_clang_type);
1966 func_decl = FunctionDecl::Create(
1967 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
1968 ClangUtil::GetQualType(function_clang_type), nullptr,
1969 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
1970 isConstexprSpecified);
1971 if (func_decl)
1972 decl_ctx->addDecl(func_decl);
1973
1974#ifdef LLDB_CONFIGURATION_DEBUG
1975 VerifyDecl(func_decl);
1976#endif
1977
1978 return func_decl;
1979}
1980
1981CompilerType ClangASTContext::CreateFunctionType(
1982 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
1983 unsigned num_args, bool is_variadic, unsigned type_quals) {
1984 if (ast == nullptr)
1985 return CompilerType(); // invalid AST
1986
1987 if (!result_type || !ClangUtil::IsClangType(result_type))
1988 return CompilerType(); // invalid return type
1989
1990 std::vector<QualType> qual_type_args;
1991 if (num_args > 0 && args == nullptr)
1992 return CompilerType(); // invalid argument array passed in
1993
1994 // Verify that all arguments are valid and the right type
1995 for (unsigned i = 0; i < num_args; ++i) {
1996 if (args[i]) {
1997 // Make sure we have a clang type in args[i] and not a type from another
1998 // language whose name might match
1999 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2000 lldbassert(is_clang_type)lldb_private::lldb_assert(is_clang_type, "is_clang_type", __FUNCTION__
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 2000)
;
2001 if (is_clang_type)
2002 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2003 else
2004 return CompilerType(); // invalid argument type (must be a clang type)
2005 } else
2006 return CompilerType(); // invalid argument type (empty)
2007 }
2008
2009 // TODO: Detect calling convention in DWARF?
2010 FunctionProtoType::ExtProtoInfo proto_info;
2011 proto_info.Variadic = is_variadic;
2012 proto_info.ExceptionSpec = EST_None;
2013 proto_info.TypeQuals = type_quals;
2014 proto_info.RefQualifier = RQ_None;
2015
2016 return CompilerType(ast,
2017 ast->getFunctionType(ClangUtil::GetQualType(result_type),
2018 qual_type_args, proto_info));
2019}
2020
2021ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
2022 const char *name, const CompilerType &param_type, int storage) {
2023 ASTContext *ast = getASTContext();
2024 assert(ast != nullptr)(static_cast <bool> (ast != nullptr) ? void (0) : __assert_fail
("ast != nullptr", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 2024, __extension__ __PRETTY_FUNCTION__))
;
2025 return ParmVarDecl::Create(*ast, ast->getTranslationUnitDecl(),
2026 SourceLocation(), SourceLocation(),
2027 name && name[0] ? &ast->Idents.get(name) : nullptr,
2028 ClangUtil::GetQualType(param_type), nullptr,
2029 (clang::StorageClass)storage, nullptr);
2030}
2031
2032void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2033 ParmVarDecl **params,
2034 unsigned num_params) {
2035 if (function_decl)
2036 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
2037}
2038
2039CompilerType
2040ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
2041 QualType block_type = m_ast_ap->getBlockPointerType(
2042 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
2043
2044 return CompilerType(this, block_type.getAsOpaquePtr());
2045}
2046
2047#pragma mark Array Types
2048
2049CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2050 size_t element_count,
2051 bool is_vector) {
2052 if (element_type.IsValid()) {
2053 ASTContext *ast = getASTContext();
2054 assert(ast != nullptr)(static_cast <bool> (ast != nullptr) ? void (0) : __assert_fail
("ast != nullptr", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 2054, __extension__ __PRETTY_FUNCTION__))
;
2055
2056 if (is_vector) {
2057 return CompilerType(
2058 ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2059 element_count));
2060 } else {
2061
2062 llvm::APInt ap_element_count(64, element_count);
2063 if (element_count == 0) {
2064 return CompilerType(ast, ast->getIncompleteArrayType(
2065 ClangUtil::GetQualType(element_type),
2066 clang::ArrayType::Normal, 0));
2067 } else {
2068 return CompilerType(
2069 ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2070 ap_element_count,
2071 clang::ArrayType::Normal, 0));
2072 }
2073 }
2074 }
2075 return CompilerType();
2076}
2077
2078CompilerType ClangASTContext::CreateStructForIdentifier(
2079 const ConstString &type_name,
2080 const std::initializer_list<std::pair<const char *, CompilerType>>
2081 &type_fields,
2082 bool packed) {
2083 CompilerType type;
2084 if (!type_name.IsEmpty() &&
2085 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2086 .IsValid()) {
2087 lldbassert(0 && "Trying to create a type for an existing name")lldb_private::lldb_assert(0 && "Trying to create a type for an existing name"
, "0 && \"Trying to create a type for an existing name\""
, __FUNCTION__, "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 2087)
;
2088 return type;
2089 }
2090
2091 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2092 clang::TTK_Struct, lldb::eLanguageTypeC);
2093 StartTagDeclarationDefinition(type);
2094 for (const auto &field : type_fields)
2095 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2096 0);
2097 if (packed)
2098 SetIsPacked(type);
2099 CompleteTagDeclarationDefinition(type);
2100 return type;
2101}
2102
2103CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
2104 const ConstString &type_name,
2105 const std::initializer_list<std::pair<const char *, CompilerType>>
2106 &type_fields,
2107 bool packed) {
2108 CompilerType type;
2109 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2110 return type;
2111
2112 return CreateStructForIdentifier(type_name, type_fields, packed);
2113}
2114
2115#pragma mark Enumeration Types
2116
2117CompilerType
2118ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2119 const Declaration &decl,
2120 const CompilerType &integer_clang_type,
2121 bool is_scoped) {
2122 // TODO: Do something intelligent with the Declaration object passed in
2123 // like maybe filling in the SourceLocation with it...
2124 ASTContext *ast = getASTContext();
2125
2126 // TODO: ask about these...
2127 // const bool IsFixed = false;
2128
2129 EnumDecl *enum_decl = EnumDecl::Create(
2130 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2131 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
2132 is_scoped, // IsScoped
2133 is_scoped, // IsScopedUsingClassTag
2134 false); // IsFixed
2135
2136 if (enum_decl) {
2137 // TODO: check if we should be setting the promotion type too?
2138 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2139
2140 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2141
2142 return CompilerType(ast, ast->getTagDeclType(enum_decl));
2143 }
2144 return CompilerType();
2145}
2146
2147CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2148 size_t bit_size,
2149 bool is_signed) {
2150 if (ast) {
2151 if (is_signed) {
2152 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
2153 return CompilerType(ast, ast->SignedCharTy);
2154
2155 if (bit_size == ast->getTypeSize(ast->ShortTy))
2156 return CompilerType(ast, ast->ShortTy);
2157
2158 if (bit_size == ast->getTypeSize(ast->IntTy))
2159 return CompilerType(ast, ast->IntTy);
2160
2161 if (bit_size == ast->getTypeSize(ast->LongTy))
2162 return CompilerType(ast, ast->LongTy);
2163
2164 if (bit_size == ast->getTypeSize(ast->LongLongTy))
2165 return CompilerType(ast, ast->LongLongTy);
2166
2167 if (bit_size == ast->getTypeSize(ast->Int128Ty))
2168 return CompilerType(ast, ast->Int128Ty);
2169 } else {
2170 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
2171 return CompilerType(ast, ast->UnsignedCharTy);
2172
2173 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
2174 return CompilerType(ast, ast->UnsignedShortTy);
2175
2176 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
2177 return CompilerType(ast, ast->UnsignedIntTy);
2178
2179 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
2180 return CompilerType(ast, ast->UnsignedLongTy);
2181
2182 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
2183 return CompilerType(ast, ast->UnsignedLongLongTy);
2184
2185 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
2186 return CompilerType(ast, ast->UnsignedInt128Ty);
2187 }
2188 }
2189 return CompilerType();
2190}
2191
2192CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2193 bool is_signed) {
2194 if (ast)
2195 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2196 is_signed);
2197 return CompilerType();
2198}
2199
2200void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2201 if (decl_ctx) {
2202 DumpDeclContextHiearchy(decl_ctx->getParent());
2203
2204 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2205 if (named_decl) {
2206 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2207 named_decl->getDeclName().getAsString().c_str());
2208 } else {
2209 printf("%20s\n", decl_ctx->getDeclKindName());
2210 }
2211 }
2212}
2213
2214void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2215 if (decl == nullptr)
2216 return;
2217 DumpDeclContextHiearchy(decl->getDeclContext());
2218
2219 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2220 if (record_decl) {
2221 printf("%20s: %s%s\n", decl->getDeclKindName(),
2222 record_decl->getDeclName().getAsString().c_str(),
2223 record_decl->isInjectedClassName() ? " (injected class name)" : "");
2224
2225 } else {
2226 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2227 if (named_decl) {
2228 printf("%20s: %s\n", decl->getDeclKindName(),
2229 named_decl->getDeclName().getAsString().c_str());
2230 } else {
2231 printf("%20s\n", decl->getDeclKindName());
2232 }
2233 }
2234}
2235
2236bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2237 clang::Decl *rhs_decl) {
2238 if (lhs_decl && rhs_decl) {
2239 //----------------------------------------------------------------------
2240 // Make sure the decl kinds match first
2241 //----------------------------------------------------------------------
2242 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2243 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
2244
2245 if (lhs_decl_kind == rhs_decl_kind) {
2246 //------------------------------------------------------------------
2247 // Now check that the decl contexts kinds are all equivalent
2248 // before we have to check any names of the decl contexts...
2249 //------------------------------------------------------------------
2250 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2251 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2252 if (lhs_decl_ctx && rhs_decl_ctx) {
2253 while (1) {
2254 if (lhs_decl_ctx && rhs_decl_ctx) {
2255 const clang::Decl::Kind lhs_decl_ctx_kind =
2256 lhs_decl_ctx->getDeclKind();
2257 const clang::Decl::Kind rhs_decl_ctx_kind =
2258 rhs_decl_ctx->getDeclKind();
2259 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2260 lhs_decl_ctx = lhs_decl_ctx->getParent();
2261 rhs_decl_ctx = rhs_decl_ctx->getParent();
2262
2263 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2264 break;
2265 } else
2266 return false;
2267 } else
2268 return false;
2269 }
2270
2271 //--------------------------------------------------------------
2272 // Now make sure the name of the decls match
2273 //--------------------------------------------------------------
2274 clang::NamedDecl *lhs_named_decl =
2275 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2276 clang::NamedDecl *rhs_named_decl =
2277 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2278 if (lhs_named_decl && rhs_named_decl) {
2279 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2280 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2281 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2282 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2283 return false;
2284 } else
2285 return false;
2286 } else
2287 return false;
2288
2289 //--------------------------------------------------------------
2290 // We know that the decl context kinds all match, so now we need
2291 // to make sure the names match as well
2292 //--------------------------------------------------------------
2293 lhs_decl_ctx = lhs_decl->getDeclContext();
2294 rhs_decl_ctx = rhs_decl->getDeclContext();
2295 while (1) {
2296 switch (lhs_decl_ctx->getDeclKind()) {
2297 case clang::Decl::TranslationUnit:
2298 // We don't care about the translation unit names
2299 return true;
2300 default: {
2301 clang::NamedDecl *lhs_named_decl =
2302 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2303 clang::NamedDecl *rhs_named_decl =
2304 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2305 if (lhs_named_decl && rhs_named_decl) {
2306 clang::DeclarationName lhs_decl_name =
2307 lhs_named_decl->getDeclName();
2308 clang::DeclarationName rhs_decl_name =
2309 rhs_named_decl->getDeclName();
2310 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2311 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2312 return false;
2313 } else
2314 return false;
2315 } else
2316 return false;
2317 } break;
2318 }
2319 lhs_decl_ctx = lhs_decl_ctx->getParent();
2320 rhs_decl_ctx = rhs_decl_ctx->getParent();
2321 }
2322 }
2323 }
2324 }
2325 return false;
2326}
2327bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2328 clang::Decl *decl) {
2329 if (!decl)
2330 return false;
2331
2332 ExternalASTSource *ast_source = ast->getExternalSource();
2333
2334 if (!ast_source)
2335 return false;
2336
2337 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2338 if (tag_decl->isCompleteDefinition())
2339 return true;
2340
2341 if (!tag_decl->hasExternalLexicalStorage())
2342 return false;
2343
2344 ast_source->CompleteType(tag_decl);
2345
2346 return !tag_decl->getTypeForDecl()->isIncompleteType();
2347 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2348 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2349 if (objc_interface_decl->getDefinition())
2350 return true;
2351
2352 if (!objc_interface_decl->hasExternalLexicalStorage())
2353 return false;
2354
2355 ast_source->CompleteType(objc_interface_decl);
2356
2357 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2358 } else {
2359 return false;
2360 }
2361}
2362
2363void ClangASTContext::SetMetadataAsUserID(const void *object,
2364 user_id_t user_id) {
2365 ClangASTMetadata meta_data;
2366 meta_data.SetUserID(user_id);
2367 SetMetadata(object, meta_data);
2368}
2369
2370void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2371 ClangASTMetadata &metadata) {
2372 ClangExternalASTSourceCommon *external_source =
2373 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2374
2375 if (external_source)
2376 external_source->SetMetadata(object, metadata);
2377}
2378
2379ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2380 const void *object) {
2381 ClangExternalASTSourceCommon *external_source =
2382 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2383
2384 if (external_source && external_source->HasMetadata(object))
2385 return external_source->GetMetadata(object);
2386 else
2387 return nullptr;
2388}
2389
2390clang::DeclContext *
2391ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2392 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2393}
2394
2395clang::DeclContext *
2396ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2397 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2398}
2399
2400bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2401 int kind) const {
2402 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2403 if (clang_type) {
2404 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2405 if (tag_type) {
2406 clang::TagDecl *tag_decl =
2407 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2408 if (tag_decl) {
2409 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2410 return true;
2411 }
2412 }
2413 }
2414 return false;
2415}
2416
2417bool ClangASTContext::SetDefaultAccessForRecordFields(
2418 clang::RecordDecl *record_decl, int default_accessibility,
2419 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2420 if (record_decl) {
2421 uint32_t field_idx;
2422 clang::RecordDecl::field_iterator field, field_end;
2423 for (field = record_decl->field_begin(),
2424 field_end = record_decl->field_end(), field_idx = 0;
2425 field != field_end; ++field, ++field_idx) {
2426 // If no accessibility was assigned, assign the correct one
2427 if (field_idx < num_assigned_accessibilities &&
2428 assigned_accessibilities[field_idx] == clang::AS_none)
2429 field->setAccess((clang::AccessSpecifier)default_accessibility);
2430 }
2431 return true;
2432 }
2433 return false;
2434}
2435
2436clang::DeclContext *
2437ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2438 return GetDeclContextForType(ClangUtil::GetQualType(type));
2439}
2440
2441clang::DeclContext *
2442ClangASTContext::GetDeclContextForType(clang::QualType type) {
2443 if (type.isNull())
2444 return nullptr;
2445
2446 clang::QualType qual_type = type.getCanonicalType();
2447 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2448 switch (type_class) {
2449 case clang::Type::ObjCInterface:
2450 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2451 ->getInterface();
2452 case clang::Type::ObjCObjectPointer:
2453 return GetDeclContextForType(
2454 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2455 ->getPointeeType());
2456 case clang::Type::Record:
2457 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2458 case clang::Type::Enum:
2459 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2460 case clang::Type::Typedef:
2461 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2462 ->getDecl()
2463 ->getUnderlyingType());
2464 case clang::Type::Auto:
2465 return GetDeclContextForType(
2466 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2467 case clang::Type::Elaborated:
2468 return GetDeclContextForType(
2469 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2470 case clang::Type::Paren:
2471 return GetDeclContextForType(
2472 llvm::cast<clang::ParenType>(qual_type)->desugar());
2473 default:
2474 break;
2475 }
2476 // No DeclContext in this type...
2477 return nullptr;
2478}
2479
2480static bool GetCompleteQualType(clang::ASTContext *ast,
2481 clang::QualType qual_type,
2482 bool allow_completion = true) {
2483 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2484 switch (type_class) {
2485 case clang::Type::ConstantArray:
2486 case clang::Type::IncompleteArray:
2487 case clang::Type::VariableArray: {
2488 const clang::ArrayType *array_type =
2489 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2490
2491 if (array_type)
2492 return GetCompleteQualType(ast, array_type->getElementType(),
2493 allow_completion);
2494 } break;
2495 case clang::Type::Record: {
2496 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2497 if (cxx_record_decl) {
2498 if (cxx_record_decl->hasExternalLexicalStorage()) {
2499 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2500 const bool fields_loaded =
2501 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2502 if (is_complete && fields_loaded)
2503 return true;
2504
2505 if (!allow_completion)
2506 return false;
2507
2508 // Call the field_begin() accessor to for it to use the external source
2509 // to load the fields...
2510 clang::ExternalASTSource *external_ast_source =
2511 ast->getExternalSource();
2512 if (external_ast_source) {
2513 external_ast_source->CompleteType(cxx_record_decl);
2514 if (cxx_record_decl->isCompleteDefinition()) {
2515 cxx_record_decl->field_begin();
2516 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2517 }
2518 }
2519 }
2520 }
2521 const clang::TagType *tag_type =
2522 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2523 return !tag_type->isIncompleteType();
2524 } break;
2525
2526 case clang::Type::Enum: {
2527 const clang::TagType *tag_type =
2528 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2529 if (tag_type) {
2530 clang::TagDecl *tag_decl = tag_type->getDecl();
2531 if (tag_decl) {
2532 if (tag_decl->getDefinition())
2533 return true;
2534
2535 if (!allow_completion)
2536 return false;
2537
2538 if (tag_decl->hasExternalLexicalStorage()) {
2539 if (ast) {
2540 clang::ExternalASTSource *external_ast_source =
2541 ast->getExternalSource();
2542 if (external_ast_source) {
2543 external_ast_source->CompleteType(tag_decl);
2544 return !tag_type->isIncompleteType();
2545 }
2546 }
2547 }
2548 return false;
2549 }
2550 }
2551
2552 } break;
2553 case clang::Type::ObjCObject:
2554 case clang::Type::ObjCInterface: {
2555 const clang::ObjCObjectType *objc_class_type =
2556 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2557 if (objc_class_type) {
2558 clang::ObjCInterfaceDecl *class_interface_decl =
2559 objc_class_type->getInterface();
2560 // We currently can't complete objective C types through the newly added
2561 // ASTContext
2562 // because it only supports TagDecl objects right now...
2563 if (class_interface_decl) {
2564 if (class_interface_decl->getDefinition())
2565 return true;
2566
2567 if (!allow_completion)
2568 return false;
2569
2570 if (class_interface_decl->hasExternalLexicalStorage()) {
2571 if (ast) {
2572 clang::ExternalASTSource *external_ast_source =
2573 ast->getExternalSource();
2574 if (external_ast_source) {
2575 external_ast_source->CompleteType(class_interface_decl);
2576 return !objc_class_type->isIncompleteType();
2577 }
2578 }
2579 }
2580 return false;
2581 }
2582 }
2583 } break;
2584
2585 case clang::Type::Typedef:
2586 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2587 ->getDecl()
2588 ->getUnderlyingType(),
2589 allow_completion);
2590
2591 case clang::Type::Auto:
2592 return GetCompleteQualType(
2593 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2594 allow_completion);
2595
2596 case clang::Type::Elaborated:
2597 return GetCompleteQualType(
2598 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2599 allow_completion);
2600
2601 case clang::Type::Paren:
2602 return GetCompleteQualType(
2603 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2604 allow_completion);
2605
2606 case clang::Type::Attributed:
2607 return GetCompleteQualType(
2608 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2609 allow_completion);
2610
2611 default:
2612 break;
2613 }
2614
2615 return true;
2616}
2617
2618static clang::ObjCIvarDecl::AccessControl
2619ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2620 switch (access) {
2621 case eAccessNone:
2622 return clang::ObjCIvarDecl::None;
2623 case eAccessPublic:
2624 return clang::ObjCIvarDecl::Public;
2625 case eAccessPrivate:
2626 return clang::ObjCIvarDecl::Private;
2627 case eAccessProtected:
2628 return clang::ObjCIvarDecl::Protected;
2629 case eAccessPackage:
2630 return clang::ObjCIvarDecl::Package;
2631 }
2632 return clang::ObjCIvarDecl::None;
2633}
2634
2635//----------------------------------------------------------------------
2636// Tests
2637//----------------------------------------------------------------------
2638
2639bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2640 clang::QualType qual_type(GetCanonicalQualType(type));
2641
2642 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2643 switch (type_class) {
2644 case clang::Type::IncompleteArray:
2645 case clang::Type::VariableArray:
2646 case clang::Type::ConstantArray:
2647 case clang::Type::ExtVector:
2648 case clang::Type::Vector:
2649 case clang::Type::Record:
2650 case clang::Type::ObjCObject:
2651 case clang::Type::ObjCInterface:
2652 return true;
2653 case clang::Type::Auto:
2654 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2655 ->getDeducedType()
2656 .getAsOpaquePtr());
2657 case clang::Type::Elaborated:
2658 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2659 ->getNamedType()
2660 .getAsOpaquePtr());
2661 case clang::Type::Typedef:
2662 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2663 ->getDecl()
2664 ->getUnderlyingType()
2665 .getAsOpaquePtr());
2666 case clang::Type::Paren:
2667 return IsAggregateType(
2668 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2669 default:
2670 break;
2671 }
2672 // The clang type does have a value
2673 return false;
2674}
2675
2676bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2677 clang::QualType qual_type(GetCanonicalQualType(type));
2678
2679 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2680 switch (type_class) {
2681 case clang::Type::Record: {
2682 if (const clang::RecordType *record_type =
2683 llvm::dyn_cast_or_null<clang::RecordType>(
2684 qual_type.getTypePtrOrNull())) {
2685 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2686 return record_decl->isAnonymousStructOrUnion();
2687 }
2688 }
2689 break;
2690 }
2691 case clang::Type::Auto:
2692 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2693 ->getDeducedType()
2694 .getAsOpaquePtr());
2695 case clang::Type::Elaborated:
2696 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2697 ->getNamedType()
2698 .getAsOpaquePtr());
2699 case clang::Type::Typedef:
2700 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2701 ->getDecl()
2702 ->getUnderlyingType()
2703 .getAsOpaquePtr());
2704 case clang::Type::Paren:
2705 return IsAnonymousType(
2706 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2707 default:
2708 break;
2709 }
2710 // The clang type does have a value
2711 return false;
2712}
2713
2714bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2715 CompilerType *element_type_ptr,
2716 uint64_t *size, bool *is_incomplete) {
2717 clang::QualType qual_type(GetCanonicalQualType(type));
2718
2719 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2720 switch (type_class) {
2721 default:
2722 break;
2723
2724 case clang::Type::ConstantArray:
2725 if (element_type_ptr)
2726 element_type_ptr->SetCompilerType(
2727 getASTContext(),
2728 llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
2729 if (size)
2730 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2731 ->getSize()
2732 .getLimitedValue(ULLONG_MAX(9223372036854775807LL*2ULL+1ULL));
2733 if (is_incomplete)
2734 *is_incomplete = false;
2735 return true;
2736
2737 case clang::Type::IncompleteArray:
2738 if (element_type_ptr)
2739 element_type_ptr->SetCompilerType(
2740 getASTContext(),
2741 llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2742 if (size)
2743 *size = 0;
2744 if (is_incomplete)
2745 *is_incomplete = true;
2746 return true;
2747
2748 case clang::Type::VariableArray:
2749 if (element_type_ptr)
2750 element_type_ptr->SetCompilerType(
2751 getASTContext(),
2752 llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2753 if (size)
2754 *size = 0;
2755 if (is_incomplete)
2756 *is_incomplete = false;
2757 return true;
2758
2759 case clang::Type::DependentSizedArray:
2760 if (element_type_ptr)
2761 element_type_ptr->SetCompilerType(
2762 getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)
2763 ->getElementType());
2764 if (size)
2765 *size = 0;
2766 if (is_incomplete)
2767 *is_incomplete = false;
2768 return true;
2769
2770 case clang::Type::Typedef:
2771 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2772 ->getDecl()
2773 ->getUnderlyingType()
2774 .getAsOpaquePtr(),
2775 element_type_ptr, size, is_incomplete);
2776 case clang::Type::Auto:
2777 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2778 ->getDeducedType()
2779 .getAsOpaquePtr(),
2780 element_type_ptr, size, is_incomplete);
2781 case clang::Type::Elaborated:
2782 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2783 ->getNamedType()
2784 .getAsOpaquePtr(),
2785 element_type_ptr, size, is_incomplete);
2786 case clang::Type::Paren:
2787 return IsArrayType(
2788 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2789 element_type_ptr, size, is_incomplete);
2790 }
2791 if (element_type_ptr)
2792 element_type_ptr->Clear();
2793 if (size)
2794 *size = 0;
2795 if (is_incomplete)
2796 *is_incomplete = false;
2797 return false;
2798}
2799
2800bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
2801 CompilerType *element_type, uint64_t *size) {
2802 clang::QualType qual_type(GetCanonicalQualType(type));
2803
2804 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2805 switch (type_class) {
2806 case clang::Type::Vector: {
2807 const clang::VectorType *vector_type =
2808 qual_type->getAs<clang::VectorType>();
2809 if (vector_type) {
2810 if (size)
2811 *size = vector_type->getNumElements();
2812 if (element_type)
2813 *element_type =
2814 CompilerType(getASTContext(), vector_type->getElementType());
2815 }
2816 return true;
2817 } break;
2818 case clang::Type::ExtVector: {
2819 const clang::ExtVectorType *ext_vector_type =
2820 qual_type->getAs<clang::ExtVectorType>();
2821 if (ext_vector_type) {
2822 if (size)
2823 *size = ext_vector_type->getNumElements();
2824 if (element_type)
2825 *element_type =
2826 CompilerType(getASTContext(), ext_vector_type->getElementType());
2827 }
2828 return true;
2829 }
2830 default:
2831 break;
2832 }
2833 return false;
2834}
2835
2836bool ClangASTContext::IsRuntimeGeneratedType(
2837 lldb::opaque_compiler_type_t type) {
2838 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
2839 ->GetDeclContextForType(GetQualType(type));
2840 if (!decl_ctx)
2841 return false;
2842
2843 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2844 return false;
2845
2846 clang::ObjCInterfaceDecl *result_iface_decl =
2847 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2848
2849 ClangASTMetadata *ast_metadata =
2850 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2851 if (!ast_metadata)
2852 return false;
2853 return (ast_metadata->GetISAPtr() != 0);
2854}
2855
2856bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
2857 return GetQualType(type).getUnqualifiedType()->isCharType();
2858}
2859
2860bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
2861 const bool allow_completion = false;
2862 return GetCompleteQualType(getASTContext(), GetQualType(type),
2863 allow_completion);
2864}
2865
2866bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
2867 return GetQualType(type).isConstQualified();
2868}
2869
2870bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
2871 uint32_t &length) {
2872 CompilerType pointee_or_element_clang_type;
2873 length = 0;
2874 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
2875
2876 if (!pointee_or_element_clang_type.IsValid())
2877 return false;
2878
2879 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
2880 if (pointee_or_element_clang_type.IsCharType()) {
2881 if (type_flags.Test(eTypeIsArray)) {
2882 // We know the size of the array and it could be a C string
2883 // since it is an array of characters
2884 length = llvm::cast<clang::ConstantArrayType>(
2885 GetCanonicalQualType(type).getTypePtr())
2886 ->getSize()
2887 .getLimitedValue();
2888 }
2889 return true;
2890 }
2891 }
2892 return false;
2893}
2894
2895bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
2896 bool *is_variadic_ptr) {
2897 if (type) {
2898 clang::QualType qual_type(GetCanonicalQualType(type));
2899
2900 if (qual_type->isFunctionType()) {
2901 if (is_variadic_ptr) {
2902 const clang::FunctionProtoType *function_proto_type =
2903 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2904 if (function_proto_type)
2905 *is_variadic_ptr = function_proto_type->isVariadic();
2906 else
2907 *is_variadic_ptr = false;
2908 }
2909 return true;
2910 }
2911
2912 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2913 switch (type_class) {
2914 default:
2915 break;
2916 case clang::Type::Typedef:
2917 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
2918 ->getDecl()
2919 ->getUnderlyingType()
2920 .getAsOpaquePtr(),
2921 nullptr);
2922 case clang::Type::Auto:
2923 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
2924 ->getDeducedType()
2925 .getAsOpaquePtr(),
2926 nullptr);
2927 case clang::Type::Elaborated:
2928 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
2929 ->getNamedType()
2930 .getAsOpaquePtr(),
2931 nullptr);
2932 case clang::Type::Paren:
2933 return IsFunctionType(
2934 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2935 nullptr);
2936 case clang::Type::LValueReference:
2937 case clang::Type::RValueReference: {
2938 const clang::ReferenceType *reference_type =
2939 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
2940 if (reference_type)
2941 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
2942 nullptr);
2943 } break;
2944 }
2945 }
2946 return false;
2947}
2948
2949// Used to detect "Homogeneous Floating-point Aggregates"
2950uint32_t
2951ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
2952 CompilerType *base_type_ptr) {
2953 if (!type)
2954 return 0;
2955
2956 clang::QualType qual_type(GetCanonicalQualType(type));
2957 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2958 switch (type_class) {
2959 case clang::Type::Record:
2960 if (GetCompleteType(type)) {
2961 const clang::CXXRecordDecl *cxx_record_decl =
2962 qual_type->getAsCXXRecordDecl();
2963 if (cxx_record_decl) {
2964 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
2965 return 0;
2966 }
2967 const clang::RecordType *record_type =
2968 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
2969 if (record_type) {
2970 const clang::RecordDecl *record_decl = record_type->getDecl();
2971 if (record_decl) {
2972 // We are looking for a structure that contains only floating point
2973 // types
2974 clang::RecordDecl::field_iterator field_pos,
2975 field_end = record_decl->field_end();
2976 uint32_t num_fields = 0;
2977 bool is_hva = false;
2978 bool is_hfa = false;
2979 clang::QualType base_qual_type;
2980 uint64_t base_bitwidth = 0;
2981 for (field_pos = record_decl->field_begin(); field_pos != field_end;
2982 ++field_pos) {
2983 clang::QualType field_qual_type = field_pos->getType();
2984 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
2985 if (field_qual_type->isFloatingType()) {
2986 if (field_qual_type->isComplexType())
2987 return 0;
2988 else {
2989 if (num_fields == 0)
2990 base_qual_type = field_qual_type;
2991 else {
2992 if (is_hva)
2993 return 0;
2994 is_hfa = true;
2995 if (field_qual_type.getTypePtr() !=
2996 base_qual_type.getTypePtr())
2997 return 0;
2998 }
2999 }
3000 } else if (field_qual_type->isVectorType() ||
3001 field_qual_type->isExtVectorType()) {
3002 if (num_fields == 0) {
3003 base_qual_type = field_qual_type;
3004 base_bitwidth = field_bitwidth;
3005 } else {
3006 if (is_hfa)
3007 return 0;
3008 is_hva = true;
3009 if (base_bitwidth != field_bitwidth)
3010 return 0;
3011 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3012 return 0;
3013 }
3014 } else
3015 return 0;
3016 ++num_fields;
3017 }
3018 if (base_type_ptr)
3019 *base_type_ptr = CompilerType(getASTContext(), base_qual_type);
3020 return num_fields;
3021 }
3022 }
3023 }
3024 break;
3025
3026 case clang::Type::Typedef:
3027 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3028 ->getDecl()
3029 ->getUnderlyingType()
3030 .getAsOpaquePtr(),
3031 base_type_ptr);
3032
3033 case clang::Type::Auto:
3034 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3035 ->getDeducedType()
3036 .getAsOpaquePtr(),
3037 base_type_ptr);
3038
3039 case clang::Type::Elaborated:
3040 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3041 ->getNamedType()
3042 .getAsOpaquePtr(),
3043 base_type_ptr);
3044 default:
3045 break;
3046 }
3047 return 0;
3048}
3049
3050size_t ClangASTContext::GetNumberOfFunctionArguments(
3051 lldb::opaque_compiler_type_t type) {
3052 if (type) {
3053 clang::QualType qual_type(GetCanonicalQualType(type));
3054 const clang::FunctionProtoType *func =
3055 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3056 if (func)
3057 return func->getNumParams();
3058 }
3059 return 0;
3060}
3061
3062CompilerType
3063ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3064 const size_t index) {
3065 if (type) {
3066 clang::QualType qual_type(GetQualType(type));
3067 const clang::FunctionProtoType *func =
3068 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3069 if (func) {
3070 if (index < func->getNumParams())
3071 return CompilerType(getASTContext(), func->getParamType(index));
3072 }
3073 }
3074 return CompilerType();
3075}
3076
3077bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3078 if (type) {
3079 clang::QualType qual_type(GetCanonicalQualType(type));
3080
3081 if (qual_type->isFunctionPointerType())
3082 return true;
3083
3084 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3085 switch (type_class) {
3086 default:
3087 break;
3088 case clang::Type::Typedef:
3089 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3090 ->getDecl()
3091 ->getUnderlyingType()
3092 .getAsOpaquePtr());
3093 case clang::Type::Auto:
3094 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3095 ->getDeducedType()
3096 .getAsOpaquePtr());
3097 case clang::Type::Elaborated:
3098 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3099 ->getNamedType()
3100 .getAsOpaquePtr());
3101 case clang::Type::Paren:
3102 return IsFunctionPointerType(
3103 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3104
3105 case clang::Type::LValueReference:
3106 case clang::Type::RValueReference: {
3107 const clang::ReferenceType *reference_type =
3108 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3109 if (reference_type)
3110 return IsFunctionPointerType(
3111 reference_type->getPointeeType().getAsOpaquePtr());
3112 } break;
3113 }
3114 }
3115 return false;
3116}
3117
3118bool ClangASTContext::IsBlockPointerType(
3119 lldb::opaque_compiler_type_t type,
3120 CompilerType *function_pointer_type_ptr) {
3121 if (type) {
3122 clang::QualType qual_type(GetCanonicalQualType(type));
3123
3124 if (qual_type->isBlockPointerType()) {
3125 if (function_pointer_type_ptr) {
3126 const clang::BlockPointerType *block_pointer_type =
3127 qual_type->getAs<clang::BlockPointerType>();
3128 QualType pointee_type = block_pointer_type->getPointeeType();
3129 QualType function_pointer_type = m_ast_ap->getPointerType(pointee_type);
3130 *function_pointer_type_ptr =
3131 CompilerType(getASTContext(), function_pointer_type);
3132 }
3133 return true;
3134 }
3135
3136 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3137 switch (type_class) {
3138 default:
3139 break;
3140 case clang::Type::Typedef:
3141 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3142 ->getDecl()
3143 ->getUnderlyingType()
3144 .getAsOpaquePtr(),
3145 function_pointer_type_ptr);
3146 case clang::Type::Auto:
3147 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3148 ->getDeducedType()
3149 .getAsOpaquePtr(),
3150 function_pointer_type_ptr);
3151 case clang::Type::Elaborated:
3152 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3153 ->getNamedType()
3154 .getAsOpaquePtr(),
3155 function_pointer_type_ptr);
3156 case clang::Type::Paren:
3157 return IsBlockPointerType(
3158 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3159 function_pointer_type_ptr);
3160
3161 case clang::Type::LValueReference:
3162 case clang::Type::RValueReference: {
3163 const clang::ReferenceType *reference_type =
3164 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3165 if (reference_type)
3166 return IsBlockPointerType(
3167 reference_type->getPointeeType().getAsOpaquePtr(),
3168 function_pointer_type_ptr);
3169 } break;
3170 }
3171 }
3172 return false;
3173}
3174
3175bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3176 bool &is_signed) {
3177 if (!type)
3178 return false;
3179
3180 clang::QualType qual_type(GetCanonicalQualType(type));
3181 const clang::BuiltinType *builtin_type =
3182 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3183
3184 if (builtin_type) {
3185 if (builtin_type->isInteger()) {
3186 is_signed = builtin_type->isSignedInteger();
3187 return true;
3188 }
3189 }
3190
3191 return false;
3192}
3193
3194bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3195 bool &is_signed) {
3196 if (type) {
3197 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3198 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3199
3200 if (enum_type) {
3201 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3202 is_signed);
3203 return true;
3204 }
3205 }
3206
3207 return false;
3208}
3209
3210bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3211 CompilerType *pointee_type) {
3212 if (type) {
3213 clang::QualType qual_type(GetCanonicalQualType(type));
3214 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3215 switch (type_class) {
3216 case clang::Type::Builtin:
3217 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3218 default:
3219 break;
3220 case clang::BuiltinType::ObjCId:
3221 case clang::BuiltinType::ObjCClass:
3222 return true;
3223 }
3224 return false;
3225 case clang::Type::ObjCObjectPointer:
3226 if (pointee_type)
3227 pointee_type->SetCompilerType(
3228 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3229 ->getPointeeType());
3230 return true;
3231 case clang::Type::BlockPointer:
3232 if (pointee_type)
3233 pointee_type->SetCompilerType(
3234 getASTContext(),
3235 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3236 return true;
3237 case clang::Type::Pointer:
3238 if (pointee_type)
3239 pointee_type->SetCompilerType(
3240 getASTContext(),
3241 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3242 return true;
3243 case clang::Type::MemberPointer:
3244 if (pointee_type)
3245 pointee_type->SetCompilerType(
3246 getASTContext(),
3247 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3248 return true;
3249 case clang::Type::Typedef:
3250 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3251 ->getDecl()
3252 ->getUnderlyingType()
3253 .getAsOpaquePtr(),
3254 pointee_type);
3255 case clang::Type::Auto:
3256 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3257 ->getDeducedType()
3258 .getAsOpaquePtr(),
3259 pointee_type);
3260 case clang::Type::Elaborated:
3261 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3262 ->getNamedType()
3263 .getAsOpaquePtr(),
3264 pointee_type);
3265 case clang::Type::Paren:
3266 return IsPointerType(
3267 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3268 pointee_type);
3269 default:
3270 break;
3271 }
3272 }
3273 if (pointee_type)
3274 pointee_type->Clear();
3275 return false;
3276}
3277
3278bool ClangASTContext::IsPointerOrReferenceType(
3279 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3280 if (type) {
3281 clang::QualType qual_type(GetCanonicalQualType(type));
3282 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3283 switch (type_class) {
3284 case clang::Type::Builtin:
3285 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3286 default:
3287 break;
3288 case clang::BuiltinType::ObjCId:
3289 case clang::BuiltinType::ObjCClass:
3290 return true;
3291 }
3292 return false;
3293 case clang::Type::ObjCObjectPointer:
3294 if (pointee_type)
3295 pointee_type->SetCompilerType(
3296 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3297 ->getPointeeType());
3298 return true;
3299 case clang::Type::BlockPointer:
3300 if (pointee_type)
3301 pointee_type->SetCompilerType(
3302 getASTContext(),
3303 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3304 return true;
3305 case clang::Type::Pointer:
3306 if (pointee_type)
3307 pointee_type->SetCompilerType(
3308 getASTContext(),
3309 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3310 return true;
3311 case clang::Type::MemberPointer:
3312 if (pointee_type)
3313 pointee_type->SetCompilerType(
3314 getASTContext(),
3315 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3316 return true;
3317 case clang::Type::LValueReference:
3318 if (pointee_type)
3319 pointee_type->SetCompilerType(
3320 getASTContext(),
3321 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3322 return true;
3323 case clang::Type::RValueReference:
3324 if (pointee_type)
3325 pointee_type->SetCompilerType(
3326 getASTContext(),
3327 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3328 return true;
3329 case clang::Type::Typedef:
3330 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3331 ->getDecl()
3332 ->getUnderlyingType()
3333 .getAsOpaquePtr(),
3334 pointee_type);
3335 case clang::Type::Auto:
3336 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3337 ->getDeducedType()
3338 .getAsOpaquePtr(),
3339 pointee_type);
3340 case clang::Type::Elaborated:
3341 return IsPointerOrReferenceType(
3342 llvm::cast<clang::ElaboratedType>(qual_type)
3343 ->getNamedType()
3344 .getAsOpaquePtr(),
3345 pointee_type);
3346 case clang::Type::Paren:
3347 return IsPointerOrReferenceType(
3348 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3349 pointee_type);
3350 default:
3351 break;
3352 }
3353 }
3354 if (pointee_type)
3355 pointee_type->Clear();
3356 return false;
3357}
3358
3359bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3360 CompilerType *pointee_type,
3361 bool *is_rvalue) {
3362 if (type) {
3363 clang::QualType qual_type(GetCanonicalQualType(type));
3364 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3365
3366 switch (type_class) {
3367 case clang::Type::LValueReference:
3368 if (pointee_type)
3369 pointee_type->SetCompilerType(
3370 getASTContext(),
3371 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3372 if (is_rvalue)
3373 *is_rvalue = false;
3374 return true;
3375 case clang::Type::RValueReference:
3376 if (pointee_type)
3377 pointee_type->SetCompilerType(
3378 getASTContext(),
3379 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3380 if (is_rvalue)
3381 *is_rvalue = true;
3382 return true;
3383 case clang::Type::Typedef:
3384 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3385 ->getDecl()
3386 ->getUnderlyingType()
3387 .getAsOpaquePtr(),
3388 pointee_type, is_rvalue);
3389 case clang::Type::Auto:
3390 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3391 ->getDeducedType()
3392 .getAsOpaquePtr(),
3393 pointee_type, is_rvalue);
3394 case clang::Type::Elaborated:
3395 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3396 ->getNamedType()
3397 .getAsOpaquePtr(),
3398 pointee_type, is_rvalue);
3399 case clang::Type::Paren:
3400 return IsReferenceType(
3401 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3402 pointee_type, is_rvalue);
3403
3404 default:
3405 break;
3406 }
3407 }
3408 if (pointee_type)
3409 pointee_type->Clear();
3410 return false;
3411}
3412
3413bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3414 uint32_t &count, bool &is_complex) {
3415 if (type) {
3416 clang::QualType qual_type(GetCanonicalQualType(type));
3417
3418 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3419 qual_type->getCanonicalTypeInternal())) {
3420 clang::BuiltinType::Kind kind = BT->getKind();
3421 if (kind >= clang::BuiltinType::Float &&
3422 kind <= clang::BuiltinType::LongDouble) {
3423 count = 1;
3424 is_complex = false;
3425 return true;
3426 }
3427 } else if (const clang::ComplexType *CT =
3428 llvm::dyn_cast<clang::ComplexType>(
3429 qual_type->getCanonicalTypeInternal())) {
3430 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3431 is_complex)) {
3432 count = 2;
3433 is_complex = true;
3434 return true;
3435 }
3436 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3437 qual_type->getCanonicalTypeInternal())) {
3438 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3439 is_complex)) {
3440 count = VT->getNumElements();
3441 is_complex = false;
3442 return true;
3443 }
3444 }
3445 }
3446 count = 0;
3447 is_complex = false;
3448 return false;
3449}
3450
3451bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3452 if (!type)
3453 return false;
3454
3455 clang::QualType qual_type(GetQualType(type));
3456 const clang::TagType *tag_type =
3457 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3458 if (tag_type) {
3459 clang::TagDecl *tag_decl = tag_type->getDecl();
3460 if (tag_decl)
3461 return tag_decl->isCompleteDefinition();
3462 return false;
3463 } else {
3464 const clang::ObjCObjectType *objc_class_type =
3465 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3466 if (objc_class_type) {
3467 clang::ObjCInterfaceDecl *class_interface_decl =
3468 objc_class_type->getInterface();
3469 if (class_interface_decl)
3470 return class_interface_decl->getDefinition() != nullptr;
3471 return false;
3472 }
3473 }
3474 return true;
3475}
3476
3477bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
3478 if (type) {
3479 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3480
3481 const clang::ObjCObjectPointerType *obj_pointer_type =
3482 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3483
3484 if (obj_pointer_type)
3485 return obj_pointer_type->isObjCClassType();
3486 }
3487 return false;
3488}
3489
3490bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3491 if (ClangUtil::IsClangType(type))
3492 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3493 return false;
3494}
3495
3496bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3497 if (!type)
3498 return false;
3499 clang::QualType qual_type(GetCanonicalQualType(type));
3500 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3501 return (type_class == clang::Type::Record);
3502}
3503
3504bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3505 if (!type)
3506 return false;
3507 clang::QualType qual_type(GetCanonicalQualType(type));
3508 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3509 return (type_class == clang::Type::Enum);
3510}
3511
3512bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3513 if (type) {
3514 clang::QualType qual_type(GetCanonicalQualType(type));
3515 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3516 switch (type_class) {
3517 case clang::Type::Record:
3518 if (GetCompleteType(type)) {
3519 const clang::RecordType *record_type =
3520 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3521 const clang::RecordDecl *record_decl = record_type->getDecl();
3522 if (record_decl) {
3523 const clang::CXXRecordDecl *cxx_record_decl =
3524 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3525 if (cxx_record_decl)
3526 return cxx_record_decl->isPolymorphic();
3527 }
3528 }
3529 break;
3530
3531 default:
3532 break;
3533 }
3534 }
3535 return false;
3536}
3537
3538bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3539 CompilerType *dynamic_pointee_type,
3540 bool check_cplusplus,
3541 bool check_objc) {
3542 clang::QualType pointee_qual_type;
3543 if (type) {
3544 clang::QualType qual_type(GetCanonicalQualType(type));
3545 bool success = false;
3546 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3547 switch (type_class) {
3548 case clang::Type::Builtin:
3549 if (check_objc &&
3550 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3551 clang::BuiltinType::ObjCId) {
3552 if (dynamic_pointee_type)
3553 dynamic_pointee_type->SetCompilerType(this, type);
3554 return true;
3555 }
3556 break;
3557
3558 case clang::Type::ObjCObjectPointer:
3559 if (check_objc) {
3560 if (auto objc_pointee_type =
3561 qual_type->getPointeeType().getTypePtrOrNull()) {
3562 if (auto objc_object_type =
3563 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3564 objc_pointee_type)) {
3565 if (objc_object_type->isObjCClass())
3566 return false;
3567 }
3568 }
3569 if (dynamic_pointee_type)
3570 dynamic_pointee_type->SetCompilerType(
3571 getASTContext(),
3572 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3573 ->getPointeeType());
3574 return true;
3575 }
3576 break;
3577
3578 case clang::Type::Pointer:
3579 pointee_qual_type =
3580 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3581 success = true;
3582 break;
3583
3584 case clang::Type::LValueReference:
3585 case clang::Type::RValueReference:
3586 pointee_qual_type =
3587 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3588 success = true;
3589 break;
3590
3591 case clang::Type::Typedef:
3592 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3593 ->getDecl()
3594 ->getUnderlyingType()
3595 .getAsOpaquePtr(),
3596 dynamic_pointee_type, check_cplusplus,
3597 check_objc);
3598
3599 case clang::Type::Auto:
3600 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3601 ->getDeducedType()
3602 .getAsOpaquePtr(),
3603 dynamic_pointee_type, check_cplusplus,
3604 check_objc);
3605
3606 case clang::Type::Elaborated:
3607 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3608 ->getNamedType()
3609 .getAsOpaquePtr(),
3610 dynamic_pointee_type, check_cplusplus,
3611 check_objc);
3612
3613 case clang::Type::Paren:
3614 return IsPossibleDynamicType(
3615 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3616 dynamic_pointee_type, check_cplusplus, check_objc);
3617 default:
3618 break;
3619 }
3620
3621 if (success) {
3622 // Check to make sure what we are pointing too is a possible dynamic C++
3623 // type
3624 // We currently accept any "void *" (in case we have a class that has been
3625 // watered down to an opaque pointer) and virtual C++ classes.
3626 const clang::Type::TypeClass pointee_type_class =
3627 pointee_qual_type.getCanonicalType()->getTypeClass();
3628 switch (pointee_type_class) {
3629 case clang::Type::Builtin:
3630 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3631 case clang::BuiltinType::UnknownAny:
3632 case clang::BuiltinType::Void:
3633 if (dynamic_pointee_type)
3634 dynamic_pointee_type->SetCompilerType(getASTContext(),
3635 pointee_qual_type);
3636 return true;
3637 default:
3638 break;
3639 }
3640 break;
3641
3642 case clang::Type::Record:
3643 if (check_cplusplus) {
3644 clang::CXXRecordDecl *cxx_record_decl =
3645 pointee_qual_type->getAsCXXRecordDecl();
3646 if (cxx_record_decl) {
3647 bool is_complete = cxx_record_decl->isCompleteDefinition();
3648
3649 if (is_complete)
3650 success = cxx_record_decl->isDynamicClass();
3651 else {
3652 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3653 getASTContext(), cxx_record_decl);
3654 if (metadata)
3655 success = metadata->GetIsDynamicCXXType();
3656 else {
3657 is_complete = CompilerType(getASTContext(), pointee_qual_type)
3658 .GetCompleteType();
3659 if (is_complete)
3660 success = cxx_record_decl->isDynamicClass();
3661 else
3662 success = false;
3663 }
3664 }
3665
3666 if (success) {
3667 if (dynamic_pointee_type)
3668 dynamic_pointee_type->SetCompilerType(getASTContext(),
3669 pointee_qual_type);
3670 return true;
3671 }
3672 }
3673 }
3674 break;
3675
3676 case clang::Type::ObjCObject:
3677 case clang::Type::ObjCInterface:
3678 if (check_objc) {
3679 if (dynamic_pointee_type)
3680 dynamic_pointee_type->SetCompilerType(getASTContext(),
3681 pointee_qual_type);
3682 return true;
3683 }
3684 break;
3685
3686 default:
3687 break;
3688 }
3689 }
3690 }
3691 if (dynamic_pointee_type)
3692 dynamic_pointee_type->Clear();
3693 return false;
3694}
3695
3696bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3697 if (!type)
3698 return false;
3699
3700 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3701}
3702
3703bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3704 if (!type)
3705 return false;
3706 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3707}
3708
3709bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3710 if (!type)
3711 return false;
3712 return GetCanonicalQualType(type)->isVoidType();
3713}
3714
3715bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3716 return ClangASTContextSupportsLanguage(language);
3717}
3718
3719bool ClangASTContext::GetCXXClassName(const CompilerType &type,
3720 std::string &class_name) {
3721 if (type) {
3722 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3723 if (!qual_type.isNull()) {
3724 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3725 if (cxx_record_decl) {
3726 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3727 return true;
3728 }
3729 }
3730 }
3731 class_name.clear();
3732 return false;
3733}
3734
3735bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3736 if (!type)
3737 return false;
3738
3739 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3740 if (!qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr)
3741 return true;
3742 return false;
3743}
3744
3745bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3746 if (!type)
3747 return false;
3748 clang::QualType qual_type(GetCanonicalQualType(type));
3749 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3750 if (tag_type)
3751 return tag_type->isBeingDefined();
3752 return false;
3753}
3754
3755bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3756 CompilerType *class_type_ptr) {
3757 if (!type)
3758 return false;
3759
3760 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3761
3762 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3763 if (class_type_ptr) {
3764 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3765 const clang::ObjCObjectPointerType *obj_pointer_type =
3766 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3767 if (obj_pointer_type == nullptr)
3768 class_type_ptr->Clear();
3769 else
3770 class_type_ptr->SetCompilerType(
3771 type.GetTypeSystem(),
3772 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3773 .getAsOpaquePtr());
3774 }
3775 }
3776 return true;
3777 }
3778 if (class_type_ptr)
3779 class_type_ptr->Clear();
3780 return false;
3781}
3782
3783bool ClangASTContext::GetObjCClassName(const CompilerType &type,
3784 std::string &class_name) {
3785 if (!type)
3786 return false;
3787
3788 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3789
3790 const clang::ObjCObjectType *object_type =
3791 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3792 if (object_type) {
3793 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3794 if (interface) {
3795 class_name = interface->getNameAsString();
3796 return true;
3797 }
3798 }
3799 return false;
3800}
3801
3802//----------------------------------------------------------------------
3803// Type Completion
3804//----------------------------------------------------------------------
3805
3806bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
3807 if (!type)
3808 return false;
3809 const bool allow_completion = true;
3810 return GetCompleteQualType(getASTContext(), GetQualType(type),
3811 allow_completion);
3812}
3813
3814ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
3815 std::string type_name;
3816 if (type) {
3817 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
3818 clang::QualType qual_type(GetQualType(type));
3819 printing_policy.SuppressTagKeyword = true;
3820 const clang::TypedefType *typedef_type =
3821 qual_type->getAs<clang::TypedefType>();
3822 if (typedef_type) {
3823 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3824 type_name = typedef_decl->getQualifiedNameAsString();
3825 } else {
3826 type_name = qual_type.getAsString(printing_policy);
3827 }
3828 }
3829 return ConstString(type_name);
3830}
3831
3832uint32_t
3833ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
3834 CompilerType *pointee_or_element_clang_type) {
3835 if (!type)
3836 return 0;
3837
3838 if (pointee_or_element_clang_type)
3839 pointee_or_element_clang_type->Clear();
3840
3841 clang::QualType qual_type(GetQualType(type));
3842
3843 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3844 switch (type_class) {
3845 case clang::Type::Attributed:
3846 return GetTypeInfo(
3847 qual_type->getAs<clang::AttributedType>()
3848 ->getModifiedType().getAsOpaquePtr(),
3849 pointee_or_element_clang_type);
3850 case clang::Type::Builtin: {
3851 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
3852 qual_type->getCanonicalTypeInternal());
3853
3854 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3855 switch (builtin_type->getKind()) {
3856 case clang::BuiltinType::ObjCId:
3857 case clang::BuiltinType::ObjCClass:
3858 if (pointee_or_element_clang_type)
3859 pointee_or_element_clang_type->SetCompilerType(
3860 getASTContext(), getASTContext()->ObjCBuiltinClassTy);
3861 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3862 break;
3863
3864 case clang::BuiltinType::ObjCSel:
3865 if (pointee_or_element_clang_type)
3866 pointee_or_element_clang_type->SetCompilerType(getASTContext(),
3867 getASTContext()->CharTy);
3868 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3869 break;
3870
3871 case clang::BuiltinType::Bool:
3872 case clang::BuiltinType::Char_U:
3873 case clang::BuiltinType::UChar:
3874 case clang::BuiltinType::WChar_U:
3875 case clang::BuiltinType::Char16:
3876 case clang::BuiltinType::Char32:
3877 case clang::BuiltinType::UShort:
3878 case clang::BuiltinType::UInt:
3879 case clang::BuiltinType::ULong:
3880 case clang::BuiltinType::ULongLong:
3881 case clang::BuiltinType::UInt128:
3882 case clang::BuiltinType::Char_S:
3883 case clang::BuiltinType::SChar:
3884 case clang::BuiltinType::WChar_S:
3885 case clang::BuiltinType::Short:
3886 case clang::BuiltinType::Int:
3887 case clang::BuiltinType::Long:
3888 case clang::BuiltinType::LongLong:
3889 case clang::BuiltinType::Int128:
3890 case clang::BuiltinType::Float:
3891 case clang::BuiltinType::Double:
3892 case clang::BuiltinType::LongDouble:
3893 builtin_type_flags |= eTypeIsScalar;
3894 if (builtin_type->isInteger()) {
3895 builtin_type_flags |= eTypeIsInteger;
3896 if (builtin_type->isSignedInteger())
3897 builtin_type_flags |= eTypeIsSigned;
3898 } else if (builtin_type->isFloatingPoint())
3899 builtin_type_flags |= eTypeIsFloat;
3900 break;
3901 default:
3902 break;
3903 }
3904 return builtin_type_flags;
3905 }
3906
3907 case clang::Type::BlockPointer:
3908 if (pointee_or_element_clang_type)
3909 pointee_or_element_clang_type->SetCompilerType(
3910 getASTContext(), qual_type->getPointeeType());
3911 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3912
3913 case clang::Type::Complex: {
3914 uint32_t complex_type_flags =
3915 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3916 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
3917 qual_type->getCanonicalTypeInternal());
3918 if (complex_type) {
3919 clang::QualType complex_element_type(complex_type->getElementType());
3920 if (complex_element_type->isIntegerType())
3921 complex_type_flags |= eTypeIsFloat;
3922 else if (complex_element_type->isFloatingType())
3923 complex_type_flags |= eTypeIsInteger;
3924 }
3925 return complex_type_flags;
3926 } break;
3927
3928 case clang::Type::ConstantArray:
3929 case clang::Type::DependentSizedArray:
3930 case clang::Type::IncompleteArray:
3931 case clang::Type::VariableArray:
3932 if (pointee_or_element_clang_type)
3933 pointee_or_element_clang_type->SetCompilerType(
3934 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
3935 ->getElementType());
3936 return eTypeHasChildren | eTypeIsArray;
3937
3938 case clang::Type::DependentName:
3939 return 0;
3940 case clang::Type::DependentSizedExtVector:
3941 return eTypeHasChildren | eTypeIsVector;
3942 case clang::Type::DependentTemplateSpecialization:
3943 return eTypeIsTemplate;
3944 case clang::Type::Decltype:
3945 return CompilerType(
3946 getASTContext(),
3947 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
3948 .GetTypeInfo(pointee_or_element_clang_type);
3949
3950 case clang::Type::Enum:
3951 if (pointee_or_element_clang_type)
3952 pointee_or_element_clang_type->SetCompilerType(
3953 getASTContext(),
3954 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
3955 return eTypeIsEnumeration | eTypeHasValue;
3956
3957 case clang::Type::Auto:
3958 return CompilerType(
3959 getASTContext(),
3960 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
3961 .GetTypeInfo(pointee_or_element_clang_type);
3962 case clang::Type::Elaborated:
3963 return CompilerType(
3964 getASTContext(),
3965 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
3966 .GetTypeInfo(pointee_or_element_clang_type);
3967 case clang::Type::Paren:
3968 return CompilerType(getASTContext(),
3969 llvm::cast<clang::ParenType>(qual_type)->desugar())
3970 .GetTypeInfo(pointee_or_element_clang_type);
3971
3972 case clang::Type::FunctionProto:
3973 return eTypeIsFuncPrototype | eTypeHasValue;
3974 case clang::Type::FunctionNoProto:
3975 return eTypeIsFuncPrototype | eTypeHasValue;
3976 case clang::Type::InjectedClassName:
3977 return 0;
3978
3979 case clang::Type::LValueReference:
3980 case clang::Type::RValueReference:
3981 if (pointee_or_element_clang_type)
3982 pointee_or_element_clang_type->SetCompilerType(
3983 getASTContext(),
3984 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
3985 ->getPointeeType());
3986 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
3987
3988 case clang::Type::MemberPointer:
3989 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
3990
3991 case clang::Type::ObjCObjectPointer:
3992 if (pointee_or_element_clang_type)
3993 pointee_or_element_clang_type->SetCompilerType(
3994 getASTContext(), qual_type->getPointeeType());
3995 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
3996 eTypeHasValue;
3997
3998 case clang::Type::ObjCObject:
3999 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4000 case clang::Type::ObjCInterface:
4001 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4002
4003 case clang::Type::Pointer:
4004 if (pointee_or_element_clang_type)
4005 pointee_or_element_clang_type->SetCompilerType(
4006 getASTContext(), qual_type->getPointeeType());
4007 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4008
4009 case clang::Type::Record:
4010 if (qual_type->getAsCXXRecordDecl())
4011 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4012 else
4013 return eTypeHasChildren | eTypeIsStructUnion;
4014 break;
4015 case clang::Type::SubstTemplateTypeParm:
4016 return eTypeIsTemplate;
4017 case clang::Type::TemplateTypeParm:
4018 return eTypeIsTemplate;
4019 case clang::Type::TemplateSpecialization:
4020 return eTypeIsTemplate;
4021
4022 case clang::Type::Typedef:
4023 return eTypeIsTypedef |
4024 CompilerType(getASTContext(),
4025 llvm::cast<clang::TypedefType>(qual_type)
4026 ->getDecl()
4027 ->getUnderlyingType())
4028 .GetTypeInfo(pointee_or_element_clang_type);
4029 case clang::Type::TypeOfExpr:
4030 return CompilerType(getASTContext(),
4031 llvm::cast<clang::TypeOfExprType>(qual_type)
4032 ->getUnderlyingExpr()
4033 ->getType())
4034 .GetTypeInfo(pointee_or_element_clang_type);
4035 case clang::Type::TypeOf:
4036 return CompilerType(
4037 getASTContext(),
4038 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4039 .GetTypeInfo(pointee_or_element_clang_type);
4040 case clang::Type::UnresolvedUsing:
4041 return 0;
4042
4043 case clang::Type::ExtVector:
4044 case clang::Type::Vector: {
4045 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4046 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4047 qual_type->getCanonicalTypeInternal());
4048 if (vector_type) {
4049 if (vector_type->isIntegerType())
4050 vector_type_flags |= eTypeIsFloat;
4051 else if (vector_type->isFloatingType())
4052 vector_type_flags |= eTypeIsInteger;
4053 }
4054 return vector_type_flags;
4055 }
4056 default:
4057 return 0;
4058 }
4059 return 0;
4060}
4061
4062lldb::LanguageType
4063ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4064 if (!type)
4065 return lldb::eLanguageTypeC;
4066
4067 // If the type is a reference, then resolve it to what it refers to first:
4068 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4069 if (qual_type->isAnyPointerType()) {
4070 if (qual_type->isObjCObjectPointerType())
4071 return lldb::eLanguageTypeObjC;
4072
4073 clang::QualType pointee_type(qual_type->getPointeeType());
4074 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
4075 return lldb::eLanguageTypeC_plus_plus;
4076 if (pointee_type->isObjCObjectOrInterfaceType())
4077 return lldb::eLanguageTypeObjC;
4078 if (pointee_type->isObjCClassType())
4079 return lldb::eLanguageTypeObjC;
4080 if (pointee_type.getTypePtr() ==
4081 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4082 return lldb::eLanguageTypeObjC;
4083 } else {
4084 if (qual_type->isObjCObjectOrInterfaceType())
4085 return lldb::eLanguageTypeObjC;
4086 if (qual_type->getAsCXXRecordDecl())
4087 return lldb::eLanguageTypeC_plus_plus;
4088 switch (qual_type->getTypeClass()) {
4089 default:
4090 break;
4091 case clang::Type::Builtin:
4092 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4093 default:
4094 case clang::BuiltinType::Void:
4095 case clang::BuiltinType::Bool:
4096 case clang::BuiltinType::Char_U:
4097 case clang::BuiltinType::UChar:
4098 case clang::BuiltinType::WChar_U:
4099 case clang::BuiltinType::Char16:
4100 case clang::BuiltinType::Char32:
4101 case clang::BuiltinType::UShort:
4102 case clang::BuiltinType::UInt:
4103 case clang::BuiltinType::ULong:
4104 case clang::BuiltinType::ULongLong:
4105 case clang::BuiltinType::UInt128:
4106 case clang::BuiltinType::Char_S:
4107 case clang::BuiltinType::SChar:
4108 case clang::BuiltinType::WChar_S:
4109 case clang::BuiltinType::Short:
4110 case clang::BuiltinType::Int:
4111 case clang::BuiltinType::Long:
4112 case clang::BuiltinType::LongLong:
4113 case clang::BuiltinType::Int128:
4114 case clang::BuiltinType::Float:
4115 case clang::BuiltinType::Double:
4116 case clang::BuiltinType::LongDouble:
4117 break;
4118
4119 case clang::BuiltinType::NullPtr:
4120 return eLanguageTypeC_plus_plus;
4121
4122 case clang::BuiltinType::ObjCId:
4123 case clang::BuiltinType::ObjCClass:
4124 case clang::BuiltinType::ObjCSel:
4125 return eLanguageTypeObjC;
4126
4127 case clang::BuiltinType::Dependent:
4128 case clang::BuiltinType::Overload:
4129 case clang::BuiltinType::BoundMember:
4130 case clang::BuiltinType::UnknownAny:
4131 break;
4132 }
4133 break;
4134 case clang::Type::Typedef:
4135 return CompilerType(getASTContext(),
4136 llvm::cast<clang::TypedefType>(qual_type)
4137 ->getDecl()
4138 ->getUnderlyingType())
4139 .GetMinimumLanguage();
4140 }
4141 }
4142 return lldb::eLanguageTypeC;
4143}
4144
4145lldb::TypeClass
4146ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4147 if (!type)
4148 return lldb::eTypeClassInvalid;
4149
4150 clang::QualType qual_type(GetQualType(type));
4151
4152 switch (qual_type->getTypeClass()) {
4153 case clang::Type::UnaryTransform:
4154 break;
4155 case clang::Type::FunctionNoProto:
4156 return lldb::eTypeClassFunction;
4157 case clang::Type::FunctionProto:
4158 return lldb::eTypeClassFunction;
4159 case clang::Type::IncompleteArray:
4160 return lldb::eTypeClassArray;
4161 case clang::Type::VariableArray:
4162 return lldb::eTypeClassArray;
4163 case clang::Type::ConstantArray:
4164 return lldb::eTypeClassArray;
4165 case clang::Type::DependentSizedArray:
4166 return lldb::eTypeClassArray;
4167 case clang::Type::DependentSizedExtVector:
4168 return lldb::eTypeClassVector;
4169 case clang::Type::ExtVector:
4170 return lldb::eTypeClassVector;
4171 case clang::Type::Vector:
4172 return lldb::eTypeClassVector;
4173 case clang::Type::Builtin:
4174 return lldb::eTypeClassBuiltin;
4175 case clang::Type::ObjCObjectPointer:
4176 return lldb::eTypeClassObjCObjectPointer;
4177 case clang::Type::BlockPointer:
4178 return lldb::eTypeClassBlockPointer;
4179 case clang::Type::Pointer:
4180 return lldb::eTypeClassPointer;
4181 case clang::Type::LValueReference:
4182 return lldb::eTypeClassReference;
4183 case clang::Type::RValueReference:
4184 return lldb::eTypeClassReference;
4185 case clang::Type::MemberPointer:
4186 return lldb::eTypeClassMemberPointer;
4187 case clang::Type::Complex:
4188 if (qual_type->isComplexType())
4189 return lldb::eTypeClassComplexFloat;
4190 else
4191 return lldb::eTypeClassComplexInteger;
4192 case clang::Type::ObjCObject:
4193 return lldb::eTypeClassObjCObject;
4194 case clang::Type::ObjCInterface:
4195 return lldb::eTypeClassObjCInterface;
4196 case clang::Type::Record: {
4197 const clang::RecordType *record_type =
4198 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4199 const clang::RecordDecl *record_decl = record_type->getDecl();
4200 if (record_decl->isUnion())
4201 return lldb::eTypeClassUnion;
4202 else if (record_decl->isStruct())
4203 return lldb::eTypeClassStruct;
4204 else
4205 return lldb::eTypeClassClass;
4206 } break;
4207 case clang::Type::Enum:
4208 return lldb::eTypeClassEnumeration;
4209 case clang::Type::Typedef:
4210 return lldb::eTypeClassTypedef;
4211 case clang::Type::UnresolvedUsing:
4212 break;
4213 case clang::Type::Paren:
4214 return CompilerType(getASTContext(),
4215 llvm::cast<clang::ParenType>(qual_type)->desugar())
4216 .GetTypeClass();
4217 case clang::Type::Auto:
4218 return CompilerType(
4219 getASTContext(),
4220 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4221 .GetTypeClass();
4222 case clang::Type::Elaborated:
4223 return CompilerType(
4224 getASTContext(),
4225 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4226 .GetTypeClass();
4227
4228 case clang::Type::Attributed:
4229 break;
4230 case clang::Type::TemplateTypeParm:
4231 break;
4232 case clang::Type::SubstTemplateTypeParm:
4233 break;
4234 case clang::Type::SubstTemplateTypeParmPack:
4235 break;
4236 case clang::Type::InjectedClassName:
4237 break;
4238 case clang::Type::DependentName:
4239 break;
4240 case clang::Type::DependentTemplateSpecialization:
4241 break;
4242 case clang::Type::PackExpansion:
4243 break;
4244
4245 case clang::Type::TypeOfExpr:
4246 return CompilerType(getASTContext(),
4247 llvm::cast<clang::TypeOfExprType>(qual_type)
4248 ->getUnderlyingExpr()
4249 ->getType())
4250 .GetTypeClass();
4251 case clang::Type::TypeOf:
4252 return CompilerType(
4253 getASTContext(),
4254 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4255 .GetTypeClass();
4256 case clang::Type::Decltype:
4257 return CompilerType(
4258 getASTContext(),
4259 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4260 .GetTypeClass();
4261 case clang::Type::TemplateSpecialization:
4262 break;
4263 case clang::Type::DeducedTemplateSpecialization:
4264 break;
4265 case clang::Type::Atomic:
4266 break;
4267 case clang::Type::Pipe:
4268 break;
4269
4270 // pointer type decayed from an array or function type.
4271 case clang::Type::Decayed:
4272 break;
4273 case clang::Type::Adjusted:
4274 break;
4275 case clang::Type::ObjCTypeParam:
4276 break;
4277
4278 case clang::Type::DependentAddressSpace:
4279 break;
4280 }
4281 // We don't know hot to display this type...
4282 return lldb::eTypeClassOther;
4283}
4284
4285unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4286 if (type)
4287 return GetQualType(type).getQualifiers().getCVRQualifiers();
4288 return 0;
4289}
4290
4291//----------------------------------------------------------------------
4292// Creating related types
4293//----------------------------------------------------------------------
4294
4295CompilerType
4296ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4297 uint64_t *stride) {
4298 if (type) {
4299 clang::QualType qual_type(GetCanonicalQualType(type));
4300
4301 const clang::Type *array_eletype =
4302 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4303
4304 if (!array_eletype)
4305 return CompilerType();
4306
4307 CompilerType element_type(getASTContext(),
4308 array_eletype->getCanonicalTypeUnqualified());
4309
4310 // TODO: the real stride will be >= this value.. find the real one!
4311 if (stride)
4312 *stride = element_type.GetByteSize(nullptr);
4313
4314 return element_type;
4315 }
4316 return CompilerType();
4317}
4318
4319CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4320 uint64_t size) {
4321 if (type) {
4322 clang::QualType qual_type(GetCanonicalQualType(type));
4323 if (clang::ASTContext *ast_ctx = getASTContext()) {
4324 if (size != 0)
4325 return CompilerType(
4326 ast_ctx, ast_ctx->getConstantArrayType(
4327 qual_type, llvm::APInt(64, size),
4328 clang::ArrayType::ArraySizeModifier::Normal, 0));
4329 else
4330 return CompilerType(
4331 ast_ctx,
4332 ast_ctx->getIncompleteArrayType(
4333 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
4334 }
4335 }
4336
4337 return CompilerType();
4338}
4339
4340CompilerType
4341ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4342 if (type)
4343 return CompilerType(getASTContext(), GetCanonicalQualType(type));
4344 return CompilerType();
4345}
4346
4347static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4348 clang::QualType qual_type) {
4349 if (qual_type->isPointerType())
4350 qual_type = ast->getPointerType(
4351 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4352 else
4353 qual_type = qual_type.getUnqualifiedType();
4354 qual_type.removeLocalConst();
4355 qual_type.removeLocalRestrict();
4356 qual_type.removeLocalVolatile();
4357 return qual_type;
4358}
4359
4360CompilerType
4361ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4362 if (type)
4363 return CompilerType(
4364 getASTContext(),
4365 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4366 return CompilerType();
4367}
4368
4369int ClangASTContext::GetFunctionArgumentCount(
4370 lldb::opaque_compiler_type_t type) {
4371 if (type) {
4372 const clang::FunctionProtoType *func =
4373 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4374 if (func)
4375 return func->getNumParams();
4376 }
4377 return -1;
4378}
4379
4380CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4381 lldb::opaque_compiler_type_t type, size_t idx) {
4382 if (type) {
4383 const clang::FunctionProtoType *func =
4384 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4385 if (func) {
4386 const uint32_t num_args = func->getNumParams();
4387 if (idx < num_args)
4388 return CompilerType(getASTContext(), func->getParamType(idx));
4389 }
4390 }
4391 return CompilerType();
4392}
4393
4394CompilerType
4395ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4396 if (type) {
4397 clang::QualType qual_type(GetQualType(type));
4398 const clang::FunctionProtoType *func =
4399 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4400 if (func)
4401 return CompilerType(getASTContext(), func->getReturnType());
4402 }
4403 return CompilerType();
4404}
4405
4406size_t
4407ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4408 size_t num_functions = 0;
4409 if (type) {
4410 clang::QualType qual_type(GetCanonicalQualType(type));
4411 switch (qual_type->getTypeClass()) {
4412 case clang::Type::Record:
4413 if (GetCompleteQualType(getASTContext(), qual_type)) {
4414 const clang::RecordType *record_type =
4415 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4416 const clang::RecordDecl *record_decl = record_type->getDecl();
4417 assert(record_decl)(static_cast <bool> (record_decl) ? void (0) : __assert_fail
("record_decl", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 4417, __extension__ __PRETTY_FUNCTION__))
;
4418 const clang::CXXRecordDecl *cxx_record_decl =
4419 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4420 if (cxx_record_decl)
4421 num_functions = std::distance(cxx_record_decl->method_begin(),
4422 cxx_record_decl->method_end());
4423 }
4424 break;
4425
4426 case clang::Type::ObjCObjectPointer: {
4427 const clang::ObjCObjectPointerType *objc_class_type =
4428 qual_type->getAs<clang::ObjCObjectPointerType>();
4429 const clang::ObjCInterfaceType *objc_interface_type =
4430 objc_class_type->getInterfaceType();
4431 if (objc_interface_type &&
4432 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4433 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
4434 clang::ObjCInterfaceDecl *class_interface_decl =
4435 objc_interface_type->getDecl();
4436 if (class_interface_decl) {
4437 num_functions = std::distance(class_interface_decl->meth_begin(),
4438 class_interface_decl->meth_end());
4439 }
4440 }
4441 break;
4442 }
4443
4444 case clang::Type::ObjCObject:
4445 case clang::Type::ObjCInterface:
4446 if (GetCompleteType(type)) {
4447 const clang::ObjCObjectType *objc_class_type =
4448 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4449 if (objc_class_type) {
4450 clang::ObjCInterfaceDecl *class_interface_decl =
4451 objc_class_type->getInterface();
4452 if (class_interface_decl)
4453 num_functions = std::distance(class_interface_decl->meth_begin(),
4454 class_interface_decl->meth_end());
4455 }
4456 }
4457 break;
4458
4459 case clang::Type::Typedef:
4460 return CompilerType(getASTContext(),
4461 llvm::cast<clang::TypedefType>(qual_type)
4462 ->getDecl()
4463 ->getUnderlyingType())
4464 .GetNumMemberFunctions();
4465
4466 case clang::Type::Auto:
4467 return CompilerType(
4468 getASTContext(),
4469 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4470 .GetNumMemberFunctions();
4471
4472 case clang::Type::Elaborated:
4473 return CompilerType(
4474 getASTContext(),
4475 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4476 .GetNumMemberFunctions();
4477
4478 case clang::Type::Paren:
4479 return CompilerType(getASTContext(),
4480 llvm::cast<clang::ParenType>(qual_type)->desugar())
4481 .GetNumMemberFunctions();
4482
4483 default:
4484 break;
4485 }
4486 }
4487 return num_functions;
4488}
4489
4490TypeMemberFunctionImpl
4491ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4492 size_t idx) {
4493 std::string name;
4494 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4495 CompilerType clang_type;
4496 CompilerDecl clang_decl;
4497 if (type) {
4498 clang::QualType qual_type(GetCanonicalQualType(type));
4499 switch (qual_type->getTypeClass()) {
4500 case clang::Type::Record:
4501 if (GetCompleteQualType(getASTContext(), qual_type)) {
4502 const clang::RecordType *record_type =
4503 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4504 const clang::RecordDecl *record_decl = record_type->getDecl();
4505 assert(record_decl)(static_cast <bool> (record_decl) ? void (0) : __assert_fail
("record_decl", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 4505, __extension__ __PRETTY_FUNCTION__))
;
4506 const clang::CXXRecordDecl *cxx_record_decl =
4507 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4508 if (cxx_record_decl) {
4509 auto method_iter = cxx_record_decl->method_begin();
4510 auto method_end = cxx_record_decl->method_end();
4511 if (idx <
4512 static_cast<size_t>(std::distance(method_iter, method_end))) {
4513 std::advance(method_iter, idx);
4514 clang::CXXMethodDecl *cxx_method_decl =
4515 method_iter->getCanonicalDecl();
4516 if (cxx_method_decl) {
4517 name = cxx_method_decl->getDeclName().getAsString();
4518 if (cxx_method_decl->isStatic())
4519 kind = lldb::eMemberFunctionKindStaticMethod;
4520 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4521 kind = lldb::eMemberFunctionKindConstructor;
4522 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4523 kind = lldb::eMemberFunctionKindDestructor;
4524 else
4525 kind = lldb::eMemberFunctionKindInstanceMethod;
4526 clang_type = CompilerType(
4527 this, cxx_method_decl->getType().getAsOpaquePtr());
4528 clang_decl = CompilerDecl(this, cxx_method_decl);
4529 }
4530 }
4531 }
4532 }
4533 break;
4534
4535 case clang::Type::ObjCObjectPointer: {
4536 const clang::ObjCObjectPointerType *objc_class_type =
4537 qual_type->getAs<clang::ObjCObjectPointerType>();
4538 const clang::ObjCInterfaceType *objc_interface_type =
4539 objc_class_type->getInterfaceType();
4540 if (objc_interface_type &&
4541 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4542 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
4543 clang::ObjCInterfaceDecl *class_interface_decl =
4544 objc_interface_type->getDecl();
4545 if (class_interface_decl) {
4546 auto method_iter = class_interface_decl->meth_begin();
4547 auto method_end = class_interface_decl->meth_end();
4548 if (idx <
4549 static_cast<size_t>(std::distance(method_iter, method_end))) {
4550 std::advance(method_iter, idx);
4551 clang::ObjCMethodDecl *objc_method_decl =
4552 method_iter->getCanonicalDecl();
4553 if (objc_method_decl) {
4554 clang_decl = CompilerDecl(this, objc_method_decl);
4555 name = objc_method_decl->getSelector().getAsString();
4556 if (objc_method_decl->isClassMethod())
4557 kind = lldb::eMemberFunctionKindStaticMethod;
4558 else
4559 kind = lldb::eMemberFunctionKindInstanceMethod;
4560 }
4561 }
4562 }
4563 }
4564 break;
4565 }
4566
4567 case clang::Type::ObjCObject:
4568 case clang::Type::ObjCInterface:
4569 if (GetCompleteType(type)) {
4570 const clang::ObjCObjectType *objc_class_type =
4571 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4572 if (objc_class_type) {
4573 clang::ObjCInterfaceDecl *class_interface_decl =
4574 objc_class_type->getInterface();
4575 if (class_interface_decl) {
4576 auto method_iter = class_interface_decl->meth_begin();
4577 auto method_end = class_interface_decl->meth_end();
4578 if (idx <
4579 static_cast<size_t>(std::distance(method_iter, method_end))) {
4580 std::advance(method_iter, idx);
4581 clang::ObjCMethodDecl *objc_method_decl =
4582 method_iter->getCanonicalDecl();
4583 if (objc_method_decl) {
4584 clang_decl = CompilerDecl(this, objc_method_decl);
4585 name = objc_method_decl->getSelector().getAsString();
4586 if (objc_method_decl->isClassMethod())
4587 kind = lldb::eMemberFunctionKindStaticMethod;
4588 else
4589 kind = lldb::eMemberFunctionKindInstanceMethod;
4590 }
4591 }
4592 }
4593 }
4594 }
4595 break;
4596
4597 case clang::Type::Typedef:
4598 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4599 ->getDecl()
4600 ->getUnderlyingType()
4601 .getAsOpaquePtr(),
4602 idx);
4603
4604 case clang::Type::Auto:
4605 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4606 ->getDeducedType()
4607 .getAsOpaquePtr(),
4608 idx);
4609
4610 case clang::Type::Elaborated:
4611 return GetMemberFunctionAtIndex(
4612 llvm::cast<clang::ElaboratedType>(qual_type)
4613 ->getNamedType()
4614 .getAsOpaquePtr(),
4615 idx);
4616
4617 case clang::Type::Paren:
4618 return GetMemberFunctionAtIndex(
4619 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4620 idx);
4621
4622 default:
4623 break;
4624 }
4625 }
4626
4627 if (kind == eMemberFunctionKindUnknown)
4628 return TypeMemberFunctionImpl();
4629 else
4630 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
4631}
4632
4633CompilerType
4634ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4635 if (type)
4636 return CompilerType(getASTContext(),
4637 GetQualType(type).getNonReferenceType());
4638 return CompilerType();
4639}
4640
4641CompilerType ClangASTContext::CreateTypedefType(
4642 const CompilerType &type, const char *typedef_name,
4643 const CompilerDeclContext &compiler_decl_ctx) {
4644 if (type && typedef_name && typedef_name[0]) {
4645 ClangASTContext *ast =
4646 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4647 if (!ast)
4648 return CompilerType();
4649 clang::ASTContext *clang_ast = ast->getASTContext();
4650 clang::QualType qual_type(ClangUtil::GetQualType(type));
4651
4652 clang::DeclContext *decl_ctx =
4653 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4654 if (decl_ctx == nullptr)
4655 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4656
4657 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4658 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4659 &clang_ast->Idents.get(typedef_name),
4660 clang_ast->getTrivialTypeSourceInfo(qual_type));
4661
4662 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4663
4664 // Get a uniqued clang::QualType for the typedef decl type
4665 return CompilerType(clang_ast, clang_ast->getTypedefType(decl));
4666 }
4667 return CompilerType();
4668}
4669
4670CompilerType
4671ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4672 if (type) {
4673 clang::QualType qual_type(GetQualType(type));
4674 return CompilerType(getASTContext(),
4675 qual_type.getTypePtr()->getPointeeType());
4676 }
4677 return CompilerType();
4678}
4679
4680CompilerType
4681ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4682 if (type) {
4683 clang::QualType qual_type(GetQualType(type));
4684
4685 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4686 switch (type_class) {
4687 case clang::Type::ObjCObject:
4688 case clang::Type::ObjCInterface:
4689 return CompilerType(getASTContext(),
4690 getASTContext()->getObjCObjectPointerType(qual_type));
4691
4692 default:
4693 return CompilerType(getASTContext(),
4694 getASTContext()->getPointerType(qual_type));
4695 }
4696 }
4697 return CompilerType();
4698}
4699
4700CompilerType
4701ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4702 if (type)
4703 return CompilerType(this, getASTContext()
4704 ->getLValueReferenceType(GetQualType(type))
4705 .getAsOpaquePtr());
4706 else
4707 return CompilerType();
4708}
4709
4710CompilerType
4711ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4712 if (type)
4713 return CompilerType(this, getASTContext()
4714 ->getRValueReferenceType(GetQualType(type))
4715 .getAsOpaquePtr());
4716 else
4717 return CompilerType();
4718}
4719
4720CompilerType
4721ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4722 if (type) {
4723 clang::QualType result(GetQualType(type));
4724 result.addConst();
4725 return CompilerType(this, result.getAsOpaquePtr());
4726 }
4727 return CompilerType();
4728}
4729
4730CompilerType
4731ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4732 if (type) {
4733 clang::QualType result(GetQualType(type));
4734 result.addVolatile();
4735 return CompilerType(this, result.getAsOpaquePtr());
4736 }
4737 return CompilerType();
4738}
4739
4740CompilerType
4741ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4742 if (type) {
4743 clang::QualType result(GetQualType(type));
4744 result.addRestrict();
4745 return CompilerType(this, result.getAsOpaquePtr());
4746 }
4747 return CompilerType();
4748}
4749
4750CompilerType
4751ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4752 const char *typedef_name,
4753 const CompilerDeclContext &compiler_decl_ctx) {
4754 if (type) {
4755 clang::ASTContext *clang_ast = getASTContext();
4756 clang::QualType qual_type(GetQualType(type));
4757
4758 clang::DeclContext *decl_ctx =
4759 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4760 if (decl_ctx == nullptr)
4761 decl_ctx = getASTContext()->getTranslationUnitDecl();
4762
4763 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4764 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4765 &clang_ast->Idents.get(typedef_name),
4766 clang_ast->getTrivialTypeSourceInfo(qual_type));
4767
4768 clang::TagDecl *tdecl = nullptr;
4769 if (!qual_type.isNull()) {
4770 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4771 tdecl = rt->getDecl();
4772 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4773 tdecl = et->getDecl();
4774 }
4775
4776 // Check whether this declaration is an anonymous struct, union, or enum,
4777 // hidden behind a typedef. If so, we
4778 // try to check whether we have a typedef tag to attach to the original
4779 // record declaration
4780 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4781 tdecl->setTypedefNameForAnonDecl(decl);
4782
4783 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4784
4785 // Get a uniqued clang::QualType for the typedef decl type
4786 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4787 }
4788 return CompilerType();
4789}
4790
4791CompilerType
4792ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4793 if (type) {
4794 const clang::TypedefType *typedef_type =
4795 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4796 if (typedef_type)
4797 return CompilerType(getASTContext(),
4798 typedef_type->getDecl()->getUnderlyingType());
4799 }
4800 return CompilerType();
4801}
4802
4803//----------------------------------------------------------------------
4804// Create related types using the current type's AST
4805//----------------------------------------------------------------------
4806
4807CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
4808 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
4809}
4810//----------------------------------------------------------------------
4811// Exploring the type
4812//----------------------------------------------------------------------
4813
4814uint64_t ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
4815 ExecutionContextScope *exe_scope) {
4816 if (GetCompleteType(type)) {
4817 clang::QualType qual_type(GetCanonicalQualType(type));
4818 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4819 switch (type_class) {
4820 case clang::Type::Record:
4821 if (GetCompleteType(type))
4822 return getASTContext()->getTypeSize(qual_type);
4823 else
4824 return 0;
4825 break;
4826
4827 case clang::Type::ObjCInterface:
4828 case clang::Type::ObjCObject: {
4829 ExecutionContext exe_ctx(exe_scope);
4830 Process *process = exe_ctx.GetProcessPtr();
4831 if (process) {
4832 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4833 if (objc_runtime) {
4834 uint64_t bit_size = 0;
4835 if (objc_runtime->GetTypeBitSize(
4836 CompilerType(getASTContext(), qual_type), bit_size))
4837 return bit_size;
4838 }
4839 } else {
4840 static bool g_printed = false;
4841 if (!g_printed) {
4842 StreamString s;
4843 DumpTypeDescription(type, &s);
4844
4845 llvm::outs() << "warning: trying to determine the size of type ";
4846 llvm::outs() << s.GetString() << "\n";
4847 llvm::outs() << "without a valid ExecutionContext. this is not "
4848 "reliable. please file a bug against LLDB.\n";
4849 llvm::outs() << "backtrace:\n";
4850 llvm::sys::PrintStackTrace(llvm::outs());
4851 llvm::outs() << "\n";
4852 g_printed = true;
4853 }
4854 }
4855 }
4856 LLVM_FALLTHROUGH[[clang::fallthrough]];
4857 default:
4858 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
4859 if (bit_size == 0) {
4860 if (qual_type->isIncompleteArrayType())
4861 return getASTContext()->getTypeSize(
4862 qual_type->getArrayElementTypeNoTypeQual()
4863 ->getCanonicalTypeUnqualified());
4864 }
4865 if (qual_type->isObjCObjectOrInterfaceType())
4866 return bit_size +
4867 getASTContext()->getTypeSize(
4868 getASTContext()->ObjCBuiltinClassTy);
4869 return bit_size;
4870 }
4871 }
4872 return 0;
4873}
4874
4875size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
4876 if (GetCompleteType(type))
4877 return getASTContext()->getTypeAlign(GetQualType(type));
4878 return 0;
4879}
4880
4881lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
4882 uint64_t &count) {
4883 if (!type)
4884 return lldb::eEncodingInvalid;
4885
4886 count = 1;
4887 clang::QualType qual_type(GetCanonicalQualType(type));
4888
4889 switch (qual_type->getTypeClass()) {
4890 case clang::Type::UnaryTransform:
4891 break;
4892
4893 case clang::Type::FunctionNoProto:
4894 case clang::Type::FunctionProto:
4895 break;
4896
4897 case clang::Type::IncompleteArray:
4898 case clang::Type::VariableArray:
4899 break;
4900
4901 case clang::Type::ConstantArray:
4902 break;
4903
4904 case clang::Type::ExtVector:
4905 case clang::Type::Vector:
4906 // TODO: Set this to more than one???
4907 break;
4908
4909 case clang::Type::Builtin:
4910 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4911 case clang::BuiltinType::Void:
4912 break;
4913
4914 case clang::BuiltinType::Bool:
4915 case clang::BuiltinType::Char_S:
4916 case clang::BuiltinType::SChar:
4917 case clang::BuiltinType::WChar_S:
4918 case clang::BuiltinType::Char16:
4919 case clang::BuiltinType::Char32:
4920 case clang::BuiltinType::Short:
4921 case clang::BuiltinType::Int:
4922 case clang::BuiltinType::Long:
4923 case clang::BuiltinType::LongLong:
4924 case clang::BuiltinType::Int128:
4925 return lldb::eEncodingSint;
4926
4927 case clang::BuiltinType::Char_U:
4928 case clang::BuiltinType::UChar:
4929 case clang::BuiltinType::WChar_U:
4930 case clang::BuiltinType::UShort:
4931 case clang::BuiltinType::UInt:
4932 case clang::BuiltinType::ULong:
4933 case clang::BuiltinType::ULongLong:
4934 case clang::BuiltinType::UInt128:
4935 return lldb::eEncodingUint;
4936
4937 case clang::BuiltinType::Half:
4938 case clang::BuiltinType::Float:
4939 case clang::BuiltinType::Float16:
4940 case clang::BuiltinType::Float128:
4941 case clang::BuiltinType::Double:
4942 case clang::BuiltinType::LongDouble:
4943 return lldb::eEncodingIEEE754;
4944
4945 case clang::BuiltinType::ObjCClass:
4946 case clang::BuiltinType::ObjCId:
4947 case clang::BuiltinType::ObjCSel:
4948 return lldb::eEncodingUint;
4949
4950 case clang::BuiltinType::NullPtr:
4951 return lldb::eEncodingUint;
4952
4953 case clang::BuiltinType::Kind::ARCUnbridgedCast:
4954 case clang::BuiltinType::Kind::BoundMember:
4955 case clang::BuiltinType::Kind::BuiltinFn:
4956 case clang::BuiltinType::Kind::Dependent:
4957 case clang::BuiltinType::Kind::OCLClkEvent:
4958 case clang::BuiltinType::Kind::OCLEvent:
4959 case clang::BuiltinType::Kind::OCLImage1dRO:
4960 case clang::BuiltinType::Kind::OCLImage1dWO:
4961 case clang::BuiltinType::Kind::OCLImage1dRW:
4962 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
4963 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
4964 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
4965 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
4966 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
4967 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
4968 case clang::BuiltinType::Kind::OCLImage2dRO:
4969 case clang::BuiltinType::Kind::OCLImage2dWO:
4970 case clang::BuiltinType::Kind::OCLImage2dRW:
4971 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
4972 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
4973 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
4974 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
4975 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
4976 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
4977 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
4978 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
4979 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
4980 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
4981 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
4982 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
4983 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
4984 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
4985 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
4986 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
4987 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
4988 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
4989 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
4990 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
4991 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
4992 case clang::BuiltinType::Kind::OCLImage3dRO:
4993 case clang::BuiltinType::Kind::OCLImage3dWO:
4994 case clang::BuiltinType::Kind::OCLImage3dRW:
4995 case clang::BuiltinType::Kind::OCLQueue:
4996 case clang::BuiltinType::Kind::OCLReserveID:
4997 case clang::BuiltinType::Kind::OCLSampler:
4998 case clang::BuiltinType::Kind::OMPArraySection:
4999 case clang::BuiltinType::Kind::Overload:
5000 case clang::BuiltinType::Kind::PseudoObject:
5001 case clang::BuiltinType::Kind::UnknownAny:
5002 break;
5003 }
5004 break;
5005 // All pointer types are represented as unsigned integer encodings.
5006 // We may nee to add a eEncodingPointer if we ever need to know the
5007 // difference
5008 case clang::Type::ObjCObjectPointer:
5009 case clang::Type::BlockPointer:
5010 case clang::Type::Pointer:
5011 case clang::Type::LValueReference:
5012 case clang::Type::RValueReference:
5013 case clang::Type::MemberPointer:
5014 return lldb::eEncodingUint;
5015 case clang::Type::Complex: {
5016 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5017 if (qual_type->isComplexType())
5018 encoding = lldb::eEncodingIEEE754;
5019 else {
5020 const clang::ComplexType *complex_type =
5021 qual_type->getAsComplexIntegerType();
5022 if (complex_type)
5023 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5024 .GetEncoding(count);
5025 else
5026 encoding = lldb::eEncodingSint;
5027 }
5028 count = 2;
5029 return encoding;
5030 }
5031
5032 case clang::Type::ObjCInterface:
5033 break;
5034 case clang::Type::Record:
5035 break;
5036 case clang::Type::Enum:
5037 return lldb::eEncodingSint;
5038 case clang::Type::Typedef:
5039 return CompilerType(getASTContext(),
5040 llvm::cast<clang::TypedefType>(qual_type)
5041 ->getDecl()
5042 ->getUnderlyingType())
5043 .GetEncoding(count);
5044
5045 case clang::Type::Auto:
5046 return CompilerType(
5047 getASTContext(),
5048 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5049 .GetEncoding(count);
5050
5051 case clang::Type::Elaborated:
5052 return CompilerType(
5053 getASTContext(),
5054 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5055 .GetEncoding(count);
5056
5057 case clang::Type::Paren:
5058 return CompilerType(getASTContext(),
5059 llvm::cast<clang::ParenType>(qual_type)->desugar())
5060 .GetEncoding(count);
5061 case clang::Type::TypeOfExpr:
5062 return CompilerType(getASTContext(),
5063 llvm::cast<clang::TypeOfExprType>(qual_type)
5064 ->getUnderlyingExpr()
5065 ->getType())
5066 .GetEncoding(count);
5067 case clang::Type::TypeOf:
5068 return CompilerType(
5069 getASTContext(),
5070 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5071 .GetEncoding(count);
5072 case clang::Type::Decltype:
5073 return CompilerType(
5074 getASTContext(),
5075 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5076 .GetEncoding(count);
5077 case clang::Type::DependentSizedArray:
5078 case clang::Type::DependentSizedExtVector:
5079 case clang::Type::UnresolvedUsing:
5080 case clang::Type::Attributed:
5081 case clang::Type::TemplateTypeParm:
5082 case clang::Type::SubstTemplateTypeParm:
5083 case clang::Type::SubstTemplateTypeParmPack:
5084 case clang::Type::InjectedClassName:
5085 case clang::Type::DependentName:
5086 case clang::Type::DependentTemplateSpecialization:
5087 case clang::Type::PackExpansion:
5088 case clang::Type::ObjCObject:
5089
5090 case clang::Type::TemplateSpecialization:
5091 case clang::Type::DeducedTemplateSpecialization:
5092 case clang::Type::Atomic:
5093 case clang::Type::Adjusted:
5094 case clang::Type::Pipe:
5095 break;
5096
5097 // pointer type decayed from an array or function type.
5098 case clang::Type::Decayed:
5099 break;
5100 case clang::Type::ObjCTypeParam:
5101 break;
5102
5103 case clang::Type::DependentAddressSpace:
5104 break;
5105 }
5106 count = 0;
5107 return lldb::eEncodingInvalid;
5108}
5109
5110lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5111 if (!type)
5112 return lldb::eFormatDefault;
5113
5114 clang::QualType qual_type(GetCanonicalQualType(type));
5115
5116 switch (qual_type->getTypeClass()) {
5117 case clang::Type::UnaryTransform:
5118 break;
5119
5120 case clang::Type::FunctionNoProto:
5121 case clang::Type::FunctionProto:
5122 break;
5123
5124 case clang::Type::IncompleteArray:
5125 case clang::Type::VariableArray:
5126 break;
5127
5128 case clang::Type::ConstantArray:
5129 return lldb::eFormatVoid; // no value
5130
5131 case clang::Type::ExtVector:
5132 case clang::Type::Vector:
5133 break;
5134
5135 case clang::Type::Builtin:
5136 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5137 // default: assert(0 && "Unknown builtin type!");
5138 case clang::BuiltinType::UnknownAny:
5139 case clang::BuiltinType::Void:
5140 case clang::BuiltinType::BoundMember:
5141 break;
5142
5143 case clang::BuiltinType::Bool:
5144 return lldb::eFormatBoolean;
5145 case clang::BuiltinType::Char_S:
5146 case clang::BuiltinType::SChar:
5147 case clang::BuiltinType::WChar_S:
5148 case clang::BuiltinType::Char_U:
5149 case clang::BuiltinType::UChar:
5150 case clang::BuiltinType::WChar_U:
5151 return lldb::eFormatChar;
5152 case clang::BuiltinType::Char16:
5153 return lldb::eFormatUnicode16;
5154 case clang::BuiltinType::Char32:
5155 return lldb::eFormatUnicode32;
5156 case clang::BuiltinType::UShort:
5157 return lldb::eFormatUnsigned;
5158 case clang::BuiltinType::Short:
5159 return lldb::eFormatDecimal;
5160 case clang::BuiltinType::UInt:
5161 return lldb::eFormatUnsigned;
5162 case clang::BuiltinType::Int:
5163 return lldb::eFormatDecimal;
5164 case clang::BuiltinType::ULong:
5165 return lldb::eFormatUnsigned;
5166 case clang::BuiltinType::Long:
5167 return lldb::eFormatDecimal;
5168 case clang::BuiltinType::ULongLong:
5169 return lldb::eFormatUnsigned;
5170 case clang::BuiltinType::LongLong:
5171 return lldb::eFormatDecimal;
5172 case clang::BuiltinType::UInt128:
5173 return lldb::eFormatUnsigned;
5174 case clang::BuiltinType::Int128:
5175 return lldb::eFormatDecimal;
5176 case clang::BuiltinType::Half:
5177 case clang::BuiltinType::Float:
5178 case clang::BuiltinType::Double:
5179 case clang::BuiltinType::LongDouble:
5180 return lldb::eFormatFloat;
5181 default:
5182 return lldb::eFormatHex;
5183 }
5184 break;
5185 case clang::Type::ObjCObjectPointer:
5186 return lldb::eFormatHex;
5187 case clang::Type::BlockPointer:
5188 return lldb::eFormatHex;
5189 case clang::Type::Pointer:
5190 return lldb::eFormatHex;
5191 case clang::Type::LValueReference:
5192 case clang::Type::RValueReference:
5193 return lldb::eFormatHex;
5194 case clang::Type::MemberPointer:
5195 break;
5196 case clang::Type::Complex: {
5197 if (qual_type->isComplexType())
5198 return lldb::eFormatComplex;
5199 else
5200 return lldb::eFormatComplexInteger;
5201 }
5202 case clang::Type::ObjCInterface:
5203 break;
5204 case clang::Type::Record:
5205 break;
5206 case clang::Type::Enum:
5207 return lldb::eFormatEnum;
5208 case clang::Type::Typedef:
5209 return CompilerType(getASTContext(),
5210 llvm::cast<clang::TypedefType>(qual_type)
5211 ->getDecl()
5212 ->getUnderlyingType())
5213 .GetFormat();
5214 case clang::Type::Auto:
5215 return CompilerType(getASTContext(),
5216 llvm::cast<clang::AutoType>(qual_type)->desugar())
5217 .GetFormat();
5218 case clang::Type::Paren:
5219 return CompilerType(getASTContext(),
5220 llvm::cast<clang::ParenType>(qual_type)->desugar())
5221 .GetFormat();
5222 case clang::Type::Elaborated:
5223 return CompilerType(
5224 getASTContext(),
5225 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5226 .GetFormat();
5227 case clang::Type::TypeOfExpr:
5228 return CompilerType(getASTContext(),
5229 llvm::cast<clang::TypeOfExprType>(qual_type)
5230 ->getUnderlyingExpr()
5231 ->getType())
5232 .GetFormat();
5233 case clang::Type::TypeOf:
5234 return CompilerType(
5235 getASTContext(),
5236 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5237 .GetFormat();
5238 case clang::Type::Decltype:
5239 return CompilerType(
5240 getASTContext(),
5241 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5242 .GetFormat();
5243 case clang::Type::DependentSizedArray:
5244 case clang::Type::DependentSizedExtVector:
5245 case clang::Type::UnresolvedUsing:
5246 case clang::Type::Attributed:
5247 case clang::Type::TemplateTypeParm:
5248 case clang::Type::SubstTemplateTypeParm:
5249 case clang::Type::SubstTemplateTypeParmPack:
5250 case clang::Type::InjectedClassName:
5251 case clang::Type::DependentName:
5252 case clang::Type::DependentTemplateSpecialization:
5253 case clang::Type::PackExpansion:
5254 case clang::Type::ObjCObject:
5255
5256 case clang::Type::TemplateSpecialization:
5257 case clang::Type::DeducedTemplateSpecialization:
5258 case clang::Type::Atomic:
5259 case clang::Type::Adjusted:
5260 case clang::Type::Pipe:
5261 break;
5262
5263 // pointer type decayed from an array or function type.
5264 case clang::Type::Decayed:
5265 break;
5266 case clang::Type::ObjCTypeParam:
5267 break;
5268
5269 case clang::Type::DependentAddressSpace:
5270 break;
5271 }
5272 // We don't know hot to display this type...
5273 return lldb::eFormatBytes;
5274}
5275
5276static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5277 bool check_superclass) {
5278 while (class_interface_decl) {
5279 if (class_interface_decl->ivar_size() > 0)
5280 return true;
5281
5282 if (check_superclass)
5283 class_interface_decl = class_interface_decl->getSuperClass();
5284 else
5285 break;
5286 }
5287 return false;
5288}
5289
5290uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
5291 bool omit_empty_base_classes) {
5292 if (!type)
5293 return 0;
5294
5295 uint32_t num_children = 0;
5296 clang::QualType qual_type(GetQualType(type));
5297 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5298 switch (type_class) {
5299 case clang::Type::Builtin:
5300 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5301 case clang::BuiltinType::ObjCId: // child is Class
5302 case clang::BuiltinType::ObjCClass: // child is Class
5303 num_children = 1;
5304 break;
5305
5306 default:
5307 break;
5308 }
5309 break;
5310
5311 case clang::Type::Complex:
5312 return 0;
5313
5314 case clang::Type::Record:
5315 if (GetCompleteQualType(getASTContext(), qual_type)) {
5316 const clang::RecordType *record_type =
5317 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5318 const clang::RecordDecl *record_decl = record_type->getDecl();
5319 assert(record_decl)(static_cast <bool> (record_decl) ? void (0) : __assert_fail
("record_decl", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 5319, __extension__ __PRETTY_FUNCTION__))
;
5320 const clang::CXXRecordDecl *cxx_record_decl =
5321 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5322 if (cxx_record_decl) {
5323 if (omit_empty_base_classes) {
5324 // Check each base classes to see if it or any of its
5325 // base classes contain any fields. This can help
5326 // limit the noise in variable views by not having to
5327 // show base classes that contain no members.
5328 clang::CXXRecordDecl::base_class_const_iterator base_class,
5329 base_class_end;
5330 for (base_class = cxx_record_decl->bases_begin(),
5331 base_class_end = cxx_record_decl->bases_end();
5332 base_class != base_class_end; ++base_class) {
5333 const clang::CXXRecordDecl *base_class_decl =
5334 llvm::cast<clang::CXXRecordDecl>(
5335 base_class->getType()
5336 ->getAs<clang::RecordType>()
5337 ->getDecl());
5338
5339 // Skip empty base classes
5340 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5341 continue;
5342
5343 num_children++;
5344 }
5345 } else {
5346 // Include all base classes
5347 num_children += cxx_record_decl->getNumBases();
5348 }
5349 }
5350 clang::RecordDecl::field_iterator field, field_end;
5351 for (field = record_decl->field_begin(),
5352 field_end = record_decl->field_end();
5353 field != field_end; ++field)
5354 ++num_children;
5355 }
5356 break;
5357
5358 case clang::Type::ObjCObject:
5359 case clang::Type::ObjCInterface:
5360 if (GetCompleteQualType(getASTContext(), qual_type)) {
5361 const clang::ObjCObjectType *objc_class_type =
5362 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5363 assert(objc_class_type)(static_cast <bool> (objc_class_type) ? void (0) : __assert_fail
("objc_class_type", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 5363, __extension__ __PRETTY_FUNCTION__))
;
5364 if (objc_class_type) {
5365 clang::ObjCInterfaceDecl *class_interface_decl =
5366 objc_class_type->getInterface();
5367
5368 if (class_interface_decl) {
5369
5370 clang::ObjCInterfaceDecl *superclass_interface_decl =
5371 class_interface_decl->getSuperClass();
5372 if (superclass_interface_decl) {
5373 if (omit_empty_base_classes) {
5374 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5375 ++num_children;
5376 } else
5377 ++num_children;
5378 }
5379
5380 num_children += class_interface_decl->ivar_size();
5381 }
5382 }
5383 }
5384 break;
5385
5386 case clang::Type::ObjCObjectPointer: {
5387 const clang::ObjCObjectPointerType *pointer_type =
5388 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5389 clang::QualType pointee_type = pointer_type->getPointeeType();
5390 uint32_t num_pointee_children =
5391 CompilerType(getASTContext(), pointee_type)
5392 .GetNumChildren(omit_empty_base_classes);
5393 // If this type points to a simple type, then it has 1 child
5394 if (num_pointee_children == 0)
5395 num_children = 1;
5396 else
5397 num_children = num_pointee_children;
5398 } break;
5399
5400 case clang::Type::Vector:
5401 case clang::Type::ExtVector:
5402 num_children =
5403 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5404 break;
5405
5406 case clang::Type::ConstantArray:
5407 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5408 ->getSize()
5409 .getLimitedValue();
5410 break;
5411
5412 case clang::Type::Pointer: {
5413 const clang::PointerType *pointer_type =
5414 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5415 clang::QualType pointee_type(pointer_type->getPointeeType());
5416 uint32_t num_pointee_children =
5417 CompilerType(getASTContext(), pointee_type)
5418 .GetNumChildren(omit_empty_base_classes);
5419 if (num_pointee_children == 0) {
5420 // We have a pointer to a pointee type that claims it has no children.
5421 // We will want to look at
5422 num_children = GetNumPointeeChildren(pointee_type);
5423 } else
5424 num_children = num_pointee_children;
5425 } break;
5426
5427 case clang::Type::LValueReference:
5428 case clang::Type::RValueReference: {
5429 const clang::ReferenceType *reference_type =
5430 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5431 clang::QualType pointee_type = reference_type->getPointeeType();
5432 uint32_t num_pointee_children =
5433 CompilerType(getASTContext(), pointee_type)
5434 .GetNumChildren(omit_empty_base_classes);
5435 // If this type points to a simple type, then it has 1 child
5436 if (num_pointee_children == 0)
5437 num_children = 1;
5438 else
5439 num_children = num_pointee_children;
5440 } break;
5441
5442 case clang::Type::Typedef:
5443 num_children =
5444 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5445 ->getDecl()
5446 ->getUnderlyingType())
5447 .GetNumChildren(omit_empty_base_classes);
5448 break;
5449
5450 case clang::Type::Auto:
5451 num_children =
5452 CompilerType(getASTContext(),
5453 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5454 .GetNumChildren(omit_empty_base_classes);
5455 break;
5456
5457 case clang::Type::Elaborated:
5458 num_children =
5459 CompilerType(
5460 getASTContext(),
5461 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5462 .GetNumChildren(omit_empty_base_classes);
5463 break;
5464
5465 case clang::Type::Paren:
5466 num_children =
5467 CompilerType(getASTContext(),
5468 llvm::cast<clang::ParenType>(qual_type)->desugar())
5469 .GetNumChildren(omit_empty_base_classes);
5470 break;
5471 default:
5472 break;
5473 }
5474 return num_children;
5475}
5476
5477CompilerType ClangASTContext::GetBuiltinTypeByName(const ConstString &name) {
5478 return GetBasicType(GetBasicTypeEnumeration(name));
1
Calling 'ClangASTContext::GetBasicType'
5479}
5480
5481lldb::BasicType
5482ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5483 if (type) {
5484 clang::QualType qual_type(GetQualType(type));
5485 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5486 if (type_class == clang::Type::Builtin) {
5487 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5488 case clang::BuiltinType::Void:
5489 return eBasicTypeVoid;
5490 case clang::BuiltinType::Bool:
5491 return eBasicTypeBool;
5492 case clang::BuiltinType::Char_S:
5493 return eBasicTypeSignedChar;
5494 case clang::BuiltinType::Char_U:
5495 return eBasicTypeUnsignedChar;
5496 case clang::BuiltinType::Char16:
5497 return eBasicTypeChar16;
5498 case clang::BuiltinType::Char32:
5499 return eBasicTypeChar32;
5500 case clang::BuiltinType::UChar:
5501 return eBasicTypeUnsignedChar;
5502 case clang::BuiltinType::SChar:
5503 return eBasicTypeSignedChar;
5504 case clang::BuiltinType::WChar_S:
5505 return eBasicTypeSignedWChar;
5506 case clang::BuiltinType::WChar_U:
5507 return eBasicTypeUnsignedWChar;
5508 case clang::BuiltinType::Short:
5509 return eBasicTypeShort;
5510 case clang::BuiltinType::UShort:
5511 return eBasicTypeUnsignedShort;
5512 case clang::BuiltinType::Int:
5513 return eBasicTypeInt;
5514 case clang::BuiltinType::UInt:
5515 return eBasicTypeUnsignedInt;
5516 case clang::BuiltinType::Long:
5517 return eBasicTypeLong;
5518 case clang::BuiltinType::ULong:
5519 return eBasicTypeUnsignedLong;
5520 case clang::BuiltinType::LongLong:
5521 return eBasicTypeLongLong;
5522 case clang::BuiltinType::ULongLong:
5523 return eBasicTypeUnsignedLongLong;
5524 case clang::BuiltinType::Int128:
5525 return eBasicTypeInt128;
5526 case clang::BuiltinType::UInt128:
5527 return eBasicTypeUnsignedInt128;
5528
5529 case clang::BuiltinType::Half:
5530 return eBasicTypeHalf;
5531 case clang::BuiltinType::Float:
5532 return eBasicTypeFloat;
5533 case clang::BuiltinType::Double:
5534 return eBasicTypeDouble;
5535 case clang::BuiltinType::LongDouble:
5536 return eBasicTypeLongDouble;
5537
5538 case clang::BuiltinType::NullPtr:
5539 return eBasicTypeNullPtr;
5540 case clang::BuiltinType::ObjCId:
5541 return eBasicTypeObjCID;
5542 case clang::BuiltinType::ObjCClass:
5543 return eBasicTypeObjCClass;
5544 case clang::BuiltinType::ObjCSel:
5545 return eBasicTypeObjCSel;
5546 default:
5547 return eBasicTypeOther;
5548 }
5549 }
5550 }
5551 return eBasicTypeInvalid;
5552}
5553
5554void ClangASTContext::ForEachEnumerator(
5555 lldb::opaque_compiler_type_t type,
5556 std::function<bool(const CompilerType &integer_type,
5557 const ConstString &name,
5558 const llvm::APSInt &value)> const &callback) {
5559 const clang::EnumType *enum_type =
5560 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5561 if (enum_type) {
5562 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5563 if (enum_decl) {
5564 CompilerType integer_type(this,
5565 enum_decl->getIntegerType().getAsOpaquePtr());
5566
5567 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5568 for (enum_pos = enum_decl->enumerator_begin(),
5569 enum_end_pos = enum_decl->enumerator_end();
5570 enum_pos != enum_end_pos; ++enum_pos) {
5571 ConstString name(enum_pos->getNameAsString().c_str());
5572 if (!callback(integer_type, name, enum_pos->getInitVal()))
5573 break;
5574 }
5575 }
5576 }
5577}
5578
5579#pragma mark Aggregate Types
5580
5581uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5582 if (!type)
5583 return 0;
5584
5585 uint32_t count = 0;
5586 clang::QualType qual_type(GetCanonicalQualType(type));
5587 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5588 switch (type_class) {
5589 case clang::Type::Record:
5590 if (GetCompleteType(type)) {
5591 const clang::RecordType *record_type =
5592 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5593 if (record_type) {
5594 clang::RecordDecl *record_decl = record_type->getDecl();
5595 if (record_decl) {
5596 uint32_t field_idx = 0;
5597 clang::RecordDecl::field_iterator field, field_end;
5598 for (field = record_decl->field_begin(),
5599 field_end = record_decl->field_end();
5600 field != field_end; ++field)
5601 ++field_idx;
5602 count = field_idx;
5603 }
5604 }
5605 }
5606 break;
5607
5608 case clang::Type::Typedef:
5609 count =
5610 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5611 ->getDecl()
5612 ->getUnderlyingType())
5613 .GetNumFields();
5614 break;
5615
5616 case clang::Type::Auto:
5617 count =
5618 CompilerType(getASTContext(),
5619 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5620 .GetNumFields();
5621 break;
5622
5623 case clang::Type::Elaborated:
5624 count = CompilerType(
5625 getASTContext(),
5626 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5627 .GetNumFields();
5628 break;
5629
5630 case clang::Type::Paren:
5631 count = CompilerType(getASTContext(),
5632 llvm::cast<clang::ParenType>(qual_type)->desugar())
5633 .GetNumFields();
5634 break;
5635
5636 case clang::Type::ObjCObjectPointer: {
5637 const clang::ObjCObjectPointerType *objc_class_type =
5638 qual_type->getAs<clang::ObjCObjectPointerType>();
5639 const clang::ObjCInterfaceType *objc_interface_type =
5640 objc_class_type->getInterfaceType();
5641 if (objc_interface_type &&
5642 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5643 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
5644 clang::ObjCInterfaceDecl *class_interface_decl =
5645 objc_interface_type->getDecl();
5646 if (class_interface_decl) {
5647 count = class_interface_decl->ivar_size();
5648 }
5649 }
5650 break;
5651 }
5652
5653 case clang::Type::ObjCObject:
5654 case clang::Type::ObjCInterface:
5655 if (GetCompleteType(type)) {
5656 const clang::ObjCObjectType *objc_class_type =
5657 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5658 if (objc_class_type) {
5659 clang::ObjCInterfaceDecl *class_interface_decl =
5660 objc_class_type->getInterface();
5661
5662 if (class_interface_decl)
5663 count = class_interface_decl->ivar_size();
5664 }
5665 }
5666 break;
5667
5668 default:
5669 break;
5670 }
5671 return count;
5672}
5673
5674static lldb::opaque_compiler_type_t
5675GetObjCFieldAtIndex(clang::ASTContext *ast,
5676 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5677 std::string &name, uint64_t *bit_offset_ptr,
5678 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5679 if (class_interface_decl) {
5680 if (idx < (class_interface_decl->ivar_size())) {
5681 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5682 ivar_end = class_interface_decl->ivar_end();
5683 uint32_t ivar_idx = 0;
5684
5685 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5686 ++ivar_pos, ++ivar_idx) {
5687 if (ivar_idx == idx) {
5688 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5689
5690 clang::QualType ivar_qual_type(ivar_decl->getType());
5691
5692 name.assign(ivar_decl->getNameAsString());
5693
5694 if (bit_offset_ptr) {
5695 const clang::ASTRecordLayout &interface_layout =
5696 ast->getASTObjCInterfaceLayout(class_interface_decl);
5697 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5698 }
5699
5700 const bool is_bitfield = ivar_pos->isBitField();
5701
5702 if (bitfield_bit_size_ptr) {
5703 *bitfield_bit_size_ptr = 0;
5704
5705 if (is_bitfield && ast) {
5706 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5707 llvm::APSInt bitfield_apsint;
5708 if (bitfield_bit_size_expr &&
5709 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5710 *ast)) {
5711 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5712 }
5713 }
5714 }
5715 if (is_bitfield_ptr)
5716 *is_bitfield_ptr = is_bitfield;
5717
5718 return ivar_qual_type.getAsOpaquePtr();
5719 }
5720 }
5721 }
5722 }
5723 return nullptr;
5724}
5725
5726CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5727 size_t idx, std::string &name,
5728 uint64_t *bit_offset_ptr,
5729 uint32_t *bitfield_bit_size_ptr,
5730 bool *is_bitfield_ptr) {
5731 if (!type)
5732 return CompilerType();
5733
5734 clang::QualType qual_type(GetCanonicalQualType(type));
5735 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5736 switch (type_class) {
5737 case clang::Type::Record:
5738 if (GetCompleteType(type)) {
5739 const clang::RecordType *record_type =
5740 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5741 const clang::RecordDecl *record_decl = record_type->getDecl();
5742 uint32_t field_idx = 0;
5743 clang::RecordDecl::field_iterator field, field_end;
5744 for (field = record_decl->field_begin(),
5745 field_end = record_decl->field_end();
5746 field != field_end; ++field, ++field_idx) {
5747 if (idx == field_idx) {
5748 // Print the member type if requested
5749 // Print the member name and equal sign
5750 name.assign(field->getNameAsString());
5751
5752 // Figure out the type byte size (field_type_info.first) and
5753 // alignment (field_type_info.second) from the AST context.
5754 if (bit_offset_ptr) {
5755 const clang::ASTRecordLayout &record_layout =
5756 getASTContext()->getASTRecordLayout(record_decl);
5757 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
5758 }
5759
5760 const bool is_bitfield = field->isBitField();
5761
5762 if (bitfield_bit_size_ptr) {
5763 *bitfield_bit_size_ptr = 0;
5764
5765 if (is_bitfield) {
5766 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5767 llvm::APSInt bitfield_apsint;
5768 if (bitfield_bit_size_expr &&
5769 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5770 *getASTContext())) {
5771 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5772 }
5773 }
5774 }
5775 if (is_bitfield_ptr)
5776 *is_bitfield_ptr = is_bitfield;
5777
5778 return CompilerType(getASTContext(), field->getType());
5779 }
5780 }
5781 }
5782 break;
5783
5784 case clang::Type::ObjCObjectPointer: {
5785 const clang::ObjCObjectPointerType *objc_class_type =
5786 qual_type->getAs<clang::ObjCObjectPointerType>();
5787 const clang::ObjCInterfaceType *objc_interface_type =
5788 objc_class_type->getInterfaceType();
5789 if (objc_interface_type &&
5790 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5791 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
5792 clang::ObjCInterfaceDecl *class_interface_decl =
5793 objc_interface_type->getDecl();
5794 if (class_interface_decl) {
5795 return CompilerType(
5796 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5797 idx, name, bit_offset_ptr,
5798 bitfield_bit_size_ptr, is_bitfield_ptr));
5799 }
5800 }
5801 break;
5802 }
5803
5804 case clang::Type::ObjCObject:
5805 case clang::Type::ObjCInterface:
5806 if (GetCompleteType(type)) {
5807 const clang::ObjCObjectType *objc_class_type =
5808 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5809 assert(objc_class_type)(static_cast <bool> (objc_class_type) ? void (0) : __assert_fail
("objc_class_type", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 5809, __extension__ __PRETTY_FUNCTION__))
;
5810 if (objc_class_type) {
5811 clang::ObjCInterfaceDecl *class_interface_decl =
5812 objc_class_type->getInterface();
5813 return CompilerType(
5814 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5815 idx, name, bit_offset_ptr,
5816 bitfield_bit_size_ptr, is_bitfield_ptr));
5817 }
5818 }
5819 break;
5820
5821 case clang::Type::Typedef:
5822 return CompilerType(getASTContext(),
5823 llvm::cast<clang::TypedefType>(qual_type)
5824 ->getDecl()
5825 ->getUnderlyingType())
5826 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5827 is_bitfield_ptr);
5828
5829 case clang::Type::Auto:
5830 return CompilerType(
5831 getASTContext(),
5832 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5833 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5834 is_bitfield_ptr);
5835
5836 case clang::Type::Elaborated:
5837 return CompilerType(
5838 getASTContext(),
5839 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5840 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5841 is_bitfield_ptr);
5842
5843 case clang::Type::Paren:
5844 return CompilerType(getASTContext(),
5845 llvm::cast<clang::ParenType>(qual_type)->desugar())
5846 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5847 is_bitfield_ptr);
5848
5849 default:
5850 break;
5851 }
5852 return CompilerType();
5853}
5854
5855uint32_t
5856ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
5857 uint32_t count = 0;
5858 clang::QualType qual_type(GetCanonicalQualType(type));
5859 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5860 switch (type_class) {
5861 case clang::Type::Record:
5862 if (GetCompleteType(type)) {
5863 const clang::CXXRecordDecl *cxx_record_decl =
5864 qual_type->getAsCXXRecordDecl();
5865 if (cxx_record_decl)
5866 count = cxx_record_decl->getNumBases();
5867 }
5868 break;
5869
5870 case clang::Type::ObjCObjectPointer:
5871 count = GetPointeeType(type).GetNumDirectBaseClasses();
5872 break;
5873
5874 case clang::Type::ObjCObject:
5875 if (GetCompleteType(type)) {
5876 const clang::ObjCObjectType *objc_class_type =
5877 qual_type->getAsObjCQualifiedInterfaceType();
5878 if (objc_class_type) {
5879 clang::ObjCInterfaceDecl *class_interface_decl =
5880 objc_class_type->getInterface();
5881
5882 if (class_interface_decl && class_interface_decl->getSuperClass())
5883 count = 1;
5884 }
5885 }
5886 break;
5887 case clang::Type::ObjCInterface:
5888 if (GetCompleteType(type)) {
5889 const clang::ObjCInterfaceType *objc_interface_type =
5890 qual_type->getAs<clang::ObjCInterfaceType>();
5891 if (objc_interface_type) {
5892 clang::ObjCInterfaceDecl *class_interface_decl =
5893 objc_interface_type->getInterface();
5894
5895 if (class_interface_decl && class_interface_decl->getSuperClass())
5896 count = 1;
5897 }
5898 }
5899 break;
5900
5901 case clang::Type::Typedef:
5902 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5903 ->getDecl()
5904 ->getUnderlyingType()
5905 .getAsOpaquePtr());
5906 break;
5907
5908 case clang::Type::Auto:
5909 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
5910 ->getDeducedType()
5911 .getAsOpaquePtr());
5912 break;
5913
5914 case clang::Type::Elaborated:
5915 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
5916 ->getNamedType()
5917 .getAsOpaquePtr());
5918 break;
5919
5920 case clang::Type::Paren:
5921 return GetNumDirectBaseClasses(
5922 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5923
5924 default:
5925 break;
5926 }
5927 return count;
5928}
5929
5930uint32_t
5931ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
5932 uint32_t count = 0;
5933 clang::QualType qual_type(GetCanonicalQualType(type));
5934 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5935 switch (type_class) {
5936 case clang::Type::Record:
5937 if (GetCompleteType(type)) {
5938 const clang::CXXRecordDecl *cxx_record_decl =
5939 qual_type->getAsCXXRecordDecl();
5940 if (cxx_record_decl)
5941 count = cxx_record_decl->getNumVBases();
5942 }
5943 break;
5944
5945 case clang::Type::Typedef:
5946 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5947 ->getDecl()
5948 ->getUnderlyingType()
5949 .getAsOpaquePtr());
5950 break;
5951
5952 case clang::Type::Auto:
5953 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
5954 ->getDeducedType()
5955 .getAsOpaquePtr());
5956 break;
5957
5958 case clang::Type::Elaborated:
5959 count =
5960 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
5961 ->getNamedType()
5962 .getAsOpaquePtr());
5963 break;
5964
5965 case clang::Type::Paren:
5966 count = GetNumVirtualBaseClasses(
5967 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5968 break;
5969
5970 default:
5971 break;
5972 }
5973 return count;
5974}
5975
5976CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
5977 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
5978 clang::QualType qual_type(GetCanonicalQualType(type));
5979 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5980 switch (type_class) {
5981 case clang::Type::Record:
5982 if (GetCompleteType(type)) {
5983 const clang::CXXRecordDecl *cxx_record_decl =
5984 qual_type->getAsCXXRecordDecl();
5985 if (cxx_record_decl) {
5986 uint32_t curr_idx = 0;
5987 clang::CXXRecordDecl::base_class_const_iterator base_class,
5988 base_class_end;
5989 for (base_class = cxx_record_decl->bases_begin(),
5990 base_class_end = cxx_record_decl->bases_end();
5991 base_class != base_class_end; ++base_class, ++curr_idx) {
5992 if (curr_idx == idx) {
5993 if (bit_offset_ptr) {
5994 const clang::ASTRecordLayout &record_layout =
5995 getASTContext()->getASTRecordLayout(cxx_record_decl);
5996 const clang::CXXRecordDecl *base_class_decl =
5997 llvm::cast<clang::CXXRecordDecl>(
5998 base_class->getType()
5999 ->getAs<clang::RecordType>()
6000 ->getDecl());
6001 if (base_class->isVirtual())
6002 *bit_offset_ptr =
6003 record_layout.getVBaseClassOffset(base_class_decl)
6004 .getQuantity() *
6005 8;
6006 else
6007 *bit_offset_ptr =
6008 record_layout.getBaseClassOffset(base_class_decl)
6009 .getQuantity() *
6010 8;
6011 }
6012 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6013 }
6014 }
6015 }
6016 }
6017 break;
6018
6019 case clang::Type::ObjCObjectPointer:
6020 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6021
6022 case clang::Type::ObjCObject:
6023 if (idx == 0 && GetCompleteType(type)) {
6024 const clang::ObjCObjectType *objc_class_type =
6025 qual_type->getAsObjCQualifiedInterfaceType();
6026 if (objc_class_type) {
6027 clang::ObjCInterfaceDecl *class_interface_decl =
6028 objc_class_type->getInterface();
6029
6030 if (class_interface_decl) {
6031 clang::ObjCInterfaceDecl *superclass_interface_decl =
6032 class_interface_decl->getSuperClass();
6033 if (superclass_interface_decl) {
6034 if (bit_offset_ptr)
6035 *bit_offset_ptr = 0;
6036 return CompilerType(getASTContext(),
6037 getASTContext()->getObjCInterfaceType(
6038 superclass_interface_decl));
6039 }
6040 }
6041 }
6042 }
6043 break;
6044 case clang::Type::ObjCInterface:
6045 if (idx == 0 && GetCompleteType(type)) {
6046 const clang::ObjCObjectType *objc_interface_type =
6047 qual_type->getAs<clang::ObjCInterfaceType>();
6048 if (objc_interface_type) {
6049 clang::ObjCInterfaceDecl *class_interface_decl =
6050 objc_interface_type->getInterface();
6051
6052 if (class_interface_decl) {
6053 clang::ObjCInterfaceDecl *superclass_interface_decl =
6054 class_interface_decl->getSuperClass();
6055 if (superclass_interface_decl) {
6056 if (bit_offset_ptr)
6057 *bit_offset_ptr = 0;
6058 return CompilerType(getASTContext(),
6059 getASTContext()->getObjCInterfaceType(
6060 superclass_interface_decl));
6061 }
6062 }
6063 }
6064 }
6065 break;
6066
6067 case clang::Type::Typedef:
6068 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6069 ->getDecl()
6070 ->getUnderlyingType()
6071 .getAsOpaquePtr(),
6072 idx, bit_offset_ptr);
6073
6074 case clang::Type::Auto:
6075 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6076 ->getDeducedType()
6077 .getAsOpaquePtr(),
6078 idx, bit_offset_ptr);
6079
6080 case clang::Type::Elaborated:
6081 return GetDirectBaseClassAtIndex(
6082 llvm::cast<clang::ElaboratedType>(qual_type)
6083 ->getNamedType()
6084 .getAsOpaquePtr(),
6085 idx, bit_offset_ptr);
6086
6087 case clang::Type::Paren:
6088 return GetDirectBaseClassAtIndex(
6089 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6090 idx, bit_offset_ptr);
6091
6092 default:
6093 break;
6094 }
6095 return CompilerType();
6096}
6097
6098CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6099 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6100 clang::QualType qual_type(GetCanonicalQualType(type));
6101 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6102 switch (type_class) {
6103 case clang::Type::Record:
6104 if (GetCompleteType(type)) {
6105 const clang::CXXRecordDecl *cxx_record_decl =
6106 qual_type->getAsCXXRecordDecl();
6107 if (cxx_record_decl) {
6108 uint32_t curr_idx = 0;
6109 clang::CXXRecordDecl::base_class_const_iterator base_class,
6110 base_class_end;
6111 for (base_class = cxx_record_decl->vbases_begin(),
6112 base_class_end = cxx_record_decl->vbases_end();
6113 base_class != base_class_end; ++base_class, ++curr_idx) {
6114 if (curr_idx == idx) {
6115 if (bit_offset_ptr) {
6116 const clang::ASTRecordLayout &record_layout =
6117 getASTContext()->getASTRecordLayout(cxx_record_decl);
6118 const clang::CXXRecordDecl *base_class_decl =
6119 llvm::cast<clang::CXXRecordDecl>(
6120 base_class->getType()
6121 ->getAs<clang::RecordType>()
6122 ->getDecl());
6123 *bit_offset_ptr =
6124 record_layout.getVBaseClassOffset(base_class_decl)
6125 .getQuantity() *
6126 8;
6127 }
6128 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6129 }
6130 }
6131 }
6132 }
6133 break;
6134
6135 case clang::Type::Typedef:
6136 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6137 ->getDecl()
6138 ->getUnderlyingType()
6139 .getAsOpaquePtr(),
6140 idx, bit_offset_ptr);
6141
6142 case clang::Type::Auto:
6143 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6144 ->getDeducedType()
6145 .getAsOpaquePtr(),
6146 idx, bit_offset_ptr);
6147
6148 case clang::Type::Elaborated:
6149 return GetVirtualBaseClassAtIndex(
6150 llvm::cast<clang::ElaboratedType>(qual_type)
6151 ->getNamedType()
6152 .getAsOpaquePtr(),
6153 idx, bit_offset_ptr);
6154
6155 case clang::Type::Paren:
6156 return GetVirtualBaseClassAtIndex(
6157 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6158 idx, bit_offset_ptr);
6159
6160 default:
6161 break;
6162 }
6163 return CompilerType();
6164}
6165
6166// If a pointer to a pointee type (the clang_type arg) says that it has no
6167// children, then we either need to trust it, or override it and return a
6168// different result. For example, an "int *" has one child that is an integer,
6169// but a function pointer doesn't have any children. Likewise if a Record type
6170// claims it has no children, then there really is nothing to show.
6171uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6172 if (type.isNull())
6173 return 0;
6174
6175 clang::QualType qual_type(type.getCanonicalType());
6176 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6177 switch (type_class) {
6178 case clang::Type::Builtin:
6179 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6180 case clang::BuiltinType::UnknownAny:
6181 case clang::BuiltinType::Void:
6182 case clang::BuiltinType::NullPtr:
6183 case clang::BuiltinType::OCLEvent:
6184 case clang::BuiltinType::OCLImage1dRO:
6185 case clang::BuiltinType::OCLImage1dWO:
6186 case clang::BuiltinType::OCLImage1dRW:
6187 case clang::BuiltinType::OCLImage1dArrayRO:
6188 case clang::BuiltinType::OCLImage1dArrayWO:
6189 case clang::BuiltinType::OCLImage1dArrayRW:
6190 case clang::BuiltinType::OCLImage1dBufferRO:
6191 case clang::BuiltinType::OCLImage1dBufferWO:
6192 case clang::BuiltinType::OCLImage1dBufferRW:
6193 case clang::BuiltinType::OCLImage2dRO:
6194 case clang::BuiltinType::OCLImage2dWO:
6195 case clang::BuiltinType::OCLImage2dRW:
6196 case clang::BuiltinType::OCLImage2dArrayRO:
6197 case clang::BuiltinType::OCLImage2dArrayWO:
6198 case clang::BuiltinType::OCLImage2dArrayRW:
6199 case clang::BuiltinType::OCLImage3dRO:
6200 case clang::BuiltinType::OCLImage3dWO:
6201 case clang::BuiltinType::OCLImage3dRW:
6202 case clang::BuiltinType::OCLSampler:
6203 return 0;
6204 case clang::BuiltinType::Bool:
6205 case clang::BuiltinType::Char_U:
6206 case clang::BuiltinType::UChar:
6207 case clang::BuiltinType::WChar_U:
6208 case clang::BuiltinType::Char16:
6209 case clang::BuiltinType::Char32:
6210 case clang::BuiltinType::UShort:
6211 case clang::BuiltinType::UInt:
6212 case clang::BuiltinType::ULong:
6213 case clang::BuiltinType::ULongLong:
6214 case clang::BuiltinType::UInt128:
6215 case clang::BuiltinType::Char_S:
6216 case clang::BuiltinType::SChar:
6217 case clang::BuiltinType::WChar_S:
6218 case clang::BuiltinType::Short:
6219 case clang::BuiltinType::Int:
6220 case clang::BuiltinType::Long:
6221 case clang::BuiltinType::LongLong:
6222 case clang::BuiltinType::Int128:
6223 case clang::BuiltinType::Float:
6224 case clang::BuiltinType::Double:
6225 case clang::BuiltinType::LongDouble:
6226 case clang::BuiltinType::Dependent:
6227 case clang::BuiltinType::Overload:
6228 case clang::BuiltinType::ObjCId:
6229 case clang::BuiltinType::ObjCClass:
6230 case clang::BuiltinType::ObjCSel:
6231 case clang::BuiltinType::BoundMember:
6232 case clang::BuiltinType::Half:
6233 case clang::BuiltinType::ARCUnbridgedCast:
6234 case clang::BuiltinType::PseudoObject:
6235 case clang::BuiltinType::BuiltinFn:
6236 case clang::BuiltinType::OMPArraySection:
6237 return 1;
6238 default:
6239 return 0;
6240 }
6241 break;
6242
6243 case clang::Type::Complex:
6244 return 1;
6245 case clang::Type::Pointer:
6246 return 1;
6247 case clang::Type::BlockPointer:
6248 return 0; // If block pointers don't have debug info, then no children for
6249 // them
6250 case clang::Type::LValueReference:
6251 return 1;
6252 case clang::Type::RValueReference:
6253 return 1;
6254 case clang::Type::MemberPointer:
6255 return 0;
6256 case clang::Type::ConstantArray:
6257 return 0;
6258 case clang::Type::IncompleteArray:
6259 return 0;
6260 case clang::Type::VariableArray:
6261 return 0;
6262 case clang::Type::DependentSizedArray:
6263 return 0;
6264 case clang::Type::DependentSizedExtVector:
6265 return 0;
6266 case clang::Type::Vector:
6267 return 0;
6268 case clang::Type::ExtVector:
6269 return 0;
6270 case clang::Type::FunctionProto:
6271 return 0; // When we function pointers, they have no children...
6272 case clang::Type::FunctionNoProto:
6273 return 0; // When we function pointers, they have no children...
6274 case clang::Type::UnresolvedUsing:
6275 return 0;
6276 case clang::Type::Paren:
6277 return GetNumPointeeChildren(
6278 llvm::cast<clang::ParenType>(qual_type)->desugar());
6279 case clang::Type::Typedef:
6280 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6281 ->getDecl()
6282 ->getUnderlyingType());
6283 case clang::Type::Auto:
6284 return GetNumPointeeChildren(
6285 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6286 case clang::Type::Elaborated:
6287 return GetNumPointeeChildren(
6288 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6289 case clang::Type::TypeOfExpr:
6290 return GetNumPointeeChildren(llvm::cast<clang::TypeOfExprType>(qual_type)
6291 ->getUnderlyingExpr()
6292 ->getType());
6293 case clang::Type::TypeOf:
6294 return GetNumPointeeChildren(
6295 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType());
6296 case clang::Type::Decltype:
6297 return GetNumPointeeChildren(
6298 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType());
6299 case clang::Type::Record:
6300 return 0;
6301 case clang::Type::Enum:
6302 return 1;
6303 case clang::Type::TemplateTypeParm:
6304 return 1;
6305 case clang::Type::SubstTemplateTypeParm:
6306 return 1;
6307 case clang::Type::TemplateSpecialization:
6308 return 1;
6309 case clang::Type::InjectedClassName:
6310 return 0;
6311 case clang::Type::DependentName:
6312 return 1;
6313 case clang::Type::DependentTemplateSpecialization:
6314 return 1;
6315 case clang::Type::ObjCObject:
6316 return 0;
6317 case clang::Type::ObjCInterface:
6318 return 0;
6319 case clang::Type::ObjCObjectPointer:
6320 return 1;
6321 default:
6322 break;
6323 }
6324 return 0;
6325}
6326
6327CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6328 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6329 bool transparent_pointers, bool omit_empty_base_classes,
6330 bool ignore_array_bounds, std::string &child_name,
6331 uint32_t &child_byte_size, int32_t &child_byte_offset,
6332 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6333 bool &child_is_base_class, bool &child_is_deref_of_parent,
6334 ValueObject *valobj, uint64_t &language_flags) {
6335 if (!type)
6336 return CompilerType();
6337
6338 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6339 const clang::Type::TypeClass parent_type_class =
6340 parent_qual_type->getTypeClass();
6341 child_bitfield_bit_size = 0;
6342 child_bitfield_bit_offset = 0;
6343 child_is_base_class = false;
6344 language_flags = 0;
6345
6346 const bool idx_is_valid = idx < GetNumChildren(type, omit_empty_base_classes);
6347 uint32_t bit_offset;
6348 switch (parent_type_class) {
6349 case clang::Type::Builtin:
6350 if (idx_is_valid) {
6351 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6352 case clang::BuiltinType::ObjCId:
6353 case clang::BuiltinType::ObjCClass:
6354 child_name = "isa";
6355 child_byte_size =
6356 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6357 CHAR_BIT8;
6358 return CompilerType(getASTContext(),
6359 getASTContext()->ObjCBuiltinClassTy);
6360
6361 default:
6362 break;
6363 }
6364 }
6365 break;
6366
6367 case clang::Type::Record:
6368 if (idx_is_valid && GetCompleteType(type)) {
6369 const clang::RecordType *record_type =
6370 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6371 const clang::RecordDecl *record_decl = record_type->getDecl();
6372 assert(record_decl)(static_cast <bool> (record_decl) ? void (0) : __assert_fail
("record_decl", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 6372, __extension__ __PRETTY_FUNCTION__))
;
6373 const clang::ASTRecordLayout &record_layout =
6374 getASTContext()->getASTRecordLayout(record_decl);
6375 uint32_t child_idx = 0;
6376
6377 const clang::CXXRecordDecl *cxx_record_decl =
6378 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6379 if (cxx_record_decl) {
6380 // We might have base classes to print out first
6381 clang::CXXRecordDecl::base_class_const_iterator base_class,
6382 base_class_end;
6383 for (base_class = cxx_record_decl->bases_begin(),
6384 base_class_end = cxx_record_decl->bases_end();
6385 base_class != base_class_end; ++base_class) {
6386 const clang::CXXRecordDecl *base_class_decl = nullptr;
6387
6388 // Skip empty base classes
6389 if (omit_empty_base_classes) {
6390 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6391 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6392 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
6393 continue;
6394 }
6395
6396 if (idx == child_idx) {
6397 if (base_class_decl == nullptr)
6398 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6399 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6400
6401 if (base_class->isVirtual()) {
6402 bool handled = false;
6403 if (valobj) {
6404 Status err;
6405 AddressType addr_type = eAddressTypeInvalid;
6406 lldb::addr_t vtable_ptr_addr =
6407 valobj->GetCPPVTableAddress(addr_type);
6408
6409 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS(18446744073709551615UL) &&
6410 addr_type == eAddressTypeLoad) {
6411
6412 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
6413 Process *process = exe_ctx.GetProcessPtr();
6414 if (process) {
6415 clang::VTableContextBase *vtable_ctx =
6416 getASTContext()->getVTableContext();
6417 if (vtable_ctx) {
6418 if (vtable_ctx->isMicrosoft()) {
6419 clang::MicrosoftVTableContext *msoft_vtable_ctx =
6420 static_cast<clang::MicrosoftVTableContext *>(
6421 vtable_ctx);
6422
6423 if (vtable_ptr_addr) {
6424 const lldb::addr_t vbtable_ptr_addr =
6425 vtable_ptr_addr +
6426 record_layout.getVBPtrOffset().getQuantity();
6427
6428 const lldb::addr_t vbtable_ptr =
6429 process->ReadPointerFromMemory(vbtable_ptr_addr,
6430 err);
6431 if (vbtable_ptr != LLDB_INVALID_ADDRESS(18446744073709551615UL)) {
6432 // Get the index into the virtual base table. The
6433 // index is the index in uint32_t from vbtable_ptr
6434 const unsigned vbtable_index =
6435 msoft_vtable_ctx->getVBTableIndex(
6436 cxx_record_decl, base_class_decl);
6437 const lldb::addr_t base_offset_addr =
6438 vbtable_ptr + vbtable_index * 4;
6439 const uint32_t base_offset =
6440 process->ReadUnsignedIntegerFromMemory(
6441 base_offset_addr, 4, UINT32_MAX(4294967295U), err);
6442 if (base_offset != UINT32_MAX(4294967295U)) {
6443 handled = true;
6444 bit_offset = base_offset * 8;
6445 }
6446 }
6447 }
6448 } else {
6449 clang::ItaniumVTableContext *itanium_vtable_ctx =
6450 static_cast<clang::ItaniumVTableContext *>(
6451 vtable_ctx);
6452 if (vtable_ptr_addr) {
6453 const lldb::addr_t vtable_ptr =
6454 process->ReadPointerFromMemory(vtable_ptr_addr,
6455 err);
6456 if (vtable_ptr != LLDB_INVALID_ADDRESS(18446744073709551615UL)) {
6457 clang::CharUnits base_offset_offset =
6458 itanium_vtable_ctx->getVirtualBaseOffsetOffset(
6459 cxx_record_decl, base_class_decl);
6460 const lldb::addr_t base_offset_addr =
6461 vtable_ptr + base_offset_offset.getQuantity();
6462 const uint32_t base_offset_size =
6463 process->GetAddressByteSize();
6464 const uint64_t base_offset =
6465 process->ReadUnsignedIntegerFromMemory(
6466 base_offset_addr, base_offset_size,
6467 UINT32_MAX(4294967295U), err);
6468 if (base_offset < UINT32_MAX(4294967295U)) {
6469 handled = true;
6470 bit_offset = base_offset * 8;
6471 }
6472 }
6473 }
6474 }
6475 }
6476 }
6477 }
6478 }
6479 if (!handled)
6480 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6481 .getQuantity() *
6482 8;
6483 } else
6484 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6485 .getQuantity() *
6486 8;
6487
6488 // Base classes should be a multiple of 8 bits in size
6489 child_byte_offset = bit_offset / 8;
6490 CompilerType base_class_clang_type(getASTContext(),
6491 base_class->getType());
6492 child_name = base_class_clang_type.GetTypeName().AsCString("");
6493 uint64_t base_class_clang_type_bit_size =
6494 base_class_clang_type.GetBitSize(
6495 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL__null);
6496
6497 // Base classes bit sizes should be a multiple of 8 bits in size
6498 assert(base_class_clang_type_bit_size % 8 == 0)(static_cast <bool> (base_class_clang_type_bit_size % 8
== 0) ? void (0) : __assert_fail ("base_class_clang_type_bit_size % 8 == 0"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 6498, __extension__ __PRETTY_FUNCTION__))
;
6499 child_byte_size = base_class_clang_type_bit_size / 8;
6500 child_is_base_class = true;
6501 return base_class_clang_type;
6502 }
6503 // We don't increment the child index in the for loop since we might
6504 // be skipping empty base classes
6505 ++child_idx;
6506 }
6507 }
6508 // Make sure index is in range...
6509 uint32_t field_idx = 0;
6510 clang::RecordDecl::field_iterator field, field_end;
6511 for (field = record_decl->field_begin(),
6512 field_end = record_decl->field_end();
6513 field != field_end; ++field, ++field_idx, ++child_idx) {
6514 if (idx == child_idx) {
6515 // Print the member type if requested
6516 // Print the member name and equal sign
6517 child_name.assign(field->getNameAsString());
6518
6519 // Figure out the type byte size (field_type_info.first) and
6520 // alignment (field_type_info.second) from the AST context.
6521 CompilerType field_clang_type(getASTContext(), field->getType());
6522 assert(field_idx < record_layout.getFieldCount())(static_cast <bool> (field_idx < record_layout.getFieldCount
()) ? void (0) : __assert_fail ("field_idx < record_layout.getFieldCount()"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 6522, __extension__ __PRETTY_FUNCTION__))
;
6523 child_byte_size = field_clang_type.GetByteSize(
6524 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL__null);
6525 const uint32_t child_bit_size = child_byte_size * 8;
6526
6527 // Figure out the field offset within the current struct/union/class
6528 // type
6529 bit_offset = record_layout.getFieldOffset(field_idx);
6530 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6531 child_bitfield_bit_size)) {
6532 child_bitfield_bit_offset = bit_offset % child_bit_size;
6533 const uint32_t child_bit_offset =
6534 bit_offset - child_bitfield_bit_offset;
6535 child_byte_offset = child_bit_offset / 8;
6536 } else {
6537 child_byte_offset = bit_offset / 8;
6538 }
6539
6540 return field_clang_type;
6541 }
6542 }
6543 }
6544 break;
6545
6546 case clang::Type::ObjCObject:
6547 case clang::Type::ObjCInterface:
6548 if (idx_is_valid && GetCompleteType(type)) {
6549 const clang::ObjCObjectType *objc_class_type =
6550 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6551 assert(objc_class_type)(static_cast <bool> (objc_class_type) ? void (0) : __assert_fail
("objc_class_type", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 6551, __extension__ __PRETTY_FUNCTION__))
;
6552 if (objc_class_type) {
6553 uint32_t child_idx = 0;
6554 clang::ObjCInterfaceDecl *class_interface_decl =
6555 objc_class_type->getInterface();
6556
6557 if (class_interface_decl) {
6558
6559 const clang::ASTRecordLayout &interface_layout =
6560 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6561 clang::ObjCInterfaceDecl *superclass_interface_decl =
6562 class_interface_decl->getSuperClass();
6563 if (superclass_interface_decl) {
6564 if (omit_empty_base_classes) {
6565 CompilerType base_class_clang_type(
6566 getASTContext(), getASTContext()->getObjCInterfaceType(
6567 superclass_interface_decl));
6568 if (base_class_clang_type.GetNumChildren(
6569 omit_empty_base_classes) > 0) {
6570 if (idx == 0) {
6571 clang::QualType ivar_qual_type(
6572 getASTContext()->getObjCInterfaceType(
6573 superclass_interface_decl));
6574
6575 child_name.assign(
6576 superclass_interface_decl->getNameAsString());
6577
6578 clang::TypeInfo ivar_type_info =
6579 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6580
6581 child_byte_size = ivar_type_info.Width / 8;
6582 child_byte_offset = 0;
6583 child_is_base_class = true;
6584
6585 return CompilerType(getASTContext(), ivar_qual_type);
6586 }
6587
6588 ++child_idx;
6589 }
6590 } else
6591 ++child_idx;
6592 }
6593
6594 const uint32_t superclass_idx = child_idx;
6595
6596 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6597 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6598 ivar_end = class_interface_decl->ivar_end();
6599
6600 for (ivar_pos = class_interface_decl->ivar_begin();
6601 ivar_pos != ivar_end; ++ivar_pos) {
6602 if (child_idx == idx) {
6603 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6604
6605 clang::QualType ivar_qual_type(ivar_decl->getType());
6606
6607 child_name.assign(ivar_decl->getNameAsString());
6608
6609 clang::TypeInfo ivar_type_info =
6610 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6611
6612 child_byte_size = ivar_type_info.Width / 8;
6613
6614 // Figure out the field offset within the current
6615 // struct/union/class type
6616 // For ObjC objects, we can't trust the bit offset we get from
6617 // the Clang AST, since
6618 // that doesn't account for the space taken up by unbacked
6619 // properties, or from
6620 // the changing size of base classes that are newer than this
6621 // class.
6622 // So if we have a process around that we can ask about this
6623 // object, do so.
6624 child_byte_offset = LLDB_INVALID_IVAR_OFFSET(4294967295U);
6625 Process *process = nullptr;
6626 if (exe_ctx)
6627 process = exe_ctx->GetProcessPtr();
6628 if (process) {
6629 ObjCLanguageRuntime *objc_runtime =
6630 process->GetObjCLanguageRuntime();
6631 if (objc_runtime != nullptr) {
6632 CompilerType parent_ast_type(getASTContext(),
6633 parent_qual_type);
6634 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6635 parent_ast_type, ivar_decl->getNameAsString().c_str());
6636 }
6637 }
6638
6639 // Setting this to UINT32_MAX to make sure we don't compute it
6640 // twice...
6641 bit_offset = UINT32_MAX(4294967295U);
6642
6643 if (child_byte_offset ==
6644 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET(4294967295U))) {
6645 bit_offset = interface_layout.getFieldOffset(child_idx -
6646 superclass_idx);
6647 child_byte_offset = bit_offset / 8;
6648 }
6649
6650 // Note, the ObjC Ivar Byte offset is just that, it doesn't
6651 // account for the bit offset
6652 // of a bitfield within its containing object. So regardless of
6653 // where we get the byte
6654 // offset from, we still need to get the bit offset for
6655 // bitfields from the layout.
6656
6657 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6658 child_bitfield_bit_size)) {
6659 if (bit_offset == UINT32_MAX(4294967295U))
6660 bit_offset = interface_layout.getFieldOffset(
6661 child_idx - superclass_idx);
6662
6663 child_bitfield_bit_offset = bit_offset % 8;
6664 }
6665 return CompilerType(getASTContext(), ivar_qual_type);
6666 }
6667 ++child_idx;
6668 }
6669 }
6670 }
6671 }
6672 }
6673 break;
6674
6675 case clang::Type::ObjCObjectPointer:
6676 if (idx_is_valid) {
6677 CompilerType pointee_clang_type(GetPointeeType(type));
6678
6679 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6680 child_is_deref_of_parent = false;
6681 bool tmp_child_is_deref_of_parent = false;
6682 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6683 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6684 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6685 child_bitfield_bit_size, child_bitfield_bit_offset,
6686 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6687 language_flags);
6688 } else {
6689 child_is_deref_of_parent = true;
6690 const char *parent_name =
6691 valobj ? valobj->GetName().GetCString() : NULL__null;
6692 if (parent_name) {
6693 child_name.assign(1, '*');
6694 child_name += parent_name;
6695 }
6696
6697 // We have a pointer to an simple type
6698 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
6699 child_byte_size = pointee_clang_type.GetByteSize(
6700 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL__null);
6701 child_byte_offset = 0;
6702 return pointee_clang_type;
6703 }
6704 }
6705 }
6706 break;
6707
6708 case clang::Type::Vector:
6709 case clang::Type::ExtVector:
6710 if (idx_is_valid) {
6711 const clang::VectorType *array =
6712 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6713 if (array) {
6714 CompilerType element_type(getASTContext(), array->getElementType());
6715 if (element_type.GetCompleteType()) {
6716 char element_name[64];
6717 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64"l" "u" "]",
6718 static_cast<uint64_t>(idx));
6719 child_name.assign(element_name);
6720 child_byte_size = element_type.GetByteSize(
6721 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL__null);
6722 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6723 return element_type;
6724 }
6725 }
6726 }
6727 break;
6728
6729 case clang::Type::ConstantArray:
6730 case clang::Type::IncompleteArray:
6731 if (ignore_array_bounds || idx_is_valid) {
6732 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6733 if (array) {
6734 CompilerType element_type(getASTContext(), array->getElementType());
6735 if (element_type.GetCompleteType()) {
6736 child_name = llvm::formatv("[{0}]", idx);
6737 child_byte_size = element_type.GetByteSize(
6738 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL__null);
6739 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6740 return element_type;
6741 }
6742 }
6743 }
6744 break;
6745
6746 case clang::Type::Pointer: {
6747 CompilerType pointee_clang_type(GetPointeeType(type));
6748
6749 // Don't dereference "void *" pointers
6750 if (pointee_clang_type.IsVoidType())
6751 return CompilerType();
6752
6753 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6754 child_is_deref_of_parent = false;
6755 bool tmp_child_is_deref_of_parent = false;
6756 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6757 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6758 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6759 child_bitfield_bit_size, child_bitfield_bit_offset,
6760 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6761 language_flags);
6762 } else {
6763 child_is_deref_of_parent = true;
6764
6765 const char *parent_name =
6766 valobj ? valobj->GetName().GetCString() : NULL__null;
6767 if (parent_name) {
6768 child_name.assign(1, '*');
6769 child_name += parent_name;
6770 }
6771
6772 // We have a pointer to an simple type
6773 if (idx == 0) {
6774 child_byte_size = pointee_clang_type.GetByteSize(
6775 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL__null);
6776 child_byte_offset = 0;
6777 return pointee_clang_type;
6778 }
6779 }
6780 break;
6781 }
6782
6783 case clang::Type::LValueReference:
6784 case clang::Type::RValueReference:
6785 if (idx_is_valid) {
6786 const clang::ReferenceType *reference_type =
6787 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
6788 CompilerType pointee_clang_type(getASTContext(),
6789 reference_type->getPointeeType());
6790 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6791 child_is_deref_of_parent = false;
6792 bool tmp_child_is_deref_of_parent = false;
6793 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6794 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6795 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6796 child_bitfield_bit_size, child_bitfield_bit_offset,
6797 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6798 language_flags);
6799 } else {
6800 const char *parent_name =
6801 valobj ? valobj->GetName().GetCString() : NULL__null;
6802 if (parent_name) {
6803 child_name.assign(1, '&');
6804 child_name += parent_name;
6805 }
6806
6807 // We have a pointer to an simple type
6808 if (idx == 0) {
6809 child_byte_size = pointee_clang_type.GetByteSize(
6810 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL__null);
6811 child_byte_offset = 0;
6812 return pointee_clang_type;
6813 }
6814 }
6815 }
6816 break;
6817
6818 case clang::Type::Typedef: {
6819 CompilerType typedefed_clang_type(
6820 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)
6821 ->getDecl()
6822 ->getUnderlyingType());
6823 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
6824 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6825 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6826 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6827 child_is_deref_of_parent, valobj, language_flags);
6828 } break;
6829
6830 case clang::Type::Auto: {
6831 CompilerType elaborated_clang_type(
6832 getASTContext(),
6833 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
6834 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6835 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6836 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6837 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6838 child_is_deref_of_parent, valobj, language_flags);
6839 }
6840
6841 case clang::Type::Elaborated: {
6842 CompilerType elaborated_clang_type(
6843 getASTContext(),
6844 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
6845 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6846 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6847 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6848 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6849 child_is_deref_of_parent, valobj, language_flags);
6850 }
6851
6852 case clang::Type::Paren: {
6853 CompilerType paren_clang_type(
6854 getASTContext(),
6855 llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
6856 return paren_clang_type.GetChildCompilerTypeAtIndex(
6857 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6858 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6859 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6860 child_is_deref_of_parent, valobj, language_flags);
6861 }
6862
6863 default:
6864 break;
6865 }
6866 return CompilerType();
6867}
6868
6869static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
6870 const clang::CXXBaseSpecifier *base_spec,
6871 bool omit_empty_base_classes) {
6872 uint32_t child_idx = 0;
6873
6874 const clang::CXXRecordDecl *cxx_record_decl =
6875 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6876
6877 // const char *super_name = record_decl->getNameAsCString();
6878 // const char *base_name =
6879 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
6880 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
6881 //
6882 if (cxx_record_decl) {
6883 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6884 for (base_class = cxx_record_decl->bases_begin(),
6885 base_class_end = cxx_record_decl->bases_end();
6886 base_class != base_class_end; ++base_class) {
6887 if (omit_empty_base_classes) {
6888 if (BaseSpecifierIsEmpty(base_class))
6889 continue;
6890 }
6891
6892 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
6893 // super_name, base_name,
6894 // child_idx,
6895 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
6896 //
6897 //
6898 if (base_class == base_spec)
6899 return child_idx;
6900 ++child_idx;
6901 }
6902 }
6903
6904 return UINT32_MAX(4294967295U);
6905}
6906
6907static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
6908 clang::NamedDecl *canonical_decl,
6909 bool omit_empty_base_classes) {
6910 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
6911 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
6912 omit_empty_base_classes);
6913
6914 clang::RecordDecl::field_iterator field, field_end;
6915 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6916 field != field_end; ++field, ++child_idx) {
6917 if (field->getCanonicalDecl() == canonical_decl)
6918 return child_idx;
6919 }
6920
6921 return UINT32_MAX(4294967295U);
6922}
6923
6924// Look for a child member (doesn't include base classes, but it does include
6925// their members) in the type hierarchy. Returns an index path into "clang_type"
6926// on how to reach the appropriate member.
6927//
6928// class A
6929// {
6930// public:
6931// int m_a;
6932// int m_b;
6933// };
6934//
6935// class B
6936// {
6937// };
6938//
6939// class C :
6940// public B,
6941// public A
6942// {
6943// };
6944//
6945// If we have a clang type that describes "class C", and we wanted to looked
6946// "m_b" in it:
6947//
6948// With omit_empty_base_classes == false we would get an integer array back
6949// with:
6950// { 1, 1 }
6951// The first index 1 is the child index for "class A" within class C
6952// The second index 1 is the child index for "m_b" within class A
6953//
6954// With omit_empty_base_classes == true we would get an integer array back with:
6955// { 0, 1 }
6956// The first index 0 is the child index for "class A" within class C (since
6957// class B doesn't have any members it doesn't count)
6958// The second index 1 is the child index for "m_b" within class A
6959
6960size_t ClangASTContext::GetIndexOfChildMemberWithName(
6961 lldb::opaque_compiler_type_t type, const char *name,
6962 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
6963 if (type && name && name[0]) {
6964 clang::QualType qual_type(GetCanonicalQualType(type));
6965 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6966 switch (type_class) {
6967 case clang::Type::Record:
6968 if (GetCompleteType(type)) {
6969 const clang::RecordType *record_type =
6970 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6971 const clang::RecordDecl *record_decl = record_type->getDecl();
6972
6973 assert(record_decl)(static_cast <bool> (record_decl) ? void (0) : __assert_fail
("record_decl", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 6973, __extension__ __PRETTY_FUNCTION__))
;
6974 uint32_t child_idx = 0;
6975
6976 const clang::CXXRecordDecl *cxx_record_decl =
6977 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6978
6979 // Try and find a field that matches NAME
6980 clang::RecordDecl::field_iterator field, field_end;
6981 llvm::StringRef name_sref(name);
6982 for (field = record_decl->field_begin(),
6983 field_end = record_decl->field_end();
6984 field != field_end; ++field, ++child_idx) {
6985 llvm::StringRef field_name = field->getName();
6986 if (field_name.empty()) {
6987 CompilerType field_type(getASTContext(), field->getType());
6988 child_indexes.push_back(child_idx);
6989 if (field_type.GetIndexOfChildMemberWithName(
6990 name, omit_empty_base_classes, child_indexes))
6991 return child_indexes.size();
6992 child_indexes.pop_back();
6993
6994 } else if (field_name.equals(name_sref)) {
6995 // We have to add on the number of base classes to this index!
6996 child_indexes.push_back(
6997 child_idx + ClangASTContext::GetNumBaseClasses(
6998 cxx_record_decl, omit_empty_base_classes));
6999 return child_indexes.size();
7000 }
7001 }
7002
7003 if (cxx_record_decl) {
7004 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7005
7006 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7007
7008 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7009 // Didn't find things easily, lets let clang do its thang...
7010 clang::IdentifierInfo &ident_ref =
7011 getASTContext()->Idents.get(name_sref);
7012 clang::DeclarationName decl_name(&ident_ref);
7013
7014 clang::CXXBasePaths paths;
7015 if (cxx_record_decl->lookupInBases(
7016 [decl_name](const clang::CXXBaseSpecifier *specifier,
7017 clang::CXXBasePath &path) {
7018 return clang::CXXRecordDecl::FindOrdinaryMember(
7019 specifier, path, decl_name);
7020 },
7021 paths)) {
7022 clang::CXXBasePaths::const_paths_iterator path,
7023 path_end = paths.end();
7024 for (path = paths.begin(); path != path_end; ++path) {
7025 const size_t num_path_elements = path->size();
7026 for (size_t e = 0; e < num_path_elements; ++e) {
7027 clang::CXXBasePathElement elem = (*path)[e];
7028
7029 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7030 omit_empty_base_classes);
7031 if (child_idx == UINT32_MAX(4294967295U)) {
7032 child_indexes.clear();
7033 return 0;
7034 } else {
7035 child_indexes.push_back(child_idx);
7036 parent_record_decl = llvm::cast<clang::RecordDecl>(
7037 elem.Base->getType()
7038 ->getAs<clang::RecordType>()
7039 ->getDecl());
7040 }
7041 }
7042 for (clang::NamedDecl *path_decl : path->Decls) {
7043 child_idx = GetIndexForRecordChild(
7044 parent_record_decl, path_decl, omit_empty_base_classes);
7045 if (child_idx == UINT32_MAX(4294967295U)) {
7046 child_indexes.clear();
7047 return 0;
7048 } else {
7049 child_indexes.push_back(child_idx);
7050 }
7051 }
7052 }
7053 return child_indexes.size();
7054 }
7055 }
7056 }
7057 break;
7058
7059 case clang::Type::ObjCObject:
7060 case clang::Type::ObjCInterface:
7061 if (GetCompleteType(type)) {
7062 llvm::StringRef name_sref(name);
7063 const clang::ObjCObjectType *objc_class_type =
7064 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7065 assert(objc_class_type)(static_cast <bool> (objc_class_type) ? void (0) : __assert_fail
("objc_class_type", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 7065, __extension__ __PRETTY_FUNCTION__))
;
7066 if (objc_class_type) {
7067 uint32_t child_idx = 0;
7068 clang::ObjCInterfaceDecl *class_interface_decl =
7069 objc_class_type->getInterface();
7070
7071 if (class_interface_decl) {
7072 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7073 ivar_end = class_interface_decl->ivar_end();
7074 clang::ObjCInterfaceDecl *superclass_interface_decl =
7075 class_interface_decl->getSuperClass();
7076
7077 for (ivar_pos = class_interface_decl->ivar_begin();
7078 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7079 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7080
7081 if (ivar_decl->getName().equals(name_sref)) {
7082 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7083 (omit_empty_base_classes &&
7084 ObjCDeclHasIVars(superclass_interface_decl, true)))
7085 ++child_idx;
7086
7087 child_indexes.push_back(child_idx);
7088 return child_indexes.size();
7089 }
7090 }
7091
7092 if (superclass_interface_decl) {
7093 // The super class index is always zero for ObjC classes,
7094 // so we push it onto the child indexes in case we find
7095 // an ivar in our superclass...
7096 child_indexes.push_back(0);
7097
7098 CompilerType superclass_clang_type(
7099 getASTContext(), getASTContext()->getObjCInterfaceType(
7100 superclass_interface_decl));
7101 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7102 name, omit_empty_base_classes, child_indexes)) {
7103 // We did find an ivar in a superclass so just
7104 // return the results!
7105 return child_indexes.size();
7106 }
7107
7108 // We didn't find an ivar matching "name" in our
7109 // superclass, pop the superclass zero index that
7110 // we pushed on above.
7111 child_indexes.pop_back();
7112 }
7113 }
7114 }
7115 }
7116 break;
7117
7118 case clang::Type::ObjCObjectPointer: {
7119 CompilerType objc_object_clang_type(
7120 getASTContext(),
7121 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7122 ->getPointeeType());
7123 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7124 name, omit_empty_base_classes, child_indexes);
7125 } break;
7126
7127 case clang::Type::ConstantArray: {
7128 // const clang::ConstantArrayType *array =
7129 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7130 // const uint64_t element_count =
7131 // array->getSize().getLimitedValue();
7132 //
7133 // if (idx < element_count)
7134 // {
7135 // std::pair<uint64_t, unsigned> field_type_info =
7136 // ast->getTypeInfo(array->getElementType());
7137 //
7138 // char element_name[32];
7139 // ::snprintf (element_name, sizeof (element_name),
7140 // "%s[%u]", parent_name ? parent_name : "", idx);
7141 //
7142 // child_name.assign(element_name);
7143 // assert(field_type_info.first % 8 == 0);
7144 // child_byte_size = field_type_info.first / 8;
7145 // child_byte_offset = idx * child_byte_size;
7146 // return array->getElementType().getAsOpaquePtr();
7147 // }
7148 } break;
7149
7150 // case clang::Type::MemberPointerType:
7151 // {
7152 // MemberPointerType *mem_ptr_type =
7153 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7154 // clang::QualType pointee_type =
7155 // mem_ptr_type->getPointeeType();
7156 //
7157 // if (ClangASTContext::IsAggregateType
7158 // (pointee_type.getAsOpaquePtr()))
7159 // {
7160 // return GetIndexOfChildWithName (ast,
7161 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7162 // name);
7163 // }
7164 // }
7165 // break;
7166 //
7167 case clang::Type::LValueReference:
7168 case clang::Type::RValueReference: {
7169 const clang::ReferenceType *reference_type =
7170 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7171 clang::QualType pointee_type(reference_type->getPointeeType());
7172 CompilerType pointee_clang_type(getASTContext(), pointee_type);
7173
7174 if (pointee_clang_type.IsAggregateType()) {
7175 return pointee_clang_type.GetIndexOfChildMemberWithName(
7176 name, omit_empty_base_classes, child_indexes);
7177 }
7178 } break;
7179
7180 case clang::Type::Pointer: {
7181 CompilerType pointee_clang_type(GetPointeeType(type));
7182
7183 if (pointee_clang_type.IsAggregateType()) {
7184 return pointee_clang_type.GetIndexOfChildMemberWithName(
7185 name, omit_empty_base_classes, child_indexes);
7186 }
7187 } break;
7188
7189 case clang::Type::Typedef:
7190 return CompilerType(getASTContext(),
7191 llvm::cast<clang::TypedefType>(qual_type)
7192 ->getDecl()
7193 ->getUnderlyingType())
7194 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7195 child_indexes);
7196
7197 case clang::Type::Auto:
7198 return CompilerType(
7199 getASTContext(),
7200 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7201 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7202 child_indexes);
7203
7204 case clang::Type::Elaborated:
7205 return CompilerType(
7206 getASTContext(),
7207 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7208 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7209 child_indexes);
7210
7211 case clang::Type::Paren:
7212 return CompilerType(getASTContext(),
7213 llvm::cast<clang::ParenType>(qual_type)->desugar())
7214 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7215 child_indexes);
7216
7217 default:
7218 break;
7219 }
7220 }
7221 return 0;
7222}
7223
7224// Get the index of the child of "clang_type" whose name matches. This function
7225// doesn't descend into the children, but only looks one level deep and name
7226// matches can include base class names.
7227
7228uint32_t
7229ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7230 const char *name,
7231 bool omit_empty_base_classes) {
7232 if (type && name && name[0]) {
7233 clang::QualType qual_type(GetCanonicalQualType(type));
7234
7235 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7236
7237 switch (type_class) {
7238 case clang::Type::Record:
7239 if (GetCompleteType(type)) {
7240 const clang::RecordType *record_type =
7241 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7242 const clang::RecordDecl *record_decl = record_type->getDecl();
7243
7244 assert(record_decl)(static_cast <bool> (record_decl) ? void (0) : __assert_fail
("record_decl", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 7244, __extension__ __PRETTY_FUNCTION__))
;
7245 uint32_t child_idx = 0;
7246
7247 const clang::CXXRecordDecl *cxx_record_decl =
7248 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7249
7250 if (cxx_record_decl) {
7251 clang::CXXRecordDecl::base_class_const_iterator base_class,
7252 base_class_end;
7253 for (base_class = cxx_record_decl->bases_begin(),
7254 base_class_end = cxx_record_decl->bases_end();
7255 base_class != base_class_end; ++base_class) {
7256 // Skip empty base classes
7257 clang::CXXRecordDecl *base_class_decl =
7258 llvm::cast<clang::CXXRecordDecl>(
7259 base_class->getType()
7260 ->getAs<clang::RecordType>()
7261 ->getDecl());
7262 if (omit_empty_base_classes &&
7263 ClangASTContext::RecordHasFields(base_class_decl) == false)
7264 continue;
7265
7266 CompilerType base_class_clang_type(getASTContext(),
7267 base_class->getType());
7268 std::string base_class_type_name(
7269 base_class_clang_type.GetTypeName().AsCString(""));
7270 if (base_class_type_name.compare(name) == 0)
7271 return child_idx;
7272 ++child_idx;
7273 }
7274 }
7275
7276 // Try and find a field that matches NAME
7277 clang::RecordDecl::field_iterator field, field_end;
7278 llvm::StringRef name_sref(name);
7279 for (field = record_decl->field_begin(),
7280 field_end = record_decl->field_end();
7281 field != field_end; ++field, ++child_idx) {
7282 if (field->getName().equals(name_sref))
7283 return child_idx;
7284 }
7285 }
7286 break;
7287
7288 case clang::Type::ObjCObject:
7289 case clang::Type::ObjCInterface:
7290 if (GetCompleteType(type)) {
7291 llvm::StringRef name_sref(name);
7292 const clang::ObjCObjectType *objc_class_type =
7293 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7294 assert(objc_class_type)(static_cast <bool> (objc_class_type) ? void (0) : __assert_fail
("objc_class_type", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 7294, __extension__ __PRETTY_FUNCTION__))
;
7295 if (objc_class_type) {
7296 uint32_t child_idx = 0;
7297 clang::ObjCInterfaceDecl *class_interface_decl =
7298 objc_class_type->getInterface();
7299
7300 if (class_interface_decl) {
7301 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7302 ivar_end = class_interface_decl->ivar_end();
7303 clang::ObjCInterfaceDecl *superclass_interface_decl =
7304 class_interface_decl->getSuperClass();
7305
7306 for (ivar_pos = class_interface_decl->ivar_begin();
7307 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7308 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7309
7310 if (ivar_decl->getName().equals(name_sref)) {
7311 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7312 (omit_empty_base_classes &&
7313 ObjCDeclHasIVars(superclass_interface_decl, true)))
7314 ++child_idx;
7315
7316 return child_idx;
7317 }
7318 }
7319
7320 if (superclass_interface_decl) {
7321 if (superclass_interface_decl->getName().equals(name_sref))
7322 return 0;
7323 }
7324 }
7325 }
7326 }
7327 break;
7328
7329 case clang::Type::ObjCObjectPointer: {
7330 CompilerType pointee_clang_type(
7331 getASTContext(),
7332 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7333 ->getPointeeType());
7334 return pointee_clang_type.GetIndexOfChildWithName(
7335 name, omit_empty_base_classes);
7336 } break;
7337
7338 case clang::Type::ConstantArray: {
7339 // const clang::ConstantArrayType *array =
7340 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7341 // const uint64_t element_count =
7342 // array->getSize().getLimitedValue();
7343 //
7344 // if (idx < element_count)
7345 // {
7346 // std::pair<uint64_t, unsigned> field_type_info =
7347 // ast->getTypeInfo(array->getElementType());
7348 //
7349 // char element_name[32];
7350 // ::snprintf (element_name, sizeof (element_name),
7351 // "%s[%u]", parent_name ? parent_name : "", idx);
7352 //
7353 // child_name.assign(element_name);
7354 // assert(field_type_info.first % 8 == 0);
7355 // child_byte_size = field_type_info.first / 8;
7356 // child_byte_offset = idx * child_byte_size;
7357 // return array->getElementType().getAsOpaquePtr();
7358 // }
7359 } break;
7360
7361 // case clang::Type::MemberPointerType:
7362 // {
7363 // MemberPointerType *mem_ptr_type =
7364 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7365 // clang::QualType pointee_type =
7366 // mem_ptr_type->getPointeeType();
7367 //
7368 // if (ClangASTContext::IsAggregateType
7369 // (pointee_type.getAsOpaquePtr()))
7370 // {
7371 // return GetIndexOfChildWithName (ast,
7372 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7373 // name);
7374 // }
7375 // }
7376 // break;
7377 //
7378 case clang::Type::LValueReference:
7379 case clang::Type::RValueReference: {
7380 const clang::ReferenceType *reference_type =
7381 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7382 CompilerType pointee_type(getASTContext(),
7383 reference_type->getPointeeType());
7384
7385 if (pointee_type.IsAggregateType()) {
7386 return pointee_type.GetIndexOfChildWithName(name,
7387 omit_empty_base_classes);
7388 }
7389 } break;
7390
7391 case clang::Type::Pointer: {
7392 const clang::PointerType *pointer_type =
7393 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7394 CompilerType pointee_type(getASTContext(),
7395 pointer_type->getPointeeType());
7396
7397 if (pointee_type.IsAggregateType()) {
7398 return pointee_type.GetIndexOfChildWithName(name,
7399 omit_empty_base_classes);
7400 } else {
7401 // if (parent_name)
7402 // {
7403 // child_name.assign(1, '*');
7404 // child_name += parent_name;
7405 // }
7406 //
7407 // // We have a pointer to an simple type
7408 // if (idx == 0)
7409 // {
7410 // std::pair<uint64_t, unsigned> clang_type_info
7411 // = ast->getTypeInfo(pointee_type);
7412 // assert(clang_type_info.first % 8 == 0);
7413 // child_byte_size = clang_type_info.first / 8;
7414 // child_byte_offset = 0;
7415 // return pointee_type.getAsOpaquePtr();
7416 // }
7417 }
7418 } break;
7419
7420 case clang::Type::Auto:
7421 return CompilerType(
7422 getASTContext(),
7423 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7424 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7425
7426 case clang::Type::Elaborated:
7427 return CompilerType(
7428 getASTContext(),
7429 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7430 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7431
7432 case clang::Type::Paren:
7433 return CompilerType(getASTContext(),
7434 llvm::cast<clang::ParenType>(qual_type)->desugar())
7435 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7436
7437 case clang::Type::Typedef:
7438 return CompilerType(getASTContext(),
7439 llvm::cast<clang::TypedefType>(qual_type)
7440 ->getDecl()
7441 ->getUnderlyingType())
7442 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7443
7444 default:
7445 break;
7446 }
7447 }
7448 return UINT32_MAX(4294967295U);
7449}
7450
7451size_t
7452ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7453 if (!type)
7454 return 0;
7455
7456 clang::QualType qual_type(GetCanonicalQualType(type));
7457 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7458 switch (type_class) {
7459 case clang::Type::Record:
7460 if (GetCompleteType(type)) {
7461 const clang::CXXRecordDecl *cxx_record_decl =
7462 qual_type->getAsCXXRecordDecl();
7463 if (cxx_record_decl) {
7464 const clang::ClassTemplateSpecializationDecl *template_decl =
7465 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7466 cxx_record_decl);
7467 if (template_decl)
7468 return template_decl->getTemplateArgs().size();
7469 }
7470 }
7471 break;
7472
7473 case clang::Type::Typedef:
7474 return (CompilerType(getASTContext(),
7475 llvm::cast<clang::TypedefType>(qual_type)
7476 ->getDecl()
7477 ->getUnderlyingType()))
7478 .GetNumTemplateArguments();
7479
7480 case clang::Type::Auto:
7481 return (CompilerType(
7482 getASTContext(),
7483 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7484 .GetNumTemplateArguments();
7485
7486 case clang::Type::Elaborated:
7487 return (CompilerType(
7488 getASTContext(),
7489 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7490 .GetNumTemplateArguments();
7491
7492 case clang::Type::Paren:
7493 return (CompilerType(getASTContext(),
7494 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7495 .GetNumTemplateArguments();
7496
7497 default:
7498 break;
7499 }
7500
7501 return 0;
7502}
7503
7504const clang::ClassTemplateSpecializationDecl *
7505ClangASTContext::GetAsTemplateSpecialization(
7506 lldb::opaque_compiler_type_t type) {
7507 if (!type)
7508 return nullptr;
7509
7510 clang::QualType qual_type(GetCanonicalQualType(type));
7511 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7512 switch (type_class) {
7513 case clang::Type::Record: {
7514 if (! GetCompleteType(type))
7515 return nullptr;
7516 const clang::CXXRecordDecl *cxx_record_decl =
7517 qual_type->getAsCXXRecordDecl();
7518 if (!cxx_record_decl)
7519 return nullptr;
7520 return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7521 cxx_record_decl);
7522 }
7523
7524 case clang::Type::Typedef:
7525 return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type)
7526 ->getDecl()
7527 ->getUnderlyingType()
7528 .getAsOpaquePtr());
7529
7530 case clang::Type::Auto:
7531 return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type)
7532 ->getDeducedType()
7533 .getAsOpaquePtr());
7534
7535 case clang::Type::Elaborated:
7536 return GetAsTemplateSpecialization(
7537 llvm::cast<clang::ElaboratedType>(qual_type)
7538 ->getNamedType()
7539 .getAsOpaquePtr());
7540
7541 case clang::Type::Paren:
7542 return GetAsTemplateSpecialization(
7543 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
7544
7545 default:
7546 return nullptr;
7547 }
7548}
7549
7550lldb::TemplateArgumentKind
7551ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
7552 size_t arg_idx) {
7553 const clang::ClassTemplateSpecializationDecl *template_decl =
7554 GetAsTemplateSpecialization(type);
7555 if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size())
7556 return eTemplateArgumentKindNull;
7557
7558 switch (template_decl->getTemplateArgs()[arg_idx].getKind()) {
7559 case clang::TemplateArgument::Null:
7560 return eTemplateArgumentKindNull;
7561
7562 case clang::TemplateArgument::NullPtr:
7563 return eTemplateArgumentKindNullPtr;
7564
7565 case clang::TemplateArgument::Type:
7566 return eTemplateArgumentKindType;
7567
7568 case clang::TemplateArgument::Declaration:
7569 return eTemplateArgumentKindDeclaration;
7570
7571 case clang::TemplateArgument::Integral:
7572 return eTemplateArgumentKindIntegral;
7573
7574 case clang::TemplateArgument::Template:
7575 return eTemplateArgumentKindTemplate;
7576
7577 case clang::TemplateArgument::TemplateExpansion:
7578 return eTemplateArgumentKindTemplateExpansion;
7579
7580 case clang::TemplateArgument::Expression:
7581 return eTemplateArgumentKindExpression;
7582
7583 case clang::TemplateArgument::Pack:
7584 return eTemplateArgumentKindPack;
7585 }
7586 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind")::llvm::llvm_unreachable_internal("Unhandled clang::TemplateArgument::ArgKind"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 7586)
;
7587}
7588
7589CompilerType
7590ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
7591 size_t idx) {
7592 const clang::ClassTemplateSpecializationDecl *template_decl =
7593 GetAsTemplateSpecialization(type);
7594 if (!template_decl || idx >= template_decl->getTemplateArgs().size())
7595 return CompilerType();
7596
7597 const clang::TemplateArgument &template_arg =
7598 template_decl->getTemplateArgs()[idx];
7599 if (template_arg.getKind() != clang::TemplateArgument::Type)
7600 return CompilerType();
7601
7602 return CompilerType(getASTContext(), template_arg.getAsType());
7603}
7604
7605llvm::Optional<CompilerType::IntegralTemplateArgument>
7606ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
7607 size_t idx) {
7608 const clang::ClassTemplateSpecializationDecl *template_decl =
7609 GetAsTemplateSpecialization(type);
7610 if (! template_decl || idx >= template_decl->getTemplateArgs().size())
7611 return llvm::None;
7612
7613 const clang::TemplateArgument &template_arg =
7614 template_decl->getTemplateArgs()[idx];
7615 if (template_arg.getKind() != clang::TemplateArgument::Integral)
7616 return llvm::None;
7617
7618 return {{template_arg.getAsIntegral(),
7619 CompilerType(getASTContext(), template_arg.getIntegralType())}};
7620}
7621
7622CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7623 if (type)
7624 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7625 return CompilerType();
7626}
7627
7628clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7629 const clang::EnumType *enutype =
7630 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7631 if (enutype)
7632 return enutype->getDecl();
7633 return NULL__null;
7634}
7635
7636clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7637 const clang::RecordType *record_type =
7638 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7639 if (record_type)
7640 return record_type->getDecl();
7641 return nullptr;
7642}
7643
7644clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
7645 clang::QualType qual_type = ClangUtil::GetCanonicalQualType(type);
7646 if (qual_type.isNull())
7647 return nullptr;
7648 else
7649 return qual_type->getAsTagDecl();
7650}
7651
7652clang::CXXRecordDecl *
7653ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7654 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
7655}
7656
7657clang::ObjCInterfaceDecl *
7658ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7659 const clang::ObjCObjectType *objc_class_type =
7660 llvm::dyn_cast<clang::ObjCObjectType>(
7661 ClangUtil::GetCanonicalQualType(type));
7662 if (objc_class_type)
7663 return objc_class_type->getInterface();
7664 return nullptr;
7665}
7666
7667clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
7668 const CompilerType &type, const char *name,
7669 const CompilerType &field_clang_type, AccessType access,
7670 uint32_t bitfield_bit_size) {
7671 if (!type.IsValid() || !field_clang_type.IsValid())
7672 return nullptr;
7673 ClangASTContext *ast =
7674 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7675 if (!ast)
7676 return nullptr;
7677 clang::ASTContext *clang_ast = ast->getASTContext();
7678
7679 clang::FieldDecl *field = nullptr;
7680
7681 clang::Expr *bit_width = nullptr;
7682 if (bitfield_bit_size != 0) {
7683 llvm::APInt bitfield_bit_size_apint(
7684 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7685 bit_width = new (*clang_ast)
7686 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7687 clang_ast->IntTy, clang::SourceLocation());
7688 }
7689
7690 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7691 if (record_decl) {
7692 field = clang::FieldDecl::Create(
7693 *clang_ast, record_decl, clang::SourceLocation(),
7694 clang::SourceLocation(),
7695 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7696 ClangUtil::GetQualType(field_clang_type), // Field type
7697 nullptr, // TInfo *
7698 bit_width, // BitWidth
7699 false, // Mutable
7700 clang::ICIS_NoInit); // HasInit
7701
7702 if (!name) {
7703 // Determine whether this field corresponds to an anonymous
7704 // struct or union.
7705 if (const clang::TagType *TagT =
7706 field->getType()->getAs<clang::TagType>()) {
7707 if (clang::RecordDecl *Rec =
7708 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7709 if (!Rec->getDeclName()) {
7710 Rec->setAnonymousStructOrUnion(true);
7711 field->setImplicit();
7712 }
7713 }
7714 }
7715
7716 if (field) {
7717 field->setAccess(
7718 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7719
7720 record_decl->addDecl(field);
7721
7722#ifdef LLDB_CONFIGURATION_DEBUG
7723 VerifyDecl(field);
7724#endif
7725 }
7726 } else {
7727 clang::ObjCInterfaceDecl *class_interface_decl =
7728 ast->GetAsObjCInterfaceDecl(type);
7729
7730 if (class_interface_decl) {
7731 const bool is_synthesized = false;
7732
7733 field_clang_type.GetCompleteType();
7734
7735 field = clang::ObjCIvarDecl::Create(
7736 *clang_ast, class_interface_decl, clang::SourceLocation(),
7737 clang::SourceLocation(),
7738 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7739 ClangUtil::GetQualType(field_clang_type), // Field type
7740 nullptr, // TypeSourceInfo *
7741 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7742 is_synthesized);
7743
7744 if (field) {
7745 class_interface_decl->addDecl(field);
7746
7747#ifdef LLDB_CONFIGURATION_DEBUG
7748 VerifyDecl(field);
7749#endif
7750 }
7751 }
7752 }
7753 return field;
7754}
7755
7756void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7757 if (!type)
7758 return;
7759
7760 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7761 if (!ast)
7762 return;
7763
7764 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7765
7766 if (!record_decl)
7767 return;
7768
7769 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7770
7771 IndirectFieldVector indirect_fields;
7772 clang::RecordDecl::field_iterator field_pos;
7773 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7774 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7775 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7776 last_field_pos = field_pos++) {
7777 if (field_pos->isAnonymousStructOrUnion()) {
7778 clang::QualType field_qual_type = field_pos->getType();
7779
7780 const clang::RecordType *field_record_type =
7781 field_qual_type->getAs<clang::RecordType>();
7782
7783 if (!field_record_type)
7784 continue;
7785
7786 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7787
7788 if (!field_record_decl)
7789 continue;
7790
7791 for (clang::RecordDecl::decl_iterator
7792 di = field_record_decl->decls_begin(),
7793 de = field_record_decl->decls_end();
7794 di != de; ++di) {
7795 if (clang::FieldDecl *nested_field_decl =
7796 llvm::dyn_cast<clang::FieldDecl>(*di)) {
7797 clang::NamedDecl **chain =
7798 new (*ast->getASTContext()) clang::NamedDecl *[2];
7799 chain[0] = *field_pos;
7800 chain[1] = nested_field_decl;
7801 clang::IndirectFieldDecl *indirect_field =
7802 clang::IndirectFieldDecl::Create(
7803 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7804 nested_field_decl->getIdentifier(),
7805 nested_field_decl->getType(), {chain, 2});
7806
7807 indirect_field->setImplicit();
7808
7809 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7810 field_pos->getAccess(), nested_field_decl->getAccess()));
7811
7812 indirect_fields.push_back(indirect_field);
7813 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
7814 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
7815 size_t nested_chain_size =
7816 nested_indirect_field_decl->getChainingSize();
7817 clang::NamedDecl **chain = new (*ast->getASTContext())
7818 clang::NamedDecl *[nested_chain_size + 1];
7819 chain[0] = *field_pos;
7820
7821 int chain_index = 1;
7822 for (clang::IndirectFieldDecl::chain_iterator
7823 nci = nested_indirect_field_decl->chain_begin(),
7824 nce = nested_indirect_field_decl->chain_end();
7825 nci < nce; ++nci) {
7826 chain[chain_index] = *nci;
7827 chain_index++;
7828 }
7829
7830 clang::IndirectFieldDecl *indirect_field =
7831 clang::IndirectFieldDecl::Create(
7832 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7833 nested_indirect_field_decl->getIdentifier(),
7834 nested_indirect_field_decl->getType(),
7835 {chain, nested_chain_size + 1});
7836
7837 indirect_field->setImplicit();
7838
7839 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7840 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
7841
7842 indirect_fields.push_back(indirect_field);
7843 }
7844 }
7845 }
7846 }
7847
7848 // Check the last field to see if it has an incomplete array type as its
7849 // last member and if it does, the tell the record decl about it
7850 if (last_field_pos != field_end_pos) {
7851 if (last_field_pos->getType()->isIncompleteArrayType())
7852 record_decl->hasFlexibleArrayMember();
7853 }
7854
7855 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
7856 ife = indirect_fields.end();
7857 ifi < ife; ++ifi) {
7858 record_decl->addDecl(*ifi);
7859 }
7860}
7861
7862void ClangASTContext::SetIsPacked(const CompilerType &type) {
7863 if (type) {
7864 ClangASTContext *ast =
7865 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7866 if (ast) {
7867 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
7868
7869 if (!record_decl)
7870 return;
7871
7872 record_decl->addAttr(
7873 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
7874 }
7875 }
7876}
7877
7878clang::VarDecl *ClangASTContext::AddVariableToRecordType(
7879 const CompilerType &type, const char *name, const CompilerType &var_type,
7880 AccessType access) {
7881 clang::VarDecl *var_decl = nullptr;
7882
7883 if (!type.IsValid() || !var_type.IsValid())
7884 return nullptr;
7885 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7886 if (!ast)
7887 return nullptr;
7888
7889 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7890 if (record_decl) {
7891 var_decl = clang::VarDecl::Create(
7892 *ast->getASTContext(), // ASTContext &
7893 record_decl, // DeclContext *
7894 clang::SourceLocation(), // clang::SourceLocation StartLoc
7895 clang::SourceLocation(), // clang::SourceLocation IdLoc
7896 name ? &ast->getASTContext()->Idents.get(name)
7897 : nullptr, // clang::IdentifierInfo *
7898 ClangUtil::GetQualType(var_type), // Variable clang::QualType
7899 nullptr, // TypeSourceInfo *
7900 clang::SC_Static); // StorageClass
7901 if (var_decl) {
7902 var_decl->setAccess(
7903 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7904 record_decl->addDecl(var_decl);
7905
7906#ifdef LLDB_CONFIGURATION_DEBUG
7907 VerifyDecl(var_decl);
7908#endif
7909 }
7910 }
7911 return var_decl;
7912}
7913
7914clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
7915 lldb::opaque_compiler_type_t type, const char *name, const char *mangled_name,
7916 const CompilerType &method_clang_type, lldb::AccessType access,
7917 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
7918 bool is_attr_used, bool is_artificial) {
7919 if (!type || !method_clang_type.IsValid() || name == nullptr ||
7920 name[0] == '\0')
7921 return nullptr;
7922
7923 clang::QualType record_qual_type(GetCanonicalQualType(type));
7924
7925 clang::CXXRecordDecl *cxx_record_decl =
7926 record_qual_type->getAsCXXRecordDecl();
7927
7928 if (cxx_record_decl == nullptr)
7929 return nullptr;
7930
7931 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
7932
7933 clang::CXXMethodDecl *cxx_method_decl = nullptr;
7934
7935 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
7936
7937 const clang::FunctionType *function_type =
7938 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
7939
7940 if (function_type == nullptr)
7941 return nullptr;
7942
7943 const clang::FunctionProtoType *method_function_prototype(
7944 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
7945
7946 if (!method_function_prototype)
7947 return nullptr;
7948
7949 unsigned int num_params = method_function_prototype->getNumParams();
7950
7951 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
7952 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
7953
7954 if (is_artificial)
7955 return nullptr; // skip everything artificial
7956
7957 if (name[0] == '~') {
7958 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
7959 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7960 clang::DeclarationNameInfo(
7961 getASTContext()->DeclarationNames.getCXXDestructorName(
7962 getASTContext()->getCanonicalType(record_qual_type)),
7963 clang::SourceLocation()),
7964 method_qual_type, nullptr, is_inline, is_artificial);
7965 cxx_method_decl = cxx_dtor_decl;
7966 } else if (decl_name == cxx_record_decl->getDeclName()) {
7967 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
7968 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7969 clang::DeclarationNameInfo(
7970 getASTContext()->DeclarationNames.getCXXConstructorName(
7971 getASTContext()->getCanonicalType(record_qual_type)),
7972 clang::SourceLocation()),
7973 method_qual_type,
7974 nullptr, // TypeSourceInfo *
7975 is_explicit, is_inline, is_artificial, false /*is_constexpr*/);
7976 cxx_method_decl = cxx_ctor_decl;
7977 } else {
7978 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
7979 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
7980
7981 if (IsOperator(name, op_kind)) {
7982 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
7983 // Check the number of operator parameters. Sometimes we have
7984 // seen bad DWARF that doesn't correctly describe operators and
7985 // if we try to create a method and add it to the class, clang
7986 // will assert and crash, so we need to make sure things are
7987 // acceptable.
7988 const bool is_method = true;
7989 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
7990 is_method, op_kind, num_params))
7991 return nullptr;
7992 cxx_method_decl = clang::CXXMethodDecl::Create(
7993 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7994 clang::DeclarationNameInfo(
7995 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
7996 clang::SourceLocation()),
7997 method_qual_type,
7998 nullptr, // TypeSourceInfo *
7999 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
8000 } else if (num_params == 0) {
8001 // Conversion operators don't take params...
8002 cxx_method_decl = clang::CXXConversionDecl::Create(
8003 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8004 clang::DeclarationNameInfo(
8005 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
8006 getASTContext()->getCanonicalType(
8007 function_type->getReturnType())),
8008 clang::SourceLocation()),
8009 method_qual_type,
8010 nullptr, // TypeSourceInfo *
8011 is_inline, is_explicit, false /*is_constexpr*/,
8012 clang::SourceLocation());
8013 }
8014 }
8015
8016 if (cxx_method_decl == nullptr) {
8017 cxx_method_decl = clang::CXXMethodDecl::Create(
8018 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8019 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8020 method_qual_type,
8021 nullptr, // TypeSourceInfo *
8022 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
8023 }
8024 }
8025
8026 clang::AccessSpecifier access_specifier =
8027 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8028
8029 cxx_method_decl->setAccess(access_specifier);
8030 cxx_method_decl->setVirtualAsWritten(is_virtual);
8031
8032 if (is_attr_used)
8033 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8034
8035 if (mangled_name != NULL__null) {
8036 cxx_method_decl->addAttr(
8037 clang::AsmLabelAttr::CreateImplicit(*getASTContext(), mangled_name));
8038 }
8039
8040 // Populate the method decl with parameter decls
8041
8042 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8043
8044 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8045 params.push_back(clang::ParmVarDecl::Create(
8046 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8047 clang::SourceLocation(),
8048 nullptr, // anonymous
8049 method_function_prototype->getParamType(param_index), nullptr,
8050 clang::SC_None, nullptr));
8051 }
8052
8053 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8054
8055 cxx_record_decl->addDecl(cxx_method_decl);
8056
8057 // Sometimes the debug info will mention a constructor (default/copy/move),
8058 // destructor, or assignment operator (copy/move) but there won't be any
8059 // version of this in the code. So we check if the function was artificially
8060 // generated and if it is trivial and this lets the compiler/backend know
8061 // that it can inline the IR for these when it needs to and we can avoid a
8062 // "missing function" error when running expressions.
8063
8064 if (is_artificial) {
8065 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8066 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8067 (cxx_ctor_decl->isCopyConstructor() &&
8068 cxx_record_decl->hasTrivialCopyConstructor()) ||
8069 (cxx_ctor_decl->isMoveConstructor() &&
8070 cxx_record_decl->hasTrivialMoveConstructor()))) {
8071 cxx_ctor_decl->setDefaulted();
8072 cxx_ctor_decl->setTrivial(true);
8073 } else if (cxx_dtor_decl) {
8074 if (cxx_record_decl->hasTrivialDestructor()) {
8075 cxx_dtor_decl->setDefaulted();
8076 cxx_dtor_decl->setTrivial(true);
8077 }
8078 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8079 cxx_record_decl->hasTrivialCopyAssignment()) ||
8080 (cxx_method_decl->isMoveAssignmentOperator() &&
8081 cxx_record_decl->hasTrivialMoveAssignment())) {
8082 cxx_method_decl->setDefaulted();
8083 cxx_method_decl->setTrivial(true);
8084 }
8085 }
8086
8087#ifdef LLDB_CONFIGURATION_DEBUG
8088 VerifyDecl(cxx_method_decl);
8089#endif
8090
8091 // printf ("decl->isPolymorphic() = %i\n",
8092 // cxx_record_decl->isPolymorphic());
8093 // printf ("decl->isAggregate() = %i\n",
8094 // cxx_record_decl->isAggregate());
8095 // printf ("decl->isPOD() = %i\n",
8096 // cxx_record_decl->isPOD());
8097 // printf ("decl->isEmpty() = %i\n",
8098 // cxx_record_decl->isEmpty());
8099 // printf ("decl->isAbstract() = %i\n",
8100 // cxx_record_decl->isAbstract());
8101 // printf ("decl->hasTrivialConstructor() = %i\n",
8102 // cxx_record_decl->hasTrivialConstructor());
8103 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8104 // cxx_record_decl->hasTrivialCopyConstructor());
8105 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8106 // cxx_record_decl->hasTrivialCopyAssignment());
8107 // printf ("decl->hasTrivialDestructor() = %i\n",
8108 // cxx_record_decl->hasTrivialDestructor());
8109 return cxx_method_decl;
8110}
8111
8112#pragma mark C++ Base Classes
8113
8114clang::CXXBaseSpecifier *
8115ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8116 AccessType access, bool is_virtual,
8117 bool base_of_class) {
8118 if (type)
8119 return new clang::CXXBaseSpecifier(
8120 clang::SourceRange(), is_virtual, base_of_class,
8121 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8122 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8123 clang::SourceLocation());
8124 return nullptr;
8125}
8126
8127void ClangASTContext::DeleteBaseClassSpecifiers(
8128 clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes) {
8129 for (unsigned i = 0; i < num_base_classes; ++i) {
8130 delete base_classes[i];
8131 base_classes[i] = nullptr;
8132 }
8133}
8134
8135bool ClangASTContext::SetBaseClassesForClassType(
8136 lldb::opaque_compiler_type_t type,
8137 clang::CXXBaseSpecifier const *const *base_classes,
8138 unsigned num_base_classes) {
8139 if (type) {
8140 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8141 if (cxx_record_decl) {
8142 cxx_record_decl->setBases(base_classes, num_base_classes);
8143 return true;
8144 }
8145 }
8146 return false;
8147}
8148
8149bool ClangASTContext::SetObjCSuperClass(
8150 const CompilerType &type, const CompilerType &superclass_clang_type) {
8151 ClangASTContext *ast =
8152 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8153 if (!ast)
8154 return false;
8155 clang::ASTContext *clang_ast = ast->getASTContext();
8156
8157 if (type && superclass_clang_type.IsValid() &&
8158 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8159 clang::ObjCInterfaceDecl *class_interface_decl =
8160 GetAsObjCInterfaceDecl(type);
8161 clang::ObjCInterfaceDecl *super_interface_decl =
8162 GetAsObjCInterfaceDecl(superclass_clang_type);
8163 if (class_interface_decl && super_interface_decl) {
8164 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8165 clang_ast->getObjCInterfaceType(super_interface_decl)));
8166 return true;
8167 }
8168 }
8169 return false;
8170}
8171
8172bool ClangASTContext::AddObjCClassProperty(
8173 const CompilerType &type, const char *property_name,
8174 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8175 const char *property_setter_name, const char *property_getter_name,
8176 uint32_t property_attributes, ClangASTMetadata *metadata) {
8177 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8178 property_name[0] == '\0')
8179 return false;
8180 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8181 if (!ast)
8182 return false;
8183 clang::ASTContext *clang_ast = ast->getASTContext();
8184
8185 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8186
8187 if (class_interface_decl) {
8188 CompilerType property_clang_type_to_access;
8189
8190 if (property_clang_type.IsValid())
8191 property_clang_type_to_access = property_clang_type;
8192 else if (ivar_decl)
8193 property_clang_type_to_access =
8194 CompilerType(clang_ast, ivar_decl->getType());
8195
8196 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8197 clang::TypeSourceInfo *prop_type_source;
8198 if (ivar_decl)
8199 prop_type_source =
8200 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8201 else
8202 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8203 ClangUtil::GetQualType(property_clang_type));
8204
8205 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8206 *clang_ast, class_interface_decl,
8207 clang::SourceLocation(), // Source Location
8208 &clang_ast->Idents.get(property_name),
8209 clang::SourceLocation(), // Source Location for AT
8210 clang::SourceLocation(), // Source location for (
8211 ivar_decl ? ivar_decl->getType()
8212 : ClangUtil::GetQualType(property_clang_type),
8213 prop_type_source);
8214
8215 if (property_decl) {
8216 if (metadata)
8217 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8218
8219 class_interface_decl->addDecl(property_decl);
8220
8221 clang::Selector setter_sel, getter_sel;
8222
8223 if (property_setter_name != nullptr) {
8224 std::string property_setter_no_colon(
8225 property_setter_name, strlen(property_setter_name) - 1);
8226 clang::IdentifierInfo *setter_ident =
8227 &clang_ast->Idents.get(property_setter_no_colon);
8228 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8229 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8230 std::string setter_sel_string("set");
8231 setter_sel_string.push_back(::toupper(property_name[0]));
8232 setter_sel_string.append(&property_name[1]);
8233 clang::IdentifierInfo *setter_ident =
8234 &clang_ast->Idents.get(setter_sel_string);
8235 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8236 }
8237 property_decl->setSetterName(setter_sel);
8238 property_decl->setPropertyAttributes(
8239 clang::ObjCPropertyDecl::OBJC_PR_setter);
8240
8241 if (property_getter_name != nullptr) {
8242 clang::IdentifierInfo *getter_ident =
8243 &clang_ast->Idents.get(property_getter_name);
8244 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8245 } else {
8246 clang::IdentifierInfo *getter_ident =
8247 &clang_ast->Idents.get(property_name);
8248 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8249 }
8250 property_decl->setGetterName(getter_sel);
8251 property_decl->setPropertyAttributes(
8252 clang::ObjCPropertyDecl::OBJC_PR_getter);
8253
8254 if (ivar_decl)
8255 property_decl->setPropertyIvarDecl(ivar_decl);
8256
8257 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8258 property_decl->setPropertyAttributes(
8259 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8260 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8261 property_decl->setPropertyAttributes(
8262 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8263 if (property_attributes & DW_APPLE_PROPERTY_assign)
8264 property_decl->setPropertyAttributes(
8265 clang::ObjCPropertyDecl::OBJC_PR_assign);
8266 if (property_attributes & DW_APPLE_PROPERTY_retain)
8267 property_decl->setPropertyAttributes(
8268 clang::ObjCPropertyDecl::OBJC_PR_retain);
8269 if (property_attributes & DW_APPLE_PROPERTY_copy)
8270 property_decl->setPropertyAttributes(
8271 clang::ObjCPropertyDecl::OBJC_PR_copy);
8272 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8273 property_decl->setPropertyAttributes(
8274 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8275 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8276 property_decl->setPropertyAttributes(
8277 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8278 if (property_attributes &
8279 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8280 property_decl->setPropertyAttributes(
8281 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8282 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8283 property_decl->setPropertyAttributes(
8284 clang::ObjCPropertyDecl::OBJC_PR_class);
8285
8286 const bool isInstance =
8287 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8288
8289 if (!getter_sel.isNull() &&
8290 !(isInstance
8291 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8292 : class_interface_decl->lookupClassMethod(getter_sel))) {
8293 const bool isVariadic = false;
8294 const bool isSynthesized = false;
8295 const bool isImplicitlyDeclared = true;
8296 const bool isDefined = false;
8297 const clang::ObjCMethodDecl::ImplementationControl impControl =
8298 clang::ObjCMethodDecl::None;
8299 const bool HasRelatedResultType = false;
8300
8301 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8302 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8303 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8304 nullptr, class_interface_decl, isInstance, isVariadic,
8305 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8306 HasRelatedResultType);
8307
8308 if (getter && metadata)
8309 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8310
8311 if (getter) {
8312 getter->setMethodParams(*clang_ast,
8313 llvm::ArrayRef<clang::ParmVarDecl *>(),
8314 llvm::ArrayRef<clang::SourceLocation>());
8315
8316 class_interface_decl->addDecl(getter);
8317 }
8318 }
8319
8320 if (!setter_sel.isNull() &&
8321 !(isInstance
8322 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8323 : class_interface_decl->lookupClassMethod(setter_sel))) {
8324 clang::QualType result_type = clang_ast->VoidTy;
8325 const bool isVariadic = false;
8326 const bool isSynthesized = false;
8327 const bool isImplicitlyDeclared = true;
8328 const bool isDefined = false;
8329 const clang::ObjCMethodDecl::ImplementationControl impControl =
8330 clang::ObjCMethodDecl::None;
8331 const bool HasRelatedResultType = false;
8332
8333 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8334 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8335 setter_sel, result_type, nullptr, class_interface_decl,
8336 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8337 isDefined, impControl, HasRelatedResultType);
8338
8339 if (setter && metadata)
8340 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8341
8342 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8343
8344 params.push_back(clang::ParmVarDecl::Create(
8345 *clang_ast, setter, clang::SourceLocation(),
8346 clang::SourceLocation(),
8347 nullptr, // anonymous
8348 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8349 clang::SC_Auto, nullptr));
8350
8351 if (setter) {
8352 setter->setMethodParams(
8353 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8354 llvm::ArrayRef<clang::SourceLocation>());
8355
8356 class_interface_decl->addDecl(setter);
8357 }
8358 }
8359
8360 return true;
8361 }
8362 }
8363 }
8364 return false;
8365}
8366
8367bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8368 bool check_superclass) {
8369 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8370 if (class_interface_decl)
8371 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8372 return false;
8373}
8374
8375clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8376 const CompilerType &type,
8377 const char *name, // the full symbol name as seen in the symbol table
8378 // (lldb::opaque_compiler_type_t type, "-[NString
8379 // stringWithCString:]")
8380 const CompilerType &method_clang_type, lldb::AccessType access,
8381 bool is_artificial, bool is_variadic) {
8382 if (!type || !method_clang_type.IsValid())
8383 return nullptr;
8384
8385 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8386
8387 if (class_interface_decl == nullptr)
8388 return nullptr;
8389 ClangASTContext *lldb_ast =
8390 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8391 if (lldb_ast == nullptr)
8392 return nullptr;
8393 clang::ASTContext *ast = lldb_ast->getASTContext();
8394
8395 const char *selector_start = ::strchr(name, ' ');
8396 if (selector_start == nullptr)
8397 return nullptr;
8398
8399 selector_start++;
8400 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8401
8402 size_t len = 0;
8403 const char *start;
8404 // printf ("name = '%s'\n", name);
8405
8406 unsigned num_selectors_with_args = 0;
8407 for (start = selector_start; start && *start != '\0' && *start != ']';
8408 start += len) {
8409 len = ::strcspn(start, ":]");
8410 bool has_arg = (start[len] == ':');
8411 if (has_arg)
8412 ++num_selectors_with_args;
8413 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8414 if (has_arg)
8415 len += 1;
8416 }
8417
8418 if (selector_idents.size() == 0)
8419 return nullptr;
8420
8421 clang::Selector method_selector = ast->Selectors.getSelector(
8422 num_selectors_with_args ? selector_idents.size() : 0,
8423 selector_idents.data());
8424
8425 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8426
8427 // Populate the method decl with parameter decls
8428 const clang::Type *method_type(method_qual_type.getTypePtr());
8429
8430 if (method_type == nullptr)
8431 return nullptr;
8432
8433 const clang::FunctionProtoType *method_function_prototype(
8434 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8435
8436 if (!method_function_prototype)
8437 return nullptr;
8438
8439 bool is_synthesized = false;
8440 bool is_defined = false;
8441 clang::ObjCMethodDecl::ImplementationControl imp_control =
8442 clang::ObjCMethodDecl::None;
8443
8444 const unsigned num_args = method_function_prototype->getNumParams();
8445
8446 if (num_args != num_selectors_with_args)
8447 return nullptr; // some debug information is corrupt. We are not going to
8448 // deal with it.
8449
8450 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8451 *ast,
8452 clang::SourceLocation(), // beginLoc,
8453 clang::SourceLocation(), // endLoc,
8454 method_selector, method_function_prototype->getReturnType(),
8455 nullptr, // TypeSourceInfo *ResultTInfo,
8456 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8457 ClangUtil::GetQualType(type)),
8458 name[0] == '-', is_variadic, is_synthesized,
8459 true, // is_implicitly_declared; we force this to true because we don't
8460 // have source locations
8461 is_defined, imp_control, false /*has_related_result_type*/);
8462
8463 if (objc_method_decl == nullptr)
8464 return nullptr;
8465
8466 if (num_args > 0) {
8467 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8468
8469 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8470 params.push_back(clang::ParmVarDecl::Create(
8471 *ast, objc_method_decl, clang::SourceLocation(),
8472 clang::SourceLocation(),
8473 nullptr, // anonymous
8474 method_function_prototype->getParamType(param_index), nullptr,
8475 clang::SC_Auto, nullptr));
8476 }
8477
8478 objc_method_decl->setMethodParams(
8479 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8480 llvm::ArrayRef<clang::SourceLocation>());
8481 }
8482
8483 class_interface_decl->addDecl(objc_method_decl);
8484
8485#ifdef LLDB_CONFIGURATION_DEBUG
8486 VerifyDecl(objc_method_decl);
8487#endif
8488
8489 return objc_method_decl;
8490}
8491
8492bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8493 if (ClangUtil::IsClangType(type))
8494 return false;
8495
8496 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
8497
8498 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8499 switch (type_class) {
8500 case clang::Type::Record: {
8501 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8502 if (cxx_record_decl)
8503 return cxx_record_decl->hasExternalLexicalStorage() ||
8504 cxx_record_decl->hasExternalVisibleStorage();
8505 } break;
8506
8507 case clang::Type::Enum: {
8508 clang::EnumDecl *enum_decl =
8509 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8510 if (enum_decl)
8511 return enum_decl->hasExternalLexicalStorage() ||
8512 enum_decl->hasExternalVisibleStorage();
8513 } break;
8514
8515 case clang::Type::ObjCObject:
8516 case clang::Type::ObjCInterface: {
8517 const clang::ObjCObjectType *objc_class_type =
8518 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8519 assert(objc_class_type)(static_cast <bool> (objc_class_type) ? void (0) : __assert_fail
("objc_class_type", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 8519, __extension__ __PRETTY_FUNCTION__))
;
8520 if (objc_class_type) {
8521 clang::ObjCInterfaceDecl *class_interface_decl =
8522 objc_class_type->getInterface();
8523
8524 if (class_interface_decl)
8525 return class_interface_decl->hasExternalLexicalStorage() ||
8526 class_interface_decl->hasExternalVisibleStorage();
8527 }
8528 } break;
8529
8530 case clang::Type::Typedef:
8531 return GetHasExternalStorage(CompilerType(
8532 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8533 ->getDecl()
8534 ->getUnderlyingType()
8535 .getAsOpaquePtr()));
8536
8537 case clang::Type::Auto:
8538 return GetHasExternalStorage(CompilerType(
8539 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8540 ->getDeducedType()
8541 .getAsOpaquePtr()));
8542
8543 case clang::Type::Elaborated:
8544 return GetHasExternalStorage(CompilerType(
8545 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8546 ->getNamedType()
8547 .getAsOpaquePtr()));
8548
8549 case clang::Type::Paren:
8550 return GetHasExternalStorage(CompilerType(
8551 type.GetTypeSystem(),
8552 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8553
8554 default:
8555 break;
8556 }
8557 return false;
8558}
8559
8560bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8561 bool has_extern) {
8562 if (!type)
8563 return false;
8564
8565 clang::QualType qual_type(GetCanonicalQualType(type));
8566
8567 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8568 switch (type_class) {
8569 case clang::Type::Record: {
8570 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8571 if (cxx_record_decl) {
8572 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8573 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8574 return true;
8575 }
8576 } break;
8577
8578 case clang::Type::Enum: {
8579 clang::EnumDecl *enum_decl =
8580 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8581 if (enum_decl) {
8582 enum_decl->setHasExternalLexicalStorage(has_extern);
8583 enum_decl->setHasExternalVisibleStorage(has_extern);
8584 return true;
8585 }
8586 } break;
8587
8588 case clang::Type::ObjCObject:
8589 case clang::Type::ObjCInterface: {
8590 const clang::ObjCObjectType *objc_class_type =
8591 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8592 assert(objc_class_type)(static_cast <bool> (objc_class_type) ? void (0) : __assert_fail
("objc_class_type", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 8592, __extension__ __PRETTY_FUNCTION__))
;
8593 if (objc_class_type) {
8594 clang::ObjCInterfaceDecl *class_interface_decl =
8595 objc_class_type->getInterface();
8596
8597 if (class_interface_decl) {
8598 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8599 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8600 return true;
8601 }
8602 }
8603 } break;
8604
8605 case clang::Type::Typedef:
8606 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8607 ->getDecl()
8608 ->getUnderlyingType()
8609 .getAsOpaquePtr(),
8610 has_extern);
8611
8612 case clang::Type::Auto:
8613 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8614 ->getDeducedType()
8615 .getAsOpaquePtr(),
8616 has_extern);
8617
8618 case clang::Type::Elaborated:
8619 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8620 ->getNamedType()
8621 .getAsOpaquePtr(),
8622 has_extern);
8623
8624 case clang::Type::Paren:
8625 return SetHasExternalStorage(
8626 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8627 has_extern);
8628
8629 default:
8630 break;
8631 }
8632 return false;
8633}
8634
8635#pragma mark TagDecl
8636
8637bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8638 clang::QualType qual_type(ClangUtil::GetQualType(type));
8639 if (!qual_type.isNull()) {
8640 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8641 if (tag_type) {
8642 clang::TagDecl *tag_decl = tag_type->getDecl();
8643 if (tag_decl) {
8644 tag_decl->startDefinition();
8645 return true;
8646 }
8647 }
8648
8649 const clang::ObjCObjectType *object_type =
8650 qual_type->getAs<clang::ObjCObjectType>();
8651 if (object_type) {
8652 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8653 if (interface_decl) {
8654 interface_decl->startDefinition();
8655 return true;
8656 }
8657 }
8658 }
8659 return false;
8660}
8661
8662bool ClangASTContext::CompleteTagDeclarationDefinition(
8663 const CompilerType &type) {
8664 clang::QualType qual_type(ClangUtil::GetQualType(type));
8665 if (!qual_type.isNull()) {
8666 // Make sure we use the same methodology as
8667 // ClangASTContext::StartTagDeclarationDefinition()
8668 // as to how we start/end the definition. Previously we were calling
8669 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8670 if (tag_type) {
8671 clang::TagDecl *tag_decl = tag_type->getDecl();
8672 if (tag_decl) {
8673 clang::CXXRecordDecl *cxx_record_decl =
8674 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8675
8676 if (cxx_record_decl) {
8677 if (!cxx_record_decl->isCompleteDefinition())
8678 cxx_record_decl->completeDefinition();
8679 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8680 cxx_record_decl->setHasExternalLexicalStorage(false);
8681 cxx_record_decl->setHasExternalVisibleStorage(false);
8682 return true;
8683 }
8684 }
8685 }
8686
8687 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8688
8689 if (enutype) {
8690 clang::EnumDecl *enum_decl = enutype->getDecl();
8691
8692 if (enum_decl) {
8693 if (!enum_decl->isCompleteDefinition()) {
8694 ClangASTContext *lldb_ast =
8695 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8696 if (lldb_ast == nullptr)
8697 return false;
8698 clang::ASTContext *ast = lldb_ast->getASTContext();
8699
8700 /// TODO This really needs to be fixed.
8701
8702 QualType integer_type(enum_decl->getIntegerType());
8703 if (!integer_type.isNull()) {
8704 unsigned NumPositiveBits = 1;
8705 unsigned NumNegativeBits = 0;
8706
8707 clang::QualType promotion_qual_type;
8708 // If the enum integer type is less than an integer in bit width,
8709 // then we must promote it to an integer size.
8710 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8711 ast->getTypeSize(ast->IntTy)) {
8712 if (enum_decl->getIntegerType()->isSignedIntegerType())
8713 promotion_qual_type = ast->IntTy;
8714 else
8715 promotion_qual_type = ast->UnsignedIntTy;
8716 } else
8717 promotion_qual_type = enum_decl->getIntegerType();
8718
8719 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8720 promotion_qual_type, NumPositiveBits,
8721 NumNegativeBits);
8722 }
8723 }
8724 return true;
8725 }
8726 }
8727 }
8728 return false;
8729}
8730
8731bool ClangASTContext::AddEnumerationValueToEnumerationType(
8732 lldb::opaque_compiler_type_t type,
8733 const CompilerType &enumerator_clang_type, const Declaration &decl,
8734 const char *name, int64_t enum_value, uint32_t enum_value_bit_size) {
8735 if (type && enumerator_clang_type.IsValid() && name && name[0]) {
8736 clang::QualType enum_qual_type(GetCanonicalQualType(type));
8737
8738 bool is_signed = false;
8739 enumerator_clang_type.IsIntegerType(is_signed);
8740 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8741 if (clang_type) {
8742 const clang::EnumType *enutype =
8743 llvm::dyn_cast<clang::EnumType>(clang_type);
8744
8745 if (enutype) {
8746 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
8747 enum_llvm_apsint = enum_value;
8748 clang::EnumConstantDecl *enumerator_decl =
8749 clang::EnumConstantDecl::Create(
8750 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8751 name ? &getASTContext()->Idents.get(name)
8752 : nullptr, // Identifier
8753 ClangUtil::GetQualType(enumerator_clang_type),
8754 nullptr, enum_llvm_apsint);
8755
8756 if (enumerator_decl) {
8757 enutype->getDecl()->addDecl(enumerator_decl);
8758
8759#ifdef LLDB_CONFIGURATION_DEBUG
8760 VerifyDecl(enumerator_decl);
8761#endif
8762
8763 return true;
8764 }
8765 }
8766 }
8767 }
8768 return false;
8769}
8770
8771CompilerType
8772ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
8773 clang::QualType enum_qual_type(GetCanonicalQualType(type));
8774 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8775 if (clang_type) {
8776 const clang::EnumType *enutype =
8777 llvm::dyn_cast<clang::EnumType>(clang_type);
8778 if (enutype) {
8779 clang::EnumDecl *enum_decl = enutype->getDecl();
8780 if (enum_decl)
8781 return CompilerType(getASTContext(), enum_decl->getIntegerType());
8782 }
8783 }
8784 return CompilerType();
8785}
8786
8787CompilerType
8788ClangASTContext::CreateMemberPointerType(const CompilerType &type,
8789 const CompilerType &pointee_type) {
8790 if (type && pointee_type.IsValid() &&
8791 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
8792 ClangASTContext *ast =
8793 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8794 if (!ast)
8795 return CompilerType();
8796 return CompilerType(ast->getASTContext(),
8797 ast->getASTContext()->getMemberPointerType(
8798 ClangUtil::GetQualType(pointee_type),
8799 ClangUtil::GetQualType(type).getTypePtr()));
8800 }
8801 return CompilerType();
8802}
8803
8804size_t
8805ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
8806 const char *s, uint8_t *dst,
8807 size_t dst_size) {
8808 if (type) {
8809 clang::QualType qual_type(GetCanonicalQualType(type));
8810 uint32_t count = 0;
8811 bool is_complex = false;
8812 if (IsFloatingPointType(type, count, is_complex)) {
8813 // TODO: handle complex and vector types
8814 if (count != 1)
8815 return false;
8816
8817 llvm::StringRef s_sref(s);
8818 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type),
8819 s_sref);
8820
8821 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type);
8822 const uint64_t byte_size = bit_size / 8;
8823 if (dst_size >= byte_size) {
8824 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(
8825 llvm::NextPowerOf2(byte_size) * 8);
8826 lldb_private::Status get_data_error;
8827 if (scalar.GetAsMemoryData(dst, byte_size,
8828 lldb_private::endian::InlHostByteOrder(),
8829 get_data_error))
8830 return byte_size;
8831 }
8832 }
8833 }
8834 return 0;
8835}
8836
8837//----------------------------------------------------------------------
8838// Dumping types
8839//----------------------------------------------------------------------
8840#define DEPTH_INCREMENT2 2
8841
8842void ClangASTContext::DumpValue(
8843 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
8844 lldb::Format format, const DataExtractor &data,
8845 lldb::offset_t data_byte_offset, size_t data_byte_size,
8846 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
8847 bool show_summary, bool verbose, uint32_t depth) {
8848 if (!type)
8849 return;
8850
8851 clang::QualType qual_type(GetQualType(type));
8852 switch (qual_type->getTypeClass()) {
8853 case clang::Type::Record:
8854 if (GetCompleteType(type)) {
8855 const clang::RecordType *record_type =
8856 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8857 const clang::RecordDecl *record_decl = record_type->getDecl();
8858 assert(record_decl)(static_cast <bool> (record_decl) ? void (0) : __assert_fail
("record_decl", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 8858, __extension__ __PRETTY_FUNCTION__))
;
8859 uint32_t field_bit_offset = 0;
8860 uint32_t field_byte_offset = 0;
8861 const clang::ASTRecordLayout &record_layout =
8862 getASTContext()->getASTRecordLayout(record_decl);
8863 uint32_t child_idx = 0;
8864
8865 const clang::CXXRecordDecl *cxx_record_decl =
8866 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8867 if (cxx_record_decl) {
8868 // We might have base classes to print out first
8869 clang::CXXRecordDecl::base_class_const_iterator base_class,
8870 base_class_end;
8871 for (base_class = cxx_record_decl->bases_begin(),
8872 base_class_end = cxx_record_decl->bases_end();
8873 base_class != base_class_end; ++base_class) {
8874 const clang::CXXRecordDecl *base_class_decl =
8875 llvm::cast<clang::CXXRecordDecl>(
8876 base_class->getType()->getAs<clang::RecordType>()->getDecl());
8877
8878 // Skip empty base classes
8879 if (verbose == false &&
8880 ClangASTContext::RecordHasFields(base_class_decl) == false)
8881 continue;
8882
8883 if (base_class->isVirtual())
8884 field_bit_offset =
8885 record_layout.getVBaseClassOffset(base_class_decl)
8886 .getQuantity() *
8887 8;
8888 else
8889 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
8890 .getQuantity() *
8891 8;
8892 field_byte_offset = field_bit_offset / 8;
8893 assert(field_bit_offset % 8 == 0)(static_cast <bool> (field_bit_offset % 8 == 0) ? void (
0) : __assert_fail ("field_bit_offset % 8 == 0", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 8893, __extension__ __PRETTY_FUNCTION__))
;
8894 if (child_idx == 0)
8895 s->PutChar('{');
8896 else
8897 s->PutChar(',');
8898
8899 clang::QualType base_class_qual_type = base_class->getType();
8900 std::string base_class_type_name(base_class_qual_type.getAsString());
8901
8902 // Indent and print the base class type name
8903 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT2),
8904 base_class_type_name);
8905
8906 clang::TypeInfo base_class_type_info =
8907 getASTContext()->getTypeInfo(base_class_qual_type);
8908
8909 // Dump the value of the member
8910 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
8911 base_clang_type.DumpValue(
8912 exe_ctx,
8913 s, // Stream to dump to
8914 base_clang_type
8915 .GetFormat(), // The format with which to display the member
8916 data, // Data buffer containing all bytes for this type
8917 data_byte_offset + field_byte_offset, // Offset into "data" where
8918 // to grab value from
8919 base_class_type_info.Width / 8, // Size of this type in bytes
8920 0, // Bitfield bit size
8921 0, // Bitfield bit offset
8922 show_types, // Boolean indicating if we should show the variable
8923 // types
8924 show_summary, // Boolean indicating if we should show a summary
8925 // for the current type
8926 verbose, // Verbose output?
8927 depth + DEPTH_INCREMENT2); // Scope depth for any types that have
8928 // children
8929
8930 ++child_idx;
8931 }
8932 }
8933 uint32_t field_idx = 0;
8934 clang::RecordDecl::field_iterator field, field_end;
8935 for (field = record_decl->field_begin(),
8936 field_end = record_decl->field_end();
8937 field != field_end; ++field, ++field_idx, ++child_idx) {
8938 // Print the starting squiggly bracket (if this is the
8939 // first member) or comma (for member 2 and beyond) for
8940 // the struct/union/class member.
8941 if (child_idx == 0)
8942 s->PutChar('{');
8943 else
8944 s->PutChar(',');
8945
8946 // Indent
8947 s->Printf("\n%*s", depth + DEPTH_INCREMENT2, "");
8948
8949 clang::QualType field_type = field->getType();
8950 // Print the member type if requested
8951 // Figure out the type byte size (field_type_info.first) and
8952 // alignment (field_type_info.second) from the AST context.
8953 clang::TypeInfo field_type_info =
8954 getASTContext()->getTypeInfo(field_type);
8955 assert(field_idx < record_layout.getFieldCount())(static_cast <bool> (field_idx < record_layout.getFieldCount
()) ? void (0) : __assert_fail ("field_idx < record_layout.getFieldCount()"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 8955, __extension__ __PRETTY_FUNCTION__))
;
8956 // Figure out the field offset within the current struct/union/class
8957 // type
8958 field_bit_offset = record_layout.getFieldOffset(field_idx);
8959 field_byte_offset = field_bit_offset / 8;
8960 uint32_t field_bitfield_bit_size = 0;
8961 uint32_t field_bitfield_bit_offset = 0;
8962 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
8963 field_bitfield_bit_size))
8964 field_bitfield_bit_offset = field_bit_offset % 8;
8965
8966 if (show_types) {
8967 std::string field_type_name(field_type.getAsString());
8968 if (field_bitfield_bit_size > 0)
8969 s->Printf("(%s:%u) ", field_type_name.c_str(),
8970 field_bitfield_bit_size);
8971 else
8972 s->Printf("(%s) ", field_type_name.c_str());
8973 }
8974 // Print the member name and equal sign
8975 s->Printf("%s = ", field->getNameAsString().c_str());
8976
8977 // Dump the value of the member
8978 CompilerType field_clang_type(getASTContext(), field_type);
8979 field_clang_type.DumpValue(
8980 exe_ctx,
8981 s, // Stream to dump to
8982 field_clang_type
8983 .GetFormat(), // The format with which to display the member
8984 data, // Data buffer containing all bytes for this type
8985 data_byte_offset + field_byte_offset, // Offset into "data" where to
8986 // grab value from
8987 field_type_info.Width / 8, // Size of this type in bytes
8988 field_bitfield_bit_size, // Bitfield bit size
8989 field_bitfield_bit_offset, // Bitfield bit offset
8990 show_types, // Boolean indicating if we should show the variable
8991 // types
8992 show_summary, // Boolean indicating if we should show a summary for
8993 // the current type
8994 verbose, // Verbose output?
8995 depth + DEPTH_INCREMENT2); // Scope depth for any types that have
8996 // children
8997 }
8998
8999 // Indent the trailing squiggly bracket
9000 if (child_idx > 0)
9001 s->Printf("\n%*s}", depth, "");
9002 }
9003 return;
9004
9005 case clang::Type::Enum:
9006 if (GetCompleteType(type)) {
9007 const clang::EnumType *enutype =
9008 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9009 const clang::EnumDecl *enum_decl = enutype->getDecl();
9010 assert(enum_decl)(static_cast <bool> (enum_decl) ? void (0) : __assert_fail
("enum_decl", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 9010, __extension__ __PRETTY_FUNCTION__))
;
9011 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9012 lldb::offset_t offset = data_byte_offset;
9013 const int64_t enum_value = data.GetMaxU64Bitfield(
9014 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
9015 for (enum_pos = enum_decl->enumerator_begin(),
9016 enum_end_pos = enum_decl->enumerator_end();
9017 enum_pos != enum_end_pos; ++enum_pos) {
9018 if (enum_pos->getInitVal() == enum_value) {
9019 s->Printf("%s", enum_pos->getNameAsString().c_str());
9020 return;
9021 }
9022 }
9023 // If we have gotten here we didn't get find the enumerator in the
9024 // enum decl, so just print the integer.
9025 s->Printf("%" PRIi64"l" "i", enum_value);
9026 }
9027 return;
9028
9029 case clang::Type::ConstantArray: {
9030 const clang::ConstantArrayType *array =
9031 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9032 bool is_array_of_characters = false;
9033 clang::QualType element_qual_type = array->getElementType();
9034
9035 const clang::Type *canonical_type =
9036 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9037 if (canonical_type)
9038 is_array_of_characters = canonical_type->isCharType();
9039
9040 const uint64_t element_count = array->getSize().getLimitedValue();
9041
9042 clang::TypeInfo field_type_info =
9043 getASTContext()->getTypeInfo(element_qual_type);
9044
9045 uint32_t element_idx = 0;
9046 uint32_t element_offset = 0;
9047 uint64_t element_byte_size = field_type_info.Width / 8;
9048 uint32_t element_stride = element_byte_size;
9049
9050 if (is_array_of_characters) {
9051 s->PutChar('"');
9052 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9053 element_byte_size, element_count, UINT32_MAX(4294967295U),
9054 LLDB_INVALID_ADDRESS(18446744073709551615UL), 0, 0);
9055 s->PutChar('"');
9056 return;
9057 } else {
9058 CompilerType element_clang_type(getASTContext(), element_qual_type);
9059 lldb::Format element_format = element_clang_type.GetFormat();
9060
9061 for (element_idx = 0; element_idx < element_count; ++element_idx) {
9062 // Print the starting squiggly bracket (if this is the
9063 // first member) or comman (for member 2 and beyong) for
9064 // the struct/union/class member.
9065 if (element_idx == 0)
9066 s->PutChar('{');
9067 else
9068 s->PutChar(',');
9069
9070 // Indent and print the index
9071 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT2, "", element_idx);
9072
9073 // Figure out the field offset within the current struct/union/class
9074 // type
9075 element_offset = element_idx * element_stride;
9076
9077 // Dump the value of the member
9078 element_clang_type.DumpValue(
9079 exe_ctx,
9080 s, // Stream to dump to
9081 element_format, // The format with which to display the element
9082 data, // Data buffer containing all bytes for this type
9083 data_byte_offset +
9084 element_offset, // Offset into "data" where to grab value from
9085 element_byte_size, // Size of this type in bytes
9086 0, // Bitfield bit size
9087 0, // Bitfield bit offset
9088 show_types, // Boolean indicating if we should show the variable
9089 // types
9090 show_summary, // Boolean indicating if we should show a summary for
9091 // the current type
9092 verbose, // Verbose output?
9093 depth + DEPTH_INCREMENT2); // Scope depth for any types that have
9094 // children
9095 }
9096
9097 // Indent the trailing squiggly bracket
9098 if (element_idx > 0)
9099 s->Printf("\n%*s}", depth, "");
9100 }
9101 }
9102 return;
9103
9104 case clang::Type::Typedef: {
9105 clang::QualType typedef_qual_type =
9106 llvm::cast<clang::TypedefType>(qual_type)
9107 ->getDecl()
9108 ->getUnderlyingType();
9109
9110 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9111 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9112 clang::TypeInfo typedef_type_info =
9113 getASTContext()->getTypeInfo(typedef_qual_type);
9114 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9115
9116 return typedef_clang_type.DumpValue(
9117 exe_ctx,
9118 s, // Stream to dump to
9119 typedef_format, // The format with which to display the element
9120 data, // Data buffer containing all bytes for this type
9121 data_byte_offset, // Offset into "data" where to grab value from
9122 typedef_byte_size, // Size of this type in bytes
9123 bitfield_bit_size, // Bitfield bit size
9124 bitfield_bit_offset, // Bitfield bit offset
9125 show_types, // Boolean indicating if we should show the variable types
9126 show_summary, // Boolean indicating if we should show a summary for the
9127 // current type
9128 verbose, // Verbose output?
9129 depth); // Scope depth for any types that have children
9130 } break;
9131
9132 case clang::Type::Auto: {
9133 clang::QualType elaborated_qual_type =
9134 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9135 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9136 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9137 clang::TypeInfo elaborated_type_info =
9138 getASTContext()->getTypeInfo(elaborated_qual_type);
9139 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9140
9141 return elaborated_clang_type.DumpValue(
9142 exe_ctx,
9143 s, // Stream to dump to
9144 elaborated_format, // The format with which to display the element
9145 data, // Data buffer containing all bytes for this type
9146 data_byte_offset, // Offset into "data" where to grab value from
9147 elaborated_byte_size, // Size of this type in bytes
9148 bitfield_bit_size, // Bitfield bit size
9149 bitfield_bit_offset, // Bitfield bit offset
9150 show_types, // Boolean indicating if we should show the variable types
9151 show_summary, // Boolean indicating if we should show a summary for the
9152 // current type
9153 verbose, // Verbose output?
9154 depth); // Scope depth for any types that have children
9155 } break;
9156
9157 case clang::Type::Elaborated: {
9158 clang::QualType elaborated_qual_type =
9159 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9160 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9161 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9162 clang::TypeInfo elaborated_type_info =
9163 getASTContext()->getTypeInfo(elaborated_qual_type);
9164 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9165
9166 return elaborated_clang_type.DumpValue(
9167 exe_ctx,
9168 s, // Stream to dump to
9169 elaborated_format, // The format with which to display the element
9170 data, // Data buffer containing all bytes for this type
9171 data_byte_offset, // Offset into "data" where to grab value from
9172 elaborated_byte_size, // Size of this type in bytes
9173 bitfield_bit_size, // Bitfield bit size
9174 bitfield_bit_offset, // Bitfield bit offset
9175 show_types, // Boolean indicating if we should show the variable types
9176 show_summary, // Boolean indicating if we should show a summary for the
9177 // current type
9178 verbose, // Verbose output?
9179 depth); // Scope depth for any types that have children
9180 } break;
9181
9182 case clang::Type::Paren: {
9183 clang::QualType desugar_qual_type =
9184 llvm::cast<clang::ParenType>(qual_type)->desugar();
9185 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type);
9186
9187 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9188 clang::TypeInfo desugar_type_info =
9189 getASTContext()->getTypeInfo(desugar_qual_type);
9190 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9191
9192 return desugar_clang_type.DumpValue(
9193 exe_ctx,
9194 s, // Stream to dump to
9195 desugar_format, // The format with which to display the element
9196 data, // Data buffer containing all bytes for this type
9197 data_byte_offset, // Offset into "data" where to grab value from
9198 desugar_byte_size, // Size of this type in bytes
9199 bitfield_bit_size, // Bitfield bit size
9200 bitfield_bit_offset, // Bitfield bit offset
9201 show_types, // Boolean indicating if we should show the variable types
9202 show_summary, // Boolean indicating if we should show a summary for the
9203 // current type
9204 verbose, // Verbose output?
9205 depth); // Scope depth for any types that have children
9206 } break;
9207
9208 default:
9209 // We are down to a scalar type that we just need to display.
9210 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9211 UINT32_MAX(4294967295U), LLDB_INVALID_ADDRESS(18446744073709551615UL), bitfield_bit_size,
9212 bitfield_bit_offset);
9213
9214 if (show_summary)
9215 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9216 break;
9217 }
9218}
9219
9220bool ClangASTContext::DumpTypeValue(
9221 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
9222 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9223 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
9224 ExecutionContextScope *exe_scope) {
9225 if (!type)
9226 return false;
9227 if (IsAggregateType(type)) {
9228 return false;
9229 } else {
9230 clang::QualType qual_type(GetQualType(type));
9231
9232 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9233 switch (type_class) {
9234 case clang::Type::Typedef: {
9235 clang::QualType typedef_qual_type =
9236 llvm::cast<clang::TypedefType>(qual_type)
9237 ->getDecl()
9238 ->getUnderlyingType();
9239 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9240 if (format == eFormatDefault)
9241 format = typedef_clang_type.GetFormat();
9242 clang::TypeInfo typedef_type_info =
9243 getASTContext()->getTypeInfo(typedef_qual_type);
9244 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9245
9246 return typedef_clang_type.DumpTypeValue(
9247 s,
9248 format, // The format with which to display the element
9249 data, // Data buffer containing all bytes for this type
9250 byte_offset, // Offset into "data" where to grab value from
9251 typedef_byte_size, // Size of this type in bytes
9252 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9253 // treat as a bitfield
9254 bitfield_bit_offset, // Offset in bits of a bitfield value if
9255 // bitfield_bit_size != 0
9256 exe_scope);
9257 } break;
9258
9259 case clang::Type::Enum:
9260 // If our format is enum or default, show the enumeration value as
9261 // its enumeration string value, else just display it as requested.
9262 if ((format == eFormatEnum || format == eFormatDefault) &&
9263 GetCompleteType(type)) {
9264 const clang::EnumType *enutype =
9265 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9266 const clang::EnumDecl *enum_decl = enutype->getDecl();
9267 assert(enum_decl)(static_cast <bool> (enum_decl) ? void (0) : __assert_fail
("enum_decl", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 9267, __extension__ __PRETTY_FUNCTION__))
;
9268 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9269 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9270 lldb::offset_t offset = byte_offset;
9271 if (is_signed) {
9272 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9273 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9274 for (enum_pos = enum_decl->enumerator_begin(),
9275 enum_end_pos = enum_decl->enumerator_end();
9276 enum_pos != enum_end_pos; ++enum_pos) {
9277 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
9278 s->PutCString(enum_pos->getNameAsString());
9279 return true;
9280 }
9281 }
9282 // If we have gotten here we didn't get find the enumerator in the
9283 // enum decl, so just print the integer.
9284 s->Printf("%" PRIi64"l" "i", enum_svalue);
9285 } else {
9286 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9287 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9288 for (enum_pos = enum_decl->enumerator_begin(),
9289 enum_end_pos = enum_decl->enumerator_end();
9290 enum_pos != enum_end_pos; ++enum_pos) {
9291 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
9292 s->PutCString(enum_pos->getNameAsString());
9293 return true;
9294 }
9295 }
9296 // If we have gotten here we didn't get find the enumerator in the
9297 // enum decl, so just print the integer.
9298 s->Printf("%" PRIu64"l" "u", enum_uvalue);
9299 }
9300 return true;
9301 }
9302 // format was not enum, just fall through and dump the value as
9303 // requested....
9304 LLVM_FALLTHROUGH[[clang::fallthrough]];
9305
9306 default:
9307 // We are down to a scalar type that we just need to display.
9308 {
9309 uint32_t item_count = 1;
9310 // A few formats, we might need to modify our size and count for
9311 // depending
9312 // on how we are trying to display the value...
9313 switch (format) {
9314 default:
9315 case eFormatBoolean:
9316 case eFormatBinary:
9317 case eFormatComplex:
9318 case eFormatCString: // NULL terminated C strings
9319 case eFormatDecimal:
9320 case eFormatEnum:
9321 case eFormatHex:
9322 case eFormatHexUppercase:
9323 case eFormatFloat:
9324 case eFormatOctal:
9325 case eFormatOSType:
9326 case eFormatUnsigned:
9327 case eFormatPointer:
9328 case eFormatVectorOfChar:
9329 case eFormatVectorOfSInt8:
9330 case eFormatVectorOfUInt8:
9331 case eFormatVectorOfSInt16:
9332 case eFormatVectorOfUInt16:
9333 case eFormatVectorOfSInt32:
9334 case eFormatVectorOfUInt32:
9335 case eFormatVectorOfSInt64:
9336 case eFormatVectorOfUInt64:
9337 case eFormatVectorOfFloat32:
9338 case eFormatVectorOfFloat64:
9339 case eFormatVectorOfUInt128:
9340 break;
9341
9342 case eFormatChar:
9343 case eFormatCharPrintable:
9344 case eFormatCharArray:
9345 case eFormatBytes:
9346 case eFormatBytesWithASCII:
9347 item_count = byte_size;
9348 byte_size = 1;
9349 break;
9350
9351 case eFormatUnicode16:
9352 item_count = byte_size / 2;
9353 byte_size = 2;
9354 break;
9355
9356 case eFormatUnicode32:
9357 item_count = byte_size / 4;
9358 byte_size = 4;
9359 break;
9360 }
9361 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9362 item_count, UINT32_MAX(4294967295U), LLDB_INVALID_ADDRESS(18446744073709551615UL),
9363 bitfield_bit_size, bitfield_bit_offset,
9364 exe_scope);
9365 }
9366 break;
9367 }
9368 }
9369 return 0;
9370}
9371
9372void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9373 ExecutionContext *exe_ctx, Stream *s,
9374 const lldb_private::DataExtractor &data,
9375 lldb::offset_t data_byte_offset,
9376 size_t data_byte_size) {
9377 uint32_t length = 0;
9378 if (IsCStringType(type, length)) {
9379 if (exe_ctx) {
9380 Process *process = exe_ctx->GetProcessPtr();
9381 if (process) {
9382 lldb::offset_t offset = data_byte_offset;
9383 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9384 std::vector<uint8_t> buf;
9385 if (length > 0)
9386 buf.resize(length);
9387 else
9388 buf.resize(256);
9389
9390 DataExtractor cstr_data(&buf.front(), buf.size(),
9391 process->GetByteOrder(), 4);
9392 buf.back() = '\0';
9393 size_t bytes_read;
9394 size_t total_cstr_len = 0;
9395 Status error;
9396 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9397 buf.size(), error)) > 0) {
9398 const size_t len = strlen((const char *)&buf.front());
9399 if (len == 0)
9400 break;
9401 if (total_cstr_len == 0)
9402 s->PutCString(" \"");
9403 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9404 UINT32_MAX(4294967295U), LLDB_INVALID_ADDRESS(18446744073709551615UL), 0, 0);
9405 total_cstr_len += len;
9406 if (len < buf.size())
9407 break;
9408 pointer_address += total_cstr_len;
9409 }
9410 if (total_cstr_len > 0)
9411 s->PutChar('"');
9412 }
9413 }
9414 }
9415}
9416
9417void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9418 StreamFile s(stdoutstdout, false);
9419 DumpTypeDescription(type, &s);
9420 ClangASTMetadata *metadata =
9421 ClangASTContext::GetMetadata(getASTContext(), type);
9422 if (metadata) {
9423 metadata->Dump(&s);
9424 }
9425}
9426
9427void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9428 Stream *s) {
9429 if (type) {
9430 clang::QualType qual_type(GetQualType(type));
9431
9432 llvm::SmallVector<char, 1024> buf;
9433 llvm::raw_svector_ostream llvm_ostrm(buf);
9434
9435 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9436 switch (type_class) {
9437 case clang::Type::ObjCObject:
9438 case clang::Type::ObjCInterface: {
9439 GetCompleteType(type);
9440
9441 const clang::ObjCObjectType *objc_class_type =
9442 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9443 assert(objc_class_type)(static_cast <bool> (objc_class_type) ? void (0) : __assert_fail
("objc_class_type", "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 9443, __extension__ __PRETTY_FUNCTION__))
;
9444 if (objc_class_type) {
9445 clang::ObjCInterfaceDecl *class_interface_decl =
9446 objc_class_type->getInterface();
9447 if (class_interface_decl) {
9448 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9449 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
9450 }
9451 }
9452 } break;
9453
9454 case clang::Type::Typedef: {
9455 const clang::TypedefType *typedef_type =
9456 qual_type->getAs<clang::TypedefType>();
9457 if (typedef_type) {
9458 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9459 std::string clang_typedef_name(
9460 typedef_decl->getQualifiedNameAsString());
9461 if (!clang_typedef_name.empty()) {
9462 s->PutCString("typedef ");
9463 s->PutCString(clang_typedef_name);
9464 }
9465 }
9466 } break;
9467
9468 case clang::Type::Auto:
9469 CompilerType(getASTContext(),
9470 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
9471 .DumpTypeDescription(s);
9472 return;
9473
9474 case clang::Type::Elaborated:
9475 CompilerType(getASTContext(),
9476 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
9477 .DumpTypeDescription(s);
9478 return;
9479
9480 case clang::Type::Paren:
9481 CompilerType(getASTContext(),
9482 llvm::cast<clang::ParenType>(qual_type)->desugar())
9483 .DumpTypeDescription(s);
9484 return;
9485
9486 case clang::Type::Record: {
9487 GetCompleteType(type);
9488
9489 const clang::RecordType *record_type =
9490 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9491 const clang::RecordDecl *record_decl = record_type->getDecl();
9492 const clang::CXXRecordDecl *cxx_record_decl =
9493 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9494
9495 if (cxx_record_decl)
9496 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9497 s->GetIndentLevel());
9498 else
9499 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9500 s->GetIndentLevel());
9501 } break;
9502
9503 default: {
9504 const clang::TagType *tag_type =
9505 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9506 if (tag_type) {
9507 clang::TagDecl *tag_decl = tag_type->getDecl();
9508 if (tag_decl)
9509 tag_decl->print(llvm_ostrm, 0);
9510 } else {
9511 std::string clang_type_name(qual_type.getAsString());
9512 if (!clang_type_name.empty())
9513 s->PutCString(clang_type_name);
9514 }
9515 }
9516 }
9517
9518 if (buf.size() > 0) {
9519 s->Write(buf.data(), buf.size());
9520 }
9521 }
9522}
9523
9524void ClangASTContext::DumpTypeName(const CompilerType &type) {
9525 if (ClangUtil::IsClangType(type)) {
9526 clang::QualType qual_type(
9527 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9528
9529 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9530 switch (type_class) {
9531 case clang::Type::Record: {
9532 const clang::CXXRecordDecl *cxx_record_decl =
9533 qual_type->getAsCXXRecordDecl();
9534 if (cxx_record_decl)
9535 printf("class %s", cxx_record_decl->getName().str().c_str());
9536 } break;
9537
9538 case clang::Type::Enum: {
9539 clang::EnumDecl *enum_decl =
9540 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9541 if (enum_decl) {
9542 printf("enum %s", enum_decl->getName().str().c_str());
9543 }
9544 } break;
9545
9546 case clang::Type::ObjCObject:
9547 case clang::Type::ObjCInterface: {
9548 const clang::ObjCObjectType *objc_class_type =
9549 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9550 if (objc_class_type) {
9551 clang::ObjCInterfaceDecl *class_interface_decl =
9552 objc_class_type->getInterface();
9553 // We currently can't complete objective C types through the newly added
9554 // ASTContext
9555 // because it only supports TagDecl objects right now...
9556 if (class_interface_decl)
9557 printf("@class %s", class_interface_decl->getName().str().c_str());
9558 }
9559 } break;
9560
9561 case clang::Type::Typedef:
9562 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9563 ->getDecl()
9564 ->getName()
9565 .str()
9566 .c_str());
9567 break;
9568
9569 case clang::Type::Auto:
9570 printf("auto ");
9571 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9572 llvm::cast<clang::AutoType>(qual_type)
9573 ->getDeducedType()
9574 .getAsOpaquePtr()));
9575
9576 case clang::Type::Elaborated:
9577 printf("elaborated ");
9578 return DumpTypeName(CompilerType(
9579 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9580 ->getNamedType()
9581 .getAsOpaquePtr()));
9582
9583 case clang::Type::Paren:
9584 printf("paren ");
9585 return DumpTypeName(CompilerType(
9586 type.GetTypeSystem(),
9587 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9588
9589 default:
9590 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9591 break;
9592 }
9593 }
9594}
9595
9596clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9597 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9598 const char *parent_name, int tag_decl_kind,
9599 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9600 if (template_param_infos.IsValid()) {
9601 std::string template_basename(parent_name);
9602 template_basename.erase(template_basename.find('<'));
9603
9604 return CreateClassTemplateDecl(decl_ctx, access_type,
9605 template_basename.c_str(), tag_decl_kind,
9606 template_param_infos);
9607 }
9608 return NULL__null;
9609}
9610
9611void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9612 ClangASTContext *ast = (ClangASTContext *)baton;
9613 SymbolFile *sym_file = ast->GetSymbolFile();
9614 if (sym_file) {
9615 CompilerType clang_type = GetTypeForDecl(decl);
9616 if (clang_type)
9617 sym_file->CompleteType(clang_type);
9618 }
9619}
9620
9621void ClangASTContext::CompleteObjCInterfaceDecl(
9622 void *baton, clang::ObjCInterfaceDecl *decl) {
9623 ClangASTContext *ast = (ClangASTContext *)baton;
9624 SymbolFile *sym_file = ast->GetSymbolFile();
9625 if (sym_file) {
9626 CompilerType clang_type = GetTypeForDecl(decl);
9627 if (clang_type)
9628 sym_file->CompleteType(clang_type);
9629 }
9630}
9631
9632DWARFASTParser *ClangASTContext::GetDWARFParser() {
9633 if (!m_dwarf_ast_parser_ap)
9634 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
9635 return m_dwarf_ast_parser_ap.get();
9636}
9637
9638PDBASTParser *ClangASTContext::GetPDBParser() {
9639 if (!m_pdb_ast_parser_ap)
9640 m_pdb_ast_parser_ap.reset(new PDBASTParser(*this));
9641 return m_pdb_ast_parser_ap.get();
9642}
9643
9644bool ClangASTContext::LayoutRecordType(
9645 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9646 uint64_t &alignment,
9647 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9648 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9649 &base_offsets,
9650 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9651 &vbase_offsets) {
9652 ClangASTContext *ast = (ClangASTContext *)baton;
9653 DWARFASTParserClang *dwarf_ast_parser =
9654 (DWARFASTParserClang *)ast->GetDWARFParser();
9655 return dwarf_ast_parser->GetClangASTImporter().LayoutRecordType(
9656 record_decl, bit_size, alignment, field_offsets, base_offsets,
9657 vbase_offsets);
9658}
9659
9660//----------------------------------------------------------------------
9661// CompilerDecl override functions
9662//----------------------------------------------------------------------
9663
9664ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9665 if (opaque_decl) {
9666 clang::NamedDecl *nd =
9667 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9668 if (nd != nullptr)
9669 return ConstString(nd->getDeclName().getAsString());
9670 }
9671 return ConstString();
9672}
9673
9674ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9675 if (opaque_decl) {
9676 clang::NamedDecl *nd =
9677 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9678 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9679 clang::MangleContext *mc = getMangleContext();
9680 if (mc && mc->shouldMangleCXXName(nd)) {
9681 llvm::SmallVector<char, 1024> buf;
9682 llvm::raw_svector_ostream llvm_ostrm(buf);
9683 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9684 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9685 Ctor_Complete, llvm_ostrm);
9686 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9687 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9688 Dtor_Complete, llvm_ostrm);
9689 } else {
9690 mc->mangleName(nd, llvm_ostrm);
9691 }
9692 if (buf.size() > 0)
9693 return ConstString(buf.data(), buf.size());
9694 }
9695 }
9696 }
9697 return ConstString();
9698}
9699
9700CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9701 if (opaque_decl)
9702 return CompilerDeclContext(this,
9703 ((clang::Decl *)opaque_decl)->getDeclContext());
9704 else
9705 return CompilerDeclContext();
9706}
9707
9708CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9709 if (clang::FunctionDecl *func_decl =
9710 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9711 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9712 if (clang::ObjCMethodDecl *objc_method =
9713 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9714 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9715 else
9716 return CompilerType();
9717}
9718
9719size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9720 if (clang::FunctionDecl *func_decl =
9721 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9722 return func_decl->param_size();
9723 if (clang::ObjCMethodDecl *objc_method =
9724 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9725 return objc_method->param_size();
9726 else
9727 return 0;
9728}
9729
9730CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
9731 size_t idx) {
9732 if (clang::FunctionDecl *func_decl =
9733 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
9734 if (idx < func_decl->param_size()) {
9735 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9736 if (var_decl)
9737 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
9738 }
9739 } else if (clang::ObjCMethodDecl *objc_method =
9740 llvm::dyn_cast<clang::ObjCMethodDecl>(
9741 (clang::Decl *)opaque_decl)) {
9742 if (idx < objc_method->param_size())
9743 return CompilerType(
9744 this,
9745 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9746 }
9747 return CompilerType();
9748}
9749
9750//----------------------------------------------------------------------
9751// CompilerDeclContext functions
9752//----------------------------------------------------------------------
9753
9754std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
9755 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
9756 std::vector<CompilerDecl> found_decls;
9757 if (opaque_decl_ctx) {
9758 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
9759 std::set<DeclContext *> searched;
9760 std::multimap<DeclContext *, DeclContext *> search_queue;
9761 SymbolFile *symbol_file = GetSymbolFile();
9762
9763 for (clang::DeclContext *decl_context = root_decl_ctx;
9764 decl_context != nullptr && found_decls.empty();
9765 decl_context = decl_context->getParent()) {
9766 search_queue.insert(std::make_pair(decl_context, decl_context));
9767
9768 for (auto it = search_queue.find(decl_context); it != search_queue.end();
9769 it++) {
9770 if (!searched.insert(it->second).second)
9771 continue;
9772 symbol_file->ParseDeclsForContext(
9773 CompilerDeclContext(this, it->second));
9774
9775 for (clang::Decl *child : it->second->decls()) {
9776 if (clang::UsingDirectiveDecl *ud =
9777 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9778 if (ignore_using_decls)
9779 continue;
9780 clang::DeclContext *from = ud->getCommonAncestor();
9781 if (searched.find(ud->getNominatedNamespace()) == searched.end())
9782 search_queue.insert(
9783 std::make_pair(from, ud->getNominatedNamespace()));
9784 } else if (clang::UsingDecl *ud =
9785 llvm::dyn_cast<clang::UsingDecl>(child)) {
9786 if (ignore_using_decls)
9787 continue;
9788 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9789 clang::Decl *target = usd->getTargetDecl();
9790 if (clang::NamedDecl *nd =
9791 llvm::dyn_cast<clang::NamedDecl>(target)) {
9792 IdentifierInfo *ii = nd->getIdentifier();
9793 if (ii != nullptr &&
9794 ii->getName().equals(name.AsCString(nullptr)))
9795 found_decls.push_back(CompilerDecl(this, nd));
9796 }
9797 }
9798 } else if (clang::NamedDecl *nd =
9799 llvm::dyn_cast<clang::NamedDecl>(child)) {
9800 IdentifierInfo *ii = nd->getIdentifier();
9801 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
9802 found_decls.push_back(CompilerDecl(this, nd));
9803 }
9804 }
9805 }
9806 }
9807 }
9808 return found_decls;
9809}
9810
9811// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
9812// and return the number of levels it took to find it, or
9813// LLDB_INVALID_DECL_LEVEL
9814// if not found. If the decl was imported via a using declaration, its name
9815// and/or
9816// type, if set, will be used to check that the decl found in the scope is a
9817// match.
9818//
9819// The optional name is required by languages (like C++) to handle using
9820// declarations
9821// like:
9822//
9823// void poo();
9824// namespace ns {
9825// void foo();
9826// void goo();
9827// }
9828// void bar() {
9829// using ns::foo;
9830// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
9831// // LLDB_INVALID_DECL_LEVEL for 'goo'.
9832// }
9833//
9834// The optional type is useful in the case that there's a specific overload
9835// that we're looking for that might otherwise be shadowed, like:
9836//
9837// void foo(int);
9838// namespace ns {
9839// void foo();
9840// }
9841// void bar() {
9842// using ns::foo;
9843// // CountDeclLevels returns 0 for { 'foo', void() },
9844// // 1 for { 'foo', void(int) }, and
9845// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
9846// }
9847//
9848// NOTE: Because file statics are at the TranslationUnit along with globals, a
9849// function at file scope will return the same level as a function at global
9850// scope.
9851// Ideally we'd like to treat the file scope as an additional scope just below
9852// the
9853// global scope. More work needs to be done to recognise that, if the decl
9854// we're
9855// trying to look up is static, we should compare its source file with that of
9856// the
9857// current scope and return a lower number for it.
9858uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
9859 clang::DeclContext *child_decl_ctx,
9860 ConstString *child_name,
9861 CompilerType *child_type) {
9862 if (frame_decl_ctx) {
9863 std::set<DeclContext *> searched;
9864 std::multimap<DeclContext *, DeclContext *> search_queue;
9865 SymbolFile *symbol_file = GetSymbolFile();
9866
9867 // Get the lookup scope for the decl we're trying to find.
9868 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
9869
9870 // Look for it in our scope's decl context and its parents.
9871 uint32_t level = 0;
9872 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
9873 decl_ctx = decl_ctx->getParent()) {
9874 if (!decl_ctx->isLookupContext())
9875 continue;
9876 if (decl_ctx == parent_decl_ctx)
9877 // Found it!
9878 return level;
9879 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
9880 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
9881 it++) {
9882 if (searched.find(it->second) != searched.end())
9883 continue;
9884
9885 // Currently DWARF has one shared translation unit for all Decls at top
9886 // level, so this
9887 // would erroneously find using statements anywhere. So don't look at
9888 // the top-level
9889 // translation unit.
9890 // TODO fix this and add a testcase that depends on it.
9891
9892 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
9893 continue;
9894
9895 searched.insert(it->second);
9896 symbol_file->ParseDeclsForContext(
9897 CompilerDeclContext(this, it->second));
9898
9899 for (clang::Decl *child : it->second->decls()) {
9900 if (clang::UsingDirectiveDecl *ud =
9901 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9902 clang::DeclContext *ns = ud->getNominatedNamespace();
9903 if (ns == parent_decl_ctx)
9904 // Found it!
9905 return level;
9906 clang::DeclContext *from = ud->getCommonAncestor();
9907 if (searched.find(ns) == searched.end())
9908 search_queue.insert(std::make_pair(from, ns));
9909 } else if (child_name) {
9910 if (clang::UsingDecl *ud =
9911 llvm::dyn_cast<clang::UsingDecl>(child)) {
9912 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9913 clang::Decl *target = usd->getTargetDecl();
9914 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
9915 if (!nd)
9916 continue;
9917 // Check names.
9918 IdentifierInfo *ii = nd->getIdentifier();
9919 if (ii == nullptr ||
9920 !ii->getName().equals(child_name->AsCString(nullptr)))
9921 continue;
9922 // Check types, if one was provided.
9923 if (child_type) {
9924 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
9925 if (!AreTypesSame(clang_type, *child_type,
9926 /*ignore_qualifiers=*/true))
9927 continue;
9928 }
9929 // Found it!
9930 return level;
9931 }
9932 }
9933 }
9934 }
9935 }
9936 ++level;
9937 }
9938 }
9939 return LLDB_INVALID_DECL_LEVEL(4294967295U);
9940}
9941
9942bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
9943 if (opaque_decl_ctx)
9944 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
9945 else
9946 return false;
9947}
9948
9949ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
9950 if (opaque_decl_ctx) {
9951 clang::NamedDecl *named_decl =
9952 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9953 if (named_decl)
9954 return ConstString(named_decl->getName());
9955 }
9956 return ConstString();
9957}
9958
9959ConstString
9960ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
9961 if (opaque_decl_ctx) {
9962 clang::NamedDecl *named_decl =
9963 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9964 if (named_decl)
9965 return ConstString(
9966 llvm::StringRef(named_decl->getQualifiedNameAsString()));
9967 }
9968 return ConstString();
9969}
9970
9971bool ClangASTContext::DeclContextIsClassMethod(
9972 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
9973 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
9974 if (opaque_decl_ctx) {
9975 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
9976 if (ObjCMethodDecl *objc_method =
9977 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
9978 if (is_instance_method_ptr)
9979 *is_instance_method_ptr = objc_method->isInstanceMethod();
9980 if (language_ptr)
9981 *language_ptr = eLanguageTypeObjC;
9982 if (language_object_name_ptr)
9983 language_object_name_ptr->SetCString("self");
9984 return true;
9985 } else if (CXXMethodDecl *cxx_method =
9986 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
9987 if (is_instance_method_ptr)
9988 *is_instance_method_ptr = cxx_method->isInstance();
9989 if (language_ptr)
9990 *language_ptr = eLanguageTypeC_plus_plus;
9991 if (language_object_name_ptr)
9992 language_object_name_ptr->SetCString("this");
9993 return true;
9994 } else if (clang::FunctionDecl *function_decl =
9995 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
9996 ClangASTMetadata *metadata =
9997 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
9998 if (metadata && metadata->HasObjectPtr()) {
9999 if (is_instance_method_ptr)
10000 *is_instance_method_ptr = true;
10001 if (language_ptr)
10002 *language_ptr = eLanguageTypeObjC;
10003 if (language_object_name_ptr)
10004 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
10005 return true;
10006 }
10007 }
10008 }
10009 return false;
10010}
10011
10012clang::DeclContext *
10013ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
10014 if (dc.IsClang())
10015 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
10016 return nullptr;
10017}
10018
10019ObjCMethodDecl *
10020ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
10021 if (dc.IsClang())
10022 return llvm::dyn_cast<clang::ObjCMethodDecl>(
10023 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10024 return nullptr;
10025}
10026
10027CXXMethodDecl *
10028ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10029 if (dc.IsClang())
10030 return llvm::dyn_cast<clang::CXXMethodDecl>(
10031 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10032 return nullptr;
10033}
10034
10035clang::FunctionDecl *
10036ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10037 if (dc.IsClang())
10038 return llvm::dyn_cast<clang::FunctionDecl>(
10039 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10040 return nullptr;
10041}
10042
10043clang::NamespaceDecl *
10044ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10045 if (dc.IsClang())
10046 return llvm::dyn_cast<clang::NamespaceDecl>(
10047 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10048 return nullptr;
10049}
10050
10051ClangASTMetadata *
10052ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10053 const void *object) {
10054 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10055 if (ast)
10056 return ClangASTContext::GetMetadata(ast, object);
10057 return nullptr;
10058}
10059
10060clang::ASTContext *
10061ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10062 ClangASTContext *ast =
10063 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10064 if (ast)
10065 return ast->getASTContext();
10066 return nullptr;
10067}
10068
10069ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10070 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10071 m_target_wp(target.shared_from_this()),
10072 m_persistent_variables(new ClangPersistentVariables) {}
10073
10074UserExpression *ClangASTContextForExpressions::GetUserExpression(
10075 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
10076 Expression::ResultType desired_type,
10077 const EvaluateExpressionOptions &options) {
10078 TargetSP target_sp = m_target_wp.lock();
10079 if (!target_sp)
10080 return nullptr;
10081
10082 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
10083 desired_type, options);
10084}
10085
10086FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10087 const CompilerType &return_type, const Address &function_address,
10088 const ValueList &arg_value_list, const char *name) {
10089 TargetSP target_sp = m_target_wp.lock();
10090 if (!target_sp)
10091 return nullptr;
10092
10093 Process *process = target_sp->GetProcessSP().get();
10094 if (!process)
10095 return nullptr;
10096
10097 return new ClangFunctionCaller(*process, return_type, function_address,
10098 arg_value_list, name);
10099}
10100
10101UtilityFunction *
10102ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10103 const char *name) {
10104 TargetSP target_sp = m_target_wp.lock();
10105 if (!target_sp)
10106 return nullptr;
10107
10108 return new ClangUtilityFunction(*target_sp.get(), text, name);
10109}
10110
10111PersistentExpressionState *
10112ClangASTContextForExpressions::GetPersistentExpressionState() {
10113 return m_persistent_variables.get();
10114}
10115
10116clang::ExternalASTMerger &
10117ClangASTContextForExpressions::GetMergerUnchecked() {
10118 lldbassert(m_scratch_ast_source_ap != nullptr)lldb_private::lldb_assert(m_scratch_ast_source_ap != nullptr,
"m_scratch_ast_source_ap != nullptr", __FUNCTION__, "/build/llvm-toolchain-snapshot-7~svn329677/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 10118)
;
10119 return m_scratch_ast_source_ap->GetMergerUnchecked();
10120}