Bug Summary

File:build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
Warning:line 9119, column 17
Although the value stored to 'bytes_read' is used in the enclosing expression, the value is never actually read from 'bytes_read'

Annotated Source Code

Press '?' to see keyboard shortcuts

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