Bug Summary

File:tools/lldb/source/Symbol/ClangASTContext.cpp
Warning:line 1814, column 32
Forming reference to null pointer

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~svn326551/build-llvm/tools/lldb/source/Symbol -I /build/llvm-toolchain-snapshot-7~svn326551/tools/lldb/source/Symbol -I /build/llvm-toolchain-snapshot-7~svn326551/build-llvm/tools/lldb/include -I /build/llvm-toolchain-snapshot-7~svn326551/tools/lldb/include -I /build/llvm-toolchain-snapshot-7~svn326551/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn326551/include -I /usr/include/python2.7 -I /build/llvm-toolchain-snapshot-7~svn326551/tools/clang/include -I /build/llvm-toolchain-snapshot-7~svn326551/build-llvm/tools/lldb/../clang/include -I /build/llvm-toolchain-snapshot-7~svn326551/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~svn326551/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-03-02-155150-1477-1 -x c++ /build/llvm-toolchain-snapshot-7~svn326551/tools/lldb/source/Symbol/ClangASTContext.cpp

/build/llvm-toolchain-snapshot-7~svn326551/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;
263
Calling defaulted default constructor for 'once_flag'
264
Returning from default constructor for 'once_flag'
135 llvm::call_once(g_once_flag, []() {
265
Calling 'call_once'
278
Returning from 'call_once'
136 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
270
Calling default constructor for 'ThreadSafeDenseMap'
275
Returning from default constructor for 'ThreadSafeDenseMap'
137 });
138 return *g_map_ptr;
139}
140
141static bool IsOperator(const char *name,
142 clang::OverloadedOperatorKind &op_kind) {
143 if (name == nullptr || name[0] == '\0')
144 return false;
145
146#define OPERATOR_PREFIX "operator"
147#define OPERATOR_PREFIX_LENGTH (sizeof(OPERATOR_PREFIX) - 1)
148
149 const char *post_op_name = nullptr;
150
151 bool no_space = true;
152
153 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
154 return false;
155
156 post_op_name = name + OPERATOR_PREFIX_LENGTH;
157
158 if (post_op_name[0] == ' ') {
159 post_op_name++;
160 no_space = false;
161 }
162
163#undef OPERATOR_PREFIX
164#undef OPERATOR_PREFIX_LENGTH
165
166 // This is an operator, set the overloaded operator kind to invalid
167 // in case this is a conversion operator...
168 op_kind = clang::NUM_OVERLOADED_OPERATORS;
169
170 switch (post_op_name[0]) {
171 default:
172 if (no_space)
173 return false;
174 break;
175 case 'n':
176 if (no_space)
177 return false;
178 if (strcmp(post_op_name, "new") == 0)
179 op_kind = clang::OO_New;
180 else if (strcmp(post_op_name, "new[]") == 0)
181 op_kind = clang::OO_Array_New;
182 break;
183
184 case 'd':
185 if (no_space)
186 return false;
187 if (strcmp(post_op_name, "delete") == 0)
188 op_kind = clang::OO_Delete;
189 else if (strcmp(post_op_name, "delete[]") == 0)
190 op_kind = clang::OO_Array_Delete;
191 break;
192
193 case '+':
194 if (post_op_name[1] == '\0')
195 op_kind = clang::OO_Plus;
196 else if (post_op_name[2] == '\0') {
197 if (post_op_name[1] == '=')
198 op_kind = clang::OO_PlusEqual;
199 else if (post_op_name[1] == '+')
200 op_kind = clang::OO_PlusPlus;
201 }
202 break;
203
204 case '-':
205 if (post_op_name[1] == '\0')
206 op_kind = clang::OO_Minus;
207 else if (post_op_name[2] == '\0') {
208 switch (post_op_name[1]) {
209 case '=':
210 op_kind = clang::OO_MinusEqual;
211 break;
212 case '-':
213 op_kind = clang::OO_MinusMinus;
214 break;
215 case '>':
216 op_kind = clang::OO_Arrow;
217 break;
218 }
219 } else if (post_op_name[3] == '\0') {
220 if (post_op_name[2] == '*')
221 op_kind = clang::OO_ArrowStar;
222 break;
223 }
224 break;
225
226 case '*':
227 if (post_op_name[1] == '\0')
228 op_kind = clang::OO_Star;
229 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
230 op_kind = clang::OO_StarEqual;
231 break;
232
233 case '/':
234 if (post_op_name[1] == '\0')
235 op_kind = clang::OO_Slash;
236 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
237 op_kind = clang::OO_SlashEqual;
238 break;
239
240 case '%':
241 if (post_op_name[1] == '\0')
242 op_kind = clang::OO_Percent;
243 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
244 op_kind = clang::OO_PercentEqual;
245 break;
246
247 case '^':
248 if (post_op_name[1] == '\0')
249 op_kind = clang::OO_Caret;
250 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
251 op_kind = clang::OO_CaretEqual;
252 break;
253
254 case '&':
255 if (post_op_name[1] == '\0')
256 op_kind = clang::OO_Amp;
257 else if (post_op_name[2] == '\0') {
258 switch (post_op_name[1]) {
259 case '=':
260 op_kind = clang::OO_AmpEqual;
261 break;
262 case '&':
263 op_kind = clang::OO_AmpAmp;
264 break;
265 }
266 }
267 break;
268
269 case '|':
270 if (post_op_name[1] == '\0')
271 op_kind = clang::OO_Pipe;
272 else if (post_op_name[2] == '\0') {
273 switch (post_op_name[1]) {
274 case '=':
275 op_kind = clang::OO_PipeEqual;
276 break;
277 case '|':
278 op_kind = clang::OO_PipePipe;
279 break;
280 }
281 }
282 break;
283
284 case '~':
285 if (post_op_name[1] == '\0')
286 op_kind = clang::OO_Tilde;
287 break;
288
289 case '!':
290 if (post_op_name[1] == '\0')
291 op_kind = clang::OO_Exclaim;
292 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
293 op_kind = clang::OO_ExclaimEqual;
294 break;
295
296 case '=':
297 if (post_op_name[1] == '\0')
298 op_kind = clang::OO_Equal;
299 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
300 op_kind = clang::OO_EqualEqual;
301 break;
302
303 case '<':
304 if (post_op_name[1] == '\0')
305 op_kind = clang::OO_Less;
306 else if (post_op_name[2] == '\0') {
307 switch (post_op_name[1]) {
308 case '<':
309 op_kind = clang::OO_LessLess;
310 break;
311 case '=':
312 op_kind = clang::OO_LessEqual;
313 break;
314 }
315 } else if (post_op_name[3] == '\0') {
316 if (post_op_name[2] == '=')
317 op_kind = clang::OO_LessLessEqual;
318 }
319 break;
320
321 case '>':
322 if (post_op_name[1] == '\0')
323 op_kind = clang::OO_Greater;
324 else if (post_op_name[2] == '\0') {
325 switch (post_op_name[1]) {
326 case '>':
327 op_kind = clang::OO_GreaterGreater;
328 break;
329 case '=':
330 op_kind = clang::OO_GreaterEqual;
331 break;
332 }
333 } else if (post_op_name[1] == '>' && post_op_name[2] == '=' &&
334 post_op_name[3] == '\0') {
335 op_kind = clang::OO_GreaterGreaterEqual;
336 }
337 break;
338
339 case ',':
340 if (post_op_name[1] == '\0')
341 op_kind = clang::OO_Comma;
342 break;
343
344 case '(':
345 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
346 op_kind = clang::OO_Call;
347 break;
348
349 case '[':
350 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
351 op_kind = clang::OO_Subscript;
352 break;
353 }
354
355 return true;
356}
357
358clang::AccessSpecifier
359ClangASTContext::ConvertAccessTypeToAccessSpecifier(AccessType access) {
360 switch (access) {
361 default:
362 break;
363 case eAccessNone:
364 return AS_none;
365 case eAccessPublic:
366 return AS_public;
367 case eAccessPrivate:
368 return AS_private;
369 case eAccessProtected:
370 return AS_protected;
371 }
372 return AS_none;
373}
374
375static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) {
376 // FIXME: Cleanup per-file based stuff.
377
378 // Set some properties which depend solely on the input kind; it would be nice
379 // to move these to the language standard, and have the driver resolve the
380 // input kind + language standard.
381 if (IK.getLanguage() == InputKind::Asm) {
382 Opts.AsmPreprocessor = 1;
383 } else if (IK.isObjectiveC()) {
384 Opts.ObjC1 = Opts.ObjC2 = 1;
385 }
386
387 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
388
389 if (LangStd == LangStandard::lang_unspecified) {
390 // Based on the base language, pick one.
391 switch (IK.getLanguage()) {
392 case InputKind::Unknown:
393 case InputKind::LLVM_IR:
394 case InputKind::RenderScript:
395 llvm_unreachable("Invalid input kind!")::llvm::llvm_unreachable_internal("Invalid input kind!", "/build/llvm-toolchain-snapshot-7~svn326551/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~svn326551/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();
338
Calling 'ClangASTContext::getASTContext'
365
Returning from 'ClangASTContext::getASTContext'
625 if (ast) {
366
Taking true branch
626 ast->setExternalSource(ast_source_ap);
367
Calling copy constructor for 'IntrusiveRefCntPtr'
375
Returning from copy constructor for 'IntrusiveRefCntPtr'
627 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
376
Calling 'ASTContext::getTranslationUnitDecl'
377
Returning from 'ASTContext::getTranslationUnitDecl'
378
Calling 'DeclContext::setHasExternalLexicalStorage'
379
Returning from 'DeclContext::setHasExternalLexicalStorage'
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) {
4
Calling 'unique_ptr::get'
15
Returning from 'unique_ptr::get'
16
Assuming the condition is true
17
Taking true branch
339
Calling 'unique_ptr::get'
350
Returning from 'unique_ptr::get'
351
Assuming the condition is false
352
Taking false branch
654 m_ast_owned = true;
655 m_ast_ap.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
18
Calling 'ClangASTContext::getLanguageOptions'
45
Returning from 'ClangASTContext::getLanguageOptions'
46
Calling 'ClangASTContext::getSourceManager'
73
Returning from 'ClangASTContext::getSourceManager'
158
Calling 'unique_ptr::reset'
175
Returning from 'unique_ptr::reset'
656 *getIdentifierTable(), *getSelectorTable(),
74
Calling 'ClangASTContext::getIdentifierTable'
101
Returning from 'ClangASTContext::getIdentifierTable'
102
Calling 'ClangASTContext::getSelectorTable'
129
Returning from 'ClangASTContext::getSelectorTable'
657 *getBuiltinContext()));
130
Calling 'ClangASTContext::getBuiltinContext'
157
Returning from 'ClangASTContext::getBuiltinContext'
658
659 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
176
Calling 'unique_ptr::operator->'
189
Returning from 'unique_ptr::operator->'
190
Calling 'ClangASTContext::getDiagnosticConsumer'
217
Returning from 'ClangASTContext::getDiagnosticConsumer'
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();
218
Calling 'ClangASTContext::getTargetInfo'
244
Returning from 'ClangASTContext::getTargetInfo'
665 if (target_info)
245
Taking true branch
666 m_ast_ap->InitBuiltinTypes(*target_info);
246
Calling 'unique_ptr::operator->'
259
Returning from 'unique_ptr::operator->'
667
668 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) {
260
Assuming the condition is false
261
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);
262
Calling 'GetASTMap'
279
Returning from 'GetASTMap'
280
Calling 'unique_ptr::get'
291
Returning from 'unique_ptr::get'
292
Calling 'ThreadSafeDenseMap::Insert'
325
Returning from 'ThreadSafeDenseMap::Insert'
674
675 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap(
328
Calling constructor for 'IntrusiveRefCntPtr'
336
Returning from constructor for 'IntrusiveRefCntPtr'
676 new ClangExternalASTSourceCallbacks(
326
Calling constructor for 'ClangExternalASTSourceCallbacks'
327
Returning from constructor for 'ClangExternalASTSourceCallbacks'
677 ClangASTContext::CompleteTagDecl,
678 ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
679 ClangASTContext::LayoutRecordType, this));
680 SetExternalSource(ast_source_ap);
337
Calling 'ClangASTContext::SetExternalSource'
380
Returning from 'ClangASTContext::SetExternalSource'
681 }
381
Calling '~IntrusiveRefCntPtr'
392
Returning from '~IntrusiveRefCntPtr'
682 return m_ast_ap.get();
353
Calling 'unique_ptr::get'
364
Returning from 'unique_ptr::get'
393
Calling 'unique_ptr::get'
404
Returning from 'unique_ptr::get'
683}
684
685ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
686 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
687 return clang_ast;
688}
689
690Builtin::Context *ClangASTContext::getBuiltinContext() {
691 if (m_builtins_ap.get() == nullptr)
131
Calling 'unique_ptr::get'
142
Returning from 'unique_ptr::get'
143
Assuming the condition is false
144
Taking false branch
692 m_builtins_ap.reset(new Builtin::Context());
693 return m_builtins_ap.get();
145
Calling 'unique_ptr::get'
156
Returning from 'unique_ptr::get'
694}
695
696IdentifierTable *ClangASTContext::getIdentifierTable() {
697 if (m_identifier_table_ap.get() == nullptr)
75
Calling 'unique_ptr::get'
86
Returning from 'unique_ptr::get'
87
Assuming the condition is false
88
Taking false branch
698 m_identifier_table_ap.reset(
699 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
700 return m_identifier_table_ap.get();
89
Calling 'unique_ptr::get'
100
Returning from 'unique_ptr::get'
701}
702
703LangOptions *ClangASTContext::getLanguageOptions() {
704 if (m_language_options_ap.get() == nullptr) {
19
Calling 'unique_ptr::get'
30
Returning from 'unique_ptr::get'
31
Assuming the condition is false
32
Taking false branch
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();
33
Calling 'unique_ptr::get'
44
Returning from 'unique_ptr::get'
710}
711
712SelectorTable *ClangASTContext::getSelectorTable() {
713 if (m_selector_table_ap.get() == nullptr)
103
Calling 'unique_ptr::get'
114
Returning from 'unique_ptr::get'
115
Assuming the condition is false
116
Taking false branch
714 m_selector_table_ap.reset(new SelectorTable());
715 return m_selector_table_ap.get();
117
Calling 'unique_ptr::get'
128
Returning from 'unique_ptr::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)
47
Calling 'unique_ptr::get'
58
Returning from 'unique_ptr::get'
59
Assuming the condition is false
60
Taking false branch
728 m_source_manager_ap.reset(
729 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
730 return m_source_manager_ap.get();
61
Calling 'unique_ptr::get'
72
Returning from 'unique_ptr::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)
191
Calling 'unique_ptr::get'
202
Returning from 'unique_ptr::get'
203
Assuming the condition is false
204
Taking false branch
774 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
775
776 return m_diagnostic_consumer_ap.get();
205
Calling 'unique_ptr::get'
216
Returning from 'unique_ptr::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())
219
Calling 'unique_ptr::get'
230
Returning from 'unique_ptr::get'
231
Assuming the condition is false
791 m_target_info_ap.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
792 getTargetOptions()));
793 return m_target_info_ap.get();
232
Calling 'unique_ptr::get'
243
Returning from 'unique_ptr::get'
794}
795
796#pragma mark Basic Types
797
798static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
799 ASTContext *ast, QualType qual_type) {
800 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
801 if (qual_type_bit_size == bit_size)
802 return true;
803 return false;
804}
805
806CompilerType
807ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
808 size_t bit_size) {
809 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
810 getASTContext(), encoding, bit_size);
811}
812
813CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
814 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
815 if (!ast)
816 return CompilerType();
817 switch (encoding) {
818 case eEncodingInvalid:
819 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
820 return CompilerType(ast, ast->VoidPtrTy);
821 break;
822
823 case eEncodingUint:
824 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
825 return CompilerType(ast, ast->UnsignedCharTy);
826 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
827 return CompilerType(ast, ast->UnsignedShortTy);
828 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
829 return CompilerType(ast, ast->UnsignedIntTy);
830 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
831 return CompilerType(ast, ast->UnsignedLongTy);
832 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
833 return CompilerType(ast, ast->UnsignedLongLongTy);
834 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
835 return CompilerType(ast, ast->UnsignedInt128Ty);
836 break;
837
838 case eEncodingSint:
839 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
840 return CompilerType(ast, ast->SignedCharTy);
841 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
842 return CompilerType(ast, ast->ShortTy);
843 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
844 return CompilerType(ast, ast->IntTy);
845 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
846 return CompilerType(ast, ast->LongTy);
847 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
848 return CompilerType(ast, ast->LongLongTy);
849 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
850 return CompilerType(ast, ast->Int128Ty);
851 break;
852
853 case eEncodingIEEE754:
854 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
855 return CompilerType(ast, ast->FloatTy);
856 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
857 return CompilerType(ast, ast->DoubleTy);
858 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
859 return CompilerType(ast, ast->LongDoubleTy);
860 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
861 return CompilerType(ast, ast->HalfTy);
862 break;
863
864 case eEncodingVector:
865 // Sanity check that bit_size is a multiple of 8's.
866 if (bit_size && !(bit_size & 0x7u))
867 return CompilerType(
868 ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8));
869 break;
870 }
871
872 return CompilerType();
873}
874
875lldb::BasicType
876ClangASTContext::GetBasicTypeEnumeration(const ConstString &name) {
877 if (name) {
878 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
879 static TypeNameToBasicTypeMap g_type_map;
880 static llvm::once_flag g_once_flag;
881 llvm::call_once(g_once_flag, []() {
882 // "void"
883 g_type_map.Append(ConstString("void"), eBasicTypeVoid);
884
885 // "char"
886 g_type_map.Append(ConstString("char"), eBasicTypeChar);
887 g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar);
888 g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar);
889 g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar);
890 g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar);
891 g_type_map.Append(ConstString("unsigned wchar_t"),
892 eBasicTypeUnsignedWChar);
893 // "short"
894 g_type_map.Append(ConstString("short"), eBasicTypeShort);
895 g_type_map.Append(ConstString("short int"), eBasicTypeShort);
896 g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort);
897 g_type_map.Append(ConstString("unsigned short int"),
898 eBasicTypeUnsignedShort);
899
900 // "int"
901 g_type_map.Append(ConstString("int"), eBasicTypeInt);
902 g_type_map.Append(ConstString("signed int"), eBasicTypeInt);
903 g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt);
904 g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt);
905
906 // "long"
907 g_type_map.Append(ConstString("long"), eBasicTypeLong);
908 g_type_map.Append(ConstString("long int"), eBasicTypeLong);
909 g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong);
910 g_type_map.Append(ConstString("unsigned long int"),
911 eBasicTypeUnsignedLong);
912
913 // "long long"
914 g_type_map.Append(ConstString("long long"), eBasicTypeLongLong);
915 g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong);
916 g_type_map.Append(ConstString("unsigned long long"),
917 eBasicTypeUnsignedLongLong);
918 g_type_map.Append(ConstString("unsigned long long int"),
919 eBasicTypeUnsignedLongLong);
920
921 // "int128"
922 g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128);
923 g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128);
924
925 // Miscellaneous
926 g_type_map.Append(ConstString("bool"), eBasicTypeBool);
927 g_type_map.Append(ConstString("float"), eBasicTypeFloat);
928 g_type_map.Append(ConstString("double"), eBasicTypeDouble);
929 g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble);
930 g_type_map.Append(ConstString("id"), eBasicTypeObjCID);
931 g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel);
932 g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr);
933 g_type_map.Sort();
934 });
935
936 return g_type_map.Find(name, eBasicTypeInvalid);
937 }
938 return eBasicTypeInvalid;
939}
940
941CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
942 const ConstString &name) {
943 if (ast) {
944 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
945 return ClangASTContext::GetBasicType(ast, basic_type);
946 }
947 return CompilerType();
948}
949
950uint32_t ClangASTContext::GetPointerByteSize() {
951 if (m_pointer_byte_size == 0)
952 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid)
953 .GetPointerType()
954 .GetByteSize(nullptr);
955 return m_pointer_byte_size;
956}
957
958CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
959 return GetBasicType(getASTContext(), basic_type);
960}
961
962CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
963 lldb::BasicType basic_type) {
964 if (!ast)
965 return CompilerType();
966 lldb::opaque_compiler_type_t clang_type =
967 GetOpaqueCompilerType(ast, basic_type);
968
969 if (clang_type)
970 return CompilerType(GetASTContext(ast), clang_type);
971 return CompilerType();
972}
973
974CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
975 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
976 ASTContext *ast = getASTContext();
977
978#define streq(a, b)strcmp(a, b) == 0 strcmp(a, b) == 0
979 assert(ast != nullptr)(static_cast <bool> (ast != nullptr) ? void (0) : __assert_fail
("ast != nullptr", "/build/llvm-toolchain-snapshot-7~svn326551/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~svn326551/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
1485ClassTemplateSpecializationDecl *
1486ClangASTContext::CreateClassTemplateSpecializationDecl(
1487 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1488 const TemplateParameterInfos &template_param_infos) {
1489 ASTContext *ast = getASTContext();
1490 llvm::SmallVector<clang::TemplateArgument, 2> args(
1491 template_param_infos.args.size() +
1492 (template_param_infos.packed_args ? 1 : 0));
1493 std::copy(template_param_infos.args.begin(), template_param_infos.args.end(),
1494 args.begin());
1495 if (template_param_infos.packed_args) {
1496 args[args.size() - 1] = TemplateArgument::CreatePackCopy(
1497 *ast, template_param_infos.packed_args->args);
1498 }
1499 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1500 ClassTemplateSpecializationDecl::Create(
1501 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1502 SourceLocation(), class_template_decl, args,
1503 nullptr);
1504
1505 class_template_specialization_decl->setSpecializationKind(
1506 TSK_ExplicitSpecialization);
1507
1508 return class_template_specialization_decl;
1509}
1510
1511CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1512 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1513 if (class_template_specialization_decl) {
1514 ASTContext *ast = getASTContext();
1515 if (ast)
1516 return CompilerType(
1517 ast, ast->getTagDeclType(class_template_specialization_decl));
1518 }
1519 return CompilerType();
1520}
1521
1522static inline bool check_op_param(bool is_method,
1523 clang::OverloadedOperatorKind op_kind,
1524 bool unary, bool binary,
1525 uint32_t num_params) {
1526 // Special-case call since it can take any number of operands
1527 if (op_kind == OO_Call)
1528 return true;
1529
1530 // The parameter count doesn't include "this"
1531 if (is_method)
1532 ++num_params;
1533 if (num_params == 1)
1534 return unary;
1535 if (num_params == 2)
1536 return binary;
1537 else
1538 return false;
1539}
1540
1541bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1542 bool is_method, clang::OverloadedOperatorKind op_kind,
1543 uint32_t num_params) {
1544 switch (op_kind) {
1545 default:
1546 break;
1547 // C++ standard allows any number of arguments to new/delete
1548 case OO_New:
1549 case OO_Array_New:
1550 case OO_Delete:
1551 case OO_Array_Delete:
1552 return true;
1553 }
1554
1555#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1556 case OO_##Name: \
1557 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1558 switch (op_kind) {
1559#include "clang/Basic/OperatorKinds.def"
1560 default:
1561 break;
1562 }
1563 return false;
1564}
1565
1566clang::AccessSpecifier
1567ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1568 clang::AccessSpecifier rhs) {
1569 // Make the access equal to the stricter of the field and the nested field's
1570 // access
1571 if (lhs == AS_none || rhs == AS_none)
1572 return AS_none;
1573 if (lhs == AS_private || rhs == AS_private)
1574 return AS_private;
1575 if (lhs == AS_protected || rhs == AS_protected)
1576 return AS_protected;
1577 return AS_public;
1578}
1579
1580bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1581 uint32_t &bitfield_bit_size) {
1582 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
1583}
1584
1585bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field,
1586 uint32_t &bitfield_bit_size) {
1587 if (ast == nullptr || field == nullptr)
1588 return false;
1589
1590 if (field->isBitField()) {
1591 Expr *bit_width_expr = field->getBitWidth();
1592 if (bit_width_expr) {
1593 llvm::APSInt bit_width_apsint;
1594 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1595 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX(4294967295U));
1596 return true;
1597 }
1598 }
1599 }
1600 return false;
1601}
1602
1603bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1604 if (record_decl == nullptr)
1605 return false;
1606
1607 if (!record_decl->field_empty())
1608 return true;
1609
1610 // No fields, lets check this is a CXX record and check the base classes
1611 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1612 if (cxx_record_decl) {
1613 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1614 for (base_class = cxx_record_decl->bases_begin(),
1615 base_class_end = cxx_record_decl->bases_end();
1616 base_class != base_class_end; ++base_class) {
1617 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1618 base_class->getType()->getAs<RecordType>()->getDecl());
1619 if (RecordHasFields(base_class_decl))
1620 return true;
1621 }
1622 }
1623 return false;
1624}
1625
1626#pragma mark Objective C Classes
1627
1628CompilerType ClangASTContext::CreateObjCClass(const char *name,
1629 DeclContext *decl_ctx,
1630 bool isForwardDecl,
1631 bool isInternal,
1632 ClangASTMetadata *metadata) {
1633 ASTContext *ast = getASTContext();
1634 assert(ast != nullptr)(static_cast <bool> (ast != nullptr) ? void (0) : __assert_fail
("ast != nullptr", "/build/llvm-toolchain-snapshot-7~svn326551/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1634, __extension__ __PRETTY_FUNCTION__))
;
1635 assert(name && name[0])(static_cast <bool> (name && name[0]) ? void (0
) : __assert_fail ("name && name[0]", "/build/llvm-toolchain-snapshot-7~svn326551/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1635, __extension__ __PRETTY_FUNCTION__))
;
1636 if (decl_ctx == nullptr)
1637 decl_ctx = ast->getTranslationUnitDecl();
1638
1639 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1640 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1641 nullptr, SourceLocation(),
1642 /*isForwardDecl,*/
1643 isInternal);
1644
1645 if (decl && metadata)
1646 SetMetadata(ast, decl, *metadata);
1647
1648 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1649}
1650
1651static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
1652 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) ==
1653 false;
1654}
1655
1656uint32_t
1657ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1658 bool omit_empty_base_classes) {
1659 uint32_t num_bases = 0;
1660 if (cxx_record_decl) {
1661 if (omit_empty_base_classes) {
1662 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1663 for (base_class = cxx_record_decl->bases_begin(),
1664 base_class_end = cxx_record_decl->bases_end();
1665 base_class != base_class_end; ++base_class) {
1666 // Skip empty base classes
1667 if (omit_empty_base_classes) {
1668 if (BaseSpecifierIsEmpty(base_class))
1669 continue;
1670 }
1671 ++num_bases;
1672 }
1673 } else
1674 num_bases = cxx_record_decl->getNumBases();
1675 }
1676 return num_bases;
1677}
1678
1679#pragma mark Namespace Declarations
1680
1681NamespaceDecl *
1682ClangASTContext::GetUniqueNamespaceDeclaration(const char *name,
1683 DeclContext *decl_ctx) {
1684 NamespaceDecl *namespace_decl = nullptr;
1685 ASTContext *ast = getASTContext();
1686 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1687 if (decl_ctx == nullptr)
1688 decl_ctx = translation_unit_decl;
1689
1690 if (name) {
1691 IdentifierInfo &identifier_info = ast->Idents.get(name);
1692 DeclarationName decl_name(&identifier_info);
1693 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1694 for (NamedDecl *decl : result) {
1695 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1696 if (namespace_decl)
1697 return namespace_decl;
1698 }
1699
1700 namespace_decl =
1701 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1702 SourceLocation(), &identifier_info, nullptr);
1703
1704 decl_ctx->addDecl(namespace_decl);
1705 } else {
1706 if (decl_ctx == translation_unit_decl) {
1707 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1708 if (namespace_decl)
1709 return namespace_decl;
1710
1711 namespace_decl =
1712 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1713 SourceLocation(), nullptr, nullptr);
1714 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1715 translation_unit_decl->addDecl(namespace_decl);
1716 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~svn326551/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1716, __extension__ __PRETTY_FUNCTION__))
;
1717 } else {
1718 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1719 if (parent_namespace_decl) {
1720 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1721 if (namespace_decl)
1722 return namespace_decl;
1723 namespace_decl =
1724 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1725 SourceLocation(), nullptr, nullptr);
1726 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1727 parent_namespace_decl->addDecl(namespace_decl);
1728 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~svn326551/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1729, __extension__ __PRETTY_FUNCTION__))
1729 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~svn326551/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1729, __extension__ __PRETTY_FUNCTION__))
;
1730 } else {
1731 // BAD!!!
1732 }
1733 }
1734 }
1735#ifdef LLDB_CONFIGURATION_DEBUG
1736 VerifyDecl(namespace_decl);
1737#endif
1738 return namespace_decl;
1739}
1740
1741NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1742 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx) {
1743 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1744 if (ast_ctx == nullptr)
1745 return nullptr;
1746
1747 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx);
1748}
1749
1750clang::BlockDecl *
1751ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1752 if (ctx != nullptr) {
1753 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1754 clang::SourceLocation());
1755 ctx->addDecl(decl);
1756 return decl;
1757 }
1758 return nullptr;
1759}
1760
1761clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1762 clang::DeclContext *right,
1763 clang::DeclContext *root) {
1764 if (root == nullptr)
1765 return nullptr;
1766
1767 std::set<clang::DeclContext *> path_left;
1768 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1769 path_left.insert(d);
1770
1771 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1772 if (path_left.find(d) != path_left.end())
1773 return d;
1774
1775 return nullptr;
1776}
1777
1778clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
1779 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
1780 if (decl_ctx != nullptr && ns_decl != nullptr) {
1781 clang::TranslationUnitDecl *translation_unit =
1782 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
1783 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
1784 *getASTContext(), decl_ctx, clang::SourceLocation(),
1785 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
1786 clang::SourceLocation(), ns_decl,
1787 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
1788 decl_ctx->addDecl(using_decl);
1789 return using_decl;
1790 }
1791 return nullptr;
1792}
1793
1794clang::UsingDecl *
1795ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
1796 clang::NamedDecl *target) {
1797 if (current_decl_ctx != nullptr && target != nullptr) {
1798 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
1799 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
1800 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
1801 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
1802 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
1803 target);
1804 using_decl->addShadowDecl(shadow_decl);
1805 current_decl_ctx->addDecl(using_decl);
1806 return using_decl;
1807 }
1808 return nullptr;
1809}
1810
1811clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
1812 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
1813 if (decl_context != nullptr) {
1
Assuming the condition is true
2
Taking true branch
1814 clang::VarDecl *var_decl = clang::VarDecl::Create(
409
Forming reference to null pointer
1815 *getASTContext(), decl_context, clang::SourceLocation(),
3
Calling 'ClangASTContext::getASTContext'
405
Returning from 'ClangASTContext::getASTContext'
1816 clang::SourceLocation(),
1817 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
406
Assuming 'name' is non-null
407
Assuming the condition is true
408
'?' condition is true
1818 nullptr, clang::SC_None);
1819 var_decl->setAccess(clang::AS_public);
1820 decl_context->addDecl(var_decl);
1821 return var_decl;
1822 }
1823 return nullptr;
1824}
1825
1826lldb::opaque_compiler_type_t
1827ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
1828 lldb::BasicType basic_type) {
1829 switch (basic_type) {
1830 case eBasicTypeVoid:
1831 return ast->VoidTy.getAsOpaquePtr();
1832 case eBasicTypeChar:
1833 return ast->CharTy.getAsOpaquePtr();
1834 case eBasicTypeSignedChar:
1835 return ast->SignedCharTy.getAsOpaquePtr();
1836 case eBasicTypeUnsignedChar:
1837 return ast->UnsignedCharTy.getAsOpaquePtr();
1838 case eBasicTypeWChar:
1839 return ast->getWCharType().getAsOpaquePtr();
1840 case eBasicTypeSignedWChar:
1841 return ast->getSignedWCharType().getAsOpaquePtr();
1842 case eBasicTypeUnsignedWChar:
1843 return ast->getUnsignedWCharType().getAsOpaquePtr();
1844 case eBasicTypeChar16:
1845 return ast->Char16Ty.getAsOpaquePtr();
1846 case eBasicTypeChar32:
1847 return ast->Char32Ty.getAsOpaquePtr();
1848 case eBasicTypeShort:
1849 return ast->ShortTy.getAsOpaquePtr();
1850 case eBasicTypeUnsignedShort:
1851 return ast->UnsignedShortTy.getAsOpaquePtr();
1852 case eBasicTypeInt:
1853 return ast->IntTy.getAsOpaquePtr();
1854 case eBasicTypeUnsignedInt:
1855 return ast->UnsignedIntTy.getAsOpaquePtr();
1856 case eBasicTypeLong:
1857 return ast->LongTy.getAsOpaquePtr();
1858 case eBasicTypeUnsignedLong:
1859 return ast->UnsignedLongTy.getAsOpaquePtr();
1860 case eBasicTypeLongLong:
1861 return ast->LongLongTy.getAsOpaquePtr();
1862 case eBasicTypeUnsignedLongLong:
1863 return ast->UnsignedLongLongTy.getAsOpaquePtr();
1864 case eBasicTypeInt128:
1865 return ast->Int128Ty.getAsOpaquePtr();
1866 case eBasicTypeUnsignedInt128:
1867 return ast->UnsignedInt128Ty.getAsOpaquePtr();
1868 case eBasicTypeBool:
1869 return ast->BoolTy.getAsOpaquePtr();
1870 case eBasicTypeHalf:
1871 return ast->HalfTy.getAsOpaquePtr();
1872 case eBasicTypeFloat:
1873 return ast->FloatTy.getAsOpaquePtr();
1874 case eBasicTypeDouble:
1875 return ast->DoubleTy.getAsOpaquePtr();
1876 case eBasicTypeLongDouble:
1877 return ast->LongDoubleTy.getAsOpaquePtr();
1878 case eBasicTypeFloatComplex:
1879 return ast->FloatComplexTy.getAsOpaquePtr();
1880 case eBasicTypeDoubleComplex:
1881 return ast->DoubleComplexTy.getAsOpaquePtr();
1882 case eBasicTypeLongDoubleComplex:
1883 return ast->LongDoubleComplexTy.getAsOpaquePtr();
1884 case eBasicTypeObjCID:
1885 return ast->getObjCIdType().getAsOpaquePtr();
1886 case eBasicTypeObjCClass:
1887 return ast->getObjCClassType().getAsOpaquePtr();
1888 case eBasicTypeObjCSel:
1889 return ast->getObjCSelType().getAsOpaquePtr();
1890 case eBasicTypeNullPtr:
1891 return ast->NullPtrTy.getAsOpaquePtr();
1892 default:
1893 return nullptr;
1894 }
1895}
1896
1897#pragma mark Function Types
1898
1899clang::DeclarationName
1900ClangASTContext::GetDeclarationName(const char *name,
1901 const CompilerType &function_clang_type) {
1902 if (!name || !name[0])
1903 return clang::DeclarationName();
1904
1905 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
1906 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
1907 return DeclarationName(&getASTContext()->Idents.get(
1908 name)); // Not operator, but a regular function.
1909
1910 // Check the number of operator parameters. Sometimes we have
1911 // seen bad DWARF that doesn't correctly describe operators and
1912 // if we try to create a method and add it to the class, clang
1913 // will assert and crash, so we need to make sure things are
1914 // acceptable.
1915 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
1916 const clang::FunctionProtoType *function_type =
1917 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
1918 if (function_type == nullptr)
1919 return clang::DeclarationName();
1920
1921 const bool is_method = false;
1922 const unsigned int num_params = function_type->getNumParams();
1923 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1924 is_method, op_kind, num_params))
1925 return clang::DeclarationName();
1926
1927 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
1928}
1929
1930FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
1931 DeclContext *decl_ctx, const char *name,
1932 const CompilerType &function_clang_type, int storage, bool is_inline) {
1933 FunctionDecl *func_decl = nullptr;
1934 ASTContext *ast = getASTContext();
1935 if (decl_ctx == nullptr)
1936 decl_ctx = ast->getTranslationUnitDecl();
1937
1938 const bool hasWrittenPrototype = true;
1939 const bool isConstexprSpecified = false;
1940
1941 clang::DeclarationName declarationName =
1942 GetDeclarationName(name, function_clang_type);
1943 func_decl = FunctionDecl::Create(
1944 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
1945 ClangUtil::GetQualType(function_clang_type), nullptr,
1946 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
1947 isConstexprSpecified);
1948 if (func_decl)
1949 decl_ctx->addDecl(func_decl);
1950
1951#ifdef LLDB_CONFIGURATION_DEBUG
1952 VerifyDecl(func_decl);
1953#endif
1954
1955 return func_decl;
1956}
1957
1958CompilerType ClangASTContext::CreateFunctionType(
1959 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
1960 unsigned num_args, bool is_variadic, unsigned type_quals) {
1961 if (ast == nullptr)
1962 return CompilerType(); // invalid AST
1963
1964 if (!result_type || !ClangUtil::IsClangType(result_type))
1965 return CompilerType(); // invalid return type
1966
1967 std::vector<QualType> qual_type_args;
1968 if (num_args > 0 && args == nullptr)
1969 return CompilerType(); // invalid argument array passed in
1970
1971 // Verify that all arguments are valid and the right type
1972 for (unsigned i = 0; i < num_args; ++i) {
1973 if (args[i]) {
1974 // Make sure we have a clang type in args[i] and not a type from another
1975 // language whose name might match
1976 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
1977 lldbassert(is_clang_type)lldb_private::lldb_assert(is_clang_type, "is_clang_type", __FUNCTION__
, "/build/llvm-toolchain-snapshot-7~svn326551/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 1977)
;
1978 if (is_clang_type)
1979 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
1980 else
1981 return CompilerType(); // invalid argument type (must be a clang type)
1982 } else
1983 return CompilerType(); // invalid argument type (empty)
1984 }
1985
1986 // TODO: Detect calling convention in DWARF?
1987 FunctionProtoType::ExtProtoInfo proto_info;
1988 proto_info.Variadic = is_variadic;
1989 proto_info.ExceptionSpec = EST_None;
1990 proto_info.TypeQuals = type_quals;
1991 proto_info.RefQualifier = RQ_None;
1992
1993 return CompilerType(ast,
1994 ast->getFunctionType(ClangUtil::GetQualType(result_type),
1995 qual_type_args, proto_info));
1996}
1997
1998ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
1999 const char *name, const CompilerType &param_type, int storage) {
2000 ASTContext *ast = getASTContext();
2001 assert(ast != nullptr)(static_cast <bool> (ast != nullptr) ? void (0) : __assert_fail
("ast != nullptr", "/build/llvm-toolchain-snapshot-7~svn326551/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 2001, __extension__ __PRETTY_FUNCTION__))
;
2002 return ParmVarDecl::Create(*ast, ast->getTranslationUnitDecl(),
2003 SourceLocation(), SourceLocation(),
2004 name && name[0] ? &ast->Idents.get(name) : nullptr,
2005 ClangUtil::GetQualType(param_type), nullptr,
2006 (clang::StorageClass)storage, nullptr);
2007}
2008
2009void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2010 ParmVarDecl **params,
2011 unsigned num_params) {
2012 if (function_decl)
2013 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
2014}
2015
2016CompilerType
2017ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
2018 QualType block_type = m_ast_ap->getBlockPointerType(
2019 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
2020
2021 return CompilerType(this, block_type.getAsOpaquePtr());
2022}
2023
2024#pragma mark Array Types
2025
2026CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2027 size_t element_count,
2028 bool is_vector) {
2029 if (element_type.IsValid()) {
2030 ASTContext *ast = getASTContext();
2031 assert(ast != nullptr)(static_cast <bool> (ast != nullptr) ? void (0) : __assert_fail
("ast != nullptr", "/build/llvm-toolchain-snapshot-7~svn326551/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 2031, __extension__ __PRETTY_FUNCTION__))
;
2032
2033 if (is_vector) {
2034 return CompilerType(
2035 ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2036 element_count));
2037 } else {
2038
2039 llvm::APInt ap_element_count(64, element_count);
2040 if (element_count == 0) {
2041 return CompilerType(ast, ast->getIncompleteArrayType(
2042 ClangUtil::GetQualType(element_type),
2043 clang::ArrayType::Normal, 0));
2044 } else {
2045 return CompilerType(
2046 ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2047 ap_element_count,
2048 clang::ArrayType::Normal, 0));
2049 }
2050 }
2051 }
2052 return CompilerType();
2053}
2054
2055CompilerType ClangASTContext::CreateStructForIdentifier(
2056 const ConstString &type_name,
2057 const std::initializer_list<std::pair<const char *, CompilerType>>
2058 &type_fields,
2059 bool packed) {
2060 CompilerType type;
2061 if (!type_name.IsEmpty() &&
2062 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2063 .IsValid()) {
2064 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~svn326551/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 2064)
;
2065 return type;
2066 }
2067
2068 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2069 clang::TTK_Struct, lldb::eLanguageTypeC);
2070 StartTagDeclarationDefinition(type);
2071 for (const auto &field : type_fields)
2072 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2073 0);
2074 if (packed)
2075 SetIsPacked(type);
2076 CompleteTagDeclarationDefinition(type);
2077 return type;
2078}
2079
2080CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
2081 const ConstString &type_name,
2082 const std::initializer_list<std::pair<const char *, CompilerType>>
2083 &type_fields,
2084 bool packed) {
2085 CompilerType type;
2086 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2087 return type;
2088
2089 return CreateStructForIdentifier(type_name, type_fields, packed);
2090}
2091
2092#pragma mark Enumeration Types
2093
2094CompilerType
2095ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2096 const Declaration &decl,
2097 const CompilerType &integer_clang_type,
2098 bool is_scoped) {
2099 // TODO: Do something intelligent with the Declaration object passed in
2100 // like maybe filling in the SourceLocation with it...
2101 ASTContext *ast = getASTContext();
2102
2103 // TODO: ask about these...
2104 // const bool IsFixed = false;
2105
2106 EnumDecl *enum_decl = EnumDecl::Create(
2107 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2108 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
2109 is_scoped, // IsScoped
2110 is_scoped, // IsScopedUsingClassTag
2111 false); // IsFixed
2112
2113 if (enum_decl) {
2114 // TODO: check if we should be setting the promotion type too?
2115 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2116
2117 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2118
2119 return CompilerType(ast, ast->getTagDeclType(enum_decl));
2120 }
2121 return CompilerType();
2122}
2123
2124// Disable this for now since I can't seem to get a nicely formatted float
2125// out of the APFloat class without just getting the float, double or quad
2126// and then using a formatted print on it which defeats the purpose. We ideally
2127// would like to get perfect string values for any kind of float semantics
2128// so we can support remote targets. The code below also requires a patch to
2129// llvm::APInt.
2130// bool
2131// ClangASTContext::ConvertFloatValueToString (ASTContext *ast,
2132// lldb::opaque_compiler_type_t clang_type, const uint8_t* bytes, size_t
2133// byte_size, int apint_byte_order, std::string &float_str)
2134//{
2135// uint32_t count = 0;
2136// bool is_complex = false;
2137// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
2138// {
2139// unsigned num_bytes_per_float = byte_size / count;
2140// unsigned num_bits_per_float = num_bytes_per_float * 8;
2141//
2142// float_str.clear();
2143// uint32_t i;
2144// for (i=0; i<count; i++)
2145// {
2146// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float,
2147// (APInt::ByteOrder)apint_byte_order);
2148// bool is_ieee = false;
2149// APFloat ap_float(ap_int, is_ieee);
2150// char s[1024];
2151// unsigned int hex_digits = 0;
2152// bool upper_case = false;
2153//
2154// if (ap_float.convertToHexString(s, hex_digits, upper_case,
2155// APFloat::rmNearestTiesToEven) > 0)
2156// {
2157// if (i > 0)
2158// float_str.append(", ");
2159// float_str.append(s);
2160// if (i == 1 && is_complex)
2161// float_str.append(1, 'i');
2162// }
2163// }
2164// return !float_str.empty();
2165// }
2166// return false;
2167//}
2168
2169CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2170 size_t bit_size,
2171 bool is_signed) {
2172 if (ast) {
2173 if (is_signed) {
2174 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
2175 return CompilerType(ast, ast->SignedCharTy);
2176
2177 if (bit_size == ast->getTypeSize(ast->ShortTy))
2178 return CompilerType(ast, ast->ShortTy);
2179
2180 if (bit_size == ast->getTypeSize(ast->IntTy))
2181 return CompilerType(ast, ast->IntTy);
2182
2183 if (bit_size == ast->getTypeSize(ast->LongTy))
2184 return CompilerType(ast, ast->LongTy);
2185
2186 if (bit_size == ast->getTypeSize(ast->LongLongTy))
2187 return CompilerType(ast, ast->LongLongTy);
2188
2189 if (bit_size == ast->getTypeSize(ast->Int128Ty))
2190 return CompilerType(ast, ast->Int128Ty);
2191 } else {
2192 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
2193 return CompilerType(ast, ast->UnsignedCharTy);
2194
2195 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
2196 return CompilerType(ast, ast->UnsignedShortTy);
2197
2198 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
2199 return CompilerType(ast, ast->UnsignedIntTy);
2200
2201 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
2202 return CompilerType(ast, ast->UnsignedLongTy);
2203
2204 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
2205 return CompilerType(ast, ast->UnsignedLongLongTy);
2206
2207 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
2208 return CompilerType(ast, ast->UnsignedInt128Ty);
2209 }
2210 }
2211 return CompilerType();
2212}
2213
2214CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2215 bool is_signed) {
2216 if (ast)
2217 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2218 is_signed);
2219 return CompilerType();
2220}
2221
2222void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2223 if (decl_ctx) {
2224 DumpDeclContextHiearchy(decl_ctx->getParent());
2225
2226 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2227 if (named_decl) {
2228 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2229 named_decl->getDeclName().getAsString().c_str());
2230 } else {
2231 printf("%20s\n", decl_ctx->getDeclKindName());
2232 }
2233 }
2234}
2235
2236void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2237 if (decl == nullptr)
2238 return;
2239 DumpDeclContextHiearchy(decl->getDeclContext());
2240
2241 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2242 if (record_decl) {
2243 printf("%20s: %s%s\n", decl->getDeclKindName(),
2244 record_decl->getDeclName().getAsString().c_str(),
2245 record_decl->isInjectedClassName() ? " (injected class name)" : "");
2246
2247 } else {
2248 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2249 if (named_decl) {
2250 printf("%20s: %s\n", decl->getDeclKindName(),
2251 named_decl->getDeclName().getAsString().c_str());
2252 } else {
2253 printf("%20s\n", decl->getDeclKindName());
2254 }
2255 }
2256}
2257
2258bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2259 clang::Decl *rhs_decl) {
2260 if (lhs_decl && rhs_decl) {
2261 //----------------------------------------------------------------------
2262 // Make sure the decl kinds match first
2263 //----------------------------------------------------------------------
2264 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2265 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
2266
2267 if (lhs_decl_kind == rhs_decl_kind) {
2268 //------------------------------------------------------------------
2269 // Now check that the decl contexts kinds are all equivalent
2270 // before we have to check any names of the decl contexts...
2271 //------------------------------------------------------------------
2272 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2273 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2274 if (lhs_decl_ctx && rhs_decl_ctx) {
2275 while (1) {
2276 if (lhs_decl_ctx && rhs_decl_ctx) {
2277 const clang::Decl::Kind lhs_decl_ctx_kind =
2278 lhs_decl_ctx->getDeclKind();
2279 const clang::Decl::Kind rhs_decl_ctx_kind =
2280 rhs_decl_ctx->getDeclKind();
2281 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2282 lhs_decl_ctx = lhs_decl_ctx->getParent();
2283 rhs_decl_ctx = rhs_decl_ctx->getParent();
2284
2285 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2286 break;
2287 } else
2288 return false;
2289 } else
2290 return false;
2291 }
2292
2293 //--------------------------------------------------------------
2294 // Now make sure the name of the decls match
2295 //--------------------------------------------------------------
2296 clang::NamedDecl *lhs_named_decl =
2297 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2298 clang::NamedDecl *rhs_named_decl =
2299 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2300 if (lhs_named_decl && rhs_named_decl) {
2301 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2302 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2303 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2304 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2305 return false;
2306 } else
2307 return false;
2308 } else
2309 return false;
2310
2311 //--------------------------------------------------------------
2312 // We know that the decl context kinds all match, so now we need
2313 // to make sure the names match as well
2314 //--------------------------------------------------------------
2315 lhs_decl_ctx = lhs_decl->getDeclContext();
2316 rhs_decl_ctx = rhs_decl->getDeclContext();
2317 while (1) {
2318 switch (lhs_decl_ctx->getDeclKind()) {
2319 case clang::Decl::TranslationUnit:
2320 // We don't care about the translation unit names
2321 return true;
2322 default: {
2323 clang::NamedDecl *lhs_named_decl =
2324 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2325 clang::NamedDecl *rhs_named_decl =
2326 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2327 if (lhs_named_decl && rhs_named_decl) {
2328 clang::DeclarationName lhs_decl_name =
2329 lhs_named_decl->getDeclName();
2330 clang::DeclarationName rhs_decl_name =
2331 rhs_named_decl->getDeclName();
2332 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2333 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2334 return false;
2335 } else
2336 return false;
2337 } else
2338 return false;
2339 } break;
2340 }
2341 lhs_decl_ctx = lhs_decl_ctx->getParent();
2342 rhs_decl_ctx = rhs_decl_ctx->getParent();
2343 }
2344 }
2345 }
2346 }
2347 return false;
2348}
2349bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2350 clang::Decl *decl) {
2351 if (!decl)
2352 return false;
2353
2354 ExternalASTSource *ast_source = ast->getExternalSource();
2355
2356 if (!ast_source)
2357 return false;
2358
2359 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2360 if (tag_decl->isCompleteDefinition())
2361 return true;
2362
2363 if (!tag_decl->hasExternalLexicalStorage())
2364 return false;
2365
2366 ast_source->CompleteType(tag_decl);
2367
2368 return !tag_decl->getTypeForDecl()->isIncompleteType();
2369 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2370 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2371 if (objc_interface_decl->getDefinition())
2372 return true;
2373
2374 if (!objc_interface_decl->hasExternalLexicalStorage())
2375 return false;
2376
2377 ast_source->CompleteType(objc_interface_decl);
2378
2379 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2380 } else {
2381 return false;
2382 }
2383}
2384
2385void ClangASTContext::SetMetadataAsUserID(const void *object,
2386 user_id_t user_id) {
2387 ClangASTMetadata meta_data;
2388 meta_data.SetUserID(user_id);
2389 SetMetadata(object, meta_data);
2390}
2391
2392void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2393 ClangASTMetadata &metadata) {
2394 ClangExternalASTSourceCommon *external_source =
2395 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2396
2397 if (external_source)
2398 external_source->SetMetadata(object, metadata);
2399}
2400
2401ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2402 const void *object) {
2403 ClangExternalASTSourceCommon *external_source =
2404 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2405
2406 if (external_source && external_source->HasMetadata(object))
2407 return external_source->GetMetadata(object);
2408 else
2409 return nullptr;
2410}
2411
2412clang::DeclContext *
2413ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2414 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2415}
2416
2417clang::DeclContext *
2418ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2419 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2420}
2421
2422bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2423 int kind) const {
2424 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2425 if (clang_type) {
2426 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2427 if (tag_type) {
2428 clang::TagDecl *tag_decl =
2429 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2430 if (tag_decl) {
2431 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2432 return true;
2433 }
2434 }
2435 }
2436 return false;
2437}
2438
2439bool ClangASTContext::SetDefaultAccessForRecordFields(
2440 clang::RecordDecl *record_decl, int default_accessibility,
2441 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2442 if (record_decl) {
2443 uint32_t field_idx;
2444 clang::RecordDecl::field_iterator field, field_end;
2445 for (field = record_decl->field_begin(),
2446 field_end = record_decl->field_end(), field_idx = 0;
2447 field != field_end; ++field, ++field_idx) {
2448 // If no accessibility was assigned, assign the correct one
2449 if (field_idx < num_assigned_accessibilities &&
2450 assigned_accessibilities[field_idx] == clang::AS_none)
2451 field->setAccess((clang::AccessSpecifier)default_accessibility);
2452 }
2453 return true;
2454 }
2455 return false;
2456}
2457
2458clang::DeclContext *
2459ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2460 return GetDeclContextForType(ClangUtil::GetQualType(type));
2461}
2462
2463clang::DeclContext *
2464ClangASTContext::GetDeclContextForType(clang::QualType type) {
2465 if (type.isNull())
2466 return nullptr;
2467
2468 clang::QualType qual_type = type.getCanonicalType();
2469 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2470 switch (type_class) {
2471 case clang::Type::ObjCInterface:
2472 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2473 ->getInterface();
2474 case clang::Type::ObjCObjectPointer:
2475 return GetDeclContextForType(
2476 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2477 ->getPointeeType());
2478 case clang::Type::Record:
2479 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2480 case clang::Type::Enum:
2481 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2482 case clang::Type::Typedef:
2483 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2484 ->getDecl()
2485 ->getUnderlyingType());
2486 case clang::Type::Auto:
2487 return GetDeclContextForType(
2488 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2489 case clang::Type::Elaborated:
2490 return GetDeclContextForType(
2491 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2492 case clang::Type::Paren:
2493 return GetDeclContextForType(
2494 llvm::cast<clang::ParenType>(qual_type)->desugar());
2495 default:
2496 break;
2497 }
2498 // No DeclContext in this type...
2499 return nullptr;
2500}
2501
2502static bool GetCompleteQualType(clang::ASTContext *ast,
2503 clang::QualType qual_type,
2504 bool allow_completion = true) {
2505 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2506 switch (type_class) {
2507 case clang::Type::ConstantArray:
2508 case clang::Type::IncompleteArray:
2509 case clang::Type::VariableArray: {
2510 const clang::ArrayType *array_type =
2511 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2512
2513 if (array_type)
2514 return GetCompleteQualType(ast, array_type->getElementType(),
2515 allow_completion);
2516 } break;
2517 case clang::Type::Record: {
2518 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2519 if (cxx_record_decl) {
2520 if (cxx_record_decl->hasExternalLexicalStorage()) {
2521 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2522 const bool fields_loaded =
2523 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2524 if (is_complete && fields_loaded)
2525 return true;
2526
2527 if (!allow_completion)
2528 return false;
2529
2530 // Call the field_begin() accessor to for it to use the external source
2531 // to load the fields...
2532 clang::ExternalASTSource *external_ast_source =
2533 ast->getExternalSource();
2534 if (external_ast_source) {
2535 external_ast_source->CompleteType(cxx_record_decl);
2536 if (cxx_record_decl->isCompleteDefinition()) {
2537 cxx_record_decl->field_begin();
2538 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2539 }
2540 }
2541 }
2542 }
2543 const clang::TagType *tag_type =
2544 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2545 return !tag_type->isIncompleteType();
2546 } break;
2547
2548 case clang::Type::Enum: {
2549 const clang::TagType *tag_type =
2550 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2551 if (tag_type) {
2552 clang::TagDecl *tag_decl = tag_type->getDecl();
2553 if (tag_decl) {
2554 if (tag_decl->getDefinition())
2555 return true;
2556
2557 if (!allow_completion)
2558 return false;
2559
2560 if (tag_decl->hasExternalLexicalStorage()) {
2561 if (ast) {
2562 clang::ExternalASTSource *external_ast_source =
2563 ast->getExternalSource();
2564 if (external_ast_source) {
2565 external_ast_source->CompleteType(tag_decl);
2566 return !tag_type->isIncompleteType();
2567 }
2568 }
2569 }
2570 return false;
2571 }
2572 }
2573
2574 } break;
2575 case clang::Type::ObjCObject:
2576 case clang::Type::ObjCInterface: {
2577 const clang::ObjCObjectType *objc_class_type =
2578 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2579 if (objc_class_type) {
2580 clang::ObjCInterfaceDecl *class_interface_decl =
2581 objc_class_type->getInterface();
2582 // We currently can't complete objective C types through the newly added
2583 // ASTContext
2584 // because it only supports TagDecl objects right now...
2585 if (class_interface_decl) {
2586 if (class_interface_decl->getDefinition())
2587 return true;
2588
2589 if (!allow_completion)
2590 return false;
2591
2592 if (class_interface_decl->hasExternalLexicalStorage()) {
2593 if (ast) {
2594 clang::ExternalASTSource *external_ast_source =
2595 ast->getExternalSource();
2596 if (external_ast_source) {
2597 external_ast_source->CompleteType(class_interface_decl);
2598 return !objc_class_type->isIncompleteType();
2599 }
2600 }
2601 }
2602 return false;
2603 }
2604 }
2605 } break;
2606
2607 case clang::Type::Typedef:
2608 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2609 ->getDecl()
2610 ->getUnderlyingType(),
2611 allow_completion);
2612
2613 case clang::Type::Auto:
2614 return GetCompleteQualType(
2615 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2616 allow_completion);
2617
2618 case clang::Type::Elaborated:
2619 return GetCompleteQualType(
2620 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2621 allow_completion);
2622
2623 case clang::Type::Paren:
2624 return GetCompleteQualType(
2625 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2626 allow_completion);
2627
2628 case clang::Type::Attributed:
2629 return GetCompleteQualType(
2630 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2631 allow_completion);
2632
2633 default:
2634 break;
2635 }
2636
2637 return true;
2638}
2639
2640static clang::ObjCIvarDecl::AccessControl
2641ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2642 switch (access) {
2643 case eAccessNone:
2644 return clang::ObjCIvarDecl::None;
2645 case eAccessPublic:
2646 return clang::ObjCIvarDecl::Public;
2647 case eAccessPrivate:
2648 return clang::ObjCIvarDecl::Private;
2649 case eAccessProtected:
2650 return clang::ObjCIvarDecl::Protected;
2651 case eAccessPackage:
2652 return clang::ObjCIvarDecl::Package;
2653 }
2654 return clang::ObjCIvarDecl::None;
2655}
2656
2657//----------------------------------------------------------------------
2658// Tests
2659//----------------------------------------------------------------------
2660
2661bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2662 clang::QualType qual_type(GetCanonicalQualType(type));
2663
2664 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2665 switch (type_class) {
2666 case clang::Type::IncompleteArray:
2667 case clang::Type::VariableArray:
2668 case clang::Type::ConstantArray:
2669 case clang::Type::ExtVector:
2670 case clang::Type::Vector:
2671 case clang::Type::Record:
2672 case clang::Type::ObjCObject:
2673 case clang::Type::ObjCInterface:
2674 return true;
2675 case clang::Type::Auto:
2676 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2677 ->getDeducedType()
2678 .getAsOpaquePtr());
2679 case clang::Type::Elaborated:
2680 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2681 ->getNamedType()
2682 .getAsOpaquePtr());
2683 case clang::Type::Typedef:
2684 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2685 ->getDecl()
2686 ->getUnderlyingType()
2687 .getAsOpaquePtr());
2688 case clang::Type::Paren:
2689 return IsAggregateType(
2690 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2691 default:
2692 break;
2693 }
2694 // The clang type does have a value
2695 return false;
2696}
2697
2698bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2699 clang::QualType qual_type(GetCanonicalQualType(type));
2700
2701 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2702 switch (type_class) {
2703 case clang::Type::Record: {
2704 if (const clang::RecordType *record_type =
2705 llvm::dyn_cast_or_null<clang::RecordType>(
2706 qual_type.getTypePtrOrNull())) {
2707 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2708 return record_decl->isAnonymousStructOrUnion();
2709 }
2710 }
2711 break;
2712 }
2713 case clang::Type::Auto:
2714 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2715 ->getDeducedType()
2716 .getAsOpaquePtr());
2717 case clang::Type::Elaborated:
2718 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2719 ->getNamedType()
2720 .getAsOpaquePtr());
2721 case clang::Type::Typedef:
2722 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2723 ->getDecl()
2724 ->getUnderlyingType()
2725 .getAsOpaquePtr());
2726 case clang::Type::Paren:
2727 return IsAnonymousType(
2728 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2729 default:
2730 break;
2731 }
2732 // The clang type does have a value
2733 return false;
2734}
2735
2736bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2737 CompilerType *element_type_ptr,
2738 uint64_t *size, bool *is_incomplete) {
2739 clang::QualType qual_type(GetCanonicalQualType(type));
2740
2741 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2742 switch (type_class) {
2743 default:
2744 break;
2745
2746 case clang::Type::ConstantArray:
2747 if (element_type_ptr)
2748 element_type_ptr->SetCompilerType(
2749 getASTContext(),
2750 llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
2751 if (size)
2752 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2753 ->getSize()
2754 .getLimitedValue(ULLONG_MAX(9223372036854775807LL*2ULL+1ULL));
2755 if (is_incomplete)
2756 *is_incomplete = false;
2757 return true;
2758
2759 case clang::Type::IncompleteArray:
2760 if (element_type_ptr)
2761 element_type_ptr->SetCompilerType(
2762 getASTContext(),
2763 llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2764 if (size)
2765 *size = 0;
2766 if (is_incomplete)
2767 *is_incomplete = true;
2768 return true;
2769
2770 case clang::Type::VariableArray:
2771 if (element_type_ptr)
2772 element_type_ptr->SetCompilerType(
2773 getASTContext(),
2774 llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2775 if (size)
2776 *size = 0;
2777 if (is_incomplete)
2778 *is_incomplete = false;
2779 return true;
2780
2781 case clang::Type::DependentSizedArray:
2782 if (element_type_ptr)
2783 element_type_ptr->SetCompilerType(
2784 getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)
2785 ->getElementType());
2786 if (size)
2787 *size = 0;
2788 if (is_incomplete)
2789 *is_incomplete = false;
2790 return true;
2791
2792 case clang::Type::Typedef:
2793 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2794 ->getDecl()
2795 ->getUnderlyingType()
2796 .getAsOpaquePtr(),
2797 element_type_ptr, size, is_incomplete);
2798 case clang::Type::Auto:
2799 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2800 ->getDeducedType()
2801 .getAsOpaquePtr(),
2802 element_type_ptr, size, is_incomplete);
2803 case clang::Type::Elaborated:
2804 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2805 ->getNamedType()
2806 .getAsOpaquePtr(),
2807 element_type_ptr, size, is_incomplete);
2808 case clang::Type::Paren:
2809 return IsArrayType(
2810 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2811 element_type_ptr, size, is_incomplete);
2812 }
2813 if (element_type_ptr)
2814 element_type_ptr->Clear();
2815 if (size)
2816 *size = 0;
2817 if (is_incomplete)
2818 *is_incomplete = false;
2819 return false;
2820}
2821
2822bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
2823 CompilerType *element_type, uint64_t *size) {
2824 clang::QualType qual_type(GetCanonicalQualType(type));
2825
2826 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2827 switch (type_class) {
2828 case clang::Type::Vector: {
2829 const clang::VectorType *vector_type =
2830 qual_type->getAs<clang::VectorType>();
2831 if (vector_type) {
2832 if (size)
2833 *size = vector_type->getNumElements();
2834 if (element_type)
2835 *element_type =
2836 CompilerType(getASTContext(), vector_type->getElementType());
2837 }
2838 return true;
2839 } break;
2840 case clang::Type::ExtVector: {
2841 const clang::ExtVectorType *ext_vector_type =
2842 qual_type->getAs<clang::ExtVectorType>();
2843 if (ext_vector_type) {
2844 if (size)
2845 *size = ext_vector_type->getNumElements();
2846 if (element_type)
2847 *element_type =
2848 CompilerType(getASTContext(), ext_vector_type->getElementType());
2849 }
2850 return true;
2851 }
2852 default:
2853 break;
2854 }
2855 return false;
2856}
2857
2858bool ClangASTContext::IsRuntimeGeneratedType(
2859 lldb::opaque_compiler_type_t type) {
2860 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
2861 ->GetDeclContextForType(GetQualType(type));
2862 if (!decl_ctx)
2863 return false;
2864
2865 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2866 return false;
2867
2868 clang::ObjCInterfaceDecl *result_iface_decl =
2869 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2870
2871 ClangASTMetadata *ast_metadata =
2872 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2873 if (!ast_metadata)
2874 return false;
2875 return (ast_metadata->GetISAPtr() != 0);
2876}
2877
2878bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
2879 return GetQualType(type).getUnqualifiedType()->isCharType();
2880}
2881
2882bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
2883 const bool allow_completion = false;
2884 return GetCompleteQualType(getASTContext(), GetQualType(type),
2885 allow_completion);
2886}
2887
2888bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
2889 return GetQualType(type).isConstQualified();
2890}
2891
2892bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
2893 uint32_t &length) {
2894 CompilerType pointee_or_element_clang_type;
2895 length = 0;
2896 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
2897
2898 if (!pointee_or_element_clang_type.IsValid())
2899 return false;
2900
2901 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
2902 if (pointee_or_element_clang_type.IsCharType()) {
2903 if (type_flags.Test(eTypeIsArray)) {
2904 // We know the size of the array and it could be a C string
2905 // since it is an array of characters
2906 length = llvm::cast<clang::ConstantArrayType>(
2907 GetCanonicalQualType(type).getTypePtr())
2908 ->getSize()
2909 .getLimitedValue();
2910 }
2911 return true;
2912 }
2913 }
2914 return false;
2915}
2916
2917bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
2918 bool *is_variadic_ptr) {
2919 if (type) {
2920 clang::QualType qual_type(GetCanonicalQualType(type));
2921
2922 if (qual_type->isFunctionType()) {
2923 if (is_variadic_ptr) {
2924 const clang::FunctionProtoType *function_proto_type =
2925 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2926 if (function_proto_type)
2927 *is_variadic_ptr = function_proto_type->isVariadic();
2928 else
2929 *is_variadic_ptr = false;
2930 }
2931 return true;
2932 }
2933
2934 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2935 switch (type_class) {
2936 default:
2937 break;
2938 case clang::Type::Typedef:
2939 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
2940 ->getDecl()
2941 ->getUnderlyingType()
2942 .getAsOpaquePtr(),
2943 nullptr);
2944 case clang::Type::Auto:
2945 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
2946 ->getDeducedType()
2947 .getAsOpaquePtr(),
2948 nullptr);
2949 case clang::Type::Elaborated:
2950 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
2951 ->getNamedType()
2952 .getAsOpaquePtr(),
2953 nullptr);
2954 case clang::Type::Paren:
2955 return IsFunctionType(
2956 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2957 nullptr);
2958 case clang::Type::LValueReference:
2959 case clang::Type::RValueReference: {
2960 const clang::ReferenceType *reference_type =
2961 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
2962 if (reference_type)
2963 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
2964 nullptr);
2965 } break;
2966 }
2967 }
2968 return false;
2969}
2970
2971// Used to detect "Homogeneous Floating-point Aggregates"
2972uint32_t
2973ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
2974 CompilerType *base_type_ptr) {
2975 if (!type)
2976 return 0;
2977
2978 clang::QualType qual_type(GetCanonicalQualType(type));
2979 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2980 switch (type_class) {
2981 case clang::Type::Record:
2982 if (GetCompleteType(type)) {
2983 const clang::CXXRecordDecl *cxx_record_decl =
2984 qual_type->getAsCXXRecordDecl();
2985 if (cxx_record_decl) {
2986 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
2987 return 0;
2988 }
2989 const clang::RecordType *record_type =
2990 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
2991 if (record_type) {
2992 const clang::RecordDecl *record_decl = record_type->getDecl();
2993 if (record_decl) {
2994 // We are looking for a structure that contains only floating point
2995 // types
2996 clang::RecordDecl::field_iterator field_pos,
2997 field_end = record_decl->field_end();
2998 uint32_t num_fields = 0;
2999 bool is_hva = false;
3000 bool is_hfa = false;
3001 clang::QualType base_qual_type;
3002 uint64_t base_bitwidth = 0;
3003 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3004 ++field_pos) {
3005 clang::QualType field_qual_type = field_pos->getType();
3006 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
3007 if (field_qual_type->isFloatingType()) {
3008 if (field_qual_type->isComplexType())
3009 return 0;
3010 else {
3011 if (num_fields == 0)
3012 base_qual_type = field_qual_type;
3013 else {
3014 if (is_hva)
3015 return 0;
3016 is_hfa = true;
3017 if (field_qual_type.getTypePtr() !=
3018 base_qual_type.getTypePtr())
3019 return 0;
3020 }
3021 }
3022 } else if (field_qual_type->isVectorType() ||
3023 field_qual_type->isExtVectorType()) {
3024 if (num_fields == 0) {
3025 base_qual_type = field_qual_type;
3026 base_bitwidth = field_bitwidth;
3027 } else {
3028 if (is_hfa)
3029 return 0;
3030 is_hva = true;
3031 if (base_bitwidth != field_bitwidth)
3032 return 0;
3033 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3034 return 0;
3035 }
3036 } else
3037 return 0;
3038 ++num_fields;
3039 }
3040 if (base_type_ptr)
3041 *base_type_ptr = CompilerType(getASTContext(), base_qual_type);
3042 return num_fields;
3043 }
3044 }
3045 }
3046 break;
3047
3048 case clang::Type::Typedef:
3049 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3050 ->getDecl()
3051 ->getUnderlyingType()
3052 .getAsOpaquePtr(),
3053 base_type_ptr);
3054
3055 case clang::Type::Auto:
3056 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3057 ->getDeducedType()
3058 .getAsOpaquePtr(),
3059 base_type_ptr);
3060
3061 case clang::Type::Elaborated:
3062 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3063 ->getNamedType()
3064 .getAsOpaquePtr(),
3065 base_type_ptr);
3066 default:
3067 break;
3068 }
3069 return 0;
3070}
3071
3072size_t ClangASTContext::GetNumberOfFunctionArguments(
3073 lldb::opaque_compiler_type_t type) {
3074 if (type) {
3075 clang::QualType qual_type(GetCanonicalQualType(type));
3076 const clang::FunctionProtoType *func =
3077 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3078 if (func)
3079 return func->getNumParams();
3080 }
3081 return 0;
3082}
3083
3084CompilerType
3085ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3086 const size_t index) {
3087 if (type) {
3088 clang::QualType qual_type(GetQualType(type));
3089 const clang::FunctionProtoType *func =
3090 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3091 if (func) {
3092 if (index < func->getNumParams())
3093 return CompilerType(getASTContext(), func->getParamType(index));
3094 }
3095 }
3096 return CompilerType();
3097}
3098
3099bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3100 if (type) {
3101 clang::QualType qual_type(GetCanonicalQualType(type));
3102
3103 if (qual_type->isFunctionPointerType())
3104 return true;
3105
3106 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3107 switch (type_class) {
3108 default:
3109 break;
3110 case clang::Type::Typedef:
3111 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3112 ->getDecl()
3113 ->getUnderlyingType()
3114 .getAsOpaquePtr());
3115 case clang::Type::Auto:
3116 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3117 ->getDeducedType()
3118 .getAsOpaquePtr());
3119 case clang::Type::Elaborated:
3120 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3121 ->getNamedType()
3122 .getAsOpaquePtr());
3123 case clang::Type::Paren:
3124 return IsFunctionPointerType(
3125 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3126
3127 case clang::Type::LValueReference:
3128 case clang::Type::RValueReference: {
3129 const clang::ReferenceType *reference_type =
3130 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3131 if (reference_type)
3132 return IsFunctionPointerType(
3133 reference_type->getPointeeType().getAsOpaquePtr());
3134 } break;
3135 }
3136 }
3137 return false;
3138}
3139
3140bool ClangASTContext::IsBlockPointerType(
3141 lldb::opaque_compiler_type_t type,
3142 CompilerType *function_pointer_type_ptr) {
3143 if (type) {
3144 clang::QualType qual_type(GetCanonicalQualType(type));
3145
3146 if (qual_type->isBlockPointerType()) {
3147 if (function_pointer_type_ptr) {
3148 const clang::BlockPointerType *block_pointer_type =
3149 qual_type->getAs<clang::BlockPointerType>();
3150 QualType pointee_type = block_pointer_type->getPointeeType();
3151 QualType function_pointer_type = m_ast_ap->getPointerType(pointee_type);
3152 *function_pointer_type_ptr =
3153 CompilerType(getASTContext(), function_pointer_type);
3154 }
3155 return true;
3156 }
3157
3158 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3159 switch (type_class) {
3160 default:
3161 break;
3162 case clang::Type::Typedef:
3163 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3164 ->getDecl()
3165 ->getUnderlyingType()
3166 .getAsOpaquePtr(),
3167 function_pointer_type_ptr);
3168 case clang::Type::Auto:
3169 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3170 ->getDeducedType()
3171 .getAsOpaquePtr(),
3172 function_pointer_type_ptr);
3173 case clang::Type::Elaborated:
3174 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3175 ->getNamedType()
3176 .getAsOpaquePtr(),
3177 function_pointer_type_ptr);
3178 case clang::Type::Paren:
3179 return IsBlockPointerType(
3180 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3181 function_pointer_type_ptr);
3182
3183 case clang::Type::LValueReference:
3184 case clang::Type::RValueReference: {
3185 const clang::ReferenceType *reference_type =
3186 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3187 if (reference_type)
3188 return IsBlockPointerType(
3189 reference_type->getPointeeType().getAsOpaquePtr(),
3190 function_pointer_type_ptr);
3191 } break;
3192 }
3193 }
3194 return false;
3195}
3196
3197bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3198 bool &is_signed) {
3199 if (!type)
3200 return false;
3201
3202 clang::QualType qual_type(GetCanonicalQualType(type));
3203 const clang::BuiltinType *builtin_type =
3204 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3205
3206 if (builtin_type) {
3207 if (builtin_type->isInteger()) {
3208 is_signed = builtin_type->isSignedInteger();
3209 return true;
3210 }
3211 }
3212
3213 return false;
3214}
3215
3216bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3217 bool &is_signed) {
3218 if (type) {
3219 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3220 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3221
3222 if (enum_type) {
3223 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3224 is_signed);
3225 return true;
3226 }
3227 }
3228
3229 return false;
3230}
3231
3232bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3233 CompilerType *pointee_type) {
3234 if (type) {
3235 clang::QualType qual_type(GetCanonicalQualType(type));
3236 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3237 switch (type_class) {
3238 case clang::Type::Builtin:
3239 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3240 default:
3241 break;
3242 case clang::BuiltinType::ObjCId:
3243 case clang::BuiltinType::ObjCClass:
3244 return true;
3245 }
3246 return false;
3247 case clang::Type::ObjCObjectPointer:
3248 if (pointee_type)
3249 pointee_type->SetCompilerType(
3250 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3251 ->getPointeeType());
3252 return true;
3253 case clang::Type::BlockPointer:
3254 if (pointee_type)
3255 pointee_type->SetCompilerType(
3256 getASTContext(),
3257 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3258 return true;
3259 case clang::Type::Pointer:
3260 if (pointee_type)
3261 pointee_type->SetCompilerType(
3262 getASTContext(),
3263 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3264 return true;
3265 case clang::Type::MemberPointer:
3266 if (pointee_type)
3267 pointee_type->SetCompilerType(
3268 getASTContext(),
3269 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3270 return true;
3271 case clang::Type::Typedef:
3272 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3273 ->getDecl()
3274 ->getUnderlyingType()
3275 .getAsOpaquePtr(),
3276 pointee_type);
3277 case clang::Type::Auto:
3278 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3279 ->getDeducedType()
3280 .getAsOpaquePtr(),
3281 pointee_type);
3282 case clang::Type::Elaborated:
3283 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3284 ->getNamedType()
3285 .getAsOpaquePtr(),
3286 pointee_type);
3287 case clang::Type::Paren:
3288 return IsPointerType(
3289 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3290 pointee_type);
3291 default:
3292 break;
3293 }
3294 }
3295 if (pointee_type)
3296 pointee_type->Clear();
3297 return false;
3298}
3299
3300bool ClangASTContext::IsPointerOrReferenceType(
3301 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3302 if (type) {
3303 clang::QualType qual_type(GetCanonicalQualType(type));
3304 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3305 switch (type_class) {
3306 case clang::Type::Builtin:
3307 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3308 default:
3309 break;
3310 case clang::BuiltinType::ObjCId:
3311 case clang::BuiltinType::ObjCClass:
3312 return true;
3313 }
3314 return false;
3315 case clang::Type::ObjCObjectPointer:
3316 if (pointee_type)
3317 pointee_type->SetCompilerType(
3318 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3319 ->getPointeeType());
3320 return true;
3321 case clang::Type::BlockPointer:
3322 if (pointee_type)
3323 pointee_type->SetCompilerType(
3324 getASTContext(),
3325 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3326 return true;
3327 case clang::Type::Pointer:
3328 if (pointee_type)
3329 pointee_type->SetCompilerType(
3330 getASTContext(),
3331 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3332 return true;
3333 case clang::Type::MemberPointer:
3334 if (pointee_type)
3335 pointee_type->SetCompilerType(
3336 getASTContext(),
3337 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3338 return true;
3339 case clang::Type::LValueReference:
3340 if (pointee_type)
3341 pointee_type->SetCompilerType(
3342 getASTContext(),
3343 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3344 return true;
3345 case clang::Type::RValueReference:
3346 if (pointee_type)
3347 pointee_type->SetCompilerType(
3348 getASTContext(),
3349 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3350 return true;
3351 case clang::Type::Typedef:
3352 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3353 ->getDecl()
3354 ->getUnderlyingType()
3355 .getAsOpaquePtr(),
3356 pointee_type);
3357 case clang::Type::Auto:
3358 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3359 ->getDeducedType()
3360 .getAsOpaquePtr(),
3361 pointee_type);
3362 case clang::Type::Elaborated:
3363 return IsPointerOrReferenceType(
3364 llvm::cast<clang::ElaboratedType>(qual_type)
3365 ->getNamedType()
3366 .getAsOpaquePtr(),
3367 pointee_type);
3368 case clang::Type::Paren:
3369 return IsPointerOrReferenceType(
3370 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3371 pointee_type);
3372 default:
3373 break;
3374 }
3375 }
3376 if (pointee_type)
3377 pointee_type->Clear();
3378 return false;
3379}
3380
3381bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3382 CompilerType *pointee_type,
3383 bool *is_rvalue) {
3384 if (type) {
3385 clang::QualType qual_type(GetCanonicalQualType(type));
3386 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3387
3388 switch (type_class) {
3389 case clang::Type::LValueReference:
3390 if (pointee_type)
3391 pointee_type->SetCompilerType(
3392 getASTContext(),
3393 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3394 if (is_rvalue)
3395 *is_rvalue = false;
3396 return true;
3397 case clang::Type::RValueReference:
3398 if (pointee_type)
3399 pointee_type->SetCompilerType(
3400 getASTContext(),
3401 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3402 if (is_rvalue)
3403 *is_rvalue = true;
3404 return true;
3405 case clang::Type::Typedef:
3406 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3407 ->getDecl()
3408 ->getUnderlyingType()
3409 .getAsOpaquePtr(),
3410 pointee_type, is_rvalue);
3411 case clang::Type::Auto:
3412 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3413 ->getDeducedType()
3414 .getAsOpaquePtr(),
3415 pointee_type, is_rvalue);
3416 case clang::Type::Elaborated:
3417 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3418 ->getNamedType()
3419 .getAsOpaquePtr(),
3420 pointee_type, is_rvalue);
3421 case clang::Type::Paren:
3422 return IsReferenceType(
3423 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3424 pointee_type, is_rvalue);
3425
3426 default:
3427 break;
3428 }
3429 }
3430 if (pointee_type)
3431 pointee_type->Clear();
3432 return false;
3433}
3434
3435bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3436 uint32_t &count, bool &is_complex) {
3437 if (type) {
3438 clang::QualType qual_type(GetCanonicalQualType(type));
3439
3440 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3441 qual_type->getCanonicalTypeInternal())) {
3442 clang::BuiltinType::Kind kind = BT->getKind();
3443 if (kind >= clang::BuiltinType::Float &&
3444 kind <= clang::BuiltinType::LongDouble) {
3445 count = 1;
3446 is_complex = false;
3447 return true;
3448 }
3449 } else if (const clang::ComplexType *CT =
3450 llvm::dyn_cast<clang::ComplexType>(
3451 qual_type->getCanonicalTypeInternal())) {
3452 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3453 is_complex)) {
3454 count = 2;
3455 is_complex = true;
3456 return true;
3457 }
3458 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3459 qual_type->getCanonicalTypeInternal())) {
3460 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3461 is_complex)) {
3462 count = VT->getNumElements();
3463 is_complex = false;
3464 return true;
3465 }
3466 }
3467 }
3468 count = 0;
3469 is_complex = false;
3470 return false;
3471}
3472
3473bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3474 if (!type)
3475 return false;
3476
3477 clang::QualType qual_type(GetQualType(type));
3478 const clang::TagType *tag_type =
3479 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3480 if (tag_type) {
3481 clang::TagDecl *tag_decl = tag_type->getDecl();
3482 if (tag_decl)
3483 return tag_decl->isCompleteDefinition();
3484 return false;
3485 } else {
3486 const clang::ObjCObjectType *objc_class_type =
3487 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3488 if (objc_class_type) {
3489 clang::ObjCInterfaceDecl *class_interface_decl =
3490 objc_class_type->getInterface();
3491 if (class_interface_decl)
3492 return class_interface_decl->getDefinition() != nullptr;
3493 return false;
3494 }
3495 }
3496 return true;
3497}
3498
3499bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
3500 if (type) {
3501 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3502
3503 const clang::ObjCObjectPointerType *obj_pointer_type =
3504 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3505
3506 if (obj_pointer_type)
3507 return obj_pointer_type->isObjCClassType();
3508 }
3509 return false;
3510}
3511
3512bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3513 if (ClangUtil::IsClangType(type))
3514 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3515 return false;
3516}
3517
3518bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3519 if (!type)
3520 return false;
3521 clang::QualType qual_type(GetCanonicalQualType(type));
3522 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3523 return (type_class == clang::Type::Record);
3524}
3525
3526bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3527 if (!type)
3528 return false;
3529 clang::QualType qual_type(GetCanonicalQualType(type));
3530 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3531 return (type_class == clang::Type::Enum);
3532}
3533
3534bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3535 if (type) {
3536 clang::QualType qual_type(GetCanonicalQualType(type));
3537 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3538 switch (type_class) {
3539 case clang::Type::Record:
3540 if (GetCompleteType(type)) {
3541 const clang::RecordType *record_type =
3542 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3543 const clang::RecordDecl *record_decl = record_type->getDecl();
3544 if (record_decl) {
3545 const clang::CXXRecordDecl *cxx_record_decl =
3546 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3547 if (cxx_record_decl)
3548 return cxx_record_decl->isPolymorphic();
3549 }
3550 }
3551 break;
3552
3553 default:
3554 break;
3555 }
3556 }
3557 return false;
3558}
3559
3560bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3561 CompilerType *dynamic_pointee_type,
3562 bool check_cplusplus,
3563 bool check_objc) {
3564 clang::QualType pointee_qual_type;
3565 if (type) {
3566 clang::QualType qual_type(GetCanonicalQualType(type));
3567 bool success = false;
3568 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3569 switch (type_class) {
3570 case clang::Type::Builtin:
3571 if (check_objc &&
3572 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3573 clang::BuiltinType::ObjCId) {
3574 if (dynamic_pointee_type)
3575 dynamic_pointee_type->SetCompilerType(this, type);
3576 return true;
3577 }
3578 break;
3579
3580 case clang::Type::ObjCObjectPointer:
3581 if (check_objc) {
3582 if (auto objc_pointee_type =
3583 qual_type->getPointeeType().getTypePtrOrNull()) {
3584 if (auto objc_object_type =
3585 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3586 objc_pointee_type)) {
3587 if (objc_object_type->isObjCClass())
3588 return false;
3589 }
3590 }
3591 if (dynamic_pointee_type)
3592 dynamic_pointee_type->SetCompilerType(
3593 getASTContext(),
3594 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3595 ->getPointeeType());
3596 return true;
3597 }
3598 break;
3599
3600 case clang::Type::Pointer:
3601 pointee_qual_type =
3602 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3603 success = true;
3604 break;
3605
3606 case clang::Type::LValueReference:
3607 case clang::Type::RValueReference:
3608 pointee_qual_type =
3609 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3610 success = true;
3611 break;
3612
3613 case clang::Type::Typedef:
3614 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3615 ->getDecl()
3616 ->getUnderlyingType()
3617 .getAsOpaquePtr(),
3618 dynamic_pointee_type, check_cplusplus,
3619 check_objc);
3620
3621 case clang::Type::Auto:
3622 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3623 ->getDeducedType()
3624 .getAsOpaquePtr(),
3625 dynamic_pointee_type, check_cplusplus,
3626 check_objc);
3627
3628 case clang::Type::Elaborated:
3629 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3630 ->getNamedType()
3631 .getAsOpaquePtr(),
3632 dynamic_pointee_type, check_cplusplus,
3633 check_objc);
3634
3635 case clang::Type::Paren:
3636 return IsPossibleDynamicType(
3637 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3638 dynamic_pointee_type, check_cplusplus, check_objc);
3639 default:
3640 break;
3641 }
3642
3643 if (success) {
3644 // Check to make sure what we are pointing too is a possible dynamic C++
3645 // type
3646 // We currently accept any "void *" (in case we have a class that has been
3647 // watered down to an opaque pointer) and virtual C++ classes.
3648 const clang::Type::TypeClass pointee_type_class =
3649 pointee_qual_type.getCanonicalType()->getTypeClass();
3650 switch (pointee_type_class) {
3651 case clang::Type::Builtin:
3652 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3653 case clang::BuiltinType::UnknownAny:
3654 case clang::BuiltinType::Void:
3655 if (dynamic_pointee_type)
3656 dynamic_pointee_type->SetCompilerType(getASTContext(),
3657 pointee_qual_type);
3658 return true;
3659 default:
3660 break;
3661 }
3662 break;
3663
3664 case clang::Type::Record:
3665 if (check_cplusplus) {
3666 clang::CXXRecordDecl *cxx_record_decl =
3667 pointee_qual_type->getAsCXXRecordDecl();
3668 if (cxx_record_decl) {
3669 bool is_complete = cxx_record_decl->isCompleteDefinition();
3670
3671 if (is_complete)
3672 success = cxx_record_decl->isDynamicClass();
3673 else {
3674 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3675 getASTContext(), cxx_record_decl);
3676 if (metadata)
3677 success = metadata->GetIsDynamicCXXType();
3678 else {
3679 is_complete = CompilerType(getASTContext(), pointee_qual_type)
3680 .GetCompleteType();
3681 if (is_complete)
3682 success = cxx_record_decl->isDynamicClass();
3683 else
3684 success = false;
3685 }
3686 }
3687
3688 if (success) {
3689 if (dynamic_pointee_type)
3690 dynamic_pointee_type->SetCompilerType(getASTContext(),
3691 pointee_qual_type);
3692 return true;
3693 }
3694 }
3695 }
3696 break;
3697
3698 case clang::Type::ObjCObject:
3699 case clang::Type::ObjCInterface:
3700 if (check_objc) {
3701 if (dynamic_pointee_type)
3702 dynamic_pointee_type->SetCompilerType(getASTContext(),
3703 pointee_qual_type);
3704 return true;
3705 }
3706 break;
3707
3708 default:
3709 break;
3710 }
3711 }
3712 }
3713 if (dynamic_pointee_type)
3714 dynamic_pointee_type->Clear();
3715 return false;
3716}
3717
3718bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3719 if (!type)
3720 return false;
3721
3722 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3723}
3724
3725bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3726 if (!type)
3727 return false;
3728 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3729}
3730
3731bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3732 if (!type)
3733 return false;
3734 return GetCanonicalQualType(type)->isVoidType();
3735}
3736
3737bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3738 return ClangASTContextSupportsLanguage(language);
3739}
3740
3741bool ClangASTContext::GetCXXClassName(const CompilerType &type,
3742 std::string &class_name) {
3743 if (type) {
3744 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3745 if (!qual_type.isNull()) {
3746 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3747 if (cxx_record_decl) {
3748 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3749 return true;
3750 }
3751 }
3752 }
3753 class_name.clear();
3754 return false;
3755}
3756
3757bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3758 if (!type)
3759 return false;
3760
3761 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3762 if (!qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr)
3763 return true;
3764 return false;
3765}
3766
3767bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3768 if (!type)
3769 return false;
3770 clang::QualType qual_type(GetCanonicalQualType(type));
3771 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3772 if (tag_type)
3773 return tag_type->isBeingDefined();
3774 return false;
3775}
3776
3777bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3778 CompilerType *class_type_ptr) {
3779 if (!type)
3780 return false;
3781
3782 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3783
3784 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3785 if (class_type_ptr) {
3786 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3787 const clang::ObjCObjectPointerType *obj_pointer_type =
3788 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3789 if (obj_pointer_type == nullptr)
3790 class_type_ptr->Clear();
3791 else
3792 class_type_ptr->SetCompilerType(
3793 type.GetTypeSystem(),
3794 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3795 .getAsOpaquePtr());
3796 }
3797 }
3798 return true;
3799 }
3800 if (class_type_ptr)
3801 class_type_ptr->Clear();
3802 return false;
3803}
3804
3805bool ClangASTContext::GetObjCClassName(const CompilerType &type,
3806 std::string &class_name) {
3807 if (!type)
3808 return false;
3809
3810 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3811
3812 const clang::ObjCObjectType *object_type =
3813 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3814 if (object_type) {
3815 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3816 if (interface) {
3817 class_name = interface->getNameAsString();
3818 return true;
3819 }
3820 }
3821 return false;
3822}
3823
3824//----------------------------------------------------------------------
3825// Type Completion
3826//----------------------------------------------------------------------
3827
3828bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
3829 if (!type)
3830 return false;
3831 const bool allow_completion = true;
3832 return GetCompleteQualType(getASTContext(), GetQualType(type),
3833 allow_completion);
3834}
3835
3836ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
3837 std::string type_name;
3838 if (type) {
3839 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
3840 clang::QualType qual_type(GetQualType(type));
3841 printing_policy.SuppressTagKeyword = true;
3842 const clang::TypedefType *typedef_type =
3843 qual_type->getAs<clang::TypedefType>();
3844 if (typedef_type) {
3845 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3846 type_name = typedef_decl->getQualifiedNameAsString();
3847 } else {
3848 type_name = qual_type.getAsString(printing_policy);
3849 }
3850 }
3851 return ConstString(type_name);
3852}
3853
3854uint32_t
3855ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
3856 CompilerType *pointee_or_element_clang_type) {
3857 if (!type)
3858 return 0;
3859
3860 if (pointee_or_element_clang_type)
3861 pointee_or_element_clang_type->Clear();
3862
3863 clang::QualType qual_type(GetQualType(type));
3864
3865 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3866 switch (type_class) {
3867 case clang::Type::Attributed:
3868 return GetTypeInfo(
3869 qual_type->getAs<clang::AttributedType>()
3870 ->getModifiedType().getAsOpaquePtr(),
3871 pointee_or_element_clang_type);
3872 case clang::Type::Builtin: {
3873 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
3874 qual_type->getCanonicalTypeInternal());
3875
3876 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3877 switch (builtin_type->getKind()) {
3878 case clang::BuiltinType::ObjCId:
3879 case clang::BuiltinType::ObjCClass:
3880 if (pointee_or_element_clang_type)
3881 pointee_or_element_clang_type->SetCompilerType(
3882 getASTContext(), getASTContext()->ObjCBuiltinClassTy);
3883 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3884 break;
3885
3886 case clang::BuiltinType::ObjCSel:
3887 if (pointee_or_element_clang_type)
3888 pointee_or_element_clang_type->SetCompilerType(getASTContext(),
3889 getASTContext()->CharTy);
3890 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3891 break;
3892
3893 case clang::BuiltinType::Bool:
3894 case clang::BuiltinType::Char_U:
3895 case clang::BuiltinType::UChar:
3896 case clang::BuiltinType::WChar_U:
3897 case clang::BuiltinType::Char16:
3898 case clang::BuiltinType::Char32:
3899 case clang::BuiltinType::UShort:
3900 case clang::BuiltinType::UInt:
3901 case clang::BuiltinType::ULong:
3902 case clang::BuiltinType::ULongLong:
3903 case clang::BuiltinType::UInt128:
3904 case clang::BuiltinType::Char_S:
3905 case clang::BuiltinType::SChar:
3906 case clang::BuiltinType::WChar_S:
3907 case clang::BuiltinType::Short:
3908 case clang::BuiltinType::Int:
3909 case clang::BuiltinType::Long:
3910 case clang::BuiltinType::LongLong:
3911 case clang::BuiltinType::Int128:
3912 case clang::BuiltinType::Float:
3913 case clang::BuiltinType::Double:
3914 case clang::BuiltinType::LongDouble:
3915 builtin_type_flags |= eTypeIsScalar;
3916 if (builtin_type->isInteger()) {
3917 builtin_type_flags |= eTypeIsInteger;
3918 if (builtin_type->isSignedInteger())
3919 builtin_type_flags |= eTypeIsSigned;
3920 } else if (builtin_type->isFloatingPoint())
3921 builtin_type_flags |= eTypeIsFloat;
3922 break;
3923 default:
3924 break;
3925 }
3926 return builtin_type_flags;
3927 }
3928
3929 case clang::Type::BlockPointer:
3930 if (pointee_or_element_clang_type)
3931 pointee_or_element_clang_type->SetCompilerType(
3932 getASTContext(), qual_type->getPointeeType());
3933 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3934
3935 case clang::Type::Complex: {
3936 uint32_t complex_type_flags =
3937 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3938 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
3939 qual_type->getCanonicalTypeInternal());
3940 if (complex_type) {
3941 clang::QualType complex_element_type(complex_type->getElementType());
3942 if (complex_element_type->isIntegerType())
3943 complex_type_flags |= eTypeIsFloat;
3944 else if (complex_element_type->isFloatingType())
3945 complex_type_flags |= eTypeIsInteger;
3946 }
3947 return complex_type_flags;
3948 } break;
3949
3950 case clang::Type::ConstantArray:
3951 case clang::Type::DependentSizedArray:
3952 case clang::Type::IncompleteArray:
3953 case clang::Type::VariableArray:
3954 if (pointee_or_element_clang_type)
3955 pointee_or_element_clang_type->SetCompilerType(
3956 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
3957 ->getElementType());
3958 return eTypeHasChildren | eTypeIsArray;
3959
3960 case clang::Type::DependentName:
3961 return 0;
3962 case clang::Type::DependentSizedExtVector:
3963 return eTypeHasChildren | eTypeIsVector;
3964 case clang::Type::DependentTemplateSpecialization:
3965 return eTypeIsTemplate;
3966 case clang::Type::Decltype:
3967 return CompilerType(
3968 getASTContext(),
3969 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
3970 .GetTypeInfo(pointee_or_element_clang_type);
3971
3972 case clang::Type::Enum:
3973 if (pointee_or_element_clang_type)
3974 pointee_or_element_clang_type->SetCompilerType(
3975 getASTContext(),
3976 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
3977 return eTypeIsEnumeration | eTypeHasValue;
3978
3979 case clang::Type::Auto:
3980 return CompilerType(
3981 getASTContext(),
3982 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
3983 .GetTypeInfo(pointee_or_element_clang_type);
3984 case clang::Type::Elaborated:
3985 return CompilerType(
3986 getASTContext(),
3987 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
3988 .GetTypeInfo(pointee_or_element_clang_type);
3989 case clang::Type::Paren:
3990 return CompilerType(getASTContext(),
3991 llvm::cast<clang::ParenType>(qual_type)->desugar())
3992 .GetTypeInfo(pointee_or_element_clang_type);
3993
3994 case clang::Type::FunctionProto:
3995 return eTypeIsFuncPrototype | eTypeHasValue;
3996 case clang::Type::FunctionNoProto:
3997 return eTypeIsFuncPrototype | eTypeHasValue;
3998 case clang::Type::InjectedClassName:
3999 return 0;
4000
4001 case clang::Type::LValueReference:
4002 case clang::Type::RValueReference:
4003 if (pointee_or_element_clang_type)
4004 pointee_or_element_clang_type->SetCompilerType(
4005 getASTContext(),
4006 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4007 ->getPointeeType());
4008 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4009
4010 case clang::Type::MemberPointer:
4011 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4012
4013 case clang::Type::ObjCObjectPointer:
4014 if (pointee_or_element_clang_type)
4015 pointee_or_element_clang_type->SetCompilerType(
4016 getASTContext(), qual_type->getPointeeType());
4017 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4018 eTypeHasValue;
4019
4020 case clang::Type::ObjCObject:
4021 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4022 case clang::Type::ObjCInterface:
4023 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4024
4025 case clang::Type::Pointer:
4026 if (pointee_or_element_clang_type)
4027 pointee_or_element_clang_type->SetCompilerType(
4028 getASTContext(), qual_type->getPointeeType());
4029 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4030
4031 case clang::Type::Record:
4032 if (qual_type->getAsCXXRecordDecl())
4033 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4034 else
4035 return eTypeHasChildren | eTypeIsStructUnion;
4036 break;
4037 case clang::Type::SubstTemplateTypeParm:
4038 return eTypeIsTemplate;
4039 case clang::Type::TemplateTypeParm:
4040 return eTypeIsTemplate;
4041 case clang::Type::TemplateSpecialization:
4042 return eTypeIsTemplate;
4043
4044 case clang::Type::Typedef:
4045 return eTypeIsTypedef |
4046 CompilerType(getASTContext(),
4047 llvm::cast<clang::TypedefType>(qual_type)
4048 ->getDecl()
4049 ->getUnderlyingType())
4050 .GetTypeInfo(pointee_or_element_clang_type);
4051 case clang::Type::TypeOfExpr:
4052 return CompilerType(getASTContext(),
4053 llvm::cast<clang::TypeOfExprType>(qual_type)
4054 ->getUnderlyingExpr()
4055 ->getType())
4056 .GetTypeInfo(pointee_or_element_clang_type);
4057 case clang::Type::TypeOf:
4058 return CompilerType(
4059 getASTContext(),
4060 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4061 .GetTypeInfo(pointee_or_element_clang_type);
4062 case clang::Type::UnresolvedUsing:
4063 return 0;
4064
4065 case clang::Type::ExtVector:
4066 case clang::Type::Vector: {
4067 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4068 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4069 qual_type->getCanonicalTypeInternal());
4070 if (vector_type) {
4071 if (vector_type->isIntegerType())
4072 vector_type_flags |= eTypeIsFloat;
4073 else if (vector_type->isFloatingType())
4074 vector_type_flags |= eTypeIsInteger;
4075 }
4076 return vector_type_flags;
4077 }
4078 default:
4079 return 0;
4080 }
4081 return 0;
4082}
4083
4084lldb::LanguageType
4085ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4086 if (!type)
4087 return lldb::eLanguageTypeC;
4088
4089 // If the type is a reference, then resolve it to what it refers to first:
4090 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4091 if (qual_type->isAnyPointerType()) {
4092 if (qual_type->isObjCObjectPointerType())
4093 return lldb::eLanguageTypeObjC;
4094
4095 clang::QualType pointee_type(qual_type->getPointeeType());
4096 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
4097 return lldb::eLanguageTypeC_plus_plus;
4098 if (pointee_type->isObjCObjectOrInterfaceType())
4099 return lldb::eLanguageTypeObjC;
4100 if (pointee_type->isObjCClassType())
4101 return lldb::eLanguageTypeObjC;
4102 if (pointee_type.getTypePtr() ==
4103 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4104 return lldb::eLanguageTypeObjC;
4105 } else {
4106 if (qual_type->isObjCObjectOrInterfaceType())
4107 return lldb::eLanguageTypeObjC;
4108 if (qual_type->getAsCXXRecordDecl())
4109 return lldb::eLanguageTypeC_plus_plus;
4110 switch (qual_type->getTypeClass()) {
4111 default:
4112 break;
4113 case clang::Type::Builtin:
4114 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4115 default:
4116 case clang::BuiltinType::Void:
4117 case clang::BuiltinType::Bool:
4118 case clang::BuiltinType::Char_U:
4119 case clang::BuiltinType::UChar:
4120 case clang::BuiltinType::WChar_U:
4121 case clang::BuiltinType::Char16:
4122 case clang::BuiltinType::Char32:
4123 case clang::BuiltinType::UShort:
4124 case clang::BuiltinType::UInt:
4125 case clang::BuiltinType::ULong:
4126 case clang::BuiltinType::ULongLong:
4127 case clang::BuiltinType::UInt128:
4128 case clang::BuiltinType::Char_S:
4129 case clang::BuiltinType::SChar:
4130 case clang::BuiltinType::WChar_S:
4131 case clang::BuiltinType::Short:
4132 case clang::BuiltinType::Int:
4133 case clang::BuiltinType::Long:
4134 case clang::BuiltinType::LongLong:
4135 case clang::BuiltinType::Int128:
4136 case clang::BuiltinType::Float:
4137 case clang::BuiltinType::Double:
4138 case clang::BuiltinType::LongDouble:
4139 break;
4140
4141 case clang::BuiltinType::NullPtr:
4142 return eLanguageTypeC_plus_plus;
4143
4144 case clang::BuiltinType::ObjCId:
4145 case clang::BuiltinType::ObjCClass:
4146 case clang::BuiltinType::ObjCSel:
4147 return eLanguageTypeObjC;
4148
4149 case clang::BuiltinType::Dependent:
4150 case clang::BuiltinType::Overload:
4151 case clang::BuiltinType::BoundMember:
4152 case clang::BuiltinType::UnknownAny:
4153 break;
4154 }
4155 break;
4156 case clang::Type::Typedef:
4157 return CompilerType(getASTContext(),
4158 llvm::cast<clang::TypedefType>(qual_type)
4159 ->getDecl()
4160 ->getUnderlyingType())
4161 .GetMinimumLanguage();
4162 }
4163 }
4164 return lldb::eLanguageTypeC;
4165}
4166
4167lldb::TypeClass
4168ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4169 if (!type)
4170 return lldb::eTypeClassInvalid;
4171
4172 clang::QualType qual_type(GetQualType(type));
4173
4174 switch (qual_type->getTypeClass()) {
4175 case clang::Type::UnaryTransform:
4176 break;
4177 case clang::Type::FunctionNoProto:
4178 return lldb::eTypeClassFunction;
4179 case clang::Type::FunctionProto:
4180 return lldb::eTypeClassFunction;
4181 case clang::Type::IncompleteArray:
4182 return lldb::eTypeClassArray;
4183 case clang::Type::VariableArray:
4184 return lldb::eTypeClassArray;
4185 case clang::Type::ConstantArray:
4186 return lldb::eTypeClassArray;
4187 case clang::Type::DependentSizedArray:
4188 return lldb::eTypeClassArray;
4189 case clang::Type::DependentSizedExtVector:
4190 return lldb::eTypeClassVector;
4191 case clang::Type::ExtVector:
4192 return lldb::eTypeClassVector;
4193 case clang::Type::Vector:
4194 return lldb::eTypeClassVector;
4195 case clang::Type::Builtin:
4196 return lldb::eTypeClassBuiltin;
4197 case clang::Type::ObjCObjectPointer:
4198 return lldb::eTypeClassObjCObjectPointer;
4199 case clang::Type::BlockPointer:
4200 return lldb::eTypeClassBlockPointer;
4201 case clang::Type::Pointer:
4202 return lldb::eTypeClassPointer;
4203 case clang::Type::LValueReference:
4204 return lldb::eTypeClassReference;
4205 case clang::Type::RValueReference:
4206 return lldb::eTypeClassReference;
4207 case clang::Type::MemberPointer:
4208 return lldb::eTypeClassMemberPointer;
4209 case clang::Type::Complex:
4210 if (qual_type->isComplexType())
4211 return lldb::eTypeClassComplexFloat;
4212 else
4213 return lldb::eTypeClassComplexInteger;
4214 case clang::Type::ObjCObject:
4215 return lldb::eTypeClassObjCObject;
4216 case clang::Type::ObjCInterface:
4217 return lldb::eTypeClassObjCInterface;
4218 case clang::Type::Record: {
4219 const clang::RecordType *record_type =
4220 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4221 const clang::RecordDecl *record_decl = record_type->getDecl();
4222 if (record_decl->isUnion())
4223 return lldb::eTypeClassUnion;
4224 else if (record_decl->isStruct())
4225 return lldb::eTypeClassStruct;
4226 else
4227 return lldb::eTypeClassClass;
4228 } break;
4229 case clang::Type::Enum:
4230 return lldb::eTypeClassEnumeration;
4231 case clang::Type::Typedef:
4232 return lldb::eTypeClassTypedef;
4233 case clang::Type::UnresolvedUsing:
4234 break;
4235 case clang::Type::Paren:
4236 return CompilerType(getASTContext(),
4237 llvm::cast<clang::ParenType>(qual_type)->desugar())
4238 .GetTypeClass();
4239 case clang::Type::Auto:
4240 return CompilerType(
4241 getASTContext(),
4242 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4243 .GetTypeClass();
4244 case clang::Type::Elaborated:
4245 return CompilerType(
4246 getASTContext(),
4247 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4248 .GetTypeClass();
4249
4250 case clang::Type::Attributed:
4251 break;
4252 case clang::Type::TemplateTypeParm:
4253 break;
4254 case clang::Type::SubstTemplateTypeParm:
4255 break;
4256 case clang::Type::SubstTemplateTypeParmPack:
4257 break;
4258 case clang::Type::InjectedClassName:
4259 break;
4260 case clang::Type::DependentName:
4261 break;
4262 case clang::Type::DependentTemplateSpecialization:
4263 break;
4264 case clang::Type::PackExpansion:
4265 break;
4266
4267 case clang::Type::TypeOfExpr:
4268 return CompilerType(getASTContext(),
4269 llvm::cast<clang::TypeOfExprType>(qual_type)
4270 ->getUnderlyingExpr()
4271 ->getType())
4272 .GetTypeClass();
4273 case clang::Type::TypeOf:
4274 return CompilerType(
4275 getASTContext(),
4276 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4277 .GetTypeClass();
4278 case clang::Type::Decltype:
4279 return CompilerType(
4280 getASTContext(),
4281 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4282 .GetTypeClass();
4283 case clang::Type::TemplateSpecialization:
4284 break;
4285 case clang::Type::DeducedTemplateSpecialization:
4286 break;
4287 case clang::Type::Atomic:
4288 break;
4289 case clang::Type::Pipe:
4290 break;
4291
4292 // pointer type decayed from an array or function type.
4293 case clang::Type::Decayed:
4294 break;
4295 case clang::Type::Adjusted:
4296 break;
4297 case clang::Type::ObjCTypeParam:
4298 break;
4299
4300 case clang::Type::DependentAddressSpace:
4301 break;
4302 }
4303 // We don't know hot to display this type...
4304 return lldb::eTypeClassOther;
4305}
4306
4307unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4308 if (type)
4309 return GetQualType(type).getQualifiers().getCVRQualifiers();
4310 return 0;
4311}
4312
4313//----------------------------------------------------------------------
4314// Creating related types
4315//----------------------------------------------------------------------
4316
4317CompilerType
4318ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4319 uint64_t *stride) {
4320 if (type) {
4321 clang::QualType qual_type(GetCanonicalQualType(type));
4322
4323 const clang::Type *array_eletype =
4324 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4325
4326 if (!array_eletype)
4327 return CompilerType();
4328
4329 CompilerType element_type(getASTContext(),
4330 array_eletype->getCanonicalTypeUnqualified());
4331
4332 // TODO: the real stride will be >= this value.. find the real one!
4333 if (stride)
4334 *stride = element_type.GetByteSize(nullptr);
4335
4336 return element_type;
4337 }
4338 return CompilerType();
4339}
4340
4341CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4342 uint64_t size) {
4343 if (type) {
4344 clang::QualType qual_type(GetCanonicalQualType(type));
4345 if (clang::ASTContext *ast_ctx = getASTContext()) {
4346 if (size != 0)
4347 return CompilerType(
4348 ast_ctx, ast_ctx->getConstantArrayType(
4349 qual_type, llvm::APInt(64, size),
4350 clang::ArrayType::ArraySizeModifier::Normal, 0));
4351 else
4352 return CompilerType(
4353 ast_ctx,
4354 ast_ctx->getIncompleteArrayType(
4355 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
4356 }
4357 }
4358
4359 return CompilerType();
4360}
4361
4362CompilerType
4363ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4364 if (type)
4365 return CompilerType(getASTContext(), GetCanonicalQualType(type));
4366 return CompilerType();
4367}
4368
4369static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4370 clang::QualType qual_type) {
4371 if (qual_type->isPointerType())
4372 qual_type = ast->getPointerType(
4373 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4374 else
4375 qual_type = qual_type.getUnqualifiedType();
4376 qual_type.removeLocalConst();
4377 qual_type.removeLocalRestrict();
4378 qual_type.removeLocalVolatile();
4379 return qual_type;
4380}
4381
4382CompilerType
4383ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4384 if (type)
4385 return CompilerType(
4386 getASTContext(),
4387 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4388 return CompilerType();
4389}
4390
4391int ClangASTContext::GetFunctionArgumentCount(
4392 lldb::opaque_compiler_type_t type) {
4393 if (type) {
4394 const clang::FunctionProtoType *func =
4395 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4396 if (func)
4397 return func->getNumParams();
4398 }
4399 return -1;
4400}
4401
4402CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4403 lldb::opaque_compiler_type_t type, size_t idx) {
4404 if (type) {
4405 const clang::FunctionProtoType *func =
4406 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4407 if (func) {
4408 const uint32_t num_args = func->getNumParams();
4409 if (idx < num_args)
4410 return CompilerType(getASTContext(), func->getParamType(idx));
4411 }
4412 }
4413 return CompilerType();
4414}
4415
4416CompilerType
4417ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4418 if (type) {
4419 clang::QualType qual_type(GetQualType(type));
4420 const clang::FunctionProtoType *func =
4421 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4422 if (func)
4423 return CompilerType(getASTContext(), func->getReturnType());
4424 }
4425 return CompilerType();
4426}
4427
4428size_t
4429ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4430 size_t num_functions = 0;
4431 if (type) {
4432 clang::QualType qual_type(GetCanonicalQualType(type));
4433 switch (qual_type->getTypeClass()) {
4434 case clang::Type::Record:
4435 if (GetCompleteQualType(getASTContext(), qual_type)) {
4436 const clang::RecordType *record_type =
4437 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4438 const clang::RecordDecl *record_decl = record_type->getDecl();
4439 assert(record_decl)(static_cast <bool> (record_decl) ? void (0) : __assert_fail
("record_decl", "/build/llvm-toolchain-snapshot-7~svn326551/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 4439, __extension__ __PRETTY_FUNCTION__))
;
4440 const clang::CXXRecordDecl *cxx_record_decl =
4441 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4442 if (cxx_record_decl)
4443 num_functions = std::distance(cxx_record_decl->method_begin(),
4444 cxx_record_decl->method_end());
4445 }
4446 break;
4447
4448 case clang::Type::ObjCObjectPointer: {
4449 const clang::ObjCObjectPointerType *objc_class_type =
4450 qual_type->getAs<clang::ObjCObjectPointerType>();
4451 const clang::ObjCInterfaceType *objc_interface_type =
4452 objc_class_type->getInterfaceType();
4453 if (objc_interface_type &&
4454 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4455 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
4456 clang::ObjCInterfaceDecl *class_interface_decl =
4457 objc_interface_type->getDecl();
4458 if (class_interface_decl) {
4459 num_functions = std::distance(class_interface_decl->meth_begin(),
4460 class_interface_decl->meth_end());
4461 }
4462 }
4463 break;
4464 }
4465
4466 case clang::Type::ObjCObject:
4467 case clang::Type::ObjCInterface:
4468 if (GetCompleteType(type)) {
4469 const clang::ObjCObjectType *objc_class_type =
4470 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4471 if (objc_class_type) {
4472 clang::ObjCInterfaceDecl *class_interface_decl =
4473 objc_class_type->getInterface();
4474 if (class_interface_decl)
4475 num_functions = std::distance(class_interface_decl->meth_begin(),
4476 class_interface_decl->meth_end());
4477 }
4478 }
4479 break;
4480
4481 case clang::Type::Typedef:
4482 return CompilerType(getASTContext(),
4483 llvm::cast<clang::TypedefType>(qual_type)
4484 ->getDecl()
4485 ->getUnderlyingType())
4486 .GetNumMemberFunctions();
4487
4488 case clang::Type::Auto:
4489 return CompilerType(
4490 getASTContext(),
4491 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4492 .GetNumMemberFunctions();
4493
4494 case clang::Type::Elaborated:
4495 return CompilerType(
4496 getASTContext(),
4497 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4498 .GetNumMemberFunctions();
4499
4500 case clang::Type::Paren:
4501 return CompilerType(getASTContext(),
4502 llvm::cast<clang::ParenType>(qual_type)->desugar())
4503 .GetNumMemberFunctions();
4504
4505 default:
4506 break;
4507 }
4508 }
4509 return num_functions;
4510}
4511
4512TypeMemberFunctionImpl
4513ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4514 size_t idx) {
4515 std::string name;
4516 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4517 CompilerType clang_type;
4518 CompilerDecl clang_decl;
4519 if (type) {
4520 clang::QualType qual_type(GetCanonicalQualType(type));
4521 switch (qual_type->getTypeClass()) {
4522 case clang::Type::Record:
4523 if (GetCompleteQualType(getASTContext(), qual_type)) {
4524 const clang::RecordType *record_type =
4525 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4526 const clang::RecordDecl *record_decl = record_type->getDecl();
4527 assert(record_decl)(static_cast <bool> (record_decl) ? void (0) : __assert_fail
("record_decl", "/build/llvm-toolchain-snapshot-7~svn326551/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 4527, __extension__ __PRETTY_FUNCTION__))
;
4528 const clang::CXXRecordDecl *cxx_record_decl =
4529 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4530 if (cxx_record_decl) {
4531 auto method_iter = cxx_record_decl->method_begin();
4532 auto method_end = cxx_record_decl->method_end();
4533 if (idx <
4534 static_cast<size_t>(std::distance(method_iter, method_end))) {
4535 std::advance(method_iter, idx);
4536 clang::CXXMethodDecl *cxx_method_decl =
4537 method_iter->getCanonicalDecl();
4538 if (cxx_method_decl) {
4539 name = cxx_method_decl->getDeclName().getAsString();
4540 if (cxx_method_decl->isStatic())
4541 kind = lldb::eMemberFunctionKindStaticMethod;
4542 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4543 kind = lldb::eMemberFunctionKindConstructor;
4544 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4545 kind = lldb::eMemberFunctionKindDestructor;
4546 else
4547 kind = lldb::eMemberFunctionKindInstanceMethod;
4548 clang_type = CompilerType(
4549 this, cxx_method_decl->getType().getAsOpaquePtr());
4550 clang_decl = CompilerDecl(this, cxx_method_decl);
4551 }
4552 }
4553 }
4554 }
4555 break;
4556
4557 case clang::Type::ObjCObjectPointer: {
4558 const clang::ObjCObjectPointerType *objc_class_type =
4559 qual_type->getAs<clang::ObjCObjectPointerType>();
4560 const clang::ObjCInterfaceType *objc_interface_type =
4561 objc_class_type->getInterfaceType();
4562 if (objc_interface_type &&
4563 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4564 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
4565 clang::ObjCInterfaceDecl *class_interface_decl =
4566 objc_interface_type->getDecl();
4567 if (class_interface_decl) {
4568 auto method_iter = class_interface_decl->meth_begin();
4569 auto method_end = class_interface_decl->meth_end();
4570 if (idx <
4571 static_cast<size_t>(std::distance(method_iter, method_end))) {
4572 std::advance(method_iter, idx);
4573 clang::ObjCMethodDecl *objc_method_decl =
4574 method_iter->getCanonicalDecl();
4575 if (objc_method_decl) {
4576 clang_decl = CompilerDecl(this, objc_method_decl);
4577 name = objc_method_decl->getSelector().getAsString();
4578 if (objc_method_decl->isClassMethod())
4579 kind = lldb::eMemberFunctionKindStaticMethod;
4580 else
4581 kind = lldb::eMemberFunctionKindInstanceMethod;
4582 }
4583 }
4584 }
4585 }
4586 break;
4587 }
4588
4589 case clang::Type::ObjCObject:
4590 case clang::Type::ObjCInterface:
4591 if (GetCompleteType(type)) {
4592 const clang::ObjCObjectType *objc_class_type =
4593 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4594 if (objc_class_type) {
4595 clang::ObjCInterfaceDecl *class_interface_decl =
4596 objc_class_type->getInterface();
4597 if (class_interface_decl) {
4598 auto method_iter = class_interface_decl->meth_begin();
4599 auto method_end = class_interface_decl->meth_end();
4600 if (idx <
4601 static_cast<size_t>(std::distance(method_iter, method_end))) {
4602 std::advance(method_iter, idx);
4603 clang::ObjCMethodDecl *objc_method_decl =
4604 method_iter->getCanonicalDecl();
4605 if (objc_method_decl) {
4606 clang_decl = CompilerDecl(this, objc_method_decl);
4607 name = objc_method_decl->getSelector().getAsString();
4608 if (objc_method_decl->isClassMethod())
4609 kind = lldb::eMemberFunctionKindStaticMethod;
4610 else
4611 kind = lldb::eMemberFunctionKindInstanceMethod;
4612 }
4613 }
4614 }
4615 }
4616 }
4617 break;
4618
4619 case clang::Type::Typedef:
4620 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4621 ->getDecl()
4622 ->getUnderlyingType()
4623 .getAsOpaquePtr(),
4624 idx);
4625
4626 case clang::Type::Auto:
4627 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4628 ->getDeducedType()
4629 .getAsOpaquePtr(),
4630 idx);
4631
4632 case clang::Type::Elaborated:
4633 return GetMemberFunctionAtIndex(
4634 llvm::cast<clang::ElaboratedType>(qual_type)
4635 ->getNamedType()
4636 .getAsOpaquePtr(),
4637 idx);
4638
4639 case clang::Type::Paren:
4640 return GetMemberFunctionAtIndex(
4641 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4642 idx);
4643
4644 default:
4645 break;
4646 }
4647 }
4648
4649 if (kind == eMemberFunctionKindUnknown)
4650 return TypeMemberFunctionImpl();
4651 else
4652 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
4653}
4654
4655CompilerType
4656ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4657 if (type)
4658 return CompilerType(getASTContext(),
4659 GetQualType(type).getNonReferenceType());
4660 return CompilerType();
4661}
4662
4663CompilerType ClangASTContext::CreateTypedefType(
4664 const CompilerType &type, const char *typedef_name,
4665 const CompilerDeclContext &compiler_decl_ctx) {
4666 if (type && typedef_name && typedef_name[0]) {
4667 ClangASTContext *ast =
4668 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4669 if (!ast)
4670 return CompilerType();
4671 clang::ASTContext *clang_ast = ast->getASTContext();
4672 clang::QualType qual_type(ClangUtil::GetQualType(type));
4673
4674 clang::DeclContext *decl_ctx =
4675 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4676 if (decl_ctx == nullptr)
4677 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4678
4679 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4680 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4681 &clang_ast->Idents.get(typedef_name),
4682 clang_ast->getTrivialTypeSourceInfo(qual_type));
4683
4684 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4685
4686 // Get a uniqued clang::QualType for the typedef decl type
4687 return CompilerType(clang_ast, clang_ast->getTypedefType(decl));
4688 }
4689 return CompilerType();
4690}
4691
4692CompilerType
4693ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4694 if (type) {
4695 clang::QualType qual_type(GetQualType(type));
4696 return CompilerType(getASTContext(),
4697 qual_type.getTypePtr()->getPointeeType());
4698 }
4699 return CompilerType();
4700}
4701
4702CompilerType
4703ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4704 if (type) {
4705 clang::QualType qual_type(GetQualType(type));
4706
4707 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4708 switch (type_class) {
4709 case clang::Type::ObjCObject:
4710 case clang::Type::ObjCInterface:
4711 return CompilerType(getASTContext(),
4712 getASTContext()->getObjCObjectPointerType(qual_type));
4713
4714 default:
4715 return CompilerType(getASTContext(),
4716 getASTContext()->getPointerType(qual_type));
4717 }
4718 }
4719 return CompilerType();
4720}
4721
4722CompilerType
4723ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4724 if (type)
4725 return CompilerType(this, getASTContext()
4726 ->getLValueReferenceType(GetQualType(type))
4727 .getAsOpaquePtr());
4728 else
4729 return CompilerType();
4730}
4731
4732CompilerType
4733ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4734 if (type)
4735 return CompilerType(this, getASTContext()
4736 ->getRValueReferenceType(GetQualType(type))
4737 .getAsOpaquePtr());
4738 else
4739 return CompilerType();
4740}
4741
4742CompilerType
4743ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4744 if (type) {
4745 clang::QualType result(GetQualType(type));
4746 result.addConst();
4747 return CompilerType(this, result.getAsOpaquePtr());
4748 }
4749 return CompilerType();
4750}
4751
4752CompilerType
4753ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4754 if (type) {
4755 clang::QualType result(GetQualType(type));
4756 result.addVolatile();
4757 return CompilerType(this, result.getAsOpaquePtr());
4758 }
4759 return CompilerType();
4760}
4761
4762CompilerType
4763ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4764 if (type) {
4765 clang::QualType result(GetQualType(type));
4766 result.addRestrict();
4767 return CompilerType(this, result.getAsOpaquePtr());
4768 }
4769 return CompilerType();
4770}
4771
4772CompilerType
4773ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4774 const char *typedef_name,
4775 const CompilerDeclContext &compiler_decl_ctx) {
4776 if (type) {
4777 clang::ASTContext *clang_ast = getASTContext();
4778 clang::QualType qual_type(GetQualType(type));
4779
4780 clang::DeclContext *decl_ctx =
4781 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4782 if (decl_ctx == nullptr)
4783 decl_ctx = getASTContext()->getTranslationUnitDecl();
4784
4785 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4786 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4787 &clang_ast->Idents.get(typedef_name),
4788 clang_ast->getTrivialTypeSourceInfo(qual_type));
4789
4790 clang::TagDecl *tdecl = nullptr;
4791 if (!qual_type.isNull()) {
4792 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4793 tdecl = rt->getDecl();
4794 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4795 tdecl = et->getDecl();
4796 }
4797
4798 // Check whether this declaration is an anonymous struct, union, or enum,
4799 // hidden behind a typedef. If so, we
4800 // try to check whether we have a typedef tag to attach to the original
4801 // record declaration
4802 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4803 tdecl->setTypedefNameForAnonDecl(decl);
4804
4805 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4806
4807 // Get a uniqued clang::QualType for the typedef decl type
4808 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4809 }
4810 return CompilerType();
4811}
4812
4813CompilerType
4814ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4815 if (type) {
4816 const clang::TypedefType *typedef_type =
4817 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4818 if (typedef_type)
4819 return CompilerType(getASTContext(),
4820 typedef_type->getDecl()->getUnderlyingType());
4821 }
4822 return CompilerType();
4823}
4824
4825//----------------------------------------------------------------------
4826// Create related types using the current type's AST
4827//----------------------------------------------------------------------
4828
4829CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
4830 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
4831}
4832//----------------------------------------------------------------------
4833// Exploring the type
4834//----------------------------------------------------------------------
4835
4836uint64_t ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
4837 ExecutionContextScope *exe_scope) {
4838 if (GetCompleteType(type)) {
4839 clang::QualType qual_type(GetCanonicalQualType(type));
4840 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4841 switch (type_class) {
4842 case clang::Type::Record:
4843 if (GetCompleteType(type))
4844 return getASTContext()->getTypeSize(qual_type);
4845 else
4846 return 0;
4847 break;
4848
4849 case clang::Type::ObjCInterface:
4850 case clang::Type::ObjCObject: {
4851 ExecutionContext exe_ctx(exe_scope);
4852 Process *process = exe_ctx.GetProcessPtr();
4853 if (process) {
4854 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4855 if (objc_runtime) {
4856 uint64_t bit_size = 0;
4857 if (objc_runtime->GetTypeBitSize(
4858 CompilerType(getASTContext(), qual_type), bit_size))
4859 return bit_size;
4860 }
4861 } else {
4862 static bool g_printed = false;
4863 if (!g_printed) {
4864 StreamString s;
4865 DumpTypeDescription(type, &s);
4866
4867 llvm::outs() << "warning: trying to determine the size of type ";
4868 llvm::outs() << s.GetString() << "\n";
4869 llvm::outs() << "without a valid ExecutionContext. this is not "
4870 "reliable. please file a bug against LLDB.\n";
4871 llvm::outs() << "backtrace:\n";
4872 llvm::sys::PrintStackTrace(llvm::outs());
4873 llvm::outs() << "\n";
4874 g_printed = true;
4875 }
4876 }
4877 }
4878 LLVM_FALLTHROUGH[[clang::fallthrough]];
4879 default:
4880 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
4881 if (bit_size == 0) {
4882 if (qual_type->isIncompleteArrayType())
4883 return getASTContext()->getTypeSize(
4884 qual_type->getArrayElementTypeNoTypeQual()
4885 ->getCanonicalTypeUnqualified());
4886 }
4887 if (qual_type->isObjCObjectOrInterfaceType())
4888 return bit_size +
4889 getASTContext()->getTypeSize(
4890 getASTContext()->ObjCBuiltinClassTy);
4891 return bit_size;
4892 }
4893 }
4894 return 0;
4895}
4896
4897size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
4898 if (GetCompleteType(type))
4899 return getASTContext()->getTypeAlign(GetQualType(type));
4900 return 0;
4901}
4902
4903lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
4904 uint64_t &count) {
4905 if (!type)
4906 return lldb::eEncodingInvalid;
4907
4908 count = 1;
4909 clang::QualType qual_type(GetCanonicalQualType(type));
4910
4911 switch (qual_type->getTypeClass()) {
4912 case clang::Type::UnaryTransform:
4913 break;
4914
4915 case clang::Type::FunctionNoProto:
4916 case clang::Type::FunctionProto:
4917 break;
4918
4919 case clang::Type::IncompleteArray:
4920 case clang::Type::VariableArray:
4921 break;
4922
4923 case clang::Type::ConstantArray:
4924 break;
4925
4926 case clang::Type::ExtVector:
4927 case clang::Type::Vector:
4928 // TODO: Set this to more than one???
4929 break;
4930
4931 case clang::Type::Builtin:
4932 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4933 case clang::BuiltinType::Void:
4934 break;
4935
4936 case clang::BuiltinType::Bool:
4937 case clang::BuiltinType::Char_S:
4938 case clang::BuiltinType::SChar:
4939 case clang::BuiltinType::WChar_S:
4940 case clang::BuiltinType::Char16:
4941 case clang::BuiltinType::Char32:
4942 case clang::BuiltinType::Short:
4943 case clang::BuiltinType::Int:
4944 case clang::BuiltinType::Long:
4945 case clang::BuiltinType::LongLong:
4946 case clang::BuiltinType::Int128:
4947 return lldb::eEncodingSint;
4948
4949 case clang::BuiltinType::Char_U:
4950 case clang::BuiltinType::UChar:
4951 case clang::BuiltinType::WChar_U:
4952 case clang::BuiltinType::UShort:
4953 case clang::BuiltinType::UInt:
4954 case clang::BuiltinType::ULong:
4955 case clang::BuiltinType::ULongLong:
4956 case clang::BuiltinType::UInt128:
4957 return lldb::eEncodingUint;
4958
4959 case clang::BuiltinType::Half:
4960 case clang::BuiltinType::Float:
4961 case clang::BuiltinType::Float16:
4962 case clang::BuiltinType::Float128:
4963 case clang::BuiltinType::Double:
4964 case clang::BuiltinType::LongDouble:
4965 return lldb::eEncodingIEEE754;
4966
4967 case clang::BuiltinType::ObjCClass:
4968 case clang::BuiltinType::ObjCId:
4969 case clang::BuiltinType::ObjCSel:
4970 return lldb::eEncodingUint;
4971
4972 case clang::BuiltinType::NullPtr:
4973 return lldb::eEncodingUint;
4974
4975 case clang::BuiltinType::Kind::ARCUnbridgedCast:
4976 case clang::BuiltinType::Kind::BoundMember:
4977 case clang::BuiltinType::Kind::BuiltinFn:
4978 case clang::BuiltinType::Kind::Dependent:
4979 case clang::BuiltinType::Kind::OCLClkEvent:
4980 case clang::BuiltinType::Kind::OCLEvent:
4981 case clang::BuiltinType::Kind::OCLImage1dRO:
4982 case clang::BuiltinType::Kind::OCLImage1dWO:
4983 case clang::BuiltinType::Kind::OCLImage1dRW:
4984 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
4985 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
4986 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
4987 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
4988 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
4989 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
4990 case clang::BuiltinType::Kind::OCLImage2dRO:
4991 case clang::BuiltinType::Kind::OCLImage2dWO:
4992 case clang::BuiltinType::Kind::OCLImage2dRW:
4993 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
4994 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
4995 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
4996 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
4997 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
4998 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
4999 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5000 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5001 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5002 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5003 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5004 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5005 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5006 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5007 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5008 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5009 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5010 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5011 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5012 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5013 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5014 case clang::BuiltinType::Kind::OCLImage3dRO:
5015 case clang::BuiltinType::Kind::OCLImage3dWO:
5016 case clang::BuiltinType::Kind::OCLImage3dRW:
5017 case clang::BuiltinType::Kind::OCLQueue:
5018 case clang::BuiltinType::Kind::OCLReserveID:
5019 case clang::BuiltinType::Kind::OCLSampler:
5020 case clang::BuiltinType::Kind::OMPArraySection:
5021 case clang::BuiltinType::Kind::Overload:
5022 case clang::BuiltinType::Kind::PseudoObject:
5023 case clang::BuiltinType::Kind::UnknownAny:
5024 break;
5025 }
5026 break;
5027 // All pointer types are represented as unsigned integer encodings.
5028 // We may nee to add a eEncodingPointer if we ever need to know the
5029 // difference
5030 case clang::Type::ObjCObjectPointer:
5031 case clang::Type::BlockPointer:
5032 case clang::Type::Pointer:
5033 case clang::Type::LValueReference:
5034 case clang::Type::RValueReference:
5035 case clang::Type::MemberPointer:
5036 return lldb::eEncodingUint;
5037 case clang::Type::Complex: {
5038 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5039 if (qual_type->isComplexType())
5040 encoding = lldb::eEncodingIEEE754;
5041 else {
5042 const clang::ComplexType *complex_type =
5043 qual_type->getAsComplexIntegerType();
5044 if (complex_type)
5045 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5046 .GetEncoding(count);
5047 else
5048 encoding = lldb::eEncodingSint;
5049 }
5050 count = 2;
5051 return encoding;
5052 }
5053
5054 case clang::Type::ObjCInterface:
5055 break;
5056 case clang::Type::Record:
5057 break;
5058 case clang::Type::Enum:
5059 return lldb::eEncodingSint;
5060 case clang::Type::Typedef:
5061 return CompilerType(getASTContext(),
5062 llvm::cast<clang::TypedefType>(qual_type)
5063 ->getDecl()
5064 ->getUnderlyingType())
5065 .GetEncoding(count);
5066
5067 case clang::Type::Auto:
5068 return CompilerType(
5069 getASTContext(),
5070 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5071 .GetEncoding(count);
5072
5073 case clang::Type::Elaborated:
5074 return CompilerType(
5075 getASTContext(),
5076 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5077 .GetEncoding(count);
5078
5079 case clang::Type::Paren:
5080 return CompilerType(getASTContext(),
5081 llvm::cast<clang::ParenType>(qual_type)->desugar())
5082 .GetEncoding(count);
5083 case clang::Type::TypeOfExpr:
5084 return CompilerType(getASTContext(),
5085 llvm::cast<clang::TypeOfExprType>(qual_type)
5086 ->getUnderlyingExpr()
5087 ->getType())
5088 .GetEncoding(count);
5089 case clang::Type::TypeOf:
5090 return CompilerType(
5091 getASTContext(),
5092 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5093 .GetEncoding(count);
5094 case clang::Type::Decltype:
5095 return CompilerType(
5096 getASTContext(),
5097 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5098 .GetEncoding(count);
5099 case clang::Type::DependentSizedArray:
5100 case clang::Type::DependentSizedExtVector:
5101 case clang::Type::UnresolvedUsing:
5102 case clang::Type::Attributed:
5103 case clang::Type::TemplateTypeParm:
5104 case clang::Type::SubstTemplateTypeParm:
5105 case clang::Type::SubstTemplateTypeParmPack:
5106 case clang::Type::InjectedClassName:
5107 case clang::Type::DependentName:
5108 case clang::Type::DependentTemplateSpecialization:
5109 case clang::Type::PackExpansion:
5110 case clang::Type::ObjCObject:
5111
5112 case clang::Type::TemplateSpecialization:
5113 case clang::Type::DeducedTemplateSpecialization:
5114 case clang::Type::Atomic:
5115 case clang::Type::Adjusted:
5116 case clang::Type::Pipe:
5117 break;
5118
5119 // pointer type decayed from an array or function type.
5120 case clang::Type::Decayed:
5121 break;
5122 case clang::Type::ObjCTypeParam:
5123 break;
5124
5125 case clang::Type::DependentAddressSpace:
5126 break;
5127 }
5128 count = 0;
5129 return lldb::eEncodingInvalid;
5130}
5131
5132lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5133 if (!type)
5134 return lldb::eFormatDefault;
5135
5136 clang::QualType qual_type(GetCanonicalQualType(type));
5137
5138 switch (qual_type->getTypeClass()) {
5139 case clang::Type::UnaryTransform:
5140 break;
5141
5142 case clang::Type::FunctionNoProto:
5143 case clang::Type::FunctionProto:
5144 break;
5145
5146 case clang::Type::IncompleteArray:
5147 case clang::Type::VariableArray:
5148 break;
5149
5150 case clang::Type::ConstantArray:
5151 return lldb::eFormatVoid; // no value
5152
5153 case clang::Type::ExtVector:
5154 case clang::Type::Vector:
5155 break;
5156
5157 case clang::Type::Builtin:
5158 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5159 // default: assert(0 && "Unknown builtin type!");
5160 case clang::BuiltinType::UnknownAny:
5161 case clang::BuiltinType::Void:
5162 case clang::BuiltinType::BoundMember:
5163 break;
5164
5165 case clang::BuiltinType::Bool:
5166 return lldb::eFormatBoolean;
5167 case clang::BuiltinType::Char_S:
5168 case clang::BuiltinType::SChar:
5169 case clang::BuiltinType::WChar_S:
5170 case clang::BuiltinType::Char_U:
5171 case clang::BuiltinType::UChar:
5172 case clang::BuiltinType::WChar_U:
5173 return lldb::eFormatChar;
5174 case clang::BuiltinType::Char16:
5175 return lldb::eFormatUnicode16;
5176 case clang::BuiltinType::Char32:
5177 return lldb::eFormatUnicode32;
5178 case clang::BuiltinType::UShort:
5179 return lldb::eFormatUnsigned;
5180 case clang::BuiltinType::Short:
5181 return lldb::eFormatDecimal;
5182 case clang::BuiltinType::UInt:
5183 return lldb::eFormatUnsigned;
5184 case clang::BuiltinType::Int:
5185 return lldb::eFormatDecimal;
5186 case clang::BuiltinType::ULong:
5187 return lldb::eFormatUnsigned;
5188 case clang::BuiltinType::Long:
5189 return lldb::eFormatDecimal;
5190 case clang::BuiltinType::ULongLong:
5191 return lldb::eFormatUnsigned;
5192 case clang::BuiltinType::LongLong:
5193 return lldb::eFormatDecimal;
5194 case clang::BuiltinType::UInt128:
5195 return lldb::eFormatUnsigned;
5196 case clang::BuiltinType::Int128:
5197 return lldb::eFormatDecimal;
5198 case clang::BuiltinType::Half:
5199 case clang::BuiltinType::Float:
5200 case clang::BuiltinType::Double:
5201 case clang::BuiltinType::LongDouble:
5202 return lldb::eFormatFloat;
5203 default:
5204 return lldb::eFormatHex;
5205 }
5206 break;
5207 case clang::Type::ObjCObjectPointer:
5208 return lldb::eFormatHex;
5209 case clang::Type::BlockPointer:
5210 return lldb::eFormatHex;
5211 case clang::Type::Pointer:
5212 return lldb::eFormatHex;
5213 case clang::Type::LValueReference:
5214 case clang::Type::RValueReference:
5215 return lldb::eFormatHex;
5216 case clang::Type::MemberPointer:
5217 break;
5218 case clang::Type::Complex: {
5219 if (qual_type->isComplexType())
5220 return lldb::eFormatComplex;
5221 else
5222 return lldb::eFormatComplexInteger;
5223 }
5224 case clang::Type::ObjCInterface:
5225 break;
5226 case clang::Type::Record:
5227 break;
5228 case clang::Type::Enum:
5229 return lldb::eFormatEnum;
5230 case clang::Type::Typedef:
5231 return CompilerType(getASTContext(),
5232 llvm::cast<clang::TypedefType>(qual_type)
5233 ->getDecl()
5234 ->getUnderlyingType())
5235 .GetFormat();
5236 case clang::Type::Auto:
5237 return CompilerType(getASTContext(),
5238 llvm::cast<clang::AutoType>(qual_type)->desugar())
5239 .GetFormat();
5240 case clang::Type::Paren:
5241 return CompilerType(getASTContext(),
5242 llvm::cast<clang::ParenType>(qual_type)->desugar())
5243 .GetFormat();
5244 case clang::Type::Elaborated:
5245 return CompilerType(
5246 getASTContext(),
5247 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5248 .GetFormat();
5249 case clang::Type::TypeOfExpr:
5250 return CompilerType(getASTContext(),
5251 llvm::cast<clang::TypeOfExprType>(qual_type)
5252 ->getUnderlyingExpr()
5253 ->getType())
5254 .GetFormat();
5255 case clang::Type::TypeOf:
5256 return CompilerType(
5257 getASTContext(),
5258 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5259 .GetFormat();
5260 case clang::Type::Decltype:
5261 return CompilerType(
5262 getASTContext(),
5263 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5264 .GetFormat();
5265 case clang::Type::DependentSizedArray:
5266 case clang::Type::DependentSizedExtVector:
5267 case clang::Type::UnresolvedUsing:
5268 case clang::Type::Attributed:
5269 case clang::Type::TemplateTypeParm:
5270 case clang::Type::SubstTemplateTypeParm:
5271 case clang::Type::SubstTemplateTypeParmPack:
5272 case clang::Type::InjectedClassName:
5273 case clang::Type::DependentName:
5274 case clang::Type::DependentTemplateSpecialization:
5275 case clang::Type::PackExpansion:
5276 case clang::Type::ObjCObject:
5277
5278 case clang::Type::TemplateSpecialization:
5279 case clang::Type::DeducedTemplateSpecialization:
5280 case clang::Type::Atomic:
5281 case clang::Type::Adjusted:
5282 case clang::Type::Pipe:
5283 break;
5284
5285 // pointer type decayed from an array or function type.
5286 case clang::Type::Decayed:
5287 break;
5288 case clang::Type::ObjCTypeParam:
5289 break;
5290
5291 case clang::Type::DependentAddressSpace:
5292 break;
5293 }
5294 // We don't know hot to display this type...
5295 return lldb::eFormatBytes;
5296}
5297
5298static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5299 bool check_superclass) {
5300 while (class_interface_decl) {
5301 if (class_interface_decl->ivar_size() > 0)
5302 return true;
5303
5304 if (check_superclass)
5305 class_interface_decl = class_interface_decl->getSuperClass();
5306 else
5307 break;
5308 }
5309 return false;
5310}
5311
5312uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
5313 bool omit_empty_base_classes) {
5314 if (!type)
5315 return 0;
5316
5317 uint32_t num_children = 0;
5318 clang::QualType qual_type(GetQualType(type));
5319 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5320 switch (type_class) {
5321 case clang::Type::Builtin:
5322 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5323 case clang::BuiltinType::ObjCId: // child is Class
5324 case clang::BuiltinType::ObjCClass: // child is Class
5325 num_children = 1;
5326 break;
5327
5328 default:
5329 break;
5330 }
5331 break;
5332
5333 case clang::Type::Complex:
5334 return 0;
5335
5336 case clang::Type::Record:
5337 if (GetCompleteQualType(getASTContext(), qual_type)) {
5338 const clang::RecordType *record_type =
5339 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5340 const clang::RecordDecl *record_decl = record_type->getDecl();
5341 assert(record_decl)(static_cast <bool> (record_decl) ? void (0) : __assert_fail
("record_decl", "/build/llvm-toolchain-snapshot-7~svn326551/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 5341, __extension__ __PRETTY_FUNCTION__))
;
5342 const clang::CXXRecordDecl *cxx_record_decl =
5343 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5344 if (cxx_record_decl) {
5345 if (omit_empty_base_classes) {
5346 // Check each base classes to see if it or any of its
5347 // base classes contain any fields. This can help
5348 // limit the noise in variable views by not having to
5349 // show base classes that contain no members.
5350 clang::CXXRecordDecl::base_class_const_iterator base_class,
5351 base_class_end;
5352 for (base_class = cxx_record_decl->bases_begin(),
5353 base_class_end = cxx_record_decl->bases_end();
5354 base_class != base_class_end; ++base_class) {
5355 const clang::CXXRecordDecl *base_class_decl =
5356 llvm::cast<clang::CXXRecordDecl>(
5357 base_class->getType()
5358 ->getAs<clang::RecordType>()
5359 ->getDecl());
5360
5361 // Skip empty base classes
5362 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5363 continue;
5364
5365 num_children++;
5366 }
5367 } else {
5368 // Include all base classes
5369 num_children += cxx_record_decl->getNumBases();
5370 }
5371 }
5372 clang::RecordDecl::field_iterator field, field_end;
5373 for (field = record_decl->field_begin(),
5374 field_end = record_decl->field_end();
5375 field != field_end; ++field)
5376 ++num_children;
5377 }
5378 break;
5379
5380 case clang::Type::ObjCObject:
5381 case clang::Type::ObjCInterface:
5382 if (GetCompleteQualType(getASTContext(), qual_type)) {
5383 const clang::ObjCObjectType *objc_class_type =
5384 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5385 assert(objc_class_type)(static_cast <bool> (objc_class_type) ? void (0) : __assert_fail
("objc_class_type", "/build/llvm-toolchain-snapshot-7~svn326551/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 5385, __extension__ __PRETTY_FUNCTION__))
;
5386 if (objc_class_type) {
5387 clang::ObjCInterfaceDecl *class_interface_decl =
5388 objc_class_type->getInterface();
5389
5390 if (class_interface_decl) {
5391
5392 clang::ObjCInterfaceDecl *superclass_interface_decl =
5393 class_interface_decl->getSuperClass();
5394 if (superclass_interface_decl) {
5395 if (omit_empty_base_classes) {
5396 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5397 ++num_children;
5398 } else
5399 ++num_children;
5400 }
5401
5402 num_children += class_interface_decl->ivar_size();
5403 }
5404 }
5405 }
5406 break;
5407
5408 case clang::Type::ObjCObjectPointer: {
5409 const clang::ObjCObjectPointerType *pointer_type =
5410 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5411 clang::QualType pointee_type = pointer_type->getPointeeType();
5412 uint32_t num_pointee_children =
5413 CompilerType(getASTContext(), pointee_type)
5414 .GetNumChildren(omit_empty_base_classes);
5415 // If this type points to a simple type, then it has 1 child
5416 if (num_pointee_children == 0)
5417 num_children = 1;
5418 else
5419 num_children = num_pointee_children;
5420 } break;
5421
5422 case clang::Type::Vector:
5423 case clang::Type::ExtVector:
5424 num_children =
5425 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5426 break;
5427
5428 case clang::Type::ConstantArray:
5429 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5430 ->getSize()
5431 .getLimitedValue();
5432 break;
5433
5434 case clang::Type::Pointer: {
5435 const clang::PointerType *pointer_type =
5436 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5437 clang::QualType pointee_type(pointer_type->getPointeeType());
5438 uint32_t num_pointee_children =
5439 CompilerType(getASTContext(), pointee_type)
5440 .GetNumChildren(omit_empty_base_classes);
5441 if (num_pointee_children == 0) {
5442 // We have a pointer to a pointee type that claims it has no children.
5443 // We will want to look at
5444 num_children = GetNumPointeeChildren(pointee_type);
5445 } else
5446 num_children = num_pointee_children;
5447 } break;
5448
5449 case clang::Type::LValueReference:
5450 case clang::Type::RValueReference: {
5451 const clang::ReferenceType *reference_type =
5452 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5453 clang::QualType pointee_type = reference_type->getPointeeType();
5454 uint32_t num_pointee_children =
5455 CompilerType(getASTContext(), pointee_type)
5456 .GetNumChildren(omit_empty_base_classes);
5457 // If this type points to a simple type, then it has 1 child
5458 if (num_pointee_children == 0)
5459 num_children = 1;
5460 else
5461 num_children = num_pointee_children;
5462 } break;
5463
5464 case clang::Type::Typedef:
5465 num_children =
5466 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5467 ->getDecl()
5468 ->getUnderlyingType())
5469 .GetNumChildren(omit_empty_base_classes);
5470 break;
5471
5472 case clang::Type::Auto:
5473 num_children =
5474 CompilerType(getASTContext(),
5475 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5476 .GetNumChildren(omit_empty_base_classes);
5477 break;
5478
5479 case clang::Type::Elaborated:
5480 num_children =
5481 CompilerType(
5482 getASTContext(),
5483 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5484 .GetNumChildren(omit_empty_base_classes);
5485 break;
5486
5487 case clang::Type::Paren:
5488 num_children =
5489 CompilerType(getASTContext(),
5490 llvm::cast<clang::ParenType>(qual_type)->desugar())
5491 .GetNumChildren(omit_empty_base_classes);
5492 break;
5493 default:
5494 break;
5495 }
5496 return num_children;
5497}
5498
5499CompilerType ClangASTContext::GetBuiltinTypeByName(const ConstString &name) {
5500 return GetBasicType(GetBasicTypeEnumeration(name));
5501}
5502
5503lldb::BasicType
5504ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5505 if (type) {
5506 clang::QualType qual_type(GetQualType(type));
5507 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5508 if (type_class == clang::Type::Builtin) {
5509 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5510 case clang::BuiltinType::Void:
5511 return eBasicTypeVoid;
5512 case clang::BuiltinType::Bool:
5513 return eBasicTypeBool;
5514 case clang::BuiltinType::Char_S:
5515 return eBasicTypeSignedChar;
5516 case clang::BuiltinType::Char_U:
5517 return eBasicTypeUnsignedChar;
5518 case clang::BuiltinType::Char16:
5519 return eBasicTypeChar16;
5520 case clang::BuiltinType::Char32:
5521 return eBasicTypeChar32;
5522 case clang::BuiltinType::UChar:
5523 return eBasicTypeUnsignedChar;
5524 case clang::BuiltinType::SChar:
5525 return eBasicTypeSignedChar;
5526 case clang::BuiltinType::WChar_S:
5527 return eBasicTypeSignedWChar;
5528 case clang::BuiltinType::WChar_U:
5529 return eBasicTypeUnsignedWChar;
5530 case clang::BuiltinType::Short:
5531 return eBasicTypeShort;
5532 case clang::BuiltinType::UShort:
5533 return eBasicTypeUnsignedShort;
5534 case clang::BuiltinType::Int:
5535 return eBasicTypeInt;
5536 case clang::BuiltinType::UInt:
5537 return eBasicTypeUnsignedInt;
5538 case clang::BuiltinType::Long:
5539 return eBasicTypeLong;
5540 case clang::BuiltinType::ULong:
5541 return eBasicTypeUnsignedLong;
5542 case clang::BuiltinType::LongLong:
5543 return eBasicTypeLongLong;
5544 case clang::BuiltinType::ULongLong:
5545 return eBasicTypeUnsignedLongLong;
5546 case clang::BuiltinType::Int128:
5547 return eBasicTypeInt128;
5548 case clang::BuiltinType::UInt128:
5549 return eBasicTypeUnsignedInt128;
5550
5551 case clang::BuiltinType::Half:
5552 return eBasicTypeHalf;
5553 case clang::BuiltinType::Float:
5554 return eBasicTypeFloat;
5555 case clang::BuiltinType::Double:
5556 return eBasicTypeDouble;
5557 case clang::BuiltinType::LongDouble:
5558 return eBasicTypeLongDouble;
5559
5560 case clang::BuiltinType::NullPtr:
5561 return eBasicTypeNullPtr;
5562 case clang::BuiltinType::ObjCId:
5563 return eBasicTypeObjCID;
5564 case clang::BuiltinType::ObjCClass:
5565 return eBasicTypeObjCClass;
5566 case clang::BuiltinType::ObjCSel:
5567 return eBasicTypeObjCSel;
5568 default:
5569 return eBasicTypeOther;
5570 }
5571 }
5572 }
5573 return eBasicTypeInvalid;
5574}
5575
5576void ClangASTContext::ForEachEnumerator(
5577 lldb::opaque_compiler_type_t type,
5578 std::function<bool(const CompilerType &integer_type,
5579 const ConstString &name,
5580 const llvm::APSInt &value)> const &callback) {
5581 const clang::EnumType *enum_type =
5582 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5583 if (enum_type) {
5584 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5585 if (enum_decl) {
5586 CompilerType integer_type(this,
5587 enum_decl->getIntegerType().getAsOpaquePtr());
5588
5589 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5590 for (enum_pos = enum_decl->enumerator_begin(),
5591 enum_end_pos = enum_decl->enumerator_end();
5592 enum_pos != enum_end_pos; ++enum_pos) {
5593 ConstString name(enum_pos->getNameAsString().c_str());
5594 if (!callback(integer_type, name, enum_pos->getInitVal()))
5595 break;
5596 }
5597 }
5598 }
5599}
5600
5601#pragma mark Aggregate Types
5602
5603uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5604 if (!type)
5605 return 0;
5606
5607 uint32_t count = 0;
5608 clang::QualType qual_type(GetCanonicalQualType(type));
5609 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5610 switch (type_class) {
5611 case clang::Type::Record:
5612 if (GetCompleteType(type)) {
5613 const clang::RecordType *record_type =
5614 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5615 if (record_type) {
5616 clang::RecordDecl *record_decl = record_type->getDecl();
5617 if (record_decl) {
5618 uint32_t field_idx = 0;
5619 clang::RecordDecl::field_iterator field, field_end;
5620 for (field = record_decl->field_begin(),
5621 field_end = record_decl->field_end();
5622 field != field_end; ++field)
5623 ++field_idx;
5624 count = field_idx;
5625 }
5626 }
5627 }
5628 break;
5629
5630 case clang::Type::Typedef:
5631 count =
5632 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5633 ->getDecl()
5634 ->getUnderlyingType())
5635 .GetNumFields();
5636 break;
5637
5638 case clang::Type::Auto:
5639 count =
5640 CompilerType(getASTContext(),
5641 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5642 .GetNumFields();
5643 break;
5644
5645 case clang::Type::Elaborated:
5646 count = CompilerType(
5647 getASTContext(),
5648 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5649 .GetNumFields();
5650 break;
5651
5652 case clang::Type::Paren:
5653 count = CompilerType(getASTContext(),
5654 llvm::cast<clang::ParenType>(qual_type)->desugar())
5655 .GetNumFields();
5656 break;
5657
5658 case clang::Type::ObjCObjectPointer: {
5659 const clang::ObjCObjectPointerType *objc_class_type =
5660 qual_type->getAs<clang::ObjCObjectPointerType>();
5661 const clang::ObjCInterfaceType *objc_interface_type =
5662 objc_class_type->getInterfaceType();
5663 if (objc_interface_type &&
5664 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5665 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
5666 clang::ObjCInterfaceDecl *class_interface_decl =
5667 objc_interface_type->getDecl();
5668 if (class_interface_decl) {
5669 count = class_interface_decl->ivar_size();
5670 }
5671 }
5672 break;
5673 }
5674
5675 case clang::Type::ObjCObject:
5676 case clang::Type::ObjCInterface:
5677 if (GetCompleteType(type)) {
5678 const clang::ObjCObjectType *objc_class_type =
5679 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5680 if (objc_class_type) {
5681 clang::ObjCInterfaceDecl *class_interface_decl =
5682 objc_class_type->getInterface();
5683
5684 if (class_interface_decl)
5685 count = class_interface_decl->ivar_size();
5686 }
5687 }
5688 break;
5689
5690 default:
5691 break;
5692 }
5693 return count;
5694}
5695
5696static lldb::opaque_compiler_type_t
5697GetObjCFieldAtIndex(clang::ASTContext *ast,
5698 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5699 std::string &name, uint64_t *bit_offset_ptr,
5700 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5701 if (class_interface_decl) {
5702 if (idx < (class_interface_decl->ivar_size())) {
5703 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5704 ivar_end = class_interface_decl->ivar_end();
5705 uint32_t ivar_idx = 0;
5706
5707 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5708 ++ivar_pos, ++ivar_idx) {
5709 if (ivar_idx == idx) {
5710 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5711
5712 clang::QualType ivar_qual_type(ivar_decl->getType());
5713
5714 name.assign(ivar_decl->getNameAsString());
5715
5716 if (bit_offset_ptr) {
5717 const clang::ASTRecordLayout &interface_layout =
5718 ast->getASTObjCInterfaceLayout(class_interface_decl);
5719 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5720 }
5721
5722 const bool is_bitfield = ivar_pos->isBitField();
5723
5724 if (bitfield_bit_size_ptr) {
5725 *bitfield_bit_size_ptr = 0;
5726
5727 if (is_bitfield && ast) {
5728 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5729 llvm::APSInt bitfield_apsint;
5730 if (bitfield_bit_size_expr &&
5731 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5732 *ast)) {
5733 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5734 }
5735 }
5736 }
5737 if (is_bitfield_ptr)
5738 *is_bitfield_ptr = is_bitfield;
5739
5740 return ivar_qual_type.getAsOpaquePtr();
5741 }
5742 }
5743 }
5744 }
5745 return nullptr;
5746}
5747
5748CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5749 size_t idx, std::string &name,
5750 uint64_t *bit_offset_ptr,
5751 uint32_t *bitfield_bit_size_ptr,
5752 bool *is_bitfield_ptr) {
5753 if (!type)
5754 return CompilerType();
5755
5756 clang::QualType qual_type(GetCanonicalQualType(type));
5757 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5758 switch (type_class) {
5759 case clang::Type::Record:
5760 if (GetCompleteType(type)) {
5761 const clang::RecordType *record_type =
5762 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5763 const clang::RecordDecl *record_decl = record_type->getDecl();
5764 uint32_t field_idx = 0;
5765 clang::RecordDecl::field_iterator field, field_end;
5766 for (field = record_decl->field_begin(),
5767 field_end = record_decl->field_end();
5768 field != field_end; ++field, ++field_idx) {
5769 if (idx == field_idx) {
5770 // Print the member type if requested
5771 // Print the member name and equal sign
5772 name.assign(field->getNameAsString());
5773
5774 // Figure out the type byte size (field_type_info.first) and
5775 // alignment (field_type_info.second) from the AST context.
5776 if (bit_offset_ptr) {
5777 const clang::ASTRecordLayout &record_layout =
5778 getASTContext()->getASTRecordLayout(record_decl);
5779 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
5780 }
5781
5782 const bool is_bitfield = field->isBitField();
5783
5784 if (bitfield_bit_size_ptr) {
5785 *bitfield_bit_size_ptr = 0;
5786
5787 if (is_bitfield) {
5788 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5789 llvm::APSInt bitfield_apsint;
5790 if (bitfield_bit_size_expr &&
5791 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5792 *getASTContext())) {
5793 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5794 }
5795 }
5796 }
5797 if (is_bitfield_ptr)
5798 *is_bitfield_ptr = is_bitfield;
5799
5800 return CompilerType(getASTContext(), field->getType());
5801 }
5802 }
5803 }
5804 break;
5805
5806 case clang::Type::ObjCObjectPointer: {
5807 const clang::ObjCObjectPointerType *objc_class_type =
5808 qual_type->getAs<clang::ObjCObjectPointerType>();
5809 const clang::ObjCInterfaceType *objc_interface_type =
5810 objc_class_type->getInterfaceType();
5811 if (objc_interface_type &&
5812 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5813 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
5814 clang::ObjCInterfaceDecl *class_interface_decl =
5815 objc_interface_type->getDecl();
5816 if (class_interface_decl) {
5817 return CompilerType(
5818 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5819 idx, name, bit_offset_ptr,
5820 bitfield_bit_size_ptr, is_bitfield_ptr));
5821 }
5822 }
5823 break;
5824 }
5825
5826 case clang::Type::ObjCObject:
5827 case clang::Type::ObjCInterface:
5828 if (GetCompleteType(type)) {
5829 const clang::ObjCObjectType *objc_class_type =
5830 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5831 assert(objc_class_type)(static_cast <bool> (objc_class_type) ? void (0) : __assert_fail
("objc_class_type", "/build/llvm-toolchain-snapshot-7~svn326551/tools/lldb/source/Symbol/ClangASTContext.cpp"
, 5831, __extension__ __PRETTY_FUNCTION__))
;
5832 if (objc_class_type) {
5833 clang::ObjCInterfaceDecl *class_interface_decl =
5834 objc_class_type->getInterface();
5835 return CompilerType(
5836 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5837 idx, name, bit_offset_ptr,
5838 bitfield_bit_size_ptr, is_bitfield_ptr));
5839 }
5840 }
5841 break;
5842
5843 case clang::Type::Typedef:
5844 return CompilerType(getASTContext(),
5845 llvm::cast<clang::TypedefType>(qual_type)
5846 ->getDecl()
5847 ->getUnderlyingType())
5848 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5849 is_bitfield_ptr);
5850
5851 case clang::Type::Auto:
5852 return CompilerType(
5853 getASTContext(),
5854 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5855 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5856 is_bitfield_ptr);
5857
5858 case clang::Type::Elaborated:
5859 return CompilerType(
5860 getASTContext(),
5861 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5862 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5863 is_bitfield_ptr);
5864
5865 case clang::Type::Paren:
5866 return CompilerType(getASTContext(),
5867 llvm::cast<clang::ParenType>(qual_type)->desugar())
5868 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5869 is_bitfield_ptr);
5870
5871 default:
5872 break;
5873 }
5874 return CompilerType();
5875}
5876
5877uint32_t
5878ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
5879 uint32_t count = 0;
5880 clang::QualType qual_type(GetCanonicalQualType(type));
5881 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5882 switch (type_class) {
5883 case clang::Type::Record:
5884 if (GetCompleteType(type)) {
5885 const clang::CXXRecordDecl *cxx_record_decl =
5886 qual_type->getAsCXXRecordDecl();
5887 if (cxx_record_decl)
5888 count = cxx_record_decl->getNumBases();
5889 }
5890 break;
5891
5892 case clang::Type::ObjCObjectPointer:
5893 count = GetPointeeType(type).GetNumDirectBaseClasses();
5894 break;
5895
5896 case clang::Type::ObjCObject:
5897 if (GetCompleteType(type)) {
5898 const clang::ObjCObjectType *objc_class_type =
5899 qual_type->getAsObjCQualifiedInterfaceType();
5900 if (objc_class_type) {
5901 clang::ObjCInterfaceDecl *class_interface_decl =
5902 objc_class_type->getInterface();
5903
5904 if (class_interface_decl && class_interface_decl->getSuperClass())
5905 count = 1;
5906 }
5907 }
5908 break;
5909 case clang::Type::ObjCInterface:
5910 if (GetCompleteType(type)) {
5911 const clang::ObjCInterfaceType *objc_interface_type =
5912 qual_type->getAs<clang::ObjCInterfaceType>();
5913 if (objc_interface_type) {
5914 clang::ObjCInterfaceDecl *class_interface_decl =
5915 objc_interface_type->getInterface();
5916
5917 if (class_interface_decl && class_interface_decl->getSuperClass())
5918 count = 1;
5919 }
5920 }
5921 break;
5922
5923 case clang::Type::Typedef:
5924 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5925 ->getDecl()
5926 ->getUnderlyingType()
5927 .getAsOpaquePtr());
5928 break;
5929
5930 case clang::Type::Auto:
5931 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
5932 ->getDeducedType()
5933 .getAsOpaquePtr());
5934 break;
5935
5936 case clang::Type::Elaborated:
5937 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
5938 ->getNamedType()
5939 .getAsOpaquePtr());
5940 break;
5941
5942 case clang::Type::Paren:
5943 return GetNumDirectBaseClasses(
5944 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5945
5946 default:
5947 break;
5948 }
5949 return count;
5950}
5951
5952uint32_t
5953ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
5954 uint32_t count = 0;
5955 clang::QualType qual_type(GetCanonicalQualType(type));
5956 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5957 switch (type_class) {
5958 case clang::Type::Record:
5959 if (GetCompleteType(type)) {
5960 const clang::CXXRecordDecl *cxx_record_decl =
5961 qual_type->getAsCXXRecordDecl();
5962 if (cxx_record_decl)
5963 count = cxx_record_decl->getNumVBases();
5964 }
5965 break;
5966
5967 case clang::Type::Typedef:
5968 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5969 ->getDecl()
5970 ->getUnderlyingType()
5971 .getAsOpaquePtr());
5972 break;
5973
5974 case clang::Type::Auto:
5975 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
5976 ->getDeducedType()
5977 .getAsOpaquePtr());
5978 break;
5979
5980 case clang::Type::Elaborated:
5981 count =
5982 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
5983 ->getNamedType()
5984 .getAsOpaquePtr());
5985 break;
5986
5987 case clang::Type::Paren:
5988 count = GetNumVirtualBaseClasses(
5989 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5990 break;
5991
5992 default:
5993 break;
5994 }
5995 return count;
5996}
5997
5998CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
5999 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6000 clang::QualType qual_type(GetCanonicalQualType(type));
6001 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6002 switch (type_class) {
6003 case clang::Type::Record:
6004 if (GetCompleteType(type)) {
6005