blob: f85c5d2b9e05dc1b70ce95e806b946dc4a2a7c7a [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 Ingham151c0322015-09-15 21:13:50 +000089#include "lldb/Target/Process.h"
90#include "lldb/Target/Target.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000091#include "lldb/Utility/DataExtractor.h"
Sean Callananc530ba92016-05-02 21:15:31 +000092#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000093#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000094#include "lldb/Utility/RegularExpression.h"
Pavel Labathd821c992018-08-07 11:07:21 +000095#include "lldb/Utility/Scalar.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000096
Alex Langfordb57017102019-07-15 22:56:12 +000097#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
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 {
Raphael Isemannc4944812019-07-04 19:49:31 +00001957 assert(false && "GetUniqueNamespaceDeclaration called with no name and "
1958 "no namespace as decl_ctx");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001959 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001960 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001961 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001962#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001963 VerifyDecl(namespace_decl);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001964#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001965 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001966}
1967
Kate Stoneb9c1b512016-09-06 20:57:50 +00001968NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
Raphael Isemanna9469972019-03-12 07:45:04 +00001969 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx,
1970 bool is_inline) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001971 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1972 if (ast_ctx == nullptr)
1973 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +00001974
Raphael Isemanna9469972019-03-12 07:45:04 +00001975 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx, is_inline);
Siva Chandra03ff5c82016-02-05 19:10:04 +00001976}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001977
Paul Hermand628cbb2015-09-15 23:44:17 +00001978clang::BlockDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001979ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1980 if (ctx != nullptr) {
1981 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1982 clang::SourceLocation());
1983 ctx->addDecl(decl);
1984 return decl;
1985 }
1986 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001987}
1988
Kate Stoneb9c1b512016-09-06 20:57:50 +00001989clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1990 clang::DeclContext *right,
1991 clang::DeclContext *root) {
1992 if (root == nullptr)
Paul Hermanea188fc2015-09-16 18:48:30 +00001993 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001994
1995 std::set<clang::DeclContext *> path_left;
1996 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1997 path_left.insert(d);
1998
1999 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
2000 if (path_left.find(d) != path_left.end())
2001 return d;
2002
2003 return nullptr;
Paul Hermanea188fc2015-09-16 18:48:30 +00002004}
2005
Kate Stoneb9c1b512016-09-06 20:57:50 +00002006clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
2007 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
2008 if (decl_ctx != nullptr && ns_decl != nullptr) {
2009 clang::TranslationUnitDecl *translation_unit =
2010 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
2011 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
2012 *getASTContext(), decl_ctx, clang::SourceLocation(),
2013 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
2014 clang::SourceLocation(), ns_decl,
2015 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
2016 decl_ctx->addDecl(using_decl);
2017 return using_decl;
2018 }
2019 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002020}
2021
2022clang::UsingDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00002023ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
2024 clang::NamedDecl *target) {
2025 if (current_decl_ctx != nullptr && target != nullptr) {
2026 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
2027 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
2028 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
2029 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
2030 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
2031 target);
2032 using_decl->addShadowDecl(shadow_decl);
2033 current_decl_ctx->addDecl(using_decl);
2034 return using_decl;
2035 }
2036 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002037}
2038
Kate Stoneb9c1b512016-09-06 20:57:50 +00002039clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
2040 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
2041 if (decl_context != nullptr) {
2042 clang::VarDecl *var_decl = clang::VarDecl::Create(
2043 *getASTContext(), decl_context, clang::SourceLocation(),
2044 clang::SourceLocation(),
2045 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
2046 nullptr, clang::SC_None);
2047 var_decl->setAccess(clang::AS_public);
2048 decl_context->addDecl(var_decl);
2049 return var_decl;
2050 }
2051 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002052}
2053
Zachary Turner9d8a97e2016-04-01 23:20:35 +00002054lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00002055ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
2056 lldb::BasicType basic_type) {
2057 switch (basic_type) {
2058 case eBasicTypeVoid:
2059 return ast->VoidTy.getAsOpaquePtr();
2060 case eBasicTypeChar:
2061 return ast->CharTy.getAsOpaquePtr();
2062 case eBasicTypeSignedChar:
2063 return ast->SignedCharTy.getAsOpaquePtr();
2064 case eBasicTypeUnsignedChar:
2065 return ast->UnsignedCharTy.getAsOpaquePtr();
2066 case eBasicTypeWChar:
2067 return ast->getWCharType().getAsOpaquePtr();
2068 case eBasicTypeSignedWChar:
2069 return ast->getSignedWCharType().getAsOpaquePtr();
2070 case eBasicTypeUnsignedWChar:
2071 return ast->getUnsignedWCharType().getAsOpaquePtr();
2072 case eBasicTypeChar16:
2073 return ast->Char16Ty.getAsOpaquePtr();
2074 case eBasicTypeChar32:
2075 return ast->Char32Ty.getAsOpaquePtr();
2076 case eBasicTypeShort:
2077 return ast->ShortTy.getAsOpaquePtr();
2078 case eBasicTypeUnsignedShort:
2079 return ast->UnsignedShortTy.getAsOpaquePtr();
2080 case eBasicTypeInt:
2081 return ast->IntTy.getAsOpaquePtr();
2082 case eBasicTypeUnsignedInt:
2083 return ast->UnsignedIntTy.getAsOpaquePtr();
2084 case eBasicTypeLong:
2085 return ast->LongTy.getAsOpaquePtr();
2086 case eBasicTypeUnsignedLong:
2087 return ast->UnsignedLongTy.getAsOpaquePtr();
2088 case eBasicTypeLongLong:
2089 return ast->LongLongTy.getAsOpaquePtr();
2090 case eBasicTypeUnsignedLongLong:
2091 return ast->UnsignedLongLongTy.getAsOpaquePtr();
2092 case eBasicTypeInt128:
2093 return ast->Int128Ty.getAsOpaquePtr();
2094 case eBasicTypeUnsignedInt128:
2095 return ast->UnsignedInt128Ty.getAsOpaquePtr();
2096 case eBasicTypeBool:
2097 return ast->BoolTy.getAsOpaquePtr();
2098 case eBasicTypeHalf:
2099 return ast->HalfTy.getAsOpaquePtr();
2100 case eBasicTypeFloat:
2101 return ast->FloatTy.getAsOpaquePtr();
2102 case eBasicTypeDouble:
2103 return ast->DoubleTy.getAsOpaquePtr();
2104 case eBasicTypeLongDouble:
2105 return ast->LongDoubleTy.getAsOpaquePtr();
2106 case eBasicTypeFloatComplex:
2107 return ast->FloatComplexTy.getAsOpaquePtr();
2108 case eBasicTypeDoubleComplex:
2109 return ast->DoubleComplexTy.getAsOpaquePtr();
2110 case eBasicTypeLongDoubleComplex:
2111 return ast->LongDoubleComplexTy.getAsOpaquePtr();
2112 case eBasicTypeObjCID:
2113 return ast->getObjCIdType().getAsOpaquePtr();
2114 case eBasicTypeObjCClass:
2115 return ast->getObjCClassType().getAsOpaquePtr();
2116 case eBasicTypeObjCSel:
2117 return ast->getObjCSelType().getAsOpaquePtr();
2118 case eBasicTypeNullPtr:
2119 return ast->NullPtrTy.getAsOpaquePtr();
2120 default:
2121 return nullptr;
2122 }
Zachary Turner9d8a97e2016-04-01 23:20:35 +00002123}
2124
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002125#pragma mark Function Types
2126
Pavel Labath1ac2b202016-08-15 14:32:32 +00002127clang::DeclarationName
Kate Stoneb9c1b512016-09-06 20:57:50 +00002128ClangASTContext::GetDeclarationName(const char *name,
2129 const CompilerType &function_clang_type) {
2130 if (!name || !name[0])
2131 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002132
Kate Stoneb9c1b512016-09-06 20:57:50 +00002133 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
2134 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
2135 return DeclarationName(&getASTContext()->Idents.get(
2136 name)); // Not operator, but a regular function.
Pavel Labath1ac2b202016-08-15 14:32:32 +00002137
Adrian Prantl05097242018-04-30 16:49:04 +00002138 // Check the number of operator parameters. Sometimes we have seen bad DWARF
2139 // that doesn't correctly describe operators and if we try to create a method
2140 // and add it to the class, clang will assert and crash, so we need to make
2141 // sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002142 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
2143 const clang::FunctionProtoType *function_type =
2144 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
2145 if (function_type == nullptr)
2146 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002147
Kate Stoneb9c1b512016-09-06 20:57:50 +00002148 const bool is_method = false;
2149 const unsigned int num_params = function_type->getNumParams();
2150 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
2151 is_method, op_kind, num_params))
2152 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002153
Kate Stoneb9c1b512016-09-06 20:57:50 +00002154 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
Pavel Labath1ac2b202016-08-15 14:32:32 +00002155}
2156
Kate Stoneb9c1b512016-09-06 20:57:50 +00002157FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
2158 DeclContext *decl_ctx, const char *name,
2159 const CompilerType &function_clang_type, int storage, bool is_inline) {
2160 FunctionDecl *func_decl = nullptr;
2161 ASTContext *ast = getASTContext();
2162 if (decl_ctx == nullptr)
2163 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002164
Kate Stoneb9c1b512016-09-06 20:57:50 +00002165 const bool hasWrittenPrototype = true;
2166 const bool isConstexprSpecified = false;
Greg Clayton0d551042013-06-28 21:08:47 +00002167
Kate Stoneb9c1b512016-09-06 20:57:50 +00002168 clang::DeclarationName declarationName =
2169 GetDeclarationName(name, function_clang_type);
2170 func_decl = FunctionDecl::Create(
2171 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
2172 ClangUtil::GetQualType(function_clang_type), nullptr,
2173 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002174 isConstexprSpecified ? CSK_constexpr : CSK_unspecified);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002175 if (func_decl)
2176 decl_ctx->addDecl(func_decl);
2177
Sean Callanan5e9e1992011-10-26 01:06:27 +00002178#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00002179 VerifyDecl(func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002180#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002181
2182 return func_decl;
2183}
2184
2185CompilerType ClangASTContext::CreateFunctionType(
2186 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002187 unsigned num_args, bool is_variadic, unsigned type_quals,
2188 clang::CallingConv cc) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002189 if (ast == nullptr)
2190 return CompilerType(); // invalid AST
2191
2192 if (!result_type || !ClangUtil::IsClangType(result_type))
2193 return CompilerType(); // invalid return type
2194
2195 std::vector<QualType> qual_type_args;
2196 if (num_args > 0 && args == nullptr)
2197 return CompilerType(); // invalid argument array passed in
2198
2199 // Verify that all arguments are valid and the right type
2200 for (unsigned i = 0; i < num_args; ++i) {
2201 if (args[i]) {
2202 // Make sure we have a clang type in args[i] and not a type from another
2203 // language whose name might match
2204 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2205 lldbassert(is_clang_type);
2206 if (is_clang_type)
2207 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2208 else
2209 return CompilerType(); // invalid argument type (must be a clang type)
2210 } else
2211 return CompilerType(); // invalid argument type (empty)
2212 }
2213
2214 // TODO: Detect calling convention in DWARF?
2215 FunctionProtoType::ExtProtoInfo proto_info;
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002216 proto_info.ExtInfo = cc;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002217 proto_info.Variadic = is_variadic;
2218 proto_info.ExceptionSpec = EST_None;
Mikael Nilsson8b3bf6c2018-12-13 10:17:26 +00002219 proto_info.TypeQuals = clang::Qualifiers::fromFastMask(type_quals);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002220 proto_info.RefQualifier = RQ_None;
2221
2222 return CompilerType(ast,
2223 ast->getFunctionType(ClangUtil::GetQualType(result_type),
2224 qual_type_args, proto_info));
2225}
2226
2227ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
Zachary Turner6753d2d2018-12-12 17:17:53 +00002228 clang::DeclContext *decl_ctx, const char *name,
2229 const CompilerType &param_type, int storage) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002230 ASTContext *ast = getASTContext();
2231 assert(ast != nullptr);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00002232 auto *decl =
2233 ParmVarDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(),
2234 name && name[0] ? &ast->Idents.get(name) : nullptr,
2235 ClangUtil::GetQualType(param_type), nullptr,
2236 (clang::StorageClass)storage, nullptr);
2237 decl_ctx->addDecl(decl);
2238 return decl;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002239}
2240
2241void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2242 ParmVarDecl **params,
2243 unsigned num_params) {
2244 if (function_decl)
2245 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002246}
2247
Greg Claytona1e5dc82015-08-11 22:53:00 +00002248CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002249ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00002250 QualType block_type = m_ast_up->getBlockPointerType(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002251 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
Greg Claytonceeb5212016-05-26 22:33:25 +00002252
Kate Stoneb9c1b512016-09-06 20:57:50 +00002253 return CompilerType(this, block_type.getAsOpaquePtr());
Sean Callananc530ba92016-05-02 21:15:31 +00002254}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002255
2256#pragma mark Array Types
2257
Kate Stoneb9c1b512016-09-06 20:57:50 +00002258CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2259 size_t element_count,
2260 bool is_vector) {
2261 if (element_type.IsValid()) {
2262 ASTContext *ast = getASTContext();
2263 assert(ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002264
Kate Stoneb9c1b512016-09-06 20:57:50 +00002265 if (is_vector) {
2266 return CompilerType(
2267 ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2268 element_count));
2269 } else {
2270
2271 llvm::APInt ap_element_count(64, element_count);
2272 if (element_count == 0) {
2273 return CompilerType(ast, ast->getIncompleteArrayType(
2274 ClangUtil::GetQualType(element_type),
2275 clang::ArrayType::Normal, 0));
2276 } else {
2277 return CompilerType(
2278 ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2279 ap_element_count,
2280 clang::ArrayType::Normal, 0));
2281 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002282 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002283 }
2284 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002285}
2286
Kate Stoneb9c1b512016-09-06 20:57:50 +00002287CompilerType ClangASTContext::CreateStructForIdentifier(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002288 ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002289 const std::initializer_list<std::pair<const char *, CompilerType>>
2290 &type_fields,
2291 bool packed) {
2292 CompilerType type;
2293 if (!type_name.IsEmpty() &&
2294 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2295 .IsValid()) {
Pavel Labathf31c9d22017-01-05 13:18:42 +00002296 lldbassert(0 && "Trying to create a type for an existing name");
Enrico Granata76b08d52014-10-29 23:08:02 +00002297 return type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002298 }
2299
2300 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2301 clang::TTK_Struct, lldb::eLanguageTypeC);
2302 StartTagDeclarationDefinition(type);
2303 for (const auto &field : type_fields)
2304 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2305 0);
2306 if (packed)
2307 SetIsPacked(type);
2308 CompleteTagDeclarationDefinition(type);
2309 return type;
Enrico Granata76b08d52014-10-29 23:08:02 +00002310}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002311
Kate Stoneb9c1b512016-09-06 20:57:50 +00002312CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002313 ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002314 const std::initializer_list<std::pair<const char *, CompilerType>>
2315 &type_fields,
2316 bool packed) {
2317 CompilerType type;
2318 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2319 return type;
Sean Callananc530ba92016-05-02 21:15:31 +00002320
Kate Stoneb9c1b512016-09-06 20:57:50 +00002321 return CreateStructForIdentifier(type_name, type_fields, packed);
Sean Callananc530ba92016-05-02 21:15:31 +00002322}
2323
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002324#pragma mark Enumeration Types
2325
Greg Claytona1e5dc82015-08-11 22:53:00 +00002326CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002327ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2328 const Declaration &decl,
Tamas Berghammer59765832017-11-07 10:39:22 +00002329 const CompilerType &integer_clang_type,
2330 bool is_scoped) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002331 // TODO: Do something intelligent with the Declaration object passed in
2332 // like maybe filling in the SourceLocation with it...
2333 ASTContext *ast = getASTContext();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002334
Kate Stoneb9c1b512016-09-06 20:57:50 +00002335 // TODO: ask about these...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002336 // const bool IsFixed = false;
2337
2338 EnumDecl *enum_decl = EnumDecl::Create(
2339 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2340 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
Tamas Berghammercf6bf4c2017-11-07 13:43:55 +00002341 is_scoped, // IsScoped
2342 is_scoped, // IsScopedUsingClassTag
2343 false); // IsFixed
Kate Stoneb9c1b512016-09-06 20:57:50 +00002344
2345 if (enum_decl) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +00002346 if (decl_ctx)
2347 decl_ctx->addDecl(enum_decl);
2348
Kate Stoneb9c1b512016-09-06 20:57:50 +00002349 // TODO: check if we should be setting the promotion type too?
2350 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2351
2352 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2353
2354 return CompilerType(ast, ast->getTagDeclType(enum_decl));
2355 }
2356 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002357}
2358
Kate Stoneb9c1b512016-09-06 20:57:50 +00002359CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2360 size_t bit_size,
2361 bool is_signed) {
2362 if (ast) {
2363 if (is_signed) {
2364 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
2365 return CompilerType(ast, ast->SignedCharTy);
2366
2367 if (bit_size == ast->getTypeSize(ast->ShortTy))
2368 return CompilerType(ast, ast->ShortTy);
2369
2370 if (bit_size == ast->getTypeSize(ast->IntTy))
2371 return CompilerType(ast, ast->IntTy);
2372
2373 if (bit_size == ast->getTypeSize(ast->LongTy))
2374 return CompilerType(ast, ast->LongTy);
2375
2376 if (bit_size == ast->getTypeSize(ast->LongLongTy))
2377 return CompilerType(ast, ast->LongLongTy);
2378
2379 if (bit_size == ast->getTypeSize(ast->Int128Ty))
2380 return CompilerType(ast, ast->Int128Ty);
2381 } else {
2382 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
2383 return CompilerType(ast, ast->UnsignedCharTy);
2384
2385 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
2386 return CompilerType(ast, ast->UnsignedShortTy);
2387
2388 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
2389 return CompilerType(ast, ast->UnsignedIntTy);
2390
2391 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
2392 return CompilerType(ast, ast->UnsignedLongTy);
2393
2394 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
2395 return CompilerType(ast, ast->UnsignedLongLongTy);
2396
2397 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
2398 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002399 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002400 }
2401 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002402}
2403
Kate Stoneb9c1b512016-09-06 20:57:50 +00002404CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2405 bool is_signed) {
2406 if (ast)
2407 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2408 is_signed);
2409 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002410}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002411
Kate Stoneb9c1b512016-09-06 20:57:50 +00002412void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2413 if (decl_ctx) {
2414 DumpDeclContextHiearchy(decl_ctx->getParent());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002415
Kate Stoneb9c1b512016-09-06 20:57:50 +00002416 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2417 if (named_decl) {
2418 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2419 named_decl->getDeclName().getAsString().c_str());
2420 } else {
2421 printf("%20s\n", decl_ctx->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002422 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002423 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002424}
2425
Kate Stoneb9c1b512016-09-06 20:57:50 +00002426void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2427 if (decl == nullptr)
2428 return;
2429 DumpDeclContextHiearchy(decl->getDeclContext());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002430
Kate Stoneb9c1b512016-09-06 20:57:50 +00002431 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2432 if (record_decl) {
2433 printf("%20s: %s%s\n", decl->getDeclKindName(),
2434 record_decl->getDeclName().getAsString().c_str(),
2435 record_decl->isInjectedClassName() ? " (injected class name)" : "");
Greg Claytone6b36cd2015-12-08 01:02:08 +00002436
Kate Stoneb9c1b512016-09-06 20:57:50 +00002437 } else {
2438 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2439 if (named_decl) {
2440 printf("%20s: %s\n", decl->getDeclKindName(),
2441 named_decl->getDeclName().getAsString().c_str());
2442 } else {
2443 printf("%20s\n", decl->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002444 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002445 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002446}
2447
Kate Stoneb9c1b512016-09-06 20:57:50 +00002448bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2449 clang::Decl *rhs_decl) {
2450 if (lhs_decl && rhs_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002451 // Make sure the decl kinds match first
Kate Stoneb9c1b512016-09-06 20:57:50 +00002452 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2453 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002454
Kate Stoneb9c1b512016-09-06 20:57:50 +00002455 if (lhs_decl_kind == rhs_decl_kind) {
Adrian Prantl05097242018-04-30 16:49:04 +00002456 // Now check that the decl contexts kinds are all equivalent before we
2457 // have to check any names of the decl contexts...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002458 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2459 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2460 if (lhs_decl_ctx && rhs_decl_ctx) {
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00002461 while (true) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002462 if (lhs_decl_ctx && rhs_decl_ctx) {
2463 const clang::Decl::Kind lhs_decl_ctx_kind =
2464 lhs_decl_ctx->getDeclKind();
2465 const clang::Decl::Kind rhs_decl_ctx_kind =
2466 rhs_decl_ctx->getDeclKind();
2467 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2468 lhs_decl_ctx = lhs_decl_ctx->getParent();
2469 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002470
Kate Stoneb9c1b512016-09-06 20:57:50 +00002471 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2472 break;
2473 } else
2474 return false;
2475 } else
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00002476 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002477 }
2478
Kate Stoneb9c1b512016-09-06 20:57:50 +00002479 // Now make sure the name of the decls match
Kate Stoneb9c1b512016-09-06 20:57:50 +00002480 clang::NamedDecl *lhs_named_decl =
2481 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2482 clang::NamedDecl *rhs_named_decl =
2483 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2484 if (lhs_named_decl && rhs_named_decl) {
2485 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2486 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2487 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2488 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2489 return false;
2490 } else
Greg Claytona2721472011-06-25 00:44:06 +00002491 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002492 } else
2493 return false;
Greg Claytona2721472011-06-25 00:44:06 +00002494
Adrian Prantl05097242018-04-30 16:49:04 +00002495 // We know that the decl context kinds all match, so now we need to
2496 // make sure the names match as well
Kate Stoneb9c1b512016-09-06 20:57:50 +00002497 lhs_decl_ctx = lhs_decl->getDeclContext();
2498 rhs_decl_ctx = rhs_decl->getDeclContext();
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00002499 while (true) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002500 switch (lhs_decl_ctx->getDeclKind()) {
2501 case clang::Decl::TranslationUnit:
2502 // We don't care about the translation unit names
2503 return true;
2504 default: {
2505 clang::NamedDecl *lhs_named_decl =
2506 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2507 clang::NamedDecl *rhs_named_decl =
2508 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2509 if (lhs_named_decl && rhs_named_decl) {
2510 clang::DeclarationName lhs_decl_name =
2511 lhs_named_decl->getDeclName();
2512 clang::DeclarationName rhs_decl_name =
2513 rhs_named_decl->getDeclName();
2514 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2515 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2516 return false;
2517 } else
2518 return false;
2519 } else
2520 return false;
2521 } break;
2522 }
2523 lhs_decl_ctx = lhs_decl_ctx->getParent();
2524 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytond8d4a572015-08-11 21:38:15 +00002525 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002526 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002527 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002528 }
2529 return false;
2530}
2531bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2532 clang::Decl *decl) {
2533 if (!decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002534 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002535
Kate Stoneb9c1b512016-09-06 20:57:50 +00002536 ExternalASTSource *ast_source = ast->getExternalSource();
Greg Claytond8d4a572015-08-11 21:38:15 +00002537
Kate Stoneb9c1b512016-09-06 20:57:50 +00002538 if (!ast_source)
Greg Claytond8d4a572015-08-11 21:38:15 +00002539 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002540
2541 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2542 if (tag_decl->isCompleteDefinition())
2543 return true;
2544
2545 if (!tag_decl->hasExternalLexicalStorage())
2546 return false;
2547
2548 ast_source->CompleteType(tag_decl);
2549
2550 return !tag_decl->getTypeForDecl()->isIncompleteType();
2551 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2552 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2553 if (objc_interface_decl->getDefinition())
2554 return true;
2555
2556 if (!objc_interface_decl->hasExternalLexicalStorage())
2557 return false;
2558
2559 ast_source->CompleteType(objc_interface_decl);
2560
2561 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2562 } else {
2563 return false;
2564 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002565}
2566
Kate Stoneb9c1b512016-09-06 20:57:50 +00002567void ClangASTContext::SetMetadataAsUserID(const void *object,
2568 user_id_t user_id) {
2569 ClangASTMetadata meta_data;
2570 meta_data.SetUserID(user_id);
2571 SetMetadata(object, meta_data);
Greg Clayton99558cc42015-08-24 23:46:31 +00002572}
2573
Kate Stoneb9c1b512016-09-06 20:57:50 +00002574void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2575 ClangASTMetadata &metadata) {
2576 ClangExternalASTSourceCommon *external_source =
2577 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2578
2579 if (external_source)
2580 external_source->SetMetadata(object, metadata);
2581}
2582
2583ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2584 const void *object) {
2585 ClangExternalASTSourceCommon *external_source =
2586 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2587
2588 if (external_source && external_source->HasMetadata(object))
2589 return external_source->GetMetadata(object);
2590 else
Greg Claytond8d4a572015-08-11 21:38:15 +00002591 return nullptr;
2592}
2593
Kate Stoneb9c1b512016-09-06 20:57:50 +00002594clang::DeclContext *
2595ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2596 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2597}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002598
Kate Stoneb9c1b512016-09-06 20:57:50 +00002599clang::DeclContext *
2600ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2601 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2602}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002603
Kate Stoneb9c1b512016-09-06 20:57:50 +00002604bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2605 int kind) const {
2606 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2607 if (clang_type) {
2608 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2609 if (tag_type) {
2610 clang::TagDecl *tag_decl =
2611 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2612 if (tag_decl) {
2613 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2614 return true;
2615 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002616 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002617 }
2618 return false;
2619}
2620
2621bool ClangASTContext::SetDefaultAccessForRecordFields(
2622 clang::RecordDecl *record_decl, int default_accessibility,
2623 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2624 if (record_decl) {
2625 uint32_t field_idx;
2626 clang::RecordDecl::field_iterator field, field_end;
2627 for (field = record_decl->field_begin(),
2628 field_end = record_decl->field_end(), field_idx = 0;
2629 field != field_end; ++field, ++field_idx) {
2630 // If no accessibility was assigned, assign the correct one
2631 if (field_idx < num_assigned_accessibilities &&
2632 assigned_accessibilities[field_idx] == clang::AS_none)
2633 field->setAccess((clang::AccessSpecifier)default_accessibility);
2634 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002635 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002636 }
2637 return false;
2638}
2639
2640clang::DeclContext *
2641ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2642 return GetDeclContextForType(ClangUtil::GetQualType(type));
2643}
2644
2645clang::DeclContext *
2646ClangASTContext::GetDeclContextForType(clang::QualType type) {
2647 if (type.isNull())
2648 return nullptr;
2649
2650 clang::QualType qual_type = type.getCanonicalType();
2651 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2652 switch (type_class) {
2653 case clang::Type::ObjCInterface:
2654 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2655 ->getInterface();
2656 case clang::Type::ObjCObjectPointer:
2657 return GetDeclContextForType(
2658 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2659 ->getPointeeType());
2660 case clang::Type::Record:
2661 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2662 case clang::Type::Enum:
2663 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2664 case clang::Type::Typedef:
2665 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2666 ->getDecl()
2667 ->getUnderlyingType());
2668 case clang::Type::Auto:
2669 return GetDeclContextForType(
2670 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2671 case clang::Type::Elaborated:
2672 return GetDeclContextForType(
2673 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2674 case clang::Type::Paren:
2675 return GetDeclContextForType(
2676 llvm::cast<clang::ParenType>(qual_type)->desugar());
2677 default:
2678 break;
2679 }
2680 // No DeclContext in this type...
2681 return nullptr;
2682}
2683
2684static bool GetCompleteQualType(clang::ASTContext *ast,
2685 clang::QualType qual_type,
2686 bool allow_completion = true) {
2687 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2688 switch (type_class) {
2689 case clang::Type::ConstantArray:
2690 case clang::Type::IncompleteArray:
2691 case clang::Type::VariableArray: {
2692 const clang::ArrayType *array_type =
2693 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2694
2695 if (array_type)
2696 return GetCompleteQualType(ast, array_type->getElementType(),
2697 allow_completion);
2698 } break;
2699 case clang::Type::Record: {
2700 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2701 if (cxx_record_decl) {
2702 if (cxx_record_decl->hasExternalLexicalStorage()) {
2703 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2704 const bool fields_loaded =
2705 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2706 if (is_complete && fields_loaded)
2707 return true;
2708
2709 if (!allow_completion)
2710 return false;
2711
2712 // Call the field_begin() accessor to for it to use the external source
2713 // to load the fields...
2714 clang::ExternalASTSource *external_ast_source =
2715 ast->getExternalSource();
2716 if (external_ast_source) {
2717 external_ast_source->CompleteType(cxx_record_decl);
2718 if (cxx_record_decl->isCompleteDefinition()) {
2719 cxx_record_decl->field_begin();
2720 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2721 }
2722 }
2723 }
2724 }
2725 const clang::TagType *tag_type =
2726 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2727 return !tag_type->isIncompleteType();
2728 } break;
2729
2730 case clang::Type::Enum: {
2731 const clang::TagType *tag_type =
2732 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2733 if (tag_type) {
2734 clang::TagDecl *tag_decl = tag_type->getDecl();
2735 if (tag_decl) {
2736 if (tag_decl->getDefinition())
2737 return true;
2738
2739 if (!allow_completion)
2740 return false;
2741
2742 if (tag_decl->hasExternalLexicalStorage()) {
2743 if (ast) {
2744 clang::ExternalASTSource *external_ast_source =
2745 ast->getExternalSource();
2746 if (external_ast_source) {
2747 external_ast_source->CompleteType(tag_decl);
2748 return !tag_type->isIncompleteType();
2749 }
2750 }
2751 }
2752 return false;
2753 }
2754 }
2755
2756 } break;
2757 case clang::Type::ObjCObject:
2758 case clang::Type::ObjCInterface: {
2759 const clang::ObjCObjectType *objc_class_type =
2760 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2761 if (objc_class_type) {
2762 clang::ObjCInterfaceDecl *class_interface_decl =
2763 objc_class_type->getInterface();
2764 // We currently can't complete objective C types through the newly added
Adrian Prantl05097242018-04-30 16:49:04 +00002765 // ASTContext because it only supports TagDecl objects right now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002766 if (class_interface_decl) {
2767 if (class_interface_decl->getDefinition())
2768 return true;
2769
2770 if (!allow_completion)
2771 return false;
2772
2773 if (class_interface_decl->hasExternalLexicalStorage()) {
2774 if (ast) {
2775 clang::ExternalASTSource *external_ast_source =
2776 ast->getExternalSource();
2777 if (external_ast_source) {
2778 external_ast_source->CompleteType(class_interface_decl);
2779 return !objc_class_type->isIncompleteType();
2780 }
2781 }
2782 }
2783 return false;
2784 }
2785 }
2786 } break;
2787
2788 case clang::Type::Typedef:
2789 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2790 ->getDecl()
2791 ->getUnderlyingType(),
2792 allow_completion);
2793
2794 case clang::Type::Auto:
2795 return GetCompleteQualType(
2796 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2797 allow_completion);
2798
2799 case clang::Type::Elaborated:
2800 return GetCompleteQualType(
2801 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2802 allow_completion);
2803
2804 case clang::Type::Paren:
2805 return GetCompleteQualType(
2806 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2807 allow_completion);
2808
2809 case clang::Type::Attributed:
2810 return GetCompleteQualType(
2811 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2812 allow_completion);
2813
2814 default:
2815 break;
2816 }
2817
2818 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00002819}
2820
2821static clang::ObjCIvarDecl::AccessControl
Kate Stoneb9c1b512016-09-06 20:57:50 +00002822ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2823 switch (access) {
2824 case eAccessNone:
Greg Claytond8d4a572015-08-11 21:38:15 +00002825 return clang::ObjCIvarDecl::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002826 case eAccessPublic:
2827 return clang::ObjCIvarDecl::Public;
2828 case eAccessPrivate:
2829 return clang::ObjCIvarDecl::Private;
2830 case eAccessProtected:
2831 return clang::ObjCIvarDecl::Protected;
2832 case eAccessPackage:
2833 return clang::ObjCIvarDecl::Package;
2834 }
2835 return clang::ObjCIvarDecl::None;
Greg Claytond8d4a572015-08-11 21:38:15 +00002836}
2837
Greg Claytond8d4a572015-08-11 21:38:15 +00002838// Tests
Greg Claytond8d4a572015-08-11 21:38:15 +00002839
Kate Stoneb9c1b512016-09-06 20:57:50 +00002840bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2841 clang::QualType qual_type(GetCanonicalQualType(type));
2842
2843 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2844 switch (type_class) {
2845 case clang::Type::IncompleteArray:
2846 case clang::Type::VariableArray:
2847 case clang::Type::ConstantArray:
2848 case clang::Type::ExtVector:
2849 case clang::Type::Vector:
2850 case clang::Type::Record:
2851 case clang::Type::ObjCObject:
2852 case clang::Type::ObjCInterface:
2853 return true;
2854 case clang::Type::Auto:
2855 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2856 ->getDeducedType()
2857 .getAsOpaquePtr());
2858 case clang::Type::Elaborated:
2859 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2860 ->getNamedType()
2861 .getAsOpaquePtr());
2862 case clang::Type::Typedef:
2863 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2864 ->getDecl()
2865 ->getUnderlyingType()
2866 .getAsOpaquePtr());
2867 case clang::Type::Paren:
2868 return IsAggregateType(
2869 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2870 default:
2871 break;
2872 }
2873 // The clang type does have a value
2874 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002875}
2876
Kate Stoneb9c1b512016-09-06 20:57:50 +00002877bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2878 clang::QualType qual_type(GetCanonicalQualType(type));
2879
2880 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2881 switch (type_class) {
2882 case clang::Type::Record: {
2883 if (const clang::RecordType *record_type =
2884 llvm::dyn_cast_or_null<clang::RecordType>(
2885 qual_type.getTypePtrOrNull())) {
2886 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2887 return record_decl->isAnonymousStructOrUnion();
2888 }
Enrico Granata7123e2b2015-11-07 02:06:57 +00002889 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002890 break;
2891 }
2892 case clang::Type::Auto:
2893 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2894 ->getDeducedType()
2895 .getAsOpaquePtr());
2896 case clang::Type::Elaborated:
2897 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2898 ->getNamedType()
2899 .getAsOpaquePtr());
2900 case clang::Type::Typedef:
2901 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2902 ->getDecl()
2903 ->getUnderlyingType()
2904 .getAsOpaquePtr());
2905 case clang::Type::Paren:
2906 return IsAnonymousType(
2907 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2908 default:
2909 break;
2910 }
2911 // The clang type does have a value
2912 return false;
Enrico Granata7123e2b2015-11-07 02:06:57 +00002913}
2914
Kate Stoneb9c1b512016-09-06 20:57:50 +00002915bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2916 CompilerType *element_type_ptr,
2917 uint64_t *size, bool *is_incomplete) {
2918 clang::QualType qual_type(GetCanonicalQualType(type));
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002919
Kate Stoneb9c1b512016-09-06 20:57:50 +00002920 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2921 switch (type_class) {
2922 default:
2923 break;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002924
Kate Stoneb9c1b512016-09-06 20:57:50 +00002925 case clang::Type::ConstantArray:
Greg Claytond8d4a572015-08-11 21:38:15 +00002926 if (element_type_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002927 element_type_ptr->SetCompilerType(
2928 getASTContext(),
2929 llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002930 if (size)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002931 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2932 ->getSize()
2933 .getLimitedValue(ULLONG_MAX);
Greg Claytond8d4a572015-08-11 21:38:15 +00002934 if (is_incomplete)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002935 *is_incomplete = false;
2936 return true;
2937
2938 case clang::Type::IncompleteArray:
2939 if (element_type_ptr)
2940 element_type_ptr->SetCompilerType(
2941 getASTContext(),
2942 llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2943 if (size)
2944 *size = 0;
2945 if (is_incomplete)
2946 *is_incomplete = true;
2947 return true;
2948
2949 case clang::Type::VariableArray:
2950 if (element_type_ptr)
2951 element_type_ptr->SetCompilerType(
2952 getASTContext(),
2953 llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2954 if (size)
2955 *size = 0;
2956 if (is_incomplete)
2957 *is_incomplete = false;
2958 return true;
2959
2960 case clang::Type::DependentSizedArray:
2961 if (element_type_ptr)
2962 element_type_ptr->SetCompilerType(
2963 getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)
2964 ->getElementType());
2965 if (size)
2966 *size = 0;
2967 if (is_incomplete)
2968 *is_incomplete = false;
2969 return true;
2970
2971 case clang::Type::Typedef:
2972 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2973 ->getDecl()
2974 ->getUnderlyingType()
2975 .getAsOpaquePtr(),
2976 element_type_ptr, size, is_incomplete);
2977 case clang::Type::Auto:
2978 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2979 ->getDeducedType()
2980 .getAsOpaquePtr(),
2981 element_type_ptr, size, is_incomplete);
2982 case clang::Type::Elaborated:
2983 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2984 ->getNamedType()
2985 .getAsOpaquePtr(),
2986 element_type_ptr, size, is_incomplete);
2987 case clang::Type::Paren:
2988 return IsArrayType(
2989 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2990 element_type_ptr, size, is_incomplete);
2991 }
2992 if (element_type_ptr)
2993 element_type_ptr->Clear();
2994 if (size)
2995 *size = 0;
2996 if (is_incomplete)
2997 *is_incomplete = false;
2998 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002999}
3000
Kate Stoneb9c1b512016-09-06 20:57:50 +00003001bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
3002 CompilerType *element_type, uint64_t *size) {
3003 clang::QualType qual_type(GetCanonicalQualType(type));
3004
3005 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3006 switch (type_class) {
3007 case clang::Type::Vector: {
3008 const clang::VectorType *vector_type =
3009 qual_type->getAs<clang::VectorType>();
3010 if (vector_type) {
3011 if (size)
3012 *size = vector_type->getNumElements();
3013 if (element_type)
3014 *element_type =
3015 CompilerType(getASTContext(), vector_type->getElementType());
3016 }
3017 return true;
3018 } break;
3019 case clang::Type::ExtVector: {
3020 const clang::ExtVectorType *ext_vector_type =
3021 qual_type->getAs<clang::ExtVectorType>();
3022 if (ext_vector_type) {
3023 if (size)
3024 *size = ext_vector_type->getNumElements();
3025 if (element_type)
3026 *element_type =
3027 CompilerType(getASTContext(), ext_vector_type->getElementType());
3028 }
3029 return true;
3030 }
3031 default:
3032 break;
3033 }
3034 return false;
3035}
3036
3037bool ClangASTContext::IsRuntimeGeneratedType(
3038 lldb::opaque_compiler_type_t type) {
3039 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
3040 ->GetDeclContextForType(GetQualType(type));
3041 if (!decl_ctx)
3042 return false;
3043
3044 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
3045 return false;
3046
3047 clang::ObjCInterfaceDecl *result_iface_decl =
3048 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
3049
3050 ClangASTMetadata *ast_metadata =
3051 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
3052 if (!ast_metadata)
3053 return false;
3054 return (ast_metadata->GetISAPtr() != 0);
3055}
3056
3057bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
3058 return GetQualType(type).getUnqualifiedType()->isCharType();
3059}
3060
3061bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
3062 const bool allow_completion = false;
3063 return GetCompleteQualType(getASTContext(), GetQualType(type),
3064 allow_completion);
3065}
3066
3067bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
3068 return GetQualType(type).isConstQualified();
3069}
3070
3071bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
3072 uint32_t &length) {
3073 CompilerType pointee_or_element_clang_type;
3074 length = 0;
3075 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
3076
3077 if (!pointee_or_element_clang_type.IsValid())
3078 return false;
3079
3080 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
3081 if (pointee_or_element_clang_type.IsCharType()) {
3082 if (type_flags.Test(eTypeIsArray)) {
Adrian Prantl05097242018-04-30 16:49:04 +00003083 // We know the size of the array and it could be a C string since it is
3084 // an array of characters
Kate Stoneb9c1b512016-09-06 20:57:50 +00003085 length = llvm::cast<clang::ConstantArrayType>(
3086 GetCanonicalQualType(type).getTypePtr())
3087 ->getSize()
3088 .getLimitedValue();
3089 }
3090 return true;
3091 }
3092 }
3093 return false;
3094}
3095
3096bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
3097 bool *is_variadic_ptr) {
3098 if (type) {
3099 clang::QualType qual_type(GetCanonicalQualType(type));
3100
3101 if (qual_type->isFunctionType()) {
3102 if (is_variadic_ptr) {
3103 const clang::FunctionProtoType *function_proto_type =
3104 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3105 if (function_proto_type)
3106 *is_variadic_ptr = function_proto_type->isVariadic();
3107 else
3108 *is_variadic_ptr = false;
3109 }
3110 return true;
3111 }
3112
Greg Claytond8d4a572015-08-11 21:38:15 +00003113 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003114 switch (type_class) {
3115 default:
3116 break;
3117 case clang::Type::Typedef:
3118 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
3119 ->getDecl()
3120 ->getUnderlyingType()
3121 .getAsOpaquePtr(),
3122 nullptr);
3123 case clang::Type::Auto:
3124 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
3125 ->getDeducedType()
3126 .getAsOpaquePtr(),
3127 nullptr);
3128 case clang::Type::Elaborated:
3129 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
3130 ->getNamedType()
3131 .getAsOpaquePtr(),
3132 nullptr);
3133 case clang::Type::Paren:
3134 return IsFunctionType(
3135 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3136 nullptr);
3137 case clang::Type::LValueReference:
3138 case clang::Type::RValueReference: {
3139 const clang::ReferenceType *reference_type =
3140 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3141 if (reference_type)
3142 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
3143 nullptr);
3144 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00003145 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003146 }
3147 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003148}
3149
3150// Used to detect "Homogeneous Floating-point Aggregates"
3151uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003152ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
3153 CompilerType *base_type_ptr) {
3154 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003155 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003156
3157 clang::QualType qual_type(GetCanonicalQualType(type));
3158 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3159 switch (type_class) {
3160 case clang::Type::Record:
3161 if (GetCompleteType(type)) {
3162 const clang::CXXRecordDecl *cxx_record_decl =
3163 qual_type->getAsCXXRecordDecl();
3164 if (cxx_record_decl) {
3165 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3166 return 0;
3167 }
3168 const clang::RecordType *record_type =
3169 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3170 if (record_type) {
3171 const clang::RecordDecl *record_decl = record_type->getDecl();
3172 if (record_decl) {
3173 // We are looking for a structure that contains only floating point
3174 // types
3175 clang::RecordDecl::field_iterator field_pos,
3176 field_end = record_decl->field_end();
3177 uint32_t num_fields = 0;
3178 bool is_hva = false;
3179 bool is_hfa = false;
3180 clang::QualType base_qual_type;
3181 uint64_t base_bitwidth = 0;
3182 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3183 ++field_pos) {
3184 clang::QualType field_qual_type = field_pos->getType();
3185 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
3186 if (field_qual_type->isFloatingType()) {
3187 if (field_qual_type->isComplexType())
3188 return 0;
3189 else {
3190 if (num_fields == 0)
3191 base_qual_type = field_qual_type;
3192 else {
3193 if (is_hva)
3194 return 0;
3195 is_hfa = true;
3196 if (field_qual_type.getTypePtr() !=
3197 base_qual_type.getTypePtr())
3198 return 0;
3199 }
3200 }
3201 } else if (field_qual_type->isVectorType() ||
3202 field_qual_type->isExtVectorType()) {
3203 if (num_fields == 0) {
3204 base_qual_type = field_qual_type;
3205 base_bitwidth = field_bitwidth;
3206 } else {
3207 if (is_hfa)
3208 return 0;
3209 is_hva = true;
3210 if (base_bitwidth != field_bitwidth)
3211 return 0;
3212 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3213 return 0;
3214 }
3215 } else
3216 return 0;
3217 ++num_fields;
3218 }
3219 if (base_type_ptr)
3220 *base_type_ptr = CompilerType(getASTContext(), base_qual_type);
3221 return num_fields;
3222 }
3223 }
3224 }
3225 break;
3226
3227 case clang::Type::Typedef:
3228 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3229 ->getDecl()
3230 ->getUnderlyingType()
3231 .getAsOpaquePtr(),
3232 base_type_ptr);
3233
3234 case clang::Type::Auto:
3235 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3236 ->getDeducedType()
3237 .getAsOpaquePtr(),
3238 base_type_ptr);
3239
3240 case clang::Type::Elaborated:
3241 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3242 ->getNamedType()
3243 .getAsOpaquePtr(),
3244 base_type_ptr);
3245 default:
3246 break;
3247 }
3248 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003249}
3250
Kate Stoneb9c1b512016-09-06 20:57:50 +00003251size_t ClangASTContext::GetNumberOfFunctionArguments(
3252 lldb::opaque_compiler_type_t type) {
3253 if (type) {
3254 clang::QualType qual_type(GetCanonicalQualType(type));
3255 const clang::FunctionProtoType *func =
3256 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3257 if (func)
3258 return func->getNumParams();
3259 }
3260 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003261}
3262
Greg Claytona1e5dc82015-08-11 22:53:00 +00003263CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00003264ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3265 const size_t index) {
3266 if (type) {
Greg Claytond8d4a572015-08-11 21:38:15 +00003267 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003268 const clang::FunctionProtoType *func =
3269 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3270 if (func) {
3271 if (index < func->getNumParams())
3272 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00003273 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003274 }
3275 return CompilerType();
3276}
3277
3278bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3279 if (type) {
3280 clang::QualType qual_type(GetCanonicalQualType(type));
3281
3282 if (qual_type->isFunctionPointerType())
3283 return true;
3284
3285 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3286 switch (type_class) {
3287 default:
3288 break;
3289 case clang::Type::Typedef:
3290 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3291 ->getDecl()
3292 ->getUnderlyingType()
3293 .getAsOpaquePtr());
3294 case clang::Type::Auto:
3295 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3296 ->getDeducedType()
3297 .getAsOpaquePtr());
3298 case clang::Type::Elaborated:
3299 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3300 ->getNamedType()
3301 .getAsOpaquePtr());
3302 case clang::Type::Paren:
3303 return IsFunctionPointerType(
3304 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3305
3306 case clang::Type::LValueReference:
3307 case clang::Type::RValueReference: {
3308 const clang::ReferenceType *reference_type =
3309 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3310 if (reference_type)
3311 return IsFunctionPointerType(
3312 reference_type->getPointeeType().getAsOpaquePtr());
3313 } break;
3314 }
3315 }
3316 return false;
3317}
3318
3319bool ClangASTContext::IsBlockPointerType(
3320 lldb::opaque_compiler_type_t type,
3321 CompilerType *function_pointer_type_ptr) {
3322 if (type) {
3323 clang::QualType qual_type(GetCanonicalQualType(type));
3324
3325 if (qual_type->isBlockPointerType()) {
3326 if (function_pointer_type_ptr) {
3327 const clang::BlockPointerType *block_pointer_type =
3328 qual_type->getAs<clang::BlockPointerType>();
3329 QualType pointee_type = block_pointer_type->getPointeeType();
Jonas Devlieghered5b44032019-02-13 06:25:41 +00003330 QualType function_pointer_type = m_ast_up->getPointerType(pointee_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003331 *function_pointer_type_ptr =
3332 CompilerType(getASTContext(), function_pointer_type);
3333 }
3334 return true;
3335 }
3336
3337 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3338 switch (type_class) {
3339 default:
3340 break;
3341 case clang::Type::Typedef:
3342 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3343 ->getDecl()
3344 ->getUnderlyingType()
3345 .getAsOpaquePtr(),
3346 function_pointer_type_ptr);
3347 case clang::Type::Auto:
3348 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3349 ->getDeducedType()
3350 .getAsOpaquePtr(),
3351 function_pointer_type_ptr);
3352 case clang::Type::Elaborated:
3353 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3354 ->getNamedType()
3355 .getAsOpaquePtr(),
3356 function_pointer_type_ptr);
3357 case clang::Type::Paren:
3358 return IsBlockPointerType(
3359 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3360 function_pointer_type_ptr);
3361
3362 case clang::Type::LValueReference:
3363 case clang::Type::RValueReference: {
3364 const clang::ReferenceType *reference_type =
3365 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3366 if (reference_type)
3367 return IsBlockPointerType(
3368 reference_type->getPointeeType().getAsOpaquePtr(),
3369 function_pointer_type_ptr);
3370 } break;
3371 }
3372 }
3373 return false;
3374}
3375
3376bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3377 bool &is_signed) {
3378 if (!type)
3379 return false;
3380
3381 clang::QualType qual_type(GetCanonicalQualType(type));
3382 const clang::BuiltinType *builtin_type =
3383 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3384
3385 if (builtin_type) {
3386 if (builtin_type->isInteger()) {
3387 is_signed = builtin_type->isSignedInteger();
3388 return true;
3389 }
3390 }
3391
3392 return false;
3393}
3394
3395bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3396 bool &is_signed) {
3397 if (type) {
3398 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3399 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3400
3401 if (enum_type) {
3402 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3403 is_signed);
3404 return true;
3405 }
3406 }
3407
3408 return false;
3409}
3410
3411bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3412 CompilerType *pointee_type) {
3413 if (type) {
3414 clang::QualType qual_type(GetCanonicalQualType(type));
3415 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3416 switch (type_class) {
3417 case clang::Type::Builtin:
3418 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3419 default:
3420 break;
3421 case clang::BuiltinType::ObjCId:
3422 case clang::BuiltinType::ObjCClass:
3423 return true;
3424 }
3425 return false;
3426 case clang::Type::ObjCObjectPointer:
3427 if (pointee_type)
3428 pointee_type->SetCompilerType(
3429 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3430 ->getPointeeType());
3431 return true;
3432 case clang::Type::BlockPointer:
3433 if (pointee_type)
3434 pointee_type->SetCompilerType(
3435 getASTContext(),
3436 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3437 return true;
3438 case clang::Type::Pointer:
3439 if (pointee_type)
3440 pointee_type->SetCompilerType(
3441 getASTContext(),
3442 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3443 return true;
3444 case clang::Type::MemberPointer:
3445 if (pointee_type)
3446 pointee_type->SetCompilerType(
3447 getASTContext(),
3448 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3449 return true;
3450 case clang::Type::Typedef:
3451 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3452 ->getDecl()
3453 ->getUnderlyingType()
3454 .getAsOpaquePtr(),
3455 pointee_type);
3456 case clang::Type::Auto:
3457 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3458 ->getDeducedType()
3459 .getAsOpaquePtr(),
3460 pointee_type);
3461 case clang::Type::Elaborated:
3462 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3463 ->getNamedType()
3464 .getAsOpaquePtr(),
3465 pointee_type);
3466 case clang::Type::Paren:
3467 return IsPointerType(
3468 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3469 pointee_type);
3470 default:
3471 break;
3472 }
3473 }
3474 if (pointee_type)
3475 pointee_type->Clear();
3476 return false;
3477}
3478
3479bool ClangASTContext::IsPointerOrReferenceType(
3480 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3481 if (type) {
3482 clang::QualType qual_type(GetCanonicalQualType(type));
3483 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3484 switch (type_class) {
3485 case clang::Type::Builtin:
3486 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3487 default:
3488 break;
3489 case clang::BuiltinType::ObjCId:
3490 case clang::BuiltinType::ObjCClass:
3491 return true;
3492 }
3493 return false;
3494 case clang::Type::ObjCObjectPointer:
3495 if (pointee_type)
3496 pointee_type->SetCompilerType(
3497 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3498 ->getPointeeType());
3499 return true;
3500 case clang::Type::BlockPointer:
3501 if (pointee_type)
3502 pointee_type->SetCompilerType(
3503 getASTContext(),
3504 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3505 return true;
3506 case clang::Type::Pointer:
3507 if (pointee_type)
3508 pointee_type->SetCompilerType(
3509 getASTContext(),
3510 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3511 return true;
3512 case clang::Type::MemberPointer:
3513 if (pointee_type)
3514 pointee_type->SetCompilerType(
3515 getASTContext(),
3516 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3517 return true;
3518 case clang::Type::LValueReference:
3519 if (pointee_type)
3520 pointee_type->SetCompilerType(
3521 getASTContext(),
3522 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3523 return true;
3524 case clang::Type::RValueReference:
3525 if (pointee_type)
3526 pointee_type->SetCompilerType(
3527 getASTContext(),
3528 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3529 return true;
3530 case clang::Type::Typedef:
3531 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3532 ->getDecl()
3533 ->getUnderlyingType()
3534 .getAsOpaquePtr(),
3535 pointee_type);
3536 case clang::Type::Auto:
3537 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3538 ->getDeducedType()
3539 .getAsOpaquePtr(),
3540 pointee_type);
3541 case clang::Type::Elaborated:
3542 return IsPointerOrReferenceType(
3543 llvm::cast<clang::ElaboratedType>(qual_type)
3544 ->getNamedType()
3545 .getAsOpaquePtr(),
3546 pointee_type);
3547 case clang::Type::Paren:
3548 return IsPointerOrReferenceType(
3549 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3550 pointee_type);
3551 default:
3552 break;
3553 }
3554 }
3555 if (pointee_type)
3556 pointee_type->Clear();
3557 return false;
3558}
3559
3560bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3561 CompilerType *pointee_type,
3562 bool *is_rvalue) {
3563 if (type) {
3564 clang::QualType qual_type(GetCanonicalQualType(type));
3565 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3566
3567 switch (type_class) {
3568 case clang::Type::LValueReference:
3569 if (pointee_type)
3570 pointee_type->SetCompilerType(
3571 getASTContext(),
3572 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3573 if (is_rvalue)
3574 *is_rvalue = false;
3575 return true;
3576 case clang::Type::RValueReference:
3577 if (pointee_type)
3578 pointee_type->SetCompilerType(
3579 getASTContext(),
3580 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3581 if (is_rvalue)
3582 *is_rvalue = true;
3583 return true;
3584 case clang::Type::Typedef:
3585 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3586 ->getDecl()
3587 ->getUnderlyingType()
3588 .getAsOpaquePtr(),
3589 pointee_type, is_rvalue);
3590 case clang::Type::Auto:
3591 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3592 ->getDeducedType()
3593 .getAsOpaquePtr(),
3594 pointee_type, is_rvalue);
3595 case clang::Type::Elaborated:
3596 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3597 ->getNamedType()
3598 .getAsOpaquePtr(),
3599 pointee_type, is_rvalue);
3600 case clang::Type::Paren:
3601 return IsReferenceType(
3602 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3603 pointee_type, is_rvalue);
3604
3605 default:
3606 break;
3607 }
3608 }
3609 if (pointee_type)
3610 pointee_type->Clear();
3611 return false;
3612}
3613
3614bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3615 uint32_t &count, bool &is_complex) {
3616 if (type) {
3617 clang::QualType qual_type(GetCanonicalQualType(type));
3618
3619 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3620 qual_type->getCanonicalTypeInternal())) {
3621 clang::BuiltinType::Kind kind = BT->getKind();
3622 if (kind >= clang::BuiltinType::Float &&
3623 kind <= clang::BuiltinType::LongDouble) {
3624 count = 1;
3625 is_complex = false;
3626 return true;
3627 }
3628 } else if (const clang::ComplexType *CT =
3629 llvm::dyn_cast<clang::ComplexType>(
3630 qual_type->getCanonicalTypeInternal())) {
3631 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3632 is_complex)) {
3633 count = 2;
3634 is_complex = true;
3635 return true;
3636 }
3637 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3638 qual_type->getCanonicalTypeInternal())) {
3639 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3640 is_complex)) {
3641 count = VT->getNumElements();
3642 is_complex = false;
3643 return true;
3644 }
3645 }
3646 }
3647 count = 0;
3648 is_complex = false;
3649 return false;
3650}
3651
3652bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3653 if (!type)
3654 return false;
3655
3656 clang::QualType qual_type(GetQualType(type));
3657 const clang::TagType *tag_type =
3658 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3659 if (tag_type) {
3660 clang::TagDecl *tag_decl = tag_type->getDecl();
3661 if (tag_decl)
3662 return tag_decl->isCompleteDefinition();
3663 return false;
3664 } else {
3665 const clang::ObjCObjectType *objc_class_type =
3666 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3667 if (objc_class_type) {
3668 clang::ObjCInterfaceDecl *class_interface_decl =
3669 objc_class_type->getInterface();
3670 if (class_interface_decl)
3671 return class_interface_decl->getDefinition() != nullptr;
3672 return false;
3673 }
3674 }
3675 return true;
3676}
3677
3678bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
3679 if (type) {
3680 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3681
3682 const clang::ObjCObjectPointerType *obj_pointer_type =
3683 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3684
3685 if (obj_pointer_type)
3686 return obj_pointer_type->isObjCClassType();
3687 }
3688 return false;
3689}
3690
3691bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3692 if (ClangUtil::IsClangType(type))
3693 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3694 return false;
3695}
3696
3697bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3698 if (!type)
3699 return false;
3700 clang::QualType qual_type(GetCanonicalQualType(type));
3701 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3702 return (type_class == clang::Type::Record);
3703}
3704
3705bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3706 if (!type)
3707 return false;
3708 clang::QualType qual_type(GetCanonicalQualType(type));
3709 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3710 return (type_class == clang::Type::Enum);
3711}
3712
3713bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3714 if (type) {
3715 clang::QualType qual_type(GetCanonicalQualType(type));
3716 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3717 switch (type_class) {
3718 case clang::Type::Record:
3719 if (GetCompleteType(type)) {
3720 const clang::RecordType *record_type =
3721 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3722 const clang::RecordDecl *record_decl = record_type->getDecl();
3723 if (record_decl) {
3724 const clang::CXXRecordDecl *cxx_record_decl =
3725 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3726 if (cxx_record_decl)
3727 return cxx_record_decl->isPolymorphic();
Greg Claytond8d4a572015-08-11 21:38:15 +00003728 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003729 }
3730 break;
3731
3732 default:
3733 break;
3734 }
3735 }
3736 return false;
3737}
3738
3739bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3740 CompilerType *dynamic_pointee_type,
3741 bool check_cplusplus,
3742 bool check_objc) {
3743 clang::QualType pointee_qual_type;
3744 if (type) {
3745 clang::QualType qual_type(GetCanonicalQualType(type));
3746 bool success = false;
3747 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3748 switch (type_class) {
3749 case clang::Type::Builtin:
3750 if (check_objc &&
3751 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3752 clang::BuiltinType::ObjCId) {
3753 if (dynamic_pointee_type)
3754 dynamic_pointee_type->SetCompilerType(this, type);
3755 return true;
3756 }
3757 break;
3758
3759 case clang::Type::ObjCObjectPointer:
3760 if (check_objc) {
3761 if (auto objc_pointee_type =
3762 qual_type->getPointeeType().getTypePtrOrNull()) {
3763 if (auto objc_object_type =
3764 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3765 objc_pointee_type)) {
3766 if (objc_object_type->isObjCClass())
3767 return false;
3768 }
3769 }
3770 if (dynamic_pointee_type)
3771 dynamic_pointee_type->SetCompilerType(
3772 getASTContext(),
3773 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3774 ->getPointeeType());
3775 return true;
3776 }
3777 break;
3778
3779 case clang::Type::Pointer:
3780 pointee_qual_type =
3781 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3782 success = true;
3783 break;
3784
3785 case clang::Type::LValueReference:
3786 case clang::Type::RValueReference:
3787 pointee_qual_type =
3788 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3789 success = true;
3790 break;
3791
3792 case clang::Type::Typedef:
3793 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3794 ->getDecl()
3795 ->getUnderlyingType()
3796 .getAsOpaquePtr(),
3797 dynamic_pointee_type, check_cplusplus,
3798 check_objc);
3799
3800 case clang::Type::Auto:
3801 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3802 ->getDeducedType()
3803 .getAsOpaquePtr(),
3804 dynamic_pointee_type, check_cplusplus,
3805 check_objc);
3806
3807 case clang::Type::Elaborated:
3808 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3809 ->getNamedType()
3810 .getAsOpaquePtr(),
3811 dynamic_pointee_type, check_cplusplus,
3812 check_objc);
3813
3814 case clang::Type::Paren:
3815 return IsPossibleDynamicType(
3816 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3817 dynamic_pointee_type, check_cplusplus, check_objc);
3818 default:
3819 break;
3820 }
3821
3822 if (success) {
3823 // Check to make sure what we are pointing too is a possible dynamic C++
Adrian Prantl05097242018-04-30 16:49:04 +00003824 // type We currently accept any "void *" (in case we have a class that
3825 // has been watered down to an opaque pointer) and virtual C++ classes.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003826 const clang::Type::TypeClass pointee_type_class =
3827 pointee_qual_type.getCanonicalType()->getTypeClass();
3828 switch (pointee_type_class) {
3829 case clang::Type::Builtin:
3830 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3831 case clang::BuiltinType::UnknownAny:
3832 case clang::BuiltinType::Void:
3833 if (dynamic_pointee_type)
3834 dynamic_pointee_type->SetCompilerType(getASTContext(),
3835 pointee_qual_type);
3836 return true;
3837 default:
3838 break;
3839 }
3840 break;
3841
3842 case clang::Type::Record:
3843 if (check_cplusplus) {
3844 clang::CXXRecordDecl *cxx_record_decl =
3845 pointee_qual_type->getAsCXXRecordDecl();
3846 if (cxx_record_decl) {
3847 bool is_complete = cxx_record_decl->isCompleteDefinition();
3848
3849 if (is_complete)
3850 success = cxx_record_decl->isDynamicClass();
3851 else {
3852 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3853 getASTContext(), cxx_record_decl);
3854 if (metadata)
3855 success = metadata->GetIsDynamicCXXType();
3856 else {
3857 is_complete = CompilerType(getASTContext(), pointee_qual_type)
3858 .GetCompleteType();
3859 if (is_complete)
3860 success = cxx_record_decl->isDynamicClass();
3861 else
3862 success = false;
3863 }
3864 }
3865
3866 if (success) {
3867 if (dynamic_pointee_type)
3868 dynamic_pointee_type->SetCompilerType(getASTContext(),
3869 pointee_qual_type);
3870 return true;
3871 }
3872 }
3873 }
3874 break;
3875
3876 case clang::Type::ObjCObject:
3877 case clang::Type::ObjCInterface:
3878 if (check_objc) {
3879 if (dynamic_pointee_type)
3880 dynamic_pointee_type->SetCompilerType(getASTContext(),
3881 pointee_qual_type);
3882 return true;
3883 }
3884 break;
3885
3886 default:
3887 break;
3888 }
3889 }
3890 }
3891 if (dynamic_pointee_type)
3892 dynamic_pointee_type->Clear();
3893 return false;
3894}
3895
3896bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3897 if (!type)
3898 return false;
3899
3900 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3901}
3902
3903bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3904 if (!type)
3905 return false;
3906 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3907}
3908
3909bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3910 if (!type)
3911 return false;
3912 return GetCanonicalQualType(type)->isVoidType();
3913}
3914
Alex Langforda03e2b22019-06-04 19:29:59 +00003915bool ClangASTContext::CanPassInRegisters(const CompilerType &type) {
3916 if (auto *record_decl =
3917 ClangASTContext::GetAsRecordDecl(type)) {
3918 return record_decl->canPassInRegisters();
3919 }
3920 return false;
3921}
3922
Kate Stoneb9c1b512016-09-06 20:57:50 +00003923bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3924 return ClangASTContextSupportsLanguage(language);
3925}
3926
3927bool ClangASTContext::GetCXXClassName(const CompilerType &type,
3928 std::string &class_name) {
3929 if (type) {
3930 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3931 if (!qual_type.isNull()) {
3932 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3933 if (cxx_record_decl) {
3934 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3935 return true;
3936 }
3937 }
3938 }
3939 class_name.clear();
3940 return false;
3941}
3942
3943bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3944 if (!type)
3945 return false;
3946
3947 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Jonas Devliegherea6682a42018-12-15 00:15:33 +00003948 return !qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003949}
3950
3951bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3952 if (!type)
3953 return false;
3954 clang::QualType qual_type(GetCanonicalQualType(type));
3955 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3956 if (tag_type)
3957 return tag_type->isBeingDefined();
3958 return false;
3959}
3960
3961bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3962 CompilerType *class_type_ptr) {
3963 if (!type)
3964 return false;
3965
3966 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3967
3968 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3969 if (class_type_ptr) {
3970 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3971 const clang::ObjCObjectPointerType *obj_pointer_type =
3972 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3973 if (obj_pointer_type == nullptr)
3974 class_type_ptr->Clear();
3975 else
3976 class_type_ptr->SetCompilerType(
3977 type.GetTypeSystem(),
3978 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3979 .getAsOpaquePtr());
3980 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003981 }
3982 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003983 }
3984 if (class_type_ptr)
3985 class_type_ptr->Clear();
3986 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003987}
3988
Kate Stoneb9c1b512016-09-06 20:57:50 +00003989bool ClangASTContext::GetObjCClassName(const CompilerType &type,
3990 std::string &class_name) {
3991 if (!type)
3992 return false;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003993
Kate Stoneb9c1b512016-09-06 20:57:50 +00003994 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3995
3996 const clang::ObjCObjectType *object_type =
3997 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3998 if (object_type) {
3999 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
4000 if (interface) {
4001 class_name = interface->getNameAsString();
4002 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00004003 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004004 }
4005 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00004006}
4007
Greg Claytond8d4a572015-08-11 21:38:15 +00004008// Type Completion
Greg Claytond8d4a572015-08-11 21:38:15 +00004009
Kate Stoneb9c1b512016-09-06 20:57:50 +00004010bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
4011 if (!type)
4012 return false;
4013 const bool allow_completion = true;
4014 return GetCompleteQualType(getASTContext(), GetQualType(type),
4015 allow_completion);
Greg Claytond8d4a572015-08-11 21:38:15 +00004016}
4017
Kate Stoneb9c1b512016-09-06 20:57:50 +00004018ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
4019 std::string type_name;
4020 if (type) {
4021 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
4022 clang::QualType qual_type(GetQualType(type));
4023 printing_policy.SuppressTagKeyword = true;
4024 const clang::TypedefType *typedef_type =
4025 qual_type->getAs<clang::TypedefType>();
4026 if (typedef_type) {
4027 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
4028 type_name = typedef_decl->getQualifiedNameAsString();
4029 } else {
4030 type_name = qual_type.getAsString(printing_policy);
Greg Claytond8d4a572015-08-11 21:38:15 +00004031 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004032 }
4033 return ConstString(type_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00004034}
4035
4036uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004037ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
4038 CompilerType *pointee_or_element_clang_type) {
4039 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004040 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004041
4042 if (pointee_or_element_clang_type)
4043 pointee_or_element_clang_type->Clear();
4044
4045 clang::QualType qual_type(GetQualType(type));
4046
4047 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4048 switch (type_class) {
Sean Callananddf802a2017-06-02 01:24:18 +00004049 case clang::Type::Attributed:
4050 return GetTypeInfo(
4051 qual_type->getAs<clang::AttributedType>()
4052 ->getModifiedType().getAsOpaquePtr(),
4053 pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004054 case clang::Type::Builtin: {
4055 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
4056 qual_type->getCanonicalTypeInternal());
4057
4058 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
4059 switch (builtin_type->getKind()) {
4060 case clang::BuiltinType::ObjCId:
4061 case clang::BuiltinType::ObjCClass:
4062 if (pointee_or_element_clang_type)
4063 pointee_or_element_clang_type->SetCompilerType(
4064 getASTContext(), getASTContext()->ObjCBuiltinClassTy);
4065 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
4066 break;
4067
4068 case clang::BuiltinType::ObjCSel:
4069 if (pointee_or_element_clang_type)
4070 pointee_or_element_clang_type->SetCompilerType(getASTContext(),
4071 getASTContext()->CharTy);
4072 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
4073 break;
4074
4075 case clang::BuiltinType::Bool:
4076 case clang::BuiltinType::Char_U:
4077 case clang::BuiltinType::UChar:
4078 case clang::BuiltinType::WChar_U:
4079 case clang::BuiltinType::Char16:
4080 case clang::BuiltinType::Char32:
4081 case clang::BuiltinType::UShort:
4082 case clang::BuiltinType::UInt:
4083 case clang::BuiltinType::ULong:
4084 case clang::BuiltinType::ULongLong:
4085 case clang::BuiltinType::UInt128:
4086 case clang::BuiltinType::Char_S:
4087 case clang::BuiltinType::SChar:
4088 case clang::BuiltinType::WChar_S:
4089 case clang::BuiltinType::Short:
4090 case clang::BuiltinType::Int:
4091 case clang::BuiltinType::Long:
4092 case clang::BuiltinType::LongLong:
4093 case clang::BuiltinType::Int128:
4094 case clang::BuiltinType::Float:
4095 case clang::BuiltinType::Double:
4096 case clang::BuiltinType::LongDouble:
4097 builtin_type_flags |= eTypeIsScalar;
4098 if (builtin_type->isInteger()) {
4099 builtin_type_flags |= eTypeIsInteger;
4100 if (builtin_type->isSignedInteger())
4101 builtin_type_flags |= eTypeIsSigned;
4102 } else if (builtin_type->isFloatingPoint())
4103 builtin_type_flags |= eTypeIsFloat;
4104 break;
4105 default:
4106 break;
4107 }
4108 return builtin_type_flags;
4109 }
4110
4111 case clang::Type::BlockPointer:
4112 if (pointee_or_element_clang_type)
4113 pointee_or_element_clang_type->SetCompilerType(
4114 getASTContext(), qual_type->getPointeeType());
4115 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
4116
4117 case clang::Type::Complex: {
4118 uint32_t complex_type_flags =
4119 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
4120 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
4121 qual_type->getCanonicalTypeInternal());
4122 if (complex_type) {
4123 clang::QualType complex_element_type(complex_type->getElementType());
4124 if (complex_element_type->isIntegerType())
4125 complex_type_flags |= eTypeIsFloat;
4126 else if (complex_element_type->isFloatingType())
4127 complex_type_flags |= eTypeIsInteger;
4128 }
4129 return complex_type_flags;
4130 } break;
4131
4132 case clang::Type::ConstantArray:
4133 case clang::Type::DependentSizedArray:
4134 case clang::Type::IncompleteArray:
4135 case clang::Type::VariableArray:
4136 if (pointee_or_element_clang_type)
4137 pointee_or_element_clang_type->SetCompilerType(
4138 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
4139 ->getElementType());
4140 return eTypeHasChildren | eTypeIsArray;
4141
4142 case clang::Type::DependentName:
4143 return 0;
4144 case clang::Type::DependentSizedExtVector:
4145 return eTypeHasChildren | eTypeIsVector;
4146 case clang::Type::DependentTemplateSpecialization:
4147 return eTypeIsTemplate;
4148 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004149 return CompilerType(
4150 getASTContext(),
4151 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
4152 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004153
4154 case clang::Type::Enum:
4155 if (pointee_or_element_clang_type)
4156 pointee_or_element_clang_type->SetCompilerType(
4157 getASTContext(),
4158 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
4159 return eTypeIsEnumeration | eTypeHasValue;
4160
4161 case clang::Type::Auto:
4162 return CompilerType(
4163 getASTContext(),
4164 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4165 .GetTypeInfo(pointee_or_element_clang_type);
4166 case clang::Type::Elaborated:
4167 return CompilerType(
4168 getASTContext(),
4169 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4170 .GetTypeInfo(pointee_or_element_clang_type);
4171 case clang::Type::Paren:
4172 return CompilerType(getASTContext(),
4173 llvm::cast<clang::ParenType>(qual_type)->desugar())
4174 .GetTypeInfo(pointee_or_element_clang_type);
4175
4176 case clang::Type::FunctionProto:
4177 return eTypeIsFuncPrototype | eTypeHasValue;
4178 case clang::Type::FunctionNoProto:
4179 return eTypeIsFuncPrototype | eTypeHasValue;
4180 case clang::Type::InjectedClassName:
4181 return 0;
4182
4183 case clang::Type::LValueReference:
4184 case clang::Type::RValueReference:
4185 if (pointee_or_element_clang_type)
4186 pointee_or_element_clang_type->SetCompilerType(
4187 getASTContext(),
4188 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4189 ->getPointeeType());
4190 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4191
4192 case clang::Type::MemberPointer:
4193 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4194
4195 case clang::Type::ObjCObjectPointer:
4196 if (pointee_or_element_clang_type)
4197 pointee_or_element_clang_type->SetCompilerType(
4198 getASTContext(), qual_type->getPointeeType());
4199 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4200 eTypeHasValue;
4201
4202 case clang::Type::ObjCObject:
4203 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4204 case clang::Type::ObjCInterface:
4205 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4206
4207 case clang::Type::Pointer:
4208 if (pointee_or_element_clang_type)
4209 pointee_or_element_clang_type->SetCompilerType(
4210 getASTContext(), qual_type->getPointeeType());
4211 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4212
4213 case clang::Type::Record:
4214 if (qual_type->getAsCXXRecordDecl())
4215 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4216 else
4217 return eTypeHasChildren | eTypeIsStructUnion;
4218 break;
4219 case clang::Type::SubstTemplateTypeParm:
4220 return eTypeIsTemplate;
4221 case clang::Type::TemplateTypeParm:
4222 return eTypeIsTemplate;
4223 case clang::Type::TemplateSpecialization:
4224 return eTypeIsTemplate;
4225
4226 case clang::Type::Typedef:
4227 return eTypeIsTypedef |
4228 CompilerType(getASTContext(),
4229 llvm::cast<clang::TypedefType>(qual_type)
4230 ->getDecl()
4231 ->getUnderlyingType())
4232 .GetTypeInfo(pointee_or_element_clang_type);
4233 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004234 return CompilerType(getASTContext(),
4235 llvm::cast<clang::TypeOfExprType>(qual_type)
4236 ->getUnderlyingExpr()
4237 ->getType())
4238 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004239 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004240 return CompilerType(
4241 getASTContext(),
4242 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4243 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004244 case clang::Type::UnresolvedUsing:
4245 return 0;
4246
4247 case clang::Type::ExtVector:
4248 case clang::Type::Vector: {
4249 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4250 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4251 qual_type->getCanonicalTypeInternal());
4252 if (vector_type) {
4253 if (vector_type->isIntegerType())
4254 vector_type_flags |= eTypeIsFloat;
4255 else if (vector_type->isFloatingType())
4256 vector_type_flags |= eTypeIsInteger;
4257 }
4258 return vector_type_flags;
4259 }
4260 default:
4261 return 0;
4262 }
4263 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004264}
4265
Greg Claytond8d4a572015-08-11 21:38:15 +00004266lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004267ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4268 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004269 return lldb::eLanguageTypeC;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004270
4271 // If the type is a reference, then resolve it to what it refers to first:
4272 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4273 if (qual_type->isAnyPointerType()) {
4274 if (qual_type->isObjCObjectPointerType())
4275 return lldb::eLanguageTypeObjC;
Adrian Prantl1db0f0c2019-05-02 23:07:23 +00004276 if (qual_type->getPointeeCXXRecordDecl())
4277 return lldb::eLanguageTypeC_plus_plus;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004278
4279 clang::QualType pointee_type(qual_type->getPointeeType());
Adrian Prantl1db0f0c2019-05-02 23:07:23 +00004280 if (pointee_type->getPointeeCXXRecordDecl())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004281 return lldb::eLanguageTypeC_plus_plus;
4282 if (pointee_type->isObjCObjectOrInterfaceType())
4283 return lldb::eLanguageTypeObjC;
4284 if (pointee_type->isObjCClassType())
4285 return lldb::eLanguageTypeObjC;
4286 if (pointee_type.getTypePtr() ==
4287 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4288 return lldb::eLanguageTypeObjC;
4289 } else {
4290 if (qual_type->isObjCObjectOrInterfaceType())
4291 return lldb::eLanguageTypeObjC;
4292 if (qual_type->getAsCXXRecordDecl())
4293 return lldb::eLanguageTypeC_plus_plus;
4294 switch (qual_type->getTypeClass()) {
4295 default:
4296 break;
4297 case clang::Type::Builtin:
4298 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4299 default:
4300 case clang::BuiltinType::Void:
4301 case clang::BuiltinType::Bool:
4302 case clang::BuiltinType::Char_U:
4303 case clang::BuiltinType::UChar:
4304 case clang::BuiltinType::WChar_U:
4305 case clang::BuiltinType::Char16:
4306 case clang::BuiltinType::Char32:
4307 case clang::BuiltinType::UShort:
4308 case clang::BuiltinType::UInt:
4309 case clang::BuiltinType::ULong:
4310 case clang::BuiltinType::ULongLong:
4311 case clang::BuiltinType::UInt128:
4312 case clang::BuiltinType::Char_S:
4313 case clang::BuiltinType::SChar:
4314 case clang::BuiltinType::WChar_S:
4315 case clang::BuiltinType::Short:
4316 case clang::BuiltinType::Int:
4317 case clang::BuiltinType::Long:
4318 case clang::BuiltinType::LongLong:
4319 case clang::BuiltinType::Int128:
4320 case clang::BuiltinType::Float:
4321 case clang::BuiltinType::Double:
4322 case clang::BuiltinType::LongDouble:
4323 break;
4324
4325 case clang::BuiltinType::NullPtr:
4326 return eLanguageTypeC_plus_plus;
4327
4328 case clang::BuiltinType::ObjCId:
4329 case clang::BuiltinType::ObjCClass:
4330 case clang::BuiltinType::ObjCSel:
4331 return eLanguageTypeObjC;
4332
4333 case clang::BuiltinType::Dependent:
4334 case clang::BuiltinType::Overload:
4335 case clang::BuiltinType::BoundMember:
4336 case clang::BuiltinType::UnknownAny:
4337 break;
4338 }
4339 break;
4340 case clang::Type::Typedef:
4341 return CompilerType(getASTContext(),
4342 llvm::cast<clang::TypedefType>(qual_type)
4343 ->getDecl()
4344 ->getUnderlyingType())
4345 .GetMinimumLanguage();
4346 }
4347 }
4348 return lldb::eLanguageTypeC;
Greg Claytond8d4a572015-08-11 21:38:15 +00004349}
4350
4351lldb::TypeClass
Kate Stoneb9c1b512016-09-06 20:57:50 +00004352ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4353 if (!type)
4354 return lldb::eTypeClassInvalid;
4355
4356 clang::QualType qual_type(GetQualType(type));
4357
4358 switch (qual_type->getTypeClass()) {
4359 case clang::Type::UnaryTransform:
4360 break;
4361 case clang::Type::FunctionNoProto:
4362 return lldb::eTypeClassFunction;
4363 case clang::Type::FunctionProto:
4364 return lldb::eTypeClassFunction;
4365 case clang::Type::IncompleteArray:
4366 return lldb::eTypeClassArray;
4367 case clang::Type::VariableArray:
4368 return lldb::eTypeClassArray;
4369 case clang::Type::ConstantArray:
4370 return lldb::eTypeClassArray;
4371 case clang::Type::DependentSizedArray:
4372 return lldb::eTypeClassArray;
4373 case clang::Type::DependentSizedExtVector:
4374 return lldb::eTypeClassVector;
Fangrui Song8f284882018-07-13 22:40:40 +00004375 case clang::Type::DependentVector:
4376 return lldb::eTypeClassVector;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004377 case clang::Type::ExtVector:
4378 return lldb::eTypeClassVector;
4379 case clang::Type::Vector:
4380 return lldb::eTypeClassVector;
4381 case clang::Type::Builtin:
4382 return lldb::eTypeClassBuiltin;
4383 case clang::Type::ObjCObjectPointer:
4384 return lldb::eTypeClassObjCObjectPointer;
4385 case clang::Type::BlockPointer:
4386 return lldb::eTypeClassBlockPointer;
4387 case clang::Type::Pointer:
4388 return lldb::eTypeClassPointer;
4389 case clang::Type::LValueReference:
4390 return lldb::eTypeClassReference;
4391 case clang::Type::RValueReference:
4392 return lldb::eTypeClassReference;
4393 case clang::Type::MemberPointer:
4394 return lldb::eTypeClassMemberPointer;
4395 case clang::Type::Complex:
4396 if (qual_type->isComplexType())
4397 return lldb::eTypeClassComplexFloat;
4398 else
4399 return lldb::eTypeClassComplexInteger;
4400 case clang::Type::ObjCObject:
4401 return lldb::eTypeClassObjCObject;
4402 case clang::Type::ObjCInterface:
4403 return lldb::eTypeClassObjCInterface;
4404 case clang::Type::Record: {
4405 const clang::RecordType *record_type =
4406 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4407 const clang::RecordDecl *record_decl = record_type->getDecl();
4408 if (record_decl->isUnion())
4409 return lldb::eTypeClassUnion;
4410 else if (record_decl->isStruct())
4411 return lldb::eTypeClassStruct;
4412 else
4413 return lldb::eTypeClassClass;
4414 } break;
4415 case clang::Type::Enum:
4416 return lldb::eTypeClassEnumeration;
4417 case clang::Type::Typedef:
4418 return lldb::eTypeClassTypedef;
4419 case clang::Type::UnresolvedUsing:
4420 break;
4421 case clang::Type::Paren:
4422 return CompilerType(getASTContext(),
4423 llvm::cast<clang::ParenType>(qual_type)->desugar())
4424 .GetTypeClass();
4425 case clang::Type::Auto:
4426 return CompilerType(
4427 getASTContext(),
4428 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4429 .GetTypeClass();
4430 case clang::Type::Elaborated:
4431 return CompilerType(
4432 getASTContext(),
4433 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4434 .GetTypeClass();
4435
4436 case clang::Type::Attributed:
4437 break;
4438 case clang::Type::TemplateTypeParm:
4439 break;
4440 case clang::Type::SubstTemplateTypeParm:
4441 break;
4442 case clang::Type::SubstTemplateTypeParmPack:
4443 break;
4444 case clang::Type::InjectedClassName:
4445 break;
4446 case clang::Type::DependentName:
4447 break;
4448 case clang::Type::DependentTemplateSpecialization:
4449 break;
4450 case clang::Type::PackExpansion:
4451 break;
4452
4453 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004454 return CompilerType(getASTContext(),
4455 llvm::cast<clang::TypeOfExprType>(qual_type)
4456 ->getUnderlyingExpr()
4457 ->getType())
4458 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004459 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004460 return CompilerType(
4461 getASTContext(),
4462 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4463 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004464 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004465 return CompilerType(
4466 getASTContext(),
4467 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4468 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004469 case clang::Type::TemplateSpecialization:
4470 break;
Pavel Labath4f19fce22017-02-17 13:39:50 +00004471 case clang::Type::DeducedTemplateSpecialization:
4472 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004473 case clang::Type::Atomic:
4474 break;
4475 case clang::Type::Pipe:
4476 break;
4477
4478 // pointer type decayed from an array or function type.
4479 case clang::Type::Decayed:
4480 break;
4481 case clang::Type::Adjusted:
4482 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00004483 case clang::Type::ObjCTypeParam:
4484 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00004485
4486 case clang::Type::DependentAddressSpace:
4487 break;
Krasimir Georgiev435e76a2019-05-07 13:59:30 +00004488 case clang::Type::MacroQualified:
4489 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004490 }
4491 // We don't know hot to display this type...
4492 return lldb::eTypeClassOther;
Greg Claytond8d4a572015-08-11 21:38:15 +00004493}
4494
Kate Stoneb9c1b512016-09-06 20:57:50 +00004495unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4496 if (type)
4497 return GetQualType(type).getQualifiers().getCVRQualifiers();
4498 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004499}
4500
Greg Claytond8d4a572015-08-11 21:38:15 +00004501// Creating related types
Greg Claytond8d4a572015-08-11 21:38:15 +00004502
Greg Claytona1e5dc82015-08-11 22:53:00 +00004503CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004504ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4505 uint64_t *stride) {
4506 if (type) {
4507 clang::QualType qual_type(GetCanonicalQualType(type));
4508
4509 const clang::Type *array_eletype =
4510 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4511
4512 if (!array_eletype)
4513 return CompilerType();
4514
4515 CompilerType element_type(getASTContext(),
4516 array_eletype->getCanonicalTypeUnqualified());
4517
4518 // TODO: the real stride will be >= this value.. find the real one!
4519 if (stride)
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00004520 if (Optional<uint64_t> size = element_type.GetByteSize(nullptr))
Adrian Prantld963a7c2019-01-15 18:07:52 +00004521 *stride = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004522
4523 return element_type;
4524 }
4525 return CompilerType();
4526}
4527
4528CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4529 uint64_t size) {
4530 if (type) {
4531 clang::QualType qual_type(GetCanonicalQualType(type));
4532 if (clang::ASTContext *ast_ctx = getASTContext()) {
4533 if (size != 0)
4534 return CompilerType(
4535 ast_ctx, ast_ctx->getConstantArrayType(
4536 qual_type, llvm::APInt(64, size),
4537 clang::ArrayType::ArraySizeModifier::Normal, 0));
4538 else
4539 return CompilerType(
4540 ast_ctx,
4541 ast_ctx->getIncompleteArrayType(
4542 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
Greg Claytond8d4a572015-08-11 21:38:15 +00004543 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004544 }
4545
4546 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004547}
4548
Greg Claytona1e5dc82015-08-11 22:53:00 +00004549CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004550ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4551 if (type)
4552 return CompilerType(getASTContext(), GetCanonicalQualType(type));
4553 return CompilerType();
4554}
4555
4556static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4557 clang::QualType qual_type) {
4558 if (qual_type->isPointerType())
4559 qual_type = ast->getPointerType(
4560 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4561 else
4562 qual_type = qual_type.getUnqualifiedType();
4563 qual_type.removeLocalConst();
4564 qual_type.removeLocalRestrict();
4565 qual_type.removeLocalVolatile();
4566 return qual_type;
Enrico Granata639392f2016-08-30 20:39:58 +00004567}
4568
4569CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004570ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4571 if (type)
4572 return CompilerType(
4573 getASTContext(),
4574 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4575 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004576}
4577
Kate Stoneb9c1b512016-09-06 20:57:50 +00004578int ClangASTContext::GetFunctionArgumentCount(
4579 lldb::opaque_compiler_type_t type) {
4580 if (type) {
4581 const clang::FunctionProtoType *func =
4582 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4583 if (func)
4584 return func->getNumParams();
4585 }
4586 return -1;
4587}
4588
4589CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4590 lldb::opaque_compiler_type_t type, size_t idx) {
4591 if (type) {
4592 const clang::FunctionProtoType *func =
4593 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4594 if (func) {
4595 const uint32_t num_args = func->getNumParams();
4596 if (idx < num_args)
4597 return CompilerType(getASTContext(), func->getParamType(idx));
4598 }
4599 }
4600 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004601}
4602
Greg Claytona1e5dc82015-08-11 22:53:00 +00004603CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004604ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4605 if (type) {
4606 clang::QualType qual_type(GetQualType(type));
4607 const clang::FunctionProtoType *func =
4608 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4609 if (func)
4610 return CompilerType(getASTContext(), func->getReturnType());
4611 }
4612 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004613}
4614
4615size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004616ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4617 size_t num_functions = 0;
4618 if (type) {
4619 clang::QualType qual_type(GetCanonicalQualType(type));
4620 switch (qual_type->getTypeClass()) {
4621 case clang::Type::Record:
4622 if (GetCompleteQualType(getASTContext(), qual_type)) {
4623 const clang::RecordType *record_type =
4624 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4625 const clang::RecordDecl *record_decl = record_type->getDecl();
4626 assert(record_decl);
4627 const clang::CXXRecordDecl *cxx_record_decl =
4628 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4629 if (cxx_record_decl)
4630 num_functions = std::distance(cxx_record_decl->method_begin(),
4631 cxx_record_decl->method_end());
4632 }
4633 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004634
Sean Callananf9c622a2016-09-30 18:44:43 +00004635 case clang::Type::ObjCObjectPointer: {
4636 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004637 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004638 const clang::ObjCInterfaceType *objc_interface_type =
4639 objc_class_type->getInterfaceType();
4640 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004641 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4642 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004643 clang::ObjCInterfaceDecl *class_interface_decl =
4644 objc_interface_type->getDecl();
4645 if (class_interface_decl) {
4646 num_functions = std::distance(class_interface_decl->meth_begin(),
4647 class_interface_decl->meth_end());
Greg Claytond8d4a572015-08-11 21:38:15 +00004648 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004649 }
4650 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004651 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004652
4653 case clang::Type::ObjCObject:
4654 case clang::Type::ObjCInterface:
4655 if (GetCompleteType(type)) {
4656 const clang::ObjCObjectType *objc_class_type =
4657 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4658 if (objc_class_type) {
4659 clang::ObjCInterfaceDecl *class_interface_decl =
4660 objc_class_type->getInterface();
4661 if (class_interface_decl)
4662 num_functions = std::distance(class_interface_decl->meth_begin(),
4663 class_interface_decl->meth_end());
4664 }
4665 }
4666 break;
4667
4668 case clang::Type::Typedef:
4669 return CompilerType(getASTContext(),
4670 llvm::cast<clang::TypedefType>(qual_type)
4671 ->getDecl()
4672 ->getUnderlyingType())
4673 .GetNumMemberFunctions();
4674
4675 case clang::Type::Auto:
4676 return CompilerType(
4677 getASTContext(),
4678 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4679 .GetNumMemberFunctions();
4680
4681 case clang::Type::Elaborated:
4682 return CompilerType(
4683 getASTContext(),
4684 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4685 .GetNumMemberFunctions();
4686
4687 case clang::Type::Paren:
4688 return CompilerType(getASTContext(),
4689 llvm::cast<clang::ParenType>(qual_type)->desugar())
4690 .GetNumMemberFunctions();
4691
4692 default:
4693 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004694 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004695 }
4696 return num_functions;
Greg Claytond8d4a572015-08-11 21:38:15 +00004697}
4698
4699TypeMemberFunctionImpl
Kate Stoneb9c1b512016-09-06 20:57:50 +00004700ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4701 size_t idx) {
4702 std::string name;
4703 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4704 CompilerType clang_type;
4705 CompilerDecl clang_decl;
4706 if (type) {
4707 clang::QualType qual_type(GetCanonicalQualType(type));
4708 switch (qual_type->getTypeClass()) {
4709 case clang::Type::Record:
4710 if (GetCompleteQualType(getASTContext(), qual_type)) {
4711 const clang::RecordType *record_type =
4712 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4713 const clang::RecordDecl *record_decl = record_type->getDecl();
4714 assert(record_decl);
4715 const clang::CXXRecordDecl *cxx_record_decl =
4716 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4717 if (cxx_record_decl) {
4718 auto method_iter = cxx_record_decl->method_begin();
4719 auto method_end = cxx_record_decl->method_end();
4720 if (idx <
4721 static_cast<size_t>(std::distance(method_iter, method_end))) {
4722 std::advance(method_iter, idx);
4723 clang::CXXMethodDecl *cxx_method_decl =
4724 method_iter->getCanonicalDecl();
4725 if (cxx_method_decl) {
4726 name = cxx_method_decl->getDeclName().getAsString();
4727 if (cxx_method_decl->isStatic())
4728 kind = lldb::eMemberFunctionKindStaticMethod;
4729 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4730 kind = lldb::eMemberFunctionKindConstructor;
4731 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4732 kind = lldb::eMemberFunctionKindDestructor;
4733 else
4734 kind = lldb::eMemberFunctionKindInstanceMethod;
4735 clang_type = CompilerType(
4736 this, cxx_method_decl->getType().getAsOpaquePtr());
4737 clang_decl = CompilerDecl(this, cxx_method_decl);
4738 }
4739 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004740 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004741 }
4742 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004743
Sean Callananf9c622a2016-09-30 18:44:43 +00004744 case clang::Type::ObjCObjectPointer: {
4745 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004746 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004747 const clang::ObjCInterfaceType *objc_interface_type =
4748 objc_class_type->getInterfaceType();
4749 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004750 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4751 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004752 clang::ObjCInterfaceDecl *class_interface_decl =
4753 objc_interface_type->getDecl();
4754 if (class_interface_decl) {
4755 auto method_iter = class_interface_decl->meth_begin();
4756 auto method_end = class_interface_decl->meth_end();
4757 if (idx <
4758 static_cast<size_t>(std::distance(method_iter, method_end))) {
4759 std::advance(method_iter, idx);
4760 clang::ObjCMethodDecl *objc_method_decl =
4761 method_iter->getCanonicalDecl();
4762 if (objc_method_decl) {
4763 clang_decl = CompilerDecl(this, objc_method_decl);
4764 name = objc_method_decl->getSelector().getAsString();
4765 if (objc_method_decl->isClassMethod())
4766 kind = lldb::eMemberFunctionKindStaticMethod;
4767 else
4768 kind = lldb::eMemberFunctionKindInstanceMethod;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004769 }
4770 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004771 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004772 }
4773 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004774 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004775
Kate Stoneb9c1b512016-09-06 20:57:50 +00004776 case clang::Type::ObjCObject:
4777 case clang::Type::ObjCInterface:
4778 if (GetCompleteType(type)) {
4779 const clang::ObjCObjectType *objc_class_type =
4780 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4781 if (objc_class_type) {
4782 clang::ObjCInterfaceDecl *class_interface_decl =
4783 objc_class_type->getInterface();
4784 if (class_interface_decl) {
4785 auto method_iter = class_interface_decl->meth_begin();
4786 auto method_end = class_interface_decl->meth_end();
4787 if (idx <
4788 static_cast<size_t>(std::distance(method_iter, method_end))) {
4789 std::advance(method_iter, idx);
4790 clang::ObjCMethodDecl *objc_method_decl =
4791 method_iter->getCanonicalDecl();
4792 if (objc_method_decl) {
4793 clang_decl = CompilerDecl(this, objc_method_decl);
4794 name = objc_method_decl->getSelector().getAsString();
4795 if (objc_method_decl->isClassMethod())
4796 kind = lldb::eMemberFunctionKindStaticMethod;
4797 else
4798 kind = lldb::eMemberFunctionKindInstanceMethod;
4799 }
4800 }
4801 }
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004802 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004803 }
4804 break;
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004805
Kate Stoneb9c1b512016-09-06 20:57:50 +00004806 case clang::Type::Typedef:
4807 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4808 ->getDecl()
4809 ->getUnderlyingType()
4810 .getAsOpaquePtr(),
4811 idx);
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004812
Kate Stoneb9c1b512016-09-06 20:57:50 +00004813 case clang::Type::Auto:
4814 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4815 ->getDeducedType()
4816 .getAsOpaquePtr(),
4817 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004818
Kate Stoneb9c1b512016-09-06 20:57:50 +00004819 case clang::Type::Elaborated:
4820 return GetMemberFunctionAtIndex(
4821 llvm::cast<clang::ElaboratedType>(qual_type)
4822 ->getNamedType()
4823 .getAsOpaquePtr(),
4824 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004825
Kate Stoneb9c1b512016-09-06 20:57:50 +00004826 case clang::Type::Paren:
4827 return GetMemberFunctionAtIndex(
4828 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4829 idx);
4830
4831 default:
4832 break;
Greg Clayton56939cb2015-09-17 22:23:34 +00004833 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004834 }
Greg Clayton56939cb2015-09-17 22:23:34 +00004835
Kate Stoneb9c1b512016-09-06 20:57:50 +00004836 if (kind == eMemberFunctionKindUnknown)
4837 return TypeMemberFunctionImpl();
4838 else
4839 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
Greg Clayton56939cb2015-09-17 22:23:34 +00004840}
4841
Greg Claytona1e5dc82015-08-11 22:53:00 +00004842CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004843ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4844 if (type)
4845 return CompilerType(getASTContext(),
4846 GetQualType(type).getNonReferenceType());
4847 return CompilerType();
4848}
4849
4850CompilerType ClangASTContext::CreateTypedefType(
4851 const CompilerType &type, const char *typedef_name,
4852 const CompilerDeclContext &compiler_decl_ctx) {
4853 if (type && typedef_name && typedef_name[0]) {
4854 ClangASTContext *ast =
4855 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4856 if (!ast)
4857 return CompilerType();
4858 clang::ASTContext *clang_ast = ast->getASTContext();
4859 clang::QualType qual_type(ClangUtil::GetQualType(type));
4860
4861 clang::DeclContext *decl_ctx =
4862 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4863 if (decl_ctx == nullptr)
4864 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4865
4866 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4867 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4868 &clang_ast->Idents.get(typedef_name),
4869 clang_ast->getTrivialTypeSourceInfo(qual_type));
4870
4871 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4872
Aleksandr Urakov709426b2018-09-10 08:08:43 +00004873 decl_ctx->addDecl(decl);
4874
Kate Stoneb9c1b512016-09-06 20:57:50 +00004875 // Get a uniqued clang::QualType for the typedef decl type
4876 return CompilerType(clang_ast, clang_ast->getTypedefType(decl));
4877 }
4878 return CompilerType();
4879}
4880
4881CompilerType
4882ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4883 if (type) {
4884 clang::QualType qual_type(GetQualType(type));
4885 return CompilerType(getASTContext(),
4886 qual_type.getTypePtr()->getPointeeType());
4887 }
4888 return CompilerType();
4889}
4890
4891CompilerType
4892ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4893 if (type) {
4894 clang::QualType qual_type(GetQualType(type));
4895
4896 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4897 switch (type_class) {
4898 case clang::Type::ObjCObject:
4899 case clang::Type::ObjCInterface:
4900 return CompilerType(getASTContext(),
4901 getASTContext()->getObjCObjectPointerType(qual_type));
4902
4903 default:
4904 return CompilerType(getASTContext(),
4905 getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004906 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004907 }
4908 return CompilerType();
4909}
4910
4911CompilerType
4912ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4913 if (type)
4914 return CompilerType(this, getASTContext()
4915 ->getLValueReferenceType(GetQualType(type))
4916 .getAsOpaquePtr());
4917 else
Greg Claytona1e5dc82015-08-11 22:53:00 +00004918 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004919}
4920
Kate Stoneb9c1b512016-09-06 20:57:50 +00004921CompilerType
4922ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4923 if (type)
4924 return CompilerType(this, getASTContext()
4925 ->getRValueReferenceType(GetQualType(type))
4926 .getAsOpaquePtr());
4927 else
4928 return CompilerType();
4929}
4930
4931CompilerType
4932ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4933 if (type) {
4934 clang::QualType result(GetQualType(type));
4935 result.addConst();
4936 return CompilerType(this, result.getAsOpaquePtr());
4937 }
4938 return CompilerType();
4939}
4940
4941CompilerType
4942ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4943 if (type) {
4944 clang::QualType result(GetQualType(type));
4945 result.addVolatile();
4946 return CompilerType(this, result.getAsOpaquePtr());
4947 }
4948 return CompilerType();
4949}
4950
4951CompilerType
4952ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4953 if (type) {
4954 clang::QualType result(GetQualType(type));
4955 result.addRestrict();
4956 return CompilerType(this, result.getAsOpaquePtr());
4957 }
4958 return CompilerType();
4959}
4960
4961CompilerType
4962ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4963 const char *typedef_name,
4964 const CompilerDeclContext &compiler_decl_ctx) {
4965 if (type) {
4966 clang::ASTContext *clang_ast = getASTContext();
4967 clang::QualType qual_type(GetQualType(type));
4968
4969 clang::DeclContext *decl_ctx =
4970 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4971 if (decl_ctx == nullptr)
4972 decl_ctx = getASTContext()->getTranslationUnitDecl();
4973
4974 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4975 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4976 &clang_ast->Idents.get(typedef_name),
4977 clang_ast->getTrivialTypeSourceInfo(qual_type));
4978
4979 clang::TagDecl *tdecl = nullptr;
4980 if (!qual_type.isNull()) {
4981 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4982 tdecl = rt->getDecl();
4983 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4984 tdecl = et->getDecl();
4985 }
4986
4987 // Check whether this declaration is an anonymous struct, union, or enum,
Adrian Prantl05097242018-04-30 16:49:04 +00004988 // hidden behind a typedef. If so, we try to check whether we have a
4989 // typedef tag to attach to the original record declaration
Kate Stoneb9c1b512016-09-06 20:57:50 +00004990 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4991 tdecl->setTypedefNameForAnonDecl(decl);
4992
4993 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4994
4995 // Get a uniqued clang::QualType for the typedef decl type
4996 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4997 }
4998 return CompilerType();
4999}
5000
5001CompilerType
5002ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
5003 if (type) {
5004 const clang::TypedefType *typedef_type =
5005 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
5006 if (typedef_type)
5007 return CompilerType(getASTContext(),
5008 typedef_type->getDecl()->getUnderlyingType());
5009 }
5010 return CompilerType();
5011}
Greg Claytond8d4a572015-08-11 21:38:15 +00005012
Greg Claytond8d4a572015-08-11 21:38:15 +00005013// Create related types using the current type's AST
Greg Claytond8d4a572015-08-11 21:38:15 +00005014
Kate Stoneb9c1b512016-09-06 20:57:50 +00005015CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
5016 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005017}
Greg Claytond8d4a572015-08-11 21:38:15 +00005018// Exploring the type
Greg Claytond8d4a572015-08-11 21:38:15 +00005019
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005020Optional<uint64_t>
5021ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
5022 ExecutionContextScope *exe_scope) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005023 if (GetCompleteType(type)) {
Greg Claytond8d4a572015-08-11 21:38:15 +00005024 clang::QualType qual_type(GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00005025 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005026 switch (type_class) {
5027 case clang::Type::Record:
5028 if (GetCompleteType(type))
5029 return getASTContext()->getTypeSize(qual_type);
5030 else
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005031 return None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005032 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00005033
Kate Stoneb9c1b512016-09-06 20:57:50 +00005034 case clang::Type::ObjCInterface:
5035 case clang::Type::ObjCObject: {
5036 ExecutionContext exe_ctx(exe_scope);
5037 Process *process = exe_ctx.GetProcessPtr();
5038 if (process) {
Alex Langforde823bbe2019-06-10 20:53:23 +00005039 ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(*process);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005040 if (objc_runtime) {
5041 uint64_t bit_size = 0;
5042 if (objc_runtime->GetTypeBitSize(
5043 CompilerType(getASTContext(), qual_type), bit_size))
5044 return bit_size;
5045 }
5046 } else {
5047 static bool g_printed = false;
5048 if (!g_printed) {
5049 StreamString s;
5050 DumpTypeDescription(type, &s);
5051
5052 llvm::outs() << "warning: trying to determine the size of type ";
5053 llvm::outs() << s.GetString() << "\n";
5054 llvm::outs() << "without a valid ExecutionContext. this is not "
5055 "reliable. please file a bug against LLDB.\n";
5056 llvm::outs() << "backtrace:\n";
5057 llvm::sys::PrintStackTrace(llvm::outs());
5058 llvm::outs() << "\n";
5059 g_printed = true;
5060 }
5061 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005062 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005063 LLVM_FALLTHROUGH;
5064 default:
5065 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
5066 if (bit_size == 0) {
5067 if (qual_type->isIncompleteArrayType())
5068 return getASTContext()->getTypeSize(
5069 qual_type->getArrayElementTypeNoTypeQual()
5070 ->getCanonicalTypeUnqualified());
5071 }
5072 if (qual_type->isObjCObjectOrInterfaceType())
5073 return bit_size +
5074 getASTContext()->getTypeSize(
5075 getASTContext()->ObjCBuiltinClassTy);
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005076 // Function types actually have a size of 0, that's not an error.
5077 if (qual_type->isFunctionProtoType())
5078 return bit_size;
5079 if (bit_size)
5080 return bit_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005081 }
5082 }
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005083 return None;
Greg Claytond8d4a572015-08-11 21:38:15 +00005084}
5085
Kate Stoneb9c1b512016-09-06 20:57:50 +00005086size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
5087 if (GetCompleteType(type))
5088 return getASTContext()->getTypeAlign(GetQualType(type));
5089 return 0;
5090}
5091
5092lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
5093 uint64_t &count) {
5094 if (!type)
5095 return lldb::eEncodingInvalid;
5096
5097 count = 1;
5098 clang::QualType qual_type(GetCanonicalQualType(type));
5099
5100 switch (qual_type->getTypeClass()) {
5101 case clang::Type::UnaryTransform:
5102 break;
5103
5104 case clang::Type::FunctionNoProto:
5105 case clang::Type::FunctionProto:
5106 break;
5107
5108 case clang::Type::IncompleteArray:
5109 case clang::Type::VariableArray:
5110 break;
5111
5112 case clang::Type::ConstantArray:
5113 break;
5114
Fangrui Song8f284882018-07-13 22:40:40 +00005115 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005116 case clang::Type::ExtVector:
5117 case clang::Type::Vector:
5118 // TODO: Set this to more than one???
5119 break;
5120
5121 case clang::Type::Builtin:
5122 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5123 case clang::BuiltinType::Void:
5124 break;
5125
5126 case clang::BuiltinType::Bool:
5127 case clang::BuiltinType::Char_S:
5128 case clang::BuiltinType::SChar:
5129 case clang::BuiltinType::WChar_S:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005130 case clang::BuiltinType::Short:
5131 case clang::BuiltinType::Int:
5132 case clang::BuiltinType::Long:
5133 case clang::BuiltinType::LongLong:
5134 case clang::BuiltinType::Int128:
5135 return lldb::eEncodingSint;
5136
5137 case clang::BuiltinType::Char_U:
5138 case clang::BuiltinType::UChar:
5139 case clang::BuiltinType::WChar_U:
Richard Smith51d12d82018-05-02 02:43:22 +00005140 case clang::BuiltinType::Char8:
5141 case clang::BuiltinType::Char16:
5142 case clang::BuiltinType::Char32:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005143 case clang::BuiltinType::UShort:
5144 case clang::BuiltinType::UInt:
5145 case clang::BuiltinType::ULong:
5146 case clang::BuiltinType::ULongLong:
5147 case clang::BuiltinType::UInt128:
5148 return lldb::eEncodingUint;
5149
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005150 // Fixed point types. Note that they are currently ignored.
5151 case clang::BuiltinType::ShortAccum:
5152 case clang::BuiltinType::Accum:
5153 case clang::BuiltinType::LongAccum:
5154 case clang::BuiltinType::UShortAccum:
5155 case clang::BuiltinType::UAccum:
5156 case clang::BuiltinType::ULongAccum:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005157 case clang::BuiltinType::ShortFract:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005158 case clang::BuiltinType::Fract:
5159 case clang::BuiltinType::LongFract:
5160 case clang::BuiltinType::UShortFract:
5161 case clang::BuiltinType::UFract:
5162 case clang::BuiltinType::ULongFract:
5163 case clang::BuiltinType::SatShortAccum:
5164 case clang::BuiltinType::SatAccum:
5165 case clang::BuiltinType::SatLongAccum:
5166 case clang::BuiltinType::SatUShortAccum:
5167 case clang::BuiltinType::SatUAccum:
5168 case clang::BuiltinType::SatULongAccum:
5169 case clang::BuiltinType::SatShortFract:
5170 case clang::BuiltinType::SatFract:
5171 case clang::BuiltinType::SatLongFract:
5172 case clang::BuiltinType::SatUShortFract:
5173 case clang::BuiltinType::SatUFract:
5174 case clang::BuiltinType::SatULongFract:
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005175 break;
5176
Kate Stoneb9c1b512016-09-06 20:57:50 +00005177 case clang::BuiltinType::Half:
5178 case clang::BuiltinType::Float:
Ted Woodward4355c7c2017-09-20 19:16:53 +00005179 case clang::BuiltinType::Float16:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005180 case clang::BuiltinType::Float128:
5181 case clang::BuiltinType::Double:
5182 case clang::BuiltinType::LongDouble:
5183 return lldb::eEncodingIEEE754;
5184
5185 case clang::BuiltinType::ObjCClass:
5186 case clang::BuiltinType::ObjCId:
5187 case clang::BuiltinType::ObjCSel:
5188 return lldb::eEncodingUint;
5189
5190 case clang::BuiltinType::NullPtr:
5191 return lldb::eEncodingUint;
5192
5193 case clang::BuiltinType::Kind::ARCUnbridgedCast:
5194 case clang::BuiltinType::Kind::BoundMember:
5195 case clang::BuiltinType::Kind::BuiltinFn:
5196 case clang::BuiltinType::Kind::Dependent:
5197 case clang::BuiltinType::Kind::OCLClkEvent:
5198 case clang::BuiltinType::Kind::OCLEvent:
5199 case clang::BuiltinType::Kind::OCLImage1dRO:
5200 case clang::BuiltinType::Kind::OCLImage1dWO:
5201 case clang::BuiltinType::Kind::OCLImage1dRW:
5202 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
5203 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
5204 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
5205 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
5206 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
5207 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
5208 case clang::BuiltinType::Kind::OCLImage2dRO:
5209 case clang::BuiltinType::Kind::OCLImage2dWO:
5210 case clang::BuiltinType::Kind::OCLImage2dRW:
5211 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
5212 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
5213 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
5214 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
5215 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
5216 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
5217 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5218 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5219 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5220 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5221 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5222 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5223 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5224 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5225 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5226 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5227 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5228 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5229 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5230 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5231 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5232 case clang::BuiltinType::Kind::OCLImage3dRO:
5233 case clang::BuiltinType::Kind::OCLImage3dWO:
5234 case clang::BuiltinType::Kind::OCLImage3dRW:
5235 case clang::BuiltinType::Kind::OCLQueue:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005236 case clang::BuiltinType::Kind::OCLReserveID:
5237 case clang::BuiltinType::Kind::OCLSampler:
5238 case clang::BuiltinType::Kind::OMPArraySection:
5239 case clang::BuiltinType::Kind::Overload:
5240 case clang::BuiltinType::Kind::PseudoObject:
5241 case clang::BuiltinType::Kind::UnknownAny:
5242 break;
Jorge Gorbe Moyaa6e6c182018-11-08 22:04:58 +00005243
5244 case clang::BuiltinType::OCLIntelSubgroupAVCMcePayload:
5245 case clang::BuiltinType::OCLIntelSubgroupAVCImePayload:
5246 case clang::BuiltinType::OCLIntelSubgroupAVCRefPayload:
5247 case clang::BuiltinType::OCLIntelSubgroupAVCSicPayload:
5248 case clang::BuiltinType::OCLIntelSubgroupAVCMceResult:
5249 case clang::BuiltinType::OCLIntelSubgroupAVCImeResult:
5250 case clang::BuiltinType::OCLIntelSubgroupAVCRefResult:
5251 case clang::BuiltinType::OCLIntelSubgroupAVCSicResult:
5252 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultSingleRefStreamout:
5253 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultDualRefStreamout:
5254 case clang::BuiltinType::OCLIntelSubgroupAVCImeSingleRefStreamin:
5255 case clang::BuiltinType::OCLIntelSubgroupAVCImeDualRefStreamin:
5256 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005257 }
5258 break;
Adrian Prantl05097242018-04-30 16:49:04 +00005259 // All pointer types are represented as unsigned integer encodings. We may
5260 // nee to add a eEncodingPointer if we ever need to know the difference
Kate Stoneb9c1b512016-09-06 20:57:50 +00005261 case clang::Type::ObjCObjectPointer:
5262 case clang::Type::BlockPointer:
5263 case clang::Type::Pointer:
5264 case clang::Type::LValueReference:
5265 case clang::Type::RValueReference:
5266 case clang::Type::MemberPointer:
5267 return lldb::eEncodingUint;
5268 case clang::Type::Complex: {
5269 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5270 if (qual_type->isComplexType())
5271 encoding = lldb::eEncodingIEEE754;
5272 else {
5273 const clang::ComplexType *complex_type =
5274 qual_type->getAsComplexIntegerType();
5275 if (complex_type)
5276 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5277 .GetEncoding(count);
5278 else
5279 encoding = lldb::eEncodingSint;
5280 }
5281 count = 2;
5282 return encoding;
5283 }
5284
5285 case clang::Type::ObjCInterface:
5286 break;
5287 case clang::Type::Record:
5288 break;
5289 case clang::Type::Enum:
5290 return lldb::eEncodingSint;
5291 case clang::Type::Typedef:
5292 return CompilerType(getASTContext(),
5293 llvm::cast<clang::TypedefType>(qual_type)
5294 ->getDecl()
5295 ->getUnderlyingType())
5296 .GetEncoding(count);
5297
5298 case clang::Type::Auto:
5299 return CompilerType(
5300 getASTContext(),
5301 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5302 .GetEncoding(count);
5303
5304 case clang::Type::Elaborated:
5305 return CompilerType(
5306 getASTContext(),
5307 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5308 .GetEncoding(count);
5309
5310 case clang::Type::Paren:
5311 return CompilerType(getASTContext(),
5312 llvm::cast<clang::ParenType>(qual_type)->desugar())
5313 .GetEncoding(count);
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005314 case clang::Type::TypeOfExpr:
5315 return CompilerType(getASTContext(),
5316 llvm::cast<clang::TypeOfExprType>(qual_type)
5317 ->getUnderlyingExpr()
5318 ->getType())
5319 .GetEncoding(count);
5320 case clang::Type::TypeOf:
5321 return CompilerType(
5322 getASTContext(),
5323 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5324 .GetEncoding(count);
5325 case clang::Type::Decltype:
5326 return CompilerType(
5327 getASTContext(),
5328 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5329 .GetEncoding(count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005330 case clang::Type::DependentSizedArray:
5331 case clang::Type::DependentSizedExtVector:
5332 case clang::Type::UnresolvedUsing:
5333 case clang::Type::Attributed:
5334 case clang::Type::TemplateTypeParm:
5335 case clang::Type::SubstTemplateTypeParm:
5336 case clang::Type::SubstTemplateTypeParmPack:
5337 case clang::Type::InjectedClassName:
5338 case clang::Type::DependentName:
5339 case clang::Type::DependentTemplateSpecialization:
5340 case clang::Type::PackExpansion:
5341 case clang::Type::ObjCObject:
5342
Kate Stoneb9c1b512016-09-06 20:57:50 +00005343 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005344 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005345 case clang::Type::Atomic:
5346 case clang::Type::Adjusted:
5347 case clang::Type::Pipe:
5348 break;
5349
5350 // pointer type decayed from an array or function type.
5351 case clang::Type::Decayed:
5352 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005353 case clang::Type::ObjCTypeParam:
5354 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005355
5356 case clang::Type::DependentAddressSpace:
5357 break;
Krasimir Georgiev435e76a2019-05-07 13:59:30 +00005358 case clang::Type::MacroQualified:
5359 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005360 }
5361 count = 0;
5362 return lldb::eEncodingInvalid;
5363}
5364
5365lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5366 if (!type)
5367 return lldb::eFormatDefault;
5368
5369 clang::QualType qual_type(GetCanonicalQualType(type));
5370
5371 switch (qual_type->getTypeClass()) {
5372 case clang::Type::UnaryTransform:
5373 break;
5374
5375 case clang::Type::FunctionNoProto:
5376 case clang::Type::FunctionProto:
5377 break;
5378
5379 case clang::Type::IncompleteArray:
5380 case clang::Type::VariableArray:
5381 break;
5382
5383 case clang::Type::ConstantArray:
5384 return lldb::eFormatVoid; // no value
5385
Fangrui Song8f284882018-07-13 22:40:40 +00005386 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005387 case clang::Type::ExtVector:
5388 case clang::Type::Vector:
5389 break;
5390
5391 case clang::Type::Builtin:
5392 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5393 // default: assert(0 && "Unknown builtin type!");
5394 case clang::BuiltinType::UnknownAny:
5395 case clang::BuiltinType::Void:
5396 case clang::BuiltinType::BoundMember:
5397 break;
5398
5399 case clang::BuiltinType::Bool:
5400 return lldb::eFormatBoolean;
5401 case clang::BuiltinType::Char_S:
5402 case clang::BuiltinType::SChar:
5403 case clang::BuiltinType::WChar_S:
5404 case clang::BuiltinType::Char_U:
5405 case clang::BuiltinType::UChar:
5406 case clang::BuiltinType::WChar_U:
5407 return lldb::eFormatChar;
5408 case clang::BuiltinType::Char16:
5409 return lldb::eFormatUnicode16;
5410 case clang::BuiltinType::Char32:
5411 return lldb::eFormatUnicode32;
5412 case clang::BuiltinType::UShort:
5413 return lldb::eFormatUnsigned;
5414 case clang::BuiltinType::Short:
5415 return lldb::eFormatDecimal;
5416 case clang::BuiltinType::UInt:
5417 return lldb::eFormatUnsigned;
5418 case clang::BuiltinType::Int:
5419 return lldb::eFormatDecimal;
5420 case clang::BuiltinType::ULong:
5421 return lldb::eFormatUnsigned;
5422 case clang::BuiltinType::Long:
5423 return lldb::eFormatDecimal;
5424 case clang::BuiltinType::ULongLong:
5425 return lldb::eFormatUnsigned;
5426 case clang::BuiltinType::LongLong:
5427 return lldb::eFormatDecimal;
5428 case clang::BuiltinType::UInt128:
5429 return lldb::eFormatUnsigned;
5430 case clang::BuiltinType::Int128:
5431 return lldb::eFormatDecimal;
5432 case clang::BuiltinType::Half:
5433 case clang::BuiltinType::Float:
5434 case clang::BuiltinType::Double:
5435 case clang::BuiltinType::LongDouble:
5436 return lldb::eFormatFloat;
5437 default:
5438 return lldb::eFormatHex;
5439 }
5440 break;
5441 case clang::Type::ObjCObjectPointer:
5442 return lldb::eFormatHex;
5443 case clang::Type::BlockPointer:
5444 return lldb::eFormatHex;
5445 case clang::Type::Pointer:
5446 return lldb::eFormatHex;
5447 case clang::Type::LValueReference:
5448 case clang::Type::RValueReference:
5449 return lldb::eFormatHex;
5450 case clang::Type::MemberPointer:
5451 break;
5452 case clang::Type::Complex: {
5453 if (qual_type->isComplexType())
5454 return lldb::eFormatComplex;
5455 else
5456 return lldb::eFormatComplexInteger;
5457 }
5458 case clang::Type::ObjCInterface:
5459 break;
5460 case clang::Type::Record:
5461 break;
5462 case clang::Type::Enum:
5463 return lldb::eFormatEnum;
5464 case clang::Type::Typedef:
5465 return CompilerType(getASTContext(),
5466 llvm::cast<clang::TypedefType>(qual_type)
5467 ->getDecl()
5468 ->getUnderlyingType())
5469 .GetFormat();
5470 case clang::Type::Auto:
5471 return CompilerType(getASTContext(),
5472 llvm::cast<clang::AutoType>(qual_type)->desugar())
5473 .GetFormat();
5474 case clang::Type::Paren:
5475 return CompilerType(getASTContext(),
5476 llvm::cast<clang::ParenType>(qual_type)->desugar())
5477 .GetFormat();
5478 case clang::Type::Elaborated:
5479 return CompilerType(
5480 getASTContext(),
5481 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5482 .GetFormat();
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005483 case clang::Type::TypeOfExpr:
5484 return CompilerType(getASTContext(),
5485 llvm::cast<clang::TypeOfExprType>(qual_type)
5486 ->getUnderlyingExpr()
5487 ->getType())
5488 .GetFormat();
5489 case clang::Type::TypeOf:
5490 return CompilerType(
5491 getASTContext(),
5492 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5493 .GetFormat();
5494 case clang::Type::Decltype:
5495 return CompilerType(
5496 getASTContext(),
5497 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5498 .GetFormat();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005499 case clang::Type::DependentSizedArray:
5500 case clang::Type::DependentSizedExtVector:
5501 case clang::Type::UnresolvedUsing:
5502 case clang::Type::Attributed:
5503 case clang::Type::TemplateTypeParm:
5504 case clang::Type::SubstTemplateTypeParm:
5505 case clang::Type::SubstTemplateTypeParmPack:
5506 case clang::Type::InjectedClassName:
5507 case clang::Type::DependentName:
5508 case clang::Type::DependentTemplateSpecialization:
5509 case clang::Type::PackExpansion:
5510 case clang::Type::ObjCObject:
5511
Kate Stoneb9c1b512016-09-06 20:57:50 +00005512 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005513 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005514 case clang::Type::Atomic:
5515 case clang::Type::Adjusted:
5516 case clang::Type::Pipe:
5517 break;
5518
5519 // pointer type decayed from an array or function type.
5520 case clang::Type::Decayed:
5521 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005522 case clang::Type::ObjCTypeParam:
5523 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005524
5525 case clang::Type::DependentAddressSpace:
5526 break;
Krasimir Georgiev435e76a2019-05-07 13:59:30 +00005527 case clang::Type::MacroQualified:
5528 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005529 }
5530 // We don't know hot to display this type...
5531 return lldb::eFormatBytes;
5532}
5533
5534static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5535 bool check_superclass) {
5536 while (class_interface_decl) {
5537 if (class_interface_decl->ivar_size() > 0)
5538 return true;
5539
5540 if (check_superclass)
5541 class_interface_decl = class_interface_decl->getSuperClass();
5542 else
5543 break;
5544 }
5545 return false;
5546}
5547
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00005548static Optional<SymbolFile::ArrayInfo>
Adrian Prantleca07c52018-11-05 20:49:07 +00005549GetDynamicArrayInfo(ClangASTContext &ast, SymbolFile *sym_file,
5550 clang::QualType qual_type,
5551 const ExecutionContext *exe_ctx) {
5552 if (qual_type->isIncompleteArrayType())
5553 if (auto *metadata = ast.GetMetadata(qual_type.getAsOpaquePtr()))
Pavel Labathffec31e2018-12-28 13:34:44 +00005554 return sym_file->GetDynamicArrayInfoForUID(metadata->GetUserID(),
5555 exe_ctx);
Adrian Prantleca07c52018-11-05 20:49:07 +00005556 return llvm::None;
5557}
5558
Kate Stoneb9c1b512016-09-06 20:57:50 +00005559uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
Adrian Prantleca07c52018-11-05 20:49:07 +00005560 bool omit_empty_base_classes,
5561 const ExecutionContext *exe_ctx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005562 if (!type)
5563 return 0;
5564
5565 uint32_t num_children = 0;
5566 clang::QualType qual_type(GetQualType(type));
5567 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5568 switch (type_class) {
5569 case clang::Type::Builtin:
5570 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5571 case clang::BuiltinType::ObjCId: // child is Class
5572 case clang::BuiltinType::ObjCClass: // child is Class
5573 num_children = 1;
5574 break;
5575
5576 default:
5577 break;
5578 }
5579 break;
5580
5581 case clang::Type::Complex:
5582 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005583 case clang::Type::Record:
5584 if (GetCompleteQualType(getASTContext(), qual_type)) {
5585 const clang::RecordType *record_type =
5586 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5587 const clang::RecordDecl *record_decl = record_type->getDecl();
5588 assert(record_decl);
5589 const clang::CXXRecordDecl *cxx_record_decl =
5590 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5591 if (cxx_record_decl) {
5592 if (omit_empty_base_classes) {
Adrian Prantl05097242018-04-30 16:49:04 +00005593 // Check each base classes to see if it or any of its base classes
5594 // contain any fields. This can help limit the noise in variable
5595 // views by not having to show base classes that contain no members.
Kate Stoneb9c1b512016-09-06 20:57:50 +00005596 clang::CXXRecordDecl::base_class_const_iterator base_class,
5597 base_class_end;
5598 for (base_class = cxx_record_decl->bases_begin(),
5599 base_class_end = cxx_record_decl->bases_end();
5600 base_class != base_class_end; ++base_class) {
5601 const clang::CXXRecordDecl *base_class_decl =
5602 llvm::cast<clang::CXXRecordDecl>(
5603 base_class->getType()
5604 ->getAs<clang::RecordType>()
5605 ->getDecl());
5606
5607 // Skip empty base classes
Jonas Devliegherea6682a42018-12-15 00:15:33 +00005608 if (!ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00005609 continue;
5610
5611 num_children++;
5612 }
5613 } else {
5614 // Include all base classes
5615 num_children += cxx_record_decl->getNumBases();
5616 }
5617 }
5618 clang::RecordDecl::field_iterator field, field_end;
5619 for (field = record_decl->field_begin(),
5620 field_end = record_decl->field_end();
5621 field != field_end; ++field)
5622 ++num_children;
5623 }
5624 break;
5625
5626 case clang::Type::ObjCObject:
5627 case clang::Type::ObjCInterface:
5628 if (GetCompleteQualType(getASTContext(), qual_type)) {
5629 const clang::ObjCObjectType *objc_class_type =
5630 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5631 assert(objc_class_type);
5632 if (objc_class_type) {
5633 clang::ObjCInterfaceDecl *class_interface_decl =
5634 objc_class_type->getInterface();
5635
5636 if (class_interface_decl) {
5637
5638 clang::ObjCInterfaceDecl *superclass_interface_decl =
5639 class_interface_decl->getSuperClass();
5640 if (superclass_interface_decl) {
5641 if (omit_empty_base_classes) {
5642 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5643 ++num_children;
5644 } else
5645 ++num_children;
5646 }
5647
5648 num_children += class_interface_decl->ivar_size();
5649 }
5650 }
5651 }
5652 break;
5653
5654 case clang::Type::ObjCObjectPointer: {
5655 const clang::ObjCObjectPointerType *pointer_type =
5656 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5657 clang::QualType pointee_type = pointer_type->getPointeeType();
5658 uint32_t num_pointee_children =
5659 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005660 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005661 // If this type points to a simple type, then it has 1 child
5662 if (num_pointee_children == 0)
5663 num_children = 1;
5664 else
5665 num_children = num_pointee_children;
5666 } break;
5667
5668 case clang::Type::Vector:
5669 case clang::Type::ExtVector:
5670 num_children =
5671 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5672 break;
5673
5674 case clang::Type::ConstantArray:
5675 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5676 ->getSize()
5677 .getLimitedValue();
5678 break;
Adrian Prantleca07c52018-11-05 20:49:07 +00005679 case clang::Type::IncompleteArray:
5680 if (auto array_info =
5681 GetDynamicArrayInfo(*this, GetSymbolFile(), qual_type, exe_ctx))
5682 // Only 1-dimensional arrays are supported.
5683 num_children = array_info->element_orders.size()
5684 ? array_info->element_orders.back()
5685 : 0;
5686 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005687
5688 case clang::Type::Pointer: {
5689 const clang::PointerType *pointer_type =
5690 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5691 clang::QualType pointee_type(pointer_type->getPointeeType());
5692 uint32_t num_pointee_children =
5693 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005694 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005695 if (num_pointee_children == 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00005696 // We have a pointer to a pointee type that claims it has no children. We
5697 // will want to look at
Kate Stoneb9c1b512016-09-06 20:57:50 +00005698 num_children = GetNumPointeeChildren(pointee_type);
5699 } else
5700 num_children = num_pointee_children;
5701 } break;
5702
5703 case clang::Type::LValueReference:
5704 case clang::Type::RValueReference: {
5705 const clang::ReferenceType *reference_type =
5706 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5707 clang::QualType pointee_type = reference_type->getPointeeType();
5708 uint32_t num_pointee_children =
5709 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005710 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005711 // If this type points to a simple type, then it has 1 child
5712 if (num_pointee_children == 0)
5713 num_children = 1;
5714 else
5715 num_children = num_pointee_children;
5716 } break;
5717
5718 case clang::Type::Typedef:
5719 num_children =
5720 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5721 ->getDecl()
5722 ->getUnderlyingType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005723 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005724 break;
5725
5726 case clang::Type::Auto:
5727 num_children =
5728 CompilerType(getASTContext(),
5729 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005730 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005731 break;
5732
5733 case clang::Type::Elaborated:
5734 num_children =
5735 CompilerType(
5736 getASTContext(),
5737 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005738 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005739 break;
5740
5741 case clang::Type::Paren:
5742 num_children =
5743 CompilerType(getASTContext(),
5744 llvm::cast<clang::ParenType>(qual_type)->desugar())
Adrian Prantleca07c52018-11-05 20:49:07 +00005745 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005746 break;
5747 default:
5748 break;
5749 }
5750 return num_children;
5751}
5752
Adrian Prantl0e4c4822019-03-06 21:22:25 +00005753CompilerType ClangASTContext::GetBuiltinTypeByName(ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005754 return GetBasicType(GetBasicTypeEnumeration(name));
Greg Clayton56939cb2015-09-17 22:23:34 +00005755}
5756
Greg Claytond8d4a572015-08-11 21:38:15 +00005757lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00005758ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5759 if (type) {
5760 clang::QualType qual_type(GetQualType(type));
5761 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5762 if (type_class == clang::Type::Builtin) {
5763 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5764 case clang::BuiltinType::Void:
5765 return eBasicTypeVoid;
5766 case clang::BuiltinType::Bool:
5767 return eBasicTypeBool;
5768 case clang::BuiltinType::Char_S:
5769 return eBasicTypeSignedChar;
5770 case clang::BuiltinType::Char_U:
5771 return eBasicTypeUnsignedChar;
5772 case clang::BuiltinType::Char16:
5773 return eBasicTypeChar16;
5774 case clang::BuiltinType::Char32:
5775 return eBasicTypeChar32;
5776 case clang::BuiltinType::UChar:
5777 return eBasicTypeUnsignedChar;
5778 case clang::BuiltinType::SChar:
5779 return eBasicTypeSignedChar;
5780 case clang::BuiltinType::WChar_S:
5781 return eBasicTypeSignedWChar;
5782 case clang::BuiltinType::WChar_U:
5783 return eBasicTypeUnsignedWChar;
5784 case clang::BuiltinType::Short:
5785 return eBasicTypeShort;
5786 case clang::BuiltinType::UShort:
5787 return eBasicTypeUnsignedShort;
5788 case clang::BuiltinType::Int:
5789 return eBasicTypeInt;
5790 case clang::BuiltinType::UInt:
5791 return eBasicTypeUnsignedInt;
5792 case clang::BuiltinType::Long:
5793 return eBasicTypeLong;
5794 case clang::BuiltinType::ULong:
5795 return eBasicTypeUnsignedLong;
5796 case clang::BuiltinType::LongLong:
5797 return eBasicTypeLongLong;
5798 case clang::BuiltinType::ULongLong:
5799 return eBasicTypeUnsignedLongLong;
5800 case clang::BuiltinType::Int128:
5801 return eBasicTypeInt128;
5802 case clang::BuiltinType::UInt128:
5803 return eBasicTypeUnsignedInt128;
5804
5805 case clang::BuiltinType::Half:
5806 return eBasicTypeHalf;
5807 case clang::BuiltinType::Float:
5808 return eBasicTypeFloat;
5809 case clang::BuiltinType::Double:
5810 return eBasicTypeDouble;
5811 case clang::BuiltinType::LongDouble:
5812 return eBasicTypeLongDouble;
5813
5814 case clang::BuiltinType::NullPtr:
5815 return eBasicTypeNullPtr;
5816 case clang::BuiltinType::ObjCId:
5817 return eBasicTypeObjCID;
5818 case clang::BuiltinType::ObjCClass:
5819 return eBasicTypeObjCClass;
5820 case clang::BuiltinType::ObjCSel:
5821 return eBasicTypeObjCSel;
5822 default:
5823 return eBasicTypeOther;
5824 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005825 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005826 }
5827 return eBasicTypeInvalid;
Greg Claytond8d4a572015-08-11 21:38:15 +00005828}
5829
Kate Stoneb9c1b512016-09-06 20:57:50 +00005830void ClangASTContext::ForEachEnumerator(
5831 lldb::opaque_compiler_type_t type,
5832 std::function<bool(const CompilerType &integer_type,
Adrian Prantl0e4c4822019-03-06 21:22:25 +00005833 ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00005834 const llvm::APSInt &value)> const &callback) {
5835 const clang::EnumType *enum_type =
5836 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5837 if (enum_type) {
5838 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5839 if (enum_decl) {
5840 CompilerType integer_type(this,
5841 enum_decl->getIntegerType().getAsOpaquePtr());
Greg Clayton99558cc42015-08-24 23:46:31 +00005842
Kate Stoneb9c1b512016-09-06 20:57:50 +00005843 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5844 for (enum_pos = enum_decl->enumerator_begin(),
5845 enum_end_pos = enum_decl->enumerator_end();
5846 enum_pos != enum_end_pos; ++enum_pos) {
5847 ConstString name(enum_pos->getNameAsString().c_str());
5848 if (!callback(integer_type, name, enum_pos->getInitVal()))
5849 break;
5850 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005851 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005852 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005853}
5854
Greg Claytond8d4a572015-08-11 21:38:15 +00005855#pragma mark Aggregate Types
5856
Kate Stoneb9c1b512016-09-06 20:57:50 +00005857uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5858 if (!type)
5859 return 0;
Enrico Granata36f51e42015-12-18 22:41:25 +00005860
Kate Stoneb9c1b512016-09-06 20:57:50 +00005861 uint32_t count = 0;
5862 clang::QualType qual_type(GetCanonicalQualType(type));
5863 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5864 switch (type_class) {
5865 case clang::Type::Record:
5866 if (GetCompleteType(type)) {
5867 const clang::RecordType *record_type =
5868 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5869 if (record_type) {
5870 clang::RecordDecl *record_decl = record_type->getDecl();
5871 if (record_decl) {
5872 uint32_t field_idx = 0;
5873 clang::RecordDecl::field_iterator field, field_end;
5874 for (field = record_decl->field_begin(),
5875 field_end = record_decl->field_end();
5876 field != field_end; ++field)
5877 ++field_idx;
5878 count = field_idx;
5879 }
5880 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005881 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005882 break;
5883
5884 case clang::Type::Typedef:
5885 count =
5886 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5887 ->getDecl()
5888 ->getUnderlyingType())
5889 .GetNumFields();
5890 break;
5891
5892 case clang::Type::Auto:
5893 count =
5894 CompilerType(getASTContext(),
5895 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5896 .GetNumFields();
5897 break;
5898
5899 case clang::Type::Elaborated:
5900 count = CompilerType(
5901 getASTContext(),
5902 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5903 .GetNumFields();
5904 break;
5905
5906 case clang::Type::Paren:
5907 count = CompilerType(getASTContext(),
5908 llvm::cast<clang::ParenType>(qual_type)->desugar())
5909 .GetNumFields();
5910 break;
5911
Sean Callananf9c622a2016-09-30 18:44:43 +00005912 case clang::Type::ObjCObjectPointer: {
5913 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005914 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005915 const clang::ObjCInterfaceType *objc_interface_type =
5916 objc_class_type->getInterfaceType();
5917 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005918 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5919 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005920 clang::ObjCInterfaceDecl *class_interface_decl =
5921 objc_interface_type->getDecl();
5922 if (class_interface_decl) {
5923 count = class_interface_decl->ivar_size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005924 }
5925 }
5926 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005927 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005928
5929 case clang::Type::ObjCObject:
5930 case clang::Type::ObjCInterface:
5931 if (GetCompleteType(type)) {
5932 const clang::ObjCObjectType *objc_class_type =
5933 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5934 if (objc_class_type) {
5935 clang::ObjCInterfaceDecl *class_interface_decl =
5936 objc_class_type->getInterface();
5937
5938 if (class_interface_decl)
5939 count = class_interface_decl->ivar_size();
5940 }
5941 }
5942 break;
5943
5944 default:
5945 break;
5946 }
5947 return count;
Greg Claytond8d4a572015-08-11 21:38:15 +00005948}
5949
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005950static lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005951GetObjCFieldAtIndex(clang::ASTContext *ast,
5952 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5953 std::string &name, uint64_t *bit_offset_ptr,
5954 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5955 if (class_interface_decl) {
5956 if (idx < (class_interface_decl->ivar_size())) {
5957 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5958 ivar_end = class_interface_decl->ivar_end();
5959 uint32_t ivar_idx = 0;
5960
5961 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5962 ++ivar_pos, ++ivar_idx) {
5963 if (ivar_idx == idx) {
5964 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5965
5966 clang::QualType ivar_qual_type(ivar_decl->getType());
5967
5968 name.assign(ivar_decl->getNameAsString());
5969
5970 if (bit_offset_ptr) {
5971 const clang::ASTRecordLayout &interface_layout =
5972 ast->getASTObjCInterfaceLayout(class_interface_decl);
5973 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5974 }
5975
5976 const bool is_bitfield = ivar_pos->isBitField();
5977
5978 if (bitfield_bit_size_ptr) {
5979 *bitfield_bit_size_ptr = 0;
5980
5981 if (is_bitfield && ast) {
5982 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
Hans Wennborg30ce9622018-11-28 14:30:18 +00005983 clang::Expr::EvalResult result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005984 if (bitfield_bit_size_expr &&
Hans Wennborg30ce9622018-11-28 14:30:18 +00005985 bitfield_bit_size_expr->EvaluateAsInt(result, *ast)) {
5986 llvm::APSInt bitfield_apsint = result.Val.getInt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005987 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5988 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005989 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005990 }
5991 if (is_bitfield_ptr)
5992 *is_bitfield_ptr = is_bitfield;
5993
5994 return ivar_qual_type.getAsOpaquePtr();
Greg Claytond8d4a572015-08-11 21:38:15 +00005995 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005996 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005997 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005998 }
5999 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00006000}
6001
Kate Stoneb9c1b512016-09-06 20:57:50 +00006002CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
6003 size_t idx, std::string &name,
6004 uint64_t *bit_offset_ptr,
6005 uint32_t *bitfield_bit_size_ptr,
6006 bool *is_bitfield_ptr) {
6007 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006008 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006009
6010 clang::QualType qual_type(GetCanonicalQualType(type));
6011 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6012 switch (type_class) {
6013 case clang::Type::Record:
6014 if (GetCompleteType(type)) {
6015 const clang::RecordType *record_type =
6016 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6017 const clang::RecordDecl *record_decl = record_type->getDecl();
6018 uint32_t field_idx = 0;
6019 clang::RecordDecl::field_iterator field, field_end;
6020 for (field = record_decl->field_begin(),
6021 field_end = record_decl->field_end();
6022 field != field_end; ++field, ++field_idx) {
6023 if (idx == field_idx) {
6024 // Print the member type if requested
6025 // Print the member name and equal sign
6026 name.assign(field->getNameAsString());
6027
6028 // Figure out the type byte size (field_type_info.first) and
6029 // alignment (field_type_info.second) from the AST context.
6030 if (bit_offset_ptr) {
6031 const clang::ASTRecordLayout &record_layout =
6032 getASTContext()->getASTRecordLayout(record_decl);
6033 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
6034 }
6035
6036 const bool is_bitfield = field->isBitField();
6037
6038 if (bitfield_bit_size_ptr) {
6039 *bitfield_bit_size_ptr = 0;
6040
6041 if (is_bitfield) {
6042 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
Hans Wennborg30ce9622018-11-28 14:30:18 +00006043 clang::Expr::EvalResult result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006044 if (bitfield_bit_size_expr &&
Hans Wennborg30ce9622018-11-28 14:30:18 +00006045 bitfield_bit_size_expr->EvaluateAsInt(result,
Kate Stoneb9c1b512016-09-06 20:57:50 +00006046 *getASTContext())) {
Hans Wennborg30ce9622018-11-28 14:30:18 +00006047 llvm::APSInt bitfield_apsint = result.Val.getInt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006048 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
6049 }
6050 }
6051 }
6052 if (is_bitfield_ptr)
6053 *is_bitfield_ptr = is_bitfield;
6054
6055 return CompilerType(getASTContext(), field->getType());
6056 }
6057 }
6058 }
6059 break;
6060
Sean Callananf9c622a2016-09-30 18:44:43 +00006061 case clang::Type::ObjCObjectPointer: {
6062 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00006063 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00006064 const clang::ObjCInterfaceType *objc_interface_type =
6065 objc_class_type->getInterfaceType();
6066 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00006067 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
6068 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00006069 clang::ObjCInterfaceDecl *class_interface_decl =
6070 objc_interface_type->getDecl();
6071 if (class_interface_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006072 return CompilerType(
6073 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
6074 idx, name, bit_offset_ptr,
6075 bitfield_bit_size_ptr, is_bitfield_ptr));
6076 }
6077 }
6078 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00006079 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006080
6081 case clang::Type::ObjCObject:
6082 case clang::Type::ObjCInterface:
6083 if (GetCompleteType(type)) {
6084 const clang::ObjCObjectType *objc_class_type =
6085 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6086 assert(objc_class_type);
6087 if (objc_class_type) {
6088 clang::ObjCInterfaceDecl *class_interface_decl =
6089 objc_class_type->getInterface();
6090 return CompilerType(
6091 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
6092 idx, name, bit_offset_ptr,
6093 bitfield_bit_size_ptr, is_bitfield_ptr));
6094 }
6095 }
6096 break;
6097
6098 case clang::Type::Typedef:
6099 return CompilerType(getASTContext(),
6100 llvm::cast<clang::TypedefType>(qual_type)
6101 ->getDecl()
6102 ->getUnderlyingType())
6103 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6104 is_bitfield_ptr);
6105
6106 case clang::Type::Auto:
6107 return CompilerType(
6108 getASTContext(),
6109 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
6110 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6111 is_bitfield_ptr);
6112
6113 case clang::Type::Elaborated:
6114 return CompilerType(
6115 getASTContext(),
6116 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
6117 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6118 is_bitfield_ptr);
6119
6120 case clang::Type::Paren:
6121 return CompilerType(getASTContext(),
6122 llvm::cast<clang::ParenType>(qual_type)->desugar())
6123 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6124 is_bitfield_ptr);
6125
6126 default:
6127 break;
6128 }
6129 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006130}
6131
Greg Clayton99558cc42015-08-24 23:46:31 +00006132uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006133ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
6134 uint32_t count = 0;
6135 clang::QualType qual_type(GetCanonicalQualType(type));
6136 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6137 switch (type_class) {
6138 case clang::Type::Record:
6139 if (GetCompleteType(type)) {
6140 const clang::CXXRecordDecl *cxx_record_decl =
6141 qual_type->getAsCXXRecordDecl();
6142 if (cxx_record_decl)
6143 count = cxx_record_decl->getNumBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006144 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006145 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006146
Kate Stoneb9c1b512016-09-06 20:57:50 +00006147 case clang::Type::ObjCObjectPointer:
6148 count = GetPointeeType(type).GetNumDirectBaseClasses();
6149 break;
6150
6151 case clang::Type::ObjCObject:
6152 if (GetCompleteType(type)) {
6153 const clang::ObjCObjectType *objc_class_type =
6154 qual_type->getAsObjCQualifiedInterfaceType();
6155 if (objc_class_type) {
6156 clang::ObjCInterfaceDecl *class_interface_decl =
6157 objc_class_type->getInterface();
6158
6159 if (class_interface_decl && class_interface_decl->getSuperClass())
6160 count = 1;
6161 }
6162 }
6163 break;
6164 case clang::Type::ObjCInterface:
6165 if (GetCompleteType(type)) {
6166 const clang::ObjCInterfaceType *objc_interface_type =
6167 qual_type->getAs<clang::ObjCInterfaceType>();
6168 if (objc_interface_type) {
6169 clang::ObjCInterfaceDecl *class_interface_decl =
6170 objc_interface_type->getInterface();
6171
6172 if (class_interface_decl && class_interface_decl->getSuperClass())
6173 count = 1;
6174 }
6175 }
6176 break;
6177
6178 case clang::Type::Typedef:
6179 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6180 ->getDecl()
6181 ->getUnderlyingType()
6182 .getAsOpaquePtr());
6183 break;
6184
6185 case clang::Type::Auto:
6186 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6187 ->getDeducedType()
6188 .getAsOpaquePtr());
6189 break;
6190
6191 case clang::Type::Elaborated:
6192 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6193 ->getNamedType()
6194 .getAsOpaquePtr());
6195 break;
6196
6197 case clang::Type::Paren:
6198 return GetNumDirectBaseClasses(
6199 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6200
6201 default:
6202 break;
6203 }
6204 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006205}
6206
6207uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006208ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
6209 uint32_t count = 0;
6210 clang::QualType qual_type(GetCanonicalQualType(type));
6211 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6212 switch (type_class) {
6213 case clang::Type::Record:
6214 if (GetCompleteType(type)) {
6215 const clang::CXXRecordDecl *cxx_record_decl =
6216 qual_type->getAsCXXRecordDecl();
6217 if (cxx_record_decl)
6218 count = cxx_record_decl->getNumVBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006219 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006220 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006221
Kate Stoneb9c1b512016-09-06 20:57:50 +00006222 case clang::Type::Typedef:
6223 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6224 ->getDecl()
6225 ->getUnderlyingType()
6226 .getAsOpaquePtr());
6227 break;
6228
6229 case clang::Type::Auto:
6230 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6231 ->getDeducedType()
6232 .getAsOpaquePtr());
6233 break;
6234
6235 case clang::Type::Elaborated:
6236 count =
6237 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6238 ->getNamedType()
6239 .getAsOpaquePtr());
6240 break;
6241
6242 case clang::Type::Paren:
6243 count = GetNumVirtualBaseClasses(
6244 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6245 break;
6246
6247 default:
6248 break;
6249 }
6250 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006251}
6252
Kate Stoneb9c1b512016-09-06 20:57:50 +00006253CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
6254 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6255 clang::QualType qual_type(GetCanonicalQualType(type));
6256 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6257 switch (type_class) {
6258 case clang::Type::Record:
6259 if (GetCompleteType(type)) {
6260 const clang::CXXRecordDecl *cxx_record_decl =
6261 qual_type->getAsCXXRecordDecl();
6262 if (cxx_record_decl) {
6263 uint32_t curr_idx = 0;
6264 clang::CXXRecordDecl::base_class_const_iterator base_class,
6265 base_class_end;
6266 for (base_class = cxx_record_decl->bases_begin(),
6267 base_class_end = cxx_record_decl->bases_end();
6268 base_class != base_class_end; ++base_class, ++curr_idx) {
6269 if (curr_idx == idx) {
6270 if (bit_offset_ptr) {
6271 const clang::ASTRecordLayout &record_layout =
6272 getASTContext()->getASTRecordLayout(cxx_record_decl);
6273 const clang::CXXRecordDecl *base_class_decl =
6274 llvm::cast<clang::CXXRecordDecl>(
6275 base_class->getType()
6276 ->getAs<clang::RecordType>()
6277 ->getDecl());
6278 if (base_class->isVirtual())
6279 *bit_offset_ptr =
6280 record_layout.getVBaseClassOffset(base_class_decl)
6281 .getQuantity() *
6282 8;
6283 else
6284 *bit_offset_ptr =
6285 record_layout.getBaseClassOffset(base_class_decl)
6286 .getQuantity() *
6287 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006288 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006289 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6290 }
6291 }
6292 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006293 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006294 break;
6295
6296 case clang::Type::ObjCObjectPointer:
6297 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6298
6299 case clang::Type::ObjCObject:
6300 if (idx == 0 && GetCompleteType(type)) {
6301 const clang::ObjCObjectType *objc_class_type =
6302 qual_type->getAsObjCQualifiedInterfaceType();
6303 if (objc_class_type) {
6304 clang::ObjCInterfaceDecl *class_interface_decl =
6305 objc_class_type->getInterface();
6306
6307 if (class_interface_decl) {
6308 clang::ObjCInterfaceDecl *superclass_interface_decl =
6309 class_interface_decl->getSuperClass();
6310 if (superclass_interface_decl) {
6311 if (bit_offset_ptr)
6312 *bit_offset_ptr = 0;
6313 return CompilerType(getASTContext(),
6314 getASTContext()->getObjCInterfaceType(
6315 superclass_interface_decl));
6316 }
6317 }
6318 }
6319 }
6320 break;
6321 case clang::Type::ObjCInterface:
6322 if (idx == 0 && GetCompleteType(type)) {
6323 const clang::ObjCObjectType *objc_interface_type =
6324 qual_type->getAs<clang::ObjCInterfaceType>();
6325 if (objc_interface_type) {
6326 clang::ObjCInterfaceDecl *class_interface_decl =
6327 objc_interface_type->getInterface();
6328
6329 if (class_interface_decl) {
6330 clang::ObjCInterfaceDecl *superclass_interface_decl =
6331 class_interface_decl->getSuperClass();
6332 if (superclass_interface_decl) {
6333 if (bit_offset_ptr)
6334 *bit_offset_ptr = 0;
6335 return CompilerType(getASTContext(),
6336 getASTContext()->getObjCInterfaceType(
6337 superclass_interface_decl));
6338 }
6339 }
6340 }
6341 }
6342 break;
6343
6344 case clang::Type::Typedef:
6345 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6346 ->getDecl()
6347 ->getUnderlyingType()
6348 .getAsOpaquePtr(),
6349 idx, bit_offset_ptr);
6350
6351 case clang::Type::Auto:
6352 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6353 ->getDeducedType()
6354 .getAsOpaquePtr(),
6355 idx, bit_offset_ptr);
6356
6357 case clang::Type::Elaborated:
6358 return GetDirectBaseClassAtIndex(
6359 llvm::cast<clang::ElaboratedType>(qual_type)
6360 ->getNamedType()
6361 .getAsOpaquePtr(),
6362 idx, bit_offset_ptr);
6363
6364 case clang::Type::Paren:
6365 return GetDirectBaseClassAtIndex(
6366 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6367 idx, bit_offset_ptr);
6368
6369 default:
6370 break;
6371 }
6372 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006373}
6374
Kate Stoneb9c1b512016-09-06 20:57:50 +00006375CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6376 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6377 clang::QualType qual_type(GetCanonicalQualType(type));
6378 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6379 switch (type_class) {
6380 case clang::Type::Record:
6381 if (GetCompleteType(type)) {
6382 const clang::CXXRecordDecl *cxx_record_decl =
6383 qual_type->getAsCXXRecordDecl();
6384 if (cxx_record_decl) {
6385 uint32_t curr_idx = 0;
6386 clang::CXXRecordDecl::base_class_const_iterator base_class,
6387 base_class_end;
6388 for (base_class = cxx_record_decl->vbases_begin(),
6389 base_class_end = cxx_record_decl->vbases_end();
6390 base_class != base_class_end; ++base_class, ++curr_idx) {
6391 if (curr_idx == idx) {
6392 if (bit_offset_ptr) {
6393 const clang::ASTRecordLayout &record_layout =
6394 getASTContext()->getASTRecordLayout(cxx_record_decl);
6395 const clang::CXXRecordDecl *base_class_decl =
6396 llvm::cast<clang::CXXRecordDecl>(
6397 base_class->getType()
6398 ->getAs<clang::RecordType>()
6399 ->getDecl());
6400 *bit_offset_ptr =
6401 record_layout.getVBaseClassOffset(base_class_decl)
6402 .getQuantity() *
6403 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006404 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006405 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6406 }
6407 }
6408 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006409 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006410 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006411
Kate Stoneb9c1b512016-09-06 20:57:50 +00006412 case clang::Type::Typedef:
6413 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6414 ->getDecl()
6415 ->getUnderlyingType()
6416 .getAsOpaquePtr(),
6417 idx, bit_offset_ptr);
6418
6419 case clang::Type::Auto:
6420 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6421 ->getDeducedType()
6422 .getAsOpaquePtr(),
6423 idx, bit_offset_ptr);
6424
6425 case clang::Type::Elaborated:
6426 return GetVirtualBaseClassAtIndex(
6427 llvm::cast<clang::ElaboratedType>(qual_type)
6428 ->getNamedType()
6429 .getAsOpaquePtr(),
6430 idx, bit_offset_ptr);
6431
6432 case clang::Type::Paren:
6433 return GetVirtualBaseClassAtIndex(
6434 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6435 idx, bit_offset_ptr);
6436
6437 default:
6438 break;
6439 }
6440 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006441}
6442
Greg Claytond8d4a572015-08-11 21:38:15 +00006443// If a pointer to a pointee type (the clang_type arg) says that it has no
6444// children, then we either need to trust it, or override it and return a
6445// different result. For example, an "int *" has one child that is an integer,
6446// but a function pointer doesn't have any children. Likewise if a Record type
6447// claims it has no children, then there really is nothing to show.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006448uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6449 if (type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00006450 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006451
6452 clang::QualType qual_type(type.getCanonicalType());
6453 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6454 switch (type_class) {
6455 case clang::Type::Builtin:
6456 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6457 case clang::BuiltinType::UnknownAny:
6458 case clang::BuiltinType::Void:
6459 case clang::BuiltinType::NullPtr:
6460 case clang::BuiltinType::OCLEvent:
6461 case clang::BuiltinType::OCLImage1dRO:
6462 case clang::BuiltinType::OCLImage1dWO:
6463 case clang::BuiltinType::OCLImage1dRW:
6464 case clang::BuiltinType::OCLImage1dArrayRO:
6465 case clang::BuiltinType::OCLImage1dArrayWO:
6466 case clang::BuiltinType::OCLImage1dArrayRW:
6467 case clang::BuiltinType::OCLImage1dBufferRO:
6468 case clang::BuiltinType::OCLImage1dBufferWO:
6469 case clang::BuiltinType::OCLImage1dBufferRW:
6470 case clang::BuiltinType::OCLImage2dRO:
6471 case clang::BuiltinType::OCLImage2dWO:
6472 case clang::BuiltinType::OCLImage2dRW:
6473 case clang::BuiltinType::OCLImage2dArrayRO:
6474 case clang::BuiltinType::OCLImage2dArrayWO:
6475 case clang::BuiltinType::OCLImage2dArrayRW:
6476 case clang::BuiltinType::OCLImage3dRO:
6477 case clang::BuiltinType::OCLImage3dWO:
6478 case clang::BuiltinType::OCLImage3dRW:
6479 case clang::BuiltinType::OCLSampler:
6480 return 0;
6481 case clang::BuiltinType::Bool:
6482 case clang::BuiltinType::Char_U:
6483 case clang::BuiltinType::UChar:
6484 case clang::BuiltinType::WChar_U:
6485 case clang::BuiltinType::Char16:
6486 case clang::BuiltinType::Char32:
6487 case clang::BuiltinType::UShort:
6488 case clang::BuiltinType::UInt:
6489 case clang::BuiltinType::ULong:
6490 case clang::BuiltinType::ULongLong:
6491 case clang::BuiltinType::UInt128:
6492 case clang::BuiltinType::Char_S:
6493 case clang::BuiltinType::SChar:
6494 case clang::BuiltinType::WChar_S:
6495 case clang::BuiltinType::Short:
6496 case clang::BuiltinType::Int:
6497 case clang::BuiltinType::Long:
6498 case clang::BuiltinType::LongLong:
6499 case clang::BuiltinType::Int128:
6500 case clang::BuiltinType::Float:
6501 case clang::BuiltinType::Double:
6502 case clang::BuiltinType::LongDouble:
6503 case clang::BuiltinType::Dependent:
6504 case clang::BuiltinType::Overload:
6505 case clang::BuiltinType::ObjCId:
6506 case clang::BuiltinType::ObjCClass:
6507 case clang::BuiltinType::ObjCSel:
6508 case clang::BuiltinType::BoundMember:
6509 case clang::BuiltinType::Half:
6510 case clang::BuiltinType::ARCUnbridgedCast:
6511 case clang::BuiltinType::PseudoObject:
6512 case clang::BuiltinType::BuiltinFn:
6513 case clang::BuiltinType::OMPArraySection:
6514 return 1;
6515 default:
6516 return 0;
6517 }
6518 break;
6519
6520 case clang::Type::Complex:
6521 return 1;
6522 case clang::Type::Pointer:
6523 return 1;
6524 case clang::Type::BlockPointer:
6525 return 0; // If block pointers don't have debug info, then no children for
6526 // them
6527 case clang::Type::LValueReference:
6528 return 1;
6529 case clang::Type::RValueReference:
6530 return 1;
6531 case clang::Type::MemberPointer:
6532 return 0;
6533 case clang::Type::ConstantArray:
6534 return 0;
6535 case clang::Type::IncompleteArray:
6536 return 0;
6537 case clang::Type::VariableArray:
6538 return 0;
6539 case clang::Type::DependentSizedArray:
6540 return 0;
6541 case clang::Type::DependentSizedExtVector:
6542 return 0;
6543 case clang::Type::Vector:
6544 return 0;
6545 case clang::Type::ExtVector:
6546 return 0;
6547 case clang::Type::FunctionProto:
6548 return 0; // When we function pointers, they have no children...
6549 case clang::Type::FunctionNoProto:
6550 return 0; // When we function pointers, they have no children...
6551 case clang::Type::UnresolvedUsing:
6552 return 0;
6553 case clang::Type::Paren:
6554 return GetNumPointeeChildren(
6555 llvm::cast<clang::ParenType>(qual_type)->desugar());
6556 case clang::Type::Typedef:
6557 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6558 ->getDecl()
6559 ->getUnderlyingType());
6560 case clang::Type::Auto:
6561 return GetNumPointeeChildren(
6562 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6563 case clang::Type::Elaborated:
6564 return GetNumPointeeChildren(
6565 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6566 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006567 return GetNumPointeeChildren(llvm::cast<clang::TypeOfExprType>(qual_type)
6568 ->getUnderlyingExpr()
6569 ->getType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006570 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006571 return GetNumPointeeChildren(
6572 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006573 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006574 return GetNumPointeeChildren(
6575 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006576 case clang::Type::Record:
6577 return 0;
6578 case clang::Type::Enum:
6579 return 1;
6580 case clang::Type::TemplateTypeParm:
6581 return 1;
6582 case clang::Type::SubstTemplateTypeParm:
6583 return 1;
6584 case clang::Type::TemplateSpecialization:
6585 return 1;
6586 case clang::Type::InjectedClassName:
6587 return 0;
6588 case clang::Type::DependentName:
6589 return 1;
6590 case clang::Type::DependentTemplateSpecialization:
6591 return 1;
6592 case clang::Type::ObjCObject:
6593 return 0;
6594 case clang::Type::ObjCInterface:
6595 return 0;
6596 case clang::Type::ObjCObjectPointer:
6597 return 1;
6598 default:
6599 break;
6600 }
6601 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006602}
6603
Kate Stoneb9c1b512016-09-06 20:57:50 +00006604CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6605 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6606 bool transparent_pointers, bool omit_empty_base_classes,
6607 bool ignore_array_bounds, std::string &child_name,
6608 uint32_t &child_byte_size, int32_t &child_byte_offset,
6609 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6610 bool &child_is_base_class, bool &child_is_deref_of_parent,
6611 ValueObject *valobj, uint64_t &language_flags) {
6612 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006613 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006614
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006615 auto get_exe_scope = [&exe_ctx]() {
6616 return exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr;
6617 };
6618
Kate Stoneb9c1b512016-09-06 20:57:50 +00006619 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6620 const clang::Type::TypeClass parent_type_class =
6621 parent_qual_type->getTypeClass();
6622 child_bitfield_bit_size = 0;
6623 child_bitfield_bit_offset = 0;
6624 child_is_base_class = false;
6625 language_flags = 0;
6626
Adrian Prantleca07c52018-11-05 20:49:07 +00006627 const bool idx_is_valid =
6628 idx < GetNumChildren(type, omit_empty_base_classes, exe_ctx);
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00006629 int32_t bit_offset;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006630 switch (parent_type_class) {
6631 case clang::Type::Builtin:
6632 if (idx_is_valid) {
6633 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6634 case clang::BuiltinType::ObjCId:
6635 case clang::BuiltinType::ObjCClass:
6636 child_name = "isa";
6637 child_byte_size =
6638 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6639 CHAR_BIT;
6640 return CompilerType(getASTContext(),
6641 getASTContext()->ObjCBuiltinClassTy);
6642
6643 default:
6644 break;
6645 }
6646 }
6647 break;
6648
6649 case clang::Type::Record:
6650 if (idx_is_valid && GetCompleteType(type)) {
6651 const clang::RecordType *record_type =
6652 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6653 const clang::RecordDecl *record_decl = record_type->getDecl();
6654 assert(record_decl);
6655 const clang::ASTRecordLayout &record_layout =
6656 getASTContext()->getASTRecordLayout(record_decl);
6657 uint32_t child_idx = 0;
6658
6659 const clang::CXXRecordDecl *cxx_record_decl =
6660 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6661 if (cxx_record_decl) {
6662 // We might have base classes to print out first
6663 clang::CXXRecordDecl::base_class_const_iterator base_class,
6664 base_class_end;
6665 for (base_class = cxx_record_decl->bases_begin(),
6666 base_class_end = cxx_record_decl->bases_end();
6667 base_class != base_class_end; ++base_class) {
6668 const clang::CXXRecordDecl *base_class_decl = nullptr;
6669
6670 // Skip empty base classes
6671 if (omit_empty_base_classes) {
6672 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6673 base_class->getType()->getAs<clang::RecordType>()->getDecl());
Jonas Devliegherea6682a42018-12-15 00:15:33 +00006674 if (!ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00006675 continue;
6676 }
6677
6678 if (idx == child_idx) {
6679 if (base_class_decl == nullptr)
6680 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6681 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6682
6683 if (base_class->isVirtual()) {
6684 bool handled = false;
6685 if (valobj) {
Aleksandr Urakov1dc51db2018-11-12 16:23:50 +00006686 clang::VTableContextBase *vtable_ctx =
6687 getASTContext()->getVTableContext();
6688 if (vtable_ctx)
6689 handled = GetVBaseBitOffset(*vtable_ctx, *valobj,
6690 record_layout, cxx_record_decl,
6691 base_class_decl, bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006692 }
6693 if (!handled)
6694 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6695 .getQuantity() *
6696 8;
6697 } else
6698 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6699 .getQuantity() *
6700 8;
6701
6702 // Base classes should be a multiple of 8 bits in size
6703 child_byte_offset = bit_offset / 8;
6704 CompilerType base_class_clang_type(getASTContext(),
6705 base_class->getType());
6706 child_name = base_class_clang_type.GetTypeName().AsCString("");
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006707 Optional<uint64_t> size =
6708 base_class_clang_type.GetBitSize(get_exe_scope());
Adrian Prantld963a7c2019-01-15 18:07:52 +00006709 if (!size)
6710 return {};
6711 uint64_t base_class_clang_type_bit_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006712
6713 // Base classes bit sizes should be a multiple of 8 bits in size
6714 assert(base_class_clang_type_bit_size % 8 == 0);
6715 child_byte_size = base_class_clang_type_bit_size / 8;
6716 child_is_base_class = true;
6717 return base_class_clang_type;
6718 }
6719 // We don't increment the child index in the for loop since we might
6720 // be skipping empty base classes
6721 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006722 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006723 }
6724 // Make sure index is in range...
6725 uint32_t field_idx = 0;
6726 clang::RecordDecl::field_iterator field, field_end;
6727 for (field = record_decl->field_begin(),
6728 field_end = record_decl->field_end();
6729 field != field_end; ++field, ++field_idx, ++child_idx) {
6730 if (idx == child_idx) {
6731 // Print the member type if requested
6732 // Print the member name and equal sign
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006733 child_name.assign(field->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006734
6735 // Figure out the type byte size (field_type_info.first) and
6736 // alignment (field_type_info.second) from the AST context.
6737 CompilerType field_clang_type(getASTContext(), field->getType());
6738 assert(field_idx < record_layout.getFieldCount());
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006739 Optional<uint64_t> size =
6740 field_clang_type.GetByteSize(get_exe_scope());
Adrian Prantld963a7c2019-01-15 18:07:52 +00006741 if (!size)
6742 return {};
6743 child_byte_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006744 const uint32_t child_bit_size = child_byte_size * 8;
6745
6746 // Figure out the field offset within the current struct/union/class
6747 // type
6748 bit_offset = record_layout.getFieldOffset(field_idx);
6749 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6750 child_bitfield_bit_size)) {
6751 child_bitfield_bit_offset = bit_offset % child_bit_size;
6752 const uint32_t child_bit_offset =
6753 bit_offset - child_bitfield_bit_offset;
6754 child_byte_offset = child_bit_offset / 8;
6755 } else {
6756 child_byte_offset = bit_offset / 8;
6757 }
6758
6759 return field_clang_type;
6760 }
6761 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006762 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006763 break;
6764
6765 case clang::Type::ObjCObject:
6766 case clang::Type::ObjCInterface:
6767 if (idx_is_valid && GetCompleteType(type)) {
6768 const clang::ObjCObjectType *objc_class_type =
6769 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6770 assert(objc_class_type);
6771 if (objc_class_type) {
6772 uint32_t child_idx = 0;
6773 clang::ObjCInterfaceDecl *class_interface_decl =
6774 objc_class_type->getInterface();
6775
6776 if (class_interface_decl) {
6777
6778 const clang::ASTRecordLayout &interface_layout =
6779 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6780 clang::ObjCInterfaceDecl *superclass_interface_decl =
6781 class_interface_decl->getSuperClass();
6782 if (superclass_interface_decl) {
6783 if (omit_empty_base_classes) {
6784 CompilerType base_class_clang_type(
6785 getASTContext(), getASTContext()->getObjCInterfaceType(
6786 superclass_interface_decl));
Adrian Prantleca07c52018-11-05 20:49:07 +00006787 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes,
6788 exe_ctx) > 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006789 if (idx == 0) {
6790 clang::QualType ivar_qual_type(
6791 getASTContext()->getObjCInterfaceType(
6792 superclass_interface_decl));
6793
6794 child_name.assign(
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006795 superclass_interface_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006796
6797 clang::TypeInfo ivar_type_info =
6798 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6799
6800 child_byte_size = ivar_type_info.Width / 8;
6801 child_byte_offset = 0;
6802 child_is_base_class = true;
6803
6804 return CompilerType(getASTContext(), ivar_qual_type);
6805 }
6806
6807 ++child_idx;
6808 }
6809 } else
6810 ++child_idx;
6811 }
6812
6813 const uint32_t superclass_idx = child_idx;
6814
6815 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6816 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6817 ivar_end = class_interface_decl->ivar_end();
6818
6819 for (ivar_pos = class_interface_decl->ivar_begin();
6820 ivar_pos != ivar_end; ++ivar_pos) {
6821 if (child_idx == idx) {
6822 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6823
6824 clang::QualType ivar_qual_type(ivar_decl->getType());
6825
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006826 child_name.assign(ivar_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006827
6828 clang::TypeInfo ivar_type_info =
6829 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6830
6831 child_byte_size = ivar_type_info.Width / 8;
6832
6833 // Figure out the field offset within the current
Adrian Prantl05097242018-04-30 16:49:04 +00006834 // struct/union/class type For ObjC objects, we can't trust the
6835 // bit offset we get from the Clang AST, since that doesn't
6836 // account for the space taken up by unbacked properties, or
6837 // from the changing size of base classes that are newer than
6838 // this class. So if we have a process around that we can ask
6839 // about this object, do so.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006840 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6841 Process *process = nullptr;
6842 if (exe_ctx)
6843 process = exe_ctx->GetProcessPtr();
6844 if (process) {
6845 ObjCLanguageRuntime *objc_runtime =
Alex Langforde823bbe2019-06-10 20:53:23 +00006846 ObjCLanguageRuntime::Get(*process);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006847 if (objc_runtime != nullptr) {
6848 CompilerType parent_ast_type(getASTContext(),
6849 parent_qual_type);
6850 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6851 parent_ast_type, ivar_decl->getNameAsString().c_str());
6852 }
6853 }
6854
Aleksandr Urakovff701722018-08-20 05:59:27 +00006855 // Setting this to INT32_MAX to make sure we don't compute it
Kate Stoneb9c1b512016-09-06 20:57:50 +00006856 // twice...
Aleksandr Urakov53459482018-08-17 07:28:24 +00006857 bit_offset = INT32_MAX;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006858
6859 if (child_byte_offset ==
6860 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6861 bit_offset = interface_layout.getFieldOffset(child_idx -
6862 superclass_idx);
6863 child_byte_offset = bit_offset / 8;
6864 }
6865
6866 // Note, the ObjC Ivar Byte offset is just that, it doesn't
Adrian Prantl05097242018-04-30 16:49:04 +00006867 // account for the bit offset of a bitfield within its
6868 // containing object. So regardless of where we get the byte
Kate Stoneb9c1b512016-09-06 20:57:50 +00006869 // offset from, we still need to get the bit offset for
6870 // bitfields from the layout.
6871
6872 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6873 child_bitfield_bit_size)) {
Aleksandr Urakov53459482018-08-17 07:28:24 +00006874 if (bit_offset == INT32_MAX)
Kate Stoneb9c1b512016-09-06 20:57:50 +00006875 bit_offset = interface_layout.getFieldOffset(
6876 child_idx - superclass_idx);
6877
6878 child_bitfield_bit_offset = bit_offset % 8;
6879 }
6880 return CompilerType(getASTContext(), ivar_qual_type);
6881 }
6882 ++child_idx;
6883 }
6884 }
6885 }
6886 }
6887 }
6888 break;
6889
6890 case clang::Type::ObjCObjectPointer:
6891 if (idx_is_valid) {
6892 CompilerType pointee_clang_type(GetPointeeType(type));
6893
6894 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6895 child_is_deref_of_parent = false;
6896 bool tmp_child_is_deref_of_parent = false;
6897 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6898 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6899 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6900 child_bitfield_bit_size, child_bitfield_bit_offset,
6901 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6902 language_flags);
6903 } else {
6904 child_is_deref_of_parent = true;
6905 const char *parent_name =
Konrad Kleine248a1302019-05-23 11:14:47 +00006906 valobj ? valobj->GetName().GetCString() : nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006907 if (parent_name) {
6908 child_name.assign(1, '*');
6909 child_name += parent_name;
6910 }
6911
6912 // We have a pointer to an simple type
6913 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006914 if (Optional<uint64_t> size =
6915 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006916 child_byte_size = *size;
6917 child_byte_offset = 0;
6918 return pointee_clang_type;
6919 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006920 }
6921 }
6922 }
6923 break;
6924
6925 case clang::Type::Vector:
6926 case clang::Type::ExtVector:
6927 if (idx_is_valid) {
6928 const clang::VectorType *array =
6929 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6930 if (array) {
6931 CompilerType element_type(getASTContext(), array->getElementType());
6932 if (element_type.GetCompleteType()) {
6933 char element_name[64];
6934 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6935 static_cast<uint64_t>(idx));
6936 child_name.assign(element_name);
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006937 if (Optional<uint64_t> size =
6938 element_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006939 child_byte_size = *size;
6940 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6941 return element_type;
6942 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006943 }
6944 }
6945 }
6946 break;
6947
6948 case clang::Type::ConstantArray:
6949 case clang::Type::IncompleteArray:
6950 if (ignore_array_bounds || idx_is_valid) {
6951 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6952 if (array) {
6953 CompilerType element_type(getASTContext(), array->getElementType());
6954 if (element_type.GetCompleteType()) {
Zachary Turner827d5d72016-12-16 04:27:00 +00006955 child_name = llvm::formatv("[{0}]", idx);
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006956 if (Optional<uint64_t> size =
6957 element_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006958 child_byte_size = *size;
6959 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6960 return element_type;
6961 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006962 }
6963 }
6964 }
6965 break;
6966
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006967 case clang::Type::Pointer: {
6968 CompilerType pointee_clang_type(GetPointeeType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00006969
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006970 // Don't dereference "void *" pointers
6971 if (pointee_clang_type.IsVoidType())
6972 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006973
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006974 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6975 child_is_deref_of_parent = false;
6976 bool tmp_child_is_deref_of_parent = false;
6977 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6978 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6979 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6980 child_bitfield_bit_size, child_bitfield_bit_offset,
6981 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6982 language_flags);
6983 } else {
6984 child_is_deref_of_parent = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006985
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006986 const char *parent_name =
Konrad Kleine248a1302019-05-23 11:14:47 +00006987 valobj ? valobj->GetName().GetCString() : nullptr;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006988 if (parent_name) {
6989 child_name.assign(1, '*');
6990 child_name += parent_name;
6991 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006992
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006993 // We have a pointer to an simple type
6994 if (idx == 0) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006995 if (Optional<uint64_t> size =
6996 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006997 child_byte_size = *size;
6998 child_byte_offset = 0;
6999 return pointee_clang_type;
7000 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007001 }
7002 }
7003 break;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00007004 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007005
7006 case clang::Type::LValueReference:
7007 case clang::Type::RValueReference:
7008 if (idx_is_valid) {
7009 const clang::ReferenceType *reference_type =
7010 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
7011 CompilerType pointee_clang_type(getASTContext(),
7012 reference_type->getPointeeType());
7013 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
7014 child_is_deref_of_parent = false;
7015 bool tmp_child_is_deref_of_parent = false;
7016 return pointee_clang_type.GetChildCompilerTypeAtIndex(
7017 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7018 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7019 child_bitfield_bit_size, child_bitfield_bit_offset,
7020 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
7021 language_flags);
7022 } else {
7023 const char *parent_name =
Konrad Kleine248a1302019-05-23 11:14:47 +00007024 valobj ? valobj->GetName().GetCString() : nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007025 if (parent_name) {
7026 child_name.assign(1, '&');
7027 child_name += parent_name;
7028 }
7029
7030 // We have a pointer to an simple type
7031 if (idx == 0) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00007032 if (Optional<uint64_t> size =
7033 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00007034 child_byte_size = *size;
7035 child_byte_offset = 0;
7036 return pointee_clang_type;
7037 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007038 }
7039 }
7040 }
7041 break;
7042
7043 case clang::Type::Typedef: {
7044 CompilerType typedefed_clang_type(
7045 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)
7046 ->getDecl()
7047 ->getUnderlyingType());
7048 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
7049 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7050 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7051 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7052 child_is_deref_of_parent, valobj, language_flags);
7053 } break;
7054
7055 case clang::Type::Auto: {
7056 CompilerType elaborated_clang_type(
7057 getASTContext(),
7058 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
7059 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
7060 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7061 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7062 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7063 child_is_deref_of_parent, valobj, language_flags);
7064 }
7065
7066 case clang::Type::Elaborated: {
7067 CompilerType elaborated_clang_type(
7068 getASTContext(),
7069 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
7070 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
7071 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7072 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7073 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7074 child_is_deref_of_parent, valobj, language_flags);
7075 }
7076
7077 case clang::Type::Paren: {
7078 CompilerType paren_clang_type(
7079 getASTContext(),
7080 llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
7081 return paren_clang_type.GetChildCompilerTypeAtIndex(
7082 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7083 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7084 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7085 child_is_deref_of_parent, valobj, language_flags);
7086 }
7087
7088 default:
7089 break;
7090 }
7091 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007092}
7093
Kate Stoneb9c1b512016-09-06 20:57:50 +00007094static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
7095 const clang::CXXBaseSpecifier *base_spec,
7096 bool omit_empty_base_classes) {
7097 uint32_t child_idx = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007098
Kate Stoneb9c1b512016-09-06 20:57:50 +00007099 const clang::CXXRecordDecl *cxx_record_decl =
7100 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7101
7102 // const char *super_name = record_decl->getNameAsCString();
7103 // const char *base_name =
7104 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
7105 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
7106 //
7107 if (cxx_record_decl) {
7108 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
7109 for (base_class = cxx_record_decl->bases_begin(),
7110 base_class_end = cxx_record_decl->bases_end();
7111 base_class != base_class_end; ++base_class) {
7112 if (omit_empty_base_classes) {
7113 if (BaseSpecifierIsEmpty(base_class))
7114 continue;
7115 }
7116
7117 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
7118 // super_name, base_name,
7119 // child_idx,
7120 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
7121 //
7122 //
7123 if (base_class == base_spec)
7124 return child_idx;
7125 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00007126 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007127 }
7128
7129 return UINT32_MAX;
7130}
7131
7132static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
7133 clang::NamedDecl *canonical_decl,
7134 bool omit_empty_base_classes) {
7135 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
7136 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
7137 omit_empty_base_classes);
7138
7139 clang::RecordDecl::field_iterator field, field_end;
7140 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
7141 field != field_end; ++field, ++child_idx) {
7142 if (field->getCanonicalDecl() == canonical_decl)
7143 return child_idx;
7144 }
7145
7146 return UINT32_MAX;
Greg Claytond8d4a572015-08-11 21:38:15 +00007147}
7148
7149// Look for a child member (doesn't include base classes, but it does include
Adrian Prantl05097242018-04-30 16:49:04 +00007150// their members) in the type hierarchy. Returns an index path into
7151// "clang_type" on how to reach the appropriate member.
Greg Claytond8d4a572015-08-11 21:38:15 +00007152//
7153// class A
7154// {
7155// public:
7156// int m_a;
7157// int m_b;
7158// };
7159//
7160// class B
7161// {
7162// };
7163//
7164// class C :
7165// public B,
7166// public A
7167// {
7168// };
7169//
7170// If we have a clang type that describes "class C", and we wanted to looked
7171// "m_b" in it:
7172//
Kate Stoneb9c1b512016-09-06 20:57:50 +00007173// With omit_empty_base_classes == false we would get an integer array back
Adrian Prantl05097242018-04-30 16:49:04 +00007174// with: { 1, 1 } The first index 1 is the child index for "class A" within
7175// class C The second index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007176//
Adrian Prantl05097242018-04-30 16:49:04 +00007177// With omit_empty_base_classes == true we would get an integer array back
7178// with: { 0, 1 } The first index 0 is the child index for "class A" within
7179// class C (since class B doesn't have any members it doesn't count) The second
7180// index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007181
Kate Stoneb9c1b512016-09-06 20:57:50 +00007182size_t ClangASTContext::GetIndexOfChildMemberWithName(
7183 lldb::opaque_compiler_type_t type, const char *name,
7184 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
7185 if (type && name && name[0]) {
7186 clang::QualType qual_type(GetCanonicalQualType(type));
7187 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7188 switch (type_class) {
7189 case clang::Type::Record:
7190 if (GetCompleteType(type)) {
7191 const clang::RecordType *record_type =
7192 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7193 const clang::RecordDecl *record_decl = record_type->getDecl();
Enrico Granata36f51e42015-12-18 22:41:25 +00007194
Kate Stoneb9c1b512016-09-06 20:57:50 +00007195 assert(record_decl);
7196 uint32_t child_idx = 0;
7197
7198 const clang::CXXRecordDecl *cxx_record_decl =
7199 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7200
7201 // Try and find a field that matches NAME
7202 clang::RecordDecl::field_iterator field, field_end;
7203 llvm::StringRef name_sref(name);
7204 for (field = record_decl->field_begin(),
7205 field_end = record_decl->field_end();
7206 field != field_end; ++field, ++child_idx) {
7207 llvm::StringRef field_name = field->getName();
7208 if (field_name.empty()) {
7209 CompilerType field_type(getASTContext(), field->getType());
7210 child_indexes.push_back(child_idx);
7211 if (field_type.GetIndexOfChildMemberWithName(
7212 name, omit_empty_base_classes, child_indexes))
7213 return child_indexes.size();
7214 child_indexes.pop_back();
7215
7216 } else if (field_name.equals(name_sref)) {
7217 // We have to add on the number of base classes to this index!
7218 child_indexes.push_back(
7219 child_idx + ClangASTContext::GetNumBaseClasses(
7220 cxx_record_decl, omit_empty_base_classes));
7221 return child_indexes.size();
7222 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007223 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007224
Kate Stoneb9c1b512016-09-06 20:57:50 +00007225 if (cxx_record_decl) {
7226 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7227
7228 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7229
7230 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7231 // Didn't find things easily, lets let clang do its thang...
7232 clang::IdentifierInfo &ident_ref =
7233 getASTContext()->Idents.get(name_sref);
7234 clang::DeclarationName decl_name(&ident_ref);
7235
7236 clang::CXXBasePaths paths;
7237 if (cxx_record_decl->lookupInBases(
7238 [decl_name](const clang::CXXBaseSpecifier *specifier,
7239 clang::CXXBasePath &path) {
7240 return clang::CXXRecordDecl::FindOrdinaryMember(
7241 specifier, path, decl_name);
7242 },
7243 paths)) {
7244 clang::CXXBasePaths::const_paths_iterator path,
7245 path_end = paths.end();
7246 for (path = paths.begin(); path != path_end; ++path) {
7247 const size_t num_path_elements = path->size();
7248 for (size_t e = 0; e < num_path_elements; ++e) {
7249 clang::CXXBasePathElement elem = (*path)[e];
7250
7251 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7252 omit_empty_base_classes);
7253 if (child_idx == UINT32_MAX) {
7254 child_indexes.clear();
7255 return 0;
7256 } else {
7257 child_indexes.push_back(child_idx);
7258 parent_record_decl = llvm::cast<clang::RecordDecl>(
7259 elem.Base->getType()
7260 ->getAs<clang::RecordType>()
7261 ->getDecl());
7262 }
7263 }
7264 for (clang::NamedDecl *path_decl : path->Decls) {
7265 child_idx = GetIndexForRecordChild(
7266 parent_record_decl, path_decl, omit_empty_base_classes);
7267 if (child_idx == UINT32_MAX) {
7268 child_indexes.clear();
7269 return 0;
7270 } else {
7271 child_indexes.push_back(child_idx);
7272 }
7273 }
7274 }
7275 return child_indexes.size();
7276 }
7277 }
7278 }
7279 break;
7280
7281 case clang::Type::ObjCObject:
7282 case clang::Type::ObjCInterface:
7283 if (GetCompleteType(type)) {
7284 llvm::StringRef name_sref(name);
7285 const clang::ObjCObjectType *objc_class_type =
7286 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7287 assert(objc_class_type);
7288 if (objc_class_type) {
7289 uint32_t child_idx = 0;
7290 clang::ObjCInterfaceDecl *class_interface_decl =
7291 objc_class_type->getInterface();
7292
7293 if (class_interface_decl) {
7294 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7295 ivar_end = class_interface_decl->ivar_end();
7296 clang::ObjCInterfaceDecl *superclass_interface_decl =
7297 class_interface_decl->getSuperClass();
7298
7299 for (ivar_pos = class_interface_decl->ivar_begin();
7300 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7301 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7302
7303 if (ivar_decl->getName().equals(name_sref)) {
7304 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7305 (omit_empty_base_classes &&
7306 ObjCDeclHasIVars(superclass_interface_decl, true)))
7307 ++child_idx;
7308
7309 child_indexes.push_back(child_idx);
7310 return child_indexes.size();
7311 }
7312 }
7313
7314 if (superclass_interface_decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00007315 // The super class index is always zero for ObjC classes, so we
7316 // push it onto the child indexes in case we find an ivar in our
7317 // superclass...
Kate Stoneb9c1b512016-09-06 20:57:50 +00007318 child_indexes.push_back(0);
7319
7320 CompilerType superclass_clang_type(
7321 getASTContext(), getASTContext()->getObjCInterfaceType(
7322 superclass_interface_decl));
7323 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7324 name, omit_empty_base_classes, child_indexes)) {
Adrian Prantl05097242018-04-30 16:49:04 +00007325 // We did find an ivar in a superclass so just return the
7326 // results!
Kate Stoneb9c1b512016-09-06 20:57:50 +00007327 return child_indexes.size();
7328 }
7329
Adrian Prantl05097242018-04-30 16:49:04 +00007330 // We didn't find an ivar matching "name" in our superclass, pop
7331 // the superclass zero index that we pushed on above.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007332 child_indexes.pop_back();
7333 }
7334 }
7335 }
7336 }
7337 break;
7338
7339 case clang::Type::ObjCObjectPointer: {
7340 CompilerType objc_object_clang_type(
7341 getASTContext(),
7342 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7343 ->getPointeeType());
7344 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7345 name, omit_empty_base_classes, child_indexes);
7346 } break;
7347
7348 case clang::Type::ConstantArray: {
7349 // const clang::ConstantArrayType *array =
7350 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7351 // const uint64_t element_count =
7352 // array->getSize().getLimitedValue();
7353 //
7354 // if (idx < element_count)
7355 // {
7356 // std::pair<uint64_t, unsigned> field_type_info =
7357 // ast->getTypeInfo(array->getElementType());
7358 //
7359 // char element_name[32];
7360 // ::snprintf (element_name, sizeof (element_name),
7361 // "%s[%u]", parent_name ? parent_name : "", idx);
7362 //
7363 // child_name.assign(element_name);
7364 // assert(field_type_info.first % 8 == 0);
7365 // child_byte_size = field_type_info.first / 8;
7366 // child_byte_offset = idx * child_byte_size;
7367 // return array->getElementType().getAsOpaquePtr();
7368 // }
7369 } break;
7370
7371 // case clang::Type::MemberPointerType:
7372 // {
7373 // MemberPointerType *mem_ptr_type =
7374 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7375 // clang::QualType pointee_type =
7376 // mem_ptr_type->getPointeeType();
7377 //
7378 // if (ClangASTContext::IsAggregateType
7379 // (pointee_type.getAsOpaquePtr()))
7380 // {
7381 // return GetIndexOfChildWithName (ast,
7382 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7383 // name);
7384 // }
7385 // }
7386 // break;
7387 //
7388 case clang::Type::LValueReference:
7389 case clang::Type::RValueReference: {
7390 const clang::ReferenceType *reference_type =
7391 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7392 clang::QualType pointee_type(reference_type->getPointeeType());
7393 CompilerType pointee_clang_type(getASTContext(), pointee_type);
7394
7395 if (pointee_clang_type.IsAggregateType()) {
7396 return pointee_clang_type.GetIndexOfChildMemberWithName(
7397 name, omit_empty_base_classes, child_indexes);
7398 }
7399 } break;
7400
7401 case clang::Type::Pointer: {
7402 CompilerType pointee_clang_type(GetPointeeType(type));
7403
7404 if (pointee_clang_type.IsAggregateType()) {
7405 return pointee_clang_type.GetIndexOfChildMemberWithName(
7406 name, omit_empty_base_classes, child_indexes);
7407 }
7408 } break;
7409
7410 case clang::Type::Typedef:
7411 return CompilerType(getASTContext(),
7412 llvm::cast<clang::TypedefType>(qual_type)
7413 ->getDecl()
7414 ->getUnderlyingType())
7415 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7416 child_indexes);
7417
7418 case clang::Type::Auto:
7419 return CompilerType(
7420 getASTContext(),
7421 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7422 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7423 child_indexes);
7424
7425 case clang::Type::Elaborated:
7426 return CompilerType(
7427 getASTContext(),
7428 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7429 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7430 child_indexes);
7431
7432 case clang::Type::Paren:
7433 return CompilerType(getASTContext(),
7434 llvm::cast<clang::ParenType>(qual_type)->desugar())
7435 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7436 child_indexes);
7437
7438 default:
7439 break;
7440 }
7441 }
7442 return 0;
7443}
Greg Claytond8d4a572015-08-11 21:38:15 +00007444
7445// Get the index of the child of "clang_type" whose name matches. This function
7446// doesn't descend into the children, but only looks one level deep and name
7447// matches can include base class names.
7448
7449uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007450ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7451 const char *name,
7452 bool omit_empty_base_classes) {
7453 if (type && name && name[0]) {
7454 clang::QualType qual_type(GetCanonicalQualType(type));
Enrico Granata36f51e42015-12-18 22:41:25 +00007455
Kate Stoneb9c1b512016-09-06 20:57:50 +00007456 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7457
7458 switch (type_class) {
7459 case clang::Type::Record:
7460 if (GetCompleteType(type)) {
7461 const clang::RecordType *record_type =
7462 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7463 const clang::RecordDecl *record_decl = record_type->getDecl();
7464
7465 assert(record_decl);
7466 uint32_t child_idx = 0;
7467
7468 const clang::CXXRecordDecl *cxx_record_decl =
7469 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7470
7471 if (cxx_record_decl) {
7472 clang::CXXRecordDecl::base_class_const_iterator base_class,
7473 base_class_end;
7474 for (base_class = cxx_record_decl->bases_begin(),
7475 base_class_end = cxx_record_decl->bases_end();
7476 base_class != base_class_end; ++base_class) {
7477 // Skip empty base classes
7478 clang::CXXRecordDecl *base_class_decl =
7479 llvm::cast<clang::CXXRecordDecl>(
7480 base_class->getType()
7481 ->getAs<clang::RecordType>()
7482 ->getDecl());
7483 if (omit_empty_base_classes &&
Jonas Devliegherea6682a42018-12-15 00:15:33 +00007484 !ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00007485 continue;
7486
7487 CompilerType base_class_clang_type(getASTContext(),
7488 base_class->getType());
7489 std::string base_class_type_name(
7490 base_class_clang_type.GetTypeName().AsCString(""));
Jonas Devlieghere8d20cfd2018-12-21 22:46:10 +00007491 if (base_class_type_name == name)
Kate Stoneb9c1b512016-09-06 20:57:50 +00007492 return child_idx;
7493 ++child_idx;
7494 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007495 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007496
Kate Stoneb9c1b512016-09-06 20:57:50 +00007497 // Try and find a field that matches NAME
7498 clang::RecordDecl::field_iterator field, field_end;
7499 llvm::StringRef name_sref(name);
7500 for (field = record_decl->field_begin(),
7501 field_end = record_decl->field_end();
7502 field != field_end; ++field, ++child_idx) {
7503 if (field->getName().equals(name_sref))
7504 return child_idx;
7505 }
7506 }
7507 break;
7508
7509 case clang::Type::ObjCObject:
7510 case clang::Type::ObjCInterface:
7511 if (GetCompleteType(type)) {
7512 llvm::StringRef name_sref(name);
7513 const clang::ObjCObjectType *objc_class_type =
7514 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7515 assert(objc_class_type);
7516 if (objc_class_type) {
7517 uint32_t child_idx = 0;
7518 clang::ObjCInterfaceDecl *class_interface_decl =
7519 objc_class_type->getInterface();
7520
7521 if (class_interface_decl) {
7522 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7523 ivar_end = class_interface_decl->ivar_end();
7524 clang::ObjCInterfaceDecl *superclass_interface_decl =
7525 class_interface_decl->getSuperClass();
7526
7527 for (ivar_pos = class_interface_decl->ivar_begin();
7528 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7529 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7530
7531 if (ivar_decl->getName().equals(name_sref)) {
7532 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7533 (omit_empty_base_classes &&
7534 ObjCDeclHasIVars(superclass_interface_decl, true)))
7535 ++child_idx;
7536
7537 return child_idx;
7538 }
7539 }
7540
7541 if (superclass_interface_decl) {
7542 if (superclass_interface_decl->getName().equals(name_sref))
7543 return 0;
7544 }
7545 }
7546 }
7547 }
7548 break;
7549
7550 case clang::Type::ObjCObjectPointer: {
7551 CompilerType pointee_clang_type(
7552 getASTContext(),
7553 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7554 ->getPointeeType());
7555 return pointee_clang_type.GetIndexOfChildWithName(
7556 name, omit_empty_base_classes);
7557 } break;
7558
7559 case clang::Type::ConstantArray: {
7560 // const clang::ConstantArrayType *array =
7561 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7562 // const uint64_t element_count =
7563 // array->getSize().getLimitedValue();
7564 //
7565 // if (idx < element_count)
7566 // {
7567 // std::pair<uint64_t, unsigned> field_type_info =
7568 // ast->getTypeInfo(array->getElementType());
7569 //
7570 // char element_name[32];
7571 // ::snprintf (element_name, sizeof (element_name),
7572 // "%s[%u]", parent_name ? parent_name : "", idx);
7573 //
7574 // child_name.assign(element_name);
7575 // assert(field_type_info.first % 8 == 0);
7576 // child_byte_size = field_type_info.first / 8;
7577 // child_byte_offset = idx * child_byte_size;
7578 // return array->getElementType().getAsOpaquePtr();
7579 // }
7580 } break;
7581
7582 // case clang::Type::MemberPointerType:
7583 // {
7584 // MemberPointerType *mem_ptr_type =
7585 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7586 // clang::QualType pointee_type =
7587 // mem_ptr_type->getPointeeType();
7588 //
7589 // if (ClangASTContext::IsAggregateType
7590 // (pointee_type.getAsOpaquePtr()))
7591 // {
7592 // return GetIndexOfChildWithName (ast,
7593 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7594 // name);
7595 // }
7596 // }
7597 // break;
7598 //
7599 case clang::Type::LValueReference:
7600 case clang::Type::RValueReference: {
7601 const clang::ReferenceType *reference_type =
7602 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7603 CompilerType pointee_type(getASTContext(),
7604 reference_type->getPointeeType());
7605
7606 if (pointee_type.IsAggregateType()) {
7607 return pointee_type.GetIndexOfChildWithName(name,
7608 omit_empty_base_classes);
7609 }
7610 } break;
7611
7612 case clang::Type::Pointer: {
7613 const clang::PointerType *pointer_type =
7614 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7615 CompilerType pointee_type(getASTContext(),
7616 pointer_type->getPointeeType());
7617
7618 if (pointee_type.IsAggregateType()) {
7619 return pointee_type.GetIndexOfChildWithName(name,
7620 omit_empty_base_classes);
7621 } else {
7622 // if (parent_name)
7623 // {
7624 // child_name.assign(1, '*');
7625 // child_name += parent_name;
7626 // }
7627 //
7628 // // We have a pointer to an simple type
7629 // if (idx == 0)
7630 // {
7631 // std::pair<uint64_t, unsigned> clang_type_info
7632 // = ast->getTypeInfo(pointee_type);
7633 // assert(clang_type_info.first % 8 == 0);
7634 // child_byte_size = clang_type_info.first / 8;
7635 // child_byte_offset = 0;
7636 // return pointee_type.getAsOpaquePtr();
7637 // }
7638 }
7639 } break;
7640
7641 case clang::Type::Auto:
7642 return CompilerType(
7643 getASTContext(),
7644 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7645 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7646
7647 case clang::Type::Elaborated:
7648 return CompilerType(
7649 getASTContext(),
7650 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7651 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7652
7653 case clang::Type::Paren:
7654 return CompilerType(getASTContext(),
7655 llvm::cast<clang::ParenType>(qual_type)->desugar())
7656 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7657
7658 case clang::Type::Typedef:
7659 return CompilerType(getASTContext(),
7660 llvm::cast<clang::TypedefType>(qual_type)
7661 ->getDecl()
7662 ->getUnderlyingType())
7663 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7664
7665 default:
7666 break;
7667 }
7668 }
7669 return UINT32_MAX;
7670}
Greg Claytond8d4a572015-08-11 21:38:15 +00007671
7672size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007673ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7674 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007675 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007676
Kate Stoneb9c1b512016-09-06 20:57:50 +00007677 clang::QualType qual_type(GetCanonicalQualType(type));
7678 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7679 switch (type_class) {
7680 case clang::Type::Record:
7681 if (GetCompleteType(type)) {
7682 const clang::CXXRecordDecl *cxx_record_decl =
7683 qual_type->getAsCXXRecordDecl();
7684 if (cxx_record_decl) {
7685 const clang::ClassTemplateSpecializationDecl *template_decl =
7686 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7687 cxx_record_decl);
7688 if (template_decl)
7689 return template_decl->getTemplateArgs().size();
7690 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007691 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007692 break;
7693
7694 case clang::Type::Typedef:
7695 return (CompilerType(getASTContext(),
7696 llvm::cast<clang::TypedefType>(qual_type)
7697 ->getDecl()
7698 ->getUnderlyingType()))
7699 .GetNumTemplateArguments();
7700
7701 case clang::Type::Auto:
7702 return (CompilerType(
7703 getASTContext(),
7704 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7705 .GetNumTemplateArguments();
7706
7707 case clang::Type::Elaborated:
7708 return (CompilerType(
7709 getASTContext(),
7710 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7711 .GetNumTemplateArguments();
7712
7713 case clang::Type::Paren:
7714 return (CompilerType(getASTContext(),
7715 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7716 .GetNumTemplateArguments();
7717
7718 default:
7719 break;
7720 }
7721
7722 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007723}
7724
Pavel Labath769b21e2017-11-13 14:26:21 +00007725const clang::ClassTemplateSpecializationDecl *
7726ClangASTContext::GetAsTemplateSpecialization(
7727 lldb::opaque_compiler_type_t type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00007728 if (!type)
Pavel Labath769b21e2017-11-13 14:26:21 +00007729 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007730
7731 clang::QualType qual_type(GetCanonicalQualType(type));
7732 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7733 switch (type_class) {
Pavel Labath769b21e2017-11-13 14:26:21 +00007734 case clang::Type::Record: {
7735 if (! GetCompleteType(type))
7736 return nullptr;
7737 const clang::CXXRecordDecl *cxx_record_decl =
7738 qual_type->getAsCXXRecordDecl();
7739 if (!cxx_record_decl)
7740 return nullptr;
7741 return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7742 cxx_record_decl);
7743 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007744
7745 case clang::Type::Typedef:
Pavel Labath769b21e2017-11-13 14:26:21 +00007746 return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type)
7747 ->getDecl()
7748 ->getUnderlyingType()
7749 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007750
7751 case clang::Type::Auto:
Pavel Labath769b21e2017-11-13 14:26:21 +00007752 return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type)
7753 ->getDeducedType()
7754 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007755
7756 case clang::Type::Elaborated:
Pavel Labath769b21e2017-11-13 14:26:21 +00007757 return GetAsTemplateSpecialization(
7758 llvm::cast<clang::ElaboratedType>(qual_type)
7759 ->getNamedType()
7760 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007761
7762 case clang::Type::Paren:
Pavel Labath769b21e2017-11-13 14:26:21 +00007763 return GetAsTemplateSpecialization(
7764 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007765
7766 default:
Pavel Labath769b21e2017-11-13 14:26:21 +00007767 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007768 }
Pavel Labath769b21e2017-11-13 14:26:21 +00007769}
7770
7771lldb::TemplateArgumentKind
7772ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
7773 size_t arg_idx) {
7774 const clang::ClassTemplateSpecializationDecl *template_decl =
7775 GetAsTemplateSpecialization(type);
7776 if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size())
7777 return eTemplateArgumentKindNull;
7778
7779 switch (template_decl->getTemplateArgs()[arg_idx].getKind()) {
7780 case clang::TemplateArgument::Null:
7781 return eTemplateArgumentKindNull;
7782
7783 case clang::TemplateArgument::NullPtr:
7784 return eTemplateArgumentKindNullPtr;
7785
7786 case clang::TemplateArgument::Type:
7787 return eTemplateArgumentKindType;
7788
7789 case clang::TemplateArgument::Declaration:
7790 return eTemplateArgumentKindDeclaration;
7791
7792 case clang::TemplateArgument::Integral:
7793 return eTemplateArgumentKindIntegral;
7794
7795 case clang::TemplateArgument::Template:
7796 return eTemplateArgumentKindTemplate;
7797
7798 case clang::TemplateArgument::TemplateExpansion:
7799 return eTemplateArgumentKindTemplateExpansion;
7800
7801 case clang::TemplateArgument::Expression:
7802 return eTemplateArgumentKindExpression;
7803
7804 case clang::TemplateArgument::Pack:
7805 return eTemplateArgumentKindPack;
7806 }
7807 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
7808}
7809
7810CompilerType
7811ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
7812 size_t idx) {
7813 const clang::ClassTemplateSpecializationDecl *template_decl =
7814 GetAsTemplateSpecialization(type);
7815 if (!template_decl || idx >= template_decl->getTemplateArgs().size())
7816 return CompilerType();
7817
7818 const clang::TemplateArgument &template_arg =
7819 template_decl->getTemplateArgs()[idx];
7820 if (template_arg.getKind() != clang::TemplateArgument::Type)
7821 return CompilerType();
7822
7823 return CompilerType(getASTContext(), template_arg.getAsType());
7824}
7825
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00007826Optional<CompilerType::IntegralTemplateArgument>
Pavel Labath769b21e2017-11-13 14:26:21 +00007827ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
7828 size_t idx) {
7829 const clang::ClassTemplateSpecializationDecl *template_decl =
7830 GetAsTemplateSpecialization(type);
7831 if (! template_decl || idx >= template_decl->getTemplateArgs().size())
Pavel Labathf59056f2017-11-30 10:16:54 +00007832 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007833
7834 const clang::TemplateArgument &template_arg =
7835 template_decl->getTemplateArgs()[idx];
7836 if (template_arg.getKind() != clang::TemplateArgument::Integral)
Pavel Labathf59056f2017-11-30 10:16:54 +00007837 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007838
Pavel Labathf59056f2017-11-30 10:16:54 +00007839 return {{template_arg.getAsIntegral(),
7840 CompilerType(getASTContext(), template_arg.getIntegralType())}};
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007841}
7842
Kate Stoneb9c1b512016-09-06 20:57:50 +00007843CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7844 if (type)
7845 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7846 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007847}
7848
Kate Stoneb9c1b512016-09-06 20:57:50 +00007849clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7850 const clang::EnumType *enutype =
7851 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7852 if (enutype)
7853 return enutype->getDecl();
Konrad Kleine248a1302019-05-23 11:14:47 +00007854 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007855}
7856
7857clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7858 const clang::RecordType *record_type =
7859 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7860 if (record_type)
7861 return record_type->getDecl();
7862 return nullptr;
7863}
7864
7865clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
Zachary Turner1639c6b2018-12-17 16:15:13 +00007866 return ClangUtil::GetAsTagDecl(type);
Greg Claytone6b36cd2015-12-08 01:02:08 +00007867}
7868
Aleksandr Urakov709426b2018-09-10 08:08:43 +00007869clang::TypedefNameDecl *
7870ClangASTContext::GetAsTypedefDecl(const CompilerType &type) {
7871 const clang::TypedefType *typedef_type =
7872 llvm::dyn_cast<clang::TypedefType>(ClangUtil::GetQualType(type));
7873 if (typedef_type)
7874 return typedef_type->getDecl();
7875 return nullptr;
7876}
7877
Greg Claytond8d4a572015-08-11 21:38:15 +00007878clang::CXXRecordDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007879ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7880 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
Greg Claytond8d4a572015-08-11 21:38:15 +00007881}
7882
7883clang::ObjCInterfaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007884ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7885 const clang::ObjCObjectType *objc_class_type =
7886 llvm::dyn_cast<clang::ObjCObjectType>(
7887 ClangUtil::GetCanonicalQualType(type));
7888 if (objc_class_type)
7889 return objc_class_type->getInterface();
7890 return nullptr;
7891}
7892
7893clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007894 const CompilerType &type, llvm::StringRef name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00007895 const CompilerType &field_clang_type, AccessType access,
7896 uint32_t bitfield_bit_size) {
7897 if (!type.IsValid() || !field_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00007898 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007899 ClangASTContext *ast =
7900 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7901 if (!ast)
7902 return nullptr;
7903 clang::ASTContext *clang_ast = ast->getASTContext();
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007904 clang::IdentifierInfo *ident = nullptr;
7905 if (!name.empty())
7906 ident = &clang_ast->Idents.get(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00007907
7908 clang::FieldDecl *field = nullptr;
7909
7910 clang::Expr *bit_width = nullptr;
7911 if (bitfield_bit_size != 0) {
7912 llvm::APInt bitfield_bit_size_apint(
7913 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7914 bit_width = new (*clang_ast)
7915 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7916 clang_ast->IntTy, clang::SourceLocation());
7917 }
7918
7919 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7920 if (record_decl) {
7921 field = clang::FieldDecl::Create(
7922 *clang_ast, record_decl, clang::SourceLocation(),
7923 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007924 ident, // Identifier
7925 ClangUtil::GetQualType(field_clang_type), // Field type
7926 nullptr, // TInfo *
7927 bit_width, // BitWidth
7928 false, // Mutable
7929 clang::ICIS_NoInit); // HasInit
Kate Stoneb9c1b512016-09-06 20:57:50 +00007930
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007931 if (name.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +00007932 // Determine whether this field corresponds to an anonymous struct or
7933 // union.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007934 if (const clang::TagType *TagT =
7935 field->getType()->getAs<clang::TagType>()) {
7936 if (clang::RecordDecl *Rec =
7937 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7938 if (!Rec->getDeclName()) {
7939 Rec->setAnonymousStructOrUnion(true);
7940 field->setImplicit();
7941 }
7942 }
7943 }
7944
7945 if (field) {
7946 field->setAccess(
7947 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7948
7949 record_decl->addDecl(field);
7950
7951#ifdef LLDB_CONFIGURATION_DEBUG
7952 VerifyDecl(field);
7953#endif
7954 }
7955 } else {
7956 clang::ObjCInterfaceDecl *class_interface_decl =
7957 ast->GetAsObjCInterfaceDecl(type);
7958
7959 if (class_interface_decl) {
7960 const bool is_synthesized = false;
7961
7962 field_clang_type.GetCompleteType();
7963
7964 field = clang::ObjCIvarDecl::Create(
7965 *clang_ast, class_interface_decl, clang::SourceLocation(),
7966 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007967 ident, // Identifier
7968 ClangUtil::GetQualType(field_clang_type), // Field type
7969 nullptr, // TypeSourceInfo *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007970 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7971 is_synthesized);
7972
7973 if (field) {
7974 class_interface_decl->addDecl(field);
7975
7976#ifdef LLDB_CONFIGURATION_DEBUG
7977 VerifyDecl(field);
7978#endif
7979 }
7980 }
7981 }
7982 return field;
Greg Claytond8d4a572015-08-11 21:38:15 +00007983}
7984
Kate Stoneb9c1b512016-09-06 20:57:50 +00007985void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7986 if (!type)
7987 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007988
Kate Stoneb9c1b512016-09-06 20:57:50 +00007989 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7990 if (!ast)
7991 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007992
Kate Stoneb9c1b512016-09-06 20:57:50 +00007993 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007994
Kate Stoneb9c1b512016-09-06 20:57:50 +00007995 if (!record_decl)
7996 return;
7997
7998 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7999
8000 IndirectFieldVector indirect_fields;
8001 clang::RecordDecl::field_iterator field_pos;
8002 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
8003 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
8004 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
8005 last_field_pos = field_pos++) {
8006 if (field_pos->isAnonymousStructOrUnion()) {
8007 clang::QualType field_qual_type = field_pos->getType();
8008
8009 const clang::RecordType *field_record_type =
8010 field_qual_type->getAs<clang::RecordType>();
8011
8012 if (!field_record_type)
8013 continue;
8014
8015 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
8016
8017 if (!field_record_decl)
8018 continue;
8019
8020 for (clang::RecordDecl::decl_iterator
8021 di = field_record_decl->decls_begin(),
8022 de = field_record_decl->decls_end();
8023 di != de; ++di) {
8024 if (clang::FieldDecl *nested_field_decl =
8025 llvm::dyn_cast<clang::FieldDecl>(*di)) {
8026 clang::NamedDecl **chain =
8027 new (*ast->getASTContext()) clang::NamedDecl *[2];
8028 chain[0] = *field_pos;
8029 chain[1] = nested_field_decl;
8030 clang::IndirectFieldDecl *indirect_field =
8031 clang::IndirectFieldDecl::Create(
8032 *ast->getASTContext(), record_decl, clang::SourceLocation(),
8033 nested_field_decl->getIdentifier(),
8034 nested_field_decl->getType(), {chain, 2});
8035
8036 indirect_field->setImplicit();
8037
8038 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
8039 field_pos->getAccess(), nested_field_decl->getAccess()));
8040
8041 indirect_fields.push_back(indirect_field);
8042 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
8043 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
8044 size_t nested_chain_size =
8045 nested_indirect_field_decl->getChainingSize();
8046 clang::NamedDecl **chain = new (*ast->getASTContext())
8047 clang::NamedDecl *[nested_chain_size + 1];
8048 chain[0] = *field_pos;
8049
8050 int chain_index = 1;
8051 for (clang::IndirectFieldDecl::chain_iterator
8052 nci = nested_indirect_field_decl->chain_begin(),
8053 nce = nested_indirect_field_decl->chain_end();
8054 nci < nce; ++nci) {
8055 chain[chain_index] = *nci;
8056 chain_index++;
8057 }
8058
8059 clang::IndirectFieldDecl *indirect_field =
8060 clang::IndirectFieldDecl::Create(
8061 *ast->getASTContext(), record_decl, clang::SourceLocation(),
8062 nested_indirect_field_decl->getIdentifier(),
8063 nested_indirect_field_decl->getType(),
8064 {chain, nested_chain_size + 1});
8065
8066 indirect_field->setImplicit();
8067
8068 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
8069 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
8070
8071 indirect_fields.push_back(indirect_field);
Greg Claytond8d4a572015-08-11 21:38:15 +00008072 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008073 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008074 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008075 }
8076
Adrian Prantl05097242018-04-30 16:49:04 +00008077 // Check the last field to see if it has an incomplete array type as its last
8078 // member and if it does, the tell the record decl about it
Kate Stoneb9c1b512016-09-06 20:57:50 +00008079 if (last_field_pos != field_end_pos) {
8080 if (last_field_pos->getType()->isIncompleteArrayType())
8081 record_decl->hasFlexibleArrayMember();
8082 }
8083
8084 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
8085 ife = indirect_fields.end();
8086 ifi < ife; ++ifi) {
8087 record_decl->addDecl(*ifi);
8088 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008089}
8090
Kate Stoneb9c1b512016-09-06 20:57:50 +00008091void ClangASTContext::SetIsPacked(const CompilerType &type) {
8092 if (type) {
8093 ClangASTContext *ast =
8094 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8095 if (ast) {
8096 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
8097
8098 if (!record_decl)
Greg Claytonf73034f2015-09-08 18:15:05 +00008099 return;
8100
Kate Stoneb9c1b512016-09-06 20:57:50 +00008101 record_decl->addAttr(
8102 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
Greg Claytond8d4a572015-08-11 21:38:15 +00008103 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008104 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008105}
8106
Kate Stoneb9c1b512016-09-06 20:57:50 +00008107clang::VarDecl *ClangASTContext::AddVariableToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008108 const CompilerType &type, llvm::StringRef name,
8109 const CompilerType &var_type, AccessType access) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00008110 if (!type.IsValid() || !var_type.IsValid())
8111 return nullptr;
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008112
Kate Stoneb9c1b512016-09-06 20:57:50 +00008113 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8114 if (!ast)
8115 return nullptr;
8116
8117 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008118 if (!record_decl)
8119 return nullptr;
8120
8121 clang::VarDecl *var_decl = nullptr;
8122 clang::IdentifierInfo *ident = nullptr;
8123 if (!name.empty())
8124 ident = &ast->getASTContext()->Idents.get(name);
8125
8126 var_decl = clang::VarDecl::Create(
8127 *ast->getASTContext(), // ASTContext &
8128 record_decl, // DeclContext *
8129 clang::SourceLocation(), // clang::SourceLocation StartLoc
8130 clang::SourceLocation(), // clang::SourceLocation IdLoc
8131 ident, // clang::IdentifierInfo *
8132 ClangUtil::GetQualType(var_type), // Variable clang::QualType
8133 nullptr, // TypeSourceInfo *
8134 clang::SC_Static); // StorageClass
8135 if (!var_decl)
8136 return nullptr;
8137
8138 var_decl->setAccess(
8139 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
8140 record_decl->addDecl(var_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008141
Greg Claytond8d4a572015-08-11 21:38:15 +00008142#ifdef LLDB_CONFIGURATION_DEBUG
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008143 VerifyDecl(var_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008144#endif
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008145
Kate Stoneb9c1b512016-09-06 20:57:50 +00008146 return var_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008147}
8148
Kate Stoneb9c1b512016-09-06 20:57:50 +00008149clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
Davide Italiano675767a2018-03-27 19:40:50 +00008150 lldb::opaque_compiler_type_t type, const char *name, const char *mangled_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008151 const CompilerType &method_clang_type, lldb::AccessType access,
8152 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
8153 bool is_attr_used, bool is_artificial) {
8154 if (!type || !method_clang_type.IsValid() || name == nullptr ||
8155 name[0] == '\0')
8156 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008157
Kate Stoneb9c1b512016-09-06 20:57:50 +00008158 clang::QualType record_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008159
Kate Stoneb9c1b512016-09-06 20:57:50 +00008160 clang::CXXRecordDecl *cxx_record_decl =
8161 record_qual_type->getAsCXXRecordDecl();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008162
Kate Stoneb9c1b512016-09-06 20:57:50 +00008163 if (cxx_record_decl == nullptr)
8164 return nullptr;
8165
8166 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8167
8168 clang::CXXMethodDecl *cxx_method_decl = nullptr;
8169
8170 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
8171
8172 const clang::FunctionType *function_type =
8173 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
8174
8175 if (function_type == nullptr)
8176 return nullptr;
8177
8178 const clang::FunctionProtoType *method_function_prototype(
8179 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
8180
8181 if (!method_function_prototype)
8182 return nullptr;
8183
8184 unsigned int num_params = method_function_prototype->getNumParams();
8185
8186 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
8187 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
8188
8189 if (is_artificial)
8190 return nullptr; // skip everything artificial
8191
Richard Smith36851a62019-05-09 04:40:57 +00008192 const clang::ExplicitSpecifier explicit_spec(
8193 nullptr /*expr*/, is_explicit
8194 ? clang::ExplicitSpecKind::ResolvedTrue
8195 : clang::ExplicitSpecKind::ResolvedFalse);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008196 if (name[0] == '~') {
8197 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
8198 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8199 clang::DeclarationNameInfo(
8200 getASTContext()->DeclarationNames.getCXXDestructorName(
8201 getASTContext()->getCanonicalType(record_qual_type)),
8202 clang::SourceLocation()),
8203 method_qual_type, nullptr, is_inline, is_artificial);
8204 cxx_method_decl = cxx_dtor_decl;
8205 } else if (decl_name == cxx_record_decl->getDeclName()) {
8206 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
8207 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8208 clang::DeclarationNameInfo(
8209 getASTContext()->DeclarationNames.getCXXConstructorName(
8210 getASTContext()->getCanonicalType(record_qual_type)),
8211 clang::SourceLocation()),
8212 method_qual_type,
8213 nullptr, // TypeSourceInfo *
Gauthier Harnisch796ed032019-06-14 08:56:20 +00008214 explicit_spec, is_inline, is_artificial, CSK_unspecified);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008215 cxx_method_decl = cxx_ctor_decl;
8216 } else {
8217 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
8218 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
8219
8220 if (IsOperator(name, op_kind)) {
8221 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
Adrian Prantl05097242018-04-30 16:49:04 +00008222 // Check the number of operator parameters. Sometimes we have seen bad
8223 // DWARF that doesn't correctly describe operators and if we try to
8224 // create a method and add it to the class, clang will assert and
8225 // crash, so we need to make sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00008226 const bool is_method = true;
8227 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
8228 is_method, op_kind, num_params))
8229 return nullptr;
8230 cxx_method_decl = clang::CXXMethodDecl::Create(
8231 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8232 clang::DeclarationNameInfo(
8233 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
8234 clang::SourceLocation()),
8235 method_qual_type,
8236 nullptr, // TypeSourceInfo *
Gauthier Harnisch796ed032019-06-14 08:56:20 +00008237 SC, is_inline, CSK_unspecified, clang::SourceLocation());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008238 } else if (num_params == 0) {
8239 // Conversion operators don't take params...
8240 cxx_method_decl = clang::CXXConversionDecl::Create(
8241 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8242 clang::DeclarationNameInfo(
8243 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
8244 getASTContext()->getCanonicalType(
8245 function_type->getReturnType())),
8246 clang::SourceLocation()),
8247 method_qual_type,
8248 nullptr, // TypeSourceInfo *
Gauthier Harnisch796ed032019-06-14 08:56:20 +00008249 is_inline, explicit_spec, CSK_unspecified,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008250 clang::SourceLocation());
8251 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008252 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008253
8254 if (cxx_method_decl == nullptr) {
8255 cxx_method_decl = clang::CXXMethodDecl::Create(
8256 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8257 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8258 method_qual_type,
8259 nullptr, // TypeSourceInfo *
Gauthier Harnisch796ed032019-06-14 08:56:20 +00008260 SC, is_inline, CSK_unspecified, clang::SourceLocation());
Greg Claytond8d4a572015-08-11 21:38:15 +00008261 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008262 }
8263
8264 clang::AccessSpecifier access_specifier =
8265 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8266
8267 cxx_method_decl->setAccess(access_specifier);
8268 cxx_method_decl->setVirtualAsWritten(is_virtual);
8269
8270 if (is_attr_used)
8271 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8272
Konrad Kleine248a1302019-05-23 11:14:47 +00008273 if (mangled_name != nullptr) {
Davide Italiano675767a2018-03-27 19:40:50 +00008274 cxx_method_decl->addAttr(
8275 clang::AsmLabelAttr::CreateImplicit(*getASTContext(), mangled_name));
8276 }
8277
Kate Stoneb9c1b512016-09-06 20:57:50 +00008278 // Populate the method decl with parameter decls
8279
8280 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8281
8282 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8283 params.push_back(clang::ParmVarDecl::Create(
8284 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8285 clang::SourceLocation(),
8286 nullptr, // anonymous
8287 method_function_prototype->getParamType(param_index), nullptr,
8288 clang::SC_None, nullptr));
8289 }
8290
8291 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8292
8293 cxx_record_decl->addDecl(cxx_method_decl);
8294
8295 // Sometimes the debug info will mention a constructor (default/copy/move),
8296 // destructor, or assignment operator (copy/move) but there won't be any
8297 // version of this in the code. So we check if the function was artificially
8298 // generated and if it is trivial and this lets the compiler/backend know
8299 // that it can inline the IR for these when it needs to and we can avoid a
8300 // "missing function" error when running expressions.
8301
8302 if (is_artificial) {
8303 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8304 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8305 (cxx_ctor_decl->isCopyConstructor() &&
8306 cxx_record_decl->hasTrivialCopyConstructor()) ||
8307 (cxx_ctor_decl->isMoveConstructor() &&
8308 cxx_record_decl->hasTrivialMoveConstructor()))) {
8309 cxx_ctor_decl->setDefaulted();
8310 cxx_ctor_decl->setTrivial(true);
8311 } else if (cxx_dtor_decl) {
8312 if (cxx_record_decl->hasTrivialDestructor()) {
8313 cxx_dtor_decl->setDefaulted();
8314 cxx_dtor_decl->setTrivial(true);
8315 }
8316 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8317 cxx_record_decl->hasTrivialCopyAssignment()) ||
8318 (cxx_method_decl->isMoveAssignmentOperator() &&
8319 cxx_record_decl->hasTrivialMoveAssignment())) {
8320 cxx_method_decl->setDefaulted();
8321 cxx_method_decl->setTrivial(true);
Greg Claytond8d4a572015-08-11 21:38:15 +00008322 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008323 }
8324
Greg Claytond8d4a572015-08-11 21:38:15 +00008325#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008326 VerifyDecl(cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008327#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008328
Kate Stoneb9c1b512016-09-06 20:57:50 +00008329 // printf ("decl->isPolymorphic() = %i\n",
8330 // cxx_record_decl->isPolymorphic());
8331 // printf ("decl->isAggregate() = %i\n",
8332 // cxx_record_decl->isAggregate());
8333 // printf ("decl->isPOD() = %i\n",
8334 // cxx_record_decl->isPOD());
8335 // printf ("decl->isEmpty() = %i\n",
8336 // cxx_record_decl->isEmpty());
8337 // printf ("decl->isAbstract() = %i\n",
8338 // cxx_record_decl->isAbstract());
8339 // printf ("decl->hasTrivialConstructor() = %i\n",
8340 // cxx_record_decl->hasTrivialConstructor());
8341 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8342 // cxx_record_decl->hasTrivialCopyConstructor());
8343 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8344 // cxx_record_decl->hasTrivialCopyAssignment());
8345 // printf ("decl->hasTrivialDestructor() = %i\n",
8346 // cxx_record_decl->hasTrivialDestructor());
8347 return cxx_method_decl;
8348}
Greg Claytond8d4a572015-08-11 21:38:15 +00008349
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00008350void ClangASTContext::AddMethodOverridesForCXXRecordType(
8351 lldb::opaque_compiler_type_t type) {
8352 if (auto *record = GetAsCXXRecordDecl(type))
8353 for (auto *method : record->methods())
8354 addOverridesForMethod(method);
8355}
8356
Greg Claytond8d4a572015-08-11 21:38:15 +00008357#pragma mark C++ Base Classes
8358
Zachary Turner970f38e2018-10-25 20:44:56 +00008359std::unique_ptr<clang::CXXBaseSpecifier>
Kate Stoneb9c1b512016-09-06 20:57:50 +00008360ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8361 AccessType access, bool is_virtual,
8362 bool base_of_class) {
Zachary Turner970f38e2018-10-25 20:44:56 +00008363 if (!type)
8364 return nullptr;
8365
8366 return llvm::make_unique<clang::CXXBaseSpecifier>(
8367 clang::SourceRange(), is_virtual, base_of_class,
8368 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8369 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8370 clang::SourceLocation());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008371}
8372
Zachary Turner970f38e2018-10-25 20:44:56 +00008373bool ClangASTContext::TransferBaseClasses(
Kate Stoneb9c1b512016-09-06 20:57:50 +00008374 lldb::opaque_compiler_type_t type,
Zachary Turner970f38e2018-10-25 20:44:56 +00008375 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases) {
8376 if (!type)
8377 return false;
8378 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8379 if (!cxx_record_decl)
8380 return false;
8381 std::vector<clang::CXXBaseSpecifier *> raw_bases;
8382 raw_bases.reserve(bases.size());
8383
8384 // Clang will make a copy of them, so it's ok that we pass pointers that we're
8385 // about to destroy.
8386 for (auto &b : bases)
8387 raw_bases.push_back(b.get());
8388 cxx_record_decl->setBases(raw_bases.data(), raw_bases.size());
8389 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008390}
8391
8392bool ClangASTContext::SetObjCSuperClass(
8393 const CompilerType &type, const CompilerType &superclass_clang_type) {
8394 ClangASTContext *ast =
8395 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8396 if (!ast)
8397 return false;
8398 clang::ASTContext *clang_ast = ast->getASTContext();
8399
8400 if (type && superclass_clang_type.IsValid() &&
8401 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8402 clang::ObjCInterfaceDecl *class_interface_decl =
8403 GetAsObjCInterfaceDecl(type);
8404 clang::ObjCInterfaceDecl *super_interface_decl =
8405 GetAsObjCInterfaceDecl(superclass_clang_type);
8406 if (class_interface_decl && super_interface_decl) {
8407 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8408 clang_ast->getObjCInterfaceType(super_interface_decl)));
8409 return true;
8410 }
8411 }
8412 return false;
8413}
8414
8415bool ClangASTContext::AddObjCClassProperty(
8416 const CompilerType &type, const char *property_name,
8417 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8418 const char *property_setter_name, const char *property_getter_name,
8419 uint32_t property_attributes, ClangASTMetadata *metadata) {
8420 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8421 property_name[0] == '\0')
8422 return false;
8423 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8424 if (!ast)
8425 return false;
8426 clang::ASTContext *clang_ast = ast->getASTContext();
8427
8428 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8429
8430 if (class_interface_decl) {
8431 CompilerType property_clang_type_to_access;
8432
8433 if (property_clang_type.IsValid())
8434 property_clang_type_to_access = property_clang_type;
8435 else if (ivar_decl)
8436 property_clang_type_to_access =
8437 CompilerType(clang_ast, ivar_decl->getType());
8438
8439 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8440 clang::TypeSourceInfo *prop_type_source;
8441 if (ivar_decl)
8442 prop_type_source =
8443 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8444 else
8445 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8446 ClangUtil::GetQualType(property_clang_type));
8447
8448 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8449 *clang_ast, class_interface_decl,
8450 clang::SourceLocation(), // Source Location
8451 &clang_ast->Idents.get(property_name),
8452 clang::SourceLocation(), // Source Location for AT
8453 clang::SourceLocation(), // Source location for (
8454 ivar_decl ? ivar_decl->getType()
8455 : ClangUtil::GetQualType(property_clang_type),
8456 prop_type_source);
8457
8458 if (property_decl) {
8459 if (metadata)
8460 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8461
8462 class_interface_decl->addDecl(property_decl);
8463
8464 clang::Selector setter_sel, getter_sel;
8465
8466 if (property_setter_name != nullptr) {
8467 std::string property_setter_no_colon(
8468 property_setter_name, strlen(property_setter_name) - 1);
8469 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008470 &clang_ast->Idents.get(property_setter_no_colon);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008471 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8472 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8473 std::string setter_sel_string("set");
8474 setter_sel_string.push_back(::toupper(property_name[0]));
8475 setter_sel_string.append(&property_name[1]);
8476 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008477 &clang_ast->Idents.get(setter_sel_string);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008478 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8479 }
8480 property_decl->setSetterName(setter_sel);
8481 property_decl->setPropertyAttributes(
8482 clang::ObjCPropertyDecl::OBJC_PR_setter);
8483
8484 if (property_getter_name != nullptr) {
8485 clang::IdentifierInfo *getter_ident =
8486 &clang_ast->Idents.get(property_getter_name);
8487 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8488 } else {
8489 clang::IdentifierInfo *getter_ident =
8490 &clang_ast->Idents.get(property_name);
8491 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8492 }
8493 property_decl->setGetterName(getter_sel);
8494 property_decl->setPropertyAttributes(
8495 clang::ObjCPropertyDecl::OBJC_PR_getter);
8496
8497 if (ivar_decl)
8498 property_decl->setPropertyIvarDecl(ivar_decl);
8499
8500 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8501 property_decl->setPropertyAttributes(
8502 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8503 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8504 property_decl->setPropertyAttributes(
8505 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8506 if (property_attributes & DW_APPLE_PROPERTY_assign)
8507 property_decl->setPropertyAttributes(
8508 clang::ObjCPropertyDecl::OBJC_PR_assign);
8509 if (property_attributes & DW_APPLE_PROPERTY_retain)
8510 property_decl->setPropertyAttributes(
8511 clang::ObjCPropertyDecl::OBJC_PR_retain);
8512 if (property_attributes & DW_APPLE_PROPERTY_copy)
8513 property_decl->setPropertyAttributes(
8514 clang::ObjCPropertyDecl::OBJC_PR_copy);
8515 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8516 property_decl->setPropertyAttributes(
8517 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8518 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8519 property_decl->setPropertyAttributes(
8520 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8521 if (property_attributes &
8522 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8523 property_decl->setPropertyAttributes(
8524 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8525 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8526 property_decl->setPropertyAttributes(
8527 clang::ObjCPropertyDecl::OBJC_PR_class);
8528
8529 const bool isInstance =
8530 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8531
8532 if (!getter_sel.isNull() &&
8533 !(isInstance
8534 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8535 : class_interface_decl->lookupClassMethod(getter_sel))) {
8536 const bool isVariadic = false;
8537 const bool isSynthesized = false;
8538 const bool isImplicitlyDeclared = true;
8539 const bool isDefined = false;
8540 const clang::ObjCMethodDecl::ImplementationControl impControl =
8541 clang::ObjCMethodDecl::None;
8542 const bool HasRelatedResultType = false;
8543
8544 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8545 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8546 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8547 nullptr, class_interface_decl, isInstance, isVariadic,
8548 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8549 HasRelatedResultType);
8550
8551 if (getter && metadata)
8552 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8553
8554 if (getter) {
8555 getter->setMethodParams(*clang_ast,
8556 llvm::ArrayRef<clang::ParmVarDecl *>(),
8557 llvm::ArrayRef<clang::SourceLocation>());
8558
8559 class_interface_decl->addDecl(getter);
8560 }
8561 }
8562
8563 if (!setter_sel.isNull() &&
8564 !(isInstance
8565 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8566 : class_interface_decl->lookupClassMethod(setter_sel))) {
8567 clang::QualType result_type = clang_ast->VoidTy;
8568 const bool isVariadic = false;
8569 const bool isSynthesized = false;
8570 const bool isImplicitlyDeclared = true;
8571 const bool isDefined = false;
8572 const clang::ObjCMethodDecl::ImplementationControl impControl =
8573 clang::ObjCMethodDecl::None;
8574 const bool HasRelatedResultType = false;
8575
8576 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8577 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8578 setter_sel, result_type, nullptr, class_interface_decl,
8579 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8580 isDefined, impControl, HasRelatedResultType);
8581
8582 if (setter && metadata)
8583 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8584
8585 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8586
8587 params.push_back(clang::ParmVarDecl::Create(
8588 *clang_ast, setter, clang::SourceLocation(),
8589 clang::SourceLocation(),
8590 nullptr, // anonymous
8591 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8592 clang::SC_Auto, nullptr));
8593
8594 if (setter) {
8595 setter->setMethodParams(
8596 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8597 llvm::ArrayRef<clang::SourceLocation>());
8598
8599 class_interface_decl->addDecl(setter);
8600 }
8601 }
8602
8603 return true;
8604 }
8605 }
8606 }
8607 return false;
8608}
8609
8610bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8611 bool check_superclass) {
8612 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8613 if (class_interface_decl)
8614 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8615 return false;
8616}
8617
8618clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8619 const CompilerType &type,
8620 const char *name, // the full symbol name as seen in the symbol table
8621 // (lldb::opaque_compiler_type_t type, "-[NString
8622 // stringWithCString:]")
8623 const CompilerType &method_clang_type, lldb::AccessType access,
8624 bool is_artificial, bool is_variadic) {
8625 if (!type || !method_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008626 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008627
Kate Stoneb9c1b512016-09-06 20:57:50 +00008628 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8629
8630 if (class_interface_decl == nullptr)
8631 return nullptr;
8632 ClangASTContext *lldb_ast =
8633 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8634 if (lldb_ast == nullptr)
8635 return nullptr;
8636 clang::ASTContext *ast = lldb_ast->getASTContext();
8637
8638 const char *selector_start = ::strchr(name, ' ');
8639 if (selector_start == nullptr)
8640 return nullptr;
8641
8642 selector_start++;
8643 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8644
8645 size_t len = 0;
8646 const char *start;
8647 // printf ("name = '%s'\n", name);
8648
8649 unsigned num_selectors_with_args = 0;
8650 for (start = selector_start; start && *start != '\0' && *start != ']';
8651 start += len) {
8652 len = ::strcspn(start, ":]");
8653 bool has_arg = (start[len] == ':');
8654 if (has_arg)
8655 ++num_selectors_with_args;
8656 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8657 if (has_arg)
8658 len += 1;
8659 }
8660
8661 if (selector_idents.size() == 0)
8662 return nullptr;
8663
8664 clang::Selector method_selector = ast->Selectors.getSelector(
8665 num_selectors_with_args ? selector_idents.size() : 0,
8666 selector_idents.data());
8667
8668 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8669
8670 // Populate the method decl with parameter decls
8671 const clang::Type *method_type(method_qual_type.getTypePtr());
8672
8673 if (method_type == nullptr)
8674 return nullptr;
8675
8676 const clang::FunctionProtoType *method_function_prototype(
8677 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8678
8679 if (!method_function_prototype)
8680 return nullptr;
8681
8682 bool is_synthesized = false;
8683 bool is_defined = false;
8684 clang::ObjCMethodDecl::ImplementationControl imp_control =
8685 clang::ObjCMethodDecl::None;
8686
8687 const unsigned num_args = method_function_prototype->getNumParams();
8688
8689 if (num_args != num_selectors_with_args)
8690 return nullptr; // some debug information is corrupt. We are not going to
8691 // deal with it.
8692
8693 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8694 *ast,
8695 clang::SourceLocation(), // beginLoc,
8696 clang::SourceLocation(), // endLoc,
8697 method_selector, method_function_prototype->getReturnType(),
8698 nullptr, // TypeSourceInfo *ResultTInfo,
8699 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8700 ClangUtil::GetQualType(type)),
8701 name[0] == '-', is_variadic, is_synthesized,
8702 true, // is_implicitly_declared; we force this to true because we don't
8703 // have source locations
8704 is_defined, imp_control, false /*has_related_result_type*/);
8705
8706 if (objc_method_decl == nullptr)
8707 return nullptr;
8708
8709 if (num_args > 0) {
8710 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8711
8712 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8713 params.push_back(clang::ParmVarDecl::Create(
8714 *ast, objc_method_decl, clang::SourceLocation(),
8715 clang::SourceLocation(),
8716 nullptr, // anonymous
8717 method_function_prototype->getParamType(param_index), nullptr,
8718 clang::SC_Auto, nullptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00008719 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008720
Kate Stoneb9c1b512016-09-06 20:57:50 +00008721 objc_method_decl->setMethodParams(
8722 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8723 llvm::ArrayRef<clang::SourceLocation>());
8724 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008725
Kate Stoneb9c1b512016-09-06 20:57:50 +00008726 class_interface_decl->addDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008727
Greg Claytond8d4a572015-08-11 21:38:15 +00008728#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008729 VerifyDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008730#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00008731
8732 return objc_method_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008733}
8734
Kate Stoneb9c1b512016-09-06 20:57:50 +00008735bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8736 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00008737 return false;
Greg Claytone6b36cd2015-12-08 01:02:08 +00008738
Kate Stoneb9c1b512016-09-06 20:57:50 +00008739 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008740
Kate Stoneb9c1b512016-09-06 20:57:50 +00008741 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8742 switch (type_class) {
8743 case clang::Type::Record: {
8744 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8745 if (cxx_record_decl)
8746 return cxx_record_decl->hasExternalLexicalStorage() ||
8747 cxx_record_decl->hasExternalVisibleStorage();
8748 } break;
Enrico Granata36f51e42015-12-18 22:41:25 +00008749
Kate Stoneb9c1b512016-09-06 20:57:50 +00008750 case clang::Type::Enum: {
8751 clang::EnumDecl *enum_decl =
8752 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8753 if (enum_decl)
8754 return enum_decl->hasExternalLexicalStorage() ||
8755 enum_decl->hasExternalVisibleStorage();
8756 } break;
8757
8758 case clang::Type::ObjCObject:
8759 case clang::Type::ObjCInterface: {
8760 const clang::ObjCObjectType *objc_class_type =
8761 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8762 assert(objc_class_type);
8763 if (objc_class_type) {
8764 clang::ObjCInterfaceDecl *class_interface_decl =
8765 objc_class_type->getInterface();
8766
8767 if (class_interface_decl)
8768 return class_interface_decl->hasExternalLexicalStorage() ||
8769 class_interface_decl->hasExternalVisibleStorage();
Greg Claytond8d4a572015-08-11 21:38:15 +00008770 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008771 } break;
8772
8773 case clang::Type::Typedef:
8774 return GetHasExternalStorage(CompilerType(
8775 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8776 ->getDecl()
8777 ->getUnderlyingType()
8778 .getAsOpaquePtr()));
8779
8780 case clang::Type::Auto:
8781 return GetHasExternalStorage(CompilerType(
8782 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8783 ->getDeducedType()
8784 .getAsOpaquePtr()));
8785
8786 case clang::Type::Elaborated:
8787 return GetHasExternalStorage(CompilerType(
8788 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8789 ->getNamedType()
8790 .getAsOpaquePtr()));
8791
8792 case clang::Type::Paren:
8793 return GetHasExternalStorage(CompilerType(
8794 type.GetTypeSystem(),
8795 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8796
8797 default:
8798 break;
8799 }
8800 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008801}
8802
Kate Stoneb9c1b512016-09-06 20:57:50 +00008803bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8804 bool has_extern) {
8805 if (!type)
8806 return false;
8807
8808 clang::QualType qual_type(GetCanonicalQualType(type));
8809
8810 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8811 switch (type_class) {
8812 case clang::Type::Record: {
8813 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8814 if (cxx_record_decl) {
8815 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8816 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8817 return true;
8818 }
8819 } break;
8820
8821 case clang::Type::Enum: {
8822 clang::EnumDecl *enum_decl =
8823 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8824 if (enum_decl) {
8825 enum_decl->setHasExternalLexicalStorage(has_extern);
8826 enum_decl->setHasExternalVisibleStorage(has_extern);
8827 return true;
8828 }
8829 } break;
8830
8831 case clang::Type::ObjCObject:
8832 case clang::Type::ObjCInterface: {
8833 const clang::ObjCObjectType *objc_class_type =
8834 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8835 assert(objc_class_type);
8836 if (objc_class_type) {
8837 clang::ObjCInterfaceDecl *class_interface_decl =
8838 objc_class_type->getInterface();
8839
8840 if (class_interface_decl) {
8841 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8842 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8843 return true;
8844 }
8845 }
8846 } break;
8847
8848 case clang::Type::Typedef:
8849 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8850 ->getDecl()
8851 ->getUnderlyingType()
8852 .getAsOpaquePtr(),
8853 has_extern);
8854
8855 case clang::Type::Auto:
8856 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8857 ->getDeducedType()
8858 .getAsOpaquePtr(),
8859 has_extern);
8860
8861 case clang::Type::Elaborated:
8862 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8863 ->getNamedType()
8864 .getAsOpaquePtr(),
8865 has_extern);
8866
8867 case clang::Type::Paren:
8868 return SetHasExternalStorage(
8869 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8870 has_extern);
8871
8872 default:
8873 break;
8874 }
8875 return false;
8876}
Greg Claytond8d4a572015-08-11 21:38:15 +00008877
8878#pragma mark TagDecl
8879
Kate Stoneb9c1b512016-09-06 20:57:50 +00008880bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8881 clang::QualType qual_type(ClangUtil::GetQualType(type));
8882 if (!qual_type.isNull()) {
8883 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8884 if (tag_type) {
8885 clang::TagDecl *tag_decl = tag_type->getDecl();
8886 if (tag_decl) {
8887 tag_decl->startDefinition();
8888 return true;
8889 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008890 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008891
8892 const clang::ObjCObjectType *object_type =
8893 qual_type->getAs<clang::ObjCObjectType>();
8894 if (object_type) {
8895 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8896 if (interface_decl) {
8897 interface_decl->startDefinition();
8898 return true;
8899 }
8900 }
8901 }
8902 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008903}
8904
Kate Stoneb9c1b512016-09-06 20:57:50 +00008905bool ClangASTContext::CompleteTagDeclarationDefinition(
8906 const CompilerType &type) {
8907 clang::QualType qual_type(ClangUtil::GetQualType(type));
8908 if (!qual_type.isNull()) {
8909 // Make sure we use the same methodology as
Adrian Prantl05097242018-04-30 16:49:04 +00008910 // ClangASTContext::StartTagDeclarationDefinition() as to how we start/end
8911 // the definition. Previously we were calling
Kate Stoneb9c1b512016-09-06 20:57:50 +00008912 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8913 if (tag_type) {
8914 clang::TagDecl *tag_decl = tag_type->getDecl();
8915 if (tag_decl) {
8916 clang::CXXRecordDecl *cxx_record_decl =
8917 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8918
8919 if (cxx_record_decl) {
8920 if (!cxx_record_decl->isCompleteDefinition())
8921 cxx_record_decl->completeDefinition();
8922 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8923 cxx_record_decl->setHasExternalLexicalStorage(false);
8924 cxx_record_decl->setHasExternalVisibleStorage(false);
8925 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008926 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008927 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008928 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008929
8930 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8931
8932 if (enutype) {
8933 clang::EnumDecl *enum_decl = enutype->getDecl();
8934
8935 if (enum_decl) {
8936 if (!enum_decl->isCompleteDefinition()) {
8937 ClangASTContext *lldb_ast =
8938 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8939 if (lldb_ast == nullptr)
8940 return false;
8941 clang::ASTContext *ast = lldb_ast->getASTContext();
8942
8943 /// TODO This really needs to be fixed.
8944
8945 QualType integer_type(enum_decl->getIntegerType());
8946 if (!integer_type.isNull()) {
8947 unsigned NumPositiveBits = 1;
8948 unsigned NumNegativeBits = 0;
8949
8950 clang::QualType promotion_qual_type;
8951 // If the enum integer type is less than an integer in bit width,
8952 // then we must promote it to an integer size.
8953 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8954 ast->getTypeSize(ast->IntTy)) {
8955 if (enum_decl->getIntegerType()->isSignedIntegerType())
8956 promotion_qual_type = ast->IntTy;
8957 else
8958 promotion_qual_type = ast->UnsignedIntTy;
8959 } else
8960 promotion_qual_type = enum_decl->getIntegerType();
8961
8962 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8963 promotion_qual_type, NumPositiveBits,
8964 NumNegativeBits);
8965 }
8966 }
8967 return true;
8968 }
8969 }
8970 }
8971 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008972}
8973
Aleksandr Urakov709426b2018-09-10 08:08:43 +00008974clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008975 const CompilerType &enum_type, const Declaration &decl, const char *name,
Zachary Turner1639c6b2018-12-17 16:15:13 +00008976 const llvm::APSInt &value) {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008977
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008978 if (!enum_type || ConstString(name).IsEmpty())
8979 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008980
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008981 lldbassert(enum_type.GetTypeSystem() == static_cast<TypeSystem *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +00008982
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008983 lldb::opaque_compiler_type_t enum_opaque_compiler_type =
8984 enum_type.GetOpaqueQualType();
8985
8986 if (!enum_opaque_compiler_type)
8987 return nullptr;
8988
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008989 clang::QualType enum_qual_type(
8990 GetCanonicalQualType(enum_opaque_compiler_type));
8991
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008992 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8993
8994 if (!clang_type)
8995 return nullptr;
8996
8997 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8998
8999 if (!enutype)
9000 return nullptr;
9001
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009002 clang::EnumConstantDecl *enumerator_decl = clang::EnumConstantDecl::Create(
9003 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
9004 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier
Zachary Turner1639c6b2018-12-17 16:15:13 +00009005 clang::QualType(enutype, 0), nullptr, value);
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009006
9007 if (!enumerator_decl)
9008 return nullptr;
9009
9010 enutype->getDecl()->addDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009011
9012#ifdef LLDB_CONFIGURATION_DEBUG
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009013 VerifyDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009014#endif
9015
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009016 return enumerator_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00009017}
9018
Zachary Turner1639c6b2018-12-17 16:15:13 +00009019clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
9020 const CompilerType &enum_type, const Declaration &decl, const char *name,
9021 int64_t enum_value, uint32_t enum_value_bit_size) {
9022 CompilerType underlying_type =
9023 GetEnumerationIntegerType(enum_type.GetOpaqueQualType());
9024 bool is_signed = false;
9025 underlying_type.IsIntegerType(is_signed);
9026
9027 llvm::APSInt value(enum_value_bit_size, is_signed);
9028 value = enum_value;
9029
9030 return AddEnumerationValueToEnumerationType(enum_type, decl, name, value);
9031}
9032
Greg Claytona1e5dc82015-08-11 22:53:00 +00009033CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00009034ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
9035 clang::QualType enum_qual_type(GetCanonicalQualType(type));
9036 const clang::Type *clang_type = enum_qual_type.getTypePtr();
9037 if (clang_type) {
9038 const clang::EnumType *enutype =
9039 llvm::dyn_cast<clang::EnumType>(clang_type);
9040 if (enutype) {
9041 clang::EnumDecl *enum_decl = enutype->getDecl();
9042 if (enum_decl)
9043 return CompilerType(getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00009044 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009045 }
9046 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00009047}
9048
Kate Stoneb9c1b512016-09-06 20:57:50 +00009049CompilerType
9050ClangASTContext::CreateMemberPointerType(const CompilerType &type,
9051 const CompilerType &pointee_type) {
9052 if (type && pointee_type.IsValid() &&
9053 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
9054 ClangASTContext *ast =
9055 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
9056 if (!ast)
9057 return CompilerType();
9058 return CompilerType(ast->getASTContext(),
9059 ast->getASTContext()->getMemberPointerType(
9060 ClangUtil::GetQualType(pointee_type),
9061 ClangUtil::GetQualType(type).getTypePtr()));
9062 }
9063 return CompilerType();
9064}
Greg Claytond8d4a572015-08-11 21:38:15 +00009065
9066size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00009067ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
9068 const char *s, uint8_t *dst,
9069 size_t dst_size) {
9070 if (type) {
9071 clang::QualType qual_type(GetCanonicalQualType(type));
9072 uint32_t count = 0;
9073 bool is_complex = false;
9074 if (IsFloatingPointType(type, count, is_complex)) {
9075 // TODO: handle complex and vector types
9076 if (count != 1)
9077 return false;
9078
9079 llvm::StringRef s_sref(s);
9080 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type),
9081 s_sref);
9082
9083 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type);
9084 const uint64_t byte_size = bit_size / 8;
9085 if (dst_size >= byte_size) {
9086 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(
9087 llvm::NextPowerOf2(byte_size) * 8);
Zachary Turner97206d52017-05-12 04:51:55 +00009088 lldb_private::Status get_data_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009089 if (scalar.GetAsMemoryData(dst, byte_size,
9090 lldb_private::endian::InlHostByteOrder(),
9091 get_data_error))
9092 return byte_size;
9093 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009094 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009095 }
9096 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00009097}
9098
Greg Claytond8d4a572015-08-11 21:38:15 +00009099// Dumping types
Greg Claytond8d4a572015-08-11 21:38:15 +00009100#define DEPTH_INCREMENT 2
9101
Adrian Prantl0c72a422019-03-07 20:20:02 +00009102#ifndef NDEBUG
9103LLVM_DUMP_METHOD void
9104ClangASTContext::dump(lldb::opaque_compiler_type_t type) const {
9105 if (!type)
9106 return;
9107 clang::QualType qual_type(GetQualType(type));
9108 qual_type.dump();
9109}
9110#endif
9111
Zachary Turner49110232018-11-05 17:40:28 +00009112void ClangASTContext::Dump(Stream &s) {
Zachary Turner115209e2018-11-05 19:25:39 +00009113 Decl *tu = Decl::castFromDeclContext(GetTranslationUnitDecl());
Zachary Turner49110232018-11-05 17:40:28 +00009114 tu->dump(s.AsRawOstream());
9115}
9116
Kate Stoneb9c1b512016-09-06 20:57:50 +00009117void ClangASTContext::DumpValue(
9118 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
Zachary Turner29cb8682017-03-03 20:57:05 +00009119 lldb::Format format, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009120 lldb::offset_t data_byte_offset, size_t data_byte_size,
9121 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
9122 bool show_summary, bool verbose, uint32_t depth) {
9123 if (!type)
9124 return;
9125
9126 clang::QualType qual_type(GetQualType(type));
9127 switch (qual_type->getTypeClass()) {
9128 case clang::Type::Record:
9129 if (GetCompleteType(type)) {
9130 const clang::RecordType *record_type =
9131 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9132 const clang::RecordDecl *record_decl = record_type->getDecl();
9133 assert(record_decl);
9134 uint32_t field_bit_offset = 0;
9135 uint32_t field_byte_offset = 0;
9136 const clang::ASTRecordLayout &record_layout =
9137 getASTContext()->getASTRecordLayout(record_decl);
9138 uint32_t child_idx = 0;
9139
9140 const clang::CXXRecordDecl *cxx_record_decl =
9141 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9142 if (cxx_record_decl) {
9143 // We might have base classes to print out first
9144 clang::CXXRecordDecl::base_class_const_iterator base_class,
9145 base_class_end;
9146 for (base_class = cxx_record_decl->bases_begin(),
9147 base_class_end = cxx_record_decl->bases_end();
9148 base_class != base_class_end; ++base_class) {
9149 const clang::CXXRecordDecl *base_class_decl =
9150 llvm::cast<clang::CXXRecordDecl>(
9151 base_class->getType()->getAs<clang::RecordType>()->getDecl());
9152
9153 // Skip empty base classes
Jonas Devliegherea6682a42018-12-15 00:15:33 +00009154 if (!verbose && !ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00009155 continue;
9156
9157 if (base_class->isVirtual())
9158 field_bit_offset =
9159 record_layout.getVBaseClassOffset(base_class_decl)
9160 .getQuantity() *
9161 8;
9162 else
9163 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
9164 .getQuantity() *
9165 8;
9166 field_byte_offset = field_bit_offset / 8;
9167 assert(field_bit_offset % 8 == 0);
9168 if (child_idx == 0)
9169 s->PutChar('{');
9170 else
9171 s->PutChar(',');
9172
9173 clang::QualType base_class_qual_type = base_class->getType();
9174 std::string base_class_type_name(base_class_qual_type.getAsString());
9175
9176 // Indent and print the base class type name
Zachary Turner827d5d72016-12-16 04:27:00 +00009177 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
9178 base_class_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009179
9180 clang::TypeInfo base_class_type_info =
9181 getASTContext()->getTypeInfo(base_class_qual_type);
9182
9183 // Dump the value of the member
9184 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
9185 base_clang_type.DumpValue(
9186 exe_ctx,
9187 s, // Stream to dump to
9188 base_clang_type
9189 .GetFormat(), // The format with which to display the member
9190 data, // Data buffer containing all bytes for this type
9191 data_byte_offset + field_byte_offset, // Offset into "data" where
9192 // to grab value from
9193 base_class_type_info.Width / 8, // Size of this type in bytes
9194 0, // Bitfield bit size
9195 0, // Bitfield bit offset
9196 show_types, // Boolean indicating if we should show the variable
9197 // types
9198 show_summary, // Boolean indicating if we should show a summary
9199 // for the current type
9200 verbose, // Verbose output?
9201 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9202 // children
9203
9204 ++child_idx;
9205 }
9206 }
9207 uint32_t field_idx = 0;
9208 clang::RecordDecl::field_iterator field, field_end;
9209 for (field = record_decl->field_begin(),
9210 field_end = record_decl->field_end();
9211 field != field_end; ++field, ++field_idx, ++child_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009212 // Print the starting squiggly bracket (if this is the first member) or
9213 // comma (for member 2 and beyond) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009214 if (child_idx == 0)
9215 s->PutChar('{');
9216 else
9217 s->PutChar(',');
9218
9219 // Indent
9220 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
9221
9222 clang::QualType field_type = field->getType();
9223 // Print the member type if requested
Adrian Prantl05097242018-04-30 16:49:04 +00009224 // Figure out the type byte size (field_type_info.first) and alignment
9225 // (field_type_info.second) from the AST context.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009226 clang::TypeInfo field_type_info =
9227 getASTContext()->getTypeInfo(field_type);
9228 assert(field_idx < record_layout.getFieldCount());
9229 // Figure out the field offset within the current struct/union/class
9230 // type
9231 field_bit_offset = record_layout.getFieldOffset(field_idx);
9232 field_byte_offset = field_bit_offset / 8;
9233 uint32_t field_bitfield_bit_size = 0;
9234 uint32_t field_bitfield_bit_offset = 0;
9235 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
9236 field_bitfield_bit_size))
9237 field_bitfield_bit_offset = field_bit_offset % 8;
9238
9239 if (show_types) {
9240 std::string field_type_name(field_type.getAsString());
9241 if (field_bitfield_bit_size > 0)
9242 s->Printf("(%s:%u) ", field_type_name.c_str(),
9243 field_bitfield_bit_size);
9244 else
9245 s->Printf("(%s) ", field_type_name.c_str());
9246 }
9247 // Print the member name and equal sign
9248 s->Printf("%s = ", field->getNameAsString().c_str());
9249
9250 // Dump the value of the member
9251 CompilerType field_clang_type(getASTContext(), field_type);
9252 field_clang_type.DumpValue(
9253 exe_ctx,
9254 s, // Stream to dump to
9255 field_clang_type
9256 .GetFormat(), // The format with which to display the member
9257 data, // Data buffer containing all bytes for this type
9258 data_byte_offset + field_byte_offset, // Offset into "data" where to
9259 // grab value from
9260 field_type_info.Width / 8, // Size of this type in bytes
9261 field_bitfield_bit_size, // Bitfield bit size
9262 field_bitfield_bit_offset, // Bitfield bit offset
9263 show_types, // Boolean indicating if we should show the variable
9264 // types
9265 show_summary, // Boolean indicating if we should show a summary for
9266 // the current type
9267 verbose, // Verbose output?
9268 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9269 // children
9270 }
9271
9272 // Indent the trailing squiggly bracket
9273 if (child_idx > 0)
9274 s->Printf("\n%*s}", depth, "");
9275 }
9276 return;
9277
9278 case clang::Type::Enum:
9279 if (GetCompleteType(type)) {
9280 const clang::EnumType *enutype =
9281 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9282 const clang::EnumDecl *enum_decl = enutype->getDecl();
9283 assert(enum_decl);
9284 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9285 lldb::offset_t offset = data_byte_offset;
9286 const int64_t enum_value = data.GetMaxU64Bitfield(
9287 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
9288 for (enum_pos = enum_decl->enumerator_begin(),
9289 enum_end_pos = enum_decl->enumerator_end();
9290 enum_pos != enum_end_pos; ++enum_pos) {
9291 if (enum_pos->getInitVal() == enum_value) {
9292 s->Printf("%s", enum_pos->getNameAsString().c_str());
9293 return;
9294 }
9295 }
Adrian Prantl05097242018-04-30 16:49:04 +00009296 // If we have gotten here we didn't get find the enumerator in the enum
9297 // decl, so just print the integer.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009298 s->Printf("%" PRIi64, enum_value);
9299 }
9300 return;
9301
9302 case clang::Type::ConstantArray: {
9303 const clang::ConstantArrayType *array =
9304 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9305 bool is_array_of_characters = false;
9306 clang::QualType element_qual_type = array->getElementType();
9307
9308 const clang::Type *canonical_type =
9309 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9310 if (canonical_type)
9311 is_array_of_characters = canonical_type->isCharType();
9312
9313 const uint64_t element_count = array->getSize().getLimitedValue();
9314
9315 clang::TypeInfo field_type_info =
9316 getASTContext()->getTypeInfo(element_qual_type);
9317
9318 uint32_t element_idx = 0;
9319 uint32_t element_offset = 0;
9320 uint64_t element_byte_size = field_type_info.Width / 8;
9321 uint32_t element_stride = element_byte_size;
9322
9323 if (is_array_of_characters) {
9324 s->PutChar('"');
Zachary Turner29cb8682017-03-03 20:57:05 +00009325 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9326 element_byte_size, element_count, UINT32_MAX,
9327 LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009328 s->PutChar('"');
9329 return;
9330 } else {
9331 CompilerType element_clang_type(getASTContext(), element_qual_type);
9332 lldb::Format element_format = element_clang_type.GetFormat();
9333
9334 for (element_idx = 0; element_idx < element_count; ++element_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009335 // Print the starting squiggly bracket (if this is the first member) or
9336 // comman (for member 2 and beyong) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009337 if (element_idx == 0)
9338 s->PutChar('{');
9339 else
9340 s->PutChar(',');
9341
9342 // Indent and print the index
9343 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9344
9345 // Figure out the field offset within the current struct/union/class
9346 // type
9347 element_offset = element_idx * element_stride;
9348
9349 // Dump the value of the member
9350 element_clang_type.DumpValue(
9351 exe_ctx,
9352 s, // Stream to dump to
9353 element_format, // The format with which to display the element
9354 data, // Data buffer containing all bytes for this type
9355 data_byte_offset +
9356 element_offset, // Offset into "data" where to grab value from
9357 element_byte_size, // Size of this type in bytes
9358 0, // Bitfield bit size
9359 0, // Bitfield bit offset
9360 show_types, // Boolean indicating if we should show the variable
9361 // types
9362 show_summary, // Boolean indicating if we should show a summary for
9363 // the current type
9364 verbose, // Verbose output?
9365 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9366 // children
9367 }
9368
9369 // Indent the trailing squiggly bracket
9370 if (element_idx > 0)
9371 s->Printf("\n%*s}", depth, "");
9372 }
9373 }
9374 return;
9375
9376 case clang::Type::Typedef: {
9377 clang::QualType typedef_qual_type =
9378 llvm::cast<clang::TypedefType>(qual_type)
9379 ->getDecl()
9380 ->getUnderlyingType();
9381
9382 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9383 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9384 clang::TypeInfo typedef_type_info =
9385 getASTContext()->getTypeInfo(typedef_qual_type);
9386 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9387
9388 return typedef_clang_type.DumpValue(
9389 exe_ctx,
9390 s, // Stream to dump to
9391 typedef_format, // The format with which to display the element
9392 data, // Data buffer containing all bytes for this type
9393 data_byte_offset, // Offset into "data" where to grab value from
9394 typedef_byte_size, // Size of this type in bytes
9395 bitfield_bit_size, // Bitfield bit size
9396 bitfield_bit_offset, // Bitfield bit offset
9397 show_types, // Boolean indicating if we should show the variable types
9398 show_summary, // Boolean indicating if we should show a summary for the
9399 // current type
9400 verbose, // Verbose output?
9401 depth); // Scope depth for any types that have children
9402 } break;
9403
9404 case clang::Type::Auto: {
9405 clang::QualType elaborated_qual_type =
9406 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9407 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9408 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9409 clang::TypeInfo elaborated_type_info =
9410 getASTContext()->getTypeInfo(elaborated_qual_type);
9411 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9412
9413 return elaborated_clang_type.DumpValue(
9414 exe_ctx,
9415 s, // Stream to dump to
9416 elaborated_format, // The format with which to display the element
9417 data, // Data buffer containing all bytes for this type
9418 data_byte_offset, // Offset into "data" where to grab value from
9419 elaborated_byte_size, // Size of this type in bytes
9420 bitfield_bit_size, // Bitfield bit size
9421 bitfield_bit_offset, // Bitfield bit offset
9422 show_types, // Boolean indicating if we should show the variable types
9423 show_summary, // Boolean indicating if we should show a summary for the
9424 // current type
9425 verbose, // Verbose output?
9426 depth); // Scope depth for any types that have children
9427 } break;
9428
9429 case clang::Type::Elaborated: {
9430 clang::QualType elaborated_qual_type =
9431 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9432 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9433 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9434 clang::TypeInfo elaborated_type_info =
9435 getASTContext()->getTypeInfo(elaborated_qual_type);
9436 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9437
9438 return elaborated_clang_type.DumpValue(
9439 exe_ctx,
9440 s, // Stream to dump to
9441 elaborated_format, // The format with which to display the element
9442 data, // Data buffer containing all bytes for this type
9443 data_byte_offset, // Offset into "data" where to grab value from
9444 elaborated_byte_size, // Size of this type in bytes
9445 bitfield_bit_size, // Bitfield bit size
9446 bitfield_bit_offset, // Bitfield bit offset
9447 show_types, // Boolean indicating if we should show the variable types
9448 show_summary, // Boolean indicating if we should show a summary for the
9449 // current type
9450 verbose, // Verbose output?
9451 depth); // Scope depth for any types that have children
9452 } break;
9453
9454 case clang::Type::Paren: {
9455 clang::QualType desugar_qual_type =
9456 llvm::cast<clang::ParenType>(qual_type)->desugar();
9457 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type);
9458
9459 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9460 clang::TypeInfo desugar_type_info =
9461 getASTContext()->getTypeInfo(desugar_qual_type);
9462 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9463
9464 return desugar_clang_type.DumpValue(
9465 exe_ctx,
9466 s, // Stream to dump to
9467 desugar_format, // The format with which to display the element
9468 data, // Data buffer containing all bytes for this type
9469 data_byte_offset, // Offset into "data" where to grab value from
9470 desugar_byte_size, // Size of this type in bytes
9471 bitfield_bit_size, // Bitfield bit size
9472 bitfield_bit_offset, // Bitfield bit offset
9473 show_types, // Boolean indicating if we should show the variable types
9474 show_summary, // Boolean indicating if we should show a summary for the
9475 // current type
9476 verbose, // Verbose output?
9477 depth); // Scope depth for any types that have children
9478 } break;
9479
9480 default:
9481 // We are down to a scalar type that we just need to display.
Zachary Turner29cb8682017-03-03 20:57:05 +00009482 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9483 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9484 bitfield_bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009485
9486 if (show_summary)
9487 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9488 break;
9489 }
9490}
9491
9492bool ClangASTContext::DumpTypeValue(
9493 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
Zachary Turner29cb8682017-03-03 20:57:05 +00009494 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9495 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009496 ExecutionContextScope *exe_scope) {
9497 if (!type)
9498 return false;
9499 if (IsAggregateType(type)) {
9500 return false;
9501 } else {
Greg Claytond8d4a572015-08-11 21:38:15 +00009502 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009503
9504 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9505 switch (type_class) {
9506 case clang::Type::Typedef: {
9507 clang::QualType typedef_qual_type =
9508 llvm::cast<clang::TypedefType>(qual_type)
9509 ->getDecl()
9510 ->getUnderlyingType();
9511 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9512 if (format == eFormatDefault)
9513 format = typedef_clang_type.GetFormat();
9514 clang::TypeInfo typedef_type_info =
9515 getASTContext()->getTypeInfo(typedef_qual_type);
9516 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9517
9518 return typedef_clang_type.DumpTypeValue(
9519 s,
9520 format, // The format with which to display the element
9521 data, // Data buffer containing all bytes for this type
9522 byte_offset, // Offset into "data" where to grab value from
9523 typedef_byte_size, // Size of this type in bytes
9524 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9525 // treat as a bitfield
9526 bitfield_bit_offset, // Offset in bits of a bitfield value if
9527 // bitfield_bit_size != 0
9528 exe_scope);
9529 } break;
9530
9531 case clang::Type::Enum:
Adrian Prantl05097242018-04-30 16:49:04 +00009532 // If our format is enum or default, show the enumeration value as its
9533 // enumeration string value, else just display it as requested.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009534 if ((format == eFormatEnum || format == eFormatDefault) &&
9535 GetCompleteType(type)) {
9536 const clang::EnumType *enutype =
9537 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9538 const clang::EnumDecl *enum_decl = enutype->getDecl();
9539 assert(enum_decl);
9540 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9541 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9542 lldb::offset_t offset = byte_offset;
9543 if (is_signed) {
9544 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9545 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9546 for (enum_pos = enum_decl->enumerator_begin(),
9547 enum_end_pos = enum_decl->enumerator_end();
9548 enum_pos != enum_end_pos; ++enum_pos) {
9549 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009550 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009551 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009552 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009553 }
9554 // If we have gotten here we didn't get find the enumerator in the
9555 // enum decl, so just print the integer.
9556 s->Printf("%" PRIi64, enum_svalue);
9557 } else {
9558 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9559 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9560 for (enum_pos = enum_decl->enumerator_begin(),
9561 enum_end_pos = enum_decl->enumerator_end();
9562 enum_pos != enum_end_pos; ++enum_pos) {
9563 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009564 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009565 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009566 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009567 }
9568 // If we have gotten here we didn't get find the enumerator in the
9569 // enum decl, so just print the integer.
9570 s->Printf("%" PRIu64, enum_uvalue);
Greg Claytond8d4a572015-08-11 21:38:15 +00009571 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009572 return true;
9573 }
9574 // format was not enum, just fall through and dump the value as
9575 // requested....
9576 LLVM_FALLTHROUGH;
9577
9578 default:
9579 // We are down to a scalar type that we just need to display.
9580 {
9581 uint32_t item_count = 1;
9582 // A few formats, we might need to modify our size and count for
9583 // depending
9584 // on how we are trying to display the value...
9585 switch (format) {
Greg Claytond8d4a572015-08-11 21:38:15 +00009586 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00009587 case eFormatBoolean:
9588 case eFormatBinary:
9589 case eFormatComplex:
9590 case eFormatCString: // NULL terminated C strings
9591 case eFormatDecimal:
9592 case eFormatEnum:
9593 case eFormatHex:
9594 case eFormatHexUppercase:
9595 case eFormatFloat:
9596 case eFormatOctal:
9597 case eFormatOSType:
9598 case eFormatUnsigned:
9599 case eFormatPointer:
9600 case eFormatVectorOfChar:
9601 case eFormatVectorOfSInt8:
9602 case eFormatVectorOfUInt8:
9603 case eFormatVectorOfSInt16:
9604 case eFormatVectorOfUInt16:
9605 case eFormatVectorOfSInt32:
9606 case eFormatVectorOfUInt32:
9607 case eFormatVectorOfSInt64:
9608 case eFormatVectorOfUInt64:
9609 case eFormatVectorOfFloat32:
9610 case eFormatVectorOfFloat64:
9611 case eFormatVectorOfUInt128:
9612 break;
9613
9614 case eFormatChar:
9615 case eFormatCharPrintable:
9616 case eFormatCharArray:
9617 case eFormatBytes:
9618 case eFormatBytesWithASCII:
9619 item_count = byte_size;
9620 byte_size = 1;
9621 break;
9622
9623 case eFormatUnicode16:
9624 item_count = byte_size / 2;
9625 byte_size = 2;
9626 break;
9627
9628 case eFormatUnicode32:
9629 item_count = byte_size / 4;
9630 byte_size = 4;
9631 break;
9632 }
Zachary Turner29cb8682017-03-03 20:57:05 +00009633 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9634 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
9635 bitfield_bit_size, bitfield_bit_offset,
9636 exe_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009637 }
9638 break;
9639 }
9640 }
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00009641 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009642}
9643
9644void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9645 ExecutionContext *exe_ctx, Stream *s,
9646 const lldb_private::DataExtractor &data,
9647 lldb::offset_t data_byte_offset,
9648 size_t data_byte_size) {
9649 uint32_t length = 0;
9650 if (IsCStringType(type, length)) {
9651 if (exe_ctx) {
9652 Process *process = exe_ctx->GetProcessPtr();
9653 if (process) {
9654 lldb::offset_t offset = data_byte_offset;
9655 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9656 std::vector<uint8_t> buf;
9657 if (length > 0)
9658 buf.resize(length);
9659 else
9660 buf.resize(256);
9661
Zachary Turner29cb8682017-03-03 20:57:05 +00009662 DataExtractor cstr_data(&buf.front(), buf.size(),
9663 process->GetByteOrder(), 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009664 buf.back() = '\0';
9665 size_t bytes_read;
9666 size_t total_cstr_len = 0;
Zachary Turner97206d52017-05-12 04:51:55 +00009667 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009668 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9669 buf.size(), error)) > 0) {
9670 const size_t len = strlen((const char *)&buf.front());
9671 if (len == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00009672 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009673 if (total_cstr_len == 0)
9674 s->PutCString(" \"");
Zachary Turner29cb8682017-03-03 20:57:05 +00009675 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9676 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009677 total_cstr_len += len;
9678 if (len < buf.size())
9679 break;
9680 pointer_address += total_cstr_len;
Greg Claytond8d4a572015-08-11 21:38:15 +00009681 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009682 if (total_cstr_len > 0)
9683 s->PutChar('"');
9684 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009685 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009686 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009687}
9688
Kate Stoneb9c1b512016-09-06 20:57:50 +00009689void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9690 StreamFile s(stdout, false);
9691 DumpTypeDescription(type, &s);
9692 ClangASTMetadata *metadata =
9693 ClangASTContext::GetMetadata(getASTContext(), type);
9694 if (metadata) {
9695 metadata->Dump(&s);
9696 }
9697}
Greg Claytond8d4a572015-08-11 21:38:15 +00009698
Kate Stoneb9c1b512016-09-06 20:57:50 +00009699void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9700 Stream *s) {
9701 if (type) {
9702 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00009703
Kate Stoneb9c1b512016-09-06 20:57:50 +00009704 llvm::SmallVector<char, 1024> buf;
9705 llvm::raw_svector_ostream llvm_ostrm(buf);
9706
9707 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9708 switch (type_class) {
9709 case clang::Type::ObjCObject:
9710 case clang::Type::ObjCInterface: {
9711 GetCompleteType(type);
9712
9713 const clang::ObjCObjectType *objc_class_type =
9714 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9715 assert(objc_class_type);
9716 if (objc_class_type) {
9717 clang::ObjCInterfaceDecl *class_interface_decl =
9718 objc_class_type->getInterface();
9719 if (class_interface_decl) {
9720 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9721 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
Greg Claytond8d4a572015-08-11 21:38:15 +00009722 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009723 }
9724 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00009725
Kate Stoneb9c1b512016-09-06 20:57:50 +00009726 case clang::Type::Typedef: {
9727 const clang::TypedefType *typedef_type =
9728 qual_type->getAs<clang::TypedefType>();
9729 if (typedef_type) {
9730 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9731 std::string clang_typedef_name(
9732 typedef_decl->getQualifiedNameAsString());
9733 if (!clang_typedef_name.empty()) {
9734 s->PutCString("typedef ");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009735 s->PutCString(clang_typedef_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00009736 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009737 }
9738 } break;
9739
9740 case clang::Type::Auto:
9741 CompilerType(getASTContext(),
9742 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
9743 .DumpTypeDescription(s);
9744 return;
9745
9746 case clang::Type::Elaborated:
9747 CompilerType(getASTContext(),
9748 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
9749 .DumpTypeDescription(s);
9750 return;
9751
9752 case clang::Type::Paren:
9753 CompilerType(getASTContext(),
9754 llvm::cast<clang::ParenType>(qual_type)->desugar())
9755 .DumpTypeDescription(s);
9756 return;
9757
9758 case clang::Type::Record: {
9759 GetCompleteType(type);
9760
9761 const clang::RecordType *record_type =
9762 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9763 const clang::RecordDecl *record_decl = record_type->getDecl();
9764 const clang::CXXRecordDecl *cxx_record_decl =
9765 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9766
9767 if (cxx_record_decl)
9768 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9769 s->GetIndentLevel());
9770 else
9771 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9772 s->GetIndentLevel());
9773 } break;
9774
9775 default: {
9776 const clang::TagType *tag_type =
9777 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9778 if (tag_type) {
9779 clang::TagDecl *tag_decl = tag_type->getDecl();
9780 if (tag_decl)
9781 tag_decl->print(llvm_ostrm, 0);
9782 } else {
9783 std::string clang_type_name(qual_type.getAsString());
9784 if (!clang_type_name.empty())
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009785 s->PutCString(clang_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009786 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009787 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00009788 }
9789
Kate Stoneb9c1b512016-09-06 20:57:50 +00009790 if (buf.size() > 0) {
9791 s->Write(buf.data(), buf.size());
Greg Clayton8b4edba2015-08-14 20:02:05 +00009792 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009793 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00009794}
9795
Kate Stoneb9c1b512016-09-06 20:57:50 +00009796void ClangASTContext::DumpTypeName(const CompilerType &type) {
9797 if (ClangUtil::IsClangType(type)) {
9798 clang::QualType qual_type(
9799 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9800
9801 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9802 switch (type_class) {
9803 case clang::Type::Record: {
9804 const clang::CXXRecordDecl *cxx_record_decl =
9805 qual_type->getAsCXXRecordDecl();
9806 if (cxx_record_decl)
9807 printf("class %s", cxx_record_decl->getName().str().c_str());
9808 } break;
9809
9810 case clang::Type::Enum: {
9811 clang::EnumDecl *enum_decl =
9812 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9813 if (enum_decl) {
9814 printf("enum %s", enum_decl->getName().str().c_str());
9815 }
9816 } break;
9817
9818 case clang::Type::ObjCObject:
9819 case clang::Type::ObjCInterface: {
9820 const clang::ObjCObjectType *objc_class_type =
9821 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9822 if (objc_class_type) {
9823 clang::ObjCInterfaceDecl *class_interface_decl =
9824 objc_class_type->getInterface();
Adrian Prantl05097242018-04-30 16:49:04 +00009825 // We currently can't complete objective C types through the newly
9826 // added ASTContext because it only supports TagDecl objects right
9827 // now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00009828 if (class_interface_decl)
9829 printf("@class %s", class_interface_decl->getName().str().c_str());
9830 }
9831 } break;
9832
9833 case clang::Type::Typedef:
9834 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9835 ->getDecl()
9836 ->getName()
9837 .str()
9838 .c_str());
9839 break;
9840
9841 case clang::Type::Auto:
9842 printf("auto ");
9843 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9844 llvm::cast<clang::AutoType>(qual_type)
9845 ->getDeducedType()
9846 .getAsOpaquePtr()));
9847
9848 case clang::Type::Elaborated:
9849 printf("elaborated ");
9850 return DumpTypeName(CompilerType(
9851 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9852 ->getNamedType()
9853 .getAsOpaquePtr()));
9854
9855 case clang::Type::Paren:
9856 printf("paren ");
9857 return DumpTypeName(CompilerType(
9858 type.GetTypeSystem(),
9859 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9860
9861 default:
9862 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9863 break;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009864 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009865 }
Greg Clayton6dc8d582015-08-18 22:32:36 +00009866}
9867
Kate Stoneb9c1b512016-09-06 20:57:50 +00009868clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9869 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9870 const char *parent_name, int tag_decl_kind,
9871 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9872 if (template_param_infos.IsValid()) {
9873 std::string template_basename(parent_name);
9874 template_basename.erase(template_basename.find('<'));
9875
9876 return CreateClassTemplateDecl(decl_ctx, access_type,
9877 template_basename.c_str(), tag_decl_kind,
9878 template_param_infos);
9879 }
Konrad Kleine248a1302019-05-23 11:14:47 +00009880 return nullptr;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009881}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009882
Kate Stoneb9c1b512016-09-06 20:57:50 +00009883void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9884 ClangASTContext *ast = (ClangASTContext *)baton;
9885 SymbolFile *sym_file = ast->GetSymbolFile();
9886 if (sym_file) {
9887 CompilerType clang_type = GetTypeForDecl(decl);
9888 if (clang_type)
9889 sym_file->CompleteType(clang_type);
9890 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00009891}
9892
Kate Stoneb9c1b512016-09-06 20:57:50 +00009893void ClangASTContext::CompleteObjCInterfaceDecl(
9894 void *baton, clang::ObjCInterfaceDecl *decl) {
9895 ClangASTContext *ast = (ClangASTContext *)baton;
9896 SymbolFile *sym_file = ast->GetSymbolFile();
9897 if (sym_file) {
9898 CompilerType clang_type = GetTypeForDecl(decl);
9899 if (clang_type)
9900 sym_file->CompleteType(clang_type);
9901 }
Zachary Turner42dff792016-04-15 00:21:26 +00009902}
Greg Clayton261ac3f2015-08-28 01:01:03 +00009903
Kate Stoneb9c1b512016-09-06 20:57:50 +00009904DWARFASTParser *ClangASTContext::GetDWARFParser() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009905 if (!m_dwarf_ast_parser_up)
9906 m_dwarf_ast_parser_up.reset(new DWARFASTParserClang(*this));
9907 return m_dwarf_ast_parser_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00009908}
9909
9910PDBASTParser *ClangASTContext::GetPDBParser() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009911 if (!m_pdb_ast_parser_up)
9912 m_pdb_ast_parser_up.reset(new PDBASTParser(*this));
9913 return m_pdb_ast_parser_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00009914}
9915
9916bool ClangASTContext::LayoutRecordType(
9917 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9918 uint64_t &alignment,
9919 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9920 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9921 &base_offsets,
9922 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9923 &vbase_offsets) {
9924 ClangASTContext *ast = (ClangASTContext *)baton;
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00009925 lldb_private::ClangASTImporter *importer = nullptr;
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009926 if (ast->m_dwarf_ast_parser_up)
9927 importer = &ast->m_dwarf_ast_parser_up->GetClangASTImporter();
9928 if (!importer && ast->m_pdb_ast_parser_up)
9929 importer = &ast->m_pdb_ast_parser_up->GetClangASTImporter();
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00009930 if (!importer)
9931 return false;
9932
9933 return importer->LayoutRecordType(record_decl, bit_size, alignment,
9934 field_offsets, base_offsets, vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009935}
9936
Paul Hermand628cbb2015-09-15 23:44:17 +00009937// CompilerDecl override functions
Paul Hermand628cbb2015-09-15 23:44:17 +00009938
Kate Stoneb9c1b512016-09-06 20:57:50 +00009939ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9940 if (opaque_decl) {
9941 clang::NamedDecl *nd =
9942 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9943 if (nd != nullptr)
9944 return ConstString(nd->getDeclName().getAsString());
9945 }
9946 return ConstString();
Paul Hermand628cbb2015-09-15 23:44:17 +00009947}
9948
Kate Stoneb9c1b512016-09-06 20:57:50 +00009949ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9950 if (opaque_decl) {
9951 clang::NamedDecl *nd =
9952 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9953 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9954 clang::MangleContext *mc = getMangleContext();
9955 if (mc && mc->shouldMangleCXXName(nd)) {
9956 llvm::SmallVector<char, 1024> buf;
9957 llvm::raw_svector_ostream llvm_ostrm(buf);
9958 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9959 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9960 Ctor_Complete, llvm_ostrm);
9961 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9962 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9963 Dtor_Complete, llvm_ostrm);
9964 } else {
9965 mc->mangleName(nd, llvm_ostrm);
Greg Claytonfe689042015-11-10 17:47:04 +00009966 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009967 if (buf.size() > 0)
9968 return ConstString(buf.data(), buf.size());
9969 }
Greg Claytonfe689042015-11-10 17:47:04 +00009970 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009971 }
9972 return ConstString();
Greg Claytonfe689042015-11-10 17:47:04 +00009973}
9974
Kate Stoneb9c1b512016-09-06 20:57:50 +00009975CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9976 if (opaque_decl)
9977 return CompilerDeclContext(this,
9978 ((clang::Decl *)opaque_decl)->getDeclContext());
9979 else
9980 return CompilerDeclContext();
Greg Claytonfe689042015-11-10 17:47:04 +00009981}
9982
Kate Stoneb9c1b512016-09-06 20:57:50 +00009983CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9984 if (clang::FunctionDecl *func_decl =
9985 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9986 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9987 if (clang::ObjCMethodDecl *objc_method =
9988 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9989 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9990 else
Greg Claytonfe689042015-11-10 17:47:04 +00009991 return CompilerType();
9992}
9993
Kate Stoneb9c1b512016-09-06 20:57:50 +00009994size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9995 if (clang::FunctionDecl *func_decl =
9996 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9997 return func_decl->param_size();
9998 if (clang::ObjCMethodDecl *objc_method =
9999 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
10000 return objc_method->param_size();
10001 else
10002 return 0;
10003}
10004
10005CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
10006 size_t idx) {
10007 if (clang::FunctionDecl *func_decl =
10008 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
10009 if (idx < func_decl->param_size()) {
10010 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
10011 if (var_decl)
10012 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
10013 }
10014 } else if (clang::ObjCMethodDecl *objc_method =
10015 llvm::dyn_cast<clang::ObjCMethodDecl>(
10016 (clang::Decl *)opaque_decl)) {
10017 if (idx < objc_method->param_size())
10018 return CompilerType(
10019 this,
10020 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
10021 }
10022 return CompilerType();
10023}
10024
Greg Clayton99558cc42015-08-24 23:46:31 +000010025// CompilerDeclContext functions
Greg Clayton99558cc42015-08-24 23:46:31 +000010026
Kate Stoneb9c1b512016-09-06 20:57:50 +000010027std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
10028 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
10029 std::vector<CompilerDecl> found_decls;
10030 if (opaque_decl_ctx) {
10031 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
10032 std::set<DeclContext *> searched;
10033 std::multimap<DeclContext *, DeclContext *> search_queue;
10034 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +000010035
Kate Stoneb9c1b512016-09-06 20:57:50 +000010036 for (clang::DeclContext *decl_context = root_decl_ctx;
10037 decl_context != nullptr && found_decls.empty();
10038 decl_context = decl_context->getParent()) {
10039 search_queue.insert(std::make_pair(decl_context, decl_context));
Paul Hermand628cbb2015-09-15 23:44:17 +000010040
Kate Stoneb9c1b512016-09-06 20:57:50 +000010041 for (auto it = search_queue.find(decl_context); it != search_queue.end();
10042 it++) {
10043 if (!searched.insert(it->second).second)
10044 continue;
10045 symbol_file->ParseDeclsForContext(
10046 CompilerDeclContext(this, it->second));
Paul Hermanea188fc2015-09-16 18:48:30 +000010047
Kate Stoneb9c1b512016-09-06 20:57:50 +000010048 for (clang::Decl *child : it->second->decls()) {
10049 if (clang::UsingDirectiveDecl *ud =
10050 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10051 if (ignore_using_decls)
10052 continue;
10053 clang::DeclContext *from = ud->getCommonAncestor();
10054 if (searched.find(ud->getNominatedNamespace()) == searched.end())
10055 search_queue.insert(
10056 std::make_pair(from, ud->getNominatedNamespace()));
10057 } else if (clang::UsingDecl *ud =
10058 llvm::dyn_cast<clang::UsingDecl>(child)) {
10059 if (ignore_using_decls)
10060 continue;
10061 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10062 clang::Decl *target = usd->getTargetDecl();
10063 if (clang::NamedDecl *nd =
10064 llvm::dyn_cast<clang::NamedDecl>(target)) {
10065 IdentifierInfo *ii = nd->getIdentifier();
10066 if (ii != nullptr &&
10067 ii->getName().equals(name.AsCString(nullptr)))
10068 found_decls.push_back(CompilerDecl(this, nd));
10069 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010070 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010071 } else if (clang::NamedDecl *nd =
10072 llvm::dyn_cast<clang::NamedDecl>(child)) {
10073 IdentifierInfo *ii = nd->getIdentifier();
10074 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
10075 found_decls.push_back(CompilerDecl(this, nd));
10076 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010077 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010078 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010079 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010080 }
10081 return found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +000010082}
10083
Dawn Perchikb5925782015-12-12 19:31:41 +000010084// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010085// and return the number of levels it took to find it, or
Adrian Prantl05097242018-04-30 16:49:04 +000010086// LLDB_INVALID_DECL_LEVEL if not found. If the decl was imported via a using
10087// declaration, its name and/or type, if set, will be used to check that the
10088// decl found in the scope is a match.
Dawn Perchikb5925782015-12-12 19:31:41 +000010089//
Kate Stoneb9c1b512016-09-06 20:57:50 +000010090// The optional name is required by languages (like C++) to handle using
Adrian Prantl05097242018-04-30 16:49:04 +000010091// declarations like:
Dawn Perchikb5925782015-12-12 19:31:41 +000010092//
10093// void poo();
10094// namespace ns {
10095// void foo();
10096// void goo();
10097// }
10098// void bar() {
10099// using ns::foo;
10100// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
10101// // LLDB_INVALID_DECL_LEVEL for 'goo'.
10102// }
10103//
10104// The optional type is useful in the case that there's a specific overload
10105// that we're looking for that might otherwise be shadowed, like:
10106//
10107// void foo(int);
10108// namespace ns {
10109// void foo();
10110// }
10111// void bar() {
10112// using ns::foo;
10113// // CountDeclLevels returns 0 for { 'foo', void() },
10114// // 1 for { 'foo', void(int) }, and
10115// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
10116// }
10117//
10118// NOTE: Because file statics are at the TranslationUnit along with globals, a
Kate Stoneb9c1b512016-09-06 20:57:50 +000010119// function at file scope will return the same level as a function at global
Adrian Prantl05097242018-04-30 16:49:04 +000010120// scope. Ideally we'd like to treat the file scope as an additional scope just
10121// below the global scope. More work needs to be done to recognise that, if
10122// the decl we're trying to look up is static, we should compare its source
10123// file with that of the current scope and return a lower number for it.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010124uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
10125 clang::DeclContext *child_decl_ctx,
10126 ConstString *child_name,
10127 CompilerType *child_type) {
10128 if (frame_decl_ctx) {
10129 std::set<DeclContext *> searched;
10130 std::multimap<DeclContext *, DeclContext *> search_queue;
10131 SymbolFile *symbol_file = GetSymbolFile();
Dawn Perchikb5925782015-12-12 19:31:41 +000010132
Kate Stoneb9c1b512016-09-06 20:57:50 +000010133 // Get the lookup scope for the decl we're trying to find.
10134 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
Dawn Perchikb5925782015-12-12 19:31:41 +000010135
Kate Stoneb9c1b512016-09-06 20:57:50 +000010136 // Look for it in our scope's decl context and its parents.
10137 uint32_t level = 0;
10138 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
10139 decl_ctx = decl_ctx->getParent()) {
10140 if (!decl_ctx->isLookupContext())
10141 continue;
10142 if (decl_ctx == parent_decl_ctx)
10143 // Found it!
10144 return level;
10145 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
10146 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
10147 it++) {
10148 if (searched.find(it->second) != searched.end())
10149 continue;
10150
10151 // Currently DWARF has one shared translation unit for all Decls at top
Adrian Prantl05097242018-04-30 16:49:04 +000010152 // level, so this would erroneously find using statements anywhere. So
10153 // don't look at the top-level translation unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010154 // TODO fix this and add a testcase that depends on it.
10155
10156 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
10157 continue;
10158
10159 searched.insert(it->second);
10160 symbol_file->ParseDeclsForContext(
10161 CompilerDeclContext(this, it->second));
10162
10163 for (clang::Decl *child : it->second->decls()) {
10164 if (clang::UsingDirectiveDecl *ud =
10165 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10166 clang::DeclContext *ns = ud->getNominatedNamespace();
10167 if (ns == parent_decl_ctx)
10168 // Found it!
10169 return level;
10170 clang::DeclContext *from = ud->getCommonAncestor();
10171 if (searched.find(ns) == searched.end())
10172 search_queue.insert(std::make_pair(from, ns));
10173 } else if (child_name) {
10174 if (clang::UsingDecl *ud =
10175 llvm::dyn_cast<clang::UsingDecl>(child)) {
10176 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10177 clang::Decl *target = usd->getTargetDecl();
10178 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
10179 if (!nd)
10180 continue;
10181 // Check names.
10182 IdentifierInfo *ii = nd->getIdentifier();
10183 if (ii == nullptr ||
10184 !ii->getName().equals(child_name->AsCString(nullptr)))
10185 continue;
10186 // Check types, if one was provided.
10187 if (child_type) {
10188 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
10189 if (!AreTypesSame(clang_type, *child_type,
10190 /*ignore_qualifiers=*/true))
10191 continue;
10192 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010193 // Found it!
10194 return level;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010195 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010196 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010197 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010198 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010199 }
10200 ++level;
Dawn Perchikb5925782015-12-12 19:31:41 +000010201 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010202 }
10203 return LLDB_INVALID_DECL_LEVEL;
Dawn Perchikb5925782015-12-12 19:31:41 +000010204}
10205
Kate Stoneb9c1b512016-09-06 20:57:50 +000010206bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
10207 if (opaque_decl_ctx)
10208 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
10209 else
Greg Clayton99558cc42015-08-24 23:46:31 +000010210 return false;
10211}
10212
Kate Stoneb9c1b512016-09-06 20:57:50 +000010213ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
10214 if (opaque_decl_ctx) {
10215 clang::NamedDecl *named_decl =
10216 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10217 if (named_decl)
10218 return ConstString(named_decl->getName());
10219 }
10220 return ConstString();
Greg Clayton99558cc42015-08-24 23:46:31 +000010221}
10222
Kate Stoneb9c1b512016-09-06 20:57:50 +000010223ConstString
10224ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
10225 if (opaque_decl_ctx) {
10226 clang::NamedDecl *named_decl =
10227 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10228 if (named_decl)
10229 return ConstString(
10230 llvm::StringRef(named_decl->getQualifiedNameAsString()));
10231 }
10232 return ConstString();
10233}
10234
10235bool ClangASTContext::DeclContextIsClassMethod(
10236 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
10237 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
10238 if (opaque_decl_ctx) {
10239 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10240 if (ObjCMethodDecl *objc_method =
10241 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
10242 if (is_instance_method_ptr)
10243 *is_instance_method_ptr = objc_method->isInstanceMethod();
10244 if (language_ptr)
10245 *language_ptr = eLanguageTypeObjC;
10246 if (language_object_name_ptr)
10247 language_object_name_ptr->SetCString("self");
10248 return true;
10249 } else if (CXXMethodDecl *cxx_method =
10250 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
10251 if (is_instance_method_ptr)
10252 *is_instance_method_ptr = cxx_method->isInstance();
10253 if (language_ptr)
10254 *language_ptr = eLanguageTypeC_plus_plus;
10255 if (language_object_name_ptr)
10256 language_object_name_ptr->SetCString("this");
10257 return true;
10258 } else if (clang::FunctionDecl *function_decl =
10259 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
10260 ClangASTMetadata *metadata =
10261 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
10262 if (metadata && metadata->HasObjectPtr()) {
10263 if (is_instance_method_ptr)
10264 *is_instance_method_ptr = true;
10265 if (language_ptr)
10266 *language_ptr = eLanguageTypeObjC;
10267 if (language_object_name_ptr)
10268 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
10269 return true;
10270 }
10271 }
10272 }
10273 return false;
10274}
10275
Raphael Isemanna9469972019-03-12 07:45:04 +000010276bool ClangASTContext::DeclContextIsContainedInLookup(
10277 void *opaque_decl_ctx, void *other_opaque_decl_ctx) {
10278 auto *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10279 auto *other = (clang::DeclContext *)other_opaque_decl_ctx;
10280
10281 do {
10282 // A decl context always includes its own contents in its lookup.
10283 if (decl_ctx == other)
10284 return true;
10285
10286 // If we have an inline namespace, then the lookup of the parent context
10287 // also includes the inline namespace contents.
10288 } while (other->isInlineNamespace() && (other = other->getParent()));
10289
10290 return false;
10291}
10292
Kate Stoneb9c1b512016-09-06 20:57:50 +000010293clang::DeclContext *
10294ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
10295 if (dc.IsClang())
10296 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
10297 return nullptr;
10298}
Greg Clayton99558cc42015-08-24 23:46:31 +000010299
10300ObjCMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010301ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
10302 if (dc.IsClang())
10303 return llvm::dyn_cast<clang::ObjCMethodDecl>(
10304 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10305 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010306}
10307
10308CXXMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010309ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10310 if (dc.IsClang())
10311 return llvm::dyn_cast<clang::CXXMethodDecl>(
10312 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10313 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010314}
10315
10316clang::FunctionDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010317ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10318 if (dc.IsClang())
10319 return llvm::dyn_cast<clang::FunctionDecl>(
10320 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10321 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010322}
10323
10324clang::NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010325ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10326 if (dc.IsClang())
10327 return llvm::dyn_cast<clang::NamespaceDecl>(
10328 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10329 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010330}
10331
10332ClangASTMetadata *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010333ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10334 const void *object) {
10335 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10336 if (ast)
10337 return ClangASTContext::GetMetadata(ast, object);
10338 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010339}
10340
10341clang::ASTContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010342ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10343 ClangASTContext *ast =
10344 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10345 if (ast)
10346 return ast->getASTContext();
10347 return nullptr;
10348}
10349
10350ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10351 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10352 m_target_wp(target.shared_from_this()),
10353 m_persistent_variables(new ClangPersistentVariables) {}
10354
10355UserExpression *ClangASTContextForExpressions::GetUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010356 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010357 Expression::ResultType desired_type,
Aleksandr Urakov40624a02019-02-05 09:14:36 +000010358 const EvaluateExpressionOptions &options,
10359 ValueObject *ctx_obj) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000010360 TargetSP target_sp = m_target_wp.lock();
10361 if (!target_sp)
Greg Clayton99558cc42015-08-24 23:46:31 +000010362 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010363
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010364 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
Aleksandr Urakov40624a02019-02-05 09:14:36 +000010365 desired_type, options, ctx_obj);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010366}
10367
Kate Stoneb9c1b512016-09-06 20:57:50 +000010368FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10369 const CompilerType &return_type, const Address &function_address,
10370 const ValueList &arg_value_list, const char *name) {
10371 TargetSP target_sp = m_target_wp.lock();
10372 if (!target_sp)
10373 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010374
Kate Stoneb9c1b512016-09-06 20:57:50 +000010375 Process *process = target_sp->GetProcessSP().get();
10376 if (!process)
10377 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010378
Kate Stoneb9c1b512016-09-06 20:57:50 +000010379 return new ClangFunctionCaller(*process, return_type, function_address,
10380 arg_value_list, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010381}
10382
10383UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010384ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10385 const char *name) {
10386 TargetSP target_sp = m_target_wp.lock();
10387 if (!target_sp)
10388 return nullptr;
10389
10390 return new ClangUtilityFunction(*target_sp.get(), text, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010391}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010392
10393PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010394ClangASTContextForExpressions::GetPersistentExpressionState() {
10395 return m_persistent_variables.get();
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010396}
Sean Callanan68e44232017-09-28 20:20:25 +000010397
10398clang::ExternalASTMerger &
10399ClangASTContextForExpressions::GetMergerUnchecked() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +000010400 lldbassert(m_scratch_ast_source_up != nullptr);
10401 return m_scratch_ast_source_up->GetMergerUnchecked();
Sean Callanan68e44232017-09-28 20:20:25 +000010402}