blob: c554d989b17e440394539c01fc109022360691cb [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ClangASTContext.cpp -------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Eli Friedman932197d2010-06-13 19:06:42 +00009#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010
Zachary Turner827d5d72016-12-16 04:27:00 +000011#include "llvm/Support/FormatAdapters.h"
12#include "llvm/Support/FormatVariadic.h"
13
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000014#include <mutex>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include <string>
Sean Callananfe38c852015-10-08 23:07:53 +000016#include <vector>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017
Greg Clayton6beaaa62011-01-17 03:46:26 +000018
Kate Stoneb9c1b512016-09-06 20:57:50 +000019// Clang headers like to use NDEBUG inside of them to enable/disable debug
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +000020// related features using "#ifndef NDEBUG" preprocessor blocks to do one thing
Greg Clayton6beaaa62011-01-17 03:46:26 +000021// or another. This is bad because it means that if clang was built in release
22// mode, it assumes that you are building in release mode which is not always
23// the case. You can end up with functions that are defined as empty in header
24// files when NDEBUG is not defined, and this can cause link errors with the
25// clang .a files that you have since you might be missing functions in the .a
26// file. So we have to define NDEBUG when including clang headers to avoid any
27// mismatches. This is covered by rdar://problem/8691220
28
Sean Callanan3b1d4f62011-10-26 17:46:51 +000029#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
Greg Clayton6beaaa62011-01-17 03:46:26 +000030#define LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000031#define NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000032// Need to include assert.h so it is as clang would expect it to be (disabled)
33#include <assert.h>
34#endif
35
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "clang/AST/ASTContext.h"
37#include "clang/AST/ASTImporter.h"
Greg Claytonf74c4032012-12-03 18:29:55 +000038#include "clang/AST/Attr.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "clang/AST/CXXInheritance.h"
Greg Clayton8cf05932010-07-22 18:30:50 +000040#include "clang/AST/DeclObjC.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000041#include "clang/AST/DeclTemplate.h"
Greg Claytonfe689042015-11-10 17:47:04 +000042#include "clang/AST/Mangle.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "clang/AST/RecordLayout.h"
44#include "clang/AST/Type.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000045#include "clang/AST/VTableBuilder.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046#include "clang/Basic/Builtins.h"
Sean Callanan7e2863b2012-02-06 21:28:03 +000047#include "clang/Basic/Diagnostic.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048#include "clang/Basic/FileManager.h"
Sean Callanan79439e82010-11-18 02:56:27 +000049#include "clang/Basic/FileSystemOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050#include "clang/Basic/SourceManager.h"
51#include "clang/Basic/TargetInfo.h"
52#include "clang/Basic/TargetOptions.h"
53#include "clang/Frontend/FrontendOptions.h"
54#include "clang/Frontend/LangStandard.h"
Raphael Isemannf74a4c12019-04-30 08:41:35 +000055#include "clang/Sema/Sema.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000056
57#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000058#undef NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000059#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
60// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
61#include <assert.h>
62#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063
Greg Claytond8d4a572015-08-11 21:38:15 +000064#include "llvm/Support/Signals.h"
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000065#include "llvm/Support/Threading.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000066
Zachary Turnerd133f6a2016-03-28 22:53:41 +000067#include "Plugins/ExpressionParser/Clang/ClangFunctionCaller.h"
68#include "Plugins/ExpressionParser/Clang/ClangUserExpression.h"
69#include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000070#include "lldb/Utility/ArchSpec.h"
Zachary Turner01c32432017-02-14 19:06:07 +000071#include "lldb/Utility/Flags.h"
72
Zachary Turner29cb8682017-03-03 20:57:05 +000073#include "lldb/Core/DumpDataExtractor.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000074#include "lldb/Core/Module.h"
75#include "lldb/Core/PluginManager.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000076#include "lldb/Core/StreamFile.h"
Enrico Granata2267ad42014-09-16 17:28:40 +000077#include "lldb/Core/ThreadSafeDenseMap.h"
Greg Clayton57ee3062013-07-11 22:46:58 +000078#include "lldb/Core/UniqueCStringMap.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000079#include "lldb/Symbol/ClangASTContext.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000080#include "lldb/Symbol/ClangASTImporter.h"
Greg Clayton6dc8d582015-08-18 22:32:36 +000081#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000082#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000083#include "lldb/Symbol/ClangUtil.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000084#include "lldb/Symbol/ObjectFile.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000085#include "lldb/Symbol/SymbolFile.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000086#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000087#include "lldb/Target/ExecutionContext.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000088#include "lldb/Target/Language.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000089#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham151c0322015-09-15 21:13:50 +000090#include "lldb/Target/Process.h"
91#include "lldb/Target/Target.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000092#include "lldb/Utility/DataExtractor.h"
Sean Callananc530ba92016-05-02 21:15:31 +000093#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000094#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000095#include "lldb/Utility/RegularExpression.h"
Pavel Labathd821c992018-08-07 11:07:21 +000096#include "lldb/Utility/Scalar.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000097
Greg Clayton261ac3f2015-08-28 01:01:03 +000098#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
Zachary Turner42dff792016-04-15 00:21:26 +000099#include "Plugins/SymbolFile/PDB/PDBASTParser.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +0000100
Eli Friedman932197d2010-06-13 19:06:42 +0000101#include <stdio.h>
102
Greg Clayton1341baf2013-07-11 23:36:31 +0000103#include <mutex>
104
Greg Claytonc86103d2010-08-05 01:57:25 +0000105using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106using namespace lldb_private;
107using namespace llvm;
108using namespace clang;
109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110namespace {
111static inline bool
112ClangASTContextSupportsLanguage(lldb::LanguageType language) {
113 return language == eLanguageTypeUnknown || // Clang is the default type system
114 Language::LanguageIsC(language) ||
115 Language::LanguageIsCPlusPlus(language) ||
116 Language::LanguageIsObjC(language) ||
117 Language::LanguageIsPascal(language) ||
118 // Use Clang for Rust until there is a proper language plugin for it
119 language == eLanguageTypeRust ||
Johan Engelen04799572016-11-25 11:01:12 +0000120 language == eLanguageTypeExtRenderScript ||
121 // Use Clang for D until there is a proper language plugin for it
Bruce Mitchenerb8233f82018-11-27 05:37:27 +0000122 language == eLanguageTypeD ||
123 // Open Dylan compiler debug info is designed to be Clang-compatible
124 language == eLanguageTypeDylan;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125}
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000126
127// Checks whether m1 is an overload of m2 (as opposed to an override). This is
128// called by addOverridesForMethod to distinguish overrides (which share a
129// vtable entry) from overloads (which require distinct entries).
130bool isOverload(clang::CXXMethodDecl *m1, clang::CXXMethodDecl *m2) {
131 // FIXME: This should detect covariant return types, but currently doesn't.
132 lldbassert(&m1->getASTContext() == &m2->getASTContext() &&
133 "Methods should have the same AST context");
134 clang::ASTContext &context = m1->getASTContext();
135
136 const auto *m1Type = llvm::cast<clang::FunctionProtoType>(
137 context.getCanonicalType(m1->getType()));
138
139 const auto *m2Type = llvm::cast<clang::FunctionProtoType>(
140 context.getCanonicalType(m2->getType()));
141
142 auto compareArgTypes = [&context](const clang::QualType &m1p,
143 const clang::QualType &m2p) {
144 return context.hasSameType(m1p.getUnqualifiedType(),
145 m2p.getUnqualifiedType());
146 };
147
148 // FIXME: In C++14 and later, we can just pass m2Type->param_type_end()
149 // as a fourth parameter to std::equal().
150 return (m1->getNumParams() != m2->getNumParams()) ||
151 !std::equal(m1Type->param_type_begin(), m1Type->param_type_end(),
152 m2Type->param_type_begin(), compareArgTypes);
153}
154
155// If decl is a virtual method, walk the base classes looking for methods that
156// decl overrides. This table of overridden methods is used by IRGen to
157// determine the vtable layout for decl's parent class.
158void addOverridesForMethod(clang::CXXMethodDecl *decl) {
159 if (!decl->isVirtual())
160 return;
161
162 clang::CXXBasePaths paths;
163
164 auto find_overridden_methods =
165 [decl](const clang::CXXBaseSpecifier *specifier,
166 clang::CXXBasePath &path) {
167 if (auto *base_record = llvm::dyn_cast<clang::CXXRecordDecl>(
168 specifier->getType()->getAs<clang::RecordType>()->getDecl())) {
169
170 clang::DeclarationName name = decl->getDeclName();
171
172 // If this is a destructor, check whether the base class destructor is
173 // virtual.
174 if (name.getNameKind() == clang::DeclarationName::CXXDestructorName)
175 if (auto *baseDtorDecl = base_record->getDestructor()) {
176 if (baseDtorDecl->isVirtual()) {
177 path.Decls = baseDtorDecl;
178 return true;
179 } else
180 return false;
181 }
182
183 // Otherwise, search for name in the base class.
184 for (path.Decls = base_record->lookup(name); !path.Decls.empty();
185 path.Decls = path.Decls.slice(1)) {
186 if (auto *method_decl =
187 llvm::dyn_cast<clang::CXXMethodDecl>(path.Decls.front()))
188 if (method_decl->isVirtual() && !isOverload(decl, method_decl)) {
189 path.Decls = method_decl;
190 return true;
191 }
192 }
193 }
194
195 return false;
196 };
197
198 if (decl->getParent()->lookupInBases(find_overridden_methods, paths)) {
199 for (auto *overridden_decl : paths.found_decls())
200 decl->addOverriddenMethod(
201 llvm::cast<clang::CXXMethodDecl>(overridden_decl));
202 }
203}
Greg Clayton56939cb2015-09-17 22:23:34 +0000204}
205
Aleksandr Urakov1dc51db2018-11-12 16:23:50 +0000206static lldb::addr_t GetVTableAddress(Process &process,
207 VTableContextBase &vtable_ctx,
208 ValueObject &valobj,
209 const ASTRecordLayout &record_layout) {
210 // Retrieve type info
211 CompilerType pointee_type;
212 CompilerType this_type(valobj.GetCompilerType());
213 uint32_t type_info = this_type.GetTypeInfo(&pointee_type);
214 if (!type_info)
215 return LLDB_INVALID_ADDRESS;
216
217 // Check if it's a pointer or reference
218 bool ptr_or_ref = false;
219 if (type_info & (eTypeIsPointer | eTypeIsReference)) {
220 ptr_or_ref = true;
221 type_info = pointee_type.GetTypeInfo();
222 }
223
224 // We process only C++ classes
225 const uint32_t cpp_class = eTypeIsClass | eTypeIsCPlusPlus;
226 if ((type_info & cpp_class) != cpp_class)
227 return LLDB_INVALID_ADDRESS;
228
229 // Calculate offset to VTable pointer
230 lldb::offset_t vbtable_ptr_offset =
231 vtable_ctx.isMicrosoft() ? record_layout.getVBPtrOffset().getQuantity()
232 : 0;
233
234 if (ptr_or_ref) {
235 // We have a pointer / ref to object, so read
236 // VTable pointer from process memory
237
238 if (valobj.GetAddressTypeOfChildren() != eAddressTypeLoad)
239 return LLDB_INVALID_ADDRESS;
240
241 auto vbtable_ptr_addr = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
242 if (vbtable_ptr_addr == LLDB_INVALID_ADDRESS)
243 return LLDB_INVALID_ADDRESS;
244
245 vbtable_ptr_addr += vbtable_ptr_offset;
246
247 Status err;
248 return process.ReadPointerFromMemory(vbtable_ptr_addr, err);
249 }
250
251 // We have an object already read from process memory,
252 // so just extract VTable pointer from it
253
254 DataExtractor data;
255 Status err;
256 auto size = valobj.GetData(data, err);
257 if (err.Fail() || vbtable_ptr_offset + data.GetAddressByteSize() > size)
258 return LLDB_INVALID_ADDRESS;
259
260 return data.GetPointer(&vbtable_ptr_offset);
261}
262
263static int64_t ReadVBaseOffsetFromVTable(Process &process,
264 VTableContextBase &vtable_ctx,
265 lldb::addr_t vtable_ptr,
266 const CXXRecordDecl *cxx_record_decl,
267 const CXXRecordDecl *base_class_decl) {
268 if (vtable_ctx.isMicrosoft()) {
269 clang::MicrosoftVTableContext &msoft_vtable_ctx =
270 static_cast<clang::MicrosoftVTableContext &>(vtable_ctx);
271
272 // Get the index into the virtual base table. The
273 // index is the index in uint32_t from vbtable_ptr
274 const unsigned vbtable_index =
275 msoft_vtable_ctx.getVBTableIndex(cxx_record_decl, base_class_decl);
276 const lldb::addr_t base_offset_addr = vtable_ptr + vbtable_index * 4;
277 Status err;
278 return process.ReadSignedIntegerFromMemory(base_offset_addr, 4, INT64_MAX,
279 err);
280 }
281
282 clang::ItaniumVTableContext &itanium_vtable_ctx =
283 static_cast<clang::ItaniumVTableContext &>(vtable_ctx);
284
285 clang::CharUnits base_offset_offset =
286 itanium_vtable_ctx.getVirtualBaseOffsetOffset(cxx_record_decl,
287 base_class_decl);
288 const lldb::addr_t base_offset_addr =
289 vtable_ptr + base_offset_offset.getQuantity();
290 const uint32_t base_offset_size = process.GetAddressByteSize();
291 Status err;
292 return process.ReadSignedIntegerFromMemory(base_offset_addr, base_offset_size,
293 INT64_MAX, err);
294}
295
296static bool GetVBaseBitOffset(VTableContextBase &vtable_ctx,
297 ValueObject &valobj,
298 const ASTRecordLayout &record_layout,
299 const CXXRecordDecl *cxx_record_decl,
300 const CXXRecordDecl *base_class_decl,
301 int32_t &bit_offset) {
302 ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
303 Process *process = exe_ctx.GetProcessPtr();
304 if (!process)
305 return false;
306
307 lldb::addr_t vtable_ptr =
308 GetVTableAddress(*process, vtable_ctx, valobj, record_layout);
309 if (vtable_ptr == LLDB_INVALID_ADDRESS)
310 return false;
311
312 auto base_offset = ReadVBaseOffsetFromVTable(
313 *process, vtable_ctx, vtable_ptr, cxx_record_decl, base_class_decl);
314 if (base_offset == INT64_MAX)
315 return false;
316
317 bit_offset = base_offset * 8;
318
319 return true;
320}
321
Kate Stoneb9c1b512016-09-06 20:57:50 +0000322typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext *>
323 ClangASTMap;
Enrico Granata5d84a692014-08-19 21:46:37 +0000324
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325static ClangASTMap &GetASTMap() {
326 static ClangASTMap *g_map_ptr = nullptr;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000327 static llvm::once_flag g_once_flag;
328 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
330 });
331 return *g_map_ptr;
Enrico Granata5d84a692014-08-19 21:46:37 +0000332}
333
Davide Italiano7e3ef4d2018-03-20 19:46:32 +0000334bool ClangASTContext::IsOperator(const char *name,
335 clang::OverloadedOperatorKind &op_kind) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336 if (name == nullptr || name[0] == '\0')
337 return false;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000338
339#define OPERATOR_PREFIX "operator"
340#define OPERATOR_PREFIX_LENGTH (sizeof(OPERATOR_PREFIX) - 1)
341
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342 const char *post_op_name = nullptr;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000343
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344 bool no_space = true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000345
Kate Stoneb9c1b512016-09-06 20:57:50 +0000346 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
347 return false;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000348
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349 post_op_name = name + OPERATOR_PREFIX_LENGTH;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000350
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351 if (post_op_name[0] == ' ') {
352 post_op_name++;
353 no_space = false;
354 }
Pavel Labath1ac2b202016-08-15 14:32:32 +0000355
356#undef OPERATOR_PREFIX
357#undef OPERATOR_PREFIX_LENGTH
358
Adrian Prantl05097242018-04-30 16:49:04 +0000359 // This is an operator, set the overloaded operator kind to invalid in case
360 // this is a conversion operator...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361 op_kind = clang::NUM_OVERLOADED_OPERATORS;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000362
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363 switch (post_op_name[0]) {
364 default:
365 if (no_space)
366 return false;
367 break;
368 case 'n':
369 if (no_space)
370 return false;
371 if (strcmp(post_op_name, "new") == 0)
372 op_kind = clang::OO_New;
373 else if (strcmp(post_op_name, "new[]") == 0)
374 op_kind = clang::OO_Array_New;
375 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000376
Kate Stoneb9c1b512016-09-06 20:57:50 +0000377 case 'd':
378 if (no_space)
379 return false;
380 if (strcmp(post_op_name, "delete") == 0)
381 op_kind = clang::OO_Delete;
382 else if (strcmp(post_op_name, "delete[]") == 0)
383 op_kind = clang::OO_Array_Delete;
384 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000385
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386 case '+':
387 if (post_op_name[1] == '\0')
388 op_kind = clang::OO_Plus;
389 else if (post_op_name[2] == '\0') {
390 if (post_op_name[1] == '=')
391 op_kind = clang::OO_PlusEqual;
392 else if (post_op_name[1] == '+')
393 op_kind = clang::OO_PlusPlus;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000394 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000396
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397 case '-':
398 if (post_op_name[1] == '\0')
399 op_kind = clang::OO_Minus;
400 else if (post_op_name[2] == '\0') {
401 switch (post_op_name[1]) {
402 case '=':
403 op_kind = clang::OO_MinusEqual;
404 break;
405 case '-':
406 op_kind = clang::OO_MinusMinus;
407 break;
408 case '>':
409 op_kind = clang::OO_Arrow;
410 break;
411 }
412 } else if (post_op_name[3] == '\0') {
413 if (post_op_name[2] == '*')
414 op_kind = clang::OO_ArrowStar;
415 break;
416 }
417 break;
418
419 case '*':
420 if (post_op_name[1] == '\0')
421 op_kind = clang::OO_Star;
422 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
423 op_kind = clang::OO_StarEqual;
424 break;
425
426 case '/':
427 if (post_op_name[1] == '\0')
428 op_kind = clang::OO_Slash;
429 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
430 op_kind = clang::OO_SlashEqual;
431 break;
432
433 case '%':
434 if (post_op_name[1] == '\0')
435 op_kind = clang::OO_Percent;
436 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
437 op_kind = clang::OO_PercentEqual;
438 break;
439
440 case '^':
441 if (post_op_name[1] == '\0')
442 op_kind = clang::OO_Caret;
443 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
444 op_kind = clang::OO_CaretEqual;
445 break;
446
447 case '&':
448 if (post_op_name[1] == '\0')
449 op_kind = clang::OO_Amp;
450 else if (post_op_name[2] == '\0') {
451 switch (post_op_name[1]) {
452 case '=':
453 op_kind = clang::OO_AmpEqual;
454 break;
455 case '&':
456 op_kind = clang::OO_AmpAmp;
457 break;
458 }
459 }
460 break;
461
462 case '|':
463 if (post_op_name[1] == '\0')
464 op_kind = clang::OO_Pipe;
465 else if (post_op_name[2] == '\0') {
466 switch (post_op_name[1]) {
467 case '=':
468 op_kind = clang::OO_PipeEqual;
469 break;
470 case '|':
471 op_kind = clang::OO_PipePipe;
472 break;
473 }
474 }
475 break;
476
477 case '~':
478 if (post_op_name[1] == '\0')
479 op_kind = clang::OO_Tilde;
480 break;
481
482 case '!':
483 if (post_op_name[1] == '\0')
484 op_kind = clang::OO_Exclaim;
485 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
486 op_kind = clang::OO_ExclaimEqual;
487 break;
488
489 case '=':
490 if (post_op_name[1] == '\0')
491 op_kind = clang::OO_Equal;
492 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
493 op_kind = clang::OO_EqualEqual;
494 break;
495
496 case '<':
497 if (post_op_name[1] == '\0')
498 op_kind = clang::OO_Less;
499 else if (post_op_name[2] == '\0') {
500 switch (post_op_name[1]) {
501 case '<':
502 op_kind = clang::OO_LessLess;
503 break;
504 case '=':
505 op_kind = clang::OO_LessEqual;
506 break;
507 }
508 } else if (post_op_name[3] == '\0') {
509 if (post_op_name[2] == '=')
510 op_kind = clang::OO_LessLessEqual;
511 }
512 break;
513
514 case '>':
515 if (post_op_name[1] == '\0')
516 op_kind = clang::OO_Greater;
517 else if (post_op_name[2] == '\0') {
518 switch (post_op_name[1]) {
519 case '>':
520 op_kind = clang::OO_GreaterGreater;
521 break;
522 case '=':
523 op_kind = clang::OO_GreaterEqual;
524 break;
525 }
526 } else if (post_op_name[1] == '>' && post_op_name[2] == '=' &&
527 post_op_name[3] == '\0') {
528 op_kind = clang::OO_GreaterGreaterEqual;
529 }
530 break;
531
532 case ',':
533 if (post_op_name[1] == '\0')
534 op_kind = clang::OO_Comma;
535 break;
536
537 case '(':
538 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
539 op_kind = clang::OO_Call;
540 break;
541
542 case '[':
543 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
544 op_kind = clang::OO_Subscript;
545 break;
546 }
547
548 return true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000549}
Enrico Granata5d84a692014-08-19 21:46:37 +0000550
Greg Clayton57ee3062013-07-11 22:46:58 +0000551clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +0000552ClangASTContext::ConvertAccessTypeToAccessSpecifier(AccessType access) {
553 switch (access) {
554 default:
555 break;
556 case eAccessNone:
Greg Clayton8cf05932010-07-22 18:30:50 +0000557 return AS_none;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000558 case eAccessPublic:
559 return AS_public;
560 case eAccessPrivate:
561 return AS_private;
562 case eAccessProtected:
563 return AS_protected;
564 }
565 return AS_none;
Greg Clayton8cf05932010-07-22 18:30:50 +0000566}
567
Kate Stoneb9c1b512016-09-06 20:57:50 +0000568static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) {
569 // FIXME: Cleanup per-file based stuff.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570
Adrian Prantl05097242018-04-30 16:49:04 +0000571 // Set some properties which depend solely on the input kind; it would be
572 // nice to move these to the language standard, and have the driver resolve
573 // the input kind + language standard.
Richard Smith8186cd42017-04-26 22:10:53 +0000574 if (IK.getLanguage() == InputKind::Asm) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000575 Opts.AsmPreprocessor = 1;
Richard Smith8186cd42017-04-26 22:10:53 +0000576 } else if (IK.isObjectiveC()) {
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000577 Opts.ObjC = 1;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000578 }
579
580 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
581
582 if (LangStd == LangStandard::lang_unspecified) {
583 // Based on the base language, pick one.
Richard Smith8186cd42017-04-26 22:10:53 +0000584 switch (IK.getLanguage()) {
585 case InputKind::Unknown:
586 case InputKind::LLVM_IR:
587 case InputKind::RenderScript:
David Blaikiea322f362017-01-06 00:38:06 +0000588 llvm_unreachable("Invalid input kind!");
Richard Smith8186cd42017-04-26 22:10:53 +0000589 case InputKind::OpenCL:
Pavel Labath47168542017-04-27 08:49:19 +0000590 LangStd = LangStandard::lang_opencl10;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000591 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000592 case InputKind::CUDA:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000593 LangStd = LangStandard::lang_cuda;
594 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000595 case InputKind::Asm:
596 case InputKind::C:
597 case InputKind::ObjC:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000598 LangStd = LangStandard::lang_gnu99;
599 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000600 case InputKind::CXX:
601 case InputKind::ObjCXX:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602 LangStd = LangStandard::lang_gnucxx98;
603 break;
Benjamin Kramer0d97c222018-04-25 13:22:47 +0000604 case InputKind::HIP:
605 LangStd = LangStandard::lang_hip;
606 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000607 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000608 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000609
Kate Stoneb9c1b512016-09-06 20:57:50 +0000610 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
611 Opts.LineComment = Std.hasLineComments();
612 Opts.C99 = Std.isC99();
613 Opts.CPlusPlus = Std.isCPlusPlus();
614 Opts.CPlusPlus11 = Std.isCPlusPlus11();
615 Opts.Digraphs = Std.hasDigraphs();
616 Opts.GNUMode = Std.isGNUMode();
617 Opts.GNUInline = !Std.isC99();
618 Opts.HexFloats = Std.hasHexFloats();
619 Opts.ImplicitInt = Std.hasImplicitInt();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620
Kate Stoneb9c1b512016-09-06 20:57:50 +0000621 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623 // OpenCL has some additional defaults.
Pavel Labath47168542017-04-27 08:49:19 +0000624 if (LangStd == LangStandard::lang_opencl10) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625 Opts.OpenCL = 1;
626 Opts.AltiVec = 1;
627 Opts.CXXOperatorNames = 1;
628 Opts.LaxVectorConversions = 1;
629 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000630
Kate Stoneb9c1b512016-09-06 20:57:50 +0000631 // OpenCL and C++ both have bool, true, false keywords.
632 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000633
Kate Stoneb9c1b512016-09-06 20:57:50 +0000634 Opts.setValueVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635
Adrian Prantl05097242018-04-30 16:49:04 +0000636 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs is
637 // specified, or -std is set to a conforming mode.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000638 Opts.Trigraphs = !Opts.GNUMode;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000639 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000640 Opts.OptimizeSize = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000641
Kate Stoneb9c1b512016-09-06 20:57:50 +0000642 // FIXME: Eliminate this dependency.
643 // unsigned Opt =
644 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
645 // Opts.Optimize = Opt != 0;
646 unsigned Opt = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000647
Kate Stoneb9c1b512016-09-06 20:57:50 +0000648 // This is the __NO_INLINE__ define, which just depends on things like the
649 // optimization level and -fno-inline, not actually whether the backend has
650 // inlining enabled.
651 //
652 // FIXME: This is affected by other options (-fno-inline).
653 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000654}
655
Kate Stoneb9c1b512016-09-06 20:57:50 +0000656ClangASTContext::ClangASTContext(const char *target_triple)
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000657 : TypeSystem(TypeSystem::eKindClang), m_target_triple(), m_ast_up(),
658 m_language_options_up(), m_source_manager_up(), m_diagnostics_engine_up(),
659 m_target_options_rp(), m_target_info_up(), m_identifier_table_up(),
660 m_selector_table_up(), m_builtins_up(), m_callback_tag_decl(nullptr),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000661 m_callback_objc_decl(nullptr), m_callback_baton(nullptr),
662 m_pointer_byte_size(0), m_ast_owned(false) {
663 if (target_triple && target_triple[0])
664 SetTargetTriple(target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000665}
666
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000667// Destructor
Kate Stoneb9c1b512016-09-06 20:57:50 +0000668ClangASTContext::~ClangASTContext() { Finalize(); }
669
670ConstString ClangASTContext::GetPluginNameStatic() {
671 return ConstString("clang");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000672}
673
Kate Stoneb9c1b512016-09-06 20:57:50 +0000674ConstString ClangASTContext::GetPluginName() {
675 return ClangASTContext::GetPluginNameStatic();
Greg Clayton56939cb2015-09-17 22:23:34 +0000676}
677
Kate Stoneb9c1b512016-09-06 20:57:50 +0000678uint32_t ClangASTContext::GetPluginVersion() { return 1; }
Greg Clayton56939cb2015-09-17 22:23:34 +0000679
Kate Stoneb9c1b512016-09-06 20:57:50 +0000680lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
681 lldb_private::Module *module,
682 Target *target) {
683 if (ClangASTContextSupportsLanguage(language)) {
684 ArchSpec arch;
685 if (module)
686 arch = module->GetArchitecture();
687 else if (target)
688 arch = target->GetArchitecture();
Greg Clayton56939cb2015-09-17 22:23:34 +0000689
Kate Stoneb9c1b512016-09-06 20:57:50 +0000690 if (arch.IsValid()) {
691 ArchSpec fixed_arch = arch;
692 // LLVM wants this to be set to iOS or MacOSX; if we're working on
693 // a bare-boards type image, change the triple for llvm's benefit.
694 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
695 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) {
696 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
697 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
698 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) {
699 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
700 } else {
701 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
Greg Clayton56939cb2015-09-17 22:23:34 +0000702 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000703 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000704
Kate Stoneb9c1b512016-09-06 20:57:50 +0000705 if (module) {
706 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
707 if (ast_sp) {
708 ast_sp->SetArchitecture(fixed_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000709 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000710 return ast_sp;
711 } else if (target && target->IsValid()) {
712 std::shared_ptr<ClangASTContextForExpressions> ast_sp(
713 new ClangASTContextForExpressions(*target));
714 if (ast_sp) {
715 ast_sp->SetArchitecture(fixed_arch);
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000716 ast_sp->m_scratch_ast_source_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000717 new ClangASTSource(target->shared_from_this()));
Sean Callanan68e44232017-09-28 20:20:25 +0000718 lldbassert(ast_sp->getFileManager());
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000719 ast_sp->m_scratch_ast_source_up->InstallASTContext(
Sean Callanan68e44232017-09-28 20:20:25 +0000720 *ast_sp->getASTContext(), *ast_sp->getFileManager(), true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000721 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000722 ast_sp->m_scratch_ast_source_up->CreateProxy());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000723 ast_sp->SetExternalSource(proxy_ast_source);
724 return ast_sp;
725 }
726 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000728 }
729 return lldb::TypeSystemSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000730}
731
Kate Stoneb9c1b512016-09-06 20:57:50 +0000732void ClangASTContext::EnumerateSupportedLanguages(
733 std::set<lldb::LanguageType> &languages_for_types,
734 std::set<lldb::LanguageType> &languages_for_expressions) {
735 static std::vector<lldb::LanguageType> s_supported_languages_for_types(
736 {lldb::eLanguageTypeC89, lldb::eLanguageTypeC, lldb::eLanguageTypeC11,
737 lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeC99,
738 lldb::eLanguageTypeObjC, lldb::eLanguageTypeObjC_plus_plus,
739 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
740 lldb::eLanguageTypeC11, lldb::eLanguageTypeC_plus_plus_14});
741
742 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions(
743 {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC_plus_plus,
744 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
745 lldb::eLanguageTypeC_plus_plus_14});
746
747 languages_for_types.insert(s_supported_languages_for_types.begin(),
748 s_supported_languages_for_types.end());
749 languages_for_expressions.insert(
750 s_supported_languages_for_expressions.begin(),
751 s_supported_languages_for_expressions.end());
Enrico Granata5d84a692014-08-19 21:46:37 +0000752}
753
Kate Stoneb9c1b512016-09-06 20:57:50 +0000754void ClangASTContext::Initialize() {
755 PluginManager::RegisterPlugin(GetPluginNameStatic(),
756 "clang base AST context plug-in",
757 CreateInstance, EnumerateSupportedLanguages);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000758}
759
Kate Stoneb9c1b512016-09-06 20:57:50 +0000760void ClangASTContext::Terminate() {
761 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000762}
763
Kate Stoneb9c1b512016-09-06 20:57:50 +0000764void ClangASTContext::Finalize() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000765 if (m_ast_up) {
766 GetASTMap().Erase(m_ast_up.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000767 if (!m_ast_owned)
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000768 m_ast_up.release();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000769 }
770
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000771 m_builtins_up.reset();
772 m_selector_table_up.reset();
773 m_identifier_table_up.reset();
774 m_target_info_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000775 m_target_options_rp.reset();
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000776 m_diagnostics_engine_up.reset();
777 m_source_manager_up.reset();
778 m_language_options_up.reset();
779 m_ast_up.reset();
780 m_scratch_ast_source_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000781}
782
783void ClangASTContext::Clear() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000784 m_ast_up.reset();
785 m_language_options_up.reset();
786 m_source_manager_up.reset();
787 m_diagnostics_engine_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000788 m_target_options_rp.reset();
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000789 m_target_info_up.reset();
790 m_identifier_table_up.reset();
791 m_selector_table_up.reset();
792 m_builtins_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000793 m_pointer_byte_size = 0;
794}
795
Raphael Isemannf74a4c12019-04-30 08:41:35 +0000796void ClangASTContext::setSema(Sema *s) {
797 // Ensure that the new sema actually belongs to our ASTContext.
798 assert(s == nullptr || &s->getASTContext() == m_ast_up.get());
799 m_sema = s;
800}
801
Kate Stoneb9c1b512016-09-06 20:57:50 +0000802const char *ClangASTContext::GetTargetTriple() {
803 return m_target_triple.c_str();
804}
805
806void ClangASTContext::SetTargetTriple(const char *target_triple) {
807 Clear();
808 m_target_triple.assign(target_triple);
809}
810
811void ClangASTContext::SetArchitecture(const ArchSpec &arch) {
812 SetTargetTriple(arch.GetTriple().str().c_str());
813}
814
815bool ClangASTContext::HasExternalSource() {
816 ASTContext *ast = getASTContext();
817 if (ast)
818 return ast->getExternalSource() != nullptr;
819 return false;
820}
821
822void ClangASTContext::SetExternalSource(
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000823 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000824 ASTContext *ast = getASTContext();
825 if (ast) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000826 ast->setExternalSource(ast_source_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000827 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000828 }
829}
830
831void ClangASTContext::RemoveExternalSource() {
832 ASTContext *ast = getASTContext();
833
834 if (ast) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000835 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_up;
836 ast->setExternalSource(empty_ast_source_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000837 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000838 }
839}
840
841void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) {
842 if (!m_ast_owned) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000843 m_ast_up.release();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000844 }
845 m_ast_owned = false;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000846 m_ast_up.reset(ast_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000847 GetASTMap().Insert(ast_ctx, this);
848}
849
850ASTContext *ClangASTContext::getASTContext() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000851 if (m_ast_up == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000852 m_ast_owned = true;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000853 m_ast_up.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000854 *getIdentifierTable(), *getSelectorTable(),
855 *getBuiltinContext()));
856
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000857 m_ast_up->getDiagnostics().setClient(getDiagnosticConsumer(), false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000858
859 // This can be NULL if we don't know anything about the architecture or if
Adrian Prantl05097242018-04-30 16:49:04 +0000860 // the target for an architecture isn't enabled in the llvm/clang that we
861 // built
Kate Stoneb9c1b512016-09-06 20:57:50 +0000862 TargetInfo *target_info = getTargetInfo();
863 if (target_info)
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000864 m_ast_up->InitBuiltinTypes(*target_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000865
866 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000867 m_ast_up->getTranslationUnitDecl()->setHasExternalLexicalStorage();
868 // m_ast_up->getTranslationUnitDecl()->setHasExternalVisibleStorage();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000869 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000870
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000871 GetASTMap().Insert(m_ast_up.get(), this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000872
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000873 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_up(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000874 new ClangExternalASTSourceCallbacks(
875 ClangASTContext::CompleteTagDecl,
876 ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
877 ClangASTContext::LayoutRecordType, this));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000878 SetExternalSource(ast_source_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000879 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000880 return m_ast_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000881}
882
Kate Stoneb9c1b512016-09-06 20:57:50 +0000883ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
884 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
885 return clang_ast;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000886}
887
Kate Stoneb9c1b512016-09-06 20:57:50 +0000888Builtin::Context *ClangASTContext::getBuiltinContext() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000889 if (m_builtins_up == nullptr)
890 m_builtins_up.reset(new Builtin::Context());
891 return m_builtins_up.get();
Sean Callanan79439e82010-11-18 02:56:27 +0000892}
893
Kate Stoneb9c1b512016-09-06 20:57:50 +0000894IdentifierTable *ClangASTContext::getIdentifierTable() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000895 if (m_identifier_table_up == nullptr)
896 m_identifier_table_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000897 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000898 return m_identifier_table_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000899}
900
Kate Stoneb9c1b512016-09-06 20:57:50 +0000901LangOptions *ClangASTContext::getLanguageOptions() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000902 if (m_language_options_up == nullptr) {
903 m_language_options_up.reset(new LangOptions());
904 ParseLangArgs(*m_language_options_up, InputKind::ObjCXX, GetTargetTriple());
905 // InitializeLangOptions(*m_language_options_up, InputKind::ObjCXX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000906 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000907 return m_language_options_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000908}
909
Kate Stoneb9c1b512016-09-06 20:57:50 +0000910SelectorTable *ClangASTContext::getSelectorTable() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000911 if (m_selector_table_up == nullptr)
912 m_selector_table_up.reset(new SelectorTable());
913 return m_selector_table_up.get();
Greg Claytonfe689042015-11-10 17:47:04 +0000914}
915
Kate Stoneb9c1b512016-09-06 20:57:50 +0000916clang::FileManager *ClangASTContext::getFileManager() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000917 if (m_file_manager_up == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000918 clang::FileSystemOptions file_system_options;
Jonas Devlieghere9764b652019-02-18 20:31:18 +0000919 m_file_manager_up.reset(new clang::FileManager(
920 file_system_options, FileSystem::Instance().GetVirtualFileSystem()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000921 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000922 return m_file_manager_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000923}
924
925clang::SourceManager *ClangASTContext::getSourceManager() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000926 if (m_source_manager_up == nullptr)
927 m_source_manager_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000928 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000929 return m_source_manager_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000930}
931
932clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000933 if (m_diagnostics_engine_up == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000934 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000935 m_diagnostics_engine_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000936 new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
937 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000938 return m_diagnostics_engine_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000939}
940
941clang::MangleContext *ClangASTContext::getMangleContext() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000942 if (m_mangle_ctx_up == nullptr)
943 m_mangle_ctx_up.reset(getASTContext()->createMangleContext());
944 return m_mangle_ctx_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000945}
946
947class NullDiagnosticConsumer : public DiagnosticConsumer {
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000948public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000949 NullDiagnosticConsumer() {
950 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
951 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000952
Kate Stoneb9c1b512016-09-06 20:57:50 +0000953 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
Raphael Isemann17566302019-05-03 10:03:28 +0000954 const clang::Diagnostic &info) override {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000955 if (m_log) {
956 llvm::SmallVector<char, 32> diag_str(10);
957 info.FormatDiagnostic(diag_str);
958 diag_str.push_back('\0');
959 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000960 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000961 }
962
963 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
964 return new NullDiagnosticConsumer();
965 }
966
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000967private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000968 Log *m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000969};
970
Kate Stoneb9c1b512016-09-06 20:57:50 +0000971DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000972 if (m_diagnostic_consumer_up == nullptr)
973 m_diagnostic_consumer_up.reset(new NullDiagnosticConsumer);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000974
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000975 return m_diagnostic_consumer_up.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000976}
977
Kate Stoneb9c1b512016-09-06 20:57:50 +0000978std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000979 if (m_target_options_rp == nullptr && !m_target_triple.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000980 m_target_options_rp = std::make_shared<clang::TargetOptions>();
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000981 if (m_target_options_rp != nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000982 m_target_options_rp->Triple = m_target_triple;
983 }
984 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000985}
986
Kate Stoneb9c1b512016-09-06 20:57:50 +0000987TargetInfo *ClangASTContext::getTargetInfo() {
988 // target_triple should be something like "x86_64-apple-macosx"
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000989 if (m_target_info_up == nullptr && !m_target_triple.empty())
990 m_target_info_up.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000991 getTargetOptions()));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000992 return m_target_info_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000993}
994
995#pragma mark Basic Types
996
Kate Stoneb9c1b512016-09-06 20:57:50 +0000997static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
998 ASTContext *ast, QualType qual_type) {
999 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001000 return qual_type_bit_size == bit_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001001}
Greg Clayton56939cb2015-09-17 22:23:34 +00001002
Greg Claytona1e5dc82015-08-11 22:53:00 +00001003CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00001004ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
1005 size_t bit_size) {
1006 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
1007 getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001008}
1009
Kate Stoneb9c1b512016-09-06 20:57:50 +00001010CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
1011 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
1012 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001013 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001014 switch (encoding) {
1015 case eEncodingInvalid:
1016 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1017 return CompilerType(ast, ast->VoidPtrTy);
1018 break;
1019
1020 case eEncodingUint:
1021 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1022 return CompilerType(ast, ast->UnsignedCharTy);
1023 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1024 return CompilerType(ast, ast->UnsignedShortTy);
1025 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1026 return CompilerType(ast, ast->UnsignedIntTy);
1027 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1028 return CompilerType(ast, ast->UnsignedLongTy);
1029 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1030 return CompilerType(ast, ast->UnsignedLongLongTy);
1031 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1032 return CompilerType(ast, ast->UnsignedInt128Ty);
1033 break;
1034
1035 case eEncodingSint:
1036 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1037 return CompilerType(ast, ast->SignedCharTy);
1038 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1039 return CompilerType(ast, ast->ShortTy);
1040 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1041 return CompilerType(ast, ast->IntTy);
1042 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1043 return CompilerType(ast, ast->LongTy);
1044 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1045 return CompilerType(ast, ast->LongLongTy);
1046 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1047 return CompilerType(ast, ast->Int128Ty);
1048 break;
1049
1050 case eEncodingIEEE754:
1051 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1052 return CompilerType(ast, ast->FloatTy);
1053 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1054 return CompilerType(ast, ast->DoubleTy);
1055 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1056 return CompilerType(ast, ast->LongDoubleTy);
1057 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1058 return CompilerType(ast, ast->HalfTy);
1059 break;
1060
1061 case eEncodingVector:
1062 // Sanity check that bit_size is a multiple of 8's.
1063 if (bit_size && !(bit_size & 0x7u))
1064 return CompilerType(
1065 ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8));
1066 break;
1067 }
1068
1069 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001070}
1071
Greg Clayton57ee3062013-07-11 22:46:58 +00001072lldb::BasicType
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001073ClangASTContext::GetBasicTypeEnumeration(ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001074 if (name) {
1075 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
1076 static TypeNameToBasicTypeMap g_type_map;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +00001077 static llvm::once_flag g_once_flag;
1078 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001079 // "void"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001080 g_type_map.Append(ConstString("void"), eBasicTypeVoid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001081
1082 // "char"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001083 g_type_map.Append(ConstString("char"), eBasicTypeChar);
1084 g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar);
1085 g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar);
1086 g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar);
1087 g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar);
1088 g_type_map.Append(ConstString("unsigned wchar_t"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001089 eBasicTypeUnsignedWChar);
1090 // "short"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001091 g_type_map.Append(ConstString("short"), eBasicTypeShort);
1092 g_type_map.Append(ConstString("short int"), eBasicTypeShort);
1093 g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort);
1094 g_type_map.Append(ConstString("unsigned short int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001095 eBasicTypeUnsignedShort);
1096
1097 // "int"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001098 g_type_map.Append(ConstString("int"), eBasicTypeInt);
1099 g_type_map.Append(ConstString("signed int"), eBasicTypeInt);
1100 g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt);
1101 g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102
1103 // "long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001104 g_type_map.Append(ConstString("long"), eBasicTypeLong);
1105 g_type_map.Append(ConstString("long int"), eBasicTypeLong);
1106 g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong);
1107 g_type_map.Append(ConstString("unsigned long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001108 eBasicTypeUnsignedLong);
1109
1110 // "long long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001111 g_type_map.Append(ConstString("long long"), eBasicTypeLongLong);
1112 g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong);
1113 g_type_map.Append(ConstString("unsigned long long"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001114 eBasicTypeUnsignedLongLong);
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001115 g_type_map.Append(ConstString("unsigned long long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001116 eBasicTypeUnsignedLongLong);
1117
1118 // "int128"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001119 g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128);
1120 g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001121
1122 // Miscellaneous
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001123 g_type_map.Append(ConstString("bool"), eBasicTypeBool);
1124 g_type_map.Append(ConstString("float"), eBasicTypeFloat);
1125 g_type_map.Append(ConstString("double"), eBasicTypeDouble);
1126 g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble);
1127 g_type_map.Append(ConstString("id"), eBasicTypeObjCID);
1128 g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel);
1129 g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001130 g_type_map.Sort();
1131 });
1132
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001133 return g_type_map.Find(name, eBasicTypeInvalid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001134 }
1135 return eBasicTypeInvalid;
Greg Clayton57ee3062013-07-11 22:46:58 +00001136}
1137
Kate Stoneb9c1b512016-09-06 20:57:50 +00001138CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001139 ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001140 if (ast) {
1141 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
1142 return ClangASTContext::GetBasicType(ast, basic_type);
1143 }
1144 return CompilerType();
1145}
1146
1147uint32_t ClangASTContext::GetPointerByteSize() {
1148 if (m_pointer_byte_size == 0)
Adrian Prantld963a7c2019-01-15 18:07:52 +00001149 if (auto size = GetBasicType(lldb::eBasicTypeVoid)
1150 .GetPointerType()
1151 .GetByteSize(nullptr))
1152 m_pointer_byte_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001153 return m_pointer_byte_size;
1154}
1155
1156CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
1157 return GetBasicType(getASTContext(), basic_type);
1158}
1159
1160CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1161 lldb::BasicType basic_type) {
1162 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001163 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001164 lldb::opaque_compiler_type_t clang_type =
1165 GetOpaqueCompilerType(ast, basic_type);
1166
1167 if (clang_type)
1168 return CompilerType(GetASTContext(ast), clang_type);
1169 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +00001170}
1171
Kate Stoneb9c1b512016-09-06 20:57:50 +00001172CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
1173 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
1174 ASTContext *ast = getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001175
Kate Stoneb9c1b512016-09-06 20:57:50 +00001176#define streq(a, b) strcmp(a, b) == 0
1177 assert(ast != nullptr);
1178 if (ast) {
1179 switch (dw_ate) {
1180 default:
1181 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001182
Kate Stoneb9c1b512016-09-06 20:57:50 +00001183 case DW_ATE_address:
1184 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1185 return CompilerType(ast, ast->VoidPtrTy);
1186 break;
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001187
Kate Stoneb9c1b512016-09-06 20:57:50 +00001188 case DW_ATE_boolean:
1189 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
1190 return CompilerType(ast, ast->BoolTy);
1191 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1192 return CompilerType(ast, ast->UnsignedCharTy);
1193 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1194 return CompilerType(ast, ast->UnsignedShortTy);
1195 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1196 return CompilerType(ast, ast->UnsignedIntTy);
1197 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001198
Kate Stoneb9c1b512016-09-06 20:57:50 +00001199 case DW_ATE_lo_user:
1200 // This has been seen to mean DW_AT_complex_integer
1201 if (type_name) {
1202 if (::strstr(type_name, "complex")) {
1203 CompilerType complex_int_clang_type =
1204 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1205 bit_size / 2);
1206 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1207 complex_int_clang_type)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001208 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001209 }
1210 break;
1211
1212 case DW_ATE_complex_float:
1213 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
1214 return CompilerType(ast, ast->FloatComplexTy);
1215 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
1216 return CompilerType(ast, ast->DoubleComplexTy);
1217 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
1218 return CompilerType(ast, ast->LongDoubleComplexTy);
1219 else {
1220 CompilerType complex_float_clang_type =
1221 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1222 bit_size / 2);
1223 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1224 complex_float_clang_type)));
1225 }
1226 break;
1227
1228 case DW_ATE_float:
1229 if (streq(type_name, "float") &&
1230 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1231 return CompilerType(ast, ast->FloatTy);
1232 if (streq(type_name, "double") &&
1233 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1234 return CompilerType(ast, ast->DoubleTy);
1235 if (streq(type_name, "long double") &&
1236 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1237 return CompilerType(ast, ast->LongDoubleTy);
1238 // Fall back to not requiring a name match
1239 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1240 return CompilerType(ast, ast->FloatTy);
1241 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1242 return CompilerType(ast, ast->DoubleTy);
1243 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1244 return CompilerType(ast, ast->LongDoubleTy);
1245 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1246 return CompilerType(ast, ast->HalfTy);
1247 break;
1248
1249 case DW_ATE_signed:
1250 if (type_name) {
1251 if (streq(type_name, "wchar_t") &&
1252 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1253 (getTargetInfo() &&
1254 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1255 return CompilerType(ast, ast->WCharTy);
1256 if (streq(type_name, "void") &&
1257 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
1258 return CompilerType(ast, ast->VoidTy);
1259 if (strstr(type_name, "long long") &&
1260 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1261 return CompilerType(ast, ast->LongLongTy);
1262 if (strstr(type_name, "long") &&
1263 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1264 return CompilerType(ast, ast->LongTy);
1265 if (strstr(type_name, "short") &&
1266 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1267 return CompilerType(ast, ast->ShortTy);
1268 if (strstr(type_name, "char")) {
1269 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1270 return CompilerType(ast, ast->CharTy);
1271 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1272 return CompilerType(ast, ast->SignedCharTy);
1273 }
1274 if (strstr(type_name, "int")) {
1275 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1276 return CompilerType(ast, ast->IntTy);
1277 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1278 return CompilerType(ast, ast->Int128Ty);
1279 }
1280 }
1281 // We weren't able to match up a type name, just search by size
1282 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1283 return CompilerType(ast, ast->CharTy);
1284 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1285 return CompilerType(ast, ast->ShortTy);
1286 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1287 return CompilerType(ast, ast->IntTy);
1288 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1289 return CompilerType(ast, ast->LongTy);
1290 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1291 return CompilerType(ast, ast->LongLongTy);
1292 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1293 return CompilerType(ast, ast->Int128Ty);
1294 break;
1295
1296 case DW_ATE_signed_char:
1297 if (ast->getLangOpts().CharIsSigned && type_name &&
1298 streq(type_name, "char")) {
1299 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1300 return CompilerType(ast, ast->CharTy);
1301 }
1302 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1303 return CompilerType(ast, ast->SignedCharTy);
1304 break;
1305
1306 case DW_ATE_unsigned:
1307 if (type_name) {
1308 if (streq(type_name, "wchar_t")) {
1309 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1310 if (!(getTargetInfo() &&
1311 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1312 return CompilerType(ast, ast->WCharTy);
1313 }
1314 }
1315 if (strstr(type_name, "long long")) {
1316 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1317 return CompilerType(ast, ast->UnsignedLongLongTy);
1318 } else if (strstr(type_name, "long")) {
1319 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1320 return CompilerType(ast, ast->UnsignedLongTy);
1321 } else if (strstr(type_name, "short")) {
1322 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1323 return CompilerType(ast, ast->UnsignedShortTy);
1324 } else if (strstr(type_name, "char")) {
1325 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1326 return CompilerType(ast, ast->UnsignedCharTy);
1327 } else if (strstr(type_name, "int")) {
1328 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1329 return CompilerType(ast, ast->UnsignedIntTy);
1330 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1331 return CompilerType(ast, ast->UnsignedInt128Ty);
1332 }
1333 }
1334 // We weren't able to match up a type name, just search by size
1335 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1336 return CompilerType(ast, ast->UnsignedCharTy);
1337 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1338 return CompilerType(ast, ast->UnsignedShortTy);
1339 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1340 return CompilerType(ast, ast->UnsignedIntTy);
1341 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1342 return CompilerType(ast, ast->UnsignedLongTy);
1343 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1344 return CompilerType(ast, ast->UnsignedLongLongTy);
1345 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1346 return CompilerType(ast, ast->UnsignedInt128Ty);
1347 break;
1348
1349 case DW_ATE_unsigned_char:
1350 if (!ast->getLangOpts().CharIsSigned && type_name &&
1351 streq(type_name, "char")) {
1352 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1353 return CompilerType(ast, ast->CharTy);
1354 }
1355 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1356 return CompilerType(ast, ast->UnsignedCharTy);
1357 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1358 return CompilerType(ast, ast->UnsignedShortTy);
1359 break;
1360
1361 case DW_ATE_imaginary_float:
1362 break;
1363
1364 case DW_ATE_UTF:
1365 if (type_name) {
1366 if (streq(type_name, "char16_t")) {
1367 return CompilerType(ast, ast->Char16Ty);
1368 } else if (streq(type_name, "char32_t")) {
1369 return CompilerType(ast, ast->Char32Ty);
1370 }
1371 }
1372 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001373 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001374 }
1375 // This assert should fire for anything that we don't catch above so we know
1376 // to fix any issues we run into.
1377 if (type_name) {
1378 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1379 "DW_TAG_base_type '%s' encoded with "
1380 "DW_ATE = 0x%x, bit_size = %u\n",
1381 type_name, dw_ate, bit_size);
1382 } else {
1383 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1384 "DW_TAG_base_type encoded with "
1385 "DW_ATE = 0x%x, bit_size = %u\n",
1386 dw_ate, bit_size);
1387 }
1388 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001389}
1390
Kate Stoneb9c1b512016-09-06 20:57:50 +00001391CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) {
1392 if (ast)
1393 return CompilerType(ast, ast->UnknownAnyTy);
1394 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001395}
1396
Kate Stoneb9c1b512016-09-06 20:57:50 +00001397CompilerType ClangASTContext::GetCStringType(bool is_const) {
1398 ASTContext *ast = getASTContext();
1399 QualType char_type(ast->CharTy);
1400
1401 if (is_const)
1402 char_type.addConst();
1403
1404 return CompilerType(ast, ast->getPointerType(char_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001405}
1406
Zachary Turner115209e2018-11-05 19:25:39 +00001407clang::DeclContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001408ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1409 return ast->getTranslationUnitDecl();
Sean Callanan09ab4b72011-11-30 22:11:59 +00001410}
1411
Kate Stoneb9c1b512016-09-06 20:57:50 +00001412clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1413 clang::Decl *source_decl) {
1414 FileSystemOptions file_system_options;
1415 FileManager file_manager(file_system_options);
1416 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1417
Gabor Marton5ac6d492019-05-15 10:29:48 +00001418 if (llvm::Expected<clang::Decl *> ret_or_error =
1419 importer.Import(source_decl)) {
1420 return *ret_or_error;
1421 } else {
1422 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
1423 LLDB_LOG_ERROR(log, ret_or_error.takeError(), "Couldn't import decl: {0}");
1424 return nullptr;
1425 }
Greg Clayton526e5af2010-11-13 03:52:47 +00001426}
1427
Kate Stoneb9c1b512016-09-06 20:57:50 +00001428bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1429 bool ignore_qualifiers) {
1430 ClangASTContext *ast =
1431 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1432 if (!ast || ast != type2.GetTypeSystem())
1433 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001434
Kate Stoneb9c1b512016-09-06 20:57:50 +00001435 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1436 return true;
Greg Clayton55995eb2012-04-06 17:38:55 +00001437
Kate Stoneb9c1b512016-09-06 20:57:50 +00001438 QualType type1_qual = ClangUtil::GetQualType(type1);
1439 QualType type2_qual = ClangUtil::GetQualType(type2);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001440
Kate Stoneb9c1b512016-09-06 20:57:50 +00001441 if (ignore_qualifiers) {
1442 type1_qual = type1_qual.getUnqualifiedType();
1443 type2_qual = type2_qual.getUnqualifiedType();
1444 }
1445
1446 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001447}
1448
Kate Stoneb9c1b512016-09-06 20:57:50 +00001449CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1450 if (clang::ObjCInterfaceDecl *interface_decl =
1451 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1452 return GetTypeForDecl(interface_decl);
1453 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1454 return GetTypeForDecl(tag_decl);
1455 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001456}
1457
Kate Stoneb9c1b512016-09-06 20:57:50 +00001458CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001459 // No need to call the getASTContext() accessor (which can create the AST if
1460 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001461 // AST if our AST didn't already exist...
1462 ASTContext *ast = &decl->getASTContext();
1463 if (ast)
1464 return CompilerType(ast, ast->getTagDeclType(decl));
1465 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001466}
1467
Kate Stoneb9c1b512016-09-06 20:57:50 +00001468CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001469 // No need to call the getASTContext() accessor (which can create the AST if
1470 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001471 // AST if our AST didn't already exist...
1472 ASTContext *ast = &decl->getASTContext();
1473 if (ast)
1474 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1475 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001476}
1477
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001478#pragma mark Structure, Unions, Classes
1479
Kate Stoneb9c1b512016-09-06 20:57:50 +00001480CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx,
1481 AccessType access_type,
1482 const char *name, int kind,
1483 LanguageType language,
1484 ClangASTMetadata *metadata) {
1485 ASTContext *ast = getASTContext();
1486 assert(ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001487
Kate Stoneb9c1b512016-09-06 20:57:50 +00001488 if (decl_ctx == nullptr)
1489 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton9e409562010-07-28 02:04:09 +00001490
Kate Stoneb9c1b512016-09-06 20:57:50 +00001491 if (language == eLanguageTypeObjC ||
1492 language == eLanguageTypeObjC_plus_plus) {
1493 bool isForwardDecl = true;
1494 bool isInternal = false;
1495 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1496 }
Greg Clayton9e409562010-07-28 02:04:09 +00001497
Kate Stoneb9c1b512016-09-06 20:57:50 +00001498 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
Adrian Prantl05097242018-04-30 16:49:04 +00001499 // we will need to update this code. I was told to currently always use the
1500 // CXXRecordDecl class since we often don't know from debug information if
1501 // something is struct or a class, so we default to always use the more
Kate Stoneb9c1b512016-09-06 20:57:50 +00001502 // complete definition just in case.
Greg Claytonc4ffd662013-03-08 01:37:30 +00001503
Kate Stoneb9c1b512016-09-06 20:57:50 +00001504 bool is_anonymous = (!name) || (!name[0]);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001505
Kate Stoneb9c1b512016-09-06 20:57:50 +00001506 CXXRecordDecl *decl = CXXRecordDecl::Create(
1507 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1508 SourceLocation(), is_anonymous ? nullptr : &ast->Idents.get(name));
1509
1510 if (is_anonymous)
1511 decl->setAnonymousStructOrUnion(true);
1512
1513 if (decl) {
1514 if (metadata)
1515 SetMetadata(ast, decl, *metadata);
1516
1517 if (access_type != eAccessNone)
1518 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1519
1520 if (decl_ctx)
1521 decl_ctx->addDecl(decl);
1522
1523 return CompilerType(ast, ast->getTagDeclType(decl));
1524 }
1525 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001526}
1527
Sean Callanan09e91ac2017-05-11 22:08:05 +00001528namespace {
1529 bool IsValueParam(const clang::TemplateArgument &argument) {
1530 return argument.getKind() == TemplateArgument::Integral;
1531 }
1532}
1533
Kate Stoneb9c1b512016-09-06 20:57:50 +00001534static TemplateParameterList *CreateTemplateParameterList(
1535 ASTContext *ast,
1536 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1537 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1538 const bool parameter_pack = false;
1539 const bool is_typename = false;
1540 const unsigned depth = 0;
Sean Callanan09e91ac2017-05-11 22:08:05 +00001541 const size_t num_template_params = template_param_infos.args.size();
1542 DeclContext *const decl_context =
1543 ast->getTranslationUnitDecl(); // Is this the right decl context?,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001544 for (size_t i = 0; i < num_template_params; ++i) {
1545 const char *name = template_param_infos.names[i];
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001546
Kate Stoneb9c1b512016-09-06 20:57:50 +00001547 IdentifierInfo *identifier_info = nullptr;
1548 if (name && name[0])
1549 identifier_info = &ast->Idents.get(name);
Sean Callanan09e91ac2017-05-11 22:08:05 +00001550 if (IsValueParam(template_param_infos.args[i])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001551 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001552 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001553 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1554 template_param_infos.args[i].getIntegralType(), parameter_pack,
1555 nullptr));
1556
1557 } else {
1558 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001559 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001560 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1561 is_typename, parameter_pack));
1562 }
1563 }
Eugene Zemtsova9d928c2017-09-29 03:15:08 +00001564
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001565 if (template_param_infos.packed_args) {
Sean Callanan09e91ac2017-05-11 22:08:05 +00001566 IdentifierInfo *identifier_info = nullptr;
1567 if (template_param_infos.pack_name && template_param_infos.pack_name[0])
1568 identifier_info = &ast->Idents.get(template_param_infos.pack_name);
1569 const bool parameter_pack_true = true;
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001570
1571 if (!template_param_infos.packed_args->args.empty() &&
1572 IsValueParam(template_param_infos.packed_args->args[0])) {
Sean Callanan09e91ac2017-05-11 22:08:05 +00001573 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001574 *ast, decl_context, SourceLocation(), SourceLocation(), depth,
1575 num_template_params, identifier_info,
Sean Callanan09e91ac2017-05-11 22:08:05 +00001576 template_param_infos.packed_args->args[0].getIntegralType(),
1577 parameter_pack_true, nullptr));
1578 } else {
1579 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001580 *ast, decl_context, SourceLocation(), SourceLocation(), depth,
1581 num_template_params, identifier_info, is_typename,
1582 parameter_pack_true));
Sean Callanan09e91ac2017-05-11 22:08:05 +00001583 }
1584 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001585 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1586 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1587 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1588 SourceLocation(), requires_clause);
1589 return template_param_list;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001590}
1591
Kate Stoneb9c1b512016-09-06 20:57:50 +00001592clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1593 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1594 const char *name, const TemplateParameterInfos &template_param_infos) {
Adrian Prantld8f460e2018-05-02 16:55:16 +00001595 // /// Create a function template node.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001596 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001597
Kate Stoneb9c1b512016-09-06 20:57:50 +00001598 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001599
Kate Stoneb9c1b512016-09-06 20:57:50 +00001600 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1601 ast, template_param_infos, template_param_decls);
1602 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1603 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1604 template_param_list, func_decl);
1605
1606 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1607 i < template_param_decl_count; ++i) {
1608 // TODO: verify which decl context we should put template_param_decls into..
1609 template_param_decls[i]->setDeclContext(func_decl);
1610 }
1611
1612 return func_tmpl_decl;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001613}
1614
Kate Stoneb9c1b512016-09-06 20:57:50 +00001615void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1616 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1617 const TemplateParameterInfos &infos) {
1618 TemplateArgumentList template_args(TemplateArgumentList::OnStack, infos.args);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001619
Kate Stoneb9c1b512016-09-06 20:57:50 +00001620 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, &template_args,
1621 nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001622}
1623
Kate Stoneb9c1b512016-09-06 20:57:50 +00001624ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1625 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1626 int kind, const TemplateParameterInfos &template_param_infos) {
1627 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001628
Kate Stoneb9c1b512016-09-06 20:57:50 +00001629 ClassTemplateDecl *class_template_decl = nullptr;
1630 if (decl_ctx == nullptr)
1631 decl_ctx = ast->getTranslationUnitDecl();
Greg Claytonf0705c82011-10-22 03:33:13 +00001632
Kate Stoneb9c1b512016-09-06 20:57:50 +00001633 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1634 DeclarationName decl_name(&identifier_info);
Greg Claytonf0705c82011-10-22 03:33:13 +00001635
Kate Stoneb9c1b512016-09-06 20:57:50 +00001636 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Greg Claytonf0705c82011-10-22 03:33:13 +00001637
Kate Stoneb9c1b512016-09-06 20:57:50 +00001638 for (NamedDecl *decl : result) {
1639 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001640 if (class_template_decl)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001641 return class_template_decl;
1642 }
1643
1644 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1645
1646 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1647 ast, template_param_infos, template_param_decls);
1648
1649 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1650 *ast, (TagDecl::TagKind)kind,
1651 decl_ctx, // What decl context do we use here? TU? The actual decl
1652 // context?
1653 SourceLocation(), SourceLocation(), &identifier_info);
1654
1655 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1656 i < template_param_decl_count; ++i) {
1657 template_param_decls[i]->setDeclContext(template_cxx_decl);
1658 }
1659
1660 // With templated classes, we say that a class is templated with
1661 // specializations, but that the bare class has no functions.
1662 // template_cxx_decl->startDefinition();
1663 // template_cxx_decl->completeDefinition();
1664
1665 class_template_decl = ClassTemplateDecl::Create(
1666 *ast,
1667 decl_ctx, // What decl context do we use here? TU? The actual decl
1668 // context?
Pavel Labath4294de32017-01-12 10:44:16 +00001669 SourceLocation(), decl_name, template_param_list, template_cxx_decl);
Richard Smith35b007e2019-02-15 21:48:09 +00001670 template_cxx_decl->setDescribedClassTemplate(class_template_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001671
1672 if (class_template_decl) {
1673 if (access_type != eAccessNone)
1674 class_template_decl->setAccess(
1675 ConvertAccessTypeToAccessSpecifier(access_type));
1676
1677 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1678 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1679
1680 decl_ctx->addDecl(class_template_decl);
1681
Sean Callanan5e9e1992011-10-26 01:06:27 +00001682#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001683 VerifyDecl(class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001684#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001685 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001686
Kate Stoneb9c1b512016-09-06 20:57:50 +00001687 return class_template_decl;
Greg Claytonf0705c82011-10-22 03:33:13 +00001688}
1689
Frederic Rissf4e7e522018-04-02 16:18:32 +00001690TemplateTemplateParmDecl *
1691ClangASTContext::CreateTemplateTemplateParmDecl(const char *template_name) {
1692 ASTContext *ast = getASTContext();
1693
1694 auto *decl_ctx = ast->getTranslationUnitDecl();
1695
1696 IdentifierInfo &identifier_info = ast->Idents.get(template_name);
1697 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1698
1699 ClangASTContext::TemplateParameterInfos template_param_infos;
1700 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1701 ast, template_param_infos, template_param_decls);
1702
1703 // LLDB needs to create those decls only to be able to display a
Adrian Prantl05097242018-04-30 16:49:04 +00001704 // type that includes a template template argument. Only the name matters for
1705 // this purpose, so we use dummy values for the other characterisitcs of the
1706 // type.
Frederic Rissf4e7e522018-04-02 16:18:32 +00001707 return TemplateTemplateParmDecl::Create(
1708 *ast, decl_ctx, SourceLocation(),
1709 /*Depth*/ 0, /*Position*/ 0,
1710 /*IsParameterPack*/ false, &identifier_info, template_param_list);
1711}
1712
Greg Claytonf0705c82011-10-22 03:33:13 +00001713ClassTemplateSpecializationDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001714ClangASTContext::CreateClassTemplateSpecializationDecl(
1715 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1716 const TemplateParameterInfos &template_param_infos) {
1717 ASTContext *ast = getASTContext();
Sean Callanan09e91ac2017-05-11 22:08:05 +00001718 llvm::SmallVector<clang::TemplateArgument, 2> args(
1719 template_param_infos.args.size() +
1720 (template_param_infos.packed_args ? 1 : 0));
1721 std::copy(template_param_infos.args.begin(), template_param_infos.args.end(),
1722 args.begin());
1723 if (template_param_infos.packed_args) {
1724 args[args.size() - 1] = TemplateArgument::CreatePackCopy(
1725 *ast, template_param_infos.packed_args->args);
1726 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001727 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1728 ClassTemplateSpecializationDecl::Create(
1729 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
Sean Callanan09e91ac2017-05-11 22:08:05 +00001730 SourceLocation(), class_template_decl, args,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001731 nullptr);
1732
1733 class_template_specialization_decl->setSpecializationKind(
1734 TSK_ExplicitSpecialization);
1735
1736 return class_template_specialization_decl;
1737}
1738
1739CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1740 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1741 if (class_template_specialization_decl) {
Greg Claytonf0705c82011-10-22 03:33:13 +00001742 ASTContext *ast = getASTContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001743 if (ast)
1744 return CompilerType(
1745 ast, ast->getTagDeclType(class_template_specialization_decl));
1746 }
1747 return CompilerType();
Greg Claytonf0705c82011-10-22 03:33:13 +00001748}
1749
Kate Stoneb9c1b512016-09-06 20:57:50 +00001750static inline bool check_op_param(bool is_method,
1751 clang::OverloadedOperatorKind op_kind,
1752 bool unary, bool binary,
1753 uint32_t num_params) {
1754 // Special-case call since it can take any number of operands
1755 if (op_kind == OO_Call)
1756 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001757
Kate Stoneb9c1b512016-09-06 20:57:50 +00001758 // The parameter count doesn't include "this"
1759 if (is_method)
1760 ++num_params;
1761 if (num_params == 1)
1762 return unary;
1763 if (num_params == 2)
1764 return binary;
1765 else
Greg Clayton090d0982011-06-19 03:43:27 +00001766 return false;
1767}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001768
Kate Stoneb9c1b512016-09-06 20:57:50 +00001769bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1770 bool is_method, clang::OverloadedOperatorKind op_kind,
1771 uint32_t num_params) {
1772 switch (op_kind) {
1773 default:
1774 break;
1775 // C++ standard allows any number of arguments to new/delete
1776 case OO_New:
1777 case OO_Array_New:
1778 case OO_Delete:
1779 case OO_Array_Delete:
1780 return true;
1781 }
Pavel Labath1ac2b202016-08-15 14:32:32 +00001782
Kate Stoneb9c1b512016-09-06 20:57:50 +00001783#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1784 case OO_##Name: \
1785 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1786 switch (op_kind) {
Greg Clayton090d0982011-06-19 03:43:27 +00001787#include "clang/Basic/OperatorKinds.def"
Kate Stoneb9c1b512016-09-06 20:57:50 +00001788 default:
1789 break;
1790 }
1791 return false;
Greg Clayton090d0982011-06-19 03:43:27 +00001792}
1793
Greg Clayton57ee3062013-07-11 22:46:58 +00001794clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +00001795ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1796 clang::AccessSpecifier rhs) {
1797 // Make the access equal to the stricter of the field and the nested field's
1798 // access
1799 if (lhs == AS_none || rhs == AS_none)
1800 return AS_none;
1801 if (lhs == AS_private || rhs == AS_private)
1802 return AS_private;
1803 if (lhs == AS_protected || rhs == AS_protected)
1804 return AS_protected;
1805 return AS_public;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001806}
1807
Kate Stoneb9c1b512016-09-06 20:57:50 +00001808bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1809 uint32_t &bitfield_bit_size) {
1810 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001811}
1812
Kate Stoneb9c1b512016-09-06 20:57:50 +00001813bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field,
1814 uint32_t &bitfield_bit_size) {
1815 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001816 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001817
Kate Stoneb9c1b512016-09-06 20:57:50 +00001818 if (field->isBitField()) {
1819 Expr *bit_width_expr = field->getBitWidth();
1820 if (bit_width_expr) {
1821 llvm::APSInt bit_width_apsint;
1822 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1823 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001824 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001825 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001826 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001827 }
1828 return false;
1829}
1830
1831bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1832 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001833 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001834
1835 if (!record_decl->field_empty())
1836 return true;
1837
1838 // No fields, lets check this is a CXX record and check the base classes
1839 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1840 if (cxx_record_decl) {
1841 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1842 for (base_class = cxx_record_decl->bases_begin(),
1843 base_class_end = cxx_record_decl->bases_end();
1844 base_class != base_class_end; ++base_class) {
1845 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1846 base_class->getType()->getAs<RecordType>()->getDecl());
1847 if (RecordHasFields(base_class_decl))
1848 return true;
1849 }
1850 }
1851 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001852}
1853
Adrian Prantl4e8be2c2018-06-13 16:21:24 +00001854#pragma mark Objective-C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001855
Kate Stoneb9c1b512016-09-06 20:57:50 +00001856CompilerType ClangASTContext::CreateObjCClass(const char *name,
1857 DeclContext *decl_ctx,
1858 bool isForwardDecl,
1859 bool isInternal,
1860 ClangASTMetadata *metadata) {
1861 ASTContext *ast = getASTContext();
1862 assert(ast != nullptr);
1863 assert(name && name[0]);
1864 if (decl_ctx == nullptr)
1865 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001866
Kate Stoneb9c1b512016-09-06 20:57:50 +00001867 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1868 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1869 nullptr, SourceLocation(),
1870 /*isForwardDecl,*/
1871 isInternal);
1872
1873 if (decl && metadata)
1874 SetMetadata(ast, decl, *metadata);
1875
1876 return CompilerType(ast, ast->getObjCInterfaceType(decl));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001877}
1878
Kate Stoneb9c1b512016-09-06 20:57:50 +00001879static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001880 return !ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001881}
1882
Greg Clayton57ee3062013-07-11 22:46:58 +00001883uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001884ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1885 bool omit_empty_base_classes) {
1886 uint32_t num_bases = 0;
1887 if (cxx_record_decl) {
1888 if (omit_empty_base_classes) {
1889 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1890 for (base_class = cxx_record_decl->bases_begin(),
1891 base_class_end = cxx_record_decl->bases_end();
1892 base_class != base_class_end; ++base_class) {
1893 // Skip empty base classes
1894 if (omit_empty_base_classes) {
1895 if (BaseSpecifierIsEmpty(base_class))
1896 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001897 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001898 ++num_bases;
1899 }
1900 } else
1901 num_bases = cxx_record_decl->getNumBases();
1902 }
1903 return num_bases;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001904}
1905
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001906#pragma mark Namespace Declarations
1907
Raphael Isemanna9469972019-03-12 07:45:04 +00001908NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1909 const char *name, DeclContext *decl_ctx, bool is_inline) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001910 NamespaceDecl *namespace_decl = nullptr;
1911 ASTContext *ast = getASTContext();
1912 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1913 if (decl_ctx == nullptr)
1914 decl_ctx = translation_unit_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00001915
Kate Stoneb9c1b512016-09-06 20:57:50 +00001916 if (name) {
1917 IdentifierInfo &identifier_info = ast->Idents.get(name);
1918 DeclarationName decl_name(&identifier_info);
1919 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1920 for (NamedDecl *decl : result) {
1921 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1922 if (namespace_decl)
1923 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001924 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001925
1926 namespace_decl =
Raphael Isemanna9469972019-03-12 07:45:04 +00001927 NamespaceDecl::Create(*ast, decl_ctx, is_inline, SourceLocation(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001928 SourceLocation(), &identifier_info, nullptr);
1929
1930 decl_ctx->addDecl(namespace_decl);
1931 } else {
1932 if (decl_ctx == translation_unit_decl) {
1933 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1934 if (namespace_decl)
1935 return namespace_decl;
1936
1937 namespace_decl =
1938 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1939 SourceLocation(), nullptr, nullptr);
1940 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1941 translation_unit_decl->addDecl(namespace_decl);
1942 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
1943 } else {
1944 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1945 if (parent_namespace_decl) {
1946 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1947 if (namespace_decl)
1948 return namespace_decl;
1949 namespace_decl =
1950 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1951 SourceLocation(), nullptr, nullptr);
1952 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1953 parent_namespace_decl->addDecl(namespace_decl);
1954 assert(namespace_decl ==
1955 parent_namespace_decl->getAnonymousNamespace());
1956 } else {
1957 // BAD!!!
1958 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001959 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001960 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001961#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001962 VerifyDecl(namespace_decl);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001963#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001964 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001965}
1966
Kate Stoneb9c1b512016-09-06 20:57:50 +00001967NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
Raphael Isemanna9469972019-03-12 07:45:04 +00001968 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx,
1969 bool is_inline) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001970 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1971 if (ast_ctx == nullptr)
1972 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +00001973
Raphael Isemanna9469972019-03-12 07:45:04 +00001974 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx, is_inline);
Siva Chandra03ff5c82016-02-05 19:10:04 +00001975}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001976
Paul Hermand628cbb2015-09-15 23:44:17 +00001977clang::BlockDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001978ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1979 if (ctx != nullptr) {
1980 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1981 clang::SourceLocation());
1982 ctx->addDecl(decl);
1983 return decl;
1984 }
1985 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001986}
1987
Kate Stoneb9c1b512016-09-06 20:57:50 +00001988clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1989 clang::DeclContext *right,
1990 clang::DeclContext *root) {
1991 if (root == nullptr)
Paul Hermanea188fc2015-09-16 18:48:30 +00001992 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001993
1994 std::set<clang::DeclContext *> path_left;
1995 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1996 path_left.insert(d);
1997
1998 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1999 if (path_left.find(d) != path_left.end())
2000 return d;
2001
2002 return nullptr;
Paul Hermanea188fc2015-09-16 18:48:30 +00002003}
2004
Kate Stoneb9c1b512016-09-06 20:57:50 +00002005clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
2006 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
2007 if (decl_ctx != nullptr && ns_decl != nullptr) {
2008 clang::TranslationUnitDecl *translation_unit =
2009 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
2010 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
2011 *getASTContext(), decl_ctx, clang::SourceLocation(),
2012 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
2013 clang::SourceLocation(), ns_decl,
2014 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
2015 decl_ctx->addDecl(using_decl);
2016 return using_decl;
2017 }
2018 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002019}
2020
2021clang::UsingDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00002022ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
2023 clang::NamedDecl *target) {
2024 if (current_decl_ctx != nullptr && target != nullptr) {
2025 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
2026 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
2027 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
2028 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
2029 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
2030 target);
2031 using_decl->addShadowDecl(shadow_decl);
2032 current_decl_ctx->addDecl(using_decl);
2033 return using_decl;
2034 }
2035 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002036}
2037
Kate Stoneb9c1b512016-09-06 20:57:50 +00002038clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
2039 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
2040 if (decl_context != nullptr) {
2041 clang::VarDecl *var_decl = clang::VarDecl::Create(
2042 *getASTContext(), decl_context, clang::SourceLocation(),
2043 clang::SourceLocation(),
2044 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
2045 nullptr, clang::SC_None);
2046 var_decl->setAccess(clang::AS_public);
2047 decl_context->addDecl(var_decl);
2048 return var_decl;
2049 }
2050 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002051}
2052
Zachary Turner9d8a97e2016-04-01 23:20:35 +00002053lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00002054ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
2055 lldb::BasicType basic_type) {
2056 switch (basic_type) {
2057 case eBasicTypeVoid:
2058 return ast->VoidTy.getAsOpaquePtr();
2059 case eBasicTypeChar:
2060 return ast->CharTy.getAsOpaquePtr();
2061 case eBasicTypeSignedChar:
2062 return ast->SignedCharTy.getAsOpaquePtr();
2063 case eBasicTypeUnsignedChar:
2064 return ast->UnsignedCharTy.getAsOpaquePtr();
2065 case eBasicTypeWChar:
2066 return ast->getWCharType().getAsOpaquePtr();
2067 case eBasicTypeSignedWChar:
2068 return ast->getSignedWCharType().getAsOpaquePtr();
2069 case eBasicTypeUnsignedWChar:
2070 return ast->getUnsignedWCharType().getAsOpaquePtr();
2071 case eBasicTypeChar16:
2072 return ast->Char16Ty.getAsOpaquePtr();
2073 case eBasicTypeChar32:
2074 return ast->Char32Ty.getAsOpaquePtr();
2075 case eBasicTypeShort:
2076 return ast->ShortTy.getAsOpaquePtr();
2077 case eBasicTypeUnsignedShort:
2078 return ast->UnsignedShortTy.getAsOpaquePtr();
2079 case eBasicTypeInt:
2080 return ast->IntTy.getAsOpaquePtr();
2081 case eBasicTypeUnsignedInt:
2082 return ast->UnsignedIntTy.getAsOpaquePtr();
2083 case eBasicTypeLong:
2084 return ast->LongTy.getAsOpaquePtr();
2085 case eBasicTypeUnsignedLong:
2086 return ast->UnsignedLongTy.getAsOpaquePtr();
2087 case eBasicTypeLongLong:
2088 return ast->LongLongTy.getAsOpaquePtr();
2089 case eBasicTypeUnsignedLongLong:
2090 return ast->UnsignedLongLongTy.getAsOpaquePtr();
2091 case eBasicTypeInt128:
2092 return ast->Int128Ty.getAsOpaquePtr();
2093 case eBasicTypeUnsignedInt128:
2094 return ast->UnsignedInt128Ty.getAsOpaquePtr();
2095 case eBasicTypeBool:
2096 return ast->BoolTy.getAsOpaquePtr();
2097 case eBasicTypeHalf:
2098 return ast->HalfTy.getAsOpaquePtr();
2099 case eBasicTypeFloat:
2100 return ast->FloatTy.getAsOpaquePtr();
2101 case eBasicTypeDouble:
2102 return ast->DoubleTy.getAsOpaquePtr();
2103 case eBasicTypeLongDouble:
2104 return ast->LongDoubleTy.getAsOpaquePtr();
2105 case eBasicTypeFloatComplex:
2106 return ast->FloatComplexTy.getAsOpaquePtr();
2107 case eBasicTypeDoubleComplex:
2108 return ast->DoubleComplexTy.getAsOpaquePtr();
2109 case eBasicTypeLongDoubleComplex:
2110 return ast->LongDoubleComplexTy.getAsOpaquePtr();
2111 case eBasicTypeObjCID:
2112 return ast->getObjCIdType().getAsOpaquePtr();
2113 case eBasicTypeObjCClass:
2114 return ast->getObjCClassType().getAsOpaquePtr();
2115 case eBasicTypeObjCSel:
2116 return ast->getObjCSelType().getAsOpaquePtr();
2117 case eBasicTypeNullPtr:
2118 return ast->NullPtrTy.getAsOpaquePtr();
2119 default:
2120 return nullptr;
2121 }
Zachary Turner9d8a97e2016-04-01 23:20:35 +00002122}
2123
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002124#pragma mark Function Types
2125
Pavel Labath1ac2b202016-08-15 14:32:32 +00002126clang::DeclarationName
Kate Stoneb9c1b512016-09-06 20:57:50 +00002127ClangASTContext::GetDeclarationName(const char *name,
2128 const CompilerType &function_clang_type) {
2129 if (!name || !name[0])
2130 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002131
Kate Stoneb9c1b512016-09-06 20:57:50 +00002132 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
2133 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
2134 return DeclarationName(&getASTContext()->Idents.get(
2135 name)); // Not operator, but a regular function.
Pavel Labath1ac2b202016-08-15 14:32:32 +00002136
Adrian Prantl05097242018-04-30 16:49:04 +00002137 // Check the number of operator parameters. Sometimes we have seen bad DWARF
2138 // that doesn't correctly describe operators and if we try to create a method
2139 // and add it to the class, clang will assert and crash, so we need to make
2140 // sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002141 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
2142 const clang::FunctionProtoType *function_type =
2143 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
2144 if (function_type == nullptr)
2145 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002146
Kate Stoneb9c1b512016-09-06 20:57:50 +00002147 const bool is_method = false;
2148 const unsigned int num_params = function_type->getNumParams();
2149 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
2150 is_method, op_kind, num_params))
2151 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002152
Kate Stoneb9c1b512016-09-06 20:57:50 +00002153 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
Pavel Labath1ac2b202016-08-15 14:32:32 +00002154}
2155
Kate Stoneb9c1b512016-09-06 20:57:50 +00002156FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
2157 DeclContext *decl_ctx, const char *name,
2158 const CompilerType &function_clang_type, int storage, bool is_inline) {
2159 FunctionDecl *func_decl = nullptr;
2160 ASTContext *ast = getASTContext();
2161 if (decl_ctx == nullptr)
2162 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002163
Kate Stoneb9c1b512016-09-06 20:57:50 +00002164 const bool hasWrittenPrototype = true;
2165 const bool isConstexprSpecified = false;
Greg Clayton0d551042013-06-28 21:08:47 +00002166
Kate Stoneb9c1b512016-09-06 20:57:50 +00002167 clang::DeclarationName declarationName =
2168 GetDeclarationName(name, function_clang_type);
2169 func_decl = FunctionDecl::Create(
2170 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
2171 ClangUtil::GetQualType(function_clang_type), nullptr,
2172 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
2173 isConstexprSpecified);
2174 if (func_decl)
2175 decl_ctx->addDecl(func_decl);
2176
Sean Callanan5e9e1992011-10-26 01:06:27 +00002177#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00002178 VerifyDecl(func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002179#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002180
2181 return func_decl;
2182}
2183
2184CompilerType ClangASTContext::CreateFunctionType(
2185 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002186 unsigned num_args, bool is_variadic, unsigned type_quals,
2187 clang::CallingConv cc) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002188 if (ast == nullptr)
2189 return CompilerType(); // invalid AST
2190
2191 if (!result_type || !ClangUtil::IsClangType(result_type))
2192 return CompilerType(); // invalid return type
2193
2194 std::vector<QualType> qual_type_args;
2195 if (num_args > 0 && args == nullptr)
2196 return CompilerType(); // invalid argument array passed in
2197
2198 // Verify that all arguments are valid and the right type
2199 for (unsigned i = 0; i < num_args; ++i) {
2200 if (args[i]) {
2201 // Make sure we have a clang type in args[i] and not a type from another
2202 // language whose name might match
2203 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2204 lldbassert(is_clang_type);
2205 if (is_clang_type)
2206 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2207 else
2208 return CompilerType(); // invalid argument type (must be a clang type)
2209 } else
2210 return CompilerType(); // invalid argument type (empty)
2211 }
2212
2213 // TODO: Detect calling convention in DWARF?
2214 FunctionProtoType::ExtProtoInfo proto_info;
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002215 proto_info.ExtInfo = cc;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002216 proto_info.Variadic = is_variadic;
2217 proto_info.ExceptionSpec = EST_None;
Mikael Nilsson8b3bf6c2018-12-13 10:17:26 +00002218 proto_info.TypeQuals = clang::Qualifiers::fromFastMask(type_quals);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002219 proto_info.RefQualifier = RQ_None;
2220
2221 return CompilerType(ast,
2222 ast->getFunctionType(ClangUtil::GetQualType(result_type),
2223 qual_type_args, proto_info));
2224}
2225
2226ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
Zachary Turner6753d2d2018-12-12 17:17:53 +00002227 clang::DeclContext *decl_ctx, const char *name,
2228 const CompilerType &param_type, int storage) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002229 ASTContext *ast = getASTContext();
2230 assert(ast != nullptr);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00002231 auto *decl =
2232 ParmVarDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(),
2233 name && name[0] ? &ast->Idents.get(name) : nullptr,
2234 ClangUtil::GetQualType(param_type), nullptr,
2235 (clang::StorageClass)storage, nullptr);
2236 decl_ctx->addDecl(decl);
2237 return decl;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002238}
2239
2240void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2241 ParmVarDecl **params,
2242 unsigned num_params) {
2243 if (function_decl)
2244 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002245}
2246
Greg Claytona1e5dc82015-08-11 22:53:00 +00002247CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002248ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00002249 QualType block_type = m_ast_up->getBlockPointerType(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002250 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
Greg Claytonceeb5212016-05-26 22:33:25 +00002251
Kate Stoneb9c1b512016-09-06 20:57:50 +00002252 return CompilerType(this, block_type.getAsOpaquePtr());
Sean Callananc530ba92016-05-02 21:15:31 +00002253}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002254
2255#pragma mark Array Types
2256
Kate Stoneb9c1b512016-09-06 20:57:50 +00002257CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2258 size_t element_count,
2259 bool is_vector) {
2260 if (element_type.IsValid()) {
2261 ASTContext *ast = getASTContext();
2262 assert(ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002263
Kate Stoneb9c1b512016-09-06 20:57:50 +00002264 if (is_vector) {
2265 return CompilerType(
2266 ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2267 element_count));
2268 } else {
2269
2270 llvm::APInt ap_element_count(64, element_count);
2271 if (element_count == 0) {
2272 return CompilerType(ast, ast->getIncompleteArrayType(
2273 ClangUtil::GetQualType(element_type),
2274 clang::ArrayType::Normal, 0));
2275 } else {
2276 return CompilerType(
2277 ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2278 ap_element_count,
2279 clang::ArrayType::Normal, 0));
2280 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002281 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002282 }
2283 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002284}
2285
Kate Stoneb9c1b512016-09-06 20:57:50 +00002286CompilerType ClangASTContext::CreateStructForIdentifier(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002287 ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002288 const std::initializer_list<std::pair<const char *, CompilerType>>
2289 &type_fields,
2290 bool packed) {
2291 CompilerType type;
2292 if (!type_name.IsEmpty() &&
2293 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2294 .IsValid()) {
Pavel Labathf31c9d22017-01-05 13:18:42 +00002295 lldbassert(0 && "Trying to create a type for an existing name");
Enrico Granata76b08d52014-10-29 23:08:02 +00002296 return type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002297 }
2298
2299 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2300 clang::TTK_Struct, lldb::eLanguageTypeC);
2301 StartTagDeclarationDefinition(type);
2302 for (const auto &field : type_fields)
2303 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2304 0);
2305 if (packed)
2306 SetIsPacked(type);
2307 CompleteTagDeclarationDefinition(type);
2308 return type;
Enrico Granata76b08d52014-10-29 23:08:02 +00002309}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002310
Kate Stoneb9c1b512016-09-06 20:57:50 +00002311CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002312 ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002313 const std::initializer_list<std::pair<const char *, CompilerType>>
2314 &type_fields,
2315 bool packed) {
2316 CompilerType type;
2317 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2318 return type;
Sean Callananc530ba92016-05-02 21:15:31 +00002319
Kate Stoneb9c1b512016-09-06 20:57:50 +00002320 return CreateStructForIdentifier(type_name, type_fields, packed);
Sean Callananc530ba92016-05-02 21:15:31 +00002321}
2322
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002323#pragma mark Enumeration Types
2324
Greg Claytona1e5dc82015-08-11 22:53:00 +00002325CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002326ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2327 const Declaration &decl,
Tamas Berghammer59765832017-11-07 10:39:22 +00002328 const CompilerType &integer_clang_type,
2329 bool is_scoped) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002330 // TODO: Do something intelligent with the Declaration object passed in
2331 // like maybe filling in the SourceLocation with it...
2332 ASTContext *ast = getASTContext();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002333
Kate Stoneb9c1b512016-09-06 20:57:50 +00002334 // TODO: ask about these...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002335 // const bool IsFixed = false;
2336
2337 EnumDecl *enum_decl = EnumDecl::Create(
2338 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2339 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
Tamas Berghammercf6bf4c2017-11-07 13:43:55 +00002340 is_scoped, // IsScoped
2341 is_scoped, // IsScopedUsingClassTag
2342 false); // IsFixed
Kate Stoneb9c1b512016-09-06 20:57:50 +00002343
2344 if (enum_decl) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +00002345 if (decl_ctx)
2346 decl_ctx->addDecl(enum_decl);
2347
Kate Stoneb9c1b512016-09-06 20:57:50 +00002348 // TODO: check if we should be setting the promotion type too?
2349 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2350
2351 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2352
2353 return CompilerType(ast, ast->getTagDeclType(enum_decl));
2354 }
2355 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002356}
2357
Kate Stoneb9c1b512016-09-06 20:57:50 +00002358CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2359 size_t bit_size,
2360 bool is_signed) {
2361 if (ast) {
2362 if (is_signed) {
2363 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
2364 return CompilerType(ast, ast->SignedCharTy);
2365
2366 if (bit_size == ast->getTypeSize(ast->ShortTy))
2367 return CompilerType(ast, ast->ShortTy);
2368
2369 if (bit_size == ast->getTypeSize(ast->IntTy))
2370 return CompilerType(ast, ast->IntTy);
2371
2372 if (bit_size == ast->getTypeSize(ast->LongTy))
2373 return CompilerType(ast, ast->LongTy);
2374
2375 if (bit_size == ast->getTypeSize(ast->LongLongTy))
2376 return CompilerType(ast, ast->LongLongTy);
2377
2378 if (bit_size == ast->getTypeSize(ast->Int128Ty))
2379 return CompilerType(ast, ast->Int128Ty);
2380 } else {
2381 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
2382 return CompilerType(ast, ast->UnsignedCharTy);
2383
2384 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
2385 return CompilerType(ast, ast->UnsignedShortTy);
2386
2387 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
2388 return CompilerType(ast, ast->UnsignedIntTy);
2389
2390 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
2391 return CompilerType(ast, ast->UnsignedLongTy);
2392
2393 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
2394 return CompilerType(ast, ast->UnsignedLongLongTy);
2395
2396 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
2397 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002398 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002399 }
2400 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002401}
2402
Kate Stoneb9c1b512016-09-06 20:57:50 +00002403CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2404 bool is_signed) {
2405 if (ast)
2406 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2407 is_signed);
2408 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002409}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002410
Kate Stoneb9c1b512016-09-06 20:57:50 +00002411void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2412 if (decl_ctx) {
2413 DumpDeclContextHiearchy(decl_ctx->getParent());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002414
Kate Stoneb9c1b512016-09-06 20:57:50 +00002415 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2416 if (named_decl) {
2417 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2418 named_decl->getDeclName().getAsString().c_str());
2419 } else {
2420 printf("%20s\n", decl_ctx->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002421 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002422 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002423}
2424
Kate Stoneb9c1b512016-09-06 20:57:50 +00002425void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2426 if (decl == nullptr)
2427 return;
2428 DumpDeclContextHiearchy(decl->getDeclContext());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002429
Kate Stoneb9c1b512016-09-06 20:57:50 +00002430 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2431 if (record_decl) {
2432 printf("%20s: %s%s\n", decl->getDeclKindName(),
2433 record_decl->getDeclName().getAsString().c_str(),
2434 record_decl->isInjectedClassName() ? " (injected class name)" : "");
Greg Claytone6b36cd2015-12-08 01:02:08 +00002435
Kate Stoneb9c1b512016-09-06 20:57:50 +00002436 } else {
2437 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2438 if (named_decl) {
2439 printf("%20s: %s\n", decl->getDeclKindName(),
2440 named_decl->getDeclName().getAsString().c_str());
2441 } else {
2442 printf("%20s\n", decl->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002443 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002444 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002445}
2446
Kate Stoneb9c1b512016-09-06 20:57:50 +00002447bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2448 clang::Decl *rhs_decl) {
2449 if (lhs_decl && rhs_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002450 // Make sure the decl kinds match first
Kate Stoneb9c1b512016-09-06 20:57:50 +00002451 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2452 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002453
Kate Stoneb9c1b512016-09-06 20:57:50 +00002454 if (lhs_decl_kind == rhs_decl_kind) {
Adrian Prantl05097242018-04-30 16:49:04 +00002455 // Now check that the decl contexts kinds are all equivalent before we
2456 // have to check any names of the decl contexts...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002457 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2458 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2459 if (lhs_decl_ctx && rhs_decl_ctx) {
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00002460 while (true) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002461 if (lhs_decl_ctx && rhs_decl_ctx) {
2462 const clang::Decl::Kind lhs_decl_ctx_kind =
2463 lhs_decl_ctx->getDeclKind();
2464 const clang::Decl::Kind rhs_decl_ctx_kind =
2465 rhs_decl_ctx->getDeclKind();
2466 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2467 lhs_decl_ctx = lhs_decl_ctx->getParent();
2468 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002469
Kate Stoneb9c1b512016-09-06 20:57:50 +00002470 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2471 break;
2472 } else
2473 return false;
2474 } else
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00002475 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002476 }
2477
Kate Stoneb9c1b512016-09-06 20:57:50 +00002478 // Now make sure the name of the decls match
Kate Stoneb9c1b512016-09-06 20:57:50 +00002479 clang::NamedDecl *lhs_named_decl =
2480 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2481 clang::NamedDecl *rhs_named_decl =
2482 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2483 if (lhs_named_decl && rhs_named_decl) {
2484 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2485 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2486 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2487 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2488 return false;
2489 } else
Greg Claytona2721472011-06-25 00:44:06 +00002490 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002491 } else
2492 return false;
Greg Claytona2721472011-06-25 00:44:06 +00002493
Adrian Prantl05097242018-04-30 16:49:04 +00002494 // We know that the decl context kinds all match, so now we need to
2495 // make sure the names match as well
Kate Stoneb9c1b512016-09-06 20:57:50 +00002496 lhs_decl_ctx = lhs_decl->getDeclContext();
2497 rhs_decl_ctx = rhs_decl->getDeclContext();
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00002498 while (true) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002499 switch (lhs_decl_ctx->getDeclKind()) {
2500 case clang::Decl::TranslationUnit:
2501 // We don't care about the translation unit names
2502 return true;
2503 default: {
2504 clang::NamedDecl *lhs_named_decl =
2505 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2506 clang::NamedDecl *rhs_named_decl =
2507 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2508 if (lhs_named_decl && rhs_named_decl) {
2509 clang::DeclarationName lhs_decl_name =
2510 lhs_named_decl->getDeclName();
2511 clang::DeclarationName rhs_decl_name =
2512 rhs_named_decl->getDeclName();
2513 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2514 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2515 return false;
2516 } else
2517 return false;
2518 } else
2519 return false;
2520 } break;
2521 }
2522 lhs_decl_ctx = lhs_decl_ctx->getParent();
2523 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytond8d4a572015-08-11 21:38:15 +00002524 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002525 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002526 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002527 }
2528 return false;
2529}
2530bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2531 clang::Decl *decl) {
2532 if (!decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002533 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002534
Kate Stoneb9c1b512016-09-06 20:57:50 +00002535 ExternalASTSource *ast_source = ast->getExternalSource();
Greg Claytond8d4a572015-08-11 21:38:15 +00002536
Kate Stoneb9c1b512016-09-06 20:57:50 +00002537 if (!ast_source)
Greg Claytond8d4a572015-08-11 21:38:15 +00002538 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002539
2540 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2541 if (tag_decl->isCompleteDefinition())
2542 return true;
2543
2544 if (!tag_decl->hasExternalLexicalStorage())
2545 return false;
2546
2547 ast_source->CompleteType(tag_decl);
2548
2549 return !tag_decl->getTypeForDecl()->isIncompleteType();
2550 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2551 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2552 if (objc_interface_decl->getDefinition())
2553 return true;
2554
2555 if (!objc_interface_decl->hasExternalLexicalStorage())
2556 return false;
2557
2558 ast_source->CompleteType(objc_interface_decl);
2559
2560 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2561 } else {
2562 return false;
2563 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002564}
2565
Kate Stoneb9c1b512016-09-06 20:57:50 +00002566void ClangASTContext::SetMetadataAsUserID(const void *object,
2567 user_id_t user_id) {
2568 ClangASTMetadata meta_data;
2569 meta_data.SetUserID(user_id);
2570 SetMetadata(object, meta_data);
Greg Clayton99558cc42015-08-24 23:46:31 +00002571}
2572
Kate Stoneb9c1b512016-09-06 20:57:50 +00002573void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2574 ClangASTMetadata &metadata) {
2575 ClangExternalASTSourceCommon *external_source =
2576 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2577
2578 if (external_source)
2579 external_source->SetMetadata(object, metadata);
2580}
2581
2582ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2583 const void *object) {
2584 ClangExternalASTSourceCommon *external_source =
2585 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2586
2587 if (external_source && external_source->HasMetadata(object))
2588 return external_source->GetMetadata(object);
2589 else
Greg Claytond8d4a572015-08-11 21:38:15 +00002590 return nullptr;
2591}
2592
Kate Stoneb9c1b512016-09-06 20:57:50 +00002593clang::DeclContext *
2594ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2595 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2596}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002597
Kate Stoneb9c1b512016-09-06 20:57:50 +00002598clang::DeclContext *
2599ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2600 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2601}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002602
Kate Stoneb9c1b512016-09-06 20:57:50 +00002603bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2604 int kind) const {
2605 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2606 if (clang_type) {
2607 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2608 if (tag_type) {
2609 clang::TagDecl *tag_decl =
2610 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2611 if (tag_decl) {
2612 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2613 return true;
2614 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002615 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002616 }
2617 return false;
2618}
2619
2620bool ClangASTContext::SetDefaultAccessForRecordFields(
2621 clang::RecordDecl *record_decl, int default_accessibility,
2622 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2623 if (record_decl) {
2624 uint32_t field_idx;
2625 clang::RecordDecl::field_iterator field, field_end;
2626 for (field = record_decl->field_begin(),
2627 field_end = record_decl->field_end(), field_idx = 0;
2628 field != field_end; ++field, ++field_idx) {
2629 // If no accessibility was assigned, assign the correct one
2630 if (field_idx < num_assigned_accessibilities &&
2631 assigned_accessibilities[field_idx] == clang::AS_none)
2632 field->setAccess((clang::AccessSpecifier)default_accessibility);
2633 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002634 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002635 }
2636 return false;
2637}
2638
2639clang::DeclContext *
2640ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2641 return GetDeclContextForType(ClangUtil::GetQualType(type));
2642}
2643
2644clang::DeclContext *
2645ClangASTContext::GetDeclContextForType(clang::QualType type) {
2646 if (type.isNull())
2647 return nullptr;
2648
2649 clang::QualType qual_type = type.getCanonicalType();
2650 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2651 switch (type_class) {
2652 case clang::Type::ObjCInterface:
2653 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2654 ->getInterface();
2655 case clang::Type::ObjCObjectPointer:
2656 return GetDeclContextForType(
2657 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2658 ->getPointeeType());
2659 case clang::Type::Record:
2660 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2661 case clang::Type::Enum:
2662 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2663 case clang::Type::Typedef:
2664 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2665 ->getDecl()
2666 ->getUnderlyingType());
2667 case clang::Type::Auto:
2668 return GetDeclContextForType(
2669 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2670 case clang::Type::Elaborated:
2671 return GetDeclContextForType(
2672 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2673 case clang::Type::Paren:
2674 return GetDeclContextForType(
2675 llvm::cast<clang::ParenType>(qual_type)->desugar());
2676 default:
2677 break;
2678 }
2679 // No DeclContext in this type...
2680 return nullptr;
2681}
2682
2683static bool GetCompleteQualType(clang::ASTContext *ast,
2684 clang::QualType qual_type,
2685 bool allow_completion = true) {
2686 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2687 switch (type_class) {
2688 case clang::Type::ConstantArray:
2689 case clang::Type::IncompleteArray:
2690 case clang::Type::VariableArray: {
2691 const clang::ArrayType *array_type =
2692 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2693
2694 if (array_type)
2695 return GetCompleteQualType(ast, array_type->getElementType(),
2696 allow_completion);
2697 } break;
2698 case clang::Type::Record: {
2699 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2700 if (cxx_record_decl) {
2701 if (cxx_record_decl->hasExternalLexicalStorage()) {
2702 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2703 const bool fields_loaded =
2704 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2705 if (is_complete && fields_loaded)
2706 return true;
2707
2708 if (!allow_completion)
2709 return false;
2710
2711 // Call the field_begin() accessor to for it to use the external source
2712 // to load the fields...
2713 clang::ExternalASTSource *external_ast_source =
2714 ast->getExternalSource();
2715 if (external_ast_source) {
2716 external_ast_source->CompleteType(cxx_record_decl);
2717 if (cxx_record_decl->isCompleteDefinition()) {
2718 cxx_record_decl->field_begin();
2719 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2720 }
2721 }
2722 }
2723 }
2724 const clang::TagType *tag_type =
2725 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2726 return !tag_type->isIncompleteType();
2727 } break;
2728
2729 case clang::Type::Enum: {
2730 const clang::TagType *tag_type =
2731 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2732 if (tag_type) {
2733 clang::TagDecl *tag_decl = tag_type->getDecl();
2734 if (tag_decl) {
2735 if (tag_decl->getDefinition())
2736 return true;
2737
2738 if (!allow_completion)
2739 return false;
2740
2741 if (tag_decl->hasExternalLexicalStorage()) {
2742 if (ast) {
2743 clang::ExternalASTSource *external_ast_source =
2744 ast->getExternalSource();
2745 if (external_ast_source) {
2746 external_ast_source->CompleteType(tag_decl);
2747 return !tag_type->isIncompleteType();
2748 }
2749 }
2750 }
2751 return false;
2752 }
2753 }
2754
2755 } break;
2756 case clang::Type::ObjCObject:
2757 case clang::Type::ObjCInterface: {
2758 const clang::ObjCObjectType *objc_class_type =
2759 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2760 if (objc_class_type) {
2761 clang::ObjCInterfaceDecl *class_interface_decl =
2762 objc_class_type->getInterface();
2763 // We currently can't complete objective C types through the newly added
Adrian Prantl05097242018-04-30 16:49:04 +00002764 // ASTContext because it only supports TagDecl objects right now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002765 if (class_interface_decl) {
2766 if (class_interface_decl->getDefinition())
2767 return true;
2768
2769 if (!allow_completion)
2770 return false;
2771
2772 if (class_interface_decl->hasExternalLexicalStorage()) {
2773 if (ast) {
2774 clang::ExternalASTSource *external_ast_source =
2775 ast->getExternalSource();
2776 if (external_ast_source) {
2777 external_ast_source->CompleteType(class_interface_decl);
2778 return !objc_class_type->isIncompleteType();
2779 }
2780 }
2781 }
2782 return false;
2783 }
2784 }
2785 } break;
2786
2787 case clang::Type::Typedef:
2788 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2789 ->getDecl()
2790 ->getUnderlyingType(),
2791 allow_completion);
2792
2793 case clang::Type::Auto:
2794 return GetCompleteQualType(
2795 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2796 allow_completion);
2797
2798 case clang::Type::Elaborated:
2799 return GetCompleteQualType(
2800 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2801 allow_completion);
2802
2803 case clang::Type::Paren:
2804 return GetCompleteQualType(
2805 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2806 allow_completion);
2807
2808 case clang::Type::Attributed:
2809 return GetCompleteQualType(
2810 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2811 allow_completion);
2812
2813 default:
2814 break;
2815 }
2816
2817 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00002818}
2819
2820static clang::ObjCIvarDecl::AccessControl
Kate Stoneb9c1b512016-09-06 20:57:50 +00002821ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2822 switch (access) {
2823 case eAccessNone:
Greg Claytond8d4a572015-08-11 21:38:15 +00002824 return clang::ObjCIvarDecl::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002825 case eAccessPublic:
2826 return clang::ObjCIvarDecl::Public;
2827 case eAccessPrivate:
2828 return clang::ObjCIvarDecl::Private;
2829 case eAccessProtected:
2830 return clang::ObjCIvarDecl::Protected;
2831 case eAccessPackage:
2832 return clang::ObjCIvarDecl::Package;
2833 }
2834 return clang::ObjCIvarDecl::None;
Greg Claytond8d4a572015-08-11 21:38:15 +00002835}
2836
Greg Claytond8d4a572015-08-11 21:38:15 +00002837// Tests
Greg Claytond8d4a572015-08-11 21:38:15 +00002838
Kate Stoneb9c1b512016-09-06 20:57:50 +00002839bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2840 clang::QualType qual_type(GetCanonicalQualType(type));
2841
2842 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2843 switch (type_class) {
2844 case clang::Type::IncompleteArray:
2845 case clang::Type::VariableArray:
2846 case clang::Type::ConstantArray:
2847 case clang::Type::ExtVector:
2848 case clang::Type::Vector:
2849 case clang::Type::Record:
2850 case clang::Type::ObjCObject:
2851 case clang::Type::ObjCInterface:
2852 return true;
2853 case clang::Type::Auto:
2854 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2855 ->getDeducedType()
2856 .getAsOpaquePtr());
2857 case clang::Type::Elaborated:
2858 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2859 ->getNamedType()
2860 .getAsOpaquePtr());
2861 case clang::Type::Typedef:
2862 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2863 ->getDecl()
2864 ->getUnderlyingType()
2865 .getAsOpaquePtr());
2866 case clang::Type::Paren:
2867 return IsAggregateType(
2868 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2869 default:
2870 break;
2871 }
2872 // The clang type does have a value
2873 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002874}
2875
Kate Stoneb9c1b512016-09-06 20:57:50 +00002876bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2877 clang::QualType qual_type(GetCanonicalQualType(type));
2878
2879 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2880 switch (type_class) {
2881 case clang::Type::Record: {
2882 if (const clang::RecordType *record_type =
2883 llvm::dyn_cast_or_null<clang::RecordType>(
2884 qual_type.getTypePtrOrNull())) {
2885 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2886 return record_decl->isAnonymousStructOrUnion();
2887 }
Enrico Granata7123e2b2015-11-07 02:06:57 +00002888 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002889 break;
2890 }
2891 case clang::Type::Auto:
2892 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2893 ->getDeducedType()
2894 .getAsOpaquePtr());
2895 case clang::Type::Elaborated:
2896 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2897 ->getNamedType()
2898 .getAsOpaquePtr());
2899 case clang::Type::Typedef:
2900 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2901 ->getDecl()
2902 ->getUnderlyingType()
2903 .getAsOpaquePtr());
2904 case clang::Type::Paren:
2905 return IsAnonymousType(
2906 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2907 default:
2908 break;
2909 }
2910 // The clang type does have a value
2911 return false;
Enrico Granata7123e2b2015-11-07 02:06:57 +00002912}
2913
Kate Stoneb9c1b512016-09-06 20:57:50 +00002914bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2915 CompilerType *element_type_ptr,
2916 uint64_t *size, bool *is_incomplete) {
2917 clang::QualType qual_type(GetCanonicalQualType(type));
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002918
Kate Stoneb9c1b512016-09-06 20:57:50 +00002919 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2920 switch (type_class) {
2921 default:
2922 break;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002923
Kate Stoneb9c1b512016-09-06 20:57:50 +00002924 case clang::Type::ConstantArray:
Greg Claytond8d4a572015-08-11 21:38:15 +00002925 if (element_type_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002926 element_type_ptr->SetCompilerType(
2927 getASTContext(),
2928 llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002929 if (size)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002930 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2931 ->getSize()
2932 .getLimitedValue(ULLONG_MAX);
Greg Claytond8d4a572015-08-11 21:38:15 +00002933 if (is_incomplete)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002934 *is_incomplete = false;
2935 return true;
2936
2937 case clang::Type::IncompleteArray:
2938 if (element_type_ptr)
2939 element_type_ptr->SetCompilerType(
2940 getASTContext(),
2941 llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2942 if (size)
2943 *size = 0;
2944 if (is_incomplete)
2945 *is_incomplete = true;
2946 return true;
2947
2948 case clang::Type::VariableArray:
2949 if (element_type_ptr)
2950 element_type_ptr->SetCompilerType(
2951 getASTContext(),
2952 llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2953 if (size)
2954 *size = 0;
2955 if (is_incomplete)
2956 *is_incomplete = false;
2957 return true;
2958
2959 case clang::Type::DependentSizedArray:
2960 if (element_type_ptr)
2961 element_type_ptr->SetCompilerType(
2962 getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)
2963 ->getElementType());
2964 if (size)
2965 *size = 0;
2966 if (is_incomplete)
2967 *is_incomplete = false;
2968 return true;
2969
2970 case clang::Type::Typedef:
2971 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2972 ->getDecl()
2973 ->getUnderlyingType()
2974 .getAsOpaquePtr(),
2975 element_type_ptr, size, is_incomplete);
2976 case clang::Type::Auto:
2977 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2978 ->getDeducedType()
2979 .getAsOpaquePtr(),
2980 element_type_ptr, size, is_incomplete);
2981 case clang::Type::Elaborated:
2982 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2983 ->getNamedType()
2984 .getAsOpaquePtr(),
2985 element_type_ptr, size, is_incomplete);
2986 case clang::Type::Paren:
2987 return IsArrayType(
2988 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2989 element_type_ptr, size, is_incomplete);
2990 }
2991 if (element_type_ptr)
2992 element_type_ptr->Clear();
2993 if (size)
2994 *size = 0;
2995 if (is_incomplete)
2996 *is_incomplete = false;
2997 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002998}
2999
Kate Stoneb9c1b512016-09-06 20:57:50 +00003000bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
3001 CompilerType *element_type, uint64_t *size) {
3002 clang::QualType qual_type(GetCanonicalQualType(type));
3003
3004 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3005 switch (type_class) {
3006 case clang::Type::Vector: {
3007 const clang::VectorType *vector_type =
3008 qual_type->getAs<clang::VectorType>();
3009 if (vector_type) {
3010 if (size)
3011 *size = vector_type->getNumElements();
3012 if (element_type)
3013 *element_type =
3014 CompilerType(getASTContext(), vector_type->getElementType());
3015 }
3016 return true;
3017 } break;
3018 case clang::Type::ExtVector: {
3019 const clang::ExtVectorType *ext_vector_type =
3020 qual_type->getAs<clang::ExtVectorType>();
3021 if (ext_vector_type) {
3022 if (size)
3023 *size = ext_vector_type->getNumElements();
3024 if (element_type)
3025 *element_type =
3026 CompilerType(getASTContext(), ext_vector_type->getElementType());
3027 }
3028 return true;
3029 }
3030 default:
3031 break;
3032 }
3033 return false;
3034}
3035
3036bool ClangASTContext::IsRuntimeGeneratedType(
3037 lldb::opaque_compiler_type_t type) {
3038 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
3039 ->GetDeclContextForType(GetQualType(type));
3040 if (!decl_ctx)
3041 return false;
3042
3043 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
3044 return false;
3045
3046 clang::ObjCInterfaceDecl *result_iface_decl =
3047 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
3048
3049 ClangASTMetadata *ast_metadata =
3050 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
3051 if (!ast_metadata)
3052 return false;
3053 return (ast_metadata->GetISAPtr() != 0);
3054}
3055
3056bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
3057 return GetQualType(type).getUnqualifiedType()->isCharType();
3058}
3059
3060bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
3061 const bool allow_completion = false;
3062 return GetCompleteQualType(getASTContext(), GetQualType(type),
3063 allow_completion);
3064}
3065
3066bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
3067 return GetQualType(type).isConstQualified();
3068}
3069
3070bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
3071 uint32_t &length) {
3072 CompilerType pointee_or_element_clang_type;
3073 length = 0;
3074 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
3075
3076 if (!pointee_or_element_clang_type.IsValid())
3077 return false;
3078
3079 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
3080 if (pointee_or_element_clang_type.IsCharType()) {
3081 if (type_flags.Test(eTypeIsArray)) {
Adrian Prantl05097242018-04-30 16:49:04 +00003082 // We know the size of the array and it could be a C string since it is
3083 // an array of characters
Kate Stoneb9c1b512016-09-06 20:57:50 +00003084 length = llvm::cast<clang::ConstantArrayType>(
3085 GetCanonicalQualType(type).getTypePtr())
3086 ->getSize()
3087 .getLimitedValue();
3088 }
3089 return true;
3090 }
3091 }
3092 return false;
3093}
3094
3095bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
3096 bool *is_variadic_ptr) {
3097 if (type) {
3098 clang::QualType qual_type(GetCanonicalQualType(type));
3099
3100 if (qual_type->isFunctionType()) {
3101 if (is_variadic_ptr) {
3102 const clang::FunctionProtoType *function_proto_type =
3103 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3104 if (function_proto_type)
3105 *is_variadic_ptr = function_proto_type->isVariadic();
3106 else
3107 *is_variadic_ptr = false;
3108 }
3109 return true;
3110 }
3111
Greg Claytond8d4a572015-08-11 21:38:15 +00003112 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003113 switch (type_class) {
3114 default:
3115 break;
3116 case clang::Type::Typedef:
3117 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
3118 ->getDecl()
3119 ->getUnderlyingType()
3120 .getAsOpaquePtr(),
3121 nullptr);
3122 case clang::Type::Auto:
3123 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
3124 ->getDeducedType()
3125 .getAsOpaquePtr(),
3126 nullptr);
3127 case clang::Type::Elaborated:
3128 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
3129 ->getNamedType()
3130 .getAsOpaquePtr(),
3131 nullptr);
3132 case clang::Type::Paren:
3133 return IsFunctionType(
3134 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3135 nullptr);
3136 case clang::Type::LValueReference:
3137 case clang::Type::RValueReference: {
3138 const clang::ReferenceType *reference_type =
3139 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3140 if (reference_type)
3141 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
3142 nullptr);
3143 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00003144 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003145 }
3146 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003147}
3148
3149// Used to detect "Homogeneous Floating-point Aggregates"
3150uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003151ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
3152 CompilerType *base_type_ptr) {
3153 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003154 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003155
3156 clang::QualType qual_type(GetCanonicalQualType(type));
3157 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3158 switch (type_class) {
3159 case clang::Type::Record:
3160 if (GetCompleteType(type)) {
3161 const clang::CXXRecordDecl *cxx_record_decl =
3162 qual_type->getAsCXXRecordDecl();
3163 if (cxx_record_decl) {
3164 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3165 return 0;
3166 }
3167 const clang::RecordType *record_type =
3168 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3169 if (record_type) {
3170 const clang::RecordDecl *record_decl = record_type->getDecl();
3171 if (record_decl) {
3172 // We are looking for a structure that contains only floating point
3173 // types
3174 clang::RecordDecl::field_iterator field_pos,
3175 field_end = record_decl->field_end();
3176 uint32_t num_fields = 0;
3177 bool is_hva = false;
3178 bool is_hfa = false;
3179 clang::QualType base_qual_type;
3180 uint64_t base_bitwidth = 0;
3181 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3182 ++field_pos) {
3183 clang::QualType field_qual_type = field_pos->getType();
3184 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
3185 if (field_qual_type->isFloatingType()) {
3186 if (field_qual_type->isComplexType())
3187 return 0;
3188 else {
3189 if (num_fields == 0)
3190 base_qual_type = field_qual_type;
3191 else {
3192 if (is_hva)
3193 return 0;
3194 is_hfa = true;
3195 if (field_qual_type.getTypePtr() !=
3196 base_qual_type.getTypePtr())
3197 return 0;
3198 }
3199 }
3200 } else if (field_qual_type->isVectorType() ||
3201 field_qual_type->isExtVectorType()) {
3202 if (num_fields == 0) {
3203 base_qual_type = field_qual_type;
3204 base_bitwidth = field_bitwidth;
3205 } else {
3206 if (is_hfa)
3207 return 0;
3208 is_hva = true;
3209 if (base_bitwidth != field_bitwidth)
3210 return 0;
3211 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3212 return 0;
3213 }
3214 } else
3215 return 0;
3216 ++num_fields;
3217 }
3218 if (base_type_ptr)
3219 *base_type_ptr = CompilerType(getASTContext(), base_qual_type);
3220 return num_fields;
3221 }
3222 }
3223 }
3224 break;
3225
3226 case clang::Type::Typedef:
3227 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3228 ->getDecl()
3229 ->getUnderlyingType()
3230 .getAsOpaquePtr(),
3231 base_type_ptr);
3232
3233 case clang::Type::Auto:
3234 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3235 ->getDeducedType()
3236 .getAsOpaquePtr(),
3237 base_type_ptr);
3238
3239 case clang::Type::Elaborated:
3240 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3241 ->getNamedType()
3242 .getAsOpaquePtr(),
3243 base_type_ptr);
3244 default:
3245 break;
3246 }
3247 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003248}
3249
Kate Stoneb9c1b512016-09-06 20:57:50 +00003250size_t ClangASTContext::GetNumberOfFunctionArguments(
3251 lldb::opaque_compiler_type_t type) {
3252 if (type) {
3253 clang::QualType qual_type(GetCanonicalQualType(type));
3254 const clang::FunctionProtoType *func =
3255 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3256 if (func)
3257 return func->getNumParams();
3258 }
3259 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003260}
3261
Greg Claytona1e5dc82015-08-11 22:53:00 +00003262CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00003263ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3264 const size_t index) {
3265 if (type) {
Greg Claytond8d4a572015-08-11 21:38:15 +00003266 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003267 const clang::FunctionProtoType *func =
3268 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3269 if (func) {
3270 if (index < func->getNumParams())
3271 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00003272 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003273 }
3274 return CompilerType();
3275}
3276
3277bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3278 if (type) {
3279 clang::QualType qual_type(GetCanonicalQualType(type));
3280
3281 if (qual_type->isFunctionPointerType())
3282 return true;
3283
3284 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3285 switch (type_class) {
3286 default:
3287 break;
3288 case clang::Type::Typedef:
3289 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3290 ->getDecl()
3291 ->getUnderlyingType()
3292 .getAsOpaquePtr());
3293 case clang::Type::Auto:
3294 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3295 ->getDeducedType()
3296 .getAsOpaquePtr());
3297 case clang::Type::Elaborated:
3298 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3299 ->getNamedType()
3300 .getAsOpaquePtr());
3301 case clang::Type::Paren:
3302 return IsFunctionPointerType(
3303 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3304
3305 case clang::Type::LValueReference:
3306 case clang::Type::RValueReference: {
3307 const clang::ReferenceType *reference_type =
3308 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3309 if (reference_type)
3310 return IsFunctionPointerType(
3311 reference_type->getPointeeType().getAsOpaquePtr());
3312 } break;
3313 }
3314 }
3315 return false;
3316}
3317
3318bool ClangASTContext::IsBlockPointerType(
3319 lldb::opaque_compiler_type_t type,
3320 CompilerType *function_pointer_type_ptr) {
3321 if (type) {
3322 clang::QualType qual_type(GetCanonicalQualType(type));
3323
3324 if (qual_type->isBlockPointerType()) {
3325 if (function_pointer_type_ptr) {
3326 const clang::BlockPointerType *block_pointer_type =
3327 qual_type->getAs<clang::BlockPointerType>();
3328 QualType pointee_type = block_pointer_type->getPointeeType();
Jonas Devlieghered5b44032019-02-13 06:25:41 +00003329 QualType function_pointer_type = m_ast_up->getPointerType(pointee_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003330 *function_pointer_type_ptr =
3331 CompilerType(getASTContext(), function_pointer_type);
3332 }
3333 return true;
3334 }
3335
3336 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3337 switch (type_class) {
3338 default:
3339 break;
3340 case clang::Type::Typedef:
3341 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3342 ->getDecl()
3343 ->getUnderlyingType()
3344 .getAsOpaquePtr(),
3345 function_pointer_type_ptr);
3346 case clang::Type::Auto:
3347 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3348 ->getDeducedType()
3349 .getAsOpaquePtr(),
3350 function_pointer_type_ptr);
3351 case clang::Type::Elaborated:
3352 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3353 ->getNamedType()
3354 .getAsOpaquePtr(),
3355 function_pointer_type_ptr);
3356 case clang::Type::Paren:
3357 return IsBlockPointerType(
3358 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3359 function_pointer_type_ptr);
3360
3361 case clang::Type::LValueReference:
3362 case clang::Type::RValueReference: {
3363 const clang::ReferenceType *reference_type =
3364 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3365 if (reference_type)
3366 return IsBlockPointerType(
3367 reference_type->getPointeeType().getAsOpaquePtr(),
3368 function_pointer_type_ptr);
3369 } break;
3370 }
3371 }
3372 return false;
3373}
3374
3375bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3376 bool &is_signed) {
3377 if (!type)
3378 return false;
3379
3380 clang::QualType qual_type(GetCanonicalQualType(type));
3381 const clang::BuiltinType *builtin_type =
3382 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3383
3384 if (builtin_type) {
3385 if (builtin_type->isInteger()) {
3386 is_signed = builtin_type->isSignedInteger();
3387 return true;
3388 }
3389 }
3390
3391 return false;
3392}
3393
3394bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3395 bool &is_signed) {
3396 if (type) {
3397 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3398 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3399
3400 if (enum_type) {
3401 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3402 is_signed);
3403 return true;
3404 }
3405 }
3406
3407 return false;
3408}
3409
3410bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3411 CompilerType *pointee_type) {
3412 if (type) {
3413 clang::QualType qual_type(GetCanonicalQualType(type));
3414 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3415 switch (type_class) {
3416 case clang::Type::Builtin:
3417 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3418 default:
3419 break;
3420 case clang::BuiltinType::ObjCId:
3421 case clang::BuiltinType::ObjCClass:
3422 return true;
3423 }
3424 return false;
3425 case clang::Type::ObjCObjectPointer:
3426 if (pointee_type)
3427 pointee_type->SetCompilerType(
3428 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3429 ->getPointeeType());
3430 return true;
3431 case clang::Type::BlockPointer:
3432 if (pointee_type)
3433 pointee_type->SetCompilerType(
3434 getASTContext(),
3435 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3436 return true;
3437 case clang::Type::Pointer:
3438 if (pointee_type)
3439 pointee_type->SetCompilerType(
3440 getASTContext(),
3441 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3442 return true;
3443 case clang::Type::MemberPointer:
3444 if (pointee_type)
3445 pointee_type->SetCompilerType(
3446 getASTContext(),
3447 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3448 return true;
3449 case clang::Type::Typedef:
3450 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3451 ->getDecl()
3452 ->getUnderlyingType()
3453 .getAsOpaquePtr(),
3454 pointee_type);
3455 case clang::Type::Auto:
3456 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3457 ->getDeducedType()
3458 .getAsOpaquePtr(),
3459 pointee_type);
3460 case clang::Type::Elaborated:
3461 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3462 ->getNamedType()
3463 .getAsOpaquePtr(),
3464 pointee_type);
3465 case clang::Type::Paren:
3466 return IsPointerType(
3467 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3468 pointee_type);
3469 default:
3470 break;
3471 }
3472 }
3473 if (pointee_type)
3474 pointee_type->Clear();
3475 return false;
3476}
3477
3478bool ClangASTContext::IsPointerOrReferenceType(
3479 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3480 if (type) {
3481 clang::QualType qual_type(GetCanonicalQualType(type));
3482 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3483 switch (type_class) {
3484 case clang::Type::Builtin:
3485 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3486 default:
3487 break;
3488 case clang::BuiltinType::ObjCId:
3489 case clang::BuiltinType::ObjCClass:
3490 return true;
3491 }
3492 return false;
3493 case clang::Type::ObjCObjectPointer:
3494 if (pointee_type)
3495 pointee_type->SetCompilerType(
3496 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3497 ->getPointeeType());
3498 return true;
3499 case clang::Type::BlockPointer:
3500 if (pointee_type)
3501 pointee_type->SetCompilerType(
3502 getASTContext(),
3503 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3504 return true;
3505 case clang::Type::Pointer:
3506 if (pointee_type)
3507 pointee_type->SetCompilerType(
3508 getASTContext(),
3509 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3510 return true;
3511 case clang::Type::MemberPointer:
3512 if (pointee_type)
3513 pointee_type->SetCompilerType(
3514 getASTContext(),
3515 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3516 return true;
3517 case clang::Type::LValueReference:
3518 if (pointee_type)
3519 pointee_type->SetCompilerType(
3520 getASTContext(),
3521 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3522 return true;
3523 case clang::Type::RValueReference:
3524 if (pointee_type)
3525 pointee_type->SetCompilerType(
3526 getASTContext(),
3527 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3528 return true;
3529 case clang::Type::Typedef:
3530 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3531 ->getDecl()
3532 ->getUnderlyingType()
3533 .getAsOpaquePtr(),
3534 pointee_type);
3535 case clang::Type::Auto:
3536 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3537 ->getDeducedType()
3538 .getAsOpaquePtr(),
3539 pointee_type);
3540 case clang::Type::Elaborated:
3541 return IsPointerOrReferenceType(
3542 llvm::cast<clang::ElaboratedType>(qual_type)
3543 ->getNamedType()
3544 .getAsOpaquePtr(),
3545 pointee_type);
3546 case clang::Type::Paren:
3547 return IsPointerOrReferenceType(
3548 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3549 pointee_type);
3550 default:
3551 break;
3552 }
3553 }
3554 if (pointee_type)
3555 pointee_type->Clear();
3556 return false;
3557}
3558
3559bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3560 CompilerType *pointee_type,
3561 bool *is_rvalue) {
3562 if (type) {
3563 clang::QualType qual_type(GetCanonicalQualType(type));
3564 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3565
3566 switch (type_class) {
3567 case clang::Type::LValueReference:
3568 if (pointee_type)
3569 pointee_type->SetCompilerType(
3570 getASTContext(),
3571 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3572 if (is_rvalue)
3573 *is_rvalue = false;
3574 return true;
3575 case clang::Type::RValueReference:
3576 if (pointee_type)
3577 pointee_type->SetCompilerType(
3578 getASTContext(),
3579 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3580 if (is_rvalue)
3581 *is_rvalue = true;
3582 return true;
3583 case clang::Type::Typedef:
3584 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3585 ->getDecl()
3586 ->getUnderlyingType()
3587 .getAsOpaquePtr(),
3588 pointee_type, is_rvalue);
3589 case clang::Type::Auto:
3590 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3591 ->getDeducedType()
3592 .getAsOpaquePtr(),
3593 pointee_type, is_rvalue);
3594 case clang::Type::Elaborated:
3595 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3596 ->getNamedType()
3597 .getAsOpaquePtr(),
3598 pointee_type, is_rvalue);
3599 case clang::Type::Paren:
3600 return IsReferenceType(
3601 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3602 pointee_type, is_rvalue);
3603
3604 default:
3605 break;
3606 }
3607 }
3608 if (pointee_type)
3609 pointee_type->Clear();
3610 return false;
3611}
3612
3613bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3614 uint32_t &count, bool &is_complex) {
3615 if (type) {
3616 clang::QualType qual_type(GetCanonicalQualType(type));
3617
3618 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3619 qual_type->getCanonicalTypeInternal())) {
3620 clang::BuiltinType::Kind kind = BT->getKind();
3621 if (kind >= clang::BuiltinType::Float &&
3622 kind <= clang::BuiltinType::LongDouble) {
3623 count = 1;
3624 is_complex = false;
3625 return true;
3626 }
3627 } else if (const clang::ComplexType *CT =
3628 llvm::dyn_cast<clang::ComplexType>(
3629 qual_type->getCanonicalTypeInternal())) {
3630 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3631 is_complex)) {
3632 count = 2;
3633 is_complex = true;
3634 return true;
3635 }
3636 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3637 qual_type->getCanonicalTypeInternal())) {
3638 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3639 is_complex)) {
3640 count = VT->getNumElements();
3641 is_complex = false;
3642 return true;
3643 }
3644 }
3645 }
3646 count = 0;
3647 is_complex = false;
3648 return false;
3649}
3650
3651bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3652 if (!type)
3653 return false;
3654
3655 clang::QualType qual_type(GetQualType(type));
3656 const clang::TagType *tag_type =
3657 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3658 if (tag_type) {
3659 clang::TagDecl *tag_decl = tag_type->getDecl();
3660 if (tag_decl)
3661 return tag_decl->isCompleteDefinition();
3662 return false;
3663 } else {
3664 const clang::ObjCObjectType *objc_class_type =
3665 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3666 if (objc_class_type) {
3667 clang::ObjCInterfaceDecl *class_interface_decl =
3668 objc_class_type->getInterface();
3669 if (class_interface_decl)
3670 return class_interface_decl->getDefinition() != nullptr;
3671 return false;
3672 }
3673 }
3674 return true;
3675}
3676
3677bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
3678 if (type) {
3679 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3680
3681 const clang::ObjCObjectPointerType *obj_pointer_type =
3682 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3683
3684 if (obj_pointer_type)
3685 return obj_pointer_type->isObjCClassType();
3686 }
3687 return false;
3688}
3689
3690bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3691 if (ClangUtil::IsClangType(type))
3692 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3693 return false;
3694}
3695
3696bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3697 if (!type)
3698 return false;
3699 clang::QualType qual_type(GetCanonicalQualType(type));
3700 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3701 return (type_class == clang::Type::Record);
3702}
3703
3704bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3705 if (!type)
3706 return false;
3707 clang::QualType qual_type(GetCanonicalQualType(type));
3708 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3709 return (type_class == clang::Type::Enum);
3710}
3711
3712bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3713 if (type) {
3714 clang::QualType qual_type(GetCanonicalQualType(type));
3715 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3716 switch (type_class) {
3717 case clang::Type::Record:
3718 if (GetCompleteType(type)) {
3719 const clang::RecordType *record_type =
3720 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3721 const clang::RecordDecl *record_decl = record_type->getDecl();
3722 if (record_decl) {
3723 const clang::CXXRecordDecl *cxx_record_decl =
3724 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3725 if (cxx_record_decl)
3726 return cxx_record_decl->isPolymorphic();
Greg Claytond8d4a572015-08-11 21:38:15 +00003727 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003728 }
3729 break;
3730
3731 default:
3732 break;
3733 }
3734 }
3735 return false;
3736}
3737
3738bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3739 CompilerType *dynamic_pointee_type,
3740 bool check_cplusplus,
3741 bool check_objc) {
3742 clang::QualType pointee_qual_type;
3743 if (type) {
3744 clang::QualType qual_type(GetCanonicalQualType(type));
3745 bool success = false;
3746 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3747 switch (type_class) {
3748 case clang::Type::Builtin:
3749 if (check_objc &&
3750 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3751 clang::BuiltinType::ObjCId) {
3752 if (dynamic_pointee_type)
3753 dynamic_pointee_type->SetCompilerType(this, type);
3754 return true;
3755 }
3756 break;
3757
3758 case clang::Type::ObjCObjectPointer:
3759 if (check_objc) {
3760 if (auto objc_pointee_type =
3761 qual_type->getPointeeType().getTypePtrOrNull()) {
3762 if (auto objc_object_type =
3763 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3764 objc_pointee_type)) {
3765 if (objc_object_type->isObjCClass())
3766 return false;
3767 }
3768 }
3769 if (dynamic_pointee_type)
3770 dynamic_pointee_type->SetCompilerType(
3771 getASTContext(),
3772 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3773 ->getPointeeType());
3774 return true;
3775 }
3776 break;
3777
3778 case clang::Type::Pointer:
3779 pointee_qual_type =
3780 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3781 success = true;
3782 break;
3783
3784 case clang::Type::LValueReference:
3785 case clang::Type::RValueReference:
3786 pointee_qual_type =
3787 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3788 success = true;
3789 break;
3790
3791 case clang::Type::Typedef:
3792 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3793 ->getDecl()
3794 ->getUnderlyingType()
3795 .getAsOpaquePtr(),
3796 dynamic_pointee_type, check_cplusplus,
3797 check_objc);
3798
3799 case clang::Type::Auto:
3800 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3801 ->getDeducedType()
3802 .getAsOpaquePtr(),
3803 dynamic_pointee_type, check_cplusplus,
3804 check_objc);
3805
3806 case clang::Type::Elaborated:
3807 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3808 ->getNamedType()
3809 .getAsOpaquePtr(),
3810 dynamic_pointee_type, check_cplusplus,
3811 check_objc);
3812
3813 case clang::Type::Paren:
3814 return IsPossibleDynamicType(
3815 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3816 dynamic_pointee_type, check_cplusplus, check_objc);
3817 default:
3818 break;
3819 }
3820
3821 if (success) {
3822 // Check to make sure what we are pointing too is a possible dynamic C++
Adrian Prantl05097242018-04-30 16:49:04 +00003823 // type We currently accept any "void *" (in case we have a class that
3824 // has been watered down to an opaque pointer) and virtual C++ classes.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003825 const clang::Type::TypeClass pointee_type_class =
3826 pointee_qual_type.getCanonicalType()->getTypeClass();
3827 switch (pointee_type_class) {
3828 case clang::Type::Builtin:
3829 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3830 case clang::BuiltinType::UnknownAny:
3831 case clang::BuiltinType::Void:
3832 if (dynamic_pointee_type)
3833 dynamic_pointee_type->SetCompilerType(getASTContext(),
3834 pointee_qual_type);
3835 return true;
3836 default:
3837 break;
3838 }
3839 break;
3840
3841 case clang::Type::Record:
3842 if (check_cplusplus) {
3843 clang::CXXRecordDecl *cxx_record_decl =
3844 pointee_qual_type->getAsCXXRecordDecl();
3845 if (cxx_record_decl) {
3846 bool is_complete = cxx_record_decl->isCompleteDefinition();
3847
3848 if (is_complete)
3849 success = cxx_record_decl->isDynamicClass();
3850 else {
3851 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3852 getASTContext(), cxx_record_decl);
3853 if (metadata)
3854 success = metadata->GetIsDynamicCXXType();
3855 else {
3856 is_complete = CompilerType(getASTContext(), pointee_qual_type)
3857 .GetCompleteType();
3858 if (is_complete)
3859 success = cxx_record_decl->isDynamicClass();
3860 else
3861 success = false;
3862 }
3863 }
3864
3865 if (success) {
3866 if (dynamic_pointee_type)
3867 dynamic_pointee_type->SetCompilerType(getASTContext(),
3868 pointee_qual_type);
3869 return true;
3870 }
3871 }
3872 }
3873 break;
3874
3875 case clang::Type::ObjCObject:
3876 case clang::Type::ObjCInterface:
3877 if (check_objc) {
3878 if (dynamic_pointee_type)
3879 dynamic_pointee_type->SetCompilerType(getASTContext(),
3880 pointee_qual_type);
3881 return true;
3882 }
3883 break;
3884
3885 default:
3886 break;
3887 }
3888 }
3889 }
3890 if (dynamic_pointee_type)
3891 dynamic_pointee_type->Clear();
3892 return false;
3893}
3894
3895bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3896 if (!type)
3897 return false;
3898
3899 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3900}
3901
3902bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3903 if (!type)
3904 return false;
3905 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3906}
3907
3908bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3909 if (!type)
3910 return false;
3911 return GetCanonicalQualType(type)->isVoidType();
3912}
3913
Alex Langforda03e2b22019-06-04 19:29:59 +00003914bool ClangASTContext::CanPassInRegisters(const CompilerType &type) {
3915 if (auto *record_decl =
3916 ClangASTContext::GetAsRecordDecl(type)) {
3917 return record_decl->canPassInRegisters();
3918 }
3919 return false;
3920}
3921
Kate Stoneb9c1b512016-09-06 20:57:50 +00003922bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3923 return ClangASTContextSupportsLanguage(language);
3924}
3925
3926bool ClangASTContext::GetCXXClassName(const CompilerType &type,
3927 std::string &class_name) {
3928 if (type) {
3929 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3930 if (!qual_type.isNull()) {
3931 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3932 if (cxx_record_decl) {
3933 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3934 return true;
3935 }
3936 }
3937 }
3938 class_name.clear();
3939 return false;
3940}
3941
3942bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3943 if (!type)
3944 return false;
3945
3946 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Jonas Devliegherea6682a42018-12-15 00:15:33 +00003947 return !qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003948}
3949
3950bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3951 if (!type)
3952 return false;
3953 clang::QualType qual_type(GetCanonicalQualType(type));
3954 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3955 if (tag_type)
3956 return tag_type->isBeingDefined();
3957 return false;
3958}
3959
3960bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3961 CompilerType *class_type_ptr) {
3962 if (!type)
3963 return false;
3964
3965 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3966
3967 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3968 if (class_type_ptr) {
3969 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3970 const clang::ObjCObjectPointerType *obj_pointer_type =
3971 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3972 if (obj_pointer_type == nullptr)
3973 class_type_ptr->Clear();
3974 else
3975 class_type_ptr->SetCompilerType(
3976 type.GetTypeSystem(),
3977 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3978 .getAsOpaquePtr());
3979 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003980 }
3981 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003982 }
3983 if (class_type_ptr)
3984 class_type_ptr->Clear();
3985 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003986}
3987
Kate Stoneb9c1b512016-09-06 20:57:50 +00003988bool ClangASTContext::GetObjCClassName(const CompilerType &type,
3989 std::string &class_name) {
3990 if (!type)
3991 return false;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003992
Kate Stoneb9c1b512016-09-06 20:57:50 +00003993 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3994
3995 const clang::ObjCObjectType *object_type =
3996 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3997 if (object_type) {
3998 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3999 if (interface) {
4000 class_name = interface->getNameAsString();
4001 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00004002 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004003 }
4004 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00004005}
4006
Greg Claytond8d4a572015-08-11 21:38:15 +00004007// Type Completion
Greg Claytond8d4a572015-08-11 21:38:15 +00004008
Kate Stoneb9c1b512016-09-06 20:57:50 +00004009bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
4010 if (!type)
4011 return false;
4012 const bool allow_completion = true;
4013 return GetCompleteQualType(getASTContext(), GetQualType(type),
4014 allow_completion);
Greg Claytond8d4a572015-08-11 21:38:15 +00004015}
4016
Kate Stoneb9c1b512016-09-06 20:57:50 +00004017ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
4018 std::string type_name;
4019 if (type) {
4020 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
4021 clang::QualType qual_type(GetQualType(type));
4022 printing_policy.SuppressTagKeyword = true;
4023 const clang::TypedefType *typedef_type =
4024 qual_type->getAs<clang::TypedefType>();
4025 if (typedef_type) {
4026 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
4027 type_name = typedef_decl->getQualifiedNameAsString();
4028 } else {
4029 type_name = qual_type.getAsString(printing_policy);
Greg Claytond8d4a572015-08-11 21:38:15 +00004030 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004031 }
4032 return ConstString(type_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00004033}
4034
4035uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004036ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
4037 CompilerType *pointee_or_element_clang_type) {
4038 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004039 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004040
4041 if (pointee_or_element_clang_type)
4042 pointee_or_element_clang_type->Clear();
4043
4044 clang::QualType qual_type(GetQualType(type));
4045
4046 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4047 switch (type_class) {
Sean Callananddf802a2017-06-02 01:24:18 +00004048 case clang::Type::Attributed:
4049 return GetTypeInfo(
4050 qual_type->getAs<clang::AttributedType>()
4051 ->getModifiedType().getAsOpaquePtr(),
4052 pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004053 case clang::Type::Builtin: {
4054 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
4055 qual_type->getCanonicalTypeInternal());
4056
4057 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
4058 switch (builtin_type->getKind()) {
4059 case clang::BuiltinType::ObjCId:
4060 case clang::BuiltinType::ObjCClass:
4061 if (pointee_or_element_clang_type)
4062 pointee_or_element_clang_type->SetCompilerType(
4063 getASTContext(), getASTContext()->ObjCBuiltinClassTy);
4064 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
4065 break;
4066
4067 case clang::BuiltinType::ObjCSel:
4068 if (pointee_or_element_clang_type)
4069 pointee_or_element_clang_type->SetCompilerType(getASTContext(),
4070 getASTContext()->CharTy);
4071 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
4072 break;
4073
4074 case clang::BuiltinType::Bool:
4075 case clang::BuiltinType::Char_U:
4076 case clang::BuiltinType::UChar:
4077 case clang::BuiltinType::WChar_U:
4078 case clang::BuiltinType::Char16:
4079 case clang::BuiltinType::Char32:
4080 case clang::BuiltinType::UShort:
4081 case clang::BuiltinType::UInt:
4082 case clang::BuiltinType::ULong:
4083 case clang::BuiltinType::ULongLong:
4084 case clang::BuiltinType::UInt128:
4085 case clang::BuiltinType::Char_S:
4086 case clang::BuiltinType::SChar:
4087 case clang::BuiltinType::WChar_S:
4088 case clang::BuiltinType::Short:
4089 case clang::BuiltinType::Int:
4090 case clang::BuiltinType::Long:
4091 case clang::BuiltinType::LongLong:
4092 case clang::BuiltinType::Int128:
4093 case clang::BuiltinType::Float:
4094 case clang::BuiltinType::Double:
4095 case clang::BuiltinType::LongDouble:
4096 builtin_type_flags |= eTypeIsScalar;
4097 if (builtin_type->isInteger()) {
4098 builtin_type_flags |= eTypeIsInteger;
4099 if (builtin_type->isSignedInteger())
4100 builtin_type_flags |= eTypeIsSigned;
4101 } else if (builtin_type->isFloatingPoint())
4102 builtin_type_flags |= eTypeIsFloat;
4103 break;
4104 default:
4105 break;
4106 }
4107 return builtin_type_flags;
4108 }
4109
4110 case clang::Type::BlockPointer:
4111 if (pointee_or_element_clang_type)
4112 pointee_or_element_clang_type->SetCompilerType(
4113 getASTContext(), qual_type->getPointeeType());
4114 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
4115
4116 case clang::Type::Complex: {
4117 uint32_t complex_type_flags =
4118 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
4119 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
4120 qual_type->getCanonicalTypeInternal());
4121 if (complex_type) {
4122 clang::QualType complex_element_type(complex_type->getElementType());
4123 if (complex_element_type->isIntegerType())
4124 complex_type_flags |= eTypeIsFloat;
4125 else if (complex_element_type->isFloatingType())
4126 complex_type_flags |= eTypeIsInteger;
4127 }
4128 return complex_type_flags;
4129 } break;
4130
4131 case clang::Type::ConstantArray:
4132 case clang::Type::DependentSizedArray:
4133 case clang::Type::IncompleteArray:
4134 case clang::Type::VariableArray:
4135 if (pointee_or_element_clang_type)
4136 pointee_or_element_clang_type->SetCompilerType(
4137 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
4138 ->getElementType());
4139 return eTypeHasChildren | eTypeIsArray;
4140
4141 case clang::Type::DependentName:
4142 return 0;
4143 case clang::Type::DependentSizedExtVector:
4144 return eTypeHasChildren | eTypeIsVector;
4145 case clang::Type::DependentTemplateSpecialization:
4146 return eTypeIsTemplate;
4147 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004148 return CompilerType(
4149 getASTContext(),
4150 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
4151 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004152
4153 case clang::Type::Enum:
4154 if (pointee_or_element_clang_type)
4155 pointee_or_element_clang_type->SetCompilerType(
4156 getASTContext(),
4157 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
4158 return eTypeIsEnumeration | eTypeHasValue;
4159
4160 case clang::Type::Auto:
4161 return CompilerType(
4162 getASTContext(),
4163 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4164 .GetTypeInfo(pointee_or_element_clang_type);
4165 case clang::Type::Elaborated:
4166 return CompilerType(
4167 getASTContext(),
4168 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4169 .GetTypeInfo(pointee_or_element_clang_type);
4170 case clang::Type::Paren:
4171 return CompilerType(getASTContext(),
4172 llvm::cast<clang::ParenType>(qual_type)->desugar())
4173 .GetTypeInfo(pointee_or_element_clang_type);
4174
4175 case clang::Type::FunctionProto:
4176 return eTypeIsFuncPrototype | eTypeHasValue;
4177 case clang::Type::FunctionNoProto:
4178 return eTypeIsFuncPrototype | eTypeHasValue;
4179 case clang::Type::InjectedClassName:
4180 return 0;
4181
4182 case clang::Type::LValueReference:
4183 case clang::Type::RValueReference:
4184 if (pointee_or_element_clang_type)
4185 pointee_or_element_clang_type->SetCompilerType(
4186 getASTContext(),
4187 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4188 ->getPointeeType());
4189 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4190
4191 case clang::Type::MemberPointer:
4192 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4193
4194 case clang::Type::ObjCObjectPointer:
4195 if (pointee_or_element_clang_type)
4196 pointee_or_element_clang_type->SetCompilerType(
4197 getASTContext(), qual_type->getPointeeType());
4198 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4199 eTypeHasValue;
4200
4201 case clang::Type::ObjCObject:
4202 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4203 case clang::Type::ObjCInterface:
4204 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4205
4206 case clang::Type::Pointer:
4207 if (pointee_or_element_clang_type)
4208 pointee_or_element_clang_type->SetCompilerType(
4209 getASTContext(), qual_type->getPointeeType());
4210 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4211
4212 case clang::Type::Record:
4213 if (qual_type->getAsCXXRecordDecl())
4214 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4215 else
4216 return eTypeHasChildren | eTypeIsStructUnion;
4217 break;
4218 case clang::Type::SubstTemplateTypeParm:
4219 return eTypeIsTemplate;
4220 case clang::Type::TemplateTypeParm:
4221 return eTypeIsTemplate;
4222 case clang::Type::TemplateSpecialization:
4223 return eTypeIsTemplate;
4224
4225 case clang::Type::Typedef:
4226 return eTypeIsTypedef |
4227 CompilerType(getASTContext(),
4228 llvm::cast<clang::TypedefType>(qual_type)
4229 ->getDecl()
4230 ->getUnderlyingType())
4231 .GetTypeInfo(pointee_or_element_clang_type);
4232 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004233 return CompilerType(getASTContext(),
4234 llvm::cast<clang::TypeOfExprType>(qual_type)
4235 ->getUnderlyingExpr()
4236 ->getType())
4237 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004238 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004239 return CompilerType(
4240 getASTContext(),
4241 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4242 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004243 case clang::Type::UnresolvedUsing:
4244 return 0;
4245
4246 case clang::Type::ExtVector:
4247 case clang::Type::Vector: {
4248 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4249 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4250 qual_type->getCanonicalTypeInternal());
4251 if (vector_type) {
4252 if (vector_type->isIntegerType())
4253 vector_type_flags |= eTypeIsFloat;
4254 else if (vector_type->isFloatingType())
4255 vector_type_flags |= eTypeIsInteger;
4256 }
4257 return vector_type_flags;
4258 }
4259 default:
4260 return 0;
4261 }
4262 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004263}
4264
Greg Claytond8d4a572015-08-11 21:38:15 +00004265lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004266ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4267 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004268 return lldb::eLanguageTypeC;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004269
4270 // If the type is a reference, then resolve it to what it refers to first:
4271 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4272 if (qual_type->isAnyPointerType()) {
4273 if (qual_type->isObjCObjectPointerType())
4274 return lldb::eLanguageTypeObjC;
Adrian Prantl1db0f0c2019-05-02 23:07:23 +00004275 if (qual_type->getPointeeCXXRecordDecl())
4276 return lldb::eLanguageTypeC_plus_plus;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004277
4278 clang::QualType pointee_type(qual_type->getPointeeType());
Adrian Prantl1db0f0c2019-05-02 23:07:23 +00004279 if (pointee_type->getPointeeCXXRecordDecl())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004280 return lldb::eLanguageTypeC_plus_plus;
4281 if (pointee_type->isObjCObjectOrInterfaceType())
4282 return lldb::eLanguageTypeObjC;
4283 if (pointee_type->isObjCClassType())
4284 return lldb::eLanguageTypeObjC;
4285 if (pointee_type.getTypePtr() ==
4286 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4287 return lldb::eLanguageTypeObjC;
4288 } else {
4289 if (qual_type->isObjCObjectOrInterfaceType())
4290 return lldb::eLanguageTypeObjC;
4291 if (qual_type->getAsCXXRecordDecl())
4292 return lldb::eLanguageTypeC_plus_plus;
4293 switch (qual_type->getTypeClass()) {
4294 default:
4295 break;
4296 case clang::Type::Builtin:
4297 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4298 default:
4299 case clang::BuiltinType::Void:
4300 case clang::BuiltinType::Bool:
4301 case clang::BuiltinType::Char_U:
4302 case clang::BuiltinType::UChar:
4303 case clang::BuiltinType::WChar_U:
4304 case clang::BuiltinType::Char16:
4305 case clang::BuiltinType::Char32:
4306 case clang::BuiltinType::UShort:
4307 case clang::BuiltinType::UInt:
4308 case clang::BuiltinType::ULong:
4309 case clang::BuiltinType::ULongLong:
4310 case clang::BuiltinType::UInt128:
4311 case clang::BuiltinType::Char_S:
4312 case clang::BuiltinType::SChar:
4313 case clang::BuiltinType::WChar_S:
4314 case clang::BuiltinType::Short:
4315 case clang::BuiltinType::Int:
4316 case clang::BuiltinType::Long:
4317 case clang::BuiltinType::LongLong:
4318 case clang::BuiltinType::Int128:
4319 case clang::BuiltinType::Float:
4320 case clang::BuiltinType::Double:
4321 case clang::BuiltinType::LongDouble:
4322 break;
4323
4324 case clang::BuiltinType::NullPtr:
4325 return eLanguageTypeC_plus_plus;
4326
4327 case clang::BuiltinType::ObjCId:
4328 case clang::BuiltinType::ObjCClass:
4329 case clang::BuiltinType::ObjCSel:
4330 return eLanguageTypeObjC;
4331
4332 case clang::BuiltinType::Dependent:
4333 case clang::BuiltinType::Overload:
4334 case clang::BuiltinType::BoundMember:
4335 case clang::BuiltinType::UnknownAny:
4336 break;
4337 }
4338 break;
4339 case clang::Type::Typedef:
4340 return CompilerType(getASTContext(),
4341 llvm::cast<clang::TypedefType>(qual_type)
4342 ->getDecl()
4343 ->getUnderlyingType())
4344 .GetMinimumLanguage();
4345 }
4346 }
4347 return lldb::eLanguageTypeC;
Greg Claytond8d4a572015-08-11 21:38:15 +00004348}
4349
4350lldb::TypeClass
Kate Stoneb9c1b512016-09-06 20:57:50 +00004351ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4352 if (!type)
4353 return lldb::eTypeClassInvalid;
4354
4355 clang::QualType qual_type(GetQualType(type));
4356
4357 switch (qual_type->getTypeClass()) {
4358 case clang::Type::UnaryTransform:
4359 break;
4360 case clang::Type::FunctionNoProto:
4361 return lldb::eTypeClassFunction;
4362 case clang::Type::FunctionProto:
4363 return lldb::eTypeClassFunction;
4364 case clang::Type::IncompleteArray:
4365 return lldb::eTypeClassArray;
4366 case clang::Type::VariableArray:
4367 return lldb::eTypeClassArray;
4368 case clang::Type::ConstantArray:
4369 return lldb::eTypeClassArray;
4370 case clang::Type::DependentSizedArray:
4371 return lldb::eTypeClassArray;
4372 case clang::Type::DependentSizedExtVector:
4373 return lldb::eTypeClassVector;
Fangrui Song8f284882018-07-13 22:40:40 +00004374 case clang::Type::DependentVector:
4375 return lldb::eTypeClassVector;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004376 case clang::Type::ExtVector:
4377 return lldb::eTypeClassVector;
4378 case clang::Type::Vector:
4379 return lldb::eTypeClassVector;
4380 case clang::Type::Builtin:
4381 return lldb::eTypeClassBuiltin;
4382 case clang::Type::ObjCObjectPointer:
4383 return lldb::eTypeClassObjCObjectPointer;
4384 case clang::Type::BlockPointer:
4385 return lldb::eTypeClassBlockPointer;
4386 case clang::Type::Pointer:
4387 return lldb::eTypeClassPointer;
4388 case clang::Type::LValueReference:
4389 return lldb::eTypeClassReference;
4390 case clang::Type::RValueReference:
4391 return lldb::eTypeClassReference;
4392 case clang::Type::MemberPointer:
4393 return lldb::eTypeClassMemberPointer;
4394 case clang::Type::Complex:
4395 if (qual_type->isComplexType())
4396 return lldb::eTypeClassComplexFloat;
4397 else
4398 return lldb::eTypeClassComplexInteger;
4399 case clang::Type::ObjCObject:
4400 return lldb::eTypeClassObjCObject;
4401 case clang::Type::ObjCInterface:
4402 return lldb::eTypeClassObjCInterface;
4403 case clang::Type::Record: {
4404 const clang::RecordType *record_type =
4405 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4406 const clang::RecordDecl *record_decl = record_type->getDecl();
4407 if (record_decl->isUnion())
4408 return lldb::eTypeClassUnion;
4409 else if (record_decl->isStruct())
4410 return lldb::eTypeClassStruct;
4411 else
4412 return lldb::eTypeClassClass;
4413 } break;
4414 case clang::Type::Enum:
4415 return lldb::eTypeClassEnumeration;
4416 case clang::Type::Typedef:
4417 return lldb::eTypeClassTypedef;
4418 case clang::Type::UnresolvedUsing:
4419 break;
4420 case clang::Type::Paren:
4421 return CompilerType(getASTContext(),
4422 llvm::cast<clang::ParenType>(qual_type)->desugar())
4423 .GetTypeClass();
4424 case clang::Type::Auto:
4425 return CompilerType(
4426 getASTContext(),
4427 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4428 .GetTypeClass();
4429 case clang::Type::Elaborated:
4430 return CompilerType(
4431 getASTContext(),
4432 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4433 .GetTypeClass();
4434
4435 case clang::Type::Attributed:
4436 break;
4437 case clang::Type::TemplateTypeParm:
4438 break;
4439 case clang::Type::SubstTemplateTypeParm:
4440 break;
4441 case clang::Type::SubstTemplateTypeParmPack:
4442 break;
4443 case clang::Type::InjectedClassName:
4444 break;
4445 case clang::Type::DependentName:
4446 break;
4447 case clang::Type::DependentTemplateSpecialization:
4448 break;
4449 case clang::Type::PackExpansion:
4450 break;
4451
4452 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004453 return CompilerType(getASTContext(),
4454 llvm::cast<clang::TypeOfExprType>(qual_type)
4455 ->getUnderlyingExpr()
4456 ->getType())
4457 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004458 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004459 return CompilerType(
4460 getASTContext(),
4461 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4462 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004463 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004464 return CompilerType(
4465 getASTContext(),
4466 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4467 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004468 case clang::Type::TemplateSpecialization:
4469 break;
Pavel Labath4f19fce22017-02-17 13:39:50 +00004470 case clang::Type::DeducedTemplateSpecialization:
4471 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004472 case clang::Type::Atomic:
4473 break;
4474 case clang::Type::Pipe:
4475 break;
4476
4477 // pointer type decayed from an array or function type.
4478 case clang::Type::Decayed:
4479 break;
4480 case clang::Type::Adjusted:
4481 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00004482 case clang::Type::ObjCTypeParam:
4483 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00004484
4485 case clang::Type::DependentAddressSpace:
4486 break;
Krasimir Georgiev435e76a2019-05-07 13:59:30 +00004487 case clang::Type::MacroQualified:
4488 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004489 }
4490 // We don't know hot to display this type...
4491 return lldb::eTypeClassOther;
Greg Claytond8d4a572015-08-11 21:38:15 +00004492}
4493
Kate Stoneb9c1b512016-09-06 20:57:50 +00004494unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4495 if (type)
4496 return GetQualType(type).getQualifiers().getCVRQualifiers();
4497 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004498}
4499
Greg Claytond8d4a572015-08-11 21:38:15 +00004500// Creating related types
Greg Claytond8d4a572015-08-11 21:38:15 +00004501
Greg Claytona1e5dc82015-08-11 22:53:00 +00004502CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004503ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4504 uint64_t *stride) {
4505 if (type) {
4506 clang::QualType qual_type(GetCanonicalQualType(type));
4507
4508 const clang::Type *array_eletype =
4509 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4510
4511 if (!array_eletype)
4512 return CompilerType();
4513
4514 CompilerType element_type(getASTContext(),
4515 array_eletype->getCanonicalTypeUnqualified());
4516
4517 // TODO: the real stride will be >= this value.. find the real one!
4518 if (stride)
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00004519 if (Optional<uint64_t> size = element_type.GetByteSize(nullptr))
Adrian Prantld963a7c2019-01-15 18:07:52 +00004520 *stride = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004521
4522 return element_type;
4523 }
4524 return CompilerType();
4525}
4526
4527CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4528 uint64_t size) {
4529 if (type) {
4530 clang::QualType qual_type(GetCanonicalQualType(type));
4531 if (clang::ASTContext *ast_ctx = getASTContext()) {
4532 if (size != 0)
4533 return CompilerType(
4534 ast_ctx, ast_ctx->getConstantArrayType(
4535 qual_type, llvm::APInt(64, size),
4536 clang::ArrayType::ArraySizeModifier::Normal, 0));
4537 else
4538 return CompilerType(
4539 ast_ctx,
4540 ast_ctx->getIncompleteArrayType(
4541 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
Greg Claytond8d4a572015-08-11 21:38:15 +00004542 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004543 }
4544
4545 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004546}
4547
Greg Claytona1e5dc82015-08-11 22:53:00 +00004548CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004549ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4550 if (type)
4551 return CompilerType(getASTContext(), GetCanonicalQualType(type));
4552 return CompilerType();
4553}
4554
4555static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4556 clang::QualType qual_type) {
4557 if (qual_type->isPointerType())
4558 qual_type = ast->getPointerType(
4559 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4560 else
4561 qual_type = qual_type.getUnqualifiedType();
4562 qual_type.removeLocalConst();
4563 qual_type.removeLocalRestrict();
4564 qual_type.removeLocalVolatile();
4565 return qual_type;
Enrico Granata639392f2016-08-30 20:39:58 +00004566}
4567
4568CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004569ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4570 if (type)
4571 return CompilerType(
4572 getASTContext(),
4573 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4574 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004575}
4576
Kate Stoneb9c1b512016-09-06 20:57:50 +00004577int ClangASTContext::GetFunctionArgumentCount(
4578 lldb::opaque_compiler_type_t type) {
4579 if (type) {
4580 const clang::FunctionProtoType *func =
4581 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4582 if (func)
4583 return func->getNumParams();
4584 }
4585 return -1;
4586}
4587
4588CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4589 lldb::opaque_compiler_type_t type, size_t idx) {
4590 if (type) {
4591 const clang::FunctionProtoType *func =
4592 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4593 if (func) {
4594 const uint32_t num_args = func->getNumParams();
4595 if (idx < num_args)
4596 return CompilerType(getASTContext(), func->getParamType(idx));
4597 }
4598 }
4599 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004600}
4601
Greg Claytona1e5dc82015-08-11 22:53:00 +00004602CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004603ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4604 if (type) {
4605 clang::QualType qual_type(GetQualType(type));
4606 const clang::FunctionProtoType *func =
4607 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4608 if (func)
4609 return CompilerType(getASTContext(), func->getReturnType());
4610 }
4611 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004612}
4613
4614size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004615ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4616 size_t num_functions = 0;
4617 if (type) {
4618 clang::QualType qual_type(GetCanonicalQualType(type));
4619 switch (qual_type->getTypeClass()) {
4620 case clang::Type::Record:
4621 if (GetCompleteQualType(getASTContext(), qual_type)) {
4622 const clang::RecordType *record_type =
4623 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4624 const clang::RecordDecl *record_decl = record_type->getDecl();
4625 assert(record_decl);
4626 const clang::CXXRecordDecl *cxx_record_decl =
4627 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4628 if (cxx_record_decl)
4629 num_functions = std::distance(cxx_record_decl->method_begin(),
4630 cxx_record_decl->method_end());
4631 }
4632 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004633
Sean Callananf9c622a2016-09-30 18:44:43 +00004634 case clang::Type::ObjCObjectPointer: {
4635 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004636 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004637 const clang::ObjCInterfaceType *objc_interface_type =
4638 objc_class_type->getInterfaceType();
4639 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004640 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4641 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004642 clang::ObjCInterfaceDecl *class_interface_decl =
4643 objc_interface_type->getDecl();
4644 if (class_interface_decl) {
4645 num_functions = std::distance(class_interface_decl->meth_begin(),
4646 class_interface_decl->meth_end());
Greg Claytond8d4a572015-08-11 21:38:15 +00004647 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004648 }
4649 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004650 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004651
4652 case clang::Type::ObjCObject:
4653 case clang::Type::ObjCInterface:
4654 if (GetCompleteType(type)) {
4655 const clang::ObjCObjectType *objc_class_type =
4656 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4657 if (objc_class_type) {
4658 clang::ObjCInterfaceDecl *class_interface_decl =
4659 objc_class_type->getInterface();
4660 if (class_interface_decl)
4661 num_functions = std::distance(class_interface_decl->meth_begin(),
4662 class_interface_decl->meth_end());
4663 }
4664 }
4665 break;
4666
4667 case clang::Type::Typedef:
4668 return CompilerType(getASTContext(),
4669 llvm::cast<clang::TypedefType>(qual_type)
4670 ->getDecl()
4671 ->getUnderlyingType())
4672 .GetNumMemberFunctions();
4673
4674 case clang::Type::Auto:
4675 return CompilerType(
4676 getASTContext(),
4677 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4678 .GetNumMemberFunctions();
4679
4680 case clang::Type::Elaborated:
4681 return CompilerType(
4682 getASTContext(),
4683 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4684 .GetNumMemberFunctions();
4685
4686 case clang::Type::Paren:
4687 return CompilerType(getASTContext(),
4688 llvm::cast<clang::ParenType>(qual_type)->desugar())
4689 .GetNumMemberFunctions();
4690
4691 default:
4692 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004693 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004694 }
4695 return num_functions;
Greg Claytond8d4a572015-08-11 21:38:15 +00004696}
4697
4698TypeMemberFunctionImpl
Kate Stoneb9c1b512016-09-06 20:57:50 +00004699ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4700 size_t idx) {
4701 std::string name;
4702 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4703 CompilerType clang_type;
4704 CompilerDecl clang_decl;
4705 if (type) {
4706 clang::QualType qual_type(GetCanonicalQualType(type));
4707 switch (qual_type->getTypeClass()) {
4708 case clang::Type::Record:
4709 if (GetCompleteQualType(getASTContext(), qual_type)) {
4710 const clang::RecordType *record_type =
4711 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4712 const clang::RecordDecl *record_decl = record_type->getDecl();
4713 assert(record_decl);
4714 const clang::CXXRecordDecl *cxx_record_decl =
4715 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4716 if (cxx_record_decl) {
4717 auto method_iter = cxx_record_decl->method_begin();
4718 auto method_end = cxx_record_decl->method_end();
4719 if (idx <
4720 static_cast<size_t>(std::distance(method_iter, method_end))) {
4721 std::advance(method_iter, idx);
4722 clang::CXXMethodDecl *cxx_method_decl =
4723 method_iter->getCanonicalDecl();
4724 if (cxx_method_decl) {
4725 name = cxx_method_decl->getDeclName().getAsString();
4726 if (cxx_method_decl->isStatic())
4727 kind = lldb::eMemberFunctionKindStaticMethod;
4728 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4729 kind = lldb::eMemberFunctionKindConstructor;
4730 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4731 kind = lldb::eMemberFunctionKindDestructor;
4732 else
4733 kind = lldb::eMemberFunctionKindInstanceMethod;
4734 clang_type = CompilerType(
4735 this, cxx_method_decl->getType().getAsOpaquePtr());
4736 clang_decl = CompilerDecl(this, cxx_method_decl);
4737 }
4738 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004739 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004740 }
4741 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004742
Sean Callananf9c622a2016-09-30 18:44:43 +00004743 case clang::Type::ObjCObjectPointer: {
4744 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004745 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004746 const clang::ObjCInterfaceType *objc_interface_type =
4747 objc_class_type->getInterfaceType();
4748 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004749 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4750 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004751 clang::ObjCInterfaceDecl *class_interface_decl =
4752 objc_interface_type->getDecl();
4753 if (class_interface_decl) {
4754 auto method_iter = class_interface_decl->meth_begin();
4755 auto method_end = class_interface_decl->meth_end();
4756 if (idx <
4757 static_cast<size_t>(std::distance(method_iter, method_end))) {
4758 std::advance(method_iter, idx);
4759 clang::ObjCMethodDecl *objc_method_decl =
4760 method_iter->getCanonicalDecl();
4761 if (objc_method_decl) {
4762 clang_decl = CompilerDecl(this, objc_method_decl);
4763 name = objc_method_decl->getSelector().getAsString();
4764 if (objc_method_decl->isClassMethod())
4765 kind = lldb::eMemberFunctionKindStaticMethod;
4766 else
4767 kind = lldb::eMemberFunctionKindInstanceMethod;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004768 }
4769 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004770 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004771 }
4772 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004773 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004774
Kate Stoneb9c1b512016-09-06 20:57:50 +00004775 case clang::Type::ObjCObject:
4776 case clang::Type::ObjCInterface:
4777 if (GetCompleteType(type)) {
4778 const clang::ObjCObjectType *objc_class_type =
4779 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4780 if (objc_class_type) {
4781 clang::ObjCInterfaceDecl *class_interface_decl =
4782 objc_class_type->getInterface();
4783 if (class_interface_decl) {
4784 auto method_iter = class_interface_decl->meth_begin();
4785 auto method_end = class_interface_decl->meth_end();
4786 if (idx <
4787 static_cast<size_t>(std::distance(method_iter, method_end))) {
4788 std::advance(method_iter, idx);
4789 clang::ObjCMethodDecl *objc_method_decl =
4790 method_iter->getCanonicalDecl();
4791 if (objc_method_decl) {
4792 clang_decl = CompilerDecl(this, objc_method_decl);
4793 name = objc_method_decl->getSelector().getAsString();
4794 if (objc_method_decl->isClassMethod())
4795 kind = lldb::eMemberFunctionKindStaticMethod;
4796 else
4797 kind = lldb::eMemberFunctionKindInstanceMethod;
4798 }
4799 }
4800 }
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004801 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004802 }
4803 break;
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004804
Kate Stoneb9c1b512016-09-06 20:57:50 +00004805 case clang::Type::Typedef:
4806 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4807 ->getDecl()
4808 ->getUnderlyingType()
4809 .getAsOpaquePtr(),
4810 idx);
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004811
Kate Stoneb9c1b512016-09-06 20:57:50 +00004812 case clang::Type::Auto:
4813 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4814 ->getDeducedType()
4815 .getAsOpaquePtr(),
4816 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004817
Kate Stoneb9c1b512016-09-06 20:57:50 +00004818 case clang::Type::Elaborated:
4819 return GetMemberFunctionAtIndex(
4820 llvm::cast<clang::ElaboratedType>(qual_type)
4821 ->getNamedType()
4822 .getAsOpaquePtr(),
4823 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004824
Kate Stoneb9c1b512016-09-06 20:57:50 +00004825 case clang::Type::Paren:
4826 return GetMemberFunctionAtIndex(
4827 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4828 idx);
4829
4830 default:
4831 break;
Greg Clayton56939cb2015-09-17 22:23:34 +00004832 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004833 }
Greg Clayton56939cb2015-09-17 22:23:34 +00004834
Kate Stoneb9c1b512016-09-06 20:57:50 +00004835 if (kind == eMemberFunctionKindUnknown)
4836 return TypeMemberFunctionImpl();
4837 else
4838 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
Greg Clayton56939cb2015-09-17 22:23:34 +00004839}
4840
Greg Claytona1e5dc82015-08-11 22:53:00 +00004841CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004842ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4843 if (type)
4844 return CompilerType(getASTContext(),
4845 GetQualType(type).getNonReferenceType());
4846 return CompilerType();
4847}
4848
4849CompilerType ClangASTContext::CreateTypedefType(
4850 const CompilerType &type, const char *typedef_name,
4851 const CompilerDeclContext &compiler_decl_ctx) {
4852 if (type && typedef_name && typedef_name[0]) {
4853 ClangASTContext *ast =
4854 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4855 if (!ast)
4856 return CompilerType();
4857 clang::ASTContext *clang_ast = ast->getASTContext();
4858 clang::QualType qual_type(ClangUtil::GetQualType(type));
4859
4860 clang::DeclContext *decl_ctx =
4861 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4862 if (decl_ctx == nullptr)
4863 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4864
4865 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4866 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4867 &clang_ast->Idents.get(typedef_name),
4868 clang_ast->getTrivialTypeSourceInfo(qual_type));
4869
4870 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4871
Aleksandr Urakov709426b2018-09-10 08:08:43 +00004872 decl_ctx->addDecl(decl);
4873
Kate Stoneb9c1b512016-09-06 20:57:50 +00004874 // Get a uniqued clang::QualType for the typedef decl type
4875 return CompilerType(clang_ast, clang_ast->getTypedefType(decl));
4876 }
4877 return CompilerType();
4878}
4879
4880CompilerType
4881ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4882 if (type) {
4883 clang::QualType qual_type(GetQualType(type));
4884 return CompilerType(getASTContext(),
4885 qual_type.getTypePtr()->getPointeeType());
4886 }
4887 return CompilerType();
4888}
4889
4890CompilerType
4891ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4892 if (type) {
4893 clang::QualType qual_type(GetQualType(type));
4894
4895 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4896 switch (type_class) {
4897 case clang::Type::ObjCObject:
4898 case clang::Type::ObjCInterface:
4899 return CompilerType(getASTContext(),
4900 getASTContext()->getObjCObjectPointerType(qual_type));
4901
4902 default:
4903 return CompilerType(getASTContext(),
4904 getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004905 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004906 }
4907 return CompilerType();
4908}
4909
4910CompilerType
4911ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4912 if (type)
4913 return CompilerType(this, getASTContext()
4914 ->getLValueReferenceType(GetQualType(type))
4915 .getAsOpaquePtr());
4916 else
Greg Claytona1e5dc82015-08-11 22:53:00 +00004917 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004918}
4919
Kate Stoneb9c1b512016-09-06 20:57:50 +00004920CompilerType
4921ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4922 if (type)
4923 return CompilerType(this, getASTContext()
4924 ->getRValueReferenceType(GetQualType(type))
4925 .getAsOpaquePtr());
4926 else
4927 return CompilerType();
4928}
4929
4930CompilerType
4931ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4932 if (type) {
4933 clang::QualType result(GetQualType(type));
4934 result.addConst();
4935 return CompilerType(this, result.getAsOpaquePtr());
4936 }
4937 return CompilerType();
4938}
4939
4940CompilerType
4941ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4942 if (type) {
4943 clang::QualType result(GetQualType(type));
4944 result.addVolatile();
4945 return CompilerType(this, result.getAsOpaquePtr());
4946 }
4947 return CompilerType();
4948}
4949
4950CompilerType
4951ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4952 if (type) {
4953 clang::QualType result(GetQualType(type));
4954 result.addRestrict();
4955 return CompilerType(this, result.getAsOpaquePtr());
4956 }
4957 return CompilerType();
4958}
4959
4960CompilerType
4961ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4962 const char *typedef_name,
4963 const CompilerDeclContext &compiler_decl_ctx) {
4964 if (type) {
4965 clang::ASTContext *clang_ast = getASTContext();
4966 clang::QualType qual_type(GetQualType(type));
4967
4968 clang::DeclContext *decl_ctx =
4969 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4970 if (decl_ctx == nullptr)
4971 decl_ctx = getASTContext()->getTranslationUnitDecl();
4972
4973 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4974 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4975 &clang_ast->Idents.get(typedef_name),
4976 clang_ast->getTrivialTypeSourceInfo(qual_type));
4977
4978 clang::TagDecl *tdecl = nullptr;
4979 if (!qual_type.isNull()) {
4980 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4981 tdecl = rt->getDecl();
4982 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4983 tdecl = et->getDecl();
4984 }
4985
4986 // Check whether this declaration is an anonymous struct, union, or enum,
Adrian Prantl05097242018-04-30 16:49:04 +00004987 // hidden behind a typedef. If so, we try to check whether we have a
4988 // typedef tag to attach to the original record declaration
Kate Stoneb9c1b512016-09-06 20:57:50 +00004989 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4990 tdecl->setTypedefNameForAnonDecl(decl);
4991
4992 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4993
4994 // Get a uniqued clang::QualType for the typedef decl type
4995 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4996 }
4997 return CompilerType();
4998}
4999
5000CompilerType
5001ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
5002 if (type) {
5003 const clang::TypedefType *typedef_type =
5004 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
5005 if (typedef_type)
5006 return CompilerType(getASTContext(),
5007 typedef_type->getDecl()->getUnderlyingType());
5008 }
5009 return CompilerType();
5010}
Greg Claytond8d4a572015-08-11 21:38:15 +00005011
Greg Claytond8d4a572015-08-11 21:38:15 +00005012// Create related types using the current type's AST
Greg Claytond8d4a572015-08-11 21:38:15 +00005013
Kate Stoneb9c1b512016-09-06 20:57:50 +00005014CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
5015 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005016}
Greg Claytond8d4a572015-08-11 21:38:15 +00005017// Exploring the type
Greg Claytond8d4a572015-08-11 21:38:15 +00005018
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005019Optional<uint64_t>
5020ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
5021 ExecutionContextScope *exe_scope) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005022 if (GetCompleteType(type)) {
Greg Claytond8d4a572015-08-11 21:38:15 +00005023 clang::QualType qual_type(GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00005024 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005025 switch (type_class) {
5026 case clang::Type::Record:
5027 if (GetCompleteType(type))
5028 return getASTContext()->getTypeSize(qual_type);
5029 else
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005030 return None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005031 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00005032
Kate Stoneb9c1b512016-09-06 20:57:50 +00005033 case clang::Type::ObjCInterface:
5034 case clang::Type::ObjCObject: {
5035 ExecutionContext exe_ctx(exe_scope);
5036 Process *process = exe_ctx.GetProcessPtr();
5037 if (process) {
5038 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
5039 if (objc_runtime) {
5040 uint64_t bit_size = 0;
5041 if (objc_runtime->GetTypeBitSize(
5042 CompilerType(getASTContext(), qual_type), bit_size))
5043 return bit_size;
5044 }
5045 } else {
5046 static bool g_printed = false;
5047 if (!g_printed) {
5048 StreamString s;
5049 DumpTypeDescription(type, &s);
5050
5051 llvm::outs() << "warning: trying to determine the size of type ";
5052 llvm::outs() << s.GetString() << "\n";
5053 llvm::outs() << "without a valid ExecutionContext. this is not "
5054 "reliable. please file a bug against LLDB.\n";
5055 llvm::outs() << "backtrace:\n";
5056 llvm::sys::PrintStackTrace(llvm::outs());
5057 llvm::outs() << "\n";
5058 g_printed = true;
5059 }
5060 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005061 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005062 LLVM_FALLTHROUGH;
5063 default:
5064 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
5065 if (bit_size == 0) {
5066 if (qual_type->isIncompleteArrayType())
5067 return getASTContext()->getTypeSize(
5068 qual_type->getArrayElementTypeNoTypeQual()
5069 ->getCanonicalTypeUnqualified());
5070 }
5071 if (qual_type->isObjCObjectOrInterfaceType())
5072 return bit_size +
5073 getASTContext()->getTypeSize(
5074 getASTContext()->ObjCBuiltinClassTy);
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005075 // Function types actually have a size of 0, that's not an error.
5076 if (qual_type->isFunctionProtoType())
5077 return bit_size;
5078 if (bit_size)
5079 return bit_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005080 }
5081 }
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005082 return None;
Greg Claytond8d4a572015-08-11 21:38:15 +00005083}
5084
Kate Stoneb9c1b512016-09-06 20:57:50 +00005085size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
5086 if (GetCompleteType(type))
5087 return getASTContext()->getTypeAlign(GetQualType(type));
5088 return 0;
5089}
5090
5091lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
5092 uint64_t &count) {
5093 if (!type)
5094 return lldb::eEncodingInvalid;
5095
5096 count = 1;
5097 clang::QualType qual_type(GetCanonicalQualType(type));
5098
5099 switch (qual_type->getTypeClass()) {
5100 case clang::Type::UnaryTransform:
5101 break;
5102
5103 case clang::Type::FunctionNoProto:
5104 case clang::Type::FunctionProto:
5105 break;
5106
5107 case clang::Type::IncompleteArray:
5108 case clang::Type::VariableArray:
5109 break;
5110
5111 case clang::Type::ConstantArray:
5112 break;
5113
Fangrui Song8f284882018-07-13 22:40:40 +00005114 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005115 case clang::Type::ExtVector:
5116 case clang::Type::Vector:
5117 // TODO: Set this to more than one???
5118 break;
5119
5120 case clang::Type::Builtin:
5121 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5122 case clang::BuiltinType::Void:
5123 break;
5124
5125 case clang::BuiltinType::Bool:
5126 case clang::BuiltinType::Char_S:
5127 case clang::BuiltinType::SChar:
5128 case clang::BuiltinType::WChar_S:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005129 case clang::BuiltinType::Short:
5130 case clang::BuiltinType::Int:
5131 case clang::BuiltinType::Long:
5132 case clang::BuiltinType::LongLong:
5133 case clang::BuiltinType::Int128:
5134 return lldb::eEncodingSint;
5135
5136 case clang::BuiltinType::Char_U:
5137 case clang::BuiltinType::UChar:
5138 case clang::BuiltinType::WChar_U:
Richard Smith51d12d82018-05-02 02:43:22 +00005139 case clang::BuiltinType::Char8:
5140 case clang::BuiltinType::Char16:
5141 case clang::BuiltinType::Char32:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005142 case clang::BuiltinType::UShort:
5143 case clang::BuiltinType::UInt:
5144 case clang::BuiltinType::ULong:
5145 case clang::BuiltinType::ULongLong:
5146 case clang::BuiltinType::UInt128:
5147 return lldb::eEncodingUint;
5148
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005149 // Fixed point types. Note that they are currently ignored.
5150 case clang::BuiltinType::ShortAccum:
5151 case clang::BuiltinType::Accum:
5152 case clang::BuiltinType::LongAccum:
5153 case clang::BuiltinType::UShortAccum:
5154 case clang::BuiltinType::UAccum:
5155 case clang::BuiltinType::ULongAccum:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005156 case clang::BuiltinType::ShortFract:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005157 case clang::BuiltinType::Fract:
5158 case clang::BuiltinType::LongFract:
5159 case clang::BuiltinType::UShortFract:
5160 case clang::BuiltinType::UFract:
5161 case clang::BuiltinType::ULongFract:
5162 case clang::BuiltinType::SatShortAccum:
5163 case clang::BuiltinType::SatAccum:
5164 case clang::BuiltinType::SatLongAccum:
5165 case clang::BuiltinType::SatUShortAccum:
5166 case clang::BuiltinType::SatUAccum:
5167 case clang::BuiltinType::SatULongAccum:
5168 case clang::BuiltinType::SatShortFract:
5169 case clang::BuiltinType::SatFract:
5170 case clang::BuiltinType::SatLongFract:
5171 case clang::BuiltinType::SatUShortFract:
5172 case clang::BuiltinType::SatUFract:
5173 case clang::BuiltinType::SatULongFract:
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005174 break;
5175
Kate Stoneb9c1b512016-09-06 20:57:50 +00005176 case clang::BuiltinType::Half:
5177 case clang::BuiltinType::Float:
Ted Woodward4355c7c2017-09-20 19:16:53 +00005178 case clang::BuiltinType::Float16:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005179 case clang::BuiltinType::Float128:
5180 case clang::BuiltinType::Double:
5181 case clang::BuiltinType::LongDouble:
5182 return lldb::eEncodingIEEE754;
5183
5184 case clang::BuiltinType::ObjCClass:
5185 case clang::BuiltinType::ObjCId:
5186 case clang::BuiltinType::ObjCSel:
5187 return lldb::eEncodingUint;
5188
5189 case clang::BuiltinType::NullPtr:
5190 return lldb::eEncodingUint;
5191
5192 case clang::BuiltinType::Kind::ARCUnbridgedCast:
5193 case clang::BuiltinType::Kind::BoundMember:
5194 case clang::BuiltinType::Kind::BuiltinFn:
5195 case clang::BuiltinType::Kind::Dependent:
5196 case clang::BuiltinType::Kind::OCLClkEvent:
5197 case clang::BuiltinType::Kind::OCLEvent:
5198 case clang::BuiltinType::Kind::OCLImage1dRO:
5199 case clang::BuiltinType::Kind::OCLImage1dWO:
5200 case clang::BuiltinType::Kind::OCLImage1dRW:
5201 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
5202 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
5203 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
5204 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
5205 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
5206 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
5207 case clang::BuiltinType::Kind::OCLImage2dRO:
5208 case clang::BuiltinType::Kind::OCLImage2dWO:
5209 case clang::BuiltinType::Kind::OCLImage2dRW:
5210 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
5211 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
5212 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
5213 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
5214 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
5215 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
5216 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5217 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5218 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5219 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5220 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5221 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5222 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5223 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5224 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5225 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5226 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5227 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5228 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5229 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5230 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5231 case clang::BuiltinType::Kind::OCLImage3dRO:
5232 case clang::BuiltinType::Kind::OCLImage3dWO:
5233 case clang::BuiltinType::Kind::OCLImage3dRW:
5234 case clang::BuiltinType::Kind::OCLQueue:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005235 case clang::BuiltinType::Kind::OCLReserveID:
5236 case clang::BuiltinType::Kind::OCLSampler:
5237 case clang::BuiltinType::Kind::OMPArraySection:
5238 case clang::BuiltinType::Kind::Overload:
5239 case clang::BuiltinType::Kind::PseudoObject:
5240 case clang::BuiltinType::Kind::UnknownAny:
5241 break;
Jorge Gorbe Moyaa6e6c182018-11-08 22:04:58 +00005242
5243 case clang::BuiltinType::OCLIntelSubgroupAVCMcePayload:
5244 case clang::BuiltinType::OCLIntelSubgroupAVCImePayload:
5245 case clang::BuiltinType::OCLIntelSubgroupAVCRefPayload:
5246 case clang::BuiltinType::OCLIntelSubgroupAVCSicPayload:
5247 case clang::BuiltinType::OCLIntelSubgroupAVCMceResult:
5248 case clang::BuiltinType::OCLIntelSubgroupAVCImeResult:
5249 case clang::BuiltinType::OCLIntelSubgroupAVCRefResult:
5250 case clang::BuiltinType::OCLIntelSubgroupAVCSicResult:
5251 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultSingleRefStreamout:
5252 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultDualRefStreamout:
5253 case clang::BuiltinType::OCLIntelSubgroupAVCImeSingleRefStreamin:
5254 case clang::BuiltinType::OCLIntelSubgroupAVCImeDualRefStreamin:
5255 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005256 }
5257 break;
Adrian Prantl05097242018-04-30 16:49:04 +00005258 // All pointer types are represented as unsigned integer encodings. We may
5259 // nee to add a eEncodingPointer if we ever need to know the difference
Kate Stoneb9c1b512016-09-06 20:57:50 +00005260 case clang::Type::ObjCObjectPointer:
5261 case clang::Type::BlockPointer:
5262 case clang::Type::Pointer:
5263 case clang::Type::LValueReference:
5264 case clang::Type::RValueReference:
5265 case clang::Type::MemberPointer:
5266 return lldb::eEncodingUint;
5267 case clang::Type::Complex: {
5268 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5269 if (qual_type->isComplexType())
5270 encoding = lldb::eEncodingIEEE754;
5271 else {
5272 const clang::ComplexType *complex_type =
5273 qual_type->getAsComplexIntegerType();
5274 if (complex_type)
5275 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5276 .GetEncoding(count);
5277 else
5278 encoding = lldb::eEncodingSint;
5279 }
5280 count = 2;
5281 return encoding;
5282 }
5283
5284 case clang::Type::ObjCInterface:
5285 break;
5286 case clang::Type::Record:
5287 break;
5288 case clang::Type::Enum:
5289 return lldb::eEncodingSint;
5290 case clang::Type::Typedef:
5291 return CompilerType(getASTContext(),
5292 llvm::cast<clang::TypedefType>(qual_type)
5293 ->getDecl()
5294 ->getUnderlyingType())
5295 .GetEncoding(count);
5296
5297 case clang::Type::Auto:
5298 return CompilerType(
5299 getASTContext(),
5300 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5301 .GetEncoding(count);
5302
5303 case clang::Type::Elaborated:
5304 return CompilerType(
5305 getASTContext(),
5306 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5307 .GetEncoding(count);
5308
5309 case clang::Type::Paren:
5310 return CompilerType(getASTContext(),
5311 llvm::cast<clang::ParenType>(qual_type)->desugar())
5312 .GetEncoding(count);
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005313 case clang::Type::TypeOfExpr:
5314 return CompilerType(getASTContext(),
5315 llvm::cast<clang::TypeOfExprType>(qual_type)
5316 ->getUnderlyingExpr()
5317 ->getType())
5318 .GetEncoding(count);
5319 case clang::Type::TypeOf:
5320 return CompilerType(
5321 getASTContext(),
5322 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5323 .GetEncoding(count);
5324 case clang::Type::Decltype:
5325 return CompilerType(
5326 getASTContext(),
5327 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5328 .GetEncoding(count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005329 case clang::Type::DependentSizedArray:
5330 case clang::Type::DependentSizedExtVector:
5331 case clang::Type::UnresolvedUsing:
5332 case clang::Type::Attributed:
5333 case clang::Type::TemplateTypeParm:
5334 case clang::Type::SubstTemplateTypeParm:
5335 case clang::Type::SubstTemplateTypeParmPack:
5336 case clang::Type::InjectedClassName:
5337 case clang::Type::DependentName:
5338 case clang::Type::DependentTemplateSpecialization:
5339 case clang::Type::PackExpansion:
5340 case clang::Type::ObjCObject:
5341
Kate Stoneb9c1b512016-09-06 20:57:50 +00005342 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005343 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005344 case clang::Type::Atomic:
5345 case clang::Type::Adjusted:
5346 case clang::Type::Pipe:
5347 break;
5348
5349 // pointer type decayed from an array or function type.
5350 case clang::Type::Decayed:
5351 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005352 case clang::Type::ObjCTypeParam:
5353 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005354
5355 case clang::Type::DependentAddressSpace:
5356 break;
Krasimir Georgiev435e76a2019-05-07 13:59:30 +00005357 case clang::Type::MacroQualified:
5358 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005359 }
5360 count = 0;
5361 return lldb::eEncodingInvalid;
5362}
5363
5364lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5365 if (!type)
5366 return lldb::eFormatDefault;
5367
5368 clang::QualType qual_type(GetCanonicalQualType(type));
5369
5370 switch (qual_type->getTypeClass()) {
5371 case clang::Type::UnaryTransform:
5372 break;
5373
5374 case clang::Type::FunctionNoProto:
5375 case clang::Type::FunctionProto:
5376 break;
5377
5378 case clang::Type::IncompleteArray:
5379 case clang::Type::VariableArray:
5380 break;
5381
5382 case clang::Type::ConstantArray:
5383 return lldb::eFormatVoid; // no value
5384
Fangrui Song8f284882018-07-13 22:40:40 +00005385 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005386 case clang::Type::ExtVector:
5387 case clang::Type::Vector:
5388 break;
5389
5390 case clang::Type::Builtin:
5391 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5392 // default: assert(0 && "Unknown builtin type!");
5393 case clang::BuiltinType::UnknownAny:
5394 case clang::BuiltinType::Void:
5395 case clang::BuiltinType::BoundMember:
5396 break;
5397
5398 case clang::BuiltinType::Bool:
5399 return lldb::eFormatBoolean;
5400 case clang::BuiltinType::Char_S:
5401 case clang::BuiltinType::SChar:
5402 case clang::BuiltinType::WChar_S:
5403 case clang::BuiltinType::Char_U:
5404 case clang::BuiltinType::UChar:
5405 case clang::BuiltinType::WChar_U:
5406 return lldb::eFormatChar;
5407 case clang::BuiltinType::Char16:
5408 return lldb::eFormatUnicode16;
5409 case clang::BuiltinType::Char32:
5410 return lldb::eFormatUnicode32;
5411 case clang::BuiltinType::UShort:
5412 return lldb::eFormatUnsigned;
5413 case clang::BuiltinType::Short:
5414 return lldb::eFormatDecimal;
5415 case clang::BuiltinType::UInt:
5416 return lldb::eFormatUnsigned;
5417 case clang::BuiltinType::Int:
5418 return lldb::eFormatDecimal;
5419 case clang::BuiltinType::ULong:
5420 return lldb::eFormatUnsigned;
5421 case clang::BuiltinType::Long:
5422 return lldb::eFormatDecimal;
5423 case clang::BuiltinType::ULongLong:
5424 return lldb::eFormatUnsigned;
5425 case clang::BuiltinType::LongLong:
5426 return lldb::eFormatDecimal;
5427 case clang::BuiltinType::UInt128:
5428 return lldb::eFormatUnsigned;
5429 case clang::BuiltinType::Int128:
5430 return lldb::eFormatDecimal;
5431 case clang::BuiltinType::Half:
5432 case clang::BuiltinType::Float:
5433 case clang::BuiltinType::Double:
5434 case clang::BuiltinType::LongDouble:
5435 return lldb::eFormatFloat;
5436 default:
5437 return lldb::eFormatHex;
5438 }
5439 break;
5440 case clang::Type::ObjCObjectPointer:
5441 return lldb::eFormatHex;
5442 case clang::Type::BlockPointer:
5443 return lldb::eFormatHex;
5444 case clang::Type::Pointer:
5445 return lldb::eFormatHex;
5446 case clang::Type::LValueReference:
5447 case clang::Type::RValueReference:
5448 return lldb::eFormatHex;
5449 case clang::Type::MemberPointer:
5450 break;
5451 case clang::Type::Complex: {
5452 if (qual_type->isComplexType())
5453 return lldb::eFormatComplex;
5454 else
5455 return lldb::eFormatComplexInteger;
5456 }
5457 case clang::Type::ObjCInterface:
5458 break;
5459 case clang::Type::Record:
5460 break;
5461 case clang::Type::Enum:
5462 return lldb::eFormatEnum;
5463 case clang::Type::Typedef:
5464 return CompilerType(getASTContext(),
5465 llvm::cast<clang::TypedefType>(qual_type)
5466 ->getDecl()
5467 ->getUnderlyingType())
5468 .GetFormat();
5469 case clang::Type::Auto:
5470 return CompilerType(getASTContext(),
5471 llvm::cast<clang::AutoType>(qual_type)->desugar())
5472 .GetFormat();
5473 case clang::Type::Paren:
5474 return CompilerType(getASTContext(),
5475 llvm::cast<clang::ParenType>(qual_type)->desugar())
5476 .GetFormat();
5477 case clang::Type::Elaborated:
5478 return CompilerType(
5479 getASTContext(),
5480 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5481 .GetFormat();
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005482 case clang::Type::TypeOfExpr:
5483 return CompilerType(getASTContext(),
5484 llvm::cast<clang::TypeOfExprType>(qual_type)
5485 ->getUnderlyingExpr()
5486 ->getType())
5487 .GetFormat();
5488 case clang::Type::TypeOf:
5489 return CompilerType(
5490 getASTContext(),
5491 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5492 .GetFormat();
5493 case clang::Type::Decltype:
5494 return CompilerType(
5495 getASTContext(),
5496 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5497 .GetFormat();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005498 case clang::Type::DependentSizedArray:
5499 case clang::Type::DependentSizedExtVector:
5500 case clang::Type::UnresolvedUsing:
5501 case clang::Type::Attributed:
5502 case clang::Type::TemplateTypeParm:
5503 case clang::Type::SubstTemplateTypeParm:
5504 case clang::Type::SubstTemplateTypeParmPack:
5505 case clang::Type::InjectedClassName:
5506 case clang::Type::DependentName:
5507 case clang::Type::DependentTemplateSpecialization:
5508 case clang::Type::PackExpansion:
5509 case clang::Type::ObjCObject:
5510
Kate Stoneb9c1b512016-09-06 20:57:50 +00005511 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005512 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005513 case clang::Type::Atomic:
5514 case clang::Type::Adjusted:
5515 case clang::Type::Pipe:
5516 break;
5517
5518 // pointer type decayed from an array or function type.
5519 case clang::Type::Decayed:
5520 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005521 case clang::Type::ObjCTypeParam:
5522 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005523
5524 case clang::Type::DependentAddressSpace:
5525 break;
Krasimir Georgiev435e76a2019-05-07 13:59:30 +00005526 case clang::Type::MacroQualified:
5527 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005528 }
5529 // We don't know hot to display this type...
5530 return lldb::eFormatBytes;
5531}
5532
5533static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5534 bool check_superclass) {
5535 while (class_interface_decl) {
5536 if (class_interface_decl->ivar_size() > 0)
5537 return true;
5538
5539 if (check_superclass)
5540 class_interface_decl = class_interface_decl->getSuperClass();
5541 else
5542 break;
5543 }
5544 return false;
5545}
5546
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00005547static Optional<SymbolFile::ArrayInfo>
Adrian Prantleca07c52018-11-05 20:49:07 +00005548GetDynamicArrayInfo(ClangASTContext &ast, SymbolFile *sym_file,
5549 clang::QualType qual_type,
5550 const ExecutionContext *exe_ctx) {
5551 if (qual_type->isIncompleteArrayType())
5552 if (auto *metadata = ast.GetMetadata(qual_type.getAsOpaquePtr()))
Pavel Labathffec31e2018-12-28 13:34:44 +00005553 return sym_file->GetDynamicArrayInfoForUID(metadata->GetUserID(),
5554 exe_ctx);
Adrian Prantleca07c52018-11-05 20:49:07 +00005555 return llvm::None;
5556}
5557
Kate Stoneb9c1b512016-09-06 20:57:50 +00005558uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
Adrian Prantleca07c52018-11-05 20:49:07 +00005559 bool omit_empty_base_classes,
5560 const ExecutionContext *exe_ctx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005561 if (!type)
5562 return 0;
5563
5564 uint32_t num_children = 0;
5565 clang::QualType qual_type(GetQualType(type));
5566 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5567 switch (type_class) {
5568 case clang::Type::Builtin:
5569 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5570 case clang::BuiltinType::ObjCId: // child is Class
5571 case clang::BuiltinType::ObjCClass: // child is Class
5572 num_children = 1;
5573 break;
5574
5575 default:
5576 break;
5577 }
5578 break;
5579
5580 case clang::Type::Complex:
5581 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005582 case clang::Type::Record:
5583 if (GetCompleteQualType(getASTContext(), qual_type)) {
5584 const clang::RecordType *record_type =
5585 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5586 const clang::RecordDecl *record_decl = record_type->getDecl();
5587 assert(record_decl);
5588 const clang::CXXRecordDecl *cxx_record_decl =
5589 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5590 if (cxx_record_decl) {
5591 if (omit_empty_base_classes) {
Adrian Prantl05097242018-04-30 16:49:04 +00005592 // Check each base classes to see if it or any of its base classes
5593 // contain any fields. This can help limit the noise in variable
5594 // views by not having to show base classes that contain no members.
Kate Stoneb9c1b512016-09-06 20:57:50 +00005595 clang::CXXRecordDecl::base_class_const_iterator base_class,
5596 base_class_end;
5597 for (base_class = cxx_record_decl->bases_begin(),
5598 base_class_end = cxx_record_decl->bases_end();
5599 base_class != base_class_end; ++base_class) {
5600 const clang::CXXRecordDecl *base_class_decl =
5601 llvm::cast<clang::CXXRecordDecl>(
5602 base_class->getType()
5603 ->getAs<clang::RecordType>()
5604 ->getDecl());
5605
5606 // Skip empty base classes
Jonas Devliegherea6682a42018-12-15 00:15:33 +00005607 if (!ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00005608 continue;
5609
5610 num_children++;
5611 }
5612 } else {
5613 // Include all base classes
5614 num_children += cxx_record_decl->getNumBases();
5615 }
5616 }
5617 clang::RecordDecl::field_iterator field, field_end;
5618 for (field = record_decl->field_begin(),
5619 field_end = record_decl->field_end();
5620 field != field_end; ++field)
5621 ++num_children;
5622 }
5623 break;
5624
5625 case clang::Type::ObjCObject:
5626 case clang::Type::ObjCInterface:
5627 if (GetCompleteQualType(getASTContext(), qual_type)) {
5628 const clang::ObjCObjectType *objc_class_type =
5629 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5630 assert(objc_class_type);
5631 if (objc_class_type) {
5632 clang::ObjCInterfaceDecl *class_interface_decl =
5633 objc_class_type->getInterface();
5634
5635 if (class_interface_decl) {
5636
5637 clang::ObjCInterfaceDecl *superclass_interface_decl =
5638 class_interface_decl->getSuperClass();
5639 if (superclass_interface_decl) {
5640 if (omit_empty_base_classes) {
5641 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5642 ++num_children;
5643 } else
5644 ++num_children;
5645 }
5646
5647 num_children += class_interface_decl->ivar_size();
5648 }
5649 }
5650 }
5651 break;
5652
5653 case clang::Type::ObjCObjectPointer: {
5654 const clang::ObjCObjectPointerType *pointer_type =
5655 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5656 clang::QualType pointee_type = pointer_type->getPointeeType();
5657 uint32_t num_pointee_children =
5658 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005659 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005660 // If this type points to a simple type, then it has 1 child
5661 if (num_pointee_children == 0)
5662 num_children = 1;
5663 else
5664 num_children = num_pointee_children;
5665 } break;
5666
5667 case clang::Type::Vector:
5668 case clang::Type::ExtVector:
5669 num_children =
5670 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5671 break;
5672
5673 case clang::Type::ConstantArray:
5674 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5675 ->getSize()
5676 .getLimitedValue();
5677 break;
Adrian Prantleca07c52018-11-05 20:49:07 +00005678 case clang::Type::IncompleteArray:
5679 if (auto array_info =
5680 GetDynamicArrayInfo(*this, GetSymbolFile(), qual_type, exe_ctx))
5681 // Only 1-dimensional arrays are supported.
5682 num_children = array_info->element_orders.size()
5683 ? array_info->element_orders.back()
5684 : 0;
5685 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005686
5687 case clang::Type::Pointer: {
5688 const clang::PointerType *pointer_type =
5689 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5690 clang::QualType pointee_type(pointer_type->getPointeeType());
5691 uint32_t num_pointee_children =
5692 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005693 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005694 if (num_pointee_children == 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00005695 // We have a pointer to a pointee type that claims it has no children. We
5696 // will want to look at
Kate Stoneb9c1b512016-09-06 20:57:50 +00005697 num_children = GetNumPointeeChildren(pointee_type);
5698 } else
5699 num_children = num_pointee_children;
5700 } break;
5701
5702 case clang::Type::LValueReference:
5703 case clang::Type::RValueReference: {
5704 const clang::ReferenceType *reference_type =
5705 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5706 clang::QualType pointee_type = reference_type->getPointeeType();
5707 uint32_t num_pointee_children =
5708 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005709 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005710 // If this type points to a simple type, then it has 1 child
5711 if (num_pointee_children == 0)
5712 num_children = 1;
5713 else
5714 num_children = num_pointee_children;
5715 } break;
5716
5717 case clang::Type::Typedef:
5718 num_children =
5719 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5720 ->getDecl()
5721 ->getUnderlyingType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005722 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005723 break;
5724
5725 case clang::Type::Auto:
5726 num_children =
5727 CompilerType(getASTContext(),
5728 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005729 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005730 break;
5731
5732 case clang::Type::Elaborated:
5733 num_children =
5734 CompilerType(
5735 getASTContext(),
5736 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005737 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005738 break;
5739
5740 case clang::Type::Paren:
5741 num_children =
5742 CompilerType(getASTContext(),
5743 llvm::cast<clang::ParenType>(qual_type)->desugar())
Adrian Prantleca07c52018-11-05 20:49:07 +00005744 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005745 break;
5746 default:
5747 break;
5748 }
5749 return num_children;
5750}
5751
Adrian Prantl0e4c4822019-03-06 21:22:25 +00005752CompilerType ClangASTContext::GetBuiltinTypeByName(ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005753 return GetBasicType(GetBasicTypeEnumeration(name));
Greg Clayton56939cb2015-09-17 22:23:34 +00005754}
5755
Greg Claytond8d4a572015-08-11 21:38:15 +00005756lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00005757ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5758 if (type) {
5759 clang::QualType qual_type(GetQualType(type));
5760 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5761 if (type_class == clang::Type::Builtin) {
5762 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5763 case clang::BuiltinType::Void:
5764 return eBasicTypeVoid;
5765 case clang::BuiltinType::Bool:
5766 return eBasicTypeBool;
5767 case clang::BuiltinType::Char_S:
5768 return eBasicTypeSignedChar;
5769 case clang::BuiltinType::Char_U:
5770 return eBasicTypeUnsignedChar;
5771 case clang::BuiltinType::Char16:
5772 return eBasicTypeChar16;
5773 case clang::BuiltinType::Char32:
5774 return eBasicTypeChar32;
5775 case clang::BuiltinType::UChar:
5776 return eBasicTypeUnsignedChar;
5777 case clang::BuiltinType::SChar:
5778 return eBasicTypeSignedChar;
5779 case clang::BuiltinType::WChar_S:
5780 return eBasicTypeSignedWChar;
5781 case clang::BuiltinType::WChar_U:
5782 return eBasicTypeUnsignedWChar;
5783 case clang::BuiltinType::Short:
5784 return eBasicTypeShort;
5785 case clang::BuiltinType::UShort:
5786 return eBasicTypeUnsignedShort;
5787 case clang::BuiltinType::Int:
5788 return eBasicTypeInt;
5789 case clang::BuiltinType::UInt:
5790 return eBasicTypeUnsignedInt;
5791 case clang::BuiltinType::Long:
5792 return eBasicTypeLong;
5793 case clang::BuiltinType::ULong:
5794 return eBasicTypeUnsignedLong;
5795 case clang::BuiltinType::LongLong:
5796 return eBasicTypeLongLong;
5797 case clang::BuiltinType::ULongLong:
5798 return eBasicTypeUnsignedLongLong;
5799 case clang::BuiltinType::Int128:
5800 return eBasicTypeInt128;
5801 case clang::BuiltinType::UInt128:
5802 return eBasicTypeUnsignedInt128;
5803
5804 case clang::BuiltinType::Half:
5805 return eBasicTypeHalf;
5806 case clang::BuiltinType::Float:
5807 return eBasicTypeFloat;
5808 case clang::BuiltinType::Double:
5809 return eBasicTypeDouble;
5810 case clang::BuiltinType::LongDouble:
5811 return eBasicTypeLongDouble;
5812
5813 case clang::BuiltinType::NullPtr:
5814 return eBasicTypeNullPtr;
5815 case clang::BuiltinType::ObjCId:
5816 return eBasicTypeObjCID;
5817 case clang::BuiltinType::ObjCClass:
5818 return eBasicTypeObjCClass;
5819 case clang::BuiltinType::ObjCSel:
5820 return eBasicTypeObjCSel;
5821 default:
5822 return eBasicTypeOther;
5823 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005824 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005825 }
5826 return eBasicTypeInvalid;
Greg Claytond8d4a572015-08-11 21:38:15 +00005827}
5828
Kate Stoneb9c1b512016-09-06 20:57:50 +00005829void ClangASTContext::ForEachEnumerator(
5830 lldb::opaque_compiler_type_t type,
5831 std::function<bool(const CompilerType &integer_type,
Adrian Prantl0e4c4822019-03-06 21:22:25 +00005832 ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00005833 const llvm::APSInt &value)> const &callback) {
5834 const clang::EnumType *enum_type =
5835 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5836 if (enum_type) {
5837 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5838 if (enum_decl) {
5839 CompilerType integer_type(this,
5840 enum_decl->getIntegerType().getAsOpaquePtr());
Greg Clayton99558cc42015-08-24 23:46:31 +00005841
Kate Stoneb9c1b512016-09-06 20:57:50 +00005842 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5843 for (enum_pos = enum_decl->enumerator_begin(),
5844 enum_end_pos = enum_decl->enumerator_end();
5845 enum_pos != enum_end_pos; ++enum_pos) {
5846 ConstString name(enum_pos->getNameAsString().c_str());
5847 if (!callback(integer_type, name, enum_pos->getInitVal()))
5848 break;
5849 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005850 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005851 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005852}
5853
Greg Claytond8d4a572015-08-11 21:38:15 +00005854#pragma mark Aggregate Types
5855
Kate Stoneb9c1b512016-09-06 20:57:50 +00005856uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5857 if (!type)
5858 return 0;
Enrico Granata36f51e42015-12-18 22:41:25 +00005859
Kate Stoneb9c1b512016-09-06 20:57:50 +00005860 uint32_t count = 0;
5861 clang::QualType qual_type(GetCanonicalQualType(type));
5862 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5863 switch (type_class) {
5864 case clang::Type::Record:
5865 if (GetCompleteType(type)) {
5866 const clang::RecordType *record_type =
5867 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5868 if (record_type) {
5869 clang::RecordDecl *record_decl = record_type->getDecl();
5870 if (record_decl) {
5871 uint32_t field_idx = 0;
5872 clang::RecordDecl::field_iterator field, field_end;
5873 for (field = record_decl->field_begin(),
5874 field_end = record_decl->field_end();
5875 field != field_end; ++field)
5876 ++field_idx;
5877 count = field_idx;
5878 }
5879 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005880 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005881 break;
5882
5883 case clang::Type::Typedef:
5884 count =
5885 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5886 ->getDecl()
5887 ->getUnderlyingType())
5888 .GetNumFields();
5889 break;
5890
5891 case clang::Type::Auto:
5892 count =
5893 CompilerType(getASTContext(),
5894 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5895 .GetNumFields();
5896 break;
5897
5898 case clang::Type::Elaborated:
5899 count = CompilerType(
5900 getASTContext(),
5901 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5902 .GetNumFields();
5903 break;
5904
5905 case clang::Type::Paren:
5906 count = CompilerType(getASTContext(),
5907 llvm::cast<clang::ParenType>(qual_type)->desugar())
5908 .GetNumFields();
5909 break;
5910
Sean Callananf9c622a2016-09-30 18:44:43 +00005911 case clang::Type::ObjCObjectPointer: {
5912 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005913 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005914 const clang::ObjCInterfaceType *objc_interface_type =
5915 objc_class_type->getInterfaceType();
5916 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005917 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5918 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005919 clang::ObjCInterfaceDecl *class_interface_decl =
5920 objc_interface_type->getDecl();
5921 if (class_interface_decl) {
5922 count = class_interface_decl->ivar_size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005923 }
5924 }
5925 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005926 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005927
5928 case clang::Type::ObjCObject:
5929 case clang::Type::ObjCInterface:
5930 if (GetCompleteType(type)) {
5931 const clang::ObjCObjectType *objc_class_type =
5932 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5933 if (objc_class_type) {
5934 clang::ObjCInterfaceDecl *class_interface_decl =
5935 objc_class_type->getInterface();
5936
5937 if (class_interface_decl)
5938 count = class_interface_decl->ivar_size();
5939 }
5940 }
5941 break;
5942
5943 default:
5944 break;
5945 }
5946 return count;
Greg Claytond8d4a572015-08-11 21:38:15 +00005947}
5948
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005949static lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005950GetObjCFieldAtIndex(clang::ASTContext *ast,
5951 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5952 std::string &name, uint64_t *bit_offset_ptr,
5953 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5954 if (class_interface_decl) {
5955 if (idx < (class_interface_decl->ivar_size())) {
5956 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5957 ivar_end = class_interface_decl->ivar_end();
5958 uint32_t ivar_idx = 0;
5959
5960 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5961 ++ivar_pos, ++ivar_idx) {
5962 if (ivar_idx == idx) {
5963 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5964
5965 clang::QualType ivar_qual_type(ivar_decl->getType());
5966
5967 name.assign(ivar_decl->getNameAsString());
5968
5969 if (bit_offset_ptr) {
5970 const clang::ASTRecordLayout &interface_layout =
5971 ast->getASTObjCInterfaceLayout(class_interface_decl);
5972 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5973 }
5974
5975 const bool is_bitfield = ivar_pos->isBitField();
5976
5977 if (bitfield_bit_size_ptr) {
5978 *bitfield_bit_size_ptr = 0;
5979
5980 if (is_bitfield && ast) {
5981 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
Hans Wennborg30ce9622018-11-28 14:30:18 +00005982 clang::Expr::EvalResult result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005983 if (bitfield_bit_size_expr &&
Hans Wennborg30ce9622018-11-28 14:30:18 +00005984 bitfield_bit_size_expr->EvaluateAsInt(result, *ast)) {
5985 llvm::APSInt bitfield_apsint = result.Val.getInt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005986 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5987 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005988 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005989 }
5990 if (is_bitfield_ptr)
5991 *is_bitfield_ptr = is_bitfield;
5992
5993 return ivar_qual_type.getAsOpaquePtr();
Greg Claytond8d4a572015-08-11 21:38:15 +00005994 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005995 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005996 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005997 }
5998 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00005999}
6000
Kate Stoneb9c1b512016-09-06 20:57:50 +00006001CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
6002 size_t idx, std::string &name,
6003 uint64_t *bit_offset_ptr,
6004 uint32_t *bitfield_bit_size_ptr,
6005 bool *is_bitfield_ptr) {
6006 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006007 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006008
6009 clang::QualType qual_type(GetCanonicalQualType(type));
6010 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6011 switch (type_class) {
6012 case clang::Type::Record:
6013 if (GetCompleteType(type)) {
6014 const clang::RecordType *record_type =
6015 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6016 const clang::RecordDecl *record_decl = record_type->getDecl();
6017 uint32_t field_idx = 0;
6018 clang::RecordDecl::field_iterator field, field_end;
6019 for (field = record_decl->field_begin(),
6020 field_end = record_decl->field_end();
6021 field != field_end; ++field, ++field_idx) {
6022 if (idx == field_idx) {
6023 // Print the member type if requested
6024 // Print the member name and equal sign
6025 name.assign(field->getNameAsString());
6026
6027 // Figure out the type byte size (field_type_info.first) and
6028 // alignment (field_type_info.second) from the AST context.
6029 if (bit_offset_ptr) {
6030 const clang::ASTRecordLayout &record_layout =
6031 getASTContext()->getASTRecordLayout(record_decl);
6032 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
6033 }
6034
6035 const bool is_bitfield = field->isBitField();
6036
6037 if (bitfield_bit_size_ptr) {
6038 *bitfield_bit_size_ptr = 0;
6039
6040 if (is_bitfield) {
6041 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
Hans Wennborg30ce9622018-11-28 14:30:18 +00006042 clang::Expr::EvalResult result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006043 if (bitfield_bit_size_expr &&
Hans Wennborg30ce9622018-11-28 14:30:18 +00006044 bitfield_bit_size_expr->EvaluateAsInt(result,
Kate Stoneb9c1b512016-09-06 20:57:50 +00006045 *getASTContext())) {
Hans Wennborg30ce9622018-11-28 14:30:18 +00006046 llvm::APSInt bitfield_apsint = result.Val.getInt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006047 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
6048 }
6049 }
6050 }
6051 if (is_bitfield_ptr)
6052 *is_bitfield_ptr = is_bitfield;
6053
6054 return CompilerType(getASTContext(), field->getType());
6055 }
6056 }
6057 }
6058 break;
6059
Sean Callananf9c622a2016-09-30 18:44:43 +00006060 case clang::Type::ObjCObjectPointer: {
6061 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00006062 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00006063 const clang::ObjCInterfaceType *objc_interface_type =
6064 objc_class_type->getInterfaceType();
6065 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00006066 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
6067 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00006068 clang::ObjCInterfaceDecl *class_interface_decl =
6069 objc_interface_type->getDecl();
6070 if (class_interface_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006071 return CompilerType(
6072 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
6073 idx, name, bit_offset_ptr,
6074 bitfield_bit_size_ptr, is_bitfield_ptr));
6075 }
6076 }
6077 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00006078 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006079
6080 case clang::Type::ObjCObject:
6081 case clang::Type::ObjCInterface:
6082 if (GetCompleteType(type)) {
6083 const clang::ObjCObjectType *objc_class_type =
6084 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6085 assert(objc_class_type);
6086 if (objc_class_type) {
6087 clang::ObjCInterfaceDecl *class_interface_decl =
6088 objc_class_type->getInterface();
6089 return CompilerType(
6090 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
6091 idx, name, bit_offset_ptr,
6092 bitfield_bit_size_ptr, is_bitfield_ptr));
6093 }
6094 }
6095 break;
6096
6097 case clang::Type::Typedef:
6098 return CompilerType(getASTContext(),
6099 llvm::cast<clang::TypedefType>(qual_type)
6100 ->getDecl()
6101 ->getUnderlyingType())
6102 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6103 is_bitfield_ptr);
6104
6105 case clang::Type::Auto:
6106 return CompilerType(
6107 getASTContext(),
6108 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
6109 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6110 is_bitfield_ptr);
6111
6112 case clang::Type::Elaborated:
6113 return CompilerType(
6114 getASTContext(),
6115 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
6116 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6117 is_bitfield_ptr);
6118
6119 case clang::Type::Paren:
6120 return CompilerType(getASTContext(),
6121 llvm::cast<clang::ParenType>(qual_type)->desugar())
6122 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6123 is_bitfield_ptr);
6124
6125 default:
6126 break;
6127 }
6128 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006129}
6130
Greg Clayton99558cc42015-08-24 23:46:31 +00006131uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006132ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
6133 uint32_t count = 0;
6134 clang::QualType qual_type(GetCanonicalQualType(type));
6135 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6136 switch (type_class) {
6137 case clang::Type::Record:
6138 if (GetCompleteType(type)) {
6139 const clang::CXXRecordDecl *cxx_record_decl =
6140 qual_type->getAsCXXRecordDecl();
6141 if (cxx_record_decl)
6142 count = cxx_record_decl->getNumBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006143 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006144 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006145
Kate Stoneb9c1b512016-09-06 20:57:50 +00006146 case clang::Type::ObjCObjectPointer:
6147 count = GetPointeeType(type).GetNumDirectBaseClasses();
6148 break;
6149
6150 case clang::Type::ObjCObject:
6151 if (GetCompleteType(type)) {
6152 const clang::ObjCObjectType *objc_class_type =
6153 qual_type->getAsObjCQualifiedInterfaceType();
6154 if (objc_class_type) {
6155 clang::ObjCInterfaceDecl *class_interface_decl =
6156 objc_class_type->getInterface();
6157
6158 if (class_interface_decl && class_interface_decl->getSuperClass())
6159 count = 1;
6160 }
6161 }
6162 break;
6163 case clang::Type::ObjCInterface:
6164 if (GetCompleteType(type)) {
6165 const clang::ObjCInterfaceType *objc_interface_type =
6166 qual_type->getAs<clang::ObjCInterfaceType>();
6167 if (objc_interface_type) {
6168 clang::ObjCInterfaceDecl *class_interface_decl =
6169 objc_interface_type->getInterface();
6170
6171 if (class_interface_decl && class_interface_decl->getSuperClass())
6172 count = 1;
6173 }
6174 }
6175 break;
6176
6177 case clang::Type::Typedef:
6178 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6179 ->getDecl()
6180 ->getUnderlyingType()
6181 .getAsOpaquePtr());
6182 break;
6183
6184 case clang::Type::Auto:
6185 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6186 ->getDeducedType()
6187 .getAsOpaquePtr());
6188 break;
6189
6190 case clang::Type::Elaborated:
6191 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6192 ->getNamedType()
6193 .getAsOpaquePtr());
6194 break;
6195
6196 case clang::Type::Paren:
6197 return GetNumDirectBaseClasses(
6198 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6199
6200 default:
6201 break;
6202 }
6203 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006204}
6205
6206uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006207ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
6208 uint32_t count = 0;
6209 clang::QualType qual_type(GetCanonicalQualType(type));
6210 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6211 switch (type_class) {
6212 case clang::Type::Record:
6213 if (GetCompleteType(type)) {
6214 const clang::CXXRecordDecl *cxx_record_decl =
6215 qual_type->getAsCXXRecordDecl();
6216 if (cxx_record_decl)
6217 count = cxx_record_decl->getNumVBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006218 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006219 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006220
Kate Stoneb9c1b512016-09-06 20:57:50 +00006221 case clang::Type::Typedef:
6222 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6223 ->getDecl()
6224 ->getUnderlyingType()
6225 .getAsOpaquePtr());
6226 break;
6227
6228 case clang::Type::Auto:
6229 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6230 ->getDeducedType()
6231 .getAsOpaquePtr());
6232 break;
6233
6234 case clang::Type::Elaborated:
6235 count =
6236 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6237 ->getNamedType()
6238 .getAsOpaquePtr());
6239 break;
6240
6241 case clang::Type::Paren:
6242 count = GetNumVirtualBaseClasses(
6243 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6244 break;
6245
6246 default:
6247 break;
6248 }
6249 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006250}
6251
Kate Stoneb9c1b512016-09-06 20:57:50 +00006252CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
6253 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6254 clang::QualType qual_type(GetCanonicalQualType(type));
6255 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6256 switch (type_class) {
6257 case clang::Type::Record:
6258 if (GetCompleteType(type)) {
6259 const clang::CXXRecordDecl *cxx_record_decl =
6260 qual_type->getAsCXXRecordDecl();
6261 if (cxx_record_decl) {
6262 uint32_t curr_idx = 0;
6263 clang::CXXRecordDecl::base_class_const_iterator base_class,
6264 base_class_end;
6265 for (base_class = cxx_record_decl->bases_begin(),
6266 base_class_end = cxx_record_decl->bases_end();
6267 base_class != base_class_end; ++base_class, ++curr_idx) {
6268 if (curr_idx == idx) {
6269 if (bit_offset_ptr) {
6270 const clang::ASTRecordLayout &record_layout =
6271 getASTContext()->getASTRecordLayout(cxx_record_decl);
6272 const clang::CXXRecordDecl *base_class_decl =
6273 llvm::cast<clang::CXXRecordDecl>(
6274 base_class->getType()
6275 ->getAs<clang::RecordType>()
6276 ->getDecl());
6277 if (base_class->isVirtual())
6278 *bit_offset_ptr =
6279 record_layout.getVBaseClassOffset(base_class_decl)
6280 .getQuantity() *
6281 8;
6282 else
6283 *bit_offset_ptr =
6284 record_layout.getBaseClassOffset(base_class_decl)
6285 .getQuantity() *
6286 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006287 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006288 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6289 }
6290 }
6291 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006292 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006293 break;
6294
6295 case clang::Type::ObjCObjectPointer:
6296 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6297
6298 case clang::Type::ObjCObject:
6299 if (idx == 0 && GetCompleteType(type)) {
6300 const clang::ObjCObjectType *objc_class_type =
6301 qual_type->getAsObjCQualifiedInterfaceType();
6302 if (objc_class_type) {
6303 clang::ObjCInterfaceDecl *class_interface_decl =
6304 objc_class_type->getInterface();
6305
6306 if (class_interface_decl) {
6307 clang::ObjCInterfaceDecl *superclass_interface_decl =
6308 class_interface_decl->getSuperClass();
6309 if (superclass_interface_decl) {
6310 if (bit_offset_ptr)
6311 *bit_offset_ptr = 0;
6312 return CompilerType(getASTContext(),
6313 getASTContext()->getObjCInterfaceType(
6314 superclass_interface_decl));
6315 }
6316 }
6317 }
6318 }
6319 break;
6320 case clang::Type::ObjCInterface:
6321 if (idx == 0 && GetCompleteType(type)) {
6322 const clang::ObjCObjectType *objc_interface_type =
6323 qual_type->getAs<clang::ObjCInterfaceType>();
6324 if (objc_interface_type) {
6325 clang::ObjCInterfaceDecl *class_interface_decl =
6326 objc_interface_type->getInterface();
6327
6328 if (class_interface_decl) {
6329 clang::ObjCInterfaceDecl *superclass_interface_decl =
6330 class_interface_decl->getSuperClass();
6331 if (superclass_interface_decl) {
6332 if (bit_offset_ptr)
6333 *bit_offset_ptr = 0;
6334 return CompilerType(getASTContext(),
6335 getASTContext()->getObjCInterfaceType(
6336 superclass_interface_decl));
6337 }
6338 }
6339 }
6340 }
6341 break;
6342
6343 case clang::Type::Typedef:
6344 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6345 ->getDecl()
6346 ->getUnderlyingType()
6347 .getAsOpaquePtr(),
6348 idx, bit_offset_ptr);
6349
6350 case clang::Type::Auto:
6351 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6352 ->getDeducedType()
6353 .getAsOpaquePtr(),
6354 idx, bit_offset_ptr);
6355
6356 case clang::Type::Elaborated:
6357 return GetDirectBaseClassAtIndex(
6358 llvm::cast<clang::ElaboratedType>(qual_type)
6359 ->getNamedType()
6360 .getAsOpaquePtr(),
6361 idx, bit_offset_ptr);
6362
6363 case clang::Type::Paren:
6364 return GetDirectBaseClassAtIndex(
6365 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6366 idx, bit_offset_ptr);
6367
6368 default:
6369 break;
6370 }
6371 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006372}
6373
Kate Stoneb9c1b512016-09-06 20:57:50 +00006374CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6375 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6376 clang::QualType qual_type(GetCanonicalQualType(type));
6377 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6378 switch (type_class) {
6379 case clang::Type::Record:
6380 if (GetCompleteType(type)) {
6381 const clang::CXXRecordDecl *cxx_record_decl =
6382 qual_type->getAsCXXRecordDecl();
6383 if (cxx_record_decl) {
6384 uint32_t curr_idx = 0;
6385 clang::CXXRecordDecl::base_class_const_iterator base_class,
6386 base_class_end;
6387 for (base_class = cxx_record_decl->vbases_begin(),
6388 base_class_end = cxx_record_decl->vbases_end();
6389 base_class != base_class_end; ++base_class, ++curr_idx) {
6390 if (curr_idx == idx) {
6391 if (bit_offset_ptr) {
6392 const clang::ASTRecordLayout &record_layout =
6393 getASTContext()->getASTRecordLayout(cxx_record_decl);
6394 const clang::CXXRecordDecl *base_class_decl =
6395 llvm::cast<clang::CXXRecordDecl>(
6396 base_class->getType()
6397 ->getAs<clang::RecordType>()
6398 ->getDecl());
6399 *bit_offset_ptr =
6400 record_layout.getVBaseClassOffset(base_class_decl)
6401 .getQuantity() *
6402 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006403 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006404 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6405 }
6406 }
6407 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006408 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006409 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006410
Kate Stoneb9c1b512016-09-06 20:57:50 +00006411 case clang::Type::Typedef:
6412 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6413 ->getDecl()
6414 ->getUnderlyingType()
6415 .getAsOpaquePtr(),
6416 idx, bit_offset_ptr);
6417
6418 case clang::Type::Auto:
6419 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6420 ->getDeducedType()
6421 .getAsOpaquePtr(),
6422 idx, bit_offset_ptr);
6423
6424 case clang::Type::Elaborated:
6425 return GetVirtualBaseClassAtIndex(
6426 llvm::cast<clang::ElaboratedType>(qual_type)
6427 ->getNamedType()
6428 .getAsOpaquePtr(),
6429 idx, bit_offset_ptr);
6430
6431 case clang::Type::Paren:
6432 return GetVirtualBaseClassAtIndex(
6433 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6434 idx, bit_offset_ptr);
6435
6436 default:
6437 break;
6438 }
6439 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006440}
6441
Greg Claytond8d4a572015-08-11 21:38:15 +00006442// If a pointer to a pointee type (the clang_type arg) says that it has no
6443// children, then we either need to trust it, or override it and return a
6444// different result. For example, an "int *" has one child that is an integer,
6445// but a function pointer doesn't have any children. Likewise if a Record type
6446// claims it has no children, then there really is nothing to show.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006447uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6448 if (type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00006449 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006450
6451 clang::QualType qual_type(type.getCanonicalType());
6452 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6453 switch (type_class) {
6454 case clang::Type::Builtin:
6455 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6456 case clang::BuiltinType::UnknownAny:
6457 case clang::BuiltinType::Void:
6458 case clang::BuiltinType::NullPtr:
6459 case clang::BuiltinType::OCLEvent:
6460 case clang::BuiltinType::OCLImage1dRO:
6461 case clang::BuiltinType::OCLImage1dWO:
6462 case clang::BuiltinType::OCLImage1dRW:
6463 case clang::BuiltinType::OCLImage1dArrayRO:
6464 case clang::BuiltinType::OCLImage1dArrayWO:
6465 case clang::BuiltinType::OCLImage1dArrayRW:
6466 case clang::BuiltinType::OCLImage1dBufferRO:
6467 case clang::BuiltinType::OCLImage1dBufferWO:
6468 case clang::BuiltinType::OCLImage1dBufferRW:
6469 case clang::BuiltinType::OCLImage2dRO:
6470 case clang::BuiltinType::OCLImage2dWO:
6471 case clang::BuiltinType::OCLImage2dRW:
6472 case clang::BuiltinType::OCLImage2dArrayRO:
6473 case clang::BuiltinType::OCLImage2dArrayWO:
6474 case clang::BuiltinType::OCLImage2dArrayRW:
6475 case clang::BuiltinType::OCLImage3dRO:
6476 case clang::BuiltinType::OCLImage3dWO:
6477 case clang::BuiltinType::OCLImage3dRW:
6478 case clang::BuiltinType::OCLSampler:
6479 return 0;
6480 case clang::BuiltinType::Bool:
6481 case clang::BuiltinType::Char_U:
6482 case clang::BuiltinType::UChar:
6483 case clang::BuiltinType::WChar_U:
6484 case clang::BuiltinType::Char16:
6485 case clang::BuiltinType::Char32:
6486 case clang::BuiltinType::UShort:
6487 case clang::BuiltinType::UInt:
6488 case clang::BuiltinType::ULong:
6489 case clang::BuiltinType::ULongLong:
6490 case clang::BuiltinType::UInt128:
6491 case clang::BuiltinType::Char_S:
6492 case clang::BuiltinType::SChar:
6493 case clang::BuiltinType::WChar_S:
6494 case clang::BuiltinType::Short:
6495 case clang::BuiltinType::Int:
6496 case clang::BuiltinType::Long:
6497 case clang::BuiltinType::LongLong:
6498 case clang::BuiltinType::Int128:
6499 case clang::BuiltinType::Float:
6500 case clang::BuiltinType::Double:
6501 case clang::BuiltinType::LongDouble:
6502 case clang::BuiltinType::Dependent:
6503 case clang::BuiltinType::Overload:
6504 case clang::BuiltinType::ObjCId:
6505 case clang::BuiltinType::ObjCClass:
6506 case clang::BuiltinType::ObjCSel:
6507 case clang::BuiltinType::BoundMember:
6508 case clang::BuiltinType::Half:
6509 case clang::BuiltinType::ARCUnbridgedCast:
6510 case clang::BuiltinType::PseudoObject:
6511 case clang::BuiltinType::BuiltinFn:
6512 case clang::BuiltinType::OMPArraySection:
6513 return 1;
6514 default:
6515 return 0;
6516 }
6517 break;
6518
6519 case clang::Type::Complex:
6520 return 1;
6521 case clang::Type::Pointer:
6522 return 1;
6523 case clang::Type::BlockPointer:
6524 return 0; // If block pointers don't have debug info, then no children for
6525 // them
6526 case clang::Type::LValueReference:
6527 return 1;
6528 case clang::Type::RValueReference:
6529 return 1;
6530 case clang::Type::MemberPointer:
6531 return 0;
6532 case clang::Type::ConstantArray:
6533 return 0;
6534 case clang::Type::IncompleteArray:
6535 return 0;
6536 case clang::Type::VariableArray:
6537 return 0;
6538 case clang::Type::DependentSizedArray:
6539 return 0;
6540 case clang::Type::DependentSizedExtVector:
6541 return 0;
6542 case clang::Type::Vector:
6543 return 0;
6544 case clang::Type::ExtVector:
6545 return 0;
6546 case clang::Type::FunctionProto:
6547 return 0; // When we function pointers, they have no children...
6548 case clang::Type::FunctionNoProto:
6549 return 0; // When we function pointers, they have no children...
6550 case clang::Type::UnresolvedUsing:
6551 return 0;
6552 case clang::Type::Paren:
6553 return GetNumPointeeChildren(
6554 llvm::cast<clang::ParenType>(qual_type)->desugar());
6555 case clang::Type::Typedef:
6556 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6557 ->getDecl()
6558 ->getUnderlyingType());
6559 case clang::Type::Auto:
6560 return GetNumPointeeChildren(
6561 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6562 case clang::Type::Elaborated:
6563 return GetNumPointeeChildren(
6564 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6565 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006566 return GetNumPointeeChildren(llvm::cast<clang::TypeOfExprType>(qual_type)
6567 ->getUnderlyingExpr()
6568 ->getType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006569 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006570 return GetNumPointeeChildren(
6571 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006572 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006573 return GetNumPointeeChildren(
6574 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006575 case clang::Type::Record:
6576 return 0;
6577 case clang::Type::Enum:
6578 return 1;
6579 case clang::Type::TemplateTypeParm:
6580 return 1;
6581 case clang::Type::SubstTemplateTypeParm:
6582 return 1;
6583 case clang::Type::TemplateSpecialization:
6584 return 1;
6585 case clang::Type::InjectedClassName:
6586 return 0;
6587 case clang::Type::DependentName:
6588 return 1;
6589 case clang::Type::DependentTemplateSpecialization:
6590 return 1;
6591 case clang::Type::ObjCObject:
6592 return 0;
6593 case clang::Type::ObjCInterface:
6594 return 0;
6595 case clang::Type::ObjCObjectPointer:
6596 return 1;
6597 default:
6598 break;
6599 }
6600 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006601}
6602
Kate Stoneb9c1b512016-09-06 20:57:50 +00006603CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6604 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6605 bool transparent_pointers, bool omit_empty_base_classes,
6606 bool ignore_array_bounds, std::string &child_name,
6607 uint32_t &child_byte_size, int32_t &child_byte_offset,
6608 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6609 bool &child_is_base_class, bool &child_is_deref_of_parent,
6610 ValueObject *valobj, uint64_t &language_flags) {
6611 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006612 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006613
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006614 auto get_exe_scope = [&exe_ctx]() {
6615 return exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr;
6616 };
6617
Kate Stoneb9c1b512016-09-06 20:57:50 +00006618 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6619 const clang::Type::TypeClass parent_type_class =
6620 parent_qual_type->getTypeClass();
6621 child_bitfield_bit_size = 0;
6622 child_bitfield_bit_offset = 0;
6623 child_is_base_class = false;
6624 language_flags = 0;
6625
Adrian Prantleca07c52018-11-05 20:49:07 +00006626 const bool idx_is_valid =
6627 idx < GetNumChildren(type, omit_empty_base_classes, exe_ctx);
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00006628 int32_t bit_offset;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006629 switch (parent_type_class) {
6630 case clang::Type::Builtin:
6631 if (idx_is_valid) {
6632 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6633 case clang::BuiltinType::ObjCId:
6634 case clang::BuiltinType::ObjCClass:
6635 child_name = "isa";
6636 child_byte_size =
6637 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6638 CHAR_BIT;
6639 return CompilerType(getASTContext(),
6640 getASTContext()->ObjCBuiltinClassTy);
6641
6642 default:
6643 break;
6644 }
6645 }
6646 break;
6647
6648 case clang::Type::Record:
6649 if (idx_is_valid && GetCompleteType(type)) {
6650 const clang::RecordType *record_type =
6651 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6652 const clang::RecordDecl *record_decl = record_type->getDecl();
6653 assert(record_decl);
6654 const clang::ASTRecordLayout &record_layout =
6655 getASTContext()->getASTRecordLayout(record_decl);
6656 uint32_t child_idx = 0;
6657
6658 const clang::CXXRecordDecl *cxx_record_decl =
6659 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6660 if (cxx_record_decl) {
6661 // We might have base classes to print out first
6662 clang::CXXRecordDecl::base_class_const_iterator base_class,
6663 base_class_end;
6664 for (base_class = cxx_record_decl->bases_begin(),
6665 base_class_end = cxx_record_decl->bases_end();
6666 base_class != base_class_end; ++base_class) {
6667 const clang::CXXRecordDecl *base_class_decl = nullptr;
6668
6669 // Skip empty base classes
6670 if (omit_empty_base_classes) {
6671 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6672 base_class->getType()->getAs<clang::RecordType>()->getDecl());
Jonas Devliegherea6682a42018-12-15 00:15:33 +00006673 if (!ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00006674 continue;
6675 }
6676
6677 if (idx == child_idx) {
6678 if (base_class_decl == nullptr)
6679 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6680 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6681
6682 if (base_class->isVirtual()) {
6683 bool handled = false;
6684 if (valobj) {
Aleksandr Urakov1dc51db2018-11-12 16:23:50 +00006685 clang::VTableContextBase *vtable_ctx =
6686 getASTContext()->getVTableContext();
6687 if (vtable_ctx)
6688 handled = GetVBaseBitOffset(*vtable_ctx, *valobj,
6689 record_layout, cxx_record_decl,
6690 base_class_decl, bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006691 }
6692 if (!handled)
6693 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6694 .getQuantity() *
6695 8;
6696 } else
6697 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6698 .getQuantity() *
6699 8;
6700
6701 // Base classes should be a multiple of 8 bits in size
6702 child_byte_offset = bit_offset / 8;
6703 CompilerType base_class_clang_type(getASTContext(),
6704 base_class->getType());
6705 child_name = base_class_clang_type.GetTypeName().AsCString("");
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006706 Optional<uint64_t> size =
6707 base_class_clang_type.GetBitSize(get_exe_scope());
Adrian Prantld963a7c2019-01-15 18:07:52 +00006708 if (!size)
6709 return {};
6710 uint64_t base_class_clang_type_bit_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006711
6712 // Base classes bit sizes should be a multiple of 8 bits in size
6713 assert(base_class_clang_type_bit_size % 8 == 0);
6714 child_byte_size = base_class_clang_type_bit_size / 8;
6715 child_is_base_class = true;
6716 return base_class_clang_type;
6717 }
6718 // We don't increment the child index in the for loop since we might
6719 // be skipping empty base classes
6720 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006721 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006722 }
6723 // Make sure index is in range...
6724 uint32_t field_idx = 0;
6725 clang::RecordDecl::field_iterator field, field_end;
6726 for (field = record_decl->field_begin(),
6727 field_end = record_decl->field_end();
6728 field != field_end; ++field, ++field_idx, ++child_idx) {
6729 if (idx == child_idx) {
6730 // Print the member type if requested
6731 // Print the member name and equal sign
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006732 child_name.assign(field->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006733
6734 // Figure out the type byte size (field_type_info.first) and
6735 // alignment (field_type_info.second) from the AST context.
6736 CompilerType field_clang_type(getASTContext(), field->getType());
6737 assert(field_idx < record_layout.getFieldCount());
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006738 Optional<uint64_t> size =
6739 field_clang_type.GetByteSize(get_exe_scope());
Adrian Prantld963a7c2019-01-15 18:07:52 +00006740 if (!size)
6741 return {};
6742 child_byte_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006743 const uint32_t child_bit_size = child_byte_size * 8;
6744
6745 // Figure out the field offset within the current struct/union/class
6746 // type
6747 bit_offset = record_layout.getFieldOffset(field_idx);
6748 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6749 child_bitfield_bit_size)) {
6750 child_bitfield_bit_offset = bit_offset % child_bit_size;
6751 const uint32_t child_bit_offset =
6752 bit_offset - child_bitfield_bit_offset;
6753 child_byte_offset = child_bit_offset / 8;
6754 } else {
6755 child_byte_offset = bit_offset / 8;
6756 }
6757
6758 return field_clang_type;
6759 }
6760 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006761 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006762 break;
6763
6764 case clang::Type::ObjCObject:
6765 case clang::Type::ObjCInterface:
6766 if (idx_is_valid && GetCompleteType(type)) {
6767 const clang::ObjCObjectType *objc_class_type =
6768 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6769 assert(objc_class_type);
6770 if (objc_class_type) {
6771 uint32_t child_idx = 0;
6772 clang::ObjCInterfaceDecl *class_interface_decl =
6773 objc_class_type->getInterface();
6774
6775 if (class_interface_decl) {
6776
6777 const clang::ASTRecordLayout &interface_layout =
6778 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6779 clang::ObjCInterfaceDecl *superclass_interface_decl =
6780 class_interface_decl->getSuperClass();
6781 if (superclass_interface_decl) {
6782 if (omit_empty_base_classes) {
6783 CompilerType base_class_clang_type(
6784 getASTContext(), getASTContext()->getObjCInterfaceType(
6785 superclass_interface_decl));
Adrian Prantleca07c52018-11-05 20:49:07 +00006786 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes,
6787 exe_ctx) > 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006788 if (idx == 0) {
6789 clang::QualType ivar_qual_type(
6790 getASTContext()->getObjCInterfaceType(
6791 superclass_interface_decl));
6792
6793 child_name.assign(
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006794 superclass_interface_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006795
6796 clang::TypeInfo ivar_type_info =
6797 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6798
6799 child_byte_size = ivar_type_info.Width / 8;
6800 child_byte_offset = 0;
6801 child_is_base_class = true;
6802
6803 return CompilerType(getASTContext(), ivar_qual_type);
6804 }
6805
6806 ++child_idx;
6807 }
6808 } else
6809 ++child_idx;
6810 }
6811
6812 const uint32_t superclass_idx = child_idx;
6813
6814 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6815 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6816 ivar_end = class_interface_decl->ivar_end();
6817
6818 for (ivar_pos = class_interface_decl->ivar_begin();
6819 ivar_pos != ivar_end; ++ivar_pos) {
6820 if (child_idx == idx) {
6821 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6822
6823 clang::QualType ivar_qual_type(ivar_decl->getType());
6824
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006825 child_name.assign(ivar_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006826
6827 clang::TypeInfo ivar_type_info =
6828 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6829
6830 child_byte_size = ivar_type_info.Width / 8;
6831
6832 // Figure out the field offset within the current
Adrian Prantl05097242018-04-30 16:49:04 +00006833 // struct/union/class type For ObjC objects, we can't trust the
6834 // bit offset we get from the Clang AST, since that doesn't
6835 // account for the space taken up by unbacked properties, or
6836 // from the changing size of base classes that are newer than
6837 // this class. So if we have a process around that we can ask
6838 // about this object, do so.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006839 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6840 Process *process = nullptr;
6841 if (exe_ctx)
6842 process = exe_ctx->GetProcessPtr();
6843 if (process) {
6844 ObjCLanguageRuntime *objc_runtime =
6845 process->GetObjCLanguageRuntime();
6846 if (objc_runtime != nullptr) {
6847 CompilerType parent_ast_type(getASTContext(),
6848 parent_qual_type);
6849 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6850 parent_ast_type, ivar_decl->getNameAsString().c_str());
6851 }
6852 }
6853
Aleksandr Urakovff701722018-08-20 05:59:27 +00006854 // Setting this to INT32_MAX to make sure we don't compute it
Kate Stoneb9c1b512016-09-06 20:57:50 +00006855 // twice...
Aleksandr Urakov53459482018-08-17 07:28:24 +00006856 bit_offset = INT32_MAX;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006857
6858 if (child_byte_offset ==
6859 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6860 bit_offset = interface_layout.getFieldOffset(child_idx -
6861 superclass_idx);
6862 child_byte_offset = bit_offset / 8;
6863 }
6864
6865 // Note, the ObjC Ivar Byte offset is just that, it doesn't
Adrian Prantl05097242018-04-30 16:49:04 +00006866 // account for the bit offset of a bitfield within its
6867 // containing object. So regardless of where we get the byte
Kate Stoneb9c1b512016-09-06 20:57:50 +00006868 // offset from, we still need to get the bit offset for
6869 // bitfields from the layout.
6870
6871 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6872 child_bitfield_bit_size)) {
Aleksandr Urakov53459482018-08-17 07:28:24 +00006873 if (bit_offset == INT32_MAX)
Kate Stoneb9c1b512016-09-06 20:57:50 +00006874 bit_offset = interface_layout.getFieldOffset(
6875 child_idx - superclass_idx);
6876
6877 child_bitfield_bit_offset = bit_offset % 8;
6878 }
6879 return CompilerType(getASTContext(), ivar_qual_type);
6880 }
6881 ++child_idx;
6882 }
6883 }
6884 }
6885 }
6886 }
6887 break;
6888
6889 case clang::Type::ObjCObjectPointer:
6890 if (idx_is_valid) {
6891 CompilerType pointee_clang_type(GetPointeeType(type));
6892
6893 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6894 child_is_deref_of_parent = false;
6895 bool tmp_child_is_deref_of_parent = false;
6896 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6897 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6898 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6899 child_bitfield_bit_size, child_bitfield_bit_offset,
6900 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6901 language_flags);
6902 } else {
6903 child_is_deref_of_parent = true;
6904 const char *parent_name =
Konrad Kleine248a1302019-05-23 11:14:47 +00006905 valobj ? valobj->GetName().GetCString() : nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006906 if (parent_name) {
6907 child_name.assign(1, '*');
6908 child_name += parent_name;
6909 }
6910
6911 // We have a pointer to an simple type
6912 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006913 if (Optional<uint64_t> size =
6914 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006915 child_byte_size = *size;
6916 child_byte_offset = 0;
6917 return pointee_clang_type;
6918 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006919 }
6920 }
6921 }
6922 break;
6923
6924 case clang::Type::Vector:
6925 case clang::Type::ExtVector:
6926 if (idx_is_valid) {
6927 const clang::VectorType *array =
6928 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6929 if (array) {
6930 CompilerType element_type(getASTContext(), array->getElementType());
6931 if (element_type.GetCompleteType()) {
6932 char element_name[64];
6933 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6934 static_cast<uint64_t>(idx));
6935 child_name.assign(element_name);
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006936 if (Optional<uint64_t> size =
6937 element_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006938 child_byte_size = *size;
6939 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6940 return element_type;
6941 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006942 }
6943 }
6944 }
6945 break;
6946
6947 case clang::Type::ConstantArray:
6948 case clang::Type::IncompleteArray:
6949 if (ignore_array_bounds || idx_is_valid) {
6950 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6951 if (array) {
6952 CompilerType element_type(getASTContext(), array->getElementType());
6953 if (element_type.GetCompleteType()) {
Zachary Turner827d5d72016-12-16 04:27:00 +00006954 child_name = llvm::formatv("[{0}]", idx);
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006955 if (Optional<uint64_t> size =
6956 element_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006957 child_byte_size = *size;
6958 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6959 return element_type;
6960 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006961 }
6962 }
6963 }
6964 break;
6965
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006966 case clang::Type::Pointer: {
6967 CompilerType pointee_clang_type(GetPointeeType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00006968
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006969 // Don't dereference "void *" pointers
6970 if (pointee_clang_type.IsVoidType())
6971 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006972
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006973 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6974 child_is_deref_of_parent = false;
6975 bool tmp_child_is_deref_of_parent = false;
6976 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6977 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6978 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6979 child_bitfield_bit_size, child_bitfield_bit_offset,
6980 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6981 language_flags);
6982 } else {
6983 child_is_deref_of_parent = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006984
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006985 const char *parent_name =
Konrad Kleine248a1302019-05-23 11:14:47 +00006986 valobj ? valobj->GetName().GetCString() : nullptr;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006987 if (parent_name) {
6988 child_name.assign(1, '*');
6989 child_name += parent_name;
6990 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006991
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006992 // We have a pointer to an simple type
6993 if (idx == 0) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006994 if (Optional<uint64_t> size =
6995 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006996 child_byte_size = *size;
6997 child_byte_offset = 0;
6998 return pointee_clang_type;
6999 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007000 }
7001 }
7002 break;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00007003 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007004
7005 case clang::Type::LValueReference:
7006 case clang::Type::RValueReference:
7007 if (idx_is_valid) {
7008 const clang::ReferenceType *reference_type =
7009 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
7010 CompilerType pointee_clang_type(getASTContext(),
7011 reference_type->getPointeeType());
7012 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
7013 child_is_deref_of_parent = false;
7014 bool tmp_child_is_deref_of_parent = false;
7015 return pointee_clang_type.GetChildCompilerTypeAtIndex(
7016 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7017 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7018 child_bitfield_bit_size, child_bitfield_bit_offset,
7019 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
7020 language_flags);
7021 } else {
7022 const char *parent_name =
Konrad Kleine248a1302019-05-23 11:14:47 +00007023 valobj ? valobj->GetName().GetCString() : nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007024 if (parent_name) {
7025 child_name.assign(1, '&');
7026 child_name += parent_name;
7027 }
7028
7029 // We have a pointer to an simple type
7030 if (idx == 0) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00007031 if (Optional<uint64_t> size =
7032 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00007033 child_byte_size = *size;
7034 child_byte_offset = 0;
7035 return pointee_clang_type;
7036 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007037 }
7038 }
7039 }
7040 break;
7041
7042 case clang::Type::Typedef: {
7043 CompilerType typedefed_clang_type(
7044 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)
7045 ->getDecl()
7046 ->getUnderlyingType());
7047 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
7048 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7049 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7050 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7051 child_is_deref_of_parent, valobj, language_flags);
7052 } break;
7053
7054 case clang::Type::Auto: {
7055 CompilerType elaborated_clang_type(
7056 getASTContext(),
7057 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
7058 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
7059 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7060 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7061 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7062 child_is_deref_of_parent, valobj, language_flags);
7063 }
7064
7065 case clang::Type::Elaborated: {
7066 CompilerType elaborated_clang_type(
7067 getASTContext(),
7068 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
7069 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
7070 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7071 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7072 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7073 child_is_deref_of_parent, valobj, language_flags);
7074 }
7075
7076 case clang::Type::Paren: {
7077 CompilerType paren_clang_type(
7078 getASTContext(),
7079 llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
7080 return paren_clang_type.GetChildCompilerTypeAtIndex(
7081 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7082 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7083 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7084 child_is_deref_of_parent, valobj, language_flags);
7085 }
7086
7087 default:
7088 break;
7089 }
7090 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007091}
7092
Kate Stoneb9c1b512016-09-06 20:57:50 +00007093static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
7094 const clang::CXXBaseSpecifier *base_spec,
7095 bool omit_empty_base_classes) {
7096 uint32_t child_idx = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007097
Kate Stoneb9c1b512016-09-06 20:57:50 +00007098 const clang::CXXRecordDecl *cxx_record_decl =
7099 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7100
7101 // const char *super_name = record_decl->getNameAsCString();
7102 // const char *base_name =
7103 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
7104 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
7105 //
7106 if (cxx_record_decl) {
7107 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
7108 for (base_class = cxx_record_decl->bases_begin(),
7109 base_class_end = cxx_record_decl->bases_end();
7110 base_class != base_class_end; ++base_class) {
7111 if (omit_empty_base_classes) {
7112 if (BaseSpecifierIsEmpty(base_class))
7113 continue;
7114 }
7115
7116 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
7117 // super_name, base_name,
7118 // child_idx,
7119 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
7120 //
7121 //
7122 if (base_class == base_spec)
7123 return child_idx;
7124 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00007125 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007126 }
7127
7128 return UINT32_MAX;
7129}
7130
7131static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
7132 clang::NamedDecl *canonical_decl,
7133 bool omit_empty_base_classes) {
7134 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
7135 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
7136 omit_empty_base_classes);
7137
7138 clang::RecordDecl::field_iterator field, field_end;
7139 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
7140 field != field_end; ++field, ++child_idx) {
7141 if (field->getCanonicalDecl() == canonical_decl)
7142 return child_idx;
7143 }
7144
7145 return UINT32_MAX;
Greg Claytond8d4a572015-08-11 21:38:15 +00007146}
7147
7148// Look for a child member (doesn't include base classes, but it does include
Adrian Prantl05097242018-04-30 16:49:04 +00007149// their members) in the type hierarchy. Returns an index path into
7150// "clang_type" on how to reach the appropriate member.
Greg Claytond8d4a572015-08-11 21:38:15 +00007151//
7152// class A
7153// {
7154// public:
7155// int m_a;
7156// int m_b;
7157// };
7158//
7159// class B
7160// {
7161// };
7162//
7163// class C :
7164// public B,
7165// public A
7166// {
7167// };
7168//
7169// If we have a clang type that describes "class C", and we wanted to looked
7170// "m_b" in it:
7171//
Kate Stoneb9c1b512016-09-06 20:57:50 +00007172// With omit_empty_base_classes == false we would get an integer array back
Adrian Prantl05097242018-04-30 16:49:04 +00007173// with: { 1, 1 } The first index 1 is the child index for "class A" within
7174// class C The second index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007175//
Adrian Prantl05097242018-04-30 16:49:04 +00007176// With omit_empty_base_classes == true we would get an integer array back
7177// with: { 0, 1 } The first index 0 is the child index for "class A" within
7178// class C (since class B doesn't have any members it doesn't count) The second
7179// index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007180
Kate Stoneb9c1b512016-09-06 20:57:50 +00007181size_t ClangASTContext::GetIndexOfChildMemberWithName(
7182 lldb::opaque_compiler_type_t type, const char *name,
7183 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
7184 if (type && name && name[0]) {
7185 clang::QualType qual_type(GetCanonicalQualType(type));
7186 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7187 switch (type_class) {
7188 case clang::Type::Record:
7189 if (GetCompleteType(type)) {
7190 const clang::RecordType *record_type =
7191 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7192 const clang::RecordDecl *record_decl = record_type->getDecl();
Enrico Granata36f51e42015-12-18 22:41:25 +00007193
Kate Stoneb9c1b512016-09-06 20:57:50 +00007194 assert(record_decl);
7195 uint32_t child_idx = 0;
7196
7197 const clang::CXXRecordDecl *cxx_record_decl =
7198 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7199
7200 // Try and find a field that matches NAME
7201 clang::RecordDecl::field_iterator field, field_end;
7202 llvm::StringRef name_sref(name);
7203 for (field = record_decl->field_begin(),
7204 field_end = record_decl->field_end();
7205 field != field_end; ++field, ++child_idx) {
7206 llvm::StringRef field_name = field->getName();
7207 if (field_name.empty()) {
7208 CompilerType field_type(getASTContext(), field->getType());
7209 child_indexes.push_back(child_idx);
7210 if (field_type.GetIndexOfChildMemberWithName(
7211 name, omit_empty_base_classes, child_indexes))
7212 return child_indexes.size();
7213 child_indexes.pop_back();
7214
7215 } else if (field_name.equals(name_sref)) {
7216 // We have to add on the number of base classes to this index!
7217 child_indexes.push_back(
7218 child_idx + ClangASTContext::GetNumBaseClasses(
7219 cxx_record_decl, omit_empty_base_classes));
7220 return child_indexes.size();
7221 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007222 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007223
Kate Stoneb9c1b512016-09-06 20:57:50 +00007224 if (cxx_record_decl) {
7225 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7226
7227 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7228
7229 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7230 // Didn't find things easily, lets let clang do its thang...
7231 clang::IdentifierInfo &ident_ref =
7232 getASTContext()->Idents.get(name_sref);
7233 clang::DeclarationName decl_name(&ident_ref);
7234
7235 clang::CXXBasePaths paths;
7236 if (cxx_record_decl->lookupInBases(
7237 [decl_name](const clang::CXXBaseSpecifier *specifier,
7238 clang::CXXBasePath &path) {
7239 return clang::CXXRecordDecl::FindOrdinaryMember(
7240 specifier, path, decl_name);
7241 },
7242 paths)) {
7243 clang::CXXBasePaths::const_paths_iterator path,
7244 path_end = paths.end();
7245 for (path = paths.begin(); path != path_end; ++path) {
7246 const size_t num_path_elements = path->size();
7247 for (size_t e = 0; e < num_path_elements; ++e) {
7248 clang::CXXBasePathElement elem = (*path)[e];
7249
7250 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7251 omit_empty_base_classes);
7252 if (child_idx == UINT32_MAX) {
7253 child_indexes.clear();
7254 return 0;
7255 } else {
7256 child_indexes.push_back(child_idx);
7257 parent_record_decl = llvm::cast<clang::RecordDecl>(
7258 elem.Base->getType()
7259 ->getAs<clang::RecordType>()
7260 ->getDecl());
7261 }
7262 }
7263 for (clang::NamedDecl *path_decl : path->Decls) {
7264 child_idx = GetIndexForRecordChild(
7265 parent_record_decl, path_decl, omit_empty_base_classes);
7266 if (child_idx == UINT32_MAX) {
7267 child_indexes.clear();
7268 return 0;
7269 } else {
7270 child_indexes.push_back(child_idx);
7271 }
7272 }
7273 }
7274 return child_indexes.size();
7275 }
7276 }
7277 }
7278 break;
7279
7280 case clang::Type::ObjCObject:
7281 case clang::Type::ObjCInterface:
7282 if (GetCompleteType(type)) {
7283 llvm::StringRef name_sref(name);
7284 const clang::ObjCObjectType *objc_class_type =
7285 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7286 assert(objc_class_type);
7287 if (objc_class_type) {
7288 uint32_t child_idx = 0;
7289 clang::ObjCInterfaceDecl *class_interface_decl =
7290 objc_class_type->getInterface();
7291
7292 if (class_interface_decl) {
7293 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7294 ivar_end = class_interface_decl->ivar_end();
7295 clang::ObjCInterfaceDecl *superclass_interface_decl =
7296 class_interface_decl->getSuperClass();
7297
7298 for (ivar_pos = class_interface_decl->ivar_begin();
7299 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7300 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7301
7302 if (ivar_decl->getName().equals(name_sref)) {
7303 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7304 (omit_empty_base_classes &&
7305 ObjCDeclHasIVars(superclass_interface_decl, true)))
7306 ++child_idx;
7307
7308 child_indexes.push_back(child_idx);
7309 return child_indexes.size();
7310 }
7311 }
7312
7313 if (superclass_interface_decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00007314 // The super class index is always zero for ObjC classes, so we
7315 // push it onto the child indexes in case we find an ivar in our
7316 // superclass...
Kate Stoneb9c1b512016-09-06 20:57:50 +00007317 child_indexes.push_back(0);
7318
7319 CompilerType superclass_clang_type(
7320 getASTContext(), getASTContext()->getObjCInterfaceType(
7321 superclass_interface_decl));
7322 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7323 name, omit_empty_base_classes, child_indexes)) {
Adrian Prantl05097242018-04-30 16:49:04 +00007324 // We did find an ivar in a superclass so just return the
7325 // results!
Kate Stoneb9c1b512016-09-06 20:57:50 +00007326 return child_indexes.size();
7327 }
7328
Adrian Prantl05097242018-04-30 16:49:04 +00007329 // We didn't find an ivar matching "name" in our superclass, pop
7330 // the superclass zero index that we pushed on above.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007331 child_indexes.pop_back();
7332 }
7333 }
7334 }
7335 }
7336 break;
7337
7338 case clang::Type::ObjCObjectPointer: {
7339 CompilerType objc_object_clang_type(
7340 getASTContext(),
7341 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7342 ->getPointeeType());
7343 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7344 name, omit_empty_base_classes, child_indexes);
7345 } break;
7346
7347 case clang::Type::ConstantArray: {
7348 // const clang::ConstantArrayType *array =
7349 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7350 // const uint64_t element_count =
7351 // array->getSize().getLimitedValue();
7352 //
7353 // if (idx < element_count)
7354 // {
7355 // std::pair<uint64_t, unsigned> field_type_info =
7356 // ast->getTypeInfo(array->getElementType());
7357 //
7358 // char element_name[32];
7359 // ::snprintf (element_name, sizeof (element_name),
7360 // "%s[%u]", parent_name ? parent_name : "", idx);
7361 //
7362 // child_name.assign(element_name);
7363 // assert(field_type_info.first % 8 == 0);
7364 // child_byte_size = field_type_info.first / 8;
7365 // child_byte_offset = idx * child_byte_size;
7366 // return array->getElementType().getAsOpaquePtr();
7367 // }
7368 } break;
7369
7370 // case clang::Type::MemberPointerType:
7371 // {
7372 // MemberPointerType *mem_ptr_type =
7373 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7374 // clang::QualType pointee_type =
7375 // mem_ptr_type->getPointeeType();
7376 //
7377 // if (ClangASTContext::IsAggregateType
7378 // (pointee_type.getAsOpaquePtr()))
7379 // {
7380 // return GetIndexOfChildWithName (ast,
7381 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7382 // name);
7383 // }
7384 // }
7385 // break;
7386 //
7387 case clang::Type::LValueReference:
7388 case clang::Type::RValueReference: {
7389 const clang::ReferenceType *reference_type =
7390 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7391 clang::QualType pointee_type(reference_type->getPointeeType());
7392 CompilerType pointee_clang_type(getASTContext(), pointee_type);
7393
7394 if (pointee_clang_type.IsAggregateType()) {
7395 return pointee_clang_type.GetIndexOfChildMemberWithName(
7396 name, omit_empty_base_classes, child_indexes);
7397 }
7398 } break;
7399
7400 case clang::Type::Pointer: {
7401 CompilerType pointee_clang_type(GetPointeeType(type));
7402
7403 if (pointee_clang_type.IsAggregateType()) {
7404 return pointee_clang_type.GetIndexOfChildMemberWithName(
7405 name, omit_empty_base_classes, child_indexes);
7406 }
7407 } break;
7408
7409 case clang::Type::Typedef:
7410 return CompilerType(getASTContext(),
7411 llvm::cast<clang::TypedefType>(qual_type)
7412 ->getDecl()
7413 ->getUnderlyingType())
7414 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7415 child_indexes);
7416
7417 case clang::Type::Auto:
7418 return CompilerType(
7419 getASTContext(),
7420 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7421 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7422 child_indexes);
7423
7424 case clang::Type::Elaborated:
7425 return CompilerType(
7426 getASTContext(),
7427 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7428 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7429 child_indexes);
7430
7431 case clang::Type::Paren:
7432 return CompilerType(getASTContext(),
7433 llvm::cast<clang::ParenType>(qual_type)->desugar())
7434 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7435 child_indexes);
7436
7437 default:
7438 break;
7439 }
7440 }
7441 return 0;
7442}
Greg Claytond8d4a572015-08-11 21:38:15 +00007443
7444// Get the index of the child of "clang_type" whose name matches. This function
7445// doesn't descend into the children, but only looks one level deep and name
7446// matches can include base class names.
7447
7448uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007449ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7450 const char *name,
7451 bool omit_empty_base_classes) {
7452 if (type && name && name[0]) {
7453 clang::QualType qual_type(GetCanonicalQualType(type));
Enrico Granata36f51e42015-12-18 22:41:25 +00007454
Kate Stoneb9c1b512016-09-06 20:57:50 +00007455 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7456
7457 switch (type_class) {
7458 case clang::Type::Record:
7459 if (GetCompleteType(type)) {
7460 const clang::RecordType *record_type =
7461 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7462 const clang::RecordDecl *record_decl = record_type->getDecl();
7463
7464 assert(record_decl);
7465 uint32_t child_idx = 0;
7466
7467 const clang::CXXRecordDecl *cxx_record_decl =
7468 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7469
7470 if (cxx_record_decl) {
7471 clang::CXXRecordDecl::base_class_const_iterator base_class,
7472 base_class_end;
7473 for (base_class = cxx_record_decl->bases_begin(),
7474 base_class_end = cxx_record_decl->bases_end();
7475 base_class != base_class_end; ++base_class) {
7476 // Skip empty base classes
7477 clang::CXXRecordDecl *base_class_decl =
7478 llvm::cast<clang::CXXRecordDecl>(
7479 base_class->getType()
7480 ->getAs<clang::RecordType>()
7481 ->getDecl());
7482 if (omit_empty_base_classes &&
Jonas Devliegherea6682a42018-12-15 00:15:33 +00007483 !ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00007484 continue;
7485
7486 CompilerType base_class_clang_type(getASTContext(),
7487 base_class->getType());
7488 std::string base_class_type_name(
7489 base_class_clang_type.GetTypeName().AsCString(""));
Jonas Devlieghere8d20cfd2018-12-21 22:46:10 +00007490 if (base_class_type_name == name)
Kate Stoneb9c1b512016-09-06 20:57:50 +00007491 return child_idx;
7492 ++child_idx;
7493 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007494 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007495
Kate Stoneb9c1b512016-09-06 20:57:50 +00007496 // Try and find a field that matches NAME
7497 clang::RecordDecl::field_iterator field, field_end;
7498 llvm::StringRef name_sref(name);
7499 for (field = record_decl->field_begin(),
7500 field_end = record_decl->field_end();
7501 field != field_end; ++field, ++child_idx) {
7502 if (field->getName().equals(name_sref))
7503 return child_idx;
7504 }
7505 }
7506 break;
7507
7508 case clang::Type::ObjCObject:
7509 case clang::Type::ObjCInterface:
7510 if (GetCompleteType(type)) {
7511 llvm::StringRef name_sref(name);
7512 const clang::ObjCObjectType *objc_class_type =
7513 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7514 assert(objc_class_type);
7515 if (objc_class_type) {
7516 uint32_t child_idx = 0;
7517 clang::ObjCInterfaceDecl *class_interface_decl =
7518 objc_class_type->getInterface();
7519
7520 if (class_interface_decl) {
7521 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7522 ivar_end = class_interface_decl->ivar_end();
7523 clang::ObjCInterfaceDecl *superclass_interface_decl =
7524 class_interface_decl->getSuperClass();
7525
7526 for (ivar_pos = class_interface_decl->ivar_begin();
7527 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7528 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7529
7530 if (ivar_decl->getName().equals(name_sref)) {
7531 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7532 (omit_empty_base_classes &&
7533 ObjCDeclHasIVars(superclass_interface_decl, true)))
7534 ++child_idx;
7535
7536 return child_idx;
7537 }
7538 }
7539
7540 if (superclass_interface_decl) {
7541 if (superclass_interface_decl->getName().equals(name_sref))
7542 return 0;
7543 }
7544 }
7545 }
7546 }
7547 break;
7548
7549 case clang::Type::ObjCObjectPointer: {
7550 CompilerType pointee_clang_type(
7551 getASTContext(),
7552 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7553 ->getPointeeType());
7554 return pointee_clang_type.GetIndexOfChildWithName(
7555 name, omit_empty_base_classes);
7556 } break;
7557
7558 case clang::Type::ConstantArray: {
7559 // const clang::ConstantArrayType *array =
7560 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7561 // const uint64_t element_count =
7562 // array->getSize().getLimitedValue();
7563 //
7564 // if (idx < element_count)
7565 // {
7566 // std::pair<uint64_t, unsigned> field_type_info =
7567 // ast->getTypeInfo(array->getElementType());
7568 //
7569 // char element_name[32];
7570 // ::snprintf (element_name, sizeof (element_name),
7571 // "%s[%u]", parent_name ? parent_name : "", idx);
7572 //
7573 // child_name.assign(element_name);
7574 // assert(field_type_info.first % 8 == 0);
7575 // child_byte_size = field_type_info.first / 8;
7576 // child_byte_offset = idx * child_byte_size;
7577 // return array->getElementType().getAsOpaquePtr();
7578 // }
7579 } break;
7580
7581 // case clang::Type::MemberPointerType:
7582 // {
7583 // MemberPointerType *mem_ptr_type =
7584 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7585 // clang::QualType pointee_type =
7586 // mem_ptr_type->getPointeeType();
7587 //
7588 // if (ClangASTContext::IsAggregateType
7589 // (pointee_type.getAsOpaquePtr()))
7590 // {
7591 // return GetIndexOfChildWithName (ast,
7592 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7593 // name);
7594 // }
7595 // }
7596 // break;
7597 //
7598 case clang::Type::LValueReference:
7599 case clang::Type::RValueReference: {
7600 const clang::ReferenceType *reference_type =
7601 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7602 CompilerType pointee_type(getASTContext(),
7603 reference_type->getPointeeType());
7604
7605 if (pointee_type.IsAggregateType()) {
7606 return pointee_type.GetIndexOfChildWithName(name,
7607 omit_empty_base_classes);
7608 }
7609 } break;
7610
7611 case clang::Type::Pointer: {
7612 const clang::PointerType *pointer_type =
7613 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7614 CompilerType pointee_type(getASTContext(),
7615 pointer_type->getPointeeType());
7616
7617 if (pointee_type.IsAggregateType()) {
7618 return pointee_type.GetIndexOfChildWithName(name,
7619 omit_empty_base_classes);
7620 } else {
7621 // if (parent_name)
7622 // {
7623 // child_name.assign(1, '*');
7624 // child_name += parent_name;
7625 // }
7626 //
7627 // // We have a pointer to an simple type
7628 // if (idx == 0)
7629 // {
7630 // std::pair<uint64_t, unsigned> clang_type_info
7631 // = ast->getTypeInfo(pointee_type);
7632 // assert(clang_type_info.first % 8 == 0);
7633 // child_byte_size = clang_type_info.first / 8;
7634 // child_byte_offset = 0;
7635 // return pointee_type.getAsOpaquePtr();
7636 // }
7637 }
7638 } break;
7639
7640 case clang::Type::Auto:
7641 return CompilerType(
7642 getASTContext(),
7643 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7644 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7645
7646 case clang::Type::Elaborated:
7647 return CompilerType(
7648 getASTContext(),
7649 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7650 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7651
7652 case clang::Type::Paren:
7653 return CompilerType(getASTContext(),
7654 llvm::cast<clang::ParenType>(qual_type)->desugar())
7655 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7656
7657 case clang::Type::Typedef:
7658 return CompilerType(getASTContext(),
7659 llvm::cast<clang::TypedefType>(qual_type)
7660 ->getDecl()
7661 ->getUnderlyingType())
7662 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7663
7664 default:
7665 break;
7666 }
7667 }
7668 return UINT32_MAX;
7669}
Greg Claytond8d4a572015-08-11 21:38:15 +00007670
7671size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007672ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7673 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007674 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007675
Kate Stoneb9c1b512016-09-06 20:57:50 +00007676 clang::QualType qual_type(GetCanonicalQualType(type));
7677 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7678 switch (type_class) {
7679 case clang::Type::Record:
7680 if (GetCompleteType(type)) {
7681 const clang::CXXRecordDecl *cxx_record_decl =
7682 qual_type->getAsCXXRecordDecl();
7683 if (cxx_record_decl) {
7684 const clang::ClassTemplateSpecializationDecl *template_decl =
7685 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7686 cxx_record_decl);
7687 if (template_decl)
7688 return template_decl->getTemplateArgs().size();
7689 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007690 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007691 break;
7692
7693 case clang::Type::Typedef:
7694 return (CompilerType(getASTContext(),
7695 llvm::cast<clang::TypedefType>(qual_type)
7696 ->getDecl()
7697 ->getUnderlyingType()))
7698 .GetNumTemplateArguments();
7699
7700 case clang::Type::Auto:
7701 return (CompilerType(
7702 getASTContext(),
7703 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7704 .GetNumTemplateArguments();
7705
7706 case clang::Type::Elaborated:
7707 return (CompilerType(
7708 getASTContext(),
7709 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7710 .GetNumTemplateArguments();
7711
7712 case clang::Type::Paren:
7713 return (CompilerType(getASTContext(),
7714 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7715 .GetNumTemplateArguments();
7716
7717 default:
7718 break;
7719 }
7720
7721 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007722}
7723
Pavel Labath769b21e2017-11-13 14:26:21 +00007724const clang::ClassTemplateSpecializationDecl *
7725ClangASTContext::GetAsTemplateSpecialization(
7726 lldb::opaque_compiler_type_t type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00007727 if (!type)
Pavel Labath769b21e2017-11-13 14:26:21 +00007728 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007729
7730 clang::QualType qual_type(GetCanonicalQualType(type));
7731 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7732 switch (type_class) {
Pavel Labath769b21e2017-11-13 14:26:21 +00007733 case clang::Type::Record: {
7734 if (! GetCompleteType(type))
7735 return nullptr;
7736 const clang::CXXRecordDecl *cxx_record_decl =
7737 qual_type->getAsCXXRecordDecl();
7738 if (!cxx_record_decl)
7739 return nullptr;
7740 return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7741 cxx_record_decl);
7742 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007743
7744 case clang::Type::Typedef:
Pavel Labath769b21e2017-11-13 14:26:21 +00007745 return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type)
7746 ->getDecl()
7747 ->getUnderlyingType()
7748 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007749
7750 case clang::Type::Auto:
Pavel Labath769b21e2017-11-13 14:26:21 +00007751 return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type)
7752 ->getDeducedType()
7753 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007754
7755 case clang::Type::Elaborated:
Pavel Labath769b21e2017-11-13 14:26:21 +00007756 return GetAsTemplateSpecialization(
7757 llvm::cast<clang::ElaboratedType>(qual_type)
7758 ->getNamedType()
7759 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007760
7761 case clang::Type::Paren:
Pavel Labath769b21e2017-11-13 14:26:21 +00007762 return GetAsTemplateSpecialization(
7763 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007764
7765 default:
Pavel Labath769b21e2017-11-13 14:26:21 +00007766 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007767 }
Pavel Labath769b21e2017-11-13 14:26:21 +00007768}
7769
7770lldb::TemplateArgumentKind
7771ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
7772 size_t arg_idx) {
7773 const clang::ClassTemplateSpecializationDecl *template_decl =
7774 GetAsTemplateSpecialization(type);
7775 if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size())
7776 return eTemplateArgumentKindNull;
7777
7778 switch (template_decl->getTemplateArgs()[arg_idx].getKind()) {
7779 case clang::TemplateArgument::Null:
7780 return eTemplateArgumentKindNull;
7781
7782 case clang::TemplateArgument::NullPtr:
7783 return eTemplateArgumentKindNullPtr;
7784
7785 case clang::TemplateArgument::Type:
7786 return eTemplateArgumentKindType;
7787
7788 case clang::TemplateArgument::Declaration:
7789 return eTemplateArgumentKindDeclaration;
7790
7791 case clang::TemplateArgument::Integral:
7792 return eTemplateArgumentKindIntegral;
7793
7794 case clang::TemplateArgument::Template:
7795 return eTemplateArgumentKindTemplate;
7796
7797 case clang::TemplateArgument::TemplateExpansion:
7798 return eTemplateArgumentKindTemplateExpansion;
7799
7800 case clang::TemplateArgument::Expression:
7801 return eTemplateArgumentKindExpression;
7802
7803 case clang::TemplateArgument::Pack:
7804 return eTemplateArgumentKindPack;
7805 }
7806 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
7807}
7808
7809CompilerType
7810ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
7811 size_t idx) {
7812 const clang::ClassTemplateSpecializationDecl *template_decl =
7813 GetAsTemplateSpecialization(type);
7814 if (!template_decl || idx >= template_decl->getTemplateArgs().size())
7815 return CompilerType();
7816
7817 const clang::TemplateArgument &template_arg =
7818 template_decl->getTemplateArgs()[idx];
7819 if (template_arg.getKind() != clang::TemplateArgument::Type)
7820 return CompilerType();
7821
7822 return CompilerType(getASTContext(), template_arg.getAsType());
7823}
7824
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00007825Optional<CompilerType::IntegralTemplateArgument>
Pavel Labath769b21e2017-11-13 14:26:21 +00007826ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
7827 size_t idx) {
7828 const clang::ClassTemplateSpecializationDecl *template_decl =
7829 GetAsTemplateSpecialization(type);
7830 if (! template_decl || idx >= template_decl->getTemplateArgs().size())
Pavel Labathf59056f2017-11-30 10:16:54 +00007831 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007832
7833 const clang::TemplateArgument &template_arg =
7834 template_decl->getTemplateArgs()[idx];
7835 if (template_arg.getKind() != clang::TemplateArgument::Integral)
Pavel Labathf59056f2017-11-30 10:16:54 +00007836 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007837
Pavel Labathf59056f2017-11-30 10:16:54 +00007838 return {{template_arg.getAsIntegral(),
7839 CompilerType(getASTContext(), template_arg.getIntegralType())}};
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007840}
7841
Kate Stoneb9c1b512016-09-06 20:57:50 +00007842CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7843 if (type)
7844 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7845 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007846}
7847
Kate Stoneb9c1b512016-09-06 20:57:50 +00007848clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7849 const clang::EnumType *enutype =
7850 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7851 if (enutype)
7852 return enutype->getDecl();
Konrad Kleine248a1302019-05-23 11:14:47 +00007853 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007854}
7855
7856clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7857 const clang::RecordType *record_type =
7858 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7859 if (record_type)
7860 return record_type->getDecl();
7861 return nullptr;
7862}
7863
7864clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
Zachary Turner1639c6b2018-12-17 16:15:13 +00007865 return ClangUtil::GetAsTagDecl(type);
Greg Claytone6b36cd2015-12-08 01:02:08 +00007866}
7867
Aleksandr Urakov709426b2018-09-10 08:08:43 +00007868clang::TypedefNameDecl *
7869ClangASTContext::GetAsTypedefDecl(const CompilerType &type) {
7870 const clang::TypedefType *typedef_type =
7871 llvm::dyn_cast<clang::TypedefType>(ClangUtil::GetQualType(type));
7872 if (typedef_type)
7873 return typedef_type->getDecl();
7874 return nullptr;
7875}
7876
Greg Claytond8d4a572015-08-11 21:38:15 +00007877clang::CXXRecordDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007878ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7879 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
Greg Claytond8d4a572015-08-11 21:38:15 +00007880}
7881
7882clang::ObjCInterfaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007883ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7884 const clang::ObjCObjectType *objc_class_type =
7885 llvm::dyn_cast<clang::ObjCObjectType>(
7886 ClangUtil::GetCanonicalQualType(type));
7887 if (objc_class_type)
7888 return objc_class_type->getInterface();
7889 return nullptr;
7890}
7891
7892clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007893 const CompilerType &type, llvm::StringRef name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00007894 const CompilerType &field_clang_type, AccessType access,
7895 uint32_t bitfield_bit_size) {
7896 if (!type.IsValid() || !field_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00007897 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007898 ClangASTContext *ast =
7899 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7900 if (!ast)
7901 return nullptr;
7902 clang::ASTContext *clang_ast = ast->getASTContext();
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007903 clang::IdentifierInfo *ident = nullptr;
7904 if (!name.empty())
7905 ident = &clang_ast->Idents.get(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00007906
7907 clang::FieldDecl *field = nullptr;
7908
7909 clang::Expr *bit_width = nullptr;
7910 if (bitfield_bit_size != 0) {
7911 llvm::APInt bitfield_bit_size_apint(
7912 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7913 bit_width = new (*clang_ast)
7914 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7915 clang_ast->IntTy, clang::SourceLocation());
7916 }
7917
7918 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7919 if (record_decl) {
7920 field = clang::FieldDecl::Create(
7921 *clang_ast, record_decl, clang::SourceLocation(),
7922 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007923 ident, // Identifier
7924 ClangUtil::GetQualType(field_clang_type), // Field type
7925 nullptr, // TInfo *
7926 bit_width, // BitWidth
7927 false, // Mutable
7928 clang::ICIS_NoInit); // HasInit
Kate Stoneb9c1b512016-09-06 20:57:50 +00007929
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007930 if (name.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +00007931 // Determine whether this field corresponds to an anonymous struct or
7932 // union.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007933 if (const clang::TagType *TagT =
7934 field->getType()->getAs<clang::TagType>()) {
7935 if (clang::RecordDecl *Rec =
7936 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7937 if (!Rec->getDeclName()) {
7938 Rec->setAnonymousStructOrUnion(true);
7939 field->setImplicit();
7940 }
7941 }
7942 }
7943
7944 if (field) {
7945 field->setAccess(
7946 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7947
7948 record_decl->addDecl(field);
7949
7950#ifdef LLDB_CONFIGURATION_DEBUG
7951 VerifyDecl(field);
7952#endif
7953 }
7954 } else {
7955 clang::ObjCInterfaceDecl *class_interface_decl =
7956 ast->GetAsObjCInterfaceDecl(type);
7957
7958 if (class_interface_decl) {
7959 const bool is_synthesized = false;
7960
7961 field_clang_type.GetCompleteType();
7962
7963 field = clang::ObjCIvarDecl::Create(
7964 *clang_ast, class_interface_decl, clang::SourceLocation(),
7965 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007966 ident, // Identifier
7967 ClangUtil::GetQualType(field_clang_type), // Field type
7968 nullptr, // TypeSourceInfo *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007969 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7970 is_synthesized);
7971
7972 if (field) {
7973 class_interface_decl->addDecl(field);
7974
7975#ifdef LLDB_CONFIGURATION_DEBUG
7976 VerifyDecl(field);
7977#endif
7978 }
7979 }
7980 }
7981 return field;
Greg Claytond8d4a572015-08-11 21:38:15 +00007982}
7983
Kate Stoneb9c1b512016-09-06 20:57:50 +00007984void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7985 if (!type)
7986 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007987
Kate Stoneb9c1b512016-09-06 20:57:50 +00007988 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7989 if (!ast)
7990 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007991
Kate Stoneb9c1b512016-09-06 20:57:50 +00007992 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007993
Kate Stoneb9c1b512016-09-06 20:57:50 +00007994 if (!record_decl)
7995 return;
7996
7997 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7998
7999 IndirectFieldVector indirect_fields;
8000 clang::RecordDecl::field_iterator field_pos;
8001 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
8002 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
8003 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
8004 last_field_pos = field_pos++) {
8005 if (field_pos->isAnonymousStructOrUnion()) {
8006 clang::QualType field_qual_type = field_pos->getType();
8007
8008 const clang::RecordType *field_record_type =
8009 field_qual_type->getAs<clang::RecordType>();
8010
8011 if (!field_record_type)
8012 continue;
8013
8014 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
8015
8016 if (!field_record_decl)
8017 continue;
8018
8019 for (clang::RecordDecl::decl_iterator
8020 di = field_record_decl->decls_begin(),
8021 de = field_record_decl->decls_end();
8022 di != de; ++di) {
8023 if (clang::FieldDecl *nested_field_decl =
8024 llvm::dyn_cast<clang::FieldDecl>(*di)) {
8025 clang::NamedDecl **chain =
8026 new (*ast->getASTContext()) clang::NamedDecl *[2];
8027 chain[0] = *field_pos;
8028 chain[1] = nested_field_decl;
8029 clang::IndirectFieldDecl *indirect_field =
8030 clang::IndirectFieldDecl::Create(
8031 *ast->getASTContext(), record_decl, clang::SourceLocation(),
8032 nested_field_decl->getIdentifier(),
8033 nested_field_decl->getType(), {chain, 2});
8034
8035 indirect_field->setImplicit();
8036
8037 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
8038 field_pos->getAccess(), nested_field_decl->getAccess()));
8039
8040 indirect_fields.push_back(indirect_field);
8041 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
8042 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
8043 size_t nested_chain_size =
8044 nested_indirect_field_decl->getChainingSize();
8045 clang::NamedDecl **chain = new (*ast->getASTContext())
8046 clang::NamedDecl *[nested_chain_size + 1];
8047 chain[0] = *field_pos;
8048
8049 int chain_index = 1;
8050 for (clang::IndirectFieldDecl::chain_iterator
8051 nci = nested_indirect_field_decl->chain_begin(),
8052 nce = nested_indirect_field_decl->chain_end();
8053 nci < nce; ++nci) {
8054 chain[chain_index] = *nci;
8055 chain_index++;
8056 }
8057
8058 clang::IndirectFieldDecl *indirect_field =
8059 clang::IndirectFieldDecl::Create(
8060 *ast->getASTContext(), record_decl, clang::SourceLocation(),
8061 nested_indirect_field_decl->getIdentifier(),
8062 nested_indirect_field_decl->getType(),
8063 {chain, nested_chain_size + 1});
8064
8065 indirect_field->setImplicit();
8066
8067 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
8068 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
8069
8070 indirect_fields.push_back(indirect_field);
Greg Claytond8d4a572015-08-11 21:38:15 +00008071 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008072 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008073 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008074 }
8075
Adrian Prantl05097242018-04-30 16:49:04 +00008076 // Check the last field to see if it has an incomplete array type as its last
8077 // member and if it does, the tell the record decl about it
Kate Stoneb9c1b512016-09-06 20:57:50 +00008078 if (last_field_pos != field_end_pos) {
8079 if (last_field_pos->getType()->isIncompleteArrayType())
8080 record_decl->hasFlexibleArrayMember();
8081 }
8082
8083 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
8084 ife = indirect_fields.end();
8085 ifi < ife; ++ifi) {
8086 record_decl->addDecl(*ifi);
8087 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008088}
8089
Kate Stoneb9c1b512016-09-06 20:57:50 +00008090void ClangASTContext::SetIsPacked(const CompilerType &type) {
8091 if (type) {
8092 ClangASTContext *ast =
8093 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8094 if (ast) {
8095 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
8096
8097 if (!record_decl)
Greg Claytonf73034f2015-09-08 18:15:05 +00008098 return;
8099
Kate Stoneb9c1b512016-09-06 20:57:50 +00008100 record_decl->addAttr(
8101 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
Greg Claytond8d4a572015-08-11 21:38:15 +00008102 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008103 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008104}
8105
Kate Stoneb9c1b512016-09-06 20:57:50 +00008106clang::VarDecl *ClangASTContext::AddVariableToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008107 const CompilerType &type, llvm::StringRef name,
8108 const CompilerType &var_type, AccessType access) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00008109 if (!type.IsValid() || !var_type.IsValid())
8110 return nullptr;
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008111
Kate Stoneb9c1b512016-09-06 20:57:50 +00008112 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8113 if (!ast)
8114 return nullptr;
8115
8116 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008117 if (!record_decl)
8118 return nullptr;
8119
8120 clang::VarDecl *var_decl = nullptr;
8121 clang::IdentifierInfo *ident = nullptr;
8122 if (!name.empty())
8123 ident = &ast->getASTContext()->Idents.get(name);
8124
8125 var_decl = clang::VarDecl::Create(
8126 *ast->getASTContext(), // ASTContext &
8127 record_decl, // DeclContext *
8128 clang::SourceLocation(), // clang::SourceLocation StartLoc
8129 clang::SourceLocation(), // clang::SourceLocation IdLoc
8130 ident, // clang::IdentifierInfo *
8131 ClangUtil::GetQualType(var_type), // Variable clang::QualType
8132 nullptr, // TypeSourceInfo *
8133 clang::SC_Static); // StorageClass
8134 if (!var_decl)
8135 return nullptr;
8136
8137 var_decl->setAccess(
8138 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
8139 record_decl->addDecl(var_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008140
Greg Claytond8d4a572015-08-11 21:38:15 +00008141#ifdef LLDB_CONFIGURATION_DEBUG
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008142 VerifyDecl(var_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008143#endif
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008144
Kate Stoneb9c1b512016-09-06 20:57:50 +00008145 return var_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008146}
8147
Kate Stoneb9c1b512016-09-06 20:57:50 +00008148clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
Davide Italiano675767a2018-03-27 19:40:50 +00008149 lldb::opaque_compiler_type_t type, const char *name, const char *mangled_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008150 const CompilerType &method_clang_type, lldb::AccessType access,
8151 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
8152 bool is_attr_used, bool is_artificial) {
8153 if (!type || !method_clang_type.IsValid() || name == nullptr ||
8154 name[0] == '\0')
8155 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008156
Kate Stoneb9c1b512016-09-06 20:57:50 +00008157 clang::QualType record_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008158
Kate Stoneb9c1b512016-09-06 20:57:50 +00008159 clang::CXXRecordDecl *cxx_record_decl =
8160 record_qual_type->getAsCXXRecordDecl();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008161
Kate Stoneb9c1b512016-09-06 20:57:50 +00008162 if (cxx_record_decl == nullptr)
8163 return nullptr;
8164
8165 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8166
8167 clang::CXXMethodDecl *cxx_method_decl = nullptr;
8168
8169 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
8170
8171 const clang::FunctionType *function_type =
8172 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
8173
8174 if (function_type == nullptr)
8175 return nullptr;
8176
8177 const clang::FunctionProtoType *method_function_prototype(
8178 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
8179
8180 if (!method_function_prototype)
8181 return nullptr;
8182
8183 unsigned int num_params = method_function_prototype->getNumParams();
8184
8185 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
8186 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
8187
8188 if (is_artificial)
8189 return nullptr; // skip everything artificial
8190
Richard Smith36851a62019-05-09 04:40:57 +00008191 const clang::ExplicitSpecifier explicit_spec(
8192 nullptr /*expr*/, is_explicit
8193 ? clang::ExplicitSpecKind::ResolvedTrue
8194 : clang::ExplicitSpecKind::ResolvedFalse);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008195 if (name[0] == '~') {
8196 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
8197 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8198 clang::DeclarationNameInfo(
8199 getASTContext()->DeclarationNames.getCXXDestructorName(
8200 getASTContext()->getCanonicalType(record_qual_type)),
8201 clang::SourceLocation()),
8202 method_qual_type, nullptr, is_inline, is_artificial);
8203 cxx_method_decl = cxx_dtor_decl;
8204 } else if (decl_name == cxx_record_decl->getDeclName()) {
8205 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
8206 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8207 clang::DeclarationNameInfo(
8208 getASTContext()->DeclarationNames.getCXXConstructorName(
8209 getASTContext()->getCanonicalType(record_qual_type)),
8210 clang::SourceLocation()),
8211 method_qual_type,
8212 nullptr, // TypeSourceInfo *
Richard Smith36851a62019-05-09 04:40:57 +00008213 explicit_spec, is_inline, is_artificial, false /*is_constexpr*/);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008214 cxx_method_decl = cxx_ctor_decl;
8215 } else {
8216 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
8217 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
8218
8219 if (IsOperator(name, op_kind)) {
8220 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
Adrian Prantl05097242018-04-30 16:49:04 +00008221 // Check the number of operator parameters. Sometimes we have seen bad
8222 // DWARF that doesn't correctly describe operators and if we try to
8223 // create a method and add it to the class, clang will assert and
8224 // crash, so we need to make sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00008225 const bool is_method = true;
8226 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
8227 is_method, op_kind, num_params))
8228 return nullptr;
8229 cxx_method_decl = clang::CXXMethodDecl::Create(
8230 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8231 clang::DeclarationNameInfo(
8232 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
8233 clang::SourceLocation()),
8234 method_qual_type,
8235 nullptr, // TypeSourceInfo *
8236 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
8237 } else if (num_params == 0) {
8238 // Conversion operators don't take params...
8239 cxx_method_decl = clang::CXXConversionDecl::Create(
8240 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8241 clang::DeclarationNameInfo(
8242 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
8243 getASTContext()->getCanonicalType(
8244 function_type->getReturnType())),
8245 clang::SourceLocation()),
8246 method_qual_type,
8247 nullptr, // TypeSourceInfo *
Richard Smith36851a62019-05-09 04:40:57 +00008248 is_inline, explicit_spec, false /*is_constexpr*/,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008249 clang::SourceLocation());
8250 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008251 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008252
8253 if (cxx_method_decl == nullptr) {
8254 cxx_method_decl = clang::CXXMethodDecl::Create(
8255 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8256 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8257 method_qual_type,
8258 nullptr, // TypeSourceInfo *
8259 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
Greg Claytond8d4a572015-08-11 21:38:15 +00008260 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008261 }
8262
8263 clang::AccessSpecifier access_specifier =
8264 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8265
8266 cxx_method_decl->setAccess(access_specifier);
8267 cxx_method_decl->setVirtualAsWritten(is_virtual);
8268
8269 if (is_attr_used)
8270 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8271
Konrad Kleine248a1302019-05-23 11:14:47 +00008272 if (mangled_name != nullptr) {
Davide Italiano675767a2018-03-27 19:40:50 +00008273 cxx_method_decl->addAttr(
8274 clang::AsmLabelAttr::CreateImplicit(*getASTContext(), mangled_name));
8275 }
8276
Kate Stoneb9c1b512016-09-06 20:57:50 +00008277 // Populate the method decl with parameter decls
8278
8279 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8280
8281 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8282 params.push_back(clang::ParmVarDecl::Create(
8283 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8284 clang::SourceLocation(),
8285 nullptr, // anonymous
8286 method_function_prototype->getParamType(param_index), nullptr,
8287 clang::SC_None, nullptr));
8288 }
8289
8290 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8291
8292 cxx_record_decl->addDecl(cxx_method_decl);
8293
8294 // Sometimes the debug info will mention a constructor (default/copy/move),
8295 // destructor, or assignment operator (copy/move) but there won't be any
8296 // version of this in the code. So we check if the function was artificially
8297 // generated and if it is trivial and this lets the compiler/backend know
8298 // that it can inline the IR for these when it needs to and we can avoid a
8299 // "missing function" error when running expressions.
8300
8301 if (is_artificial) {
8302 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8303 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8304 (cxx_ctor_decl->isCopyConstructor() &&
8305 cxx_record_decl->hasTrivialCopyConstructor()) ||
8306 (cxx_ctor_decl->isMoveConstructor() &&
8307 cxx_record_decl->hasTrivialMoveConstructor()))) {
8308 cxx_ctor_decl->setDefaulted();
8309 cxx_ctor_decl->setTrivial(true);
8310 } else if (cxx_dtor_decl) {
8311 if (cxx_record_decl->hasTrivialDestructor()) {
8312 cxx_dtor_decl->setDefaulted();
8313 cxx_dtor_decl->setTrivial(true);
8314 }
8315 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8316 cxx_record_decl->hasTrivialCopyAssignment()) ||
8317 (cxx_method_decl->isMoveAssignmentOperator() &&
8318 cxx_record_decl->hasTrivialMoveAssignment())) {
8319 cxx_method_decl->setDefaulted();
8320 cxx_method_decl->setTrivial(true);
Greg Claytond8d4a572015-08-11 21:38:15 +00008321 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008322 }
8323
Greg Claytond8d4a572015-08-11 21:38:15 +00008324#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008325 VerifyDecl(cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008326#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008327
Kate Stoneb9c1b512016-09-06 20:57:50 +00008328 // printf ("decl->isPolymorphic() = %i\n",
8329 // cxx_record_decl->isPolymorphic());
8330 // printf ("decl->isAggregate() = %i\n",
8331 // cxx_record_decl->isAggregate());
8332 // printf ("decl->isPOD() = %i\n",
8333 // cxx_record_decl->isPOD());
8334 // printf ("decl->isEmpty() = %i\n",
8335 // cxx_record_decl->isEmpty());
8336 // printf ("decl->isAbstract() = %i\n",
8337 // cxx_record_decl->isAbstract());
8338 // printf ("decl->hasTrivialConstructor() = %i\n",
8339 // cxx_record_decl->hasTrivialConstructor());
8340 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8341 // cxx_record_decl->hasTrivialCopyConstructor());
8342 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8343 // cxx_record_decl->hasTrivialCopyAssignment());
8344 // printf ("decl->hasTrivialDestructor() = %i\n",
8345 // cxx_record_decl->hasTrivialDestructor());
8346 return cxx_method_decl;
8347}
Greg Claytond8d4a572015-08-11 21:38:15 +00008348
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00008349void ClangASTContext::AddMethodOverridesForCXXRecordType(
8350 lldb::opaque_compiler_type_t type) {
8351 if (auto *record = GetAsCXXRecordDecl(type))
8352 for (auto *method : record->methods())
8353 addOverridesForMethod(method);
8354}
8355
Greg Claytond8d4a572015-08-11 21:38:15 +00008356#pragma mark C++ Base Classes
8357
Zachary Turner970f38e2018-10-25 20:44:56 +00008358std::unique_ptr<clang::CXXBaseSpecifier>
Kate Stoneb9c1b512016-09-06 20:57:50 +00008359ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8360 AccessType access, bool is_virtual,
8361 bool base_of_class) {
Zachary Turner970f38e2018-10-25 20:44:56 +00008362 if (!type)
8363 return nullptr;
8364
8365 return llvm::make_unique<clang::CXXBaseSpecifier>(
8366 clang::SourceRange(), is_virtual, base_of_class,
8367 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8368 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8369 clang::SourceLocation());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008370}
8371
Zachary Turner970f38e2018-10-25 20:44:56 +00008372bool ClangASTContext::TransferBaseClasses(
Kate Stoneb9c1b512016-09-06 20:57:50 +00008373 lldb::opaque_compiler_type_t type,
Zachary Turner970f38e2018-10-25 20:44:56 +00008374 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases) {
8375 if (!type)
8376 return false;
8377 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8378 if (!cxx_record_decl)
8379 return false;
8380 std::vector<clang::CXXBaseSpecifier *> raw_bases;
8381 raw_bases.reserve(bases.size());
8382
8383 // Clang will make a copy of them, so it's ok that we pass pointers that we're
8384 // about to destroy.
8385 for (auto &b : bases)
8386 raw_bases.push_back(b.get());
8387 cxx_record_decl->setBases(raw_bases.data(), raw_bases.size());
8388 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008389}
8390
8391bool ClangASTContext::SetObjCSuperClass(
8392 const CompilerType &type, const CompilerType &superclass_clang_type) {
8393 ClangASTContext *ast =
8394 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8395 if (!ast)
8396 return false;
8397 clang::ASTContext *clang_ast = ast->getASTContext();
8398
8399 if (type && superclass_clang_type.IsValid() &&
8400 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8401 clang::ObjCInterfaceDecl *class_interface_decl =
8402 GetAsObjCInterfaceDecl(type);
8403 clang::ObjCInterfaceDecl *super_interface_decl =
8404 GetAsObjCInterfaceDecl(superclass_clang_type);
8405 if (class_interface_decl && super_interface_decl) {
8406 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8407 clang_ast->getObjCInterfaceType(super_interface_decl)));
8408 return true;
8409 }
8410 }
8411 return false;
8412}
8413
8414bool ClangASTContext::AddObjCClassProperty(
8415 const CompilerType &type, const char *property_name,
8416 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8417 const char *property_setter_name, const char *property_getter_name,
8418 uint32_t property_attributes, ClangASTMetadata *metadata) {
8419 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8420 property_name[0] == '\0')
8421 return false;
8422 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8423 if (!ast)
8424 return false;
8425 clang::ASTContext *clang_ast = ast->getASTContext();
8426
8427 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8428
8429 if (class_interface_decl) {
8430 CompilerType property_clang_type_to_access;
8431
8432 if (property_clang_type.IsValid())
8433 property_clang_type_to_access = property_clang_type;
8434 else if (ivar_decl)
8435 property_clang_type_to_access =
8436 CompilerType(clang_ast, ivar_decl->getType());
8437
8438 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8439 clang::TypeSourceInfo *prop_type_source;
8440 if (ivar_decl)
8441 prop_type_source =
8442 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8443 else
8444 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8445 ClangUtil::GetQualType(property_clang_type));
8446
8447 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8448 *clang_ast, class_interface_decl,
8449 clang::SourceLocation(), // Source Location
8450 &clang_ast->Idents.get(property_name),
8451 clang::SourceLocation(), // Source Location for AT
8452 clang::SourceLocation(), // Source location for (
8453 ivar_decl ? ivar_decl->getType()
8454 : ClangUtil::GetQualType(property_clang_type),
8455 prop_type_source);
8456
8457 if (property_decl) {
8458 if (metadata)
8459 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8460
8461 class_interface_decl->addDecl(property_decl);
8462
8463 clang::Selector setter_sel, getter_sel;
8464
8465 if (property_setter_name != nullptr) {
8466 std::string property_setter_no_colon(
8467 property_setter_name, strlen(property_setter_name) - 1);
8468 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008469 &clang_ast->Idents.get(property_setter_no_colon);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008470 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8471 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8472 std::string setter_sel_string("set");
8473 setter_sel_string.push_back(::toupper(property_name[0]));
8474 setter_sel_string.append(&property_name[1]);
8475 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008476 &clang_ast->Idents.get(setter_sel_string);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008477 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8478 }
8479 property_decl->setSetterName(setter_sel);
8480 property_decl->setPropertyAttributes(
8481 clang::ObjCPropertyDecl::OBJC_PR_setter);
8482
8483 if (property_getter_name != nullptr) {
8484 clang::IdentifierInfo *getter_ident =
8485 &clang_ast->Idents.get(property_getter_name);
8486 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8487 } else {
8488 clang::IdentifierInfo *getter_ident =
8489 &clang_ast->Idents.get(property_name);
8490 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8491 }
8492 property_decl->setGetterName(getter_sel);
8493 property_decl->setPropertyAttributes(
8494 clang::ObjCPropertyDecl::OBJC_PR_getter);
8495
8496 if (ivar_decl)
8497 property_decl->setPropertyIvarDecl(ivar_decl);
8498
8499 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8500 property_decl->setPropertyAttributes(
8501 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8502 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8503 property_decl->setPropertyAttributes(
8504 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8505 if (property_attributes & DW_APPLE_PROPERTY_assign)
8506 property_decl->setPropertyAttributes(
8507 clang::ObjCPropertyDecl::OBJC_PR_assign);
8508 if (property_attributes & DW_APPLE_PROPERTY_retain)
8509 property_decl->setPropertyAttributes(
8510 clang::ObjCPropertyDecl::OBJC_PR_retain);
8511 if (property_attributes & DW_APPLE_PROPERTY_copy)
8512 property_decl->setPropertyAttributes(
8513 clang::ObjCPropertyDecl::OBJC_PR_copy);
8514 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8515 property_decl->setPropertyAttributes(
8516 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8517 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8518 property_decl->setPropertyAttributes(
8519 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8520 if (property_attributes &
8521 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8522 property_decl->setPropertyAttributes(
8523 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8524 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8525 property_decl->setPropertyAttributes(
8526 clang::ObjCPropertyDecl::OBJC_PR_class);
8527
8528 const bool isInstance =
8529 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8530
8531 if (!getter_sel.isNull() &&
8532 !(isInstance
8533 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8534 : class_interface_decl->lookupClassMethod(getter_sel))) {
8535 const bool isVariadic = false;
8536 const bool isSynthesized = false;
8537 const bool isImplicitlyDeclared = true;
8538 const bool isDefined = false;
8539 const clang::ObjCMethodDecl::ImplementationControl impControl =
8540 clang::ObjCMethodDecl::None;
8541 const bool HasRelatedResultType = false;
8542
8543 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8544 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8545 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8546 nullptr, class_interface_decl, isInstance, isVariadic,
8547 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8548 HasRelatedResultType);
8549
8550 if (getter && metadata)
8551 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8552
8553 if (getter) {
8554 getter->setMethodParams(*clang_ast,
8555 llvm::ArrayRef<clang::ParmVarDecl *>(),
8556 llvm::ArrayRef<clang::SourceLocation>());
8557
8558 class_interface_decl->addDecl(getter);
8559 }
8560 }
8561
8562 if (!setter_sel.isNull() &&
8563 !(isInstance
8564 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8565 : class_interface_decl->lookupClassMethod(setter_sel))) {
8566 clang::QualType result_type = clang_ast->VoidTy;
8567 const bool isVariadic = false;
8568 const bool isSynthesized = false;
8569 const bool isImplicitlyDeclared = true;
8570 const bool isDefined = false;
8571 const clang::ObjCMethodDecl::ImplementationControl impControl =
8572 clang::ObjCMethodDecl::None;
8573 const bool HasRelatedResultType = false;
8574
8575 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8576 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8577 setter_sel, result_type, nullptr, class_interface_decl,
8578 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8579 isDefined, impControl, HasRelatedResultType);
8580
8581 if (setter && metadata)
8582 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8583
8584 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8585
8586 params.push_back(clang::ParmVarDecl::Create(
8587 *clang_ast, setter, clang::SourceLocation(),
8588 clang::SourceLocation(),
8589 nullptr, // anonymous
8590 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8591 clang::SC_Auto, nullptr));
8592
8593 if (setter) {
8594 setter->setMethodParams(
8595 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8596 llvm::ArrayRef<clang::SourceLocation>());
8597
8598 class_interface_decl->addDecl(setter);
8599 }
8600 }
8601
8602 return true;
8603 }
8604 }
8605 }
8606 return false;
8607}
8608
8609bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8610 bool check_superclass) {
8611 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8612 if (class_interface_decl)
8613 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8614 return false;
8615}
8616
8617clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8618 const CompilerType &type,
8619 const char *name, // the full symbol name as seen in the symbol table
8620 // (lldb::opaque_compiler_type_t type, "-[NString
8621 // stringWithCString:]")
8622 const CompilerType &method_clang_type, lldb::AccessType access,
8623 bool is_artificial, bool is_variadic) {
8624 if (!type || !method_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008625 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008626
Kate Stoneb9c1b512016-09-06 20:57:50 +00008627 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8628
8629 if (class_interface_decl == nullptr)
8630 return nullptr;
8631 ClangASTContext *lldb_ast =
8632 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8633 if (lldb_ast == nullptr)
8634 return nullptr;
8635 clang::ASTContext *ast = lldb_ast->getASTContext();
8636
8637 const char *selector_start = ::strchr(name, ' ');
8638 if (selector_start == nullptr)
8639 return nullptr;
8640
8641 selector_start++;
8642 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8643
8644 size_t len = 0;
8645 const char *start;
8646 // printf ("name = '%s'\n", name);
8647
8648 unsigned num_selectors_with_args = 0;
8649 for (start = selector_start; start && *start != '\0' && *start != ']';
8650 start += len) {
8651 len = ::strcspn(start, ":]");
8652 bool has_arg = (start[len] == ':');
8653 if (has_arg)
8654 ++num_selectors_with_args;
8655 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8656 if (has_arg)
8657 len += 1;
8658 }
8659
8660 if (selector_idents.size() == 0)
8661 return nullptr;
8662
8663 clang::Selector method_selector = ast->Selectors.getSelector(
8664 num_selectors_with_args ? selector_idents.size() : 0,
8665 selector_idents.data());
8666
8667 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8668
8669 // Populate the method decl with parameter decls
8670 const clang::Type *method_type(method_qual_type.getTypePtr());
8671
8672 if (method_type == nullptr)
8673 return nullptr;
8674
8675 const clang::FunctionProtoType *method_function_prototype(
8676 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8677
8678 if (!method_function_prototype)
8679 return nullptr;
8680
8681 bool is_synthesized = false;
8682 bool is_defined = false;
8683 clang::ObjCMethodDecl::ImplementationControl imp_control =
8684 clang::ObjCMethodDecl::None;
8685
8686 const unsigned num_args = method_function_prototype->getNumParams();
8687
8688 if (num_args != num_selectors_with_args)
8689 return nullptr; // some debug information is corrupt. We are not going to
8690 // deal with it.
8691
8692 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8693 *ast,
8694 clang::SourceLocation(), // beginLoc,
8695 clang::SourceLocation(), // endLoc,
8696 method_selector, method_function_prototype->getReturnType(),
8697 nullptr, // TypeSourceInfo *ResultTInfo,
8698 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8699 ClangUtil::GetQualType(type)),
8700 name[0] == '-', is_variadic, is_synthesized,
8701 true, // is_implicitly_declared; we force this to true because we don't
8702 // have source locations
8703 is_defined, imp_control, false /*has_related_result_type*/);
8704
8705 if (objc_method_decl == nullptr)
8706 return nullptr;
8707
8708 if (num_args > 0) {
8709 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8710
8711 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8712 params.push_back(clang::ParmVarDecl::Create(
8713 *ast, objc_method_decl, clang::SourceLocation(),
8714 clang::SourceLocation(),
8715 nullptr, // anonymous
8716 method_function_prototype->getParamType(param_index), nullptr,
8717 clang::SC_Auto, nullptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00008718 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008719
Kate Stoneb9c1b512016-09-06 20:57:50 +00008720 objc_method_decl->setMethodParams(
8721 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8722 llvm::ArrayRef<clang::SourceLocation>());
8723 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008724
Kate Stoneb9c1b512016-09-06 20:57:50 +00008725 class_interface_decl->addDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008726
Greg Claytond8d4a572015-08-11 21:38:15 +00008727#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008728 VerifyDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008729#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00008730
8731 return objc_method_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008732}
8733
Kate Stoneb9c1b512016-09-06 20:57:50 +00008734bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8735 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00008736 return false;
Greg Claytone6b36cd2015-12-08 01:02:08 +00008737
Kate Stoneb9c1b512016-09-06 20:57:50 +00008738 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008739
Kate Stoneb9c1b512016-09-06 20:57:50 +00008740 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8741 switch (type_class) {
8742 case clang::Type::Record: {
8743 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8744 if (cxx_record_decl)
8745 return cxx_record_decl->hasExternalLexicalStorage() ||
8746 cxx_record_decl->hasExternalVisibleStorage();
8747 } break;
Enrico Granata36f51e42015-12-18 22:41:25 +00008748
Kate Stoneb9c1b512016-09-06 20:57:50 +00008749 case clang::Type::Enum: {
8750 clang::EnumDecl *enum_decl =
8751 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8752 if (enum_decl)
8753 return enum_decl->hasExternalLexicalStorage() ||
8754 enum_decl->hasExternalVisibleStorage();
8755 } break;
8756
8757 case clang::Type::ObjCObject:
8758 case clang::Type::ObjCInterface: {
8759 const clang::ObjCObjectType *objc_class_type =
8760 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8761 assert(objc_class_type);
8762 if (objc_class_type) {
8763 clang::ObjCInterfaceDecl *class_interface_decl =
8764 objc_class_type->getInterface();
8765
8766 if (class_interface_decl)
8767 return class_interface_decl->hasExternalLexicalStorage() ||
8768 class_interface_decl->hasExternalVisibleStorage();
Greg Claytond8d4a572015-08-11 21:38:15 +00008769 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008770 } break;
8771
8772 case clang::Type::Typedef:
8773 return GetHasExternalStorage(CompilerType(
8774 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8775 ->getDecl()
8776 ->getUnderlyingType()
8777 .getAsOpaquePtr()));
8778
8779 case clang::Type::Auto:
8780 return GetHasExternalStorage(CompilerType(
8781 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8782 ->getDeducedType()
8783 .getAsOpaquePtr()));
8784
8785 case clang::Type::Elaborated:
8786 return GetHasExternalStorage(CompilerType(
8787 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8788 ->getNamedType()
8789 .getAsOpaquePtr()));
8790
8791 case clang::Type::Paren:
8792 return GetHasExternalStorage(CompilerType(
8793 type.GetTypeSystem(),
8794 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8795
8796 default:
8797 break;
8798 }
8799 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008800}
8801
Kate Stoneb9c1b512016-09-06 20:57:50 +00008802bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8803 bool has_extern) {
8804 if (!type)
8805 return false;
8806
8807 clang::QualType qual_type(GetCanonicalQualType(type));
8808
8809 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8810 switch (type_class) {
8811 case clang::Type::Record: {
8812 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8813 if (cxx_record_decl) {
8814 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8815 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8816 return true;
8817 }
8818 } break;
8819
8820 case clang::Type::Enum: {
8821 clang::EnumDecl *enum_decl =
8822 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8823 if (enum_decl) {
8824 enum_decl->setHasExternalLexicalStorage(has_extern);
8825 enum_decl->setHasExternalVisibleStorage(has_extern);
8826 return true;
8827 }
8828 } break;
8829
8830 case clang::Type::ObjCObject:
8831 case clang::Type::ObjCInterface: {
8832 const clang::ObjCObjectType *objc_class_type =
8833 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8834 assert(objc_class_type);
8835 if (objc_class_type) {
8836 clang::ObjCInterfaceDecl *class_interface_decl =
8837 objc_class_type->getInterface();
8838
8839 if (class_interface_decl) {
8840 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8841 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8842 return true;
8843 }
8844 }
8845 } break;
8846
8847 case clang::Type::Typedef:
8848 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8849 ->getDecl()
8850 ->getUnderlyingType()
8851 .getAsOpaquePtr(),
8852 has_extern);
8853
8854 case clang::Type::Auto:
8855 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8856 ->getDeducedType()
8857 .getAsOpaquePtr(),
8858 has_extern);
8859
8860 case clang::Type::Elaborated:
8861 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8862 ->getNamedType()
8863 .getAsOpaquePtr(),
8864 has_extern);
8865
8866 case clang::Type::Paren:
8867 return SetHasExternalStorage(
8868 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8869 has_extern);
8870
8871 default:
8872 break;
8873 }
8874 return false;
8875}
Greg Claytond8d4a572015-08-11 21:38:15 +00008876
8877#pragma mark TagDecl
8878
Kate Stoneb9c1b512016-09-06 20:57:50 +00008879bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8880 clang::QualType qual_type(ClangUtil::GetQualType(type));
8881 if (!qual_type.isNull()) {
8882 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8883 if (tag_type) {
8884 clang::TagDecl *tag_decl = tag_type->getDecl();
8885 if (tag_decl) {
8886 tag_decl->startDefinition();
8887 return true;
8888 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008889 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008890
8891 const clang::ObjCObjectType *object_type =
8892 qual_type->getAs<clang::ObjCObjectType>();
8893 if (object_type) {
8894 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8895 if (interface_decl) {
8896 interface_decl->startDefinition();
8897 return true;
8898 }
8899 }
8900 }
8901 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008902}
8903
Kate Stoneb9c1b512016-09-06 20:57:50 +00008904bool ClangASTContext::CompleteTagDeclarationDefinition(
8905 const CompilerType &type) {
8906 clang::QualType qual_type(ClangUtil::GetQualType(type));
8907 if (!qual_type.isNull()) {
8908 // Make sure we use the same methodology as
Adrian Prantl05097242018-04-30 16:49:04 +00008909 // ClangASTContext::StartTagDeclarationDefinition() as to how we start/end
8910 // the definition. Previously we were calling
Kate Stoneb9c1b512016-09-06 20:57:50 +00008911 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8912 if (tag_type) {
8913 clang::TagDecl *tag_decl = tag_type->getDecl();
8914 if (tag_decl) {
8915 clang::CXXRecordDecl *cxx_record_decl =
8916 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8917
8918 if (cxx_record_decl) {
8919 if (!cxx_record_decl->isCompleteDefinition())
8920 cxx_record_decl->completeDefinition();
8921 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8922 cxx_record_decl->setHasExternalLexicalStorage(false);
8923 cxx_record_decl->setHasExternalVisibleStorage(false);
8924 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008925 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008926 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008927 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008928
8929 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8930
8931 if (enutype) {
8932 clang::EnumDecl *enum_decl = enutype->getDecl();
8933
8934 if (enum_decl) {
8935 if (!enum_decl->isCompleteDefinition()) {
8936 ClangASTContext *lldb_ast =
8937 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8938 if (lldb_ast == nullptr)
8939 return false;
8940 clang::ASTContext *ast = lldb_ast->getASTContext();
8941
8942 /// TODO This really needs to be fixed.
8943
8944 QualType integer_type(enum_decl->getIntegerType());
8945 if (!integer_type.isNull()) {
8946 unsigned NumPositiveBits = 1;
8947 unsigned NumNegativeBits = 0;
8948
8949 clang::QualType promotion_qual_type;
8950 // If the enum integer type is less than an integer in bit width,
8951 // then we must promote it to an integer size.
8952 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8953 ast->getTypeSize(ast->IntTy)) {
8954 if (enum_decl->getIntegerType()->isSignedIntegerType())
8955 promotion_qual_type = ast->IntTy;
8956 else
8957 promotion_qual_type = ast->UnsignedIntTy;
8958 } else
8959 promotion_qual_type = enum_decl->getIntegerType();
8960
8961 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8962 promotion_qual_type, NumPositiveBits,
8963 NumNegativeBits);
8964 }
8965 }
8966 return true;
8967 }
8968 }
8969 }
8970 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008971}
8972
Aleksandr Urakov709426b2018-09-10 08:08:43 +00008973clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008974 const CompilerType &enum_type, const Declaration &decl, const char *name,
Zachary Turner1639c6b2018-12-17 16:15:13 +00008975 const llvm::APSInt &value) {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008976
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008977 if (!enum_type || ConstString(name).IsEmpty())
8978 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008979
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008980 lldbassert(enum_type.GetTypeSystem() == static_cast<TypeSystem *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +00008981
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008982 lldb::opaque_compiler_type_t enum_opaque_compiler_type =
8983 enum_type.GetOpaqueQualType();
8984
8985 if (!enum_opaque_compiler_type)
8986 return nullptr;
8987
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008988 clang::QualType enum_qual_type(
8989 GetCanonicalQualType(enum_opaque_compiler_type));
8990
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008991 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8992
8993 if (!clang_type)
8994 return nullptr;
8995
8996 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8997
8998 if (!enutype)
8999 return nullptr;
9000
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009001 clang::EnumConstantDecl *enumerator_decl = clang::EnumConstantDecl::Create(
9002 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
9003 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier
Zachary Turner1639c6b2018-12-17 16:15:13 +00009004 clang::QualType(enutype, 0), nullptr, value);
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009005
9006 if (!enumerator_decl)
9007 return nullptr;
9008
9009 enutype->getDecl()->addDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009010
9011#ifdef LLDB_CONFIGURATION_DEBUG
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009012 VerifyDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009013#endif
9014
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009015 return enumerator_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00009016}
9017
Zachary Turner1639c6b2018-12-17 16:15:13 +00009018clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
9019 const CompilerType &enum_type, const Declaration &decl, const char *name,
9020 int64_t enum_value, uint32_t enum_value_bit_size) {
9021 CompilerType underlying_type =
9022 GetEnumerationIntegerType(enum_type.GetOpaqueQualType());
9023 bool is_signed = false;
9024 underlying_type.IsIntegerType(is_signed);
9025
9026 llvm::APSInt value(enum_value_bit_size, is_signed);
9027 value = enum_value;
9028
9029 return AddEnumerationValueToEnumerationType(enum_type, decl, name, value);
9030}
9031
Greg Claytona1e5dc82015-08-11 22:53:00 +00009032CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00009033ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
9034 clang::QualType enum_qual_type(GetCanonicalQualType(type));
9035 const clang::Type *clang_type = enum_qual_type.getTypePtr();
9036 if (clang_type) {
9037 const clang::EnumType *enutype =
9038 llvm::dyn_cast<clang::EnumType>(clang_type);
9039 if (enutype) {
9040 clang::EnumDecl *enum_decl = enutype->getDecl();
9041 if (enum_decl)
9042 return CompilerType(getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00009043 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009044 }
9045 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00009046}
9047
Kate Stoneb9c1b512016-09-06 20:57:50 +00009048CompilerType
9049ClangASTContext::CreateMemberPointerType(const CompilerType &type,
9050 const CompilerType &pointee_type) {
9051 if (type && pointee_type.IsValid() &&
9052 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
9053 ClangASTContext *ast =
9054 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
9055 if (!ast)
9056 return CompilerType();
9057 return CompilerType(ast->getASTContext(),
9058 ast->getASTContext()->getMemberPointerType(
9059 ClangUtil::GetQualType(pointee_type),
9060 ClangUtil::GetQualType(type).getTypePtr()));
9061 }
9062 return CompilerType();
9063}
Greg Claytond8d4a572015-08-11 21:38:15 +00009064
9065size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00009066ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
9067 const char *s, uint8_t *dst,
9068 size_t dst_size) {
9069 if (type) {
9070 clang::QualType qual_type(GetCanonicalQualType(type));
9071 uint32_t count = 0;
9072 bool is_complex = false;
9073 if (IsFloatingPointType(type, count, is_complex)) {
9074 // TODO: handle complex and vector types
9075 if (count != 1)
9076 return false;
9077
9078 llvm::StringRef s_sref(s);
9079 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type),
9080 s_sref);
9081
9082 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type);
9083 const uint64_t byte_size = bit_size / 8;
9084 if (dst_size >= byte_size) {
9085 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(
9086 llvm::NextPowerOf2(byte_size) * 8);
Zachary Turner97206d52017-05-12 04:51:55 +00009087 lldb_private::Status get_data_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009088 if (scalar.GetAsMemoryData(dst, byte_size,
9089 lldb_private::endian::InlHostByteOrder(),
9090 get_data_error))
9091 return byte_size;
9092 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009093 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009094 }
9095 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00009096}
9097
Greg Claytond8d4a572015-08-11 21:38:15 +00009098// Dumping types
Greg Claytond8d4a572015-08-11 21:38:15 +00009099#define DEPTH_INCREMENT 2
9100
Adrian Prantl0c72a422019-03-07 20:20:02 +00009101#ifndef NDEBUG
9102LLVM_DUMP_METHOD void
9103ClangASTContext::dump(lldb::opaque_compiler_type_t type) const {
9104 if (!type)
9105 return;
9106 clang::QualType qual_type(GetQualType(type));
9107 qual_type.dump();
9108}
9109#endif
9110
Zachary Turner49110232018-11-05 17:40:28 +00009111void ClangASTContext::Dump(Stream &s) {
Zachary Turner115209e2018-11-05 19:25:39 +00009112 Decl *tu = Decl::castFromDeclContext(GetTranslationUnitDecl());
Zachary Turner49110232018-11-05 17:40:28 +00009113 tu->dump(s.AsRawOstream());
9114}
9115
Kate Stoneb9c1b512016-09-06 20:57:50 +00009116void ClangASTContext::DumpValue(
9117 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
Zachary Turner29cb8682017-03-03 20:57:05 +00009118 lldb::Format format, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009119 lldb::offset_t data_byte_offset, size_t data_byte_size,
9120 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
9121 bool show_summary, bool verbose, uint32_t depth) {
9122 if (!type)
9123 return;
9124
9125 clang::QualType qual_type(GetQualType(type));
9126 switch (qual_type->getTypeClass()) {
9127 case clang::Type::Record:
9128 if (GetCompleteType(type)) {
9129 const clang::RecordType *record_type =
9130 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9131 const clang::RecordDecl *record_decl = record_type->getDecl();
9132 assert(record_decl);
9133 uint32_t field_bit_offset = 0;
9134 uint32_t field_byte_offset = 0;
9135 const clang::ASTRecordLayout &record_layout =
9136 getASTContext()->getASTRecordLayout(record_decl);
9137 uint32_t child_idx = 0;
9138
9139 const clang::CXXRecordDecl *cxx_record_decl =
9140 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9141 if (cxx_record_decl) {
9142 // We might have base classes to print out first
9143 clang::CXXRecordDecl::base_class_const_iterator base_class,
9144 base_class_end;
9145 for (base_class = cxx_record_decl->bases_begin(),
9146 base_class_end = cxx_record_decl->bases_end();
9147 base_class != base_class_end; ++base_class) {
9148 const clang::CXXRecordDecl *base_class_decl =
9149 llvm::cast<clang::CXXRecordDecl>(
9150 base_class->getType()->getAs<clang::RecordType>()->getDecl());
9151
9152 // Skip empty base classes
Jonas Devliegherea6682a42018-12-15 00:15:33 +00009153 if (!verbose && !ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00009154 continue;
9155
9156 if (base_class->isVirtual())
9157 field_bit_offset =
9158 record_layout.getVBaseClassOffset(base_class_decl)
9159 .getQuantity() *
9160 8;
9161 else
9162 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
9163 .getQuantity() *
9164 8;
9165 field_byte_offset = field_bit_offset / 8;
9166 assert(field_bit_offset % 8 == 0);
9167 if (child_idx == 0)
9168 s->PutChar('{');
9169 else
9170 s->PutChar(',');
9171
9172 clang::QualType base_class_qual_type = base_class->getType();
9173 std::string base_class_type_name(base_class_qual_type.getAsString());
9174
9175 // Indent and print the base class type name
Zachary Turner827d5d72016-12-16 04:27:00 +00009176 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
9177 base_class_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009178
9179 clang::TypeInfo base_class_type_info =
9180 getASTContext()->getTypeInfo(base_class_qual_type);
9181
9182 // Dump the value of the member
9183 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
9184 base_clang_type.DumpValue(
9185 exe_ctx,
9186 s, // Stream to dump to
9187 base_clang_type
9188 .GetFormat(), // The format with which to display the member
9189 data, // Data buffer containing all bytes for this type
9190 data_byte_offset + field_byte_offset, // Offset into "data" where
9191 // to grab value from
9192 base_class_type_info.Width / 8, // Size of this type in bytes
9193 0, // Bitfield bit size
9194 0, // Bitfield bit offset
9195 show_types, // Boolean indicating if we should show the variable
9196 // types
9197 show_summary, // Boolean indicating if we should show a summary
9198 // for the current type
9199 verbose, // Verbose output?
9200 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9201 // children
9202
9203 ++child_idx;
9204 }
9205 }
9206 uint32_t field_idx = 0;
9207 clang::RecordDecl::field_iterator field, field_end;
9208 for (field = record_decl->field_begin(),
9209 field_end = record_decl->field_end();
9210 field != field_end; ++field, ++field_idx, ++child_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009211 // Print the starting squiggly bracket (if this is the first member) or
9212 // comma (for member 2 and beyond) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009213 if (child_idx == 0)
9214 s->PutChar('{');
9215 else
9216 s->PutChar(',');
9217
9218 // Indent
9219 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
9220
9221 clang::QualType field_type = field->getType();
9222 // Print the member type if requested
Adrian Prantl05097242018-04-30 16:49:04 +00009223 // Figure out the type byte size (field_type_info.first) and alignment
9224 // (field_type_info.second) from the AST context.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009225 clang::TypeInfo field_type_info =
9226 getASTContext()->getTypeInfo(field_type);
9227 assert(field_idx < record_layout.getFieldCount());
9228 // Figure out the field offset within the current struct/union/class
9229 // type
9230 field_bit_offset = record_layout.getFieldOffset(field_idx);
9231 field_byte_offset = field_bit_offset / 8;
9232 uint32_t field_bitfield_bit_size = 0;
9233 uint32_t field_bitfield_bit_offset = 0;
9234 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
9235 field_bitfield_bit_size))
9236 field_bitfield_bit_offset = field_bit_offset % 8;
9237
9238 if (show_types) {
9239 std::string field_type_name(field_type.getAsString());
9240 if (field_bitfield_bit_size > 0)
9241 s->Printf("(%s:%u) ", field_type_name.c_str(),
9242 field_bitfield_bit_size);
9243 else
9244 s->Printf("(%s) ", field_type_name.c_str());
9245 }
9246 // Print the member name and equal sign
9247 s->Printf("%s = ", field->getNameAsString().c_str());
9248
9249 // Dump the value of the member
9250 CompilerType field_clang_type(getASTContext(), field_type);
9251 field_clang_type.DumpValue(
9252 exe_ctx,
9253 s, // Stream to dump to
9254 field_clang_type
9255 .GetFormat(), // The format with which to display the member
9256 data, // Data buffer containing all bytes for this type
9257 data_byte_offset + field_byte_offset, // Offset into "data" where to
9258 // grab value from
9259 field_type_info.Width / 8, // Size of this type in bytes
9260 field_bitfield_bit_size, // Bitfield bit size
9261 field_bitfield_bit_offset, // Bitfield bit offset
9262 show_types, // Boolean indicating if we should show the variable
9263 // types
9264 show_summary, // Boolean indicating if we should show a summary for
9265 // the current type
9266 verbose, // Verbose output?
9267 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9268 // children
9269 }
9270
9271 // Indent the trailing squiggly bracket
9272 if (child_idx > 0)
9273 s->Printf("\n%*s}", depth, "");
9274 }
9275 return;
9276
9277 case clang::Type::Enum:
9278 if (GetCompleteType(type)) {
9279 const clang::EnumType *enutype =
9280 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9281 const clang::EnumDecl *enum_decl = enutype->getDecl();
9282 assert(enum_decl);
9283 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9284 lldb::offset_t offset = data_byte_offset;
9285 const int64_t enum_value = data.GetMaxU64Bitfield(
9286 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
9287 for (enum_pos = enum_decl->enumerator_begin(),
9288 enum_end_pos = enum_decl->enumerator_end();
9289 enum_pos != enum_end_pos; ++enum_pos) {
9290 if (enum_pos->getInitVal() == enum_value) {
9291 s->Printf("%s", enum_pos->getNameAsString().c_str());
9292 return;
9293 }
9294 }
Adrian Prantl05097242018-04-30 16:49:04 +00009295 // If we have gotten here we didn't get find the enumerator in the enum
9296 // decl, so just print the integer.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009297 s->Printf("%" PRIi64, enum_value);
9298 }
9299 return;
9300
9301 case clang::Type::ConstantArray: {
9302 const clang::ConstantArrayType *array =
9303 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9304 bool is_array_of_characters = false;
9305 clang::QualType element_qual_type = array->getElementType();
9306
9307 const clang::Type *canonical_type =
9308 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9309 if (canonical_type)
9310 is_array_of_characters = canonical_type->isCharType();
9311
9312 const uint64_t element_count = array->getSize().getLimitedValue();
9313
9314 clang::TypeInfo field_type_info =
9315 getASTContext()->getTypeInfo(element_qual_type);
9316
9317 uint32_t element_idx = 0;
9318 uint32_t element_offset = 0;
9319 uint64_t element_byte_size = field_type_info.Width / 8;
9320 uint32_t element_stride = element_byte_size;
9321
9322 if (is_array_of_characters) {
9323 s->PutChar('"');
Zachary Turner29cb8682017-03-03 20:57:05 +00009324 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9325 element_byte_size, element_count, UINT32_MAX,
9326 LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009327 s->PutChar('"');
9328 return;
9329 } else {
9330 CompilerType element_clang_type(getASTContext(), element_qual_type);
9331 lldb::Format element_format = element_clang_type.GetFormat();
9332
9333 for (element_idx = 0; element_idx < element_count; ++element_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009334 // Print the starting squiggly bracket (if this is the first member) or
9335 // comman (for member 2 and beyong) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009336 if (element_idx == 0)
9337 s->PutChar('{');
9338 else
9339 s->PutChar(',');
9340
9341 // Indent and print the index
9342 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9343
9344 // Figure out the field offset within the current struct/union/class
9345 // type
9346 element_offset = element_idx * element_stride;
9347
9348 // Dump the value of the member
9349 element_clang_type.DumpValue(
9350 exe_ctx,
9351 s, // Stream to dump to
9352 element_format, // The format with which to display the element
9353 data, // Data buffer containing all bytes for this type
9354 data_byte_offset +
9355 element_offset, // Offset into "data" where to grab value from
9356 element_byte_size, // Size of this type in bytes
9357 0, // Bitfield bit size
9358 0, // Bitfield bit offset
9359 show_types, // Boolean indicating if we should show the variable
9360 // types
9361 show_summary, // Boolean indicating if we should show a summary for
9362 // the current type
9363 verbose, // Verbose output?
9364 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9365 // children
9366 }
9367
9368 // Indent the trailing squiggly bracket
9369 if (element_idx > 0)
9370 s->Printf("\n%*s}", depth, "");
9371 }
9372 }
9373 return;
9374
9375 case clang::Type::Typedef: {
9376 clang::QualType typedef_qual_type =
9377 llvm::cast<clang::TypedefType>(qual_type)
9378 ->getDecl()
9379 ->getUnderlyingType();
9380
9381 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9382 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9383 clang::TypeInfo typedef_type_info =
9384 getASTContext()->getTypeInfo(typedef_qual_type);
9385 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9386
9387 return typedef_clang_type.DumpValue(
9388 exe_ctx,
9389 s, // Stream to dump to
9390 typedef_format, // The format with which to display the element
9391 data, // Data buffer containing all bytes for this type
9392 data_byte_offset, // Offset into "data" where to grab value from
9393 typedef_byte_size, // Size of this type in bytes
9394 bitfield_bit_size, // Bitfield bit size
9395 bitfield_bit_offset, // Bitfield bit offset
9396 show_types, // Boolean indicating if we should show the variable types
9397 show_summary, // Boolean indicating if we should show a summary for the
9398 // current type
9399 verbose, // Verbose output?
9400 depth); // Scope depth for any types that have children
9401 } break;
9402
9403 case clang::Type::Auto: {
9404 clang::QualType elaborated_qual_type =
9405 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9406 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9407 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9408 clang::TypeInfo elaborated_type_info =
9409 getASTContext()->getTypeInfo(elaborated_qual_type);
9410 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9411
9412 return elaborated_clang_type.DumpValue(
9413 exe_ctx,
9414 s, // Stream to dump to
9415 elaborated_format, // The format with which to display the element
9416 data, // Data buffer containing all bytes for this type
9417 data_byte_offset, // Offset into "data" where to grab value from
9418 elaborated_byte_size, // Size of this type in bytes
9419 bitfield_bit_size, // Bitfield bit size
9420 bitfield_bit_offset, // Bitfield bit offset
9421 show_types, // Boolean indicating if we should show the variable types
9422 show_summary, // Boolean indicating if we should show a summary for the
9423 // current type
9424 verbose, // Verbose output?
9425 depth); // Scope depth for any types that have children
9426 } break;
9427
9428 case clang::Type::Elaborated: {
9429 clang::QualType elaborated_qual_type =
9430 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9431 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9432 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9433 clang::TypeInfo elaborated_type_info =
9434 getASTContext()->getTypeInfo(elaborated_qual_type);
9435 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9436
9437 return elaborated_clang_type.DumpValue(
9438 exe_ctx,
9439 s, // Stream to dump to
9440 elaborated_format, // The format with which to display the element
9441 data, // Data buffer containing all bytes for this type
9442 data_byte_offset, // Offset into "data" where to grab value from
9443 elaborated_byte_size, // Size of this type in bytes
9444 bitfield_bit_size, // Bitfield bit size
9445 bitfield_bit_offset, // Bitfield bit offset
9446 show_types, // Boolean indicating if we should show the variable types
9447 show_summary, // Boolean indicating if we should show a summary for the
9448 // current type
9449 verbose, // Verbose output?
9450 depth); // Scope depth for any types that have children
9451 } break;
9452
9453 case clang::Type::Paren: {
9454 clang::QualType desugar_qual_type =
9455 llvm::cast<clang::ParenType>(qual_type)->desugar();
9456 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type);
9457
9458 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9459 clang::TypeInfo desugar_type_info =
9460 getASTContext()->getTypeInfo(desugar_qual_type);
9461 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9462
9463 return desugar_clang_type.DumpValue(
9464 exe_ctx,
9465 s, // Stream to dump to
9466 desugar_format, // The format with which to display the element
9467 data, // Data buffer containing all bytes for this type
9468 data_byte_offset, // Offset into "data" where to grab value from
9469 desugar_byte_size, // Size of this type in bytes
9470 bitfield_bit_size, // Bitfield bit size
9471 bitfield_bit_offset, // Bitfield bit offset
9472 show_types, // Boolean indicating if we should show the variable types
9473 show_summary, // Boolean indicating if we should show a summary for the
9474 // current type
9475 verbose, // Verbose output?
9476 depth); // Scope depth for any types that have children
9477 } break;
9478
9479 default:
9480 // We are down to a scalar type that we just need to display.
Zachary Turner29cb8682017-03-03 20:57:05 +00009481 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9482 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9483 bitfield_bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009484
9485 if (show_summary)
9486 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9487 break;
9488 }
9489}
9490
9491bool ClangASTContext::DumpTypeValue(
9492 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
Zachary Turner29cb8682017-03-03 20:57:05 +00009493 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9494 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009495 ExecutionContextScope *exe_scope) {
9496 if (!type)
9497 return false;
9498 if (IsAggregateType(type)) {
9499 return false;
9500 } else {
Greg Claytond8d4a572015-08-11 21:38:15 +00009501 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009502
9503 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9504 switch (type_class) {
9505 case clang::Type::Typedef: {
9506 clang::QualType typedef_qual_type =
9507 llvm::cast<clang::TypedefType>(qual_type)
9508 ->getDecl()
9509 ->getUnderlyingType();
9510 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9511 if (format == eFormatDefault)
9512 format = typedef_clang_type.GetFormat();
9513 clang::TypeInfo typedef_type_info =
9514 getASTContext()->getTypeInfo(typedef_qual_type);
9515 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9516
9517 return typedef_clang_type.DumpTypeValue(
9518 s,
9519 format, // The format with which to display the element
9520 data, // Data buffer containing all bytes for this type
9521 byte_offset, // Offset into "data" where to grab value from
9522 typedef_byte_size, // Size of this type in bytes
9523 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9524 // treat as a bitfield
9525 bitfield_bit_offset, // Offset in bits of a bitfield value if
9526 // bitfield_bit_size != 0
9527 exe_scope);
9528 } break;
9529
9530 case clang::Type::Enum:
Adrian Prantl05097242018-04-30 16:49:04 +00009531 // If our format is enum or default, show the enumeration value as its
9532 // enumeration string value, else just display it as requested.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009533 if ((format == eFormatEnum || format == eFormatDefault) &&
9534 GetCompleteType(type)) {
9535 const clang::EnumType *enutype =
9536 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9537 const clang::EnumDecl *enum_decl = enutype->getDecl();
9538 assert(enum_decl);
9539 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9540 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9541 lldb::offset_t offset = byte_offset;
9542 if (is_signed) {
9543 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9544 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9545 for (enum_pos = enum_decl->enumerator_begin(),
9546 enum_end_pos = enum_decl->enumerator_end();
9547 enum_pos != enum_end_pos; ++enum_pos) {
9548 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009549 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009550 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009551 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009552 }
9553 // If we have gotten here we didn't get find the enumerator in the
9554 // enum decl, so just print the integer.
9555 s->Printf("%" PRIi64, enum_svalue);
9556 } else {
9557 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9558 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9559 for (enum_pos = enum_decl->enumerator_begin(),
9560 enum_end_pos = enum_decl->enumerator_end();
9561 enum_pos != enum_end_pos; ++enum_pos) {
9562 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009563 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009564 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009565 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009566 }
9567 // If we have gotten here we didn't get find the enumerator in the
9568 // enum decl, so just print the integer.
9569 s->Printf("%" PRIu64, enum_uvalue);
Greg Claytond8d4a572015-08-11 21:38:15 +00009570 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009571 return true;
9572 }
9573 // format was not enum, just fall through and dump the value as
9574 // requested....
9575 LLVM_FALLTHROUGH;
9576
9577 default:
9578 // We are down to a scalar type that we just need to display.
9579 {
9580 uint32_t item_count = 1;
9581 // A few formats, we might need to modify our size and count for
9582 // depending
9583 // on how we are trying to display the value...
9584 switch (format) {
Greg Claytond8d4a572015-08-11 21:38:15 +00009585 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00009586 case eFormatBoolean:
9587 case eFormatBinary:
9588 case eFormatComplex:
9589 case eFormatCString: // NULL terminated C strings
9590 case eFormatDecimal:
9591 case eFormatEnum:
9592 case eFormatHex:
9593 case eFormatHexUppercase:
9594 case eFormatFloat:
9595 case eFormatOctal:
9596 case eFormatOSType:
9597 case eFormatUnsigned:
9598 case eFormatPointer:
9599 case eFormatVectorOfChar:
9600 case eFormatVectorOfSInt8:
9601 case eFormatVectorOfUInt8:
9602 case eFormatVectorOfSInt16:
9603 case eFormatVectorOfUInt16:
9604 case eFormatVectorOfSInt32:
9605 case eFormatVectorOfUInt32:
9606 case eFormatVectorOfSInt64:
9607 case eFormatVectorOfUInt64:
9608 case eFormatVectorOfFloat32:
9609 case eFormatVectorOfFloat64:
9610 case eFormatVectorOfUInt128:
9611 break;
9612
9613 case eFormatChar:
9614 case eFormatCharPrintable:
9615 case eFormatCharArray:
9616 case eFormatBytes:
9617 case eFormatBytesWithASCII:
9618 item_count = byte_size;
9619 byte_size = 1;
9620 break;
9621
9622 case eFormatUnicode16:
9623 item_count = byte_size / 2;
9624 byte_size = 2;
9625 break;
9626
9627 case eFormatUnicode32:
9628 item_count = byte_size / 4;
9629 byte_size = 4;
9630 break;
9631 }
Zachary Turner29cb8682017-03-03 20:57:05 +00009632 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9633 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
9634 bitfield_bit_size, bitfield_bit_offset,
9635 exe_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009636 }
9637 break;
9638 }
9639 }
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00009640 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009641}
9642
9643void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9644 ExecutionContext *exe_ctx, Stream *s,
9645 const lldb_private::DataExtractor &data,
9646 lldb::offset_t data_byte_offset,
9647 size_t data_byte_size) {
9648 uint32_t length = 0;
9649 if (IsCStringType(type, length)) {
9650 if (exe_ctx) {
9651 Process *process = exe_ctx->GetProcessPtr();
9652 if (process) {
9653 lldb::offset_t offset = data_byte_offset;
9654 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9655 std::vector<uint8_t> buf;
9656 if (length > 0)
9657 buf.resize(length);
9658 else
9659 buf.resize(256);
9660
Zachary Turner29cb8682017-03-03 20:57:05 +00009661 DataExtractor cstr_data(&buf.front(), buf.size(),
9662 process->GetByteOrder(), 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009663 buf.back() = '\0';
9664 size_t bytes_read;
9665 size_t total_cstr_len = 0;
Zachary Turner97206d52017-05-12 04:51:55 +00009666 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009667 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9668 buf.size(), error)) > 0) {
9669 const size_t len = strlen((const char *)&buf.front());
9670 if (len == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00009671 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009672 if (total_cstr_len == 0)
9673 s->PutCString(" \"");
Zachary Turner29cb8682017-03-03 20:57:05 +00009674 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9675 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009676 total_cstr_len += len;
9677 if (len < buf.size())
9678 break;
9679 pointer_address += total_cstr_len;
Greg Claytond8d4a572015-08-11 21:38:15 +00009680 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009681 if (total_cstr_len > 0)
9682 s->PutChar('"');
9683 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009684 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009685 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009686}
9687
Kate Stoneb9c1b512016-09-06 20:57:50 +00009688void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9689 StreamFile s(stdout, false);
9690 DumpTypeDescription(type, &s);
9691 ClangASTMetadata *metadata =
9692 ClangASTContext::GetMetadata(getASTContext(), type);
9693 if (metadata) {
9694 metadata->Dump(&s);
9695 }
9696}
Greg Claytond8d4a572015-08-11 21:38:15 +00009697
Kate Stoneb9c1b512016-09-06 20:57:50 +00009698void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9699 Stream *s) {
9700 if (type) {
9701 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00009702
Kate Stoneb9c1b512016-09-06 20:57:50 +00009703 llvm::SmallVector<char, 1024> buf;
9704 llvm::raw_svector_ostream llvm_ostrm(buf);
9705
9706 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9707 switch (type_class) {
9708 case clang::Type::ObjCObject:
9709 case clang::Type::ObjCInterface: {
9710 GetCompleteType(type);
9711
9712 const clang::ObjCObjectType *objc_class_type =
9713 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9714 assert(objc_class_type);
9715 if (objc_class_type) {
9716 clang::ObjCInterfaceDecl *class_interface_decl =
9717 objc_class_type->getInterface();
9718 if (class_interface_decl) {
9719 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9720 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
Greg Claytond8d4a572015-08-11 21:38:15 +00009721 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009722 }
9723 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00009724
Kate Stoneb9c1b512016-09-06 20:57:50 +00009725 case clang::Type::Typedef: {
9726 const clang::TypedefType *typedef_type =
9727 qual_type->getAs<clang::TypedefType>();
9728 if (typedef_type) {
9729 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9730 std::string clang_typedef_name(
9731 typedef_decl->getQualifiedNameAsString());
9732 if (!clang_typedef_name.empty()) {
9733 s->PutCString("typedef ");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009734 s->PutCString(clang_typedef_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00009735 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009736 }
9737 } break;
9738
9739 case clang::Type::Auto:
9740 CompilerType(getASTContext(),
9741 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
9742 .DumpTypeDescription(s);
9743 return;
9744
9745 case clang::Type::Elaborated:
9746 CompilerType(getASTContext(),
9747 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
9748 .DumpTypeDescription(s);
9749 return;
9750
9751 case clang::Type::Paren:
9752 CompilerType(getASTContext(),
9753 llvm::cast<clang::ParenType>(qual_type)->desugar())
9754 .DumpTypeDescription(s);
9755 return;
9756
9757 case clang::Type::Record: {
9758 GetCompleteType(type);
9759
9760 const clang::RecordType *record_type =
9761 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9762 const clang::RecordDecl *record_decl = record_type->getDecl();
9763 const clang::CXXRecordDecl *cxx_record_decl =
9764 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9765
9766 if (cxx_record_decl)
9767 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9768 s->GetIndentLevel());
9769 else
9770 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9771 s->GetIndentLevel());
9772 } break;
9773
9774 default: {
9775 const clang::TagType *tag_type =
9776 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9777 if (tag_type) {
9778 clang::TagDecl *tag_decl = tag_type->getDecl();
9779 if (tag_decl)
9780 tag_decl->print(llvm_ostrm, 0);
9781 } else {
9782 std::string clang_type_name(qual_type.getAsString());
9783 if (!clang_type_name.empty())
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009784 s->PutCString(clang_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009785 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009786 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00009787 }
9788
Kate Stoneb9c1b512016-09-06 20:57:50 +00009789 if (buf.size() > 0) {
9790 s->Write(buf.data(), buf.size());
Greg Clayton8b4edba2015-08-14 20:02:05 +00009791 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009792 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00009793}
9794
Kate Stoneb9c1b512016-09-06 20:57:50 +00009795void ClangASTContext::DumpTypeName(const CompilerType &type) {
9796 if (ClangUtil::IsClangType(type)) {
9797 clang::QualType qual_type(
9798 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9799
9800 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9801 switch (type_class) {
9802 case clang::Type::Record: {
9803 const clang::CXXRecordDecl *cxx_record_decl =
9804 qual_type->getAsCXXRecordDecl();
9805 if (cxx_record_decl)
9806 printf("class %s", cxx_record_decl->getName().str().c_str());
9807 } break;
9808
9809 case clang::Type::Enum: {
9810 clang::EnumDecl *enum_decl =
9811 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9812 if (enum_decl) {
9813 printf("enum %s", enum_decl->getName().str().c_str());
9814 }
9815 } break;
9816
9817 case clang::Type::ObjCObject:
9818 case clang::Type::ObjCInterface: {
9819 const clang::ObjCObjectType *objc_class_type =
9820 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9821 if (objc_class_type) {
9822 clang::ObjCInterfaceDecl *class_interface_decl =
9823 objc_class_type->getInterface();
Adrian Prantl05097242018-04-30 16:49:04 +00009824 // We currently can't complete objective C types through the newly
9825 // added ASTContext because it only supports TagDecl objects right
9826 // now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00009827 if (class_interface_decl)
9828 printf("@class %s", class_interface_decl->getName().str().c_str());
9829 }
9830 } break;
9831
9832 case clang::Type::Typedef:
9833 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9834 ->getDecl()
9835 ->getName()
9836 .str()
9837 .c_str());
9838 break;
9839
9840 case clang::Type::Auto:
9841 printf("auto ");
9842 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9843 llvm::cast<clang::AutoType>(qual_type)
9844 ->getDeducedType()
9845 .getAsOpaquePtr()));
9846
9847 case clang::Type::Elaborated:
9848 printf("elaborated ");
9849 return DumpTypeName(CompilerType(
9850 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9851 ->getNamedType()
9852 .getAsOpaquePtr()));
9853
9854 case clang::Type::Paren:
9855 printf("paren ");
9856 return DumpTypeName(CompilerType(
9857 type.GetTypeSystem(),
9858 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9859
9860 default:
9861 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9862 break;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009863 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009864 }
Greg Clayton6dc8d582015-08-18 22:32:36 +00009865}
9866
Kate Stoneb9c1b512016-09-06 20:57:50 +00009867clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9868 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9869 const char *parent_name, int tag_decl_kind,
9870 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9871 if (template_param_infos.IsValid()) {
9872 std::string template_basename(parent_name);
9873 template_basename.erase(template_basename.find('<'));
9874
9875 return CreateClassTemplateDecl(decl_ctx, access_type,
9876 template_basename.c_str(), tag_decl_kind,
9877 template_param_infos);
9878 }
Konrad Kleine248a1302019-05-23 11:14:47 +00009879 return nullptr;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009880}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009881
Kate Stoneb9c1b512016-09-06 20:57:50 +00009882void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9883 ClangASTContext *ast = (ClangASTContext *)baton;
9884 SymbolFile *sym_file = ast->GetSymbolFile();
9885 if (sym_file) {
9886 CompilerType clang_type = GetTypeForDecl(decl);
9887 if (clang_type)
9888 sym_file->CompleteType(clang_type);
9889 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00009890}
9891
Kate Stoneb9c1b512016-09-06 20:57:50 +00009892void ClangASTContext::CompleteObjCInterfaceDecl(
9893 void *baton, clang::ObjCInterfaceDecl *decl) {
9894 ClangASTContext *ast = (ClangASTContext *)baton;
9895 SymbolFile *sym_file = ast->GetSymbolFile();
9896 if (sym_file) {
9897 CompilerType clang_type = GetTypeForDecl(decl);
9898 if (clang_type)
9899 sym_file->CompleteType(clang_type);
9900 }
Zachary Turner42dff792016-04-15 00:21:26 +00009901}
Greg Clayton261ac3f2015-08-28 01:01:03 +00009902
Kate Stoneb9c1b512016-09-06 20:57:50 +00009903DWARFASTParser *ClangASTContext::GetDWARFParser() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009904 if (!m_dwarf_ast_parser_up)
9905 m_dwarf_ast_parser_up.reset(new DWARFASTParserClang(*this));
9906 return m_dwarf_ast_parser_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00009907}
9908
9909PDBASTParser *ClangASTContext::GetPDBParser() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009910 if (!m_pdb_ast_parser_up)
9911 m_pdb_ast_parser_up.reset(new PDBASTParser(*this));
9912 return m_pdb_ast_parser_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00009913}
9914
9915bool ClangASTContext::LayoutRecordType(
9916 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9917 uint64_t &alignment,
9918 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9919 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9920 &base_offsets,
9921 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9922 &vbase_offsets) {
9923 ClangASTContext *ast = (ClangASTContext *)baton;
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00009924 lldb_private::ClangASTImporter *importer = nullptr;
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009925 if (ast->m_dwarf_ast_parser_up)
9926 importer = &ast->m_dwarf_ast_parser_up->GetClangASTImporter();
9927 if (!importer && ast->m_pdb_ast_parser_up)
9928 importer = &ast->m_pdb_ast_parser_up->GetClangASTImporter();
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00009929 if (!importer)
9930 return false;
9931
9932 return importer->LayoutRecordType(record_decl, bit_size, alignment,
9933 field_offsets, base_offsets, vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009934}
9935
Paul Hermand628cbb2015-09-15 23:44:17 +00009936// CompilerDecl override functions
Paul Hermand628cbb2015-09-15 23:44:17 +00009937
Kate Stoneb9c1b512016-09-06 20:57:50 +00009938ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9939 if (opaque_decl) {
9940 clang::NamedDecl *nd =
9941 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9942 if (nd != nullptr)
9943 return ConstString(nd->getDeclName().getAsString());
9944 }
9945 return ConstString();
Paul Hermand628cbb2015-09-15 23:44:17 +00009946}
9947
Kate Stoneb9c1b512016-09-06 20:57:50 +00009948ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9949 if (opaque_decl) {
9950 clang::NamedDecl *nd =
9951 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9952 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9953 clang::MangleContext *mc = getMangleContext();
9954 if (mc && mc->shouldMangleCXXName(nd)) {
9955 llvm::SmallVector<char, 1024> buf;
9956 llvm::raw_svector_ostream llvm_ostrm(buf);
9957 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9958 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9959 Ctor_Complete, llvm_ostrm);
9960 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9961 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9962 Dtor_Complete, llvm_ostrm);
9963 } else {
9964 mc->mangleName(nd, llvm_ostrm);
Greg Claytonfe689042015-11-10 17:47:04 +00009965 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009966 if (buf.size() > 0)
9967 return ConstString(buf.data(), buf.size());
9968 }
Greg Claytonfe689042015-11-10 17:47:04 +00009969 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009970 }
9971 return ConstString();
Greg Claytonfe689042015-11-10 17:47:04 +00009972}
9973
Kate Stoneb9c1b512016-09-06 20:57:50 +00009974CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9975 if (opaque_decl)
9976 return CompilerDeclContext(this,
9977 ((clang::Decl *)opaque_decl)->getDeclContext());
9978 else
9979 return CompilerDeclContext();
Greg Claytonfe689042015-11-10 17:47:04 +00009980}
9981
Kate Stoneb9c1b512016-09-06 20:57:50 +00009982CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9983 if (clang::FunctionDecl *func_decl =
9984 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9985 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9986 if (clang::ObjCMethodDecl *objc_method =
9987 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9988 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9989 else
Greg Claytonfe689042015-11-10 17:47:04 +00009990 return CompilerType();
9991}
9992
Kate Stoneb9c1b512016-09-06 20:57:50 +00009993size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9994 if (clang::FunctionDecl *func_decl =
9995 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9996 return func_decl->param_size();
9997 if (clang::ObjCMethodDecl *objc_method =
9998 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9999 return objc_method->param_size();
10000 else
10001 return 0;
10002}
10003
10004CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
10005 size_t idx) {
10006 if (clang::FunctionDecl *func_decl =
10007 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
10008 if (idx < func_decl->param_size()) {
10009 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
10010 if (var_decl)
10011 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
10012 }
10013 } else if (clang::ObjCMethodDecl *objc_method =
10014 llvm::dyn_cast<clang::ObjCMethodDecl>(
10015 (clang::Decl *)opaque_decl)) {
10016 if (idx < objc_method->param_size())
10017 return CompilerType(
10018 this,
10019 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
10020 }
10021 return CompilerType();
10022}
10023
Greg Clayton99558cc42015-08-24 23:46:31 +000010024// CompilerDeclContext functions
Greg Clayton99558cc42015-08-24 23:46:31 +000010025
Kate Stoneb9c1b512016-09-06 20:57:50 +000010026std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
10027 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
10028 std::vector<CompilerDecl> found_decls;
10029 if (opaque_decl_ctx) {
10030 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
10031 std::set<DeclContext *> searched;
10032 std::multimap<DeclContext *, DeclContext *> search_queue;
10033 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +000010034
Kate Stoneb9c1b512016-09-06 20:57:50 +000010035 for (clang::DeclContext *decl_context = root_decl_ctx;
10036 decl_context != nullptr && found_decls.empty();
10037 decl_context = decl_context->getParent()) {
10038 search_queue.insert(std::make_pair(decl_context, decl_context));
Paul Hermand628cbb2015-09-15 23:44:17 +000010039
Kate Stoneb9c1b512016-09-06 20:57:50 +000010040 for (auto it = search_queue.find(decl_context); it != search_queue.end();
10041 it++) {
10042 if (!searched.insert(it->second).second)
10043 continue;
10044 symbol_file->ParseDeclsForContext(
10045 CompilerDeclContext(this, it->second));
Paul Hermanea188fc2015-09-16 18:48:30 +000010046
Kate Stoneb9c1b512016-09-06 20:57:50 +000010047 for (clang::Decl *child : it->second->decls()) {
10048 if (clang::UsingDirectiveDecl *ud =
10049 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10050 if (ignore_using_decls)
10051 continue;
10052 clang::DeclContext *from = ud->getCommonAncestor();
10053 if (searched.find(ud->getNominatedNamespace()) == searched.end())
10054 search_queue.insert(
10055 std::make_pair(from, ud->getNominatedNamespace()));
10056 } else if (clang::UsingDecl *ud =
10057 llvm::dyn_cast<clang::UsingDecl>(child)) {
10058 if (ignore_using_decls)
10059 continue;
10060 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10061 clang::Decl *target = usd->getTargetDecl();
10062 if (clang::NamedDecl *nd =
10063 llvm::dyn_cast<clang::NamedDecl>(target)) {
10064 IdentifierInfo *ii = nd->getIdentifier();
10065 if (ii != nullptr &&
10066 ii->getName().equals(name.AsCString(nullptr)))
10067 found_decls.push_back(CompilerDecl(this, nd));
10068 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010069 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010070 } else if (clang::NamedDecl *nd =
10071 llvm::dyn_cast<clang::NamedDecl>(child)) {
10072 IdentifierInfo *ii = nd->getIdentifier();
10073 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
10074 found_decls.push_back(CompilerDecl(this, nd));
10075 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010076 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010077 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010078 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010079 }
10080 return found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +000010081}
10082
Dawn Perchikb5925782015-12-12 19:31:41 +000010083// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010084// and return the number of levels it took to find it, or
Adrian Prantl05097242018-04-30 16:49:04 +000010085// LLDB_INVALID_DECL_LEVEL if not found. If the decl was imported via a using
10086// declaration, its name and/or type, if set, will be used to check that the
10087// decl found in the scope is a match.
Dawn Perchikb5925782015-12-12 19:31:41 +000010088//
Kate Stoneb9c1b512016-09-06 20:57:50 +000010089// The optional name is required by languages (like C++) to handle using
Adrian Prantl05097242018-04-30 16:49:04 +000010090// declarations like:
Dawn Perchikb5925782015-12-12 19:31:41 +000010091//
10092// void poo();
10093// namespace ns {
10094// void foo();
10095// void goo();
10096// }
10097// void bar() {
10098// using ns::foo;
10099// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
10100// // LLDB_INVALID_DECL_LEVEL for 'goo'.
10101// }
10102//
10103// The optional type is useful in the case that there's a specific overload
10104// that we're looking for that might otherwise be shadowed, like:
10105//
10106// void foo(int);
10107// namespace ns {
10108// void foo();
10109// }
10110// void bar() {
10111// using ns::foo;
10112// // CountDeclLevels returns 0 for { 'foo', void() },
10113// // 1 for { 'foo', void(int) }, and
10114// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
10115// }
10116//
10117// NOTE: Because file statics are at the TranslationUnit along with globals, a
Kate Stoneb9c1b512016-09-06 20:57:50 +000010118// function at file scope will return the same level as a function at global
Adrian Prantl05097242018-04-30 16:49:04 +000010119// scope. Ideally we'd like to treat the file scope as an additional scope just
10120// below the global scope. More work needs to be done to recognise that, if
10121// the decl we're trying to look up is static, we should compare its source
10122// file with that of the current scope and return a lower number for it.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010123uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
10124 clang::DeclContext *child_decl_ctx,
10125 ConstString *child_name,
10126 CompilerType *child_type) {
10127 if (frame_decl_ctx) {
10128 std::set<DeclContext *> searched;
10129 std::multimap<DeclContext *, DeclContext *> search_queue;
10130 SymbolFile *symbol_file = GetSymbolFile();
Dawn Perchikb5925782015-12-12 19:31:41 +000010131
Kate Stoneb9c1b512016-09-06 20:57:50 +000010132 // Get the lookup scope for the decl we're trying to find.
10133 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
Dawn Perchikb5925782015-12-12 19:31:41 +000010134
Kate Stoneb9c1b512016-09-06 20:57:50 +000010135 // Look for it in our scope's decl context and its parents.
10136 uint32_t level = 0;
10137 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
10138 decl_ctx = decl_ctx->getParent()) {
10139 if (!decl_ctx->isLookupContext())
10140 continue;
10141 if (decl_ctx == parent_decl_ctx)
10142 // Found it!
10143 return level;
10144 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
10145 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
10146 it++) {
10147 if (searched.find(it->second) != searched.end())
10148 continue;
10149
10150 // Currently DWARF has one shared translation unit for all Decls at top
Adrian Prantl05097242018-04-30 16:49:04 +000010151 // level, so this would erroneously find using statements anywhere. So
10152 // don't look at the top-level translation unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010153 // TODO fix this and add a testcase that depends on it.
10154
10155 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
10156 continue;
10157
10158 searched.insert(it->second);
10159 symbol_file->ParseDeclsForContext(
10160 CompilerDeclContext(this, it->second));
10161
10162 for (clang::Decl *child : it->second->decls()) {
10163 if (clang::UsingDirectiveDecl *ud =
10164 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10165 clang::DeclContext *ns = ud->getNominatedNamespace();
10166 if (ns == parent_decl_ctx)
10167 // Found it!
10168 return level;
10169 clang::DeclContext *from = ud->getCommonAncestor();
10170 if (searched.find(ns) == searched.end())
10171 search_queue.insert(std::make_pair(from, ns));
10172 } else if (child_name) {
10173 if (clang::UsingDecl *ud =
10174 llvm::dyn_cast<clang::UsingDecl>(child)) {
10175 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10176 clang::Decl *target = usd->getTargetDecl();
10177 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
10178 if (!nd)
10179 continue;
10180 // Check names.
10181 IdentifierInfo *ii = nd->getIdentifier();
10182 if (ii == nullptr ||
10183 !ii->getName().equals(child_name->AsCString(nullptr)))
10184 continue;
10185 // Check types, if one was provided.
10186 if (child_type) {
10187 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
10188 if (!AreTypesSame(clang_type, *child_type,
10189 /*ignore_qualifiers=*/true))
10190 continue;
10191 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010192 // Found it!
10193 return level;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010194 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010195 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010196 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010197 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010198 }
10199 ++level;
Dawn Perchikb5925782015-12-12 19:31:41 +000010200 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010201 }
10202 return LLDB_INVALID_DECL_LEVEL;
Dawn Perchikb5925782015-12-12 19:31:41 +000010203}
10204
Kate Stoneb9c1b512016-09-06 20:57:50 +000010205bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
10206 if (opaque_decl_ctx)
10207 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
10208 else
Greg Clayton99558cc42015-08-24 23:46:31 +000010209 return false;
10210}
10211
Kate Stoneb9c1b512016-09-06 20:57:50 +000010212ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
10213 if (opaque_decl_ctx) {
10214 clang::NamedDecl *named_decl =
10215 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10216 if (named_decl)
10217 return ConstString(named_decl->getName());
10218 }
10219 return ConstString();
Greg Clayton99558cc42015-08-24 23:46:31 +000010220}
10221
Kate Stoneb9c1b512016-09-06 20:57:50 +000010222ConstString
10223ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
10224 if (opaque_decl_ctx) {
10225 clang::NamedDecl *named_decl =
10226 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10227 if (named_decl)
10228 return ConstString(
10229 llvm::StringRef(named_decl->getQualifiedNameAsString()));
10230 }
10231 return ConstString();
10232}
10233
10234bool ClangASTContext::DeclContextIsClassMethod(
10235 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
10236 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
10237 if (opaque_decl_ctx) {
10238 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10239 if (ObjCMethodDecl *objc_method =
10240 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
10241 if (is_instance_method_ptr)
10242 *is_instance_method_ptr = objc_method->isInstanceMethod();
10243 if (language_ptr)
10244 *language_ptr = eLanguageTypeObjC;
10245 if (language_object_name_ptr)
10246 language_object_name_ptr->SetCString("self");
10247 return true;
10248 } else if (CXXMethodDecl *cxx_method =
10249 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
10250 if (is_instance_method_ptr)
10251 *is_instance_method_ptr = cxx_method->isInstance();
10252 if (language_ptr)
10253 *language_ptr = eLanguageTypeC_plus_plus;
10254 if (language_object_name_ptr)
10255 language_object_name_ptr->SetCString("this");
10256 return true;
10257 } else if (clang::FunctionDecl *function_decl =
10258 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
10259 ClangASTMetadata *metadata =
10260 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
10261 if (metadata && metadata->HasObjectPtr()) {
10262 if (is_instance_method_ptr)
10263 *is_instance_method_ptr = true;
10264 if (language_ptr)
10265 *language_ptr = eLanguageTypeObjC;
10266 if (language_object_name_ptr)
10267 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
10268 return true;
10269 }
10270 }
10271 }
10272 return false;
10273}
10274
Raphael Isemanna9469972019-03-12 07:45:04 +000010275bool ClangASTContext::DeclContextIsContainedInLookup(
10276 void *opaque_decl_ctx, void *other_opaque_decl_ctx) {
10277 auto *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10278 auto *other = (clang::DeclContext *)other_opaque_decl_ctx;
10279
10280 do {
10281 // A decl context always includes its own contents in its lookup.
10282 if (decl_ctx == other)
10283 return true;
10284
10285 // If we have an inline namespace, then the lookup of the parent context
10286 // also includes the inline namespace contents.
10287 } while (other->isInlineNamespace() && (other = other->getParent()));
10288
10289 return false;
10290}
10291
Kate Stoneb9c1b512016-09-06 20:57:50 +000010292clang::DeclContext *
10293ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
10294 if (dc.IsClang())
10295 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
10296 return nullptr;
10297}
Greg Clayton99558cc42015-08-24 23:46:31 +000010298
10299ObjCMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010300ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
10301 if (dc.IsClang())
10302 return llvm::dyn_cast<clang::ObjCMethodDecl>(
10303 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10304 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010305}
10306
10307CXXMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010308ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10309 if (dc.IsClang())
10310 return llvm::dyn_cast<clang::CXXMethodDecl>(
10311 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10312 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010313}
10314
10315clang::FunctionDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010316ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10317 if (dc.IsClang())
10318 return llvm::dyn_cast<clang::FunctionDecl>(
10319 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10320 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010321}
10322
10323clang::NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010324ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10325 if (dc.IsClang())
10326 return llvm::dyn_cast<clang::NamespaceDecl>(
10327 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10328 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010329}
10330
10331ClangASTMetadata *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010332ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10333 const void *object) {
10334 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10335 if (ast)
10336 return ClangASTContext::GetMetadata(ast, object);
10337 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010338}
10339
10340clang::ASTContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010341ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10342 ClangASTContext *ast =
10343 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10344 if (ast)
10345 return ast->getASTContext();
10346 return nullptr;
10347}
10348
10349ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10350 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10351 m_target_wp(target.shared_from_this()),
10352 m_persistent_variables(new ClangPersistentVariables) {}
10353
10354UserExpression *ClangASTContextForExpressions::GetUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010355 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010356 Expression::ResultType desired_type,
Aleksandr Urakov40624a02019-02-05 09:14:36 +000010357 const EvaluateExpressionOptions &options,
10358 ValueObject *ctx_obj) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000010359 TargetSP target_sp = m_target_wp.lock();
10360 if (!target_sp)
Greg Clayton99558cc42015-08-24 23:46:31 +000010361 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010362
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010363 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
Aleksandr Urakov40624a02019-02-05 09:14:36 +000010364 desired_type, options, ctx_obj);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010365}
10366
Kate Stoneb9c1b512016-09-06 20:57:50 +000010367FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10368 const CompilerType &return_type, const Address &function_address,
10369 const ValueList &arg_value_list, const char *name) {
10370 TargetSP target_sp = m_target_wp.lock();
10371 if (!target_sp)
10372 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010373
Kate Stoneb9c1b512016-09-06 20:57:50 +000010374 Process *process = target_sp->GetProcessSP().get();
10375 if (!process)
10376 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010377
Kate Stoneb9c1b512016-09-06 20:57:50 +000010378 return new ClangFunctionCaller(*process, return_type, function_address,
10379 arg_value_list, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010380}
10381
10382UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010383ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10384 const char *name) {
10385 TargetSP target_sp = m_target_wp.lock();
10386 if (!target_sp)
10387 return nullptr;
10388
10389 return new ClangUtilityFunction(*target_sp.get(), text, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010390}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010391
10392PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010393ClangASTContextForExpressions::GetPersistentExpressionState() {
10394 return m_persistent_variables.get();
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010395}
Sean Callanan68e44232017-09-28 20:20:25 +000010396
10397clang::ExternalASTMerger &
10398ClangASTContextForExpressions::GetMergerUnchecked() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +000010399 lldbassert(m_scratch_ast_source_up != nullptr);
10400 return m_scratch_ast_source_up->GetMergerUnchecked();
Sean Callanan68e44232017-09-28 20:20:25 +000010401}