blob: 55befb4bbcf3f1f3ab8750d5820a18f1f8250455 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ClangASTContext.cpp -------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Eli Friedman932197d2010-06-13 19:06:42 +00009#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010
Zachary Turner827d5d72016-12-16 04:27:00 +000011#include "llvm/Support/FormatAdapters.h"
12#include "llvm/Support/FormatVariadic.h"
13
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000014#include <mutex>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include <string>
Sean Callananfe38c852015-10-08 23:07:53 +000016#include <vector>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017
Greg Clayton6beaaa62011-01-17 03:46:26 +000018
Kate Stoneb9c1b512016-09-06 20:57:50 +000019// Clang headers like to use NDEBUG inside of them to enable/disable debug
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +000020// related features using "#ifndef NDEBUG" preprocessor blocks to do one thing
Greg Clayton6beaaa62011-01-17 03:46:26 +000021// or another. This is bad because it means that if clang was built in release
22// mode, it assumes that you are building in release mode which is not always
23// the case. You can end up with functions that are defined as empty in header
24// files when NDEBUG is not defined, and this can cause link errors with the
25// clang .a files that you have since you might be missing functions in the .a
26// file. So we have to define NDEBUG when including clang headers to avoid any
27// mismatches. This is covered by rdar://problem/8691220
28
Sean Callanan3b1d4f62011-10-26 17:46:51 +000029#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
Greg Clayton6beaaa62011-01-17 03:46:26 +000030#define LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000031#define NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000032// Need to include assert.h so it is as clang would expect it to be (disabled)
33#include <assert.h>
34#endif
35
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "clang/AST/ASTContext.h"
37#include "clang/AST/ASTImporter.h"
Greg Claytonf74c4032012-12-03 18:29:55 +000038#include "clang/AST/Attr.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "clang/AST/CXXInheritance.h"
Greg Clayton8cf05932010-07-22 18:30:50 +000040#include "clang/AST/DeclObjC.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000041#include "clang/AST/DeclTemplate.h"
Greg Claytonfe689042015-11-10 17:47:04 +000042#include "clang/AST/Mangle.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "clang/AST/RecordLayout.h"
44#include "clang/AST/Type.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000045#include "clang/AST/VTableBuilder.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046#include "clang/Basic/Builtins.h"
Sean Callanan7e2863b2012-02-06 21:28:03 +000047#include "clang/Basic/Diagnostic.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048#include "clang/Basic/FileManager.h"
Sean Callanan79439e82010-11-18 02:56:27 +000049#include "clang/Basic/FileSystemOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050#include "clang/Basic/SourceManager.h"
51#include "clang/Basic/TargetInfo.h"
52#include "clang/Basic/TargetOptions.h"
53#include "clang/Frontend/FrontendOptions.h"
54#include "clang/Frontend/LangStandard.h"
Raphael Isemannf74a4c12019-04-30 08:41:35 +000055#include "clang/Sema/Sema.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000056
57#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000058#undef NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000059#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
60// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
61#include <assert.h>
62#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063
Greg Claytond8d4a572015-08-11 21:38:15 +000064#include "llvm/Support/Signals.h"
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000065#include "llvm/Support/Threading.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000066
Zachary Turnerd133f6a2016-03-28 22:53:41 +000067#include "Plugins/ExpressionParser/Clang/ClangFunctionCaller.h"
68#include "Plugins/ExpressionParser/Clang/ClangUserExpression.h"
69#include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000070#include "lldb/Utility/ArchSpec.h"
Zachary Turner01c32432017-02-14 19:06:07 +000071#include "lldb/Utility/Flags.h"
72
Zachary Turner29cb8682017-03-03 20:57:05 +000073#include "lldb/Core/DumpDataExtractor.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000074#include "lldb/Core/Module.h"
75#include "lldb/Core/PluginManager.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000076#include "lldb/Core/StreamFile.h"
Enrico Granata2267ad42014-09-16 17:28:40 +000077#include "lldb/Core/ThreadSafeDenseMap.h"
Greg Clayton57ee3062013-07-11 22:46:58 +000078#include "lldb/Core/UniqueCStringMap.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000079#include "lldb/Symbol/ClangASTContext.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000080#include "lldb/Symbol/ClangASTImporter.h"
Greg Clayton6dc8d582015-08-18 22:32:36 +000081#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000082#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000083#include "lldb/Symbol/ClangUtil.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000084#include "lldb/Symbol/ObjectFile.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000085#include "lldb/Symbol/SymbolFile.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000086#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000087#include "lldb/Target/ExecutionContext.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000088#include "lldb/Target/Language.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000089#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham151c0322015-09-15 21:13:50 +000090#include "lldb/Target/Process.h"
91#include "lldb/Target/Target.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000092#include "lldb/Utility/DataExtractor.h"
Sean Callananc530ba92016-05-02 21:15:31 +000093#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000094#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000095#include "lldb/Utility/RegularExpression.h"
Pavel Labathd821c992018-08-07 11:07:21 +000096#include "lldb/Utility/Scalar.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000097
Greg Clayton261ac3f2015-08-28 01:01:03 +000098#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
Zachary Turner42dff792016-04-15 00:21:26 +000099#include "Plugins/SymbolFile/PDB/PDBASTParser.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +0000100
Eli Friedman932197d2010-06-13 19:06:42 +0000101#include <stdio.h>
102
Greg Clayton1341baf2013-07-11 23:36:31 +0000103#include <mutex>
104
Greg Claytonc86103d2010-08-05 01:57:25 +0000105using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106using namespace lldb_private;
107using namespace llvm;
108using namespace clang;
109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110namespace {
111static inline bool
112ClangASTContextSupportsLanguage(lldb::LanguageType language) {
113 return language == eLanguageTypeUnknown || // Clang is the default type system
114 Language::LanguageIsC(language) ||
115 Language::LanguageIsCPlusPlus(language) ||
116 Language::LanguageIsObjC(language) ||
117 Language::LanguageIsPascal(language) ||
118 // Use Clang for Rust until there is a proper language plugin for it
119 language == eLanguageTypeRust ||
Johan Engelen04799572016-11-25 11:01:12 +0000120 language == eLanguageTypeExtRenderScript ||
121 // Use Clang for D until there is a proper language plugin for it
Bruce Mitchenerb8233f82018-11-27 05:37:27 +0000122 language == eLanguageTypeD ||
123 // Open Dylan compiler debug info is designed to be Clang-compatible
124 language == eLanguageTypeDylan;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125}
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000126
127// Checks whether m1 is an overload of m2 (as opposed to an override). This is
128// called by addOverridesForMethod to distinguish overrides (which share a
129// vtable entry) from overloads (which require distinct entries).
130bool isOverload(clang::CXXMethodDecl *m1, clang::CXXMethodDecl *m2) {
131 // FIXME: This should detect covariant return types, but currently doesn't.
132 lldbassert(&m1->getASTContext() == &m2->getASTContext() &&
133 "Methods should have the same AST context");
134 clang::ASTContext &context = m1->getASTContext();
135
136 const auto *m1Type = llvm::cast<clang::FunctionProtoType>(
137 context.getCanonicalType(m1->getType()));
138
139 const auto *m2Type = llvm::cast<clang::FunctionProtoType>(
140 context.getCanonicalType(m2->getType()));
141
142 auto compareArgTypes = [&context](const clang::QualType &m1p,
143 const clang::QualType &m2p) {
144 return context.hasSameType(m1p.getUnqualifiedType(),
145 m2p.getUnqualifiedType());
146 };
147
148 // FIXME: In C++14 and later, we can just pass m2Type->param_type_end()
149 // as a fourth parameter to std::equal().
150 return (m1->getNumParams() != m2->getNumParams()) ||
151 !std::equal(m1Type->param_type_begin(), m1Type->param_type_end(),
152 m2Type->param_type_begin(), compareArgTypes);
153}
154
155// If decl is a virtual method, walk the base classes looking for methods that
156// decl overrides. This table of overridden methods is used by IRGen to
157// determine the vtable layout for decl's parent class.
158void addOverridesForMethod(clang::CXXMethodDecl *decl) {
159 if (!decl->isVirtual())
160 return;
161
162 clang::CXXBasePaths paths;
163
164 auto find_overridden_methods =
165 [decl](const clang::CXXBaseSpecifier *specifier,
166 clang::CXXBasePath &path) {
167 if (auto *base_record = llvm::dyn_cast<clang::CXXRecordDecl>(
168 specifier->getType()->getAs<clang::RecordType>()->getDecl())) {
169
170 clang::DeclarationName name = decl->getDeclName();
171
172 // If this is a destructor, check whether the base class destructor is
173 // virtual.
174 if (name.getNameKind() == clang::DeclarationName::CXXDestructorName)
175 if (auto *baseDtorDecl = base_record->getDestructor()) {
176 if (baseDtorDecl->isVirtual()) {
177 path.Decls = baseDtorDecl;
178 return true;
179 } else
180 return false;
181 }
182
183 // Otherwise, search for name in the base class.
184 for (path.Decls = base_record->lookup(name); !path.Decls.empty();
185 path.Decls = path.Decls.slice(1)) {
186 if (auto *method_decl =
187 llvm::dyn_cast<clang::CXXMethodDecl>(path.Decls.front()))
188 if (method_decl->isVirtual() && !isOverload(decl, method_decl)) {
189 path.Decls = method_decl;
190 return true;
191 }
192 }
193 }
194
195 return false;
196 };
197
198 if (decl->getParent()->lookupInBases(find_overridden_methods, paths)) {
199 for (auto *overridden_decl : paths.found_decls())
200 decl->addOverriddenMethod(
201 llvm::cast<clang::CXXMethodDecl>(overridden_decl));
202 }
203}
Greg Clayton56939cb2015-09-17 22:23:34 +0000204}
205
Aleksandr Urakov1dc51db2018-11-12 16:23:50 +0000206static lldb::addr_t GetVTableAddress(Process &process,
207 VTableContextBase &vtable_ctx,
208 ValueObject &valobj,
209 const ASTRecordLayout &record_layout) {
210 // Retrieve type info
211 CompilerType pointee_type;
212 CompilerType this_type(valobj.GetCompilerType());
213 uint32_t type_info = this_type.GetTypeInfo(&pointee_type);
214 if (!type_info)
215 return LLDB_INVALID_ADDRESS;
216
217 // Check if it's a pointer or reference
218 bool ptr_or_ref = false;
219 if (type_info & (eTypeIsPointer | eTypeIsReference)) {
220 ptr_or_ref = true;
221 type_info = pointee_type.GetTypeInfo();
222 }
223
224 // We process only C++ classes
225 const uint32_t cpp_class = eTypeIsClass | eTypeIsCPlusPlus;
226 if ((type_info & cpp_class) != cpp_class)
227 return LLDB_INVALID_ADDRESS;
228
229 // Calculate offset to VTable pointer
230 lldb::offset_t vbtable_ptr_offset =
231 vtable_ctx.isMicrosoft() ? record_layout.getVBPtrOffset().getQuantity()
232 : 0;
233
234 if (ptr_or_ref) {
235 // We have a pointer / ref to object, so read
236 // VTable pointer from process memory
237
238 if (valobj.GetAddressTypeOfChildren() != eAddressTypeLoad)
239 return LLDB_INVALID_ADDRESS;
240
241 auto vbtable_ptr_addr = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
242 if (vbtable_ptr_addr == LLDB_INVALID_ADDRESS)
243 return LLDB_INVALID_ADDRESS;
244
245 vbtable_ptr_addr += vbtable_ptr_offset;
246
247 Status err;
248 return process.ReadPointerFromMemory(vbtable_ptr_addr, err);
249 }
250
251 // We have an object already read from process memory,
252 // so just extract VTable pointer from it
253
254 DataExtractor data;
255 Status err;
256 auto size = valobj.GetData(data, err);
257 if (err.Fail() || vbtable_ptr_offset + data.GetAddressByteSize() > size)
258 return LLDB_INVALID_ADDRESS;
259
260 return data.GetPointer(&vbtable_ptr_offset);
261}
262
263static int64_t ReadVBaseOffsetFromVTable(Process &process,
264 VTableContextBase &vtable_ctx,
265 lldb::addr_t vtable_ptr,
266 const CXXRecordDecl *cxx_record_decl,
267 const CXXRecordDecl *base_class_decl) {
268 if (vtable_ctx.isMicrosoft()) {
269 clang::MicrosoftVTableContext &msoft_vtable_ctx =
270 static_cast<clang::MicrosoftVTableContext &>(vtable_ctx);
271
272 // Get the index into the virtual base table. The
273 // index is the index in uint32_t from vbtable_ptr
274 const unsigned vbtable_index =
275 msoft_vtable_ctx.getVBTableIndex(cxx_record_decl, base_class_decl);
276 const lldb::addr_t base_offset_addr = vtable_ptr + vbtable_index * 4;
277 Status err;
278 return process.ReadSignedIntegerFromMemory(base_offset_addr, 4, INT64_MAX,
279 err);
280 }
281
282 clang::ItaniumVTableContext &itanium_vtable_ctx =
283 static_cast<clang::ItaniumVTableContext &>(vtable_ctx);
284
285 clang::CharUnits base_offset_offset =
286 itanium_vtable_ctx.getVirtualBaseOffsetOffset(cxx_record_decl,
287 base_class_decl);
288 const lldb::addr_t base_offset_addr =
289 vtable_ptr + base_offset_offset.getQuantity();
290 const uint32_t base_offset_size = process.GetAddressByteSize();
291 Status err;
292 return process.ReadSignedIntegerFromMemory(base_offset_addr, base_offset_size,
293 INT64_MAX, err);
294}
295
296static bool GetVBaseBitOffset(VTableContextBase &vtable_ctx,
297 ValueObject &valobj,
298 const ASTRecordLayout &record_layout,
299 const CXXRecordDecl *cxx_record_decl,
300 const CXXRecordDecl *base_class_decl,
301 int32_t &bit_offset) {
302 ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
303 Process *process = exe_ctx.GetProcessPtr();
304 if (!process)
305 return false;
306
307 lldb::addr_t vtable_ptr =
308 GetVTableAddress(*process, vtable_ctx, valobj, record_layout);
309 if (vtable_ptr == LLDB_INVALID_ADDRESS)
310 return false;
311
312 auto base_offset = ReadVBaseOffsetFromVTable(
313 *process, vtable_ctx, vtable_ptr, cxx_record_decl, base_class_decl);
314 if (base_offset == INT64_MAX)
315 return false;
316
317 bit_offset = base_offset * 8;
318
319 return true;
320}
321
Kate Stoneb9c1b512016-09-06 20:57:50 +0000322typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext *>
323 ClangASTMap;
Enrico Granata5d84a692014-08-19 21:46:37 +0000324
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325static ClangASTMap &GetASTMap() {
326 static ClangASTMap *g_map_ptr = nullptr;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000327 static llvm::once_flag g_once_flag;
328 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
330 });
331 return *g_map_ptr;
Enrico Granata5d84a692014-08-19 21:46:37 +0000332}
333
Davide Italiano7e3ef4d2018-03-20 19:46:32 +0000334bool ClangASTContext::IsOperator(const char *name,
335 clang::OverloadedOperatorKind &op_kind) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336 if (name == nullptr || name[0] == '\0')
337 return false;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000338
339#define OPERATOR_PREFIX "operator"
340#define OPERATOR_PREFIX_LENGTH (sizeof(OPERATOR_PREFIX) - 1)
341
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342 const char *post_op_name = nullptr;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000343
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344 bool no_space = true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000345
Kate Stoneb9c1b512016-09-06 20:57:50 +0000346 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
347 return false;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000348
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349 post_op_name = name + OPERATOR_PREFIX_LENGTH;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000350
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351 if (post_op_name[0] == ' ') {
352 post_op_name++;
353 no_space = false;
354 }
Pavel Labath1ac2b202016-08-15 14:32:32 +0000355
356#undef OPERATOR_PREFIX
357#undef OPERATOR_PREFIX_LENGTH
358
Adrian Prantl05097242018-04-30 16:49:04 +0000359 // This is an operator, set the overloaded operator kind to invalid in case
360 // this is a conversion operator...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361 op_kind = clang::NUM_OVERLOADED_OPERATORS;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000362
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363 switch (post_op_name[0]) {
364 default:
365 if (no_space)
366 return false;
367 break;
368 case 'n':
369 if (no_space)
370 return false;
371 if (strcmp(post_op_name, "new") == 0)
372 op_kind = clang::OO_New;
373 else if (strcmp(post_op_name, "new[]") == 0)
374 op_kind = clang::OO_Array_New;
375 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000376
Kate Stoneb9c1b512016-09-06 20:57:50 +0000377 case 'd':
378 if (no_space)
379 return false;
380 if (strcmp(post_op_name, "delete") == 0)
381 op_kind = clang::OO_Delete;
382 else if (strcmp(post_op_name, "delete[]") == 0)
383 op_kind = clang::OO_Array_Delete;
384 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000385
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386 case '+':
387 if (post_op_name[1] == '\0')
388 op_kind = clang::OO_Plus;
389 else if (post_op_name[2] == '\0') {
390 if (post_op_name[1] == '=')
391 op_kind = clang::OO_PlusEqual;
392 else if (post_op_name[1] == '+')
393 op_kind = clang::OO_PlusPlus;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000394 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000396
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397 case '-':
398 if (post_op_name[1] == '\0')
399 op_kind = clang::OO_Minus;
400 else if (post_op_name[2] == '\0') {
401 switch (post_op_name[1]) {
402 case '=':
403 op_kind = clang::OO_MinusEqual;
404 break;
405 case '-':
406 op_kind = clang::OO_MinusMinus;
407 break;
408 case '>':
409 op_kind = clang::OO_Arrow;
410 break;
411 }
412 } else if (post_op_name[3] == '\0') {
413 if (post_op_name[2] == '*')
414 op_kind = clang::OO_ArrowStar;
415 break;
416 }
417 break;
418
419 case '*':
420 if (post_op_name[1] == '\0')
421 op_kind = clang::OO_Star;
422 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
423 op_kind = clang::OO_StarEqual;
424 break;
425
426 case '/':
427 if (post_op_name[1] == '\0')
428 op_kind = clang::OO_Slash;
429 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
430 op_kind = clang::OO_SlashEqual;
431 break;
432
433 case '%':
434 if (post_op_name[1] == '\0')
435 op_kind = clang::OO_Percent;
436 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
437 op_kind = clang::OO_PercentEqual;
438 break;
439
440 case '^':
441 if (post_op_name[1] == '\0')
442 op_kind = clang::OO_Caret;
443 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
444 op_kind = clang::OO_CaretEqual;
445 break;
446
447 case '&':
448 if (post_op_name[1] == '\0')
449 op_kind = clang::OO_Amp;
450 else if (post_op_name[2] == '\0') {
451 switch (post_op_name[1]) {
452 case '=':
453 op_kind = clang::OO_AmpEqual;
454 break;
455 case '&':
456 op_kind = clang::OO_AmpAmp;
457 break;
458 }
459 }
460 break;
461
462 case '|':
463 if (post_op_name[1] == '\0')
464 op_kind = clang::OO_Pipe;
465 else if (post_op_name[2] == '\0') {
466 switch (post_op_name[1]) {
467 case '=':
468 op_kind = clang::OO_PipeEqual;
469 break;
470 case '|':
471 op_kind = clang::OO_PipePipe;
472 break;
473 }
474 }
475 break;
476
477 case '~':
478 if (post_op_name[1] == '\0')
479 op_kind = clang::OO_Tilde;
480 break;
481
482 case '!':
483 if (post_op_name[1] == '\0')
484 op_kind = clang::OO_Exclaim;
485 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
486 op_kind = clang::OO_ExclaimEqual;
487 break;
488
489 case '=':
490 if (post_op_name[1] == '\0')
491 op_kind = clang::OO_Equal;
492 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
493 op_kind = clang::OO_EqualEqual;
494 break;
495
496 case '<':
497 if (post_op_name[1] == '\0')
498 op_kind = clang::OO_Less;
499 else if (post_op_name[2] == '\0') {
500 switch (post_op_name[1]) {
501 case '<':
502 op_kind = clang::OO_LessLess;
503 break;
504 case '=':
505 op_kind = clang::OO_LessEqual;
506 break;
507 }
508 } else if (post_op_name[3] == '\0') {
509 if (post_op_name[2] == '=')
510 op_kind = clang::OO_LessLessEqual;
511 }
512 break;
513
514 case '>':
515 if (post_op_name[1] == '\0')
516 op_kind = clang::OO_Greater;
517 else if (post_op_name[2] == '\0') {
518 switch (post_op_name[1]) {
519 case '>':
520 op_kind = clang::OO_GreaterGreater;
521 break;
522 case '=':
523 op_kind = clang::OO_GreaterEqual;
524 break;
525 }
526 } else if (post_op_name[1] == '>' && post_op_name[2] == '=' &&
527 post_op_name[3] == '\0') {
528 op_kind = clang::OO_GreaterGreaterEqual;
529 }
530 break;
531
532 case ',':
533 if (post_op_name[1] == '\0')
534 op_kind = clang::OO_Comma;
535 break;
536
537 case '(':
538 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
539 op_kind = clang::OO_Call;
540 break;
541
542 case '[':
543 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
544 op_kind = clang::OO_Subscript;
545 break;
546 }
547
548 return true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000549}
Enrico Granata5d84a692014-08-19 21:46:37 +0000550
Greg Clayton57ee3062013-07-11 22:46:58 +0000551clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +0000552ClangASTContext::ConvertAccessTypeToAccessSpecifier(AccessType access) {
553 switch (access) {
554 default:
555 break;
556 case eAccessNone:
Greg Clayton8cf05932010-07-22 18:30:50 +0000557 return AS_none;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000558 case eAccessPublic:
559 return AS_public;
560 case eAccessPrivate:
561 return AS_private;
562 case eAccessProtected:
563 return AS_protected;
564 }
565 return AS_none;
Greg Clayton8cf05932010-07-22 18:30:50 +0000566}
567
Kate Stoneb9c1b512016-09-06 20:57:50 +0000568static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) {
569 // FIXME: Cleanup per-file based stuff.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570
Adrian Prantl05097242018-04-30 16:49:04 +0000571 // Set some properties which depend solely on the input kind; it would be
572 // nice to move these to the language standard, and have the driver resolve
573 // the input kind + language standard.
Richard Smith8186cd42017-04-26 22:10:53 +0000574 if (IK.getLanguage() == InputKind::Asm) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000575 Opts.AsmPreprocessor = 1;
Richard Smith8186cd42017-04-26 22:10:53 +0000576 } else if (IK.isObjectiveC()) {
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000577 Opts.ObjC = 1;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000578 }
579
580 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
581
582 if (LangStd == LangStandard::lang_unspecified) {
583 // Based on the base language, pick one.
Richard Smith8186cd42017-04-26 22:10:53 +0000584 switch (IK.getLanguage()) {
585 case InputKind::Unknown:
586 case InputKind::LLVM_IR:
587 case InputKind::RenderScript:
David Blaikiea322f362017-01-06 00:38:06 +0000588 llvm_unreachable("Invalid input kind!");
Richard Smith8186cd42017-04-26 22:10:53 +0000589 case InputKind::OpenCL:
Pavel Labath47168542017-04-27 08:49:19 +0000590 LangStd = LangStandard::lang_opencl10;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000591 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000592 case InputKind::CUDA:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000593 LangStd = LangStandard::lang_cuda;
594 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000595 case InputKind::Asm:
596 case InputKind::C:
597 case InputKind::ObjC:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000598 LangStd = LangStandard::lang_gnu99;
599 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000600 case InputKind::CXX:
601 case InputKind::ObjCXX:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602 LangStd = LangStandard::lang_gnucxx98;
603 break;
Benjamin Kramer0d97c222018-04-25 13:22:47 +0000604 case InputKind::HIP:
605 LangStd = LangStandard::lang_hip;
606 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000607 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000608 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000609
Kate Stoneb9c1b512016-09-06 20:57:50 +0000610 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
611 Opts.LineComment = Std.hasLineComments();
612 Opts.C99 = Std.isC99();
613 Opts.CPlusPlus = Std.isCPlusPlus();
614 Opts.CPlusPlus11 = Std.isCPlusPlus11();
615 Opts.Digraphs = Std.hasDigraphs();
616 Opts.GNUMode = Std.isGNUMode();
617 Opts.GNUInline = !Std.isC99();
618 Opts.HexFloats = Std.hasHexFloats();
619 Opts.ImplicitInt = Std.hasImplicitInt();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620
Kate Stoneb9c1b512016-09-06 20:57:50 +0000621 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623 // OpenCL has some additional defaults.
Pavel Labath47168542017-04-27 08:49:19 +0000624 if (LangStd == LangStandard::lang_opencl10) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625 Opts.OpenCL = 1;
626 Opts.AltiVec = 1;
627 Opts.CXXOperatorNames = 1;
628 Opts.LaxVectorConversions = 1;
629 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000630
Kate Stoneb9c1b512016-09-06 20:57:50 +0000631 // OpenCL and C++ both have bool, true, false keywords.
632 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000633
Kate Stoneb9c1b512016-09-06 20:57:50 +0000634 Opts.setValueVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635
Adrian Prantl05097242018-04-30 16:49:04 +0000636 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs is
637 // specified, or -std is set to a conforming mode.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000638 Opts.Trigraphs = !Opts.GNUMode;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000639 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000640 Opts.OptimizeSize = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000641
Kate Stoneb9c1b512016-09-06 20:57:50 +0000642 // FIXME: Eliminate this dependency.
643 // unsigned Opt =
644 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
645 // Opts.Optimize = Opt != 0;
646 unsigned Opt = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000647
Kate Stoneb9c1b512016-09-06 20:57:50 +0000648 // This is the __NO_INLINE__ define, which just depends on things like the
649 // optimization level and -fno-inline, not actually whether the backend has
650 // inlining enabled.
651 //
652 // FIXME: This is affected by other options (-fno-inline).
653 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000654}
655
Kate Stoneb9c1b512016-09-06 20:57:50 +0000656ClangASTContext::ClangASTContext(const char *target_triple)
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000657 : TypeSystem(TypeSystem::eKindClang), m_target_triple(), m_ast_up(),
658 m_language_options_up(), m_source_manager_up(), m_diagnostics_engine_up(),
659 m_target_options_rp(), m_target_info_up(), m_identifier_table_up(),
660 m_selector_table_up(), m_builtins_up(), m_callback_tag_decl(nullptr),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000661 m_callback_objc_decl(nullptr), m_callback_baton(nullptr),
662 m_pointer_byte_size(0), m_ast_owned(false) {
663 if (target_triple && target_triple[0])
664 SetTargetTriple(target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000665}
666
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000667// Destructor
Kate Stoneb9c1b512016-09-06 20:57:50 +0000668ClangASTContext::~ClangASTContext() { Finalize(); }
669
670ConstString ClangASTContext::GetPluginNameStatic() {
671 return ConstString("clang");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000672}
673
Kate Stoneb9c1b512016-09-06 20:57:50 +0000674ConstString ClangASTContext::GetPluginName() {
675 return ClangASTContext::GetPluginNameStatic();
Greg Clayton56939cb2015-09-17 22:23:34 +0000676}
677
Kate Stoneb9c1b512016-09-06 20:57:50 +0000678uint32_t ClangASTContext::GetPluginVersion() { return 1; }
Greg Clayton56939cb2015-09-17 22:23:34 +0000679
Kate Stoneb9c1b512016-09-06 20:57:50 +0000680lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
681 lldb_private::Module *module,
682 Target *target) {
683 if (ClangASTContextSupportsLanguage(language)) {
684 ArchSpec arch;
685 if (module)
686 arch = module->GetArchitecture();
687 else if (target)
688 arch = target->GetArchitecture();
Greg Clayton56939cb2015-09-17 22:23:34 +0000689
Kate Stoneb9c1b512016-09-06 20:57:50 +0000690 if (arch.IsValid()) {
691 ArchSpec fixed_arch = arch;
692 // LLVM wants this to be set to iOS or MacOSX; if we're working on
693 // a bare-boards type image, change the triple for llvm's benefit.
694 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
695 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) {
696 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
697 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
698 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) {
699 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
700 } else {
701 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
Greg Clayton56939cb2015-09-17 22:23:34 +0000702 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000703 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000704
Kate Stoneb9c1b512016-09-06 20:57:50 +0000705 if (module) {
706 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
707 if (ast_sp) {
708 ast_sp->SetArchitecture(fixed_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000709 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000710 return ast_sp;
711 } else if (target && target->IsValid()) {
712 std::shared_ptr<ClangASTContextForExpressions> ast_sp(
713 new ClangASTContextForExpressions(*target));
714 if (ast_sp) {
715 ast_sp->SetArchitecture(fixed_arch);
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000716 ast_sp->m_scratch_ast_source_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000717 new ClangASTSource(target->shared_from_this()));
Sean Callanan68e44232017-09-28 20:20:25 +0000718 lldbassert(ast_sp->getFileManager());
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000719 ast_sp->m_scratch_ast_source_up->InstallASTContext(
Sean Callanan68e44232017-09-28 20:20:25 +0000720 *ast_sp->getASTContext(), *ast_sp->getFileManager(), true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000721 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000722 ast_sp->m_scratch_ast_source_up->CreateProxy());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000723 ast_sp->SetExternalSource(proxy_ast_source);
724 return ast_sp;
725 }
726 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000728 }
729 return lldb::TypeSystemSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000730}
731
Kate Stoneb9c1b512016-09-06 20:57:50 +0000732void ClangASTContext::EnumerateSupportedLanguages(
733 std::set<lldb::LanguageType> &languages_for_types,
734 std::set<lldb::LanguageType> &languages_for_expressions) {
735 static std::vector<lldb::LanguageType> s_supported_languages_for_types(
736 {lldb::eLanguageTypeC89, lldb::eLanguageTypeC, lldb::eLanguageTypeC11,
737 lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeC99,
738 lldb::eLanguageTypeObjC, lldb::eLanguageTypeObjC_plus_plus,
739 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
740 lldb::eLanguageTypeC11, lldb::eLanguageTypeC_plus_plus_14});
741
742 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions(
743 {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC_plus_plus,
744 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
745 lldb::eLanguageTypeC_plus_plus_14});
746
747 languages_for_types.insert(s_supported_languages_for_types.begin(),
748 s_supported_languages_for_types.end());
749 languages_for_expressions.insert(
750 s_supported_languages_for_expressions.begin(),
751 s_supported_languages_for_expressions.end());
Enrico Granata5d84a692014-08-19 21:46:37 +0000752}
753
Kate Stoneb9c1b512016-09-06 20:57:50 +0000754void ClangASTContext::Initialize() {
755 PluginManager::RegisterPlugin(GetPluginNameStatic(),
756 "clang base AST context plug-in",
757 CreateInstance, EnumerateSupportedLanguages);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000758}
759
Kate Stoneb9c1b512016-09-06 20:57:50 +0000760void ClangASTContext::Terminate() {
761 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000762}
763
Kate Stoneb9c1b512016-09-06 20:57:50 +0000764void ClangASTContext::Finalize() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000765 if (m_ast_up) {
766 GetASTMap().Erase(m_ast_up.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000767 if (!m_ast_owned)
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000768 m_ast_up.release();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000769 }
770
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000771 m_builtins_up.reset();
772 m_selector_table_up.reset();
773 m_identifier_table_up.reset();
774 m_target_info_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000775 m_target_options_rp.reset();
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000776 m_diagnostics_engine_up.reset();
777 m_source_manager_up.reset();
778 m_language_options_up.reset();
779 m_ast_up.reset();
780 m_scratch_ast_source_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000781}
782
783void ClangASTContext::Clear() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000784 m_ast_up.reset();
785 m_language_options_up.reset();
786 m_source_manager_up.reset();
787 m_diagnostics_engine_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000788 m_target_options_rp.reset();
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000789 m_target_info_up.reset();
790 m_identifier_table_up.reset();
791 m_selector_table_up.reset();
792 m_builtins_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000793 m_pointer_byte_size = 0;
794}
795
Raphael Isemannf74a4c12019-04-30 08:41:35 +0000796void ClangASTContext::setSema(Sema *s) {
797 // Ensure that the new sema actually belongs to our ASTContext.
798 assert(s == nullptr || &s->getASTContext() == m_ast_up.get());
799 m_sema = s;
800}
801
Kate Stoneb9c1b512016-09-06 20:57:50 +0000802const char *ClangASTContext::GetTargetTriple() {
803 return m_target_triple.c_str();
804}
805
806void ClangASTContext::SetTargetTriple(const char *target_triple) {
807 Clear();
808 m_target_triple.assign(target_triple);
809}
810
811void ClangASTContext::SetArchitecture(const ArchSpec &arch) {
812 SetTargetTriple(arch.GetTriple().str().c_str());
813}
814
815bool ClangASTContext::HasExternalSource() {
816 ASTContext *ast = getASTContext();
817 if (ast)
818 return ast->getExternalSource() != nullptr;
819 return false;
820}
821
822void ClangASTContext::SetExternalSource(
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000823 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000824 ASTContext *ast = getASTContext();
825 if (ast) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000826 ast->setExternalSource(ast_source_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000827 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000828 }
829}
830
831void ClangASTContext::RemoveExternalSource() {
832 ASTContext *ast = getASTContext();
833
834 if (ast) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000835 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_up;
836 ast->setExternalSource(empty_ast_source_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000837 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000838 }
839}
840
841void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) {
842 if (!m_ast_owned) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000843 m_ast_up.release();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000844 }
845 m_ast_owned = false;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000846 m_ast_up.reset(ast_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000847 GetASTMap().Insert(ast_ctx, this);
848}
849
850ASTContext *ClangASTContext::getASTContext() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000851 if (m_ast_up == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000852 m_ast_owned = true;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000853 m_ast_up.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000854 *getIdentifierTable(), *getSelectorTable(),
855 *getBuiltinContext()));
856
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000857 m_ast_up->getDiagnostics().setClient(getDiagnosticConsumer(), false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000858
859 // This can be NULL if we don't know anything about the architecture or if
Adrian Prantl05097242018-04-30 16:49:04 +0000860 // the target for an architecture isn't enabled in the llvm/clang that we
861 // built
Kate Stoneb9c1b512016-09-06 20:57:50 +0000862 TargetInfo *target_info = getTargetInfo();
863 if (target_info)
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000864 m_ast_up->InitBuiltinTypes(*target_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000865
866 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000867 m_ast_up->getTranslationUnitDecl()->setHasExternalLexicalStorage();
868 // m_ast_up->getTranslationUnitDecl()->setHasExternalVisibleStorage();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000869 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000870
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000871 GetASTMap().Insert(m_ast_up.get(), this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000872
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000873 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_up(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000874 new ClangExternalASTSourceCallbacks(
875 ClangASTContext::CompleteTagDecl,
876 ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
877 ClangASTContext::LayoutRecordType, this));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000878 SetExternalSource(ast_source_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000879 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000880 return m_ast_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000881}
882
Kate Stoneb9c1b512016-09-06 20:57:50 +0000883ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
884 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
885 return clang_ast;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000886}
887
Kate Stoneb9c1b512016-09-06 20:57:50 +0000888Builtin::Context *ClangASTContext::getBuiltinContext() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000889 if (m_builtins_up == nullptr)
890 m_builtins_up.reset(new Builtin::Context());
891 return m_builtins_up.get();
Sean Callanan79439e82010-11-18 02:56:27 +0000892}
893
Kate Stoneb9c1b512016-09-06 20:57:50 +0000894IdentifierTable *ClangASTContext::getIdentifierTable() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000895 if (m_identifier_table_up == nullptr)
896 m_identifier_table_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000897 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000898 return m_identifier_table_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000899}
900
Kate Stoneb9c1b512016-09-06 20:57:50 +0000901LangOptions *ClangASTContext::getLanguageOptions() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000902 if (m_language_options_up == nullptr) {
903 m_language_options_up.reset(new LangOptions());
904 ParseLangArgs(*m_language_options_up, InputKind::ObjCXX, GetTargetTriple());
905 // InitializeLangOptions(*m_language_options_up, InputKind::ObjCXX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000906 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000907 return m_language_options_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000908}
909
Kate Stoneb9c1b512016-09-06 20:57:50 +0000910SelectorTable *ClangASTContext::getSelectorTable() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000911 if (m_selector_table_up == nullptr)
912 m_selector_table_up.reset(new SelectorTable());
913 return m_selector_table_up.get();
Greg Claytonfe689042015-11-10 17:47:04 +0000914}
915
Kate Stoneb9c1b512016-09-06 20:57:50 +0000916clang::FileManager *ClangASTContext::getFileManager() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000917 if (m_file_manager_up == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000918 clang::FileSystemOptions file_system_options;
Jonas Devlieghere9764b652019-02-18 20:31:18 +0000919 m_file_manager_up.reset(new clang::FileManager(
920 file_system_options, FileSystem::Instance().GetVirtualFileSystem()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000921 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000922 return m_file_manager_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000923}
924
925clang::SourceManager *ClangASTContext::getSourceManager() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000926 if (m_source_manager_up == nullptr)
927 m_source_manager_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000928 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000929 return m_source_manager_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000930}
931
932clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000933 if (m_diagnostics_engine_up == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000934 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000935 m_diagnostics_engine_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000936 new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
937 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000938 return m_diagnostics_engine_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000939}
940
941clang::MangleContext *ClangASTContext::getMangleContext() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000942 if (m_mangle_ctx_up == nullptr)
943 m_mangle_ctx_up.reset(getASTContext()->createMangleContext());
944 return m_mangle_ctx_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000945}
946
947class NullDiagnosticConsumer : public DiagnosticConsumer {
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000948public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000949 NullDiagnosticConsumer() {
950 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
951 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000952
Kate Stoneb9c1b512016-09-06 20:57:50 +0000953 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
Raphael Isemann17566302019-05-03 10:03:28 +0000954 const clang::Diagnostic &info) override {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000955 if (m_log) {
956 llvm::SmallVector<char, 32> diag_str(10);
957 info.FormatDiagnostic(diag_str);
958 diag_str.push_back('\0');
959 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000960 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000961 }
962
963 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
964 return new NullDiagnosticConsumer();
965 }
966
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000967private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000968 Log *m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000969};
970
Kate Stoneb9c1b512016-09-06 20:57:50 +0000971DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000972 if (m_diagnostic_consumer_up == nullptr)
973 m_diagnostic_consumer_up.reset(new NullDiagnosticConsumer);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000974
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000975 return m_diagnostic_consumer_up.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000976}
977
Kate Stoneb9c1b512016-09-06 20:57:50 +0000978std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000979 if (m_target_options_rp == nullptr && !m_target_triple.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000980 m_target_options_rp = std::make_shared<clang::TargetOptions>();
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000981 if (m_target_options_rp != nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000982 m_target_options_rp->Triple = m_target_triple;
983 }
984 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000985}
986
Kate Stoneb9c1b512016-09-06 20:57:50 +0000987TargetInfo *ClangASTContext::getTargetInfo() {
988 // target_triple should be something like "x86_64-apple-macosx"
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000989 if (m_target_info_up == nullptr && !m_target_triple.empty())
990 m_target_info_up.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000991 getTargetOptions()));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000992 return m_target_info_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000993}
994
995#pragma mark Basic Types
996
Kate Stoneb9c1b512016-09-06 20:57:50 +0000997static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
998 ASTContext *ast, QualType qual_type) {
999 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001000 return qual_type_bit_size == bit_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001001}
Greg Clayton56939cb2015-09-17 22:23:34 +00001002
Greg Claytona1e5dc82015-08-11 22:53:00 +00001003CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00001004ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
1005 size_t bit_size) {
1006 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
1007 getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001008}
1009
Kate Stoneb9c1b512016-09-06 20:57:50 +00001010CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
1011 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
1012 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001013 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001014 switch (encoding) {
1015 case eEncodingInvalid:
1016 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1017 return CompilerType(ast, ast->VoidPtrTy);
1018 break;
1019
1020 case eEncodingUint:
1021 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1022 return CompilerType(ast, ast->UnsignedCharTy);
1023 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1024 return CompilerType(ast, ast->UnsignedShortTy);
1025 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1026 return CompilerType(ast, ast->UnsignedIntTy);
1027 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1028 return CompilerType(ast, ast->UnsignedLongTy);
1029 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1030 return CompilerType(ast, ast->UnsignedLongLongTy);
1031 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1032 return CompilerType(ast, ast->UnsignedInt128Ty);
1033 break;
1034
1035 case eEncodingSint:
1036 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1037 return CompilerType(ast, ast->SignedCharTy);
1038 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1039 return CompilerType(ast, ast->ShortTy);
1040 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1041 return CompilerType(ast, ast->IntTy);
1042 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1043 return CompilerType(ast, ast->LongTy);
1044 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1045 return CompilerType(ast, ast->LongLongTy);
1046 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1047 return CompilerType(ast, ast->Int128Ty);
1048 break;
1049
1050 case eEncodingIEEE754:
1051 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1052 return CompilerType(ast, ast->FloatTy);
1053 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1054 return CompilerType(ast, ast->DoubleTy);
1055 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1056 return CompilerType(ast, ast->LongDoubleTy);
1057 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1058 return CompilerType(ast, ast->HalfTy);
1059 break;
1060
1061 case eEncodingVector:
1062 // Sanity check that bit_size is a multiple of 8's.
1063 if (bit_size && !(bit_size & 0x7u))
1064 return CompilerType(
1065 ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8));
1066 break;
1067 }
1068
1069 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001070}
1071
Greg Clayton57ee3062013-07-11 22:46:58 +00001072lldb::BasicType
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001073ClangASTContext::GetBasicTypeEnumeration(ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001074 if (name) {
1075 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
1076 static TypeNameToBasicTypeMap g_type_map;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +00001077 static llvm::once_flag g_once_flag;
1078 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001079 // "void"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001080 g_type_map.Append(ConstString("void"), eBasicTypeVoid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001081
1082 // "char"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001083 g_type_map.Append(ConstString("char"), eBasicTypeChar);
1084 g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar);
1085 g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar);
1086 g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar);
1087 g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar);
1088 g_type_map.Append(ConstString("unsigned wchar_t"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001089 eBasicTypeUnsignedWChar);
1090 // "short"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001091 g_type_map.Append(ConstString("short"), eBasicTypeShort);
1092 g_type_map.Append(ConstString("short int"), eBasicTypeShort);
1093 g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort);
1094 g_type_map.Append(ConstString("unsigned short int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001095 eBasicTypeUnsignedShort);
1096
1097 // "int"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001098 g_type_map.Append(ConstString("int"), eBasicTypeInt);
1099 g_type_map.Append(ConstString("signed int"), eBasicTypeInt);
1100 g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt);
1101 g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102
1103 // "long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001104 g_type_map.Append(ConstString("long"), eBasicTypeLong);
1105 g_type_map.Append(ConstString("long int"), eBasicTypeLong);
1106 g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong);
1107 g_type_map.Append(ConstString("unsigned long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001108 eBasicTypeUnsignedLong);
1109
1110 // "long long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001111 g_type_map.Append(ConstString("long long"), eBasicTypeLongLong);
1112 g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong);
1113 g_type_map.Append(ConstString("unsigned long long"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001114 eBasicTypeUnsignedLongLong);
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001115 g_type_map.Append(ConstString("unsigned long long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001116 eBasicTypeUnsignedLongLong);
1117
1118 // "int128"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001119 g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128);
1120 g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001121
1122 // Miscellaneous
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001123 g_type_map.Append(ConstString("bool"), eBasicTypeBool);
1124 g_type_map.Append(ConstString("float"), eBasicTypeFloat);
1125 g_type_map.Append(ConstString("double"), eBasicTypeDouble);
1126 g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble);
1127 g_type_map.Append(ConstString("id"), eBasicTypeObjCID);
1128 g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel);
1129 g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001130 g_type_map.Sort();
1131 });
1132
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001133 return g_type_map.Find(name, eBasicTypeInvalid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001134 }
1135 return eBasicTypeInvalid;
Greg Clayton57ee3062013-07-11 22:46:58 +00001136}
1137
Kate Stoneb9c1b512016-09-06 20:57:50 +00001138CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001139 ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001140 if (ast) {
1141 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
1142 return ClangASTContext::GetBasicType(ast, basic_type);
1143 }
1144 return CompilerType();
1145}
1146
1147uint32_t ClangASTContext::GetPointerByteSize() {
1148 if (m_pointer_byte_size == 0)
Adrian Prantld963a7c2019-01-15 18:07:52 +00001149 if (auto size = GetBasicType(lldb::eBasicTypeVoid)
1150 .GetPointerType()
1151 .GetByteSize(nullptr))
1152 m_pointer_byte_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001153 return m_pointer_byte_size;
1154}
1155
1156CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
1157 return GetBasicType(getASTContext(), basic_type);
1158}
1159
1160CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1161 lldb::BasicType basic_type) {
1162 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001163 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001164 lldb::opaque_compiler_type_t clang_type =
1165 GetOpaqueCompilerType(ast, basic_type);
1166
1167 if (clang_type)
1168 return CompilerType(GetASTContext(ast), clang_type);
1169 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +00001170}
1171
Kate Stoneb9c1b512016-09-06 20:57:50 +00001172CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
1173 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
1174 ASTContext *ast = getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001175
Kate Stoneb9c1b512016-09-06 20:57:50 +00001176#define streq(a, b) strcmp(a, b) == 0
1177 assert(ast != nullptr);
1178 if (ast) {
1179 switch (dw_ate) {
1180 default:
1181 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001182
Kate Stoneb9c1b512016-09-06 20:57:50 +00001183 case DW_ATE_address:
1184 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1185 return CompilerType(ast, ast->VoidPtrTy);
1186 break;
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001187
Kate Stoneb9c1b512016-09-06 20:57:50 +00001188 case DW_ATE_boolean:
1189 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
1190 return CompilerType(ast, ast->BoolTy);
1191 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1192 return CompilerType(ast, ast->UnsignedCharTy);
1193 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1194 return CompilerType(ast, ast->UnsignedShortTy);
1195 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1196 return CompilerType(ast, ast->UnsignedIntTy);
1197 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001198
Kate Stoneb9c1b512016-09-06 20:57:50 +00001199 case DW_ATE_lo_user:
1200 // This has been seen to mean DW_AT_complex_integer
1201 if (type_name) {
1202 if (::strstr(type_name, "complex")) {
1203 CompilerType complex_int_clang_type =
1204 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1205 bit_size / 2);
1206 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1207 complex_int_clang_type)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001208 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001209 }
1210 break;
1211
1212 case DW_ATE_complex_float:
1213 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
1214 return CompilerType(ast, ast->FloatComplexTy);
1215 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
1216 return CompilerType(ast, ast->DoubleComplexTy);
1217 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
1218 return CompilerType(ast, ast->LongDoubleComplexTy);
1219 else {
1220 CompilerType complex_float_clang_type =
1221 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1222 bit_size / 2);
1223 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1224 complex_float_clang_type)));
1225 }
1226 break;
1227
1228 case DW_ATE_float:
1229 if (streq(type_name, "float") &&
1230 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1231 return CompilerType(ast, ast->FloatTy);
1232 if (streq(type_name, "double") &&
1233 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1234 return CompilerType(ast, ast->DoubleTy);
1235 if (streq(type_name, "long double") &&
1236 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1237 return CompilerType(ast, ast->LongDoubleTy);
1238 // Fall back to not requiring a name match
1239 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1240 return CompilerType(ast, ast->FloatTy);
1241 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1242 return CompilerType(ast, ast->DoubleTy);
1243 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1244 return CompilerType(ast, ast->LongDoubleTy);
1245 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1246 return CompilerType(ast, ast->HalfTy);
1247 break;
1248
1249 case DW_ATE_signed:
1250 if (type_name) {
1251 if (streq(type_name, "wchar_t") &&
1252 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1253 (getTargetInfo() &&
1254 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1255 return CompilerType(ast, ast->WCharTy);
1256 if (streq(type_name, "void") &&
1257 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
1258 return CompilerType(ast, ast->VoidTy);
1259 if (strstr(type_name, "long long") &&
1260 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1261 return CompilerType(ast, ast->LongLongTy);
1262 if (strstr(type_name, "long") &&
1263 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1264 return CompilerType(ast, ast->LongTy);
1265 if (strstr(type_name, "short") &&
1266 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1267 return CompilerType(ast, ast->ShortTy);
1268 if (strstr(type_name, "char")) {
1269 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1270 return CompilerType(ast, ast->CharTy);
1271 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1272 return CompilerType(ast, ast->SignedCharTy);
1273 }
1274 if (strstr(type_name, "int")) {
1275 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1276 return CompilerType(ast, ast->IntTy);
1277 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1278 return CompilerType(ast, ast->Int128Ty);
1279 }
1280 }
1281 // We weren't able to match up a type name, just search by size
1282 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1283 return CompilerType(ast, ast->CharTy);
1284 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1285 return CompilerType(ast, ast->ShortTy);
1286 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1287 return CompilerType(ast, ast->IntTy);
1288 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1289 return CompilerType(ast, ast->LongTy);
1290 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1291 return CompilerType(ast, ast->LongLongTy);
1292 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1293 return CompilerType(ast, ast->Int128Ty);
1294 break;
1295
1296 case DW_ATE_signed_char:
1297 if (ast->getLangOpts().CharIsSigned && type_name &&
1298 streq(type_name, "char")) {
1299 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1300 return CompilerType(ast, ast->CharTy);
1301 }
1302 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1303 return CompilerType(ast, ast->SignedCharTy);
1304 break;
1305
1306 case DW_ATE_unsigned:
1307 if (type_name) {
1308 if (streq(type_name, "wchar_t")) {
1309 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1310 if (!(getTargetInfo() &&
1311 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1312 return CompilerType(ast, ast->WCharTy);
1313 }
1314 }
1315 if (strstr(type_name, "long long")) {
1316 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1317 return CompilerType(ast, ast->UnsignedLongLongTy);
1318 } else if (strstr(type_name, "long")) {
1319 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1320 return CompilerType(ast, ast->UnsignedLongTy);
1321 } else if (strstr(type_name, "short")) {
1322 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1323 return CompilerType(ast, ast->UnsignedShortTy);
1324 } else if (strstr(type_name, "char")) {
1325 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1326 return CompilerType(ast, ast->UnsignedCharTy);
1327 } else if (strstr(type_name, "int")) {
1328 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1329 return CompilerType(ast, ast->UnsignedIntTy);
1330 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1331 return CompilerType(ast, ast->UnsignedInt128Ty);
1332 }
1333 }
1334 // We weren't able to match up a type name, just search by size
1335 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1336 return CompilerType(ast, ast->UnsignedCharTy);
1337 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1338 return CompilerType(ast, ast->UnsignedShortTy);
1339 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1340 return CompilerType(ast, ast->UnsignedIntTy);
1341 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1342 return CompilerType(ast, ast->UnsignedLongTy);
1343 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1344 return CompilerType(ast, ast->UnsignedLongLongTy);
1345 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1346 return CompilerType(ast, ast->UnsignedInt128Ty);
1347 break;
1348
1349 case DW_ATE_unsigned_char:
1350 if (!ast->getLangOpts().CharIsSigned && type_name &&
1351 streq(type_name, "char")) {
1352 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1353 return CompilerType(ast, ast->CharTy);
1354 }
1355 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1356 return CompilerType(ast, ast->UnsignedCharTy);
1357 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1358 return CompilerType(ast, ast->UnsignedShortTy);
1359 break;
1360
1361 case DW_ATE_imaginary_float:
1362 break;
1363
1364 case DW_ATE_UTF:
1365 if (type_name) {
1366 if (streq(type_name, "char16_t")) {
1367 return CompilerType(ast, ast->Char16Ty);
1368 } else if (streq(type_name, "char32_t")) {
1369 return CompilerType(ast, ast->Char32Ty);
1370 }
1371 }
1372 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001373 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001374 }
1375 // This assert should fire for anything that we don't catch above so we know
1376 // to fix any issues we run into.
1377 if (type_name) {
1378 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1379 "DW_TAG_base_type '%s' encoded with "
1380 "DW_ATE = 0x%x, bit_size = %u\n",
1381 type_name, dw_ate, bit_size);
1382 } else {
1383 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1384 "DW_TAG_base_type encoded with "
1385 "DW_ATE = 0x%x, bit_size = %u\n",
1386 dw_ate, bit_size);
1387 }
1388 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001389}
1390
Kate Stoneb9c1b512016-09-06 20:57:50 +00001391CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) {
1392 if (ast)
1393 return CompilerType(ast, ast->UnknownAnyTy);
1394 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001395}
1396
Kate Stoneb9c1b512016-09-06 20:57:50 +00001397CompilerType ClangASTContext::GetCStringType(bool is_const) {
1398 ASTContext *ast = getASTContext();
1399 QualType char_type(ast->CharTy);
1400
1401 if (is_const)
1402 char_type.addConst();
1403
1404 return CompilerType(ast, ast->getPointerType(char_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001405}
1406
Zachary Turner115209e2018-11-05 19:25:39 +00001407clang::DeclContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001408ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1409 return ast->getTranslationUnitDecl();
Sean Callanan09ab4b72011-11-30 22:11:59 +00001410}
1411
Kate Stoneb9c1b512016-09-06 20:57:50 +00001412clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1413 clang::Decl *source_decl) {
1414 FileSystemOptions file_system_options;
1415 FileManager file_manager(file_system_options);
1416 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1417
Gabor Marton5ac6d492019-05-15 10:29:48 +00001418 if (llvm::Expected<clang::Decl *> ret_or_error =
1419 importer.Import(source_decl)) {
1420 return *ret_or_error;
1421 } else {
1422 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
1423 LLDB_LOG_ERROR(log, ret_or_error.takeError(), "Couldn't import decl: {0}");
1424 return nullptr;
1425 }
Greg Clayton526e5af2010-11-13 03:52:47 +00001426}
1427
Kate Stoneb9c1b512016-09-06 20:57:50 +00001428bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1429 bool ignore_qualifiers) {
1430 ClangASTContext *ast =
1431 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1432 if (!ast || ast != type2.GetTypeSystem())
1433 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001434
Kate Stoneb9c1b512016-09-06 20:57:50 +00001435 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1436 return true;
Greg Clayton55995eb2012-04-06 17:38:55 +00001437
Kate Stoneb9c1b512016-09-06 20:57:50 +00001438 QualType type1_qual = ClangUtil::GetQualType(type1);
1439 QualType type2_qual = ClangUtil::GetQualType(type2);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001440
Kate Stoneb9c1b512016-09-06 20:57:50 +00001441 if (ignore_qualifiers) {
1442 type1_qual = type1_qual.getUnqualifiedType();
1443 type2_qual = type2_qual.getUnqualifiedType();
1444 }
1445
1446 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001447}
1448
Kate Stoneb9c1b512016-09-06 20:57:50 +00001449CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1450 if (clang::ObjCInterfaceDecl *interface_decl =
1451 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1452 return GetTypeForDecl(interface_decl);
1453 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1454 return GetTypeForDecl(tag_decl);
1455 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001456}
1457
Kate Stoneb9c1b512016-09-06 20:57:50 +00001458CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001459 // No need to call the getASTContext() accessor (which can create the AST if
1460 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001461 // AST if our AST didn't already exist...
1462 ASTContext *ast = &decl->getASTContext();
1463 if (ast)
1464 return CompilerType(ast, ast->getTagDeclType(decl));
1465 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001466}
1467
Kate Stoneb9c1b512016-09-06 20:57:50 +00001468CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001469 // No need to call the getASTContext() accessor (which can create the AST if
1470 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001471 // AST if our AST didn't already exist...
1472 ASTContext *ast = &decl->getASTContext();
1473 if (ast)
1474 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1475 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001476}
1477
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001478#pragma mark Structure, Unions, Classes
1479
Kate Stoneb9c1b512016-09-06 20:57:50 +00001480CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx,
1481 AccessType access_type,
1482 const char *name, int kind,
1483 LanguageType language,
1484 ClangASTMetadata *metadata) {
1485 ASTContext *ast = getASTContext();
1486 assert(ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001487
Kate Stoneb9c1b512016-09-06 20:57:50 +00001488 if (decl_ctx == nullptr)
1489 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton9e409562010-07-28 02:04:09 +00001490
Kate Stoneb9c1b512016-09-06 20:57:50 +00001491 if (language == eLanguageTypeObjC ||
1492 language == eLanguageTypeObjC_plus_plus) {
1493 bool isForwardDecl = true;
1494 bool isInternal = false;
1495 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1496 }
Greg Clayton9e409562010-07-28 02:04:09 +00001497
Kate Stoneb9c1b512016-09-06 20:57:50 +00001498 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
Adrian Prantl05097242018-04-30 16:49:04 +00001499 // we will need to update this code. I was told to currently always use the
1500 // CXXRecordDecl class since we often don't know from debug information if
1501 // something is struct or a class, so we default to always use the more
Kate Stoneb9c1b512016-09-06 20:57:50 +00001502 // complete definition just in case.
Greg Claytonc4ffd662013-03-08 01:37:30 +00001503
Kate Stoneb9c1b512016-09-06 20:57:50 +00001504 bool is_anonymous = (!name) || (!name[0]);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001505
Kate Stoneb9c1b512016-09-06 20:57:50 +00001506 CXXRecordDecl *decl = CXXRecordDecl::Create(
1507 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1508 SourceLocation(), is_anonymous ? nullptr : &ast->Idents.get(name));
1509
1510 if (is_anonymous)
1511 decl->setAnonymousStructOrUnion(true);
1512
1513 if (decl) {
1514 if (metadata)
1515 SetMetadata(ast, decl, *metadata);
1516
1517 if (access_type != eAccessNone)
1518 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1519
1520 if (decl_ctx)
1521 decl_ctx->addDecl(decl);
1522
1523 return CompilerType(ast, ast->getTagDeclType(decl));
1524 }
1525 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001526}
1527
Sean Callanan09e91ac2017-05-11 22:08:05 +00001528namespace {
1529 bool IsValueParam(const clang::TemplateArgument &argument) {
1530 return argument.getKind() == TemplateArgument::Integral;
1531 }
1532}
1533
Kate Stoneb9c1b512016-09-06 20:57:50 +00001534static TemplateParameterList *CreateTemplateParameterList(
1535 ASTContext *ast,
1536 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1537 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1538 const bool parameter_pack = false;
1539 const bool is_typename = false;
1540 const unsigned depth = 0;
Sean Callanan09e91ac2017-05-11 22:08:05 +00001541 const size_t num_template_params = template_param_infos.args.size();
1542 DeclContext *const decl_context =
1543 ast->getTranslationUnitDecl(); // Is this the right decl context?,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001544 for (size_t i = 0; i < num_template_params; ++i) {
1545 const char *name = template_param_infos.names[i];
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001546
Kate Stoneb9c1b512016-09-06 20:57:50 +00001547 IdentifierInfo *identifier_info = nullptr;
1548 if (name && name[0])
1549 identifier_info = &ast->Idents.get(name);
Sean Callanan09e91ac2017-05-11 22:08:05 +00001550 if (IsValueParam(template_param_infos.args[i])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001551 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001552 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001553 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1554 template_param_infos.args[i].getIntegralType(), parameter_pack,
1555 nullptr));
1556
1557 } else {
1558 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001559 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001560 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1561 is_typename, parameter_pack));
1562 }
1563 }
Eugene Zemtsova9d928c2017-09-29 03:15:08 +00001564
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001565 if (template_param_infos.packed_args) {
Sean Callanan09e91ac2017-05-11 22:08:05 +00001566 IdentifierInfo *identifier_info = nullptr;
1567 if (template_param_infos.pack_name && template_param_infos.pack_name[0])
1568 identifier_info = &ast->Idents.get(template_param_infos.pack_name);
1569 const bool parameter_pack_true = true;
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001570
1571 if (!template_param_infos.packed_args->args.empty() &&
1572 IsValueParam(template_param_infos.packed_args->args[0])) {
Sean Callanan09e91ac2017-05-11 22:08:05 +00001573 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001574 *ast, decl_context, SourceLocation(), SourceLocation(), depth,
1575 num_template_params, identifier_info,
Sean Callanan09e91ac2017-05-11 22:08:05 +00001576 template_param_infos.packed_args->args[0].getIntegralType(),
1577 parameter_pack_true, nullptr));
1578 } else {
1579 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001580 *ast, decl_context, SourceLocation(), SourceLocation(), depth,
1581 num_template_params, identifier_info, is_typename,
1582 parameter_pack_true));
Sean Callanan09e91ac2017-05-11 22:08:05 +00001583 }
1584 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001585 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1586 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1587 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1588 SourceLocation(), requires_clause);
1589 return template_param_list;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001590}
1591
Kate Stoneb9c1b512016-09-06 20:57:50 +00001592clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1593 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1594 const char *name, const TemplateParameterInfos &template_param_infos) {
Adrian Prantld8f460e2018-05-02 16:55:16 +00001595 // /// Create a function template node.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001596 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001597
Kate Stoneb9c1b512016-09-06 20:57:50 +00001598 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001599
Kate Stoneb9c1b512016-09-06 20:57:50 +00001600 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1601 ast, template_param_infos, template_param_decls);
1602 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1603 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1604 template_param_list, func_decl);
1605
1606 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1607 i < template_param_decl_count; ++i) {
1608 // TODO: verify which decl context we should put template_param_decls into..
1609 template_param_decls[i]->setDeclContext(func_decl);
1610 }
1611
1612 return func_tmpl_decl;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001613}
1614
Kate Stoneb9c1b512016-09-06 20:57:50 +00001615void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1616 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1617 const TemplateParameterInfos &infos) {
1618 TemplateArgumentList template_args(TemplateArgumentList::OnStack, infos.args);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001619
Kate Stoneb9c1b512016-09-06 20:57:50 +00001620 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, &template_args,
1621 nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001622}
1623
Kate Stoneb9c1b512016-09-06 20:57:50 +00001624ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1625 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1626 int kind, const TemplateParameterInfos &template_param_infos) {
1627 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001628
Kate Stoneb9c1b512016-09-06 20:57:50 +00001629 ClassTemplateDecl *class_template_decl = nullptr;
1630 if (decl_ctx == nullptr)
1631 decl_ctx = ast->getTranslationUnitDecl();
Greg Claytonf0705c82011-10-22 03:33:13 +00001632
Kate Stoneb9c1b512016-09-06 20:57:50 +00001633 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1634 DeclarationName decl_name(&identifier_info);
Greg Claytonf0705c82011-10-22 03:33:13 +00001635
Kate Stoneb9c1b512016-09-06 20:57:50 +00001636 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Greg Claytonf0705c82011-10-22 03:33:13 +00001637
Kate Stoneb9c1b512016-09-06 20:57:50 +00001638 for (NamedDecl *decl : result) {
1639 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001640 if (class_template_decl)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001641 return class_template_decl;
1642 }
1643
1644 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1645
1646 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1647 ast, template_param_infos, template_param_decls);
1648
1649 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1650 *ast, (TagDecl::TagKind)kind,
1651 decl_ctx, // What decl context do we use here? TU? The actual decl
1652 // context?
1653 SourceLocation(), SourceLocation(), &identifier_info);
1654
1655 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1656 i < template_param_decl_count; ++i) {
1657 template_param_decls[i]->setDeclContext(template_cxx_decl);
1658 }
1659
1660 // With templated classes, we say that a class is templated with
1661 // specializations, but that the bare class has no functions.
1662 // template_cxx_decl->startDefinition();
1663 // template_cxx_decl->completeDefinition();
1664
1665 class_template_decl = ClassTemplateDecl::Create(
1666 *ast,
1667 decl_ctx, // What decl context do we use here? TU? The actual decl
1668 // context?
Pavel Labath4294de32017-01-12 10:44:16 +00001669 SourceLocation(), decl_name, template_param_list, template_cxx_decl);
Richard Smith35b007e2019-02-15 21:48:09 +00001670 template_cxx_decl->setDescribedClassTemplate(class_template_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001671
1672 if (class_template_decl) {
1673 if (access_type != eAccessNone)
1674 class_template_decl->setAccess(
1675 ConvertAccessTypeToAccessSpecifier(access_type));
1676
1677 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1678 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1679
1680 decl_ctx->addDecl(class_template_decl);
1681
Sean Callanan5e9e1992011-10-26 01:06:27 +00001682#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001683 VerifyDecl(class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001684#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001685 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001686
Kate Stoneb9c1b512016-09-06 20:57:50 +00001687 return class_template_decl;
Greg Claytonf0705c82011-10-22 03:33:13 +00001688}
1689
Frederic Rissf4e7e522018-04-02 16:18:32 +00001690TemplateTemplateParmDecl *
1691ClangASTContext::CreateTemplateTemplateParmDecl(const char *template_name) {
1692 ASTContext *ast = getASTContext();
1693
1694 auto *decl_ctx = ast->getTranslationUnitDecl();
1695
1696 IdentifierInfo &identifier_info = ast->Idents.get(template_name);
1697 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1698
1699 ClangASTContext::TemplateParameterInfos template_param_infos;
1700 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1701 ast, template_param_infos, template_param_decls);
1702
1703 // LLDB needs to create those decls only to be able to display a
Adrian Prantl05097242018-04-30 16:49:04 +00001704 // type that includes a template template argument. Only the name matters for
1705 // this purpose, so we use dummy values for the other characterisitcs of the
1706 // type.
Frederic Rissf4e7e522018-04-02 16:18:32 +00001707 return TemplateTemplateParmDecl::Create(
1708 *ast, decl_ctx, SourceLocation(),
1709 /*Depth*/ 0, /*Position*/ 0,
1710 /*IsParameterPack*/ false, &identifier_info, template_param_list);
1711}
1712
Greg Claytonf0705c82011-10-22 03:33:13 +00001713ClassTemplateSpecializationDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001714ClangASTContext::CreateClassTemplateSpecializationDecl(
1715 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1716 const TemplateParameterInfos &template_param_infos) {
1717 ASTContext *ast = getASTContext();
Sean Callanan09e91ac2017-05-11 22:08:05 +00001718 llvm::SmallVector<clang::TemplateArgument, 2> args(
1719 template_param_infos.args.size() +
1720 (template_param_infos.packed_args ? 1 : 0));
1721 std::copy(template_param_infos.args.begin(), template_param_infos.args.end(),
1722 args.begin());
1723 if (template_param_infos.packed_args) {
1724 args[args.size() - 1] = TemplateArgument::CreatePackCopy(
1725 *ast, template_param_infos.packed_args->args);
1726 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001727 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1728 ClassTemplateSpecializationDecl::Create(
1729 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
Sean Callanan09e91ac2017-05-11 22:08:05 +00001730 SourceLocation(), class_template_decl, args,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001731 nullptr);
1732
1733 class_template_specialization_decl->setSpecializationKind(
1734 TSK_ExplicitSpecialization);
1735
1736 return class_template_specialization_decl;
1737}
1738
1739CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1740 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1741 if (class_template_specialization_decl) {
Greg Claytonf0705c82011-10-22 03:33:13 +00001742 ASTContext *ast = getASTContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001743 if (ast)
1744 return CompilerType(
1745 ast, ast->getTagDeclType(class_template_specialization_decl));
1746 }
1747 return CompilerType();
Greg Claytonf0705c82011-10-22 03:33:13 +00001748}
1749
Kate Stoneb9c1b512016-09-06 20:57:50 +00001750static inline bool check_op_param(bool is_method,
1751 clang::OverloadedOperatorKind op_kind,
1752 bool unary, bool binary,
1753 uint32_t num_params) {
1754 // Special-case call since it can take any number of operands
1755 if (op_kind == OO_Call)
1756 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001757
Kate Stoneb9c1b512016-09-06 20:57:50 +00001758 // The parameter count doesn't include "this"
1759 if (is_method)
1760 ++num_params;
1761 if (num_params == 1)
1762 return unary;
1763 if (num_params == 2)
1764 return binary;
1765 else
Greg Clayton090d0982011-06-19 03:43:27 +00001766 return false;
1767}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001768
Kate Stoneb9c1b512016-09-06 20:57:50 +00001769bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1770 bool is_method, clang::OverloadedOperatorKind op_kind,
1771 uint32_t num_params) {
1772 switch (op_kind) {
1773 default:
1774 break;
1775 // C++ standard allows any number of arguments to new/delete
1776 case OO_New:
1777 case OO_Array_New:
1778 case OO_Delete:
1779 case OO_Array_Delete:
1780 return true;
1781 }
Pavel Labath1ac2b202016-08-15 14:32:32 +00001782
Kate Stoneb9c1b512016-09-06 20:57:50 +00001783#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1784 case OO_##Name: \
1785 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1786 switch (op_kind) {
Greg Clayton090d0982011-06-19 03:43:27 +00001787#include "clang/Basic/OperatorKinds.def"
Kate Stoneb9c1b512016-09-06 20:57:50 +00001788 default:
1789 break;
1790 }
1791 return false;
Greg Clayton090d0982011-06-19 03:43:27 +00001792}
1793
Greg Clayton57ee3062013-07-11 22:46:58 +00001794clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +00001795ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1796 clang::AccessSpecifier rhs) {
1797 // Make the access equal to the stricter of the field and the nested field's
1798 // access
1799 if (lhs == AS_none || rhs == AS_none)
1800 return AS_none;
1801 if (lhs == AS_private || rhs == AS_private)
1802 return AS_private;
1803 if (lhs == AS_protected || rhs == AS_protected)
1804 return AS_protected;
1805 return AS_public;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001806}
1807
Kate Stoneb9c1b512016-09-06 20:57:50 +00001808bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1809 uint32_t &bitfield_bit_size) {
1810 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001811}
1812
Kate Stoneb9c1b512016-09-06 20:57:50 +00001813bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field,
1814 uint32_t &bitfield_bit_size) {
1815 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001816 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001817
Kate Stoneb9c1b512016-09-06 20:57:50 +00001818 if (field->isBitField()) {
1819 Expr *bit_width_expr = field->getBitWidth();
1820 if (bit_width_expr) {
1821 llvm::APSInt bit_width_apsint;
1822 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1823 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001824 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001825 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001826 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001827 }
1828 return false;
1829}
1830
1831bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1832 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001833 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001834
1835 if (!record_decl->field_empty())
1836 return true;
1837
1838 // No fields, lets check this is a CXX record and check the base classes
1839 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1840 if (cxx_record_decl) {
1841 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1842 for (base_class = cxx_record_decl->bases_begin(),
1843 base_class_end = cxx_record_decl->bases_end();
1844 base_class != base_class_end; ++base_class) {
1845 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1846 base_class->getType()->getAs<RecordType>()->getDecl());
1847 if (RecordHasFields(base_class_decl))
1848 return true;
1849 }
1850 }
1851 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001852}
1853
Adrian Prantl4e8be2c2018-06-13 16:21:24 +00001854#pragma mark Objective-C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001855
Kate Stoneb9c1b512016-09-06 20:57:50 +00001856CompilerType ClangASTContext::CreateObjCClass(const char *name,
1857 DeclContext *decl_ctx,
1858 bool isForwardDecl,
1859 bool isInternal,
1860 ClangASTMetadata *metadata) {
1861 ASTContext *ast = getASTContext();
1862 assert(ast != nullptr);
1863 assert(name && name[0]);
1864 if (decl_ctx == nullptr)
1865 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001866
Kate Stoneb9c1b512016-09-06 20:57:50 +00001867 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1868 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1869 nullptr, SourceLocation(),
1870 /*isForwardDecl,*/
1871 isInternal);
1872
1873 if (decl && metadata)
1874 SetMetadata(ast, decl, *metadata);
1875
1876 return CompilerType(ast, ast->getObjCInterfaceType(decl));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001877}
1878
Kate Stoneb9c1b512016-09-06 20:57:50 +00001879static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001880 return !ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001881}
1882
Greg Clayton57ee3062013-07-11 22:46:58 +00001883uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001884ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1885 bool omit_empty_base_classes) {
1886 uint32_t num_bases = 0;
1887 if (cxx_record_decl) {
1888 if (omit_empty_base_classes) {
1889 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1890 for (base_class = cxx_record_decl->bases_begin(),
1891 base_class_end = cxx_record_decl->bases_end();
1892 base_class != base_class_end; ++base_class) {
1893 // Skip empty base classes
1894 if (omit_empty_base_classes) {
1895 if (BaseSpecifierIsEmpty(base_class))
1896 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001897 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001898 ++num_bases;
1899 }
1900 } else
1901 num_bases = cxx_record_decl->getNumBases();
1902 }
1903 return num_bases;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001904}
1905
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001906#pragma mark Namespace Declarations
1907
Raphael Isemanna9469972019-03-12 07:45:04 +00001908NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1909 const char *name, DeclContext *decl_ctx, bool is_inline) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001910 NamespaceDecl *namespace_decl = nullptr;
1911 ASTContext *ast = getASTContext();
1912 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1913 if (decl_ctx == nullptr)
1914 decl_ctx = translation_unit_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00001915
Kate Stoneb9c1b512016-09-06 20:57:50 +00001916 if (name) {
1917 IdentifierInfo &identifier_info = ast->Idents.get(name);
1918 DeclarationName decl_name(&identifier_info);
1919 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1920 for (NamedDecl *decl : result) {
1921 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1922 if (namespace_decl)
1923 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001924 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001925
1926 namespace_decl =
Raphael Isemanna9469972019-03-12 07:45:04 +00001927 NamespaceDecl::Create(*ast, decl_ctx, is_inline, SourceLocation(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001928 SourceLocation(), &identifier_info, nullptr);
1929
1930 decl_ctx->addDecl(namespace_decl);
1931 } else {
1932 if (decl_ctx == translation_unit_decl) {
1933 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1934 if (namespace_decl)
1935 return namespace_decl;
1936
1937 namespace_decl =
1938 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1939 SourceLocation(), nullptr, nullptr);
1940 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1941 translation_unit_decl->addDecl(namespace_decl);
1942 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
1943 } else {
1944 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1945 if (parent_namespace_decl) {
1946 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1947 if (namespace_decl)
1948 return namespace_decl;
1949 namespace_decl =
1950 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1951 SourceLocation(), nullptr, nullptr);
1952 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1953 parent_namespace_decl->addDecl(namespace_decl);
1954 assert(namespace_decl ==
1955 parent_namespace_decl->getAnonymousNamespace());
1956 } else {
1957 // BAD!!!
1958 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001959 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001960 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001961#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001962 VerifyDecl(namespace_decl);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001963#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001964 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001965}
1966
Kate Stoneb9c1b512016-09-06 20:57:50 +00001967NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
Raphael Isemanna9469972019-03-12 07:45:04 +00001968 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx,
1969 bool is_inline) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001970 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1971 if (ast_ctx == nullptr)
1972 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +00001973
Raphael Isemanna9469972019-03-12 07:45:04 +00001974 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx, is_inline);
Siva Chandra03ff5c82016-02-05 19:10:04 +00001975}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001976
Paul Hermand628cbb2015-09-15 23:44:17 +00001977clang::BlockDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001978ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1979 if (ctx != nullptr) {
1980 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1981 clang::SourceLocation());
1982 ctx->addDecl(decl);
1983 return decl;
1984 }
1985 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001986}
1987
Kate Stoneb9c1b512016-09-06 20:57:50 +00001988clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1989 clang::DeclContext *right,
1990 clang::DeclContext *root) {
1991 if (root == nullptr)
Paul Hermanea188fc2015-09-16 18:48:30 +00001992 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001993
1994 std::set<clang::DeclContext *> path_left;
1995 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1996 path_left.insert(d);
1997
1998 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1999 if (path_left.find(d) != path_left.end())
2000 return d;
2001
2002 return nullptr;
Paul Hermanea188fc2015-09-16 18:48:30 +00002003}
2004
Kate Stoneb9c1b512016-09-06 20:57:50 +00002005clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
2006 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
2007 if (decl_ctx != nullptr && ns_decl != nullptr) {
2008 clang::TranslationUnitDecl *translation_unit =
2009 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
2010 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
2011 *getASTContext(), decl_ctx, clang::SourceLocation(),
2012 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
2013 clang::SourceLocation(), ns_decl,
2014 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
2015 decl_ctx->addDecl(using_decl);
2016 return using_decl;
2017 }
2018 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002019}
2020
2021clang::UsingDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00002022ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
2023 clang::NamedDecl *target) {
2024 if (current_decl_ctx != nullptr && target != nullptr) {
2025 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
2026 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
2027 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
2028 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
2029 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
2030 target);
2031 using_decl->addShadowDecl(shadow_decl);
2032 current_decl_ctx->addDecl(using_decl);
2033 return using_decl;
2034 }
2035 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002036}
2037
Kate Stoneb9c1b512016-09-06 20:57:50 +00002038clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
2039 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
2040 if (decl_context != nullptr) {
2041 clang::VarDecl *var_decl = clang::VarDecl::Create(
2042 *getASTContext(), decl_context, clang::SourceLocation(),
2043 clang::SourceLocation(),
2044 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
2045 nullptr, clang::SC_None);
2046 var_decl->setAccess(clang::AS_public);
2047 decl_context->addDecl(var_decl);
2048 return var_decl;
2049 }
2050 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002051}
2052
Zachary Turner9d8a97e2016-04-01 23:20:35 +00002053lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00002054ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
2055 lldb::BasicType basic_type) {
2056 switch (basic_type) {
2057 case eBasicTypeVoid:
2058 return ast->VoidTy.getAsOpaquePtr();
2059 case eBasicTypeChar:
2060 return ast->CharTy.getAsOpaquePtr();
2061 case eBasicTypeSignedChar:
2062 return ast->SignedCharTy.getAsOpaquePtr();
2063 case eBasicTypeUnsignedChar:
2064 return ast->UnsignedCharTy.getAsOpaquePtr();
2065 case eBasicTypeWChar:
2066 return ast->getWCharType().getAsOpaquePtr();
2067 case eBasicTypeSignedWChar:
2068 return ast->getSignedWCharType().getAsOpaquePtr();
2069 case eBasicTypeUnsignedWChar:
2070 return ast->getUnsignedWCharType().getAsOpaquePtr();
2071 case eBasicTypeChar16:
2072 return ast->Char16Ty.getAsOpaquePtr();
2073 case eBasicTypeChar32:
2074 return ast->Char32Ty.getAsOpaquePtr();
2075 case eBasicTypeShort:
2076 return ast->ShortTy.getAsOpaquePtr();
2077 case eBasicTypeUnsignedShort:
2078 return ast->UnsignedShortTy.getAsOpaquePtr();
2079 case eBasicTypeInt:
2080 return ast->IntTy.getAsOpaquePtr();
2081 case eBasicTypeUnsignedInt:
2082 return ast->UnsignedIntTy.getAsOpaquePtr();
2083 case eBasicTypeLong:
2084 return ast->LongTy.getAsOpaquePtr();
2085 case eBasicTypeUnsignedLong:
2086 return ast->UnsignedLongTy.getAsOpaquePtr();
2087 case eBasicTypeLongLong:
2088 return ast->LongLongTy.getAsOpaquePtr();
2089 case eBasicTypeUnsignedLongLong:
2090 return ast->UnsignedLongLongTy.getAsOpaquePtr();
2091 case eBasicTypeInt128:
2092 return ast->Int128Ty.getAsOpaquePtr();
2093 case eBasicTypeUnsignedInt128:
2094 return ast->UnsignedInt128Ty.getAsOpaquePtr();
2095 case eBasicTypeBool:
2096 return ast->BoolTy.getAsOpaquePtr();
2097 case eBasicTypeHalf:
2098 return ast->HalfTy.getAsOpaquePtr();
2099 case eBasicTypeFloat:
2100 return ast->FloatTy.getAsOpaquePtr();
2101 case eBasicTypeDouble:
2102 return ast->DoubleTy.getAsOpaquePtr();
2103 case eBasicTypeLongDouble:
2104 return ast->LongDoubleTy.getAsOpaquePtr();
2105 case eBasicTypeFloatComplex:
2106 return ast->FloatComplexTy.getAsOpaquePtr();
2107 case eBasicTypeDoubleComplex:
2108 return ast->DoubleComplexTy.getAsOpaquePtr();
2109 case eBasicTypeLongDoubleComplex:
2110 return ast->LongDoubleComplexTy.getAsOpaquePtr();
2111 case eBasicTypeObjCID:
2112 return ast->getObjCIdType().getAsOpaquePtr();
2113 case eBasicTypeObjCClass:
2114 return ast->getObjCClassType().getAsOpaquePtr();
2115 case eBasicTypeObjCSel:
2116 return ast->getObjCSelType().getAsOpaquePtr();
2117 case eBasicTypeNullPtr:
2118 return ast->NullPtrTy.getAsOpaquePtr();
2119 default:
2120 return nullptr;
2121 }
Zachary Turner9d8a97e2016-04-01 23:20:35 +00002122}
2123
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002124#pragma mark Function Types
2125
Pavel Labath1ac2b202016-08-15 14:32:32 +00002126clang::DeclarationName
Kate Stoneb9c1b512016-09-06 20:57:50 +00002127ClangASTContext::GetDeclarationName(const char *name,
2128 const CompilerType &function_clang_type) {
2129 if (!name || !name[0])
2130 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002131
Kate Stoneb9c1b512016-09-06 20:57:50 +00002132 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
2133 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
2134 return DeclarationName(&getASTContext()->Idents.get(
2135 name)); // Not operator, but a regular function.
Pavel Labath1ac2b202016-08-15 14:32:32 +00002136
Adrian Prantl05097242018-04-30 16:49:04 +00002137 // Check the number of operator parameters. Sometimes we have seen bad DWARF
2138 // that doesn't correctly describe operators and if we try to create a method
2139 // and add it to the class, clang will assert and crash, so we need to make
2140 // sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002141 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
2142 const clang::FunctionProtoType *function_type =
2143 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
2144 if (function_type == nullptr)
2145 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002146
Kate Stoneb9c1b512016-09-06 20:57:50 +00002147 const bool is_method = false;
2148 const unsigned int num_params = function_type->getNumParams();
2149 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
2150 is_method, op_kind, num_params))
2151 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002152
Kate Stoneb9c1b512016-09-06 20:57:50 +00002153 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
Pavel Labath1ac2b202016-08-15 14:32:32 +00002154}
2155
Kate Stoneb9c1b512016-09-06 20:57:50 +00002156FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
2157 DeclContext *decl_ctx, const char *name,
2158 const CompilerType &function_clang_type, int storage, bool is_inline) {
2159 FunctionDecl *func_decl = nullptr;
2160 ASTContext *ast = getASTContext();
2161 if (decl_ctx == nullptr)
2162 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002163
Kate Stoneb9c1b512016-09-06 20:57:50 +00002164 const bool hasWrittenPrototype = true;
2165 const bool isConstexprSpecified = false;
Greg Clayton0d551042013-06-28 21:08:47 +00002166
Kate Stoneb9c1b512016-09-06 20:57:50 +00002167 clang::DeclarationName declarationName =
2168 GetDeclarationName(name, function_clang_type);
2169 func_decl = FunctionDecl::Create(
2170 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
2171 ClangUtil::GetQualType(function_clang_type), nullptr,
2172 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
2173 isConstexprSpecified);
2174 if (func_decl)
2175 decl_ctx->addDecl(func_decl);
2176
Sean Callanan5e9e1992011-10-26 01:06:27 +00002177#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00002178 VerifyDecl(func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002179#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002180
2181 return func_decl;
2182}
2183
2184CompilerType ClangASTContext::CreateFunctionType(
2185 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002186 unsigned num_args, bool is_variadic, unsigned type_quals,
2187 clang::CallingConv cc) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002188 if (ast == nullptr)
2189 return CompilerType(); // invalid AST
2190
2191 if (!result_type || !ClangUtil::IsClangType(result_type))
2192 return CompilerType(); // invalid return type
2193
2194 std::vector<QualType> qual_type_args;
2195 if (num_args > 0 && args == nullptr)
2196 return CompilerType(); // invalid argument array passed in
2197
2198 // Verify that all arguments are valid and the right type
2199 for (unsigned i = 0; i < num_args; ++i) {
2200 if (args[i]) {
2201 // Make sure we have a clang type in args[i] and not a type from another
2202 // language whose name might match
2203 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2204 lldbassert(is_clang_type);
2205 if (is_clang_type)
2206 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2207 else
2208 return CompilerType(); // invalid argument type (must be a clang type)
2209 } else
2210 return CompilerType(); // invalid argument type (empty)
2211 }
2212
2213 // TODO: Detect calling convention in DWARF?
2214 FunctionProtoType::ExtProtoInfo proto_info;
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002215 proto_info.ExtInfo = cc;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002216 proto_info.Variadic = is_variadic;
2217 proto_info.ExceptionSpec = EST_None;
Mikael Nilsson8b3bf6c2018-12-13 10:17:26 +00002218 proto_info.TypeQuals = clang::Qualifiers::fromFastMask(type_quals);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002219 proto_info.RefQualifier = RQ_None;
2220
2221 return CompilerType(ast,
2222 ast->getFunctionType(ClangUtil::GetQualType(result_type),
2223 qual_type_args, proto_info));
2224}
2225
2226ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
Zachary Turner6753d2d2018-12-12 17:17:53 +00002227 clang::DeclContext *decl_ctx, const char *name,
2228 const CompilerType &param_type, int storage) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002229 ASTContext *ast = getASTContext();
2230 assert(ast != nullptr);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00002231 auto *decl =
2232 ParmVarDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(),
2233 name && name[0] ? &ast->Idents.get(name) : nullptr,
2234 ClangUtil::GetQualType(param_type), nullptr,
2235 (clang::StorageClass)storage, nullptr);
2236 decl_ctx->addDecl(decl);
2237 return decl;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002238}
2239
2240void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2241 ParmVarDecl **params,
2242 unsigned num_params) {
2243 if (function_decl)
2244 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002245}
2246
Greg Claytona1e5dc82015-08-11 22:53:00 +00002247CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002248ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00002249 QualType block_type = m_ast_up->getBlockPointerType(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002250 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
Greg Claytonceeb5212016-05-26 22:33:25 +00002251
Kate Stoneb9c1b512016-09-06 20:57:50 +00002252 return CompilerType(this, block_type.getAsOpaquePtr());
Sean Callananc530ba92016-05-02 21:15:31 +00002253}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002254
2255#pragma mark Array Types
2256
Kate Stoneb9c1b512016-09-06 20:57:50 +00002257CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2258 size_t element_count,
2259 bool is_vector) {
2260 if (element_type.IsValid()) {
2261 ASTContext *ast = getASTContext();
2262 assert(ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002263
Kate Stoneb9c1b512016-09-06 20:57:50 +00002264 if (is_vector) {
2265 return CompilerType(
2266 ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2267 element_count));
2268 } else {
2269
2270 llvm::APInt ap_element_count(64, element_count);
2271 if (element_count == 0) {
2272 return CompilerType(ast, ast->getIncompleteArrayType(
2273 ClangUtil::GetQualType(element_type),
2274 clang::ArrayType::Normal, 0));
2275 } else {
2276 return CompilerType(
2277 ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2278 ap_element_count,
2279 clang::ArrayType::Normal, 0));
2280 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002281 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002282 }
2283 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002284}
2285
Kate Stoneb9c1b512016-09-06 20:57:50 +00002286CompilerType ClangASTContext::CreateStructForIdentifier(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002287 ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002288 const std::initializer_list<std::pair<const char *, CompilerType>>
2289 &type_fields,
2290 bool packed) {
2291 CompilerType type;
2292 if (!type_name.IsEmpty() &&
2293 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2294 .IsValid()) {
Pavel Labathf31c9d22017-01-05 13:18:42 +00002295 lldbassert(0 && "Trying to create a type for an existing name");
Enrico Granata76b08d52014-10-29 23:08:02 +00002296 return type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002297 }
2298
2299 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2300 clang::TTK_Struct, lldb::eLanguageTypeC);
2301 StartTagDeclarationDefinition(type);
2302 for (const auto &field : type_fields)
2303 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2304 0);
2305 if (packed)
2306 SetIsPacked(type);
2307 CompleteTagDeclarationDefinition(type);
2308 return type;
Enrico Granata76b08d52014-10-29 23:08:02 +00002309}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002310
Kate Stoneb9c1b512016-09-06 20:57:50 +00002311CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002312 ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002313 const std::initializer_list<std::pair<const char *, CompilerType>>
2314 &type_fields,
2315 bool packed) {
2316 CompilerType type;
2317 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2318 return type;
Sean Callananc530ba92016-05-02 21:15:31 +00002319
Kate Stoneb9c1b512016-09-06 20:57:50 +00002320 return CreateStructForIdentifier(type_name, type_fields, packed);
Sean Callananc530ba92016-05-02 21:15:31 +00002321}
2322
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002323#pragma mark Enumeration Types
2324
Greg Claytona1e5dc82015-08-11 22:53:00 +00002325CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002326ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2327 const Declaration &decl,
Tamas Berghammer59765832017-11-07 10:39:22 +00002328 const CompilerType &integer_clang_type,
2329 bool is_scoped) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002330 // TODO: Do something intelligent with the Declaration object passed in
2331 // like maybe filling in the SourceLocation with it...
2332 ASTContext *ast = getASTContext();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002333
Kate Stoneb9c1b512016-09-06 20:57:50 +00002334 // TODO: ask about these...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002335 // const bool IsFixed = false;
2336
2337 EnumDecl *enum_decl = EnumDecl::Create(
2338 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2339 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
Tamas Berghammercf6bf4c2017-11-07 13:43:55 +00002340 is_scoped, // IsScoped
2341 is_scoped, // IsScopedUsingClassTag
2342 false); // IsFixed
Kate Stoneb9c1b512016-09-06 20:57:50 +00002343
2344 if (enum_decl) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +00002345 if (decl_ctx)
2346 decl_ctx->addDecl(enum_decl);
2347
Kate Stoneb9c1b512016-09-06 20:57:50 +00002348 // TODO: check if we should be setting the promotion type too?
2349 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2350
2351 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2352
2353 return CompilerType(ast, ast->getTagDeclType(enum_decl));
2354 }
2355 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002356}
2357
Kate Stoneb9c1b512016-09-06 20:57:50 +00002358CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2359 size_t bit_size,
2360 bool is_signed) {
2361 if (ast) {
2362 if (is_signed) {
2363 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
2364 return CompilerType(ast, ast->SignedCharTy);
2365
2366 if (bit_size == ast->getTypeSize(ast->ShortTy))
2367 return CompilerType(ast, ast->ShortTy);
2368
2369 if (bit_size == ast->getTypeSize(ast->IntTy))
2370 return CompilerType(ast, ast->IntTy);
2371
2372 if (bit_size == ast->getTypeSize(ast->LongTy))
2373 return CompilerType(ast, ast->LongTy);
2374
2375 if (bit_size == ast->getTypeSize(ast->LongLongTy))
2376 return CompilerType(ast, ast->LongLongTy);
2377
2378 if (bit_size == ast->getTypeSize(ast->Int128Ty))
2379 return CompilerType(ast, ast->Int128Ty);
2380 } else {
2381 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
2382 return CompilerType(ast, ast->UnsignedCharTy);
2383
2384 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
2385 return CompilerType(ast, ast->UnsignedShortTy);
2386
2387 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
2388 return CompilerType(ast, ast->UnsignedIntTy);
2389
2390 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
2391 return CompilerType(ast, ast->UnsignedLongTy);
2392
2393 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
2394 return CompilerType(ast, ast->UnsignedLongLongTy);
2395
2396 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
2397 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002398 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002399 }
2400 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002401}
2402
Kate Stoneb9c1b512016-09-06 20:57:50 +00002403CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2404 bool is_signed) {
2405 if (ast)
2406 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2407 is_signed);
2408 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002409}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002410
Kate Stoneb9c1b512016-09-06 20:57:50 +00002411void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2412 if (decl_ctx) {
2413 DumpDeclContextHiearchy(decl_ctx->getParent());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002414
Kate Stoneb9c1b512016-09-06 20:57:50 +00002415 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2416 if (named_decl) {
2417 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2418 named_decl->getDeclName().getAsString().c_str());
2419 } else {
2420 printf("%20s\n", decl_ctx->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002421 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002422 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002423}
2424
Kate Stoneb9c1b512016-09-06 20:57:50 +00002425void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2426 if (decl == nullptr)
2427 return;
2428 DumpDeclContextHiearchy(decl->getDeclContext());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002429
Kate Stoneb9c1b512016-09-06 20:57:50 +00002430 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2431 if (record_decl) {
2432 printf("%20s: %s%s\n", decl->getDeclKindName(),
2433 record_decl->getDeclName().getAsString().c_str(),
2434 record_decl->isInjectedClassName() ? " (injected class name)" : "");
Greg Claytone6b36cd2015-12-08 01:02:08 +00002435
Kate Stoneb9c1b512016-09-06 20:57:50 +00002436 } else {
2437 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2438 if (named_decl) {
2439 printf("%20s: %s\n", decl->getDeclKindName(),
2440 named_decl->getDeclName().getAsString().c_str());
2441 } else {
2442 printf("%20s\n", decl->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002443 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002444 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002445}
2446
Kate Stoneb9c1b512016-09-06 20:57:50 +00002447bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2448 clang::Decl *rhs_decl) {
2449 if (lhs_decl && rhs_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002450 // Make sure the decl kinds match first
Kate Stoneb9c1b512016-09-06 20:57:50 +00002451 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2452 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002453
Kate Stoneb9c1b512016-09-06 20:57:50 +00002454 if (lhs_decl_kind == rhs_decl_kind) {
Adrian Prantl05097242018-04-30 16:49:04 +00002455 // Now check that the decl contexts kinds are all equivalent before we
2456 // have to check any names of the decl contexts...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002457 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2458 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2459 if (lhs_decl_ctx && rhs_decl_ctx) {
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00002460 while (true) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002461 if (lhs_decl_ctx && rhs_decl_ctx) {
2462 const clang::Decl::Kind lhs_decl_ctx_kind =
2463 lhs_decl_ctx->getDeclKind();
2464 const clang::Decl::Kind rhs_decl_ctx_kind =
2465 rhs_decl_ctx->getDeclKind();
2466 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2467 lhs_decl_ctx = lhs_decl_ctx->getParent();
2468 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002469
Kate Stoneb9c1b512016-09-06 20:57:50 +00002470 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2471 break;
2472 } else
2473 return false;
2474 } else
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00002475 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002476 }
2477
Kate Stoneb9c1b512016-09-06 20:57:50 +00002478 // Now make sure the name of the decls match
Kate Stoneb9c1b512016-09-06 20:57:50 +00002479 clang::NamedDecl *lhs_named_decl =
2480 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2481 clang::NamedDecl *rhs_named_decl =
2482 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2483 if (lhs_named_decl && rhs_named_decl) {
2484 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2485 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2486 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2487 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2488 return false;
2489 } else
Greg Claytona2721472011-06-25 00:44:06 +00002490 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002491 } else
2492 return false;
Greg Claytona2721472011-06-25 00:44:06 +00002493
Adrian Prantl05097242018-04-30 16:49:04 +00002494 // We know that the decl context kinds all match, so now we need to
2495 // make sure the names match as well
Kate Stoneb9c1b512016-09-06 20:57:50 +00002496 lhs_decl_ctx = lhs_decl->getDeclContext();
2497 rhs_decl_ctx = rhs_decl->getDeclContext();
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00002498 while (true) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002499 switch (lhs_decl_ctx->getDeclKind()) {
2500 case clang::Decl::TranslationUnit:
2501 // We don't care about the translation unit names
2502 return true;
2503 default: {
2504 clang::NamedDecl *lhs_named_decl =
2505 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2506 clang::NamedDecl *rhs_named_decl =
2507 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2508 if (lhs_named_decl && rhs_named_decl) {
2509 clang::DeclarationName lhs_decl_name =
2510 lhs_named_decl->getDeclName();
2511 clang::DeclarationName rhs_decl_name =
2512 rhs_named_decl->getDeclName();
2513 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2514 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2515 return false;
2516 } else
2517 return false;
2518 } else
2519 return false;
2520 } break;
2521 }
2522 lhs_decl_ctx = lhs_decl_ctx->getParent();
2523 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytond8d4a572015-08-11 21:38:15 +00002524 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002525 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002526 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002527 }
2528 return false;
2529}
2530bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2531 clang::Decl *decl) {
2532 if (!decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002533 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002534
Kate Stoneb9c1b512016-09-06 20:57:50 +00002535 ExternalASTSource *ast_source = ast->getExternalSource();
Greg Claytond8d4a572015-08-11 21:38:15 +00002536
Kate Stoneb9c1b512016-09-06 20:57:50 +00002537 if (!ast_source)
Greg Claytond8d4a572015-08-11 21:38:15 +00002538 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002539
2540 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2541 if (tag_decl->isCompleteDefinition())
2542 return true;
2543
2544 if (!tag_decl->hasExternalLexicalStorage())
2545 return false;
2546
2547 ast_source->CompleteType(tag_decl);
2548
2549 return !tag_decl->getTypeForDecl()->isIncompleteType();
2550 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2551 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2552 if (objc_interface_decl->getDefinition())
2553 return true;
2554
2555 if (!objc_interface_decl->hasExternalLexicalStorage())
2556 return false;
2557
2558 ast_source->CompleteType(objc_interface_decl);
2559
2560 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2561 } else {
2562 return false;
2563 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002564}
2565
Kate Stoneb9c1b512016-09-06 20:57:50 +00002566void ClangASTContext::SetMetadataAsUserID(const void *object,
2567 user_id_t user_id) {
2568 ClangASTMetadata meta_data;
2569 meta_data.SetUserID(user_id);
2570 SetMetadata(object, meta_data);
Greg Clayton99558cc42015-08-24 23:46:31 +00002571}
2572
Kate Stoneb9c1b512016-09-06 20:57:50 +00002573void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2574 ClangASTMetadata &metadata) {
2575 ClangExternalASTSourceCommon *external_source =
2576 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2577
2578 if (external_source)
2579 external_source->SetMetadata(object, metadata);
2580}
2581
2582ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2583 const void *object) {
2584 ClangExternalASTSourceCommon *external_source =
2585 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2586
2587 if (external_source && external_source->HasMetadata(object))
2588 return external_source->GetMetadata(object);
2589 else
Greg Claytond8d4a572015-08-11 21:38:15 +00002590 return nullptr;
2591}
2592
Kate Stoneb9c1b512016-09-06 20:57:50 +00002593clang::DeclContext *
2594ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2595 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2596}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002597
Kate Stoneb9c1b512016-09-06 20:57:50 +00002598clang::DeclContext *
2599ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2600 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2601}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002602
Kate Stoneb9c1b512016-09-06 20:57:50 +00002603bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2604 int kind) const {
2605 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2606 if (clang_type) {
2607 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2608 if (tag_type) {
2609 clang::TagDecl *tag_decl =
2610 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2611 if (tag_decl) {
2612 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2613 return true;
2614 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002615 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002616 }
2617 return false;
2618}
2619
2620bool ClangASTContext::SetDefaultAccessForRecordFields(
2621 clang::RecordDecl *record_decl, int default_accessibility,
2622 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2623 if (record_decl) {
2624 uint32_t field_idx;
2625 clang::RecordDecl::field_iterator field, field_end;
2626 for (field = record_decl->field_begin(),
2627 field_end = record_decl->field_end(), field_idx = 0;
2628 field != field_end; ++field, ++field_idx) {
2629 // If no accessibility was assigned, assign the correct one
2630 if (field_idx < num_assigned_accessibilities &&
2631 assigned_accessibilities[field_idx] == clang::AS_none)
2632 field->setAccess((clang::AccessSpecifier)default_accessibility);
2633 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002634 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002635 }
2636 return false;
2637}
2638
2639clang::DeclContext *
2640ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2641 return GetDeclContextForType(ClangUtil::GetQualType(type));
2642}
2643
2644clang::DeclContext *
2645ClangASTContext::GetDeclContextForType(clang::QualType type) {
2646 if (type.isNull())
2647 return nullptr;
2648
2649 clang::QualType qual_type = type.getCanonicalType();
2650 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2651 switch (type_class) {
2652 case clang::Type::ObjCInterface:
2653 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2654 ->getInterface();
2655 case clang::Type::ObjCObjectPointer:
2656 return GetDeclContextForType(
2657 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2658 ->getPointeeType());
2659 case clang::Type::Record:
2660 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2661 case clang::Type::Enum:
2662 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2663 case clang::Type::Typedef:
2664 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2665 ->getDecl()
2666 ->getUnderlyingType());
2667 case clang::Type::Auto:
2668 return GetDeclContextForType(
2669 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2670 case clang::Type::Elaborated:
2671 return GetDeclContextForType(
2672 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2673 case clang::Type::Paren:
2674 return GetDeclContextForType(
2675 llvm::cast<clang::ParenType>(qual_type)->desugar());
2676 default:
2677 break;
2678 }
2679 // No DeclContext in this type...
2680 return nullptr;
2681}
2682
2683static bool GetCompleteQualType(clang::ASTContext *ast,
2684 clang::QualType qual_type,
2685 bool allow_completion = true) {
2686 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2687 switch (type_class) {
2688 case clang::Type::ConstantArray:
2689 case clang::Type::IncompleteArray:
2690 case clang::Type::VariableArray: {
2691 const clang::ArrayType *array_type =
2692 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2693
2694 if (array_type)
2695 return GetCompleteQualType(ast, array_type->getElementType(),
2696 allow_completion);
2697 } break;
2698 case clang::Type::Record: {
2699 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2700 if (cxx_record_decl) {
2701 if (cxx_record_decl->hasExternalLexicalStorage()) {
2702 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2703 const bool fields_loaded =
2704 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2705 if (is_complete && fields_loaded)
2706 return true;
2707
2708 if (!allow_completion)
2709 return false;
2710
2711 // Call the field_begin() accessor to for it to use the external source
2712 // to load the fields...
2713 clang::ExternalASTSource *external_ast_source =
2714 ast->getExternalSource();
2715 if (external_ast_source) {
2716 external_ast_source->CompleteType(cxx_record_decl);
2717 if (cxx_record_decl->isCompleteDefinition()) {
2718 cxx_record_decl->field_begin();
2719 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2720 }
2721 }
2722 }
2723 }
2724 const clang::TagType *tag_type =
2725 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2726 return !tag_type->isIncompleteType();
2727 } break;
2728
2729 case clang::Type::Enum: {
2730 const clang::TagType *tag_type =
2731 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2732 if (tag_type) {
2733 clang::TagDecl *tag_decl = tag_type->getDecl();
2734 if (tag_decl) {
2735 if (tag_decl->getDefinition())
2736 return true;
2737
2738 if (!allow_completion)
2739 return false;
2740
2741 if (tag_decl->hasExternalLexicalStorage()) {
2742 if (ast) {
2743 clang::ExternalASTSource *external_ast_source =
2744 ast->getExternalSource();
2745 if (external_ast_source) {
2746 external_ast_source->CompleteType(tag_decl);
2747 return !tag_type->isIncompleteType();
2748 }
2749 }
2750 }
2751 return false;
2752 }
2753 }
2754
2755 } break;
2756 case clang::Type::ObjCObject:
2757 case clang::Type::ObjCInterface: {
2758 const clang::ObjCObjectType *objc_class_type =
2759 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2760 if (objc_class_type) {
2761 clang::ObjCInterfaceDecl *class_interface_decl =
2762 objc_class_type->getInterface();
2763 // We currently can't complete objective C types through the newly added
Adrian Prantl05097242018-04-30 16:49:04 +00002764 // ASTContext because it only supports TagDecl objects right now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002765 if (class_interface_decl) {
2766 if (class_interface_decl->getDefinition())
2767 return true;
2768
2769 if (!allow_completion)
2770 return false;
2771
2772 if (class_interface_decl->hasExternalLexicalStorage()) {
2773 if (ast) {
2774 clang::ExternalASTSource *external_ast_source =
2775 ast->getExternalSource();
2776 if (external_ast_source) {
2777 external_ast_source->CompleteType(class_interface_decl);
2778 return !objc_class_type->isIncompleteType();
2779 }
2780 }
2781 }
2782 return false;
2783 }
2784 }
2785 } break;
2786
2787 case clang::Type::Typedef:
2788 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2789 ->getDecl()
2790 ->getUnderlyingType(),
2791 allow_completion);
2792
2793 case clang::Type::Auto:
2794 return GetCompleteQualType(
2795 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2796 allow_completion);
2797
2798 case clang::Type::Elaborated:
2799 return GetCompleteQualType(
2800 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2801 allow_completion);
2802
2803 case clang::Type::Paren:
2804 return GetCompleteQualType(
2805 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2806 allow_completion);
2807
2808 case clang::Type::Attributed:
2809 return GetCompleteQualType(
2810 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2811 allow_completion);
2812
2813 default:
2814 break;
2815 }
2816
2817 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00002818}
2819
2820static clang::ObjCIvarDecl::AccessControl
Kate Stoneb9c1b512016-09-06 20:57:50 +00002821ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2822 switch (access) {
2823 case eAccessNone:
Greg Claytond8d4a572015-08-11 21:38:15 +00002824 return clang::ObjCIvarDecl::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002825 case eAccessPublic:
2826 return clang::ObjCIvarDecl::Public;
2827 case eAccessPrivate:
2828 return clang::ObjCIvarDecl::Private;
2829 case eAccessProtected:
2830 return clang::ObjCIvarDecl::Protected;
2831 case eAccessPackage:
2832 return clang::ObjCIvarDecl::Package;
2833 }
2834 return clang::ObjCIvarDecl::None;
Greg Claytond8d4a572015-08-11 21:38:15 +00002835}
2836
Greg Claytond8d4a572015-08-11 21:38:15 +00002837// Tests
Greg Claytond8d4a572015-08-11 21:38:15 +00002838
Kate Stoneb9c1b512016-09-06 20:57:50 +00002839bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2840 clang::QualType qual_type(GetCanonicalQualType(type));
2841
2842 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2843 switch (type_class) {
2844 case clang::Type::IncompleteArray:
2845 case clang::Type::VariableArray:
2846 case clang::Type::ConstantArray:
2847 case clang::Type::ExtVector:
2848 case clang::Type::Vector:
2849 case clang::Type::Record:
2850 case clang::Type::ObjCObject:
2851 case clang::Type::ObjCInterface:
2852 return true;
2853 case clang::Type::Auto:
2854 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2855 ->getDeducedType()
2856 .getAsOpaquePtr());
2857 case clang::Type::Elaborated:
2858 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2859 ->getNamedType()
2860 .getAsOpaquePtr());
2861 case clang::Type::Typedef:
2862 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2863 ->getDecl()
2864 ->getUnderlyingType()
2865 .getAsOpaquePtr());
2866 case clang::Type::Paren:
2867 return IsAggregateType(
2868 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2869 default:
2870 break;
2871 }
2872 // The clang type does have a value
2873 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002874}
2875
Kate Stoneb9c1b512016-09-06 20:57:50 +00002876bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2877 clang::QualType qual_type(GetCanonicalQualType(type));
2878
2879 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2880 switch (type_class) {
2881 case clang::Type::Record: {
2882 if (const clang::RecordType *record_type =
2883 llvm::dyn_cast_or_null<clang::RecordType>(
2884 qual_type.getTypePtrOrNull())) {
2885 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2886 return record_decl->isAnonymousStructOrUnion();
2887 }
Enrico Granata7123e2b2015-11-07 02:06:57 +00002888 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002889 break;
2890 }
2891 case clang::Type::Auto:
2892 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2893 ->getDeducedType()
2894 .getAsOpaquePtr());
2895 case clang::Type::Elaborated:
2896 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2897 ->getNamedType()
2898 .getAsOpaquePtr());
2899 case clang::Type::Typedef:
2900 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2901 ->getDecl()
2902 ->getUnderlyingType()
2903 .getAsOpaquePtr());
2904 case clang::Type::Paren:
2905 return IsAnonymousType(
2906 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2907 default:
2908 break;
2909 }
2910 // The clang type does have a value
2911 return false;
Enrico Granata7123e2b2015-11-07 02:06:57 +00002912}
2913
Kate Stoneb9c1b512016-09-06 20:57:50 +00002914bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2915 CompilerType *element_type_ptr,
2916 uint64_t *size, bool *is_incomplete) {
2917 clang::QualType qual_type(GetCanonicalQualType(type));
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002918
Kate Stoneb9c1b512016-09-06 20:57:50 +00002919 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2920 switch (type_class) {
2921 default:
2922 break;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002923
Kate Stoneb9c1b512016-09-06 20:57:50 +00002924 case clang::Type::ConstantArray:
Greg Claytond8d4a572015-08-11 21:38:15 +00002925 if (element_type_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002926 element_type_ptr->SetCompilerType(
2927 getASTContext(),
2928 llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002929 if (size)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002930 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2931 ->getSize()
2932 .getLimitedValue(ULLONG_MAX);
Greg Claytond8d4a572015-08-11 21:38:15 +00002933 if (is_incomplete)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002934 *is_incomplete = false;
2935 return true;
2936
2937 case clang::Type::IncompleteArray:
2938 if (element_type_ptr)
2939 element_type_ptr->SetCompilerType(
2940 getASTContext(),
2941 llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2942 if (size)
2943 *size = 0;
2944 if (is_incomplete)
2945 *is_incomplete = true;
2946 return true;
2947
2948 case clang::Type::VariableArray:
2949 if (element_type_ptr)
2950 element_type_ptr->SetCompilerType(
2951 getASTContext(),
2952 llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2953 if (size)
2954 *size = 0;
2955 if (is_incomplete)
2956 *is_incomplete = false;
2957 return true;
2958
2959 case clang::Type::DependentSizedArray:
2960 if (element_type_ptr)
2961 element_type_ptr->SetCompilerType(
2962 getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)
2963 ->getElementType());
2964 if (size)
2965 *size = 0;
2966 if (is_incomplete)
2967 *is_incomplete = false;
2968 return true;
2969
2970 case clang::Type::Typedef:
2971 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2972 ->getDecl()
2973 ->getUnderlyingType()
2974 .getAsOpaquePtr(),
2975 element_type_ptr, size, is_incomplete);
2976 case clang::Type::Auto:
2977 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2978 ->getDeducedType()
2979 .getAsOpaquePtr(),
2980 element_type_ptr, size, is_incomplete);
2981 case clang::Type::Elaborated:
2982 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2983 ->getNamedType()
2984 .getAsOpaquePtr(),
2985 element_type_ptr, size, is_incomplete);
2986 case clang::Type::Paren:
2987 return IsArrayType(
2988 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2989 element_type_ptr, size, is_incomplete);
2990 }
2991 if (element_type_ptr)
2992 element_type_ptr->Clear();
2993 if (size)
2994 *size = 0;
2995 if (is_incomplete)
2996 *is_incomplete = false;
2997 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002998}
2999
Kate Stoneb9c1b512016-09-06 20:57:50 +00003000bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
3001 CompilerType *element_type, uint64_t *size) {
3002 clang::QualType qual_type(GetCanonicalQualType(type));
3003
3004 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3005 switch (type_class) {
3006 case clang::Type::Vector: {
3007 const clang::VectorType *vector_type =
3008 qual_type->getAs<clang::VectorType>();
3009 if (vector_type) {
3010 if (size)
3011 *size = vector_type->getNumElements();
3012 if (element_type)
3013 *element_type =
3014 CompilerType(getASTContext(), vector_type->getElementType());
3015 }
3016 return true;
3017 } break;
3018 case clang::Type::ExtVector: {
3019 const clang::ExtVectorType *ext_vector_type =
3020 qual_type->getAs<clang::ExtVectorType>();
3021 if (ext_vector_type) {
3022 if (size)
3023 *size = ext_vector_type->getNumElements();
3024 if (element_type)
3025 *element_type =
3026 CompilerType(getASTContext(), ext_vector_type->getElementType());
3027 }
3028 return true;
3029 }
3030 default:
3031 break;
3032 }
3033 return false;
3034}
3035
3036bool ClangASTContext::IsRuntimeGeneratedType(
3037 lldb::opaque_compiler_type_t type) {
3038 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
3039 ->GetDeclContextForType(GetQualType(type));
3040 if (!decl_ctx)
3041 return false;
3042
3043 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
3044 return false;
3045
3046 clang::ObjCInterfaceDecl *result_iface_decl =
3047 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
3048
3049 ClangASTMetadata *ast_metadata =
3050 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
3051 if (!ast_metadata)
3052 return false;
3053 return (ast_metadata->GetISAPtr() != 0);
3054}
3055
3056bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
3057 return GetQualType(type).getUnqualifiedType()->isCharType();
3058}
3059
3060bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
3061 const bool allow_completion = false;
3062 return GetCompleteQualType(getASTContext(), GetQualType(type),
3063 allow_completion);
3064}
3065
3066bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
3067 return GetQualType(type).isConstQualified();
3068}
3069
3070bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
3071 uint32_t &length) {
3072 CompilerType pointee_or_element_clang_type;
3073 length = 0;
3074 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
3075
3076 if (!pointee_or_element_clang_type.IsValid())
3077 return false;
3078
3079 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
3080 if (pointee_or_element_clang_type.IsCharType()) {
3081 if (type_flags.Test(eTypeIsArray)) {
Adrian Prantl05097242018-04-30 16:49:04 +00003082 // We know the size of the array and it could be a C string since it is
3083 // an array of characters
Kate Stoneb9c1b512016-09-06 20:57:50 +00003084 length = llvm::cast<clang::ConstantArrayType>(
3085 GetCanonicalQualType(type).getTypePtr())
3086 ->getSize()
3087 .getLimitedValue();
3088 }
3089 return true;
3090 }
3091 }
3092 return false;
3093}
3094
3095bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
3096 bool *is_variadic_ptr) {
3097 if (type) {
3098 clang::QualType qual_type(GetCanonicalQualType(type));
3099
3100 if (qual_type->isFunctionType()) {
3101 if (is_variadic_ptr) {
3102 const clang::FunctionProtoType *function_proto_type =
3103 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3104 if (function_proto_type)
3105 *is_variadic_ptr = function_proto_type->isVariadic();
3106 else
3107 *is_variadic_ptr = false;
3108 }
3109 return true;
3110 }
3111
Greg Claytond8d4a572015-08-11 21:38:15 +00003112 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003113 switch (type_class) {
3114 default:
3115 break;
3116 case clang::Type::Typedef:
3117 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
3118 ->getDecl()
3119 ->getUnderlyingType()
3120 .getAsOpaquePtr(),
3121 nullptr);
3122 case clang::Type::Auto:
3123 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
3124 ->getDeducedType()
3125 .getAsOpaquePtr(),
3126 nullptr);
3127 case clang::Type::Elaborated:
3128 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
3129 ->getNamedType()
3130 .getAsOpaquePtr(),
3131 nullptr);
3132 case clang::Type::Paren:
3133 return IsFunctionType(
3134 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3135 nullptr);
3136 case clang::Type::LValueReference:
3137 case clang::Type::RValueReference: {
3138 const clang::ReferenceType *reference_type =
3139 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3140 if (reference_type)
3141 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
3142 nullptr);
3143 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00003144 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003145 }
3146 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003147}
3148
3149// Used to detect "Homogeneous Floating-point Aggregates"
3150uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003151ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
3152 CompilerType *base_type_ptr) {
3153 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003154 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003155
3156 clang::QualType qual_type(GetCanonicalQualType(type));
3157 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3158 switch (type_class) {
3159 case clang::Type::Record:
3160 if (GetCompleteType(type)) {
3161 const clang::CXXRecordDecl *cxx_record_decl =
3162 qual_type->getAsCXXRecordDecl();
3163 if (cxx_record_decl) {
3164 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3165 return 0;
3166 }
3167 const clang::RecordType *record_type =
3168 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3169 if (record_type) {
3170 const clang::RecordDecl *record_decl = record_type->getDecl();
3171 if (record_decl) {
3172 // We are looking for a structure that contains only floating point
3173 // types
3174 clang::RecordDecl::field_iterator field_pos,
3175 field_end = record_decl->field_end();
3176 uint32_t num_fields = 0;
3177 bool is_hva = false;
3178 bool is_hfa = false;
3179 clang::QualType base_qual_type;
3180 uint64_t base_bitwidth = 0;
3181 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3182 ++field_pos) {
3183 clang::QualType field_qual_type = field_pos->getType();
3184 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
3185 if (field_qual_type->isFloatingType()) {
3186 if (field_qual_type->isComplexType())
3187 return 0;
3188 else {
3189 if (num_fields == 0)
3190 base_qual_type = field_qual_type;
3191 else {
3192 if (is_hva)
3193 return 0;
3194 is_hfa = true;
3195 if (field_qual_type.getTypePtr() !=
3196 base_qual_type.getTypePtr())
3197 return 0;
3198 }
3199 }
3200 } else if (field_qual_type->isVectorType() ||
3201 field_qual_type->isExtVectorType()) {
3202 if (num_fields == 0) {
3203 base_qual_type = field_qual_type;
3204 base_bitwidth = field_bitwidth;
3205 } else {
3206 if (is_hfa)
3207 return 0;
3208 is_hva = true;
3209 if (base_bitwidth != field_bitwidth)
3210 return 0;
3211 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3212 return 0;
3213 }
3214 } else
3215 return 0;
3216 ++num_fields;
3217 }
3218 if (base_type_ptr)
3219 *base_type_ptr = CompilerType(getASTContext(), base_qual_type);
3220 return num_fields;
3221 }
3222 }
3223 }
3224 break;
3225
3226 case clang::Type::Typedef:
3227 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3228 ->getDecl()
3229 ->getUnderlyingType()
3230 .getAsOpaquePtr(),
3231 base_type_ptr);
3232
3233 case clang::Type::Auto:
3234 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3235 ->getDeducedType()
3236 .getAsOpaquePtr(),
3237 base_type_ptr);
3238
3239 case clang::Type::Elaborated:
3240 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3241 ->getNamedType()
3242 .getAsOpaquePtr(),
3243 base_type_ptr);
3244 default:
3245 break;
3246 }
3247 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003248}
3249
Kate Stoneb9c1b512016-09-06 20:57:50 +00003250size_t ClangASTContext::GetNumberOfFunctionArguments(
3251 lldb::opaque_compiler_type_t type) {
3252 if (type) {
3253 clang::QualType qual_type(GetCanonicalQualType(type));
3254 const clang::FunctionProtoType *func =
3255 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3256 if (func)
3257 return func->getNumParams();
3258 }
3259 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003260}
3261
Greg Claytona1e5dc82015-08-11 22:53:00 +00003262CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00003263ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3264 const size_t index) {
3265 if (type) {
Greg Claytond8d4a572015-08-11 21:38:15 +00003266 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003267 const clang::FunctionProtoType *func =
3268 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3269 if (func) {
3270 if (index < func->getNumParams())
3271 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00003272 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003273 }
3274 return CompilerType();
3275}
3276
3277bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3278 if (type) {
3279 clang::QualType qual_type(GetCanonicalQualType(type));
3280
3281 if (qual_type->isFunctionPointerType())
3282 return true;
3283
3284 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3285 switch (type_class) {
3286 default:
3287 break;
3288 case clang::Type::Typedef:
3289 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3290 ->getDecl()
3291 ->getUnderlyingType()
3292 .getAsOpaquePtr());
3293 case clang::Type::Auto:
3294 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3295 ->getDeducedType()
3296 .getAsOpaquePtr());
3297 case clang::Type::Elaborated:
3298 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3299 ->getNamedType()
3300 .getAsOpaquePtr());
3301 case clang::Type::Paren:
3302 return IsFunctionPointerType(
3303 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3304
3305 case clang::Type::LValueReference:
3306 case clang::Type::RValueReference: {
3307 const clang::ReferenceType *reference_type =
3308 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3309 if (reference_type)
3310 return IsFunctionPointerType(
3311 reference_type->getPointeeType().getAsOpaquePtr());
3312 } break;
3313 }
3314 }
3315 return false;
3316}
3317
3318bool ClangASTContext::IsBlockPointerType(
3319 lldb::opaque_compiler_type_t type,
3320 CompilerType *function_pointer_type_ptr) {
3321 if (type) {
3322 clang::QualType qual_type(GetCanonicalQualType(type));
3323
3324 if (qual_type->isBlockPointerType()) {
3325 if (function_pointer_type_ptr) {
3326 const clang::BlockPointerType *block_pointer_type =
3327 qual_type->getAs<clang::BlockPointerType>();
3328 QualType pointee_type = block_pointer_type->getPointeeType();
Jonas Devlieghered5b44032019-02-13 06:25:41 +00003329 QualType function_pointer_type = m_ast_up->getPointerType(pointee_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003330 *function_pointer_type_ptr =
3331 CompilerType(getASTContext(), function_pointer_type);
3332 }
3333 return true;
3334 }
3335
3336 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3337 switch (type_class) {
3338 default:
3339 break;
3340 case clang::Type::Typedef:
3341 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3342 ->getDecl()
3343 ->getUnderlyingType()
3344 .getAsOpaquePtr(),
3345 function_pointer_type_ptr);
3346 case clang::Type::Auto:
3347 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3348 ->getDeducedType()
3349 .getAsOpaquePtr(),
3350 function_pointer_type_ptr);
3351 case clang::Type::Elaborated:
3352 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3353 ->getNamedType()
3354 .getAsOpaquePtr(),
3355 function_pointer_type_ptr);
3356 case clang::Type::Paren:
3357 return IsBlockPointerType(
3358 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3359 function_pointer_type_ptr);
3360
3361 case clang::Type::LValueReference:
3362 case clang::Type::RValueReference: {
3363 const clang::ReferenceType *reference_type =
3364 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3365 if (reference_type)
3366 return IsBlockPointerType(
3367 reference_type->getPointeeType().getAsOpaquePtr(),
3368 function_pointer_type_ptr);
3369 } break;
3370 }
3371 }
3372 return false;
3373}
3374
3375bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3376 bool &is_signed) {
3377 if (!type)
3378 return false;
3379
3380 clang::QualType qual_type(GetCanonicalQualType(type));
3381 const clang::BuiltinType *builtin_type =
3382 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3383
3384 if (builtin_type) {
3385 if (builtin_type->isInteger()) {
3386 is_signed = builtin_type->isSignedInteger();
3387 return true;
3388 }
3389 }
3390
3391 return false;
3392}
3393
3394bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3395 bool &is_signed) {
3396 if (type) {
3397 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3398 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3399
3400 if (enum_type) {
3401 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3402 is_signed);
3403 return true;
3404 }
3405 }
3406
3407 return false;
3408}
3409
3410bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3411 CompilerType *pointee_type) {
3412 if (type) {
3413 clang::QualType qual_type(GetCanonicalQualType(type));
3414 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3415 switch (type_class) {
3416 case clang::Type::Builtin:
3417 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3418 default:
3419 break;
3420 case clang::BuiltinType::ObjCId:
3421 case clang::BuiltinType::ObjCClass:
3422 return true;
3423 }
3424 return false;
3425 case clang::Type::ObjCObjectPointer:
3426 if (pointee_type)
3427 pointee_type->SetCompilerType(
3428 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3429 ->getPointeeType());
3430 return true;
3431 case clang::Type::BlockPointer:
3432 if (pointee_type)
3433 pointee_type->SetCompilerType(
3434 getASTContext(),
3435 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3436 return true;
3437 case clang::Type::Pointer:
3438 if (pointee_type)
3439 pointee_type->SetCompilerType(
3440 getASTContext(),
3441 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3442 return true;
3443 case clang::Type::MemberPointer:
3444 if (pointee_type)
3445 pointee_type->SetCompilerType(
3446 getASTContext(),
3447 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3448 return true;
3449 case clang::Type::Typedef:
3450 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3451 ->getDecl()
3452 ->getUnderlyingType()
3453 .getAsOpaquePtr(),
3454 pointee_type);
3455 case clang::Type::Auto:
3456 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3457 ->getDeducedType()
3458 .getAsOpaquePtr(),
3459 pointee_type);
3460 case clang::Type::Elaborated:
3461 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3462 ->getNamedType()
3463 .getAsOpaquePtr(),
3464 pointee_type);
3465 case clang::Type::Paren:
3466 return IsPointerType(
3467 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3468 pointee_type);
3469 default:
3470 break;
3471 }
3472 }
3473 if (pointee_type)
3474 pointee_type->Clear();
3475 return false;
3476}
3477
3478bool ClangASTContext::IsPointerOrReferenceType(
3479 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3480 if (type) {
3481 clang::QualType qual_type(GetCanonicalQualType(type));
3482 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3483 switch (type_class) {
3484 case clang::Type::Builtin:
3485 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3486 default:
3487 break;
3488 case clang::BuiltinType::ObjCId:
3489 case clang::BuiltinType::ObjCClass:
3490 return true;
3491 }
3492 return false;
3493 case clang::Type::ObjCObjectPointer:
3494 if (pointee_type)
3495 pointee_type->SetCompilerType(
3496 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3497 ->getPointeeType());
3498 return true;
3499 case clang::Type::BlockPointer:
3500 if (pointee_type)
3501 pointee_type->SetCompilerType(
3502 getASTContext(),
3503 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3504 return true;
3505 case clang::Type::Pointer:
3506 if (pointee_type)
3507 pointee_type->SetCompilerType(
3508 getASTContext(),
3509 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3510 return true;
3511 case clang::Type::MemberPointer:
3512 if (pointee_type)
3513 pointee_type->SetCompilerType(
3514 getASTContext(),
3515 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3516 return true;
3517 case clang::Type::LValueReference:
3518 if (pointee_type)
3519 pointee_type->SetCompilerType(
3520 getASTContext(),
3521 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3522 return true;
3523 case clang::Type::RValueReference:
3524 if (pointee_type)
3525 pointee_type->SetCompilerType(
3526 getASTContext(),
3527 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3528 return true;
3529 case clang::Type::Typedef:
3530 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3531 ->getDecl()
3532 ->getUnderlyingType()
3533 .getAsOpaquePtr(),
3534 pointee_type);
3535 case clang::Type::Auto:
3536 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3537 ->getDeducedType()
3538 .getAsOpaquePtr(),
3539 pointee_type);
3540 case clang::Type::Elaborated:
3541 return IsPointerOrReferenceType(
3542 llvm::cast<clang::ElaboratedType>(qual_type)
3543 ->getNamedType()
3544 .getAsOpaquePtr(),
3545 pointee_type);
3546 case clang::Type::Paren:
3547 return IsPointerOrReferenceType(
3548 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3549 pointee_type);
3550 default:
3551 break;
3552 }
3553 }
3554 if (pointee_type)
3555 pointee_type->Clear();
3556 return false;
3557}
3558
3559bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3560 CompilerType *pointee_type,
3561 bool *is_rvalue) {
3562 if (type) {
3563 clang::QualType qual_type(GetCanonicalQualType(type));
3564 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3565
3566 switch (type_class) {
3567 case clang::Type::LValueReference:
3568 if (pointee_type)
3569 pointee_type->SetCompilerType(
3570 getASTContext(),
3571 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3572 if (is_rvalue)
3573 *is_rvalue = false;
3574 return true;
3575 case clang::Type::RValueReference:
3576 if (pointee_type)
3577 pointee_type->SetCompilerType(
3578 getASTContext(),
3579 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3580 if (is_rvalue)
3581 *is_rvalue = true;
3582 return true;
3583 case clang::Type::Typedef:
3584 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3585 ->getDecl()
3586 ->getUnderlyingType()
3587 .getAsOpaquePtr(),
3588 pointee_type, is_rvalue);
3589 case clang::Type::Auto:
3590 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3591 ->getDeducedType()
3592 .getAsOpaquePtr(),
3593 pointee_type, is_rvalue);
3594 case clang::Type::Elaborated:
3595 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3596 ->getNamedType()
3597 .getAsOpaquePtr(),
3598 pointee_type, is_rvalue);
3599 case clang::Type::Paren:
3600 return IsReferenceType(
3601 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3602 pointee_type, is_rvalue);
3603
3604 default:
3605 break;
3606 }
3607 }
3608 if (pointee_type)
3609 pointee_type->Clear();
3610 return false;
3611}
3612
3613bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3614 uint32_t &count, bool &is_complex) {
3615 if (type) {
3616 clang::QualType qual_type(GetCanonicalQualType(type));
3617
3618 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3619 qual_type->getCanonicalTypeInternal())) {
3620 clang::BuiltinType::Kind kind = BT->getKind();
3621 if (kind >= clang::BuiltinType::Float &&
3622 kind <= clang::BuiltinType::LongDouble) {
3623 count = 1;
3624 is_complex = false;
3625 return true;
3626 }
3627 } else if (const clang::ComplexType *CT =
3628 llvm::dyn_cast<clang::ComplexType>(
3629 qual_type->getCanonicalTypeInternal())) {
3630 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3631 is_complex)) {
3632 count = 2;
3633 is_complex = true;
3634 return true;
3635 }
3636 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3637 qual_type->getCanonicalTypeInternal())) {
3638 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3639 is_complex)) {
3640 count = VT->getNumElements();
3641 is_complex = false;
3642 return true;
3643 }
3644 }
3645 }
3646 count = 0;
3647 is_complex = false;
3648 return false;
3649}
3650
3651bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3652 if (!type)
3653 return false;
3654
3655 clang::QualType qual_type(GetQualType(type));
3656 const clang::TagType *tag_type =
3657 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3658 if (tag_type) {
3659 clang::TagDecl *tag_decl = tag_type->getDecl();
3660 if (tag_decl)
3661 return tag_decl->isCompleteDefinition();
3662 return false;
3663 } else {
3664 const clang::ObjCObjectType *objc_class_type =
3665 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3666 if (objc_class_type) {
3667 clang::ObjCInterfaceDecl *class_interface_decl =
3668 objc_class_type->getInterface();
3669 if (class_interface_decl)
3670 return class_interface_decl->getDefinition() != nullptr;
3671 return false;
3672 }
3673 }
3674 return true;
3675}
3676
3677bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
3678 if (type) {
3679 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3680
3681 const clang::ObjCObjectPointerType *obj_pointer_type =
3682 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3683
3684 if (obj_pointer_type)
3685 return obj_pointer_type->isObjCClassType();
3686 }
3687 return false;
3688}
3689
3690bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3691 if (ClangUtil::IsClangType(type))
3692 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3693 return false;
3694}
3695
3696bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3697 if (!type)
3698 return false;
3699 clang::QualType qual_type(GetCanonicalQualType(type));
3700 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3701 return (type_class == clang::Type::Record);
3702}
3703
3704bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3705 if (!type)
3706 return false;
3707 clang::QualType qual_type(GetCanonicalQualType(type));
3708 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3709 return (type_class == clang::Type::Enum);
3710}
3711
3712bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3713 if (type) {
3714 clang::QualType qual_type(GetCanonicalQualType(type));
3715 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3716 switch (type_class) {
3717 case clang::Type::Record:
3718 if (GetCompleteType(type)) {
3719 const clang::RecordType *record_type =
3720 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3721 const clang::RecordDecl *record_decl = record_type->getDecl();
3722 if (record_decl) {
3723 const clang::CXXRecordDecl *cxx_record_decl =
3724 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3725 if (cxx_record_decl)
3726 return cxx_record_decl->isPolymorphic();
Greg Claytond8d4a572015-08-11 21:38:15 +00003727 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003728 }
3729 break;
3730
3731 default:
3732 break;
3733 }
3734 }
3735 return false;
3736}
3737
3738bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3739 CompilerType *dynamic_pointee_type,
3740 bool check_cplusplus,
3741 bool check_objc) {
3742 clang::QualType pointee_qual_type;
3743 if (type) {
3744 clang::QualType qual_type(GetCanonicalQualType(type));
3745 bool success = false;
3746 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3747 switch (type_class) {
3748 case clang::Type::Builtin:
3749 if (check_objc &&
3750 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3751 clang::BuiltinType::ObjCId) {
3752 if (dynamic_pointee_type)
3753 dynamic_pointee_type->SetCompilerType(this, type);
3754 return true;
3755 }
3756 break;
3757
3758 case clang::Type::ObjCObjectPointer:
3759 if (check_objc) {
3760 if (auto objc_pointee_type =
3761 qual_type->getPointeeType().getTypePtrOrNull()) {
3762 if (auto objc_object_type =
3763 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3764 objc_pointee_type)) {
3765 if (objc_object_type->isObjCClass())
3766 return false;
3767 }
3768 }
3769 if (dynamic_pointee_type)
3770 dynamic_pointee_type->SetCompilerType(
3771 getASTContext(),
3772 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3773 ->getPointeeType());
3774 return true;
3775 }
3776 break;
3777
3778 case clang::Type::Pointer:
3779 pointee_qual_type =
3780 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3781 success = true;
3782 break;
3783
3784 case clang::Type::LValueReference:
3785 case clang::Type::RValueReference:
3786 pointee_qual_type =
3787 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3788 success = true;
3789 break;
3790
3791 case clang::Type::Typedef:
3792 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3793 ->getDecl()
3794 ->getUnderlyingType()
3795 .getAsOpaquePtr(),
3796 dynamic_pointee_type, check_cplusplus,
3797 check_objc);
3798
3799 case clang::Type::Auto:
3800 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3801 ->getDeducedType()
3802 .getAsOpaquePtr(),
3803 dynamic_pointee_type, check_cplusplus,
3804 check_objc);
3805
3806 case clang::Type::Elaborated:
3807 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3808 ->getNamedType()
3809 .getAsOpaquePtr(),
3810 dynamic_pointee_type, check_cplusplus,
3811 check_objc);
3812
3813 case clang::Type::Paren:
3814 return IsPossibleDynamicType(
3815 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3816 dynamic_pointee_type, check_cplusplus, check_objc);
3817 default:
3818 break;
3819 }
3820
3821 if (success) {
3822 // Check to make sure what we are pointing too is a possible dynamic C++
Adrian Prantl05097242018-04-30 16:49:04 +00003823 // type We currently accept any "void *" (in case we have a class that
3824 // has been watered down to an opaque pointer) and virtual C++ classes.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003825 const clang::Type::TypeClass pointee_type_class =
3826 pointee_qual_type.getCanonicalType()->getTypeClass();
3827 switch (pointee_type_class) {
3828 case clang::Type::Builtin:
3829 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3830 case clang::BuiltinType::UnknownAny:
3831 case clang::BuiltinType::Void:
3832 if (dynamic_pointee_type)
3833 dynamic_pointee_type->SetCompilerType(getASTContext(),
3834 pointee_qual_type);
3835 return true;
3836 default:
3837 break;
3838 }
3839 break;
3840
3841 case clang::Type::Record:
3842 if (check_cplusplus) {
3843 clang::CXXRecordDecl *cxx_record_decl =
3844 pointee_qual_type->getAsCXXRecordDecl();
3845 if (cxx_record_decl) {
3846 bool is_complete = cxx_record_decl->isCompleteDefinition();
3847
3848 if (is_complete)
3849 success = cxx_record_decl->isDynamicClass();
3850 else {
3851 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3852 getASTContext(), cxx_record_decl);
3853 if (metadata)
3854 success = metadata->GetIsDynamicCXXType();
3855 else {
3856 is_complete = CompilerType(getASTContext(), pointee_qual_type)
3857 .GetCompleteType();
3858 if (is_complete)
3859 success = cxx_record_decl->isDynamicClass();
3860 else
3861 success = false;
3862 }
3863 }
3864
3865 if (success) {
3866 if (dynamic_pointee_type)
3867 dynamic_pointee_type->SetCompilerType(getASTContext(),
3868 pointee_qual_type);
3869 return true;
3870 }
3871 }
3872 }
3873 break;
3874
3875 case clang::Type::ObjCObject:
3876 case clang::Type::ObjCInterface:
3877 if (check_objc) {
3878 if (dynamic_pointee_type)
3879 dynamic_pointee_type->SetCompilerType(getASTContext(),
3880 pointee_qual_type);
3881 return true;
3882 }
3883 break;
3884
3885 default:
3886 break;
3887 }
3888 }
3889 }
3890 if (dynamic_pointee_type)
3891 dynamic_pointee_type->Clear();
3892 return false;
3893}
3894
3895bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3896 if (!type)
3897 return false;
3898
3899 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3900}
3901
3902bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3903 if (!type)
3904 return false;
3905 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3906}
3907
3908bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3909 if (!type)
3910 return false;
3911 return GetCanonicalQualType(type)->isVoidType();
3912}
3913
3914bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3915 return ClangASTContextSupportsLanguage(language);
3916}
3917
3918bool ClangASTContext::GetCXXClassName(const CompilerType &type,
3919 std::string &class_name) {
3920 if (type) {
3921 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3922 if (!qual_type.isNull()) {
3923 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3924 if (cxx_record_decl) {
3925 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3926 return true;
3927 }
3928 }
3929 }
3930 class_name.clear();
3931 return false;
3932}
3933
3934bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3935 if (!type)
3936 return false;
3937
3938 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Jonas Devliegherea6682a42018-12-15 00:15:33 +00003939 return !qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003940}
3941
3942bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3943 if (!type)
3944 return false;
3945 clang::QualType qual_type(GetCanonicalQualType(type));
3946 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3947 if (tag_type)
3948 return tag_type->isBeingDefined();
3949 return false;
3950}
3951
3952bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3953 CompilerType *class_type_ptr) {
3954 if (!type)
3955 return false;
3956
3957 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3958
3959 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3960 if (class_type_ptr) {
3961 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3962 const clang::ObjCObjectPointerType *obj_pointer_type =
3963 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3964 if (obj_pointer_type == nullptr)
3965 class_type_ptr->Clear();
3966 else
3967 class_type_ptr->SetCompilerType(
3968 type.GetTypeSystem(),
3969 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3970 .getAsOpaquePtr());
3971 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003972 }
3973 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003974 }
3975 if (class_type_ptr)
3976 class_type_ptr->Clear();
3977 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003978}
3979
Kate Stoneb9c1b512016-09-06 20:57:50 +00003980bool ClangASTContext::GetObjCClassName(const CompilerType &type,
3981 std::string &class_name) {
3982 if (!type)
3983 return false;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003984
Kate Stoneb9c1b512016-09-06 20:57:50 +00003985 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3986
3987 const clang::ObjCObjectType *object_type =
3988 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3989 if (object_type) {
3990 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3991 if (interface) {
3992 class_name = interface->getNameAsString();
3993 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00003994 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003995 }
3996 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003997}
3998
Greg Claytond8d4a572015-08-11 21:38:15 +00003999// Type Completion
Greg Claytond8d4a572015-08-11 21:38:15 +00004000
Kate Stoneb9c1b512016-09-06 20:57:50 +00004001bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
4002 if (!type)
4003 return false;
4004 const bool allow_completion = true;
4005 return GetCompleteQualType(getASTContext(), GetQualType(type),
4006 allow_completion);
Greg Claytond8d4a572015-08-11 21:38:15 +00004007}
4008
Kate Stoneb9c1b512016-09-06 20:57:50 +00004009ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
4010 std::string type_name;
4011 if (type) {
4012 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
4013 clang::QualType qual_type(GetQualType(type));
4014 printing_policy.SuppressTagKeyword = true;
4015 const clang::TypedefType *typedef_type =
4016 qual_type->getAs<clang::TypedefType>();
4017 if (typedef_type) {
4018 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
4019 type_name = typedef_decl->getQualifiedNameAsString();
4020 } else {
4021 type_name = qual_type.getAsString(printing_policy);
Greg Claytond8d4a572015-08-11 21:38:15 +00004022 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004023 }
4024 return ConstString(type_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00004025}
4026
4027uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004028ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
4029 CompilerType *pointee_or_element_clang_type) {
4030 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004031 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004032
4033 if (pointee_or_element_clang_type)
4034 pointee_or_element_clang_type->Clear();
4035
4036 clang::QualType qual_type(GetQualType(type));
4037
4038 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4039 switch (type_class) {
Sean Callananddf802a2017-06-02 01:24:18 +00004040 case clang::Type::Attributed:
4041 return GetTypeInfo(
4042 qual_type->getAs<clang::AttributedType>()
4043 ->getModifiedType().getAsOpaquePtr(),
4044 pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004045 case clang::Type::Builtin: {
4046 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
4047 qual_type->getCanonicalTypeInternal());
4048
4049 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
4050 switch (builtin_type->getKind()) {
4051 case clang::BuiltinType::ObjCId:
4052 case clang::BuiltinType::ObjCClass:
4053 if (pointee_or_element_clang_type)
4054 pointee_or_element_clang_type->SetCompilerType(
4055 getASTContext(), getASTContext()->ObjCBuiltinClassTy);
4056 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
4057 break;
4058
4059 case clang::BuiltinType::ObjCSel:
4060 if (pointee_or_element_clang_type)
4061 pointee_or_element_clang_type->SetCompilerType(getASTContext(),
4062 getASTContext()->CharTy);
4063 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
4064 break;
4065
4066 case clang::BuiltinType::Bool:
4067 case clang::BuiltinType::Char_U:
4068 case clang::BuiltinType::UChar:
4069 case clang::BuiltinType::WChar_U:
4070 case clang::BuiltinType::Char16:
4071 case clang::BuiltinType::Char32:
4072 case clang::BuiltinType::UShort:
4073 case clang::BuiltinType::UInt:
4074 case clang::BuiltinType::ULong:
4075 case clang::BuiltinType::ULongLong:
4076 case clang::BuiltinType::UInt128:
4077 case clang::BuiltinType::Char_S:
4078 case clang::BuiltinType::SChar:
4079 case clang::BuiltinType::WChar_S:
4080 case clang::BuiltinType::Short:
4081 case clang::BuiltinType::Int:
4082 case clang::BuiltinType::Long:
4083 case clang::BuiltinType::LongLong:
4084 case clang::BuiltinType::Int128:
4085 case clang::BuiltinType::Float:
4086 case clang::BuiltinType::Double:
4087 case clang::BuiltinType::LongDouble:
4088 builtin_type_flags |= eTypeIsScalar;
4089 if (builtin_type->isInteger()) {
4090 builtin_type_flags |= eTypeIsInteger;
4091 if (builtin_type->isSignedInteger())
4092 builtin_type_flags |= eTypeIsSigned;
4093 } else if (builtin_type->isFloatingPoint())
4094 builtin_type_flags |= eTypeIsFloat;
4095 break;
4096 default:
4097 break;
4098 }
4099 return builtin_type_flags;
4100 }
4101
4102 case clang::Type::BlockPointer:
4103 if (pointee_or_element_clang_type)
4104 pointee_or_element_clang_type->SetCompilerType(
4105 getASTContext(), qual_type->getPointeeType());
4106 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
4107
4108 case clang::Type::Complex: {
4109 uint32_t complex_type_flags =
4110 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
4111 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
4112 qual_type->getCanonicalTypeInternal());
4113 if (complex_type) {
4114 clang::QualType complex_element_type(complex_type->getElementType());
4115 if (complex_element_type->isIntegerType())
4116 complex_type_flags |= eTypeIsFloat;
4117 else if (complex_element_type->isFloatingType())
4118 complex_type_flags |= eTypeIsInteger;
4119 }
4120 return complex_type_flags;
4121 } break;
4122
4123 case clang::Type::ConstantArray:
4124 case clang::Type::DependentSizedArray:
4125 case clang::Type::IncompleteArray:
4126 case clang::Type::VariableArray:
4127 if (pointee_or_element_clang_type)
4128 pointee_or_element_clang_type->SetCompilerType(
4129 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
4130 ->getElementType());
4131 return eTypeHasChildren | eTypeIsArray;
4132
4133 case clang::Type::DependentName:
4134 return 0;
4135 case clang::Type::DependentSizedExtVector:
4136 return eTypeHasChildren | eTypeIsVector;
4137 case clang::Type::DependentTemplateSpecialization:
4138 return eTypeIsTemplate;
4139 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004140 return CompilerType(
4141 getASTContext(),
4142 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
4143 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004144
4145 case clang::Type::Enum:
4146 if (pointee_or_element_clang_type)
4147 pointee_or_element_clang_type->SetCompilerType(
4148 getASTContext(),
4149 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
4150 return eTypeIsEnumeration | eTypeHasValue;
4151
4152 case clang::Type::Auto:
4153 return CompilerType(
4154 getASTContext(),
4155 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4156 .GetTypeInfo(pointee_or_element_clang_type);
4157 case clang::Type::Elaborated:
4158 return CompilerType(
4159 getASTContext(),
4160 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4161 .GetTypeInfo(pointee_or_element_clang_type);
4162 case clang::Type::Paren:
4163 return CompilerType(getASTContext(),
4164 llvm::cast<clang::ParenType>(qual_type)->desugar())
4165 .GetTypeInfo(pointee_or_element_clang_type);
4166
4167 case clang::Type::FunctionProto:
4168 return eTypeIsFuncPrototype | eTypeHasValue;
4169 case clang::Type::FunctionNoProto:
4170 return eTypeIsFuncPrototype | eTypeHasValue;
4171 case clang::Type::InjectedClassName:
4172 return 0;
4173
4174 case clang::Type::LValueReference:
4175 case clang::Type::RValueReference:
4176 if (pointee_or_element_clang_type)
4177 pointee_or_element_clang_type->SetCompilerType(
4178 getASTContext(),
4179 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4180 ->getPointeeType());
4181 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4182
4183 case clang::Type::MemberPointer:
4184 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4185
4186 case clang::Type::ObjCObjectPointer:
4187 if (pointee_or_element_clang_type)
4188 pointee_or_element_clang_type->SetCompilerType(
4189 getASTContext(), qual_type->getPointeeType());
4190 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4191 eTypeHasValue;
4192
4193 case clang::Type::ObjCObject:
4194 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4195 case clang::Type::ObjCInterface:
4196 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4197
4198 case clang::Type::Pointer:
4199 if (pointee_or_element_clang_type)
4200 pointee_or_element_clang_type->SetCompilerType(
4201 getASTContext(), qual_type->getPointeeType());
4202 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4203
4204 case clang::Type::Record:
4205 if (qual_type->getAsCXXRecordDecl())
4206 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4207 else
4208 return eTypeHasChildren | eTypeIsStructUnion;
4209 break;
4210 case clang::Type::SubstTemplateTypeParm:
4211 return eTypeIsTemplate;
4212 case clang::Type::TemplateTypeParm:
4213 return eTypeIsTemplate;
4214 case clang::Type::TemplateSpecialization:
4215 return eTypeIsTemplate;
4216
4217 case clang::Type::Typedef:
4218 return eTypeIsTypedef |
4219 CompilerType(getASTContext(),
4220 llvm::cast<clang::TypedefType>(qual_type)
4221 ->getDecl()
4222 ->getUnderlyingType())
4223 .GetTypeInfo(pointee_or_element_clang_type);
4224 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004225 return CompilerType(getASTContext(),
4226 llvm::cast<clang::TypeOfExprType>(qual_type)
4227 ->getUnderlyingExpr()
4228 ->getType())
4229 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004230 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004231 return CompilerType(
4232 getASTContext(),
4233 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4234 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004235 case clang::Type::UnresolvedUsing:
4236 return 0;
4237
4238 case clang::Type::ExtVector:
4239 case clang::Type::Vector: {
4240 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4241 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4242 qual_type->getCanonicalTypeInternal());
4243 if (vector_type) {
4244 if (vector_type->isIntegerType())
4245 vector_type_flags |= eTypeIsFloat;
4246 else if (vector_type->isFloatingType())
4247 vector_type_flags |= eTypeIsInteger;
4248 }
4249 return vector_type_flags;
4250 }
4251 default:
4252 return 0;
4253 }
4254 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004255}
4256
Greg Claytond8d4a572015-08-11 21:38:15 +00004257lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004258ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4259 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004260 return lldb::eLanguageTypeC;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004261
4262 // If the type is a reference, then resolve it to what it refers to first:
4263 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4264 if (qual_type->isAnyPointerType()) {
4265 if (qual_type->isObjCObjectPointerType())
4266 return lldb::eLanguageTypeObjC;
Adrian Prantl1db0f0c2019-05-02 23:07:23 +00004267 if (qual_type->getPointeeCXXRecordDecl())
4268 return lldb::eLanguageTypeC_plus_plus;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004269
4270 clang::QualType pointee_type(qual_type->getPointeeType());
Adrian Prantl1db0f0c2019-05-02 23:07:23 +00004271 if (pointee_type->getPointeeCXXRecordDecl())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004272 return lldb::eLanguageTypeC_plus_plus;
4273 if (pointee_type->isObjCObjectOrInterfaceType())
4274 return lldb::eLanguageTypeObjC;
4275 if (pointee_type->isObjCClassType())
4276 return lldb::eLanguageTypeObjC;
4277 if (pointee_type.getTypePtr() ==
4278 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4279 return lldb::eLanguageTypeObjC;
4280 } else {
4281 if (qual_type->isObjCObjectOrInterfaceType())
4282 return lldb::eLanguageTypeObjC;
4283 if (qual_type->getAsCXXRecordDecl())
4284 return lldb::eLanguageTypeC_plus_plus;
4285 switch (qual_type->getTypeClass()) {
4286 default:
4287 break;
4288 case clang::Type::Builtin:
4289 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4290 default:
4291 case clang::BuiltinType::Void:
4292 case clang::BuiltinType::Bool:
4293 case clang::BuiltinType::Char_U:
4294 case clang::BuiltinType::UChar:
4295 case clang::BuiltinType::WChar_U:
4296 case clang::BuiltinType::Char16:
4297 case clang::BuiltinType::Char32:
4298 case clang::BuiltinType::UShort:
4299 case clang::BuiltinType::UInt:
4300 case clang::BuiltinType::ULong:
4301 case clang::BuiltinType::ULongLong:
4302 case clang::BuiltinType::UInt128:
4303 case clang::BuiltinType::Char_S:
4304 case clang::BuiltinType::SChar:
4305 case clang::BuiltinType::WChar_S:
4306 case clang::BuiltinType::Short:
4307 case clang::BuiltinType::Int:
4308 case clang::BuiltinType::Long:
4309 case clang::BuiltinType::LongLong:
4310 case clang::BuiltinType::Int128:
4311 case clang::BuiltinType::Float:
4312 case clang::BuiltinType::Double:
4313 case clang::BuiltinType::LongDouble:
4314 break;
4315
4316 case clang::BuiltinType::NullPtr:
4317 return eLanguageTypeC_plus_plus;
4318
4319 case clang::BuiltinType::ObjCId:
4320 case clang::BuiltinType::ObjCClass:
4321 case clang::BuiltinType::ObjCSel:
4322 return eLanguageTypeObjC;
4323
4324 case clang::BuiltinType::Dependent:
4325 case clang::BuiltinType::Overload:
4326 case clang::BuiltinType::BoundMember:
4327 case clang::BuiltinType::UnknownAny:
4328 break;
4329 }
4330 break;
4331 case clang::Type::Typedef:
4332 return CompilerType(getASTContext(),
4333 llvm::cast<clang::TypedefType>(qual_type)
4334 ->getDecl()
4335 ->getUnderlyingType())
4336 .GetMinimumLanguage();
4337 }
4338 }
4339 return lldb::eLanguageTypeC;
Greg Claytond8d4a572015-08-11 21:38:15 +00004340}
4341
4342lldb::TypeClass
Kate Stoneb9c1b512016-09-06 20:57:50 +00004343ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4344 if (!type)
4345 return lldb::eTypeClassInvalid;
4346
4347 clang::QualType qual_type(GetQualType(type));
4348
4349 switch (qual_type->getTypeClass()) {
4350 case clang::Type::UnaryTransform:
4351 break;
4352 case clang::Type::FunctionNoProto:
4353 return lldb::eTypeClassFunction;
4354 case clang::Type::FunctionProto:
4355 return lldb::eTypeClassFunction;
4356 case clang::Type::IncompleteArray:
4357 return lldb::eTypeClassArray;
4358 case clang::Type::VariableArray:
4359 return lldb::eTypeClassArray;
4360 case clang::Type::ConstantArray:
4361 return lldb::eTypeClassArray;
4362 case clang::Type::DependentSizedArray:
4363 return lldb::eTypeClassArray;
4364 case clang::Type::DependentSizedExtVector:
4365 return lldb::eTypeClassVector;
Fangrui Song8f284882018-07-13 22:40:40 +00004366 case clang::Type::DependentVector:
4367 return lldb::eTypeClassVector;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004368 case clang::Type::ExtVector:
4369 return lldb::eTypeClassVector;
4370 case clang::Type::Vector:
4371 return lldb::eTypeClassVector;
4372 case clang::Type::Builtin:
4373 return lldb::eTypeClassBuiltin;
4374 case clang::Type::ObjCObjectPointer:
4375 return lldb::eTypeClassObjCObjectPointer;
4376 case clang::Type::BlockPointer:
4377 return lldb::eTypeClassBlockPointer;
4378 case clang::Type::Pointer:
4379 return lldb::eTypeClassPointer;
4380 case clang::Type::LValueReference:
4381 return lldb::eTypeClassReference;
4382 case clang::Type::RValueReference:
4383 return lldb::eTypeClassReference;
4384 case clang::Type::MemberPointer:
4385 return lldb::eTypeClassMemberPointer;
4386 case clang::Type::Complex:
4387 if (qual_type->isComplexType())
4388 return lldb::eTypeClassComplexFloat;
4389 else
4390 return lldb::eTypeClassComplexInteger;
4391 case clang::Type::ObjCObject:
4392 return lldb::eTypeClassObjCObject;
4393 case clang::Type::ObjCInterface:
4394 return lldb::eTypeClassObjCInterface;
4395 case clang::Type::Record: {
4396 const clang::RecordType *record_type =
4397 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4398 const clang::RecordDecl *record_decl = record_type->getDecl();
4399 if (record_decl->isUnion())
4400 return lldb::eTypeClassUnion;
4401 else if (record_decl->isStruct())
4402 return lldb::eTypeClassStruct;
4403 else
4404 return lldb::eTypeClassClass;
4405 } break;
4406 case clang::Type::Enum:
4407 return lldb::eTypeClassEnumeration;
4408 case clang::Type::Typedef:
4409 return lldb::eTypeClassTypedef;
4410 case clang::Type::UnresolvedUsing:
4411 break;
4412 case clang::Type::Paren:
4413 return CompilerType(getASTContext(),
4414 llvm::cast<clang::ParenType>(qual_type)->desugar())
4415 .GetTypeClass();
4416 case clang::Type::Auto:
4417 return CompilerType(
4418 getASTContext(),
4419 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4420 .GetTypeClass();
4421 case clang::Type::Elaborated:
4422 return CompilerType(
4423 getASTContext(),
4424 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4425 .GetTypeClass();
4426
4427 case clang::Type::Attributed:
4428 break;
4429 case clang::Type::TemplateTypeParm:
4430 break;
4431 case clang::Type::SubstTemplateTypeParm:
4432 break;
4433 case clang::Type::SubstTemplateTypeParmPack:
4434 break;
4435 case clang::Type::InjectedClassName:
4436 break;
4437 case clang::Type::DependentName:
4438 break;
4439 case clang::Type::DependentTemplateSpecialization:
4440 break;
4441 case clang::Type::PackExpansion:
4442 break;
4443
4444 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004445 return CompilerType(getASTContext(),
4446 llvm::cast<clang::TypeOfExprType>(qual_type)
4447 ->getUnderlyingExpr()
4448 ->getType())
4449 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004450 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004451 return CompilerType(
4452 getASTContext(),
4453 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4454 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004455 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004456 return CompilerType(
4457 getASTContext(),
4458 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4459 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004460 case clang::Type::TemplateSpecialization:
4461 break;
Pavel Labath4f19fce22017-02-17 13:39:50 +00004462 case clang::Type::DeducedTemplateSpecialization:
4463 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004464 case clang::Type::Atomic:
4465 break;
4466 case clang::Type::Pipe:
4467 break;
4468
4469 // pointer type decayed from an array or function type.
4470 case clang::Type::Decayed:
4471 break;
4472 case clang::Type::Adjusted:
4473 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00004474 case clang::Type::ObjCTypeParam:
4475 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00004476
4477 case clang::Type::DependentAddressSpace:
4478 break;
Krasimir Georgiev435e76a2019-05-07 13:59:30 +00004479 case clang::Type::MacroQualified:
4480 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004481 }
4482 // We don't know hot to display this type...
4483 return lldb::eTypeClassOther;
Greg Claytond8d4a572015-08-11 21:38:15 +00004484}
4485
Kate Stoneb9c1b512016-09-06 20:57:50 +00004486unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4487 if (type)
4488 return GetQualType(type).getQualifiers().getCVRQualifiers();
4489 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004490}
4491
Greg Claytond8d4a572015-08-11 21:38:15 +00004492// Creating related types
Greg Claytond8d4a572015-08-11 21:38:15 +00004493
Greg Claytona1e5dc82015-08-11 22:53:00 +00004494CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004495ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4496 uint64_t *stride) {
4497 if (type) {
4498 clang::QualType qual_type(GetCanonicalQualType(type));
4499
4500 const clang::Type *array_eletype =
4501 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4502
4503 if (!array_eletype)
4504 return CompilerType();
4505
4506 CompilerType element_type(getASTContext(),
4507 array_eletype->getCanonicalTypeUnqualified());
4508
4509 // TODO: the real stride will be >= this value.. find the real one!
4510 if (stride)
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00004511 if (Optional<uint64_t> size = element_type.GetByteSize(nullptr))
Adrian Prantld963a7c2019-01-15 18:07:52 +00004512 *stride = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004513
4514 return element_type;
4515 }
4516 return CompilerType();
4517}
4518
4519CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4520 uint64_t size) {
4521 if (type) {
4522 clang::QualType qual_type(GetCanonicalQualType(type));
4523 if (clang::ASTContext *ast_ctx = getASTContext()) {
4524 if (size != 0)
4525 return CompilerType(
4526 ast_ctx, ast_ctx->getConstantArrayType(
4527 qual_type, llvm::APInt(64, size),
4528 clang::ArrayType::ArraySizeModifier::Normal, 0));
4529 else
4530 return CompilerType(
4531 ast_ctx,
4532 ast_ctx->getIncompleteArrayType(
4533 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
Greg Claytond8d4a572015-08-11 21:38:15 +00004534 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004535 }
4536
4537 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004538}
4539
Greg Claytona1e5dc82015-08-11 22:53:00 +00004540CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004541ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4542 if (type)
4543 return CompilerType(getASTContext(), GetCanonicalQualType(type));
4544 return CompilerType();
4545}
4546
4547static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4548 clang::QualType qual_type) {
4549 if (qual_type->isPointerType())
4550 qual_type = ast->getPointerType(
4551 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4552 else
4553 qual_type = qual_type.getUnqualifiedType();
4554 qual_type.removeLocalConst();
4555 qual_type.removeLocalRestrict();
4556 qual_type.removeLocalVolatile();
4557 return qual_type;
Enrico Granata639392f2016-08-30 20:39:58 +00004558}
4559
4560CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004561ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4562 if (type)
4563 return CompilerType(
4564 getASTContext(),
4565 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4566 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004567}
4568
Kate Stoneb9c1b512016-09-06 20:57:50 +00004569int ClangASTContext::GetFunctionArgumentCount(
4570 lldb::opaque_compiler_type_t type) {
4571 if (type) {
4572 const clang::FunctionProtoType *func =
4573 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4574 if (func)
4575 return func->getNumParams();
4576 }
4577 return -1;
4578}
4579
4580CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4581 lldb::opaque_compiler_type_t type, size_t idx) {
4582 if (type) {
4583 const clang::FunctionProtoType *func =
4584 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4585 if (func) {
4586 const uint32_t num_args = func->getNumParams();
4587 if (idx < num_args)
4588 return CompilerType(getASTContext(), func->getParamType(idx));
4589 }
4590 }
4591 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004592}
4593
Greg Claytona1e5dc82015-08-11 22:53:00 +00004594CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004595ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4596 if (type) {
4597 clang::QualType qual_type(GetQualType(type));
4598 const clang::FunctionProtoType *func =
4599 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4600 if (func)
4601 return CompilerType(getASTContext(), func->getReturnType());
4602 }
4603 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004604}
4605
4606size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004607ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4608 size_t num_functions = 0;
4609 if (type) {
4610 clang::QualType qual_type(GetCanonicalQualType(type));
4611 switch (qual_type->getTypeClass()) {
4612 case clang::Type::Record:
4613 if (GetCompleteQualType(getASTContext(), qual_type)) {
4614 const clang::RecordType *record_type =
4615 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4616 const clang::RecordDecl *record_decl = record_type->getDecl();
4617 assert(record_decl);
4618 const clang::CXXRecordDecl *cxx_record_decl =
4619 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4620 if (cxx_record_decl)
4621 num_functions = std::distance(cxx_record_decl->method_begin(),
4622 cxx_record_decl->method_end());
4623 }
4624 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004625
Sean Callananf9c622a2016-09-30 18:44:43 +00004626 case clang::Type::ObjCObjectPointer: {
4627 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004628 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004629 const clang::ObjCInterfaceType *objc_interface_type =
4630 objc_class_type->getInterfaceType();
4631 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004632 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4633 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004634 clang::ObjCInterfaceDecl *class_interface_decl =
4635 objc_interface_type->getDecl();
4636 if (class_interface_decl) {
4637 num_functions = std::distance(class_interface_decl->meth_begin(),
4638 class_interface_decl->meth_end());
Greg Claytond8d4a572015-08-11 21:38:15 +00004639 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004640 }
4641 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004642 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004643
4644 case clang::Type::ObjCObject:
4645 case clang::Type::ObjCInterface:
4646 if (GetCompleteType(type)) {
4647 const clang::ObjCObjectType *objc_class_type =
4648 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4649 if (objc_class_type) {
4650 clang::ObjCInterfaceDecl *class_interface_decl =
4651 objc_class_type->getInterface();
4652 if (class_interface_decl)
4653 num_functions = std::distance(class_interface_decl->meth_begin(),
4654 class_interface_decl->meth_end());
4655 }
4656 }
4657 break;
4658
4659 case clang::Type::Typedef:
4660 return CompilerType(getASTContext(),
4661 llvm::cast<clang::TypedefType>(qual_type)
4662 ->getDecl()
4663 ->getUnderlyingType())
4664 .GetNumMemberFunctions();
4665
4666 case clang::Type::Auto:
4667 return CompilerType(
4668 getASTContext(),
4669 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4670 .GetNumMemberFunctions();
4671
4672 case clang::Type::Elaborated:
4673 return CompilerType(
4674 getASTContext(),
4675 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4676 .GetNumMemberFunctions();
4677
4678 case clang::Type::Paren:
4679 return CompilerType(getASTContext(),
4680 llvm::cast<clang::ParenType>(qual_type)->desugar())
4681 .GetNumMemberFunctions();
4682
4683 default:
4684 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004685 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004686 }
4687 return num_functions;
Greg Claytond8d4a572015-08-11 21:38:15 +00004688}
4689
4690TypeMemberFunctionImpl
Kate Stoneb9c1b512016-09-06 20:57:50 +00004691ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4692 size_t idx) {
4693 std::string name;
4694 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4695 CompilerType clang_type;
4696 CompilerDecl clang_decl;
4697 if (type) {
4698 clang::QualType qual_type(GetCanonicalQualType(type));
4699 switch (qual_type->getTypeClass()) {
4700 case clang::Type::Record:
4701 if (GetCompleteQualType(getASTContext(), qual_type)) {
4702 const clang::RecordType *record_type =
4703 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4704 const clang::RecordDecl *record_decl = record_type->getDecl();
4705 assert(record_decl);
4706 const clang::CXXRecordDecl *cxx_record_decl =
4707 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4708 if (cxx_record_decl) {
4709 auto method_iter = cxx_record_decl->method_begin();
4710 auto method_end = cxx_record_decl->method_end();
4711 if (idx <
4712 static_cast<size_t>(std::distance(method_iter, method_end))) {
4713 std::advance(method_iter, idx);
4714 clang::CXXMethodDecl *cxx_method_decl =
4715 method_iter->getCanonicalDecl();
4716 if (cxx_method_decl) {
4717 name = cxx_method_decl->getDeclName().getAsString();
4718 if (cxx_method_decl->isStatic())
4719 kind = lldb::eMemberFunctionKindStaticMethod;
4720 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4721 kind = lldb::eMemberFunctionKindConstructor;
4722 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4723 kind = lldb::eMemberFunctionKindDestructor;
4724 else
4725 kind = lldb::eMemberFunctionKindInstanceMethod;
4726 clang_type = CompilerType(
4727 this, cxx_method_decl->getType().getAsOpaquePtr());
4728 clang_decl = CompilerDecl(this, cxx_method_decl);
4729 }
4730 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004731 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004732 }
4733 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004734
Sean Callananf9c622a2016-09-30 18:44:43 +00004735 case clang::Type::ObjCObjectPointer: {
4736 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004737 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004738 const clang::ObjCInterfaceType *objc_interface_type =
4739 objc_class_type->getInterfaceType();
4740 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004741 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4742 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004743 clang::ObjCInterfaceDecl *class_interface_decl =
4744 objc_interface_type->getDecl();
4745 if (class_interface_decl) {
4746 auto method_iter = class_interface_decl->meth_begin();
4747 auto method_end = class_interface_decl->meth_end();
4748 if (idx <
4749 static_cast<size_t>(std::distance(method_iter, method_end))) {
4750 std::advance(method_iter, idx);
4751 clang::ObjCMethodDecl *objc_method_decl =
4752 method_iter->getCanonicalDecl();
4753 if (objc_method_decl) {
4754 clang_decl = CompilerDecl(this, objc_method_decl);
4755 name = objc_method_decl->getSelector().getAsString();
4756 if (objc_method_decl->isClassMethod())
4757 kind = lldb::eMemberFunctionKindStaticMethod;
4758 else
4759 kind = lldb::eMemberFunctionKindInstanceMethod;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004760 }
4761 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004762 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004763 }
4764 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004765 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004766
Kate Stoneb9c1b512016-09-06 20:57:50 +00004767 case clang::Type::ObjCObject:
4768 case clang::Type::ObjCInterface:
4769 if (GetCompleteType(type)) {
4770 const clang::ObjCObjectType *objc_class_type =
4771 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4772 if (objc_class_type) {
4773 clang::ObjCInterfaceDecl *class_interface_decl =
4774 objc_class_type->getInterface();
4775 if (class_interface_decl) {
4776 auto method_iter = class_interface_decl->meth_begin();
4777 auto method_end = class_interface_decl->meth_end();
4778 if (idx <
4779 static_cast<size_t>(std::distance(method_iter, method_end))) {
4780 std::advance(method_iter, idx);
4781 clang::ObjCMethodDecl *objc_method_decl =
4782 method_iter->getCanonicalDecl();
4783 if (objc_method_decl) {
4784 clang_decl = CompilerDecl(this, objc_method_decl);
4785 name = objc_method_decl->getSelector().getAsString();
4786 if (objc_method_decl->isClassMethod())
4787 kind = lldb::eMemberFunctionKindStaticMethod;
4788 else
4789 kind = lldb::eMemberFunctionKindInstanceMethod;
4790 }
4791 }
4792 }
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004793 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004794 }
4795 break;
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004796
Kate Stoneb9c1b512016-09-06 20:57:50 +00004797 case clang::Type::Typedef:
4798 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4799 ->getDecl()
4800 ->getUnderlyingType()
4801 .getAsOpaquePtr(),
4802 idx);
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004803
Kate Stoneb9c1b512016-09-06 20:57:50 +00004804 case clang::Type::Auto:
4805 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4806 ->getDeducedType()
4807 .getAsOpaquePtr(),
4808 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004809
Kate Stoneb9c1b512016-09-06 20:57:50 +00004810 case clang::Type::Elaborated:
4811 return GetMemberFunctionAtIndex(
4812 llvm::cast<clang::ElaboratedType>(qual_type)
4813 ->getNamedType()
4814 .getAsOpaquePtr(),
4815 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004816
Kate Stoneb9c1b512016-09-06 20:57:50 +00004817 case clang::Type::Paren:
4818 return GetMemberFunctionAtIndex(
4819 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4820 idx);
4821
4822 default:
4823 break;
Greg Clayton56939cb2015-09-17 22:23:34 +00004824 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004825 }
Greg Clayton56939cb2015-09-17 22:23:34 +00004826
Kate Stoneb9c1b512016-09-06 20:57:50 +00004827 if (kind == eMemberFunctionKindUnknown)
4828 return TypeMemberFunctionImpl();
4829 else
4830 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
Greg Clayton56939cb2015-09-17 22:23:34 +00004831}
4832
Greg Claytona1e5dc82015-08-11 22:53:00 +00004833CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004834ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4835 if (type)
4836 return CompilerType(getASTContext(),
4837 GetQualType(type).getNonReferenceType());
4838 return CompilerType();
4839}
4840
4841CompilerType ClangASTContext::CreateTypedefType(
4842 const CompilerType &type, const char *typedef_name,
4843 const CompilerDeclContext &compiler_decl_ctx) {
4844 if (type && typedef_name && typedef_name[0]) {
4845 ClangASTContext *ast =
4846 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4847 if (!ast)
4848 return CompilerType();
4849 clang::ASTContext *clang_ast = ast->getASTContext();
4850 clang::QualType qual_type(ClangUtil::GetQualType(type));
4851
4852 clang::DeclContext *decl_ctx =
4853 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4854 if (decl_ctx == nullptr)
4855 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4856
4857 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4858 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4859 &clang_ast->Idents.get(typedef_name),
4860 clang_ast->getTrivialTypeSourceInfo(qual_type));
4861
4862 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4863
Aleksandr Urakov709426b2018-09-10 08:08:43 +00004864 decl_ctx->addDecl(decl);
4865
Kate Stoneb9c1b512016-09-06 20:57:50 +00004866 // Get a uniqued clang::QualType for the typedef decl type
4867 return CompilerType(clang_ast, clang_ast->getTypedefType(decl));
4868 }
4869 return CompilerType();
4870}
4871
4872CompilerType
4873ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4874 if (type) {
4875 clang::QualType qual_type(GetQualType(type));
4876 return CompilerType(getASTContext(),
4877 qual_type.getTypePtr()->getPointeeType());
4878 }
4879 return CompilerType();
4880}
4881
4882CompilerType
4883ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4884 if (type) {
4885 clang::QualType qual_type(GetQualType(type));
4886
4887 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4888 switch (type_class) {
4889 case clang::Type::ObjCObject:
4890 case clang::Type::ObjCInterface:
4891 return CompilerType(getASTContext(),
4892 getASTContext()->getObjCObjectPointerType(qual_type));
4893
4894 default:
4895 return CompilerType(getASTContext(),
4896 getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004897 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004898 }
4899 return CompilerType();
4900}
4901
4902CompilerType
4903ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4904 if (type)
4905 return CompilerType(this, getASTContext()
4906 ->getLValueReferenceType(GetQualType(type))
4907 .getAsOpaquePtr());
4908 else
Greg Claytona1e5dc82015-08-11 22:53:00 +00004909 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004910}
4911
Kate Stoneb9c1b512016-09-06 20:57:50 +00004912CompilerType
4913ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4914 if (type)
4915 return CompilerType(this, getASTContext()
4916 ->getRValueReferenceType(GetQualType(type))
4917 .getAsOpaquePtr());
4918 else
4919 return CompilerType();
4920}
4921
4922CompilerType
4923ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4924 if (type) {
4925 clang::QualType result(GetQualType(type));
4926 result.addConst();
4927 return CompilerType(this, result.getAsOpaquePtr());
4928 }
4929 return CompilerType();
4930}
4931
4932CompilerType
4933ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4934 if (type) {
4935 clang::QualType result(GetQualType(type));
4936 result.addVolatile();
4937 return CompilerType(this, result.getAsOpaquePtr());
4938 }
4939 return CompilerType();
4940}
4941
4942CompilerType
4943ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4944 if (type) {
4945 clang::QualType result(GetQualType(type));
4946 result.addRestrict();
4947 return CompilerType(this, result.getAsOpaquePtr());
4948 }
4949 return CompilerType();
4950}
4951
4952CompilerType
4953ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4954 const char *typedef_name,
4955 const CompilerDeclContext &compiler_decl_ctx) {
4956 if (type) {
4957 clang::ASTContext *clang_ast = getASTContext();
4958 clang::QualType qual_type(GetQualType(type));
4959
4960 clang::DeclContext *decl_ctx =
4961 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4962 if (decl_ctx == nullptr)
4963 decl_ctx = getASTContext()->getTranslationUnitDecl();
4964
4965 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4966 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4967 &clang_ast->Idents.get(typedef_name),
4968 clang_ast->getTrivialTypeSourceInfo(qual_type));
4969
4970 clang::TagDecl *tdecl = nullptr;
4971 if (!qual_type.isNull()) {
4972 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4973 tdecl = rt->getDecl();
4974 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4975 tdecl = et->getDecl();
4976 }
4977
4978 // Check whether this declaration is an anonymous struct, union, or enum,
Adrian Prantl05097242018-04-30 16:49:04 +00004979 // hidden behind a typedef. If so, we try to check whether we have a
4980 // typedef tag to attach to the original record declaration
Kate Stoneb9c1b512016-09-06 20:57:50 +00004981 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4982 tdecl->setTypedefNameForAnonDecl(decl);
4983
4984 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4985
4986 // Get a uniqued clang::QualType for the typedef decl type
4987 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4988 }
4989 return CompilerType();
4990}
4991
4992CompilerType
4993ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4994 if (type) {
4995 const clang::TypedefType *typedef_type =
4996 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4997 if (typedef_type)
4998 return CompilerType(getASTContext(),
4999 typedef_type->getDecl()->getUnderlyingType());
5000 }
5001 return CompilerType();
5002}
Greg Claytond8d4a572015-08-11 21:38:15 +00005003
Greg Claytond8d4a572015-08-11 21:38:15 +00005004// Create related types using the current type's AST
Greg Claytond8d4a572015-08-11 21:38:15 +00005005
Kate Stoneb9c1b512016-09-06 20:57:50 +00005006CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
5007 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005008}
Greg Claytond8d4a572015-08-11 21:38:15 +00005009// Exploring the type
Greg Claytond8d4a572015-08-11 21:38:15 +00005010
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005011Optional<uint64_t>
5012ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
5013 ExecutionContextScope *exe_scope) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005014 if (GetCompleteType(type)) {
Greg Claytond8d4a572015-08-11 21:38:15 +00005015 clang::QualType qual_type(GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00005016 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005017 switch (type_class) {
5018 case clang::Type::Record:
5019 if (GetCompleteType(type))
5020 return getASTContext()->getTypeSize(qual_type);
5021 else
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005022 return None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005023 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00005024
Kate Stoneb9c1b512016-09-06 20:57:50 +00005025 case clang::Type::ObjCInterface:
5026 case clang::Type::ObjCObject: {
5027 ExecutionContext exe_ctx(exe_scope);
5028 Process *process = exe_ctx.GetProcessPtr();
5029 if (process) {
5030 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
5031 if (objc_runtime) {
5032 uint64_t bit_size = 0;
5033 if (objc_runtime->GetTypeBitSize(
5034 CompilerType(getASTContext(), qual_type), bit_size))
5035 return bit_size;
5036 }
5037 } else {
5038 static bool g_printed = false;
5039 if (!g_printed) {
5040 StreamString s;
5041 DumpTypeDescription(type, &s);
5042
5043 llvm::outs() << "warning: trying to determine the size of type ";
5044 llvm::outs() << s.GetString() << "\n";
5045 llvm::outs() << "without a valid ExecutionContext. this is not "
5046 "reliable. please file a bug against LLDB.\n";
5047 llvm::outs() << "backtrace:\n";
5048 llvm::sys::PrintStackTrace(llvm::outs());
5049 llvm::outs() << "\n";
5050 g_printed = true;
5051 }
5052 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005053 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005054 LLVM_FALLTHROUGH;
5055 default:
5056 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
5057 if (bit_size == 0) {
5058 if (qual_type->isIncompleteArrayType())
5059 return getASTContext()->getTypeSize(
5060 qual_type->getArrayElementTypeNoTypeQual()
5061 ->getCanonicalTypeUnqualified());
5062 }
5063 if (qual_type->isObjCObjectOrInterfaceType())
5064 return bit_size +
5065 getASTContext()->getTypeSize(
5066 getASTContext()->ObjCBuiltinClassTy);
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005067 // Function types actually have a size of 0, that's not an error.
5068 if (qual_type->isFunctionProtoType())
5069 return bit_size;
5070 if (bit_size)
5071 return bit_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005072 }
5073 }
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005074 return None;
Greg Claytond8d4a572015-08-11 21:38:15 +00005075}
5076
Kate Stoneb9c1b512016-09-06 20:57:50 +00005077size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
5078 if (GetCompleteType(type))
5079 return getASTContext()->getTypeAlign(GetQualType(type));
5080 return 0;
5081}
5082
5083lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
5084 uint64_t &count) {
5085 if (!type)
5086 return lldb::eEncodingInvalid;
5087
5088 count = 1;
5089 clang::QualType qual_type(GetCanonicalQualType(type));
5090
5091 switch (qual_type->getTypeClass()) {
5092 case clang::Type::UnaryTransform:
5093 break;
5094
5095 case clang::Type::FunctionNoProto:
5096 case clang::Type::FunctionProto:
5097 break;
5098
5099 case clang::Type::IncompleteArray:
5100 case clang::Type::VariableArray:
5101 break;
5102
5103 case clang::Type::ConstantArray:
5104 break;
5105
Fangrui Song8f284882018-07-13 22:40:40 +00005106 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005107 case clang::Type::ExtVector:
5108 case clang::Type::Vector:
5109 // TODO: Set this to more than one???
5110 break;
5111
5112 case clang::Type::Builtin:
5113 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5114 case clang::BuiltinType::Void:
5115 break;
5116
5117 case clang::BuiltinType::Bool:
5118 case clang::BuiltinType::Char_S:
5119 case clang::BuiltinType::SChar:
5120 case clang::BuiltinType::WChar_S:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005121 case clang::BuiltinType::Short:
5122 case clang::BuiltinType::Int:
5123 case clang::BuiltinType::Long:
5124 case clang::BuiltinType::LongLong:
5125 case clang::BuiltinType::Int128:
5126 return lldb::eEncodingSint;
5127
5128 case clang::BuiltinType::Char_U:
5129 case clang::BuiltinType::UChar:
5130 case clang::BuiltinType::WChar_U:
Richard Smith51d12d82018-05-02 02:43:22 +00005131 case clang::BuiltinType::Char8:
5132 case clang::BuiltinType::Char16:
5133 case clang::BuiltinType::Char32:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005134 case clang::BuiltinType::UShort:
5135 case clang::BuiltinType::UInt:
5136 case clang::BuiltinType::ULong:
5137 case clang::BuiltinType::ULongLong:
5138 case clang::BuiltinType::UInt128:
5139 return lldb::eEncodingUint;
5140
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005141 // Fixed point types. Note that they are currently ignored.
5142 case clang::BuiltinType::ShortAccum:
5143 case clang::BuiltinType::Accum:
5144 case clang::BuiltinType::LongAccum:
5145 case clang::BuiltinType::UShortAccum:
5146 case clang::BuiltinType::UAccum:
5147 case clang::BuiltinType::ULongAccum:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005148 case clang::BuiltinType::ShortFract:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005149 case clang::BuiltinType::Fract:
5150 case clang::BuiltinType::LongFract:
5151 case clang::BuiltinType::UShortFract:
5152 case clang::BuiltinType::UFract:
5153 case clang::BuiltinType::ULongFract:
5154 case clang::BuiltinType::SatShortAccum:
5155 case clang::BuiltinType::SatAccum:
5156 case clang::BuiltinType::SatLongAccum:
5157 case clang::BuiltinType::SatUShortAccum:
5158 case clang::BuiltinType::SatUAccum:
5159 case clang::BuiltinType::SatULongAccum:
5160 case clang::BuiltinType::SatShortFract:
5161 case clang::BuiltinType::SatFract:
5162 case clang::BuiltinType::SatLongFract:
5163 case clang::BuiltinType::SatUShortFract:
5164 case clang::BuiltinType::SatUFract:
5165 case clang::BuiltinType::SatULongFract:
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005166 break;
5167
Kate Stoneb9c1b512016-09-06 20:57:50 +00005168 case clang::BuiltinType::Half:
5169 case clang::BuiltinType::Float:
Ted Woodward4355c7c2017-09-20 19:16:53 +00005170 case clang::BuiltinType::Float16:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005171 case clang::BuiltinType::Float128:
5172 case clang::BuiltinType::Double:
5173 case clang::BuiltinType::LongDouble:
5174 return lldb::eEncodingIEEE754;
5175
5176 case clang::BuiltinType::ObjCClass:
5177 case clang::BuiltinType::ObjCId:
5178 case clang::BuiltinType::ObjCSel:
5179 return lldb::eEncodingUint;
5180
5181 case clang::BuiltinType::NullPtr:
5182 return lldb::eEncodingUint;
5183
5184 case clang::BuiltinType::Kind::ARCUnbridgedCast:
5185 case clang::BuiltinType::Kind::BoundMember:
5186 case clang::BuiltinType::Kind::BuiltinFn:
5187 case clang::BuiltinType::Kind::Dependent:
5188 case clang::BuiltinType::Kind::OCLClkEvent:
5189 case clang::BuiltinType::Kind::OCLEvent:
5190 case clang::BuiltinType::Kind::OCLImage1dRO:
5191 case clang::BuiltinType::Kind::OCLImage1dWO:
5192 case clang::BuiltinType::Kind::OCLImage1dRW:
5193 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
5194 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
5195 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
5196 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
5197 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
5198 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
5199 case clang::BuiltinType::Kind::OCLImage2dRO:
5200 case clang::BuiltinType::Kind::OCLImage2dWO:
5201 case clang::BuiltinType::Kind::OCLImage2dRW:
5202 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
5203 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
5204 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
5205 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
5206 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
5207 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
5208 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5209 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5210 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5211 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5212 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5213 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5214 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5215 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5216 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5217 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5218 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5219 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5220 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5221 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5222 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5223 case clang::BuiltinType::Kind::OCLImage3dRO:
5224 case clang::BuiltinType::Kind::OCLImage3dWO:
5225 case clang::BuiltinType::Kind::OCLImage3dRW:
5226 case clang::BuiltinType::Kind::OCLQueue:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005227 case clang::BuiltinType::Kind::OCLReserveID:
5228 case clang::BuiltinType::Kind::OCLSampler:
5229 case clang::BuiltinType::Kind::OMPArraySection:
5230 case clang::BuiltinType::Kind::Overload:
5231 case clang::BuiltinType::Kind::PseudoObject:
5232 case clang::BuiltinType::Kind::UnknownAny:
5233 break;
Jorge Gorbe Moyaa6e6c182018-11-08 22:04:58 +00005234
5235 case clang::BuiltinType::OCLIntelSubgroupAVCMcePayload:
5236 case clang::BuiltinType::OCLIntelSubgroupAVCImePayload:
5237 case clang::BuiltinType::OCLIntelSubgroupAVCRefPayload:
5238 case clang::BuiltinType::OCLIntelSubgroupAVCSicPayload:
5239 case clang::BuiltinType::OCLIntelSubgroupAVCMceResult:
5240 case clang::BuiltinType::OCLIntelSubgroupAVCImeResult:
5241 case clang::BuiltinType::OCLIntelSubgroupAVCRefResult:
5242 case clang::BuiltinType::OCLIntelSubgroupAVCSicResult:
5243 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultSingleRefStreamout:
5244 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultDualRefStreamout:
5245 case clang::BuiltinType::OCLIntelSubgroupAVCImeSingleRefStreamin:
5246 case clang::BuiltinType::OCLIntelSubgroupAVCImeDualRefStreamin:
5247 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005248 }
5249 break;
Adrian Prantl05097242018-04-30 16:49:04 +00005250 // All pointer types are represented as unsigned integer encodings. We may
5251 // nee to add a eEncodingPointer if we ever need to know the difference
Kate Stoneb9c1b512016-09-06 20:57:50 +00005252 case clang::Type::ObjCObjectPointer:
5253 case clang::Type::BlockPointer:
5254 case clang::Type::Pointer:
5255 case clang::Type::LValueReference:
5256 case clang::Type::RValueReference:
5257 case clang::Type::MemberPointer:
5258 return lldb::eEncodingUint;
5259 case clang::Type::Complex: {
5260 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5261 if (qual_type->isComplexType())
5262 encoding = lldb::eEncodingIEEE754;
5263 else {
5264 const clang::ComplexType *complex_type =
5265 qual_type->getAsComplexIntegerType();
5266 if (complex_type)
5267 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5268 .GetEncoding(count);
5269 else
5270 encoding = lldb::eEncodingSint;
5271 }
5272 count = 2;
5273 return encoding;
5274 }
5275
5276 case clang::Type::ObjCInterface:
5277 break;
5278 case clang::Type::Record:
5279 break;
5280 case clang::Type::Enum:
5281 return lldb::eEncodingSint;
5282 case clang::Type::Typedef:
5283 return CompilerType(getASTContext(),
5284 llvm::cast<clang::TypedefType>(qual_type)
5285 ->getDecl()
5286 ->getUnderlyingType())
5287 .GetEncoding(count);
5288
5289 case clang::Type::Auto:
5290 return CompilerType(
5291 getASTContext(),
5292 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5293 .GetEncoding(count);
5294
5295 case clang::Type::Elaborated:
5296 return CompilerType(
5297 getASTContext(),
5298 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5299 .GetEncoding(count);
5300
5301 case clang::Type::Paren:
5302 return CompilerType(getASTContext(),
5303 llvm::cast<clang::ParenType>(qual_type)->desugar())
5304 .GetEncoding(count);
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005305 case clang::Type::TypeOfExpr:
5306 return CompilerType(getASTContext(),
5307 llvm::cast<clang::TypeOfExprType>(qual_type)
5308 ->getUnderlyingExpr()
5309 ->getType())
5310 .GetEncoding(count);
5311 case clang::Type::TypeOf:
5312 return CompilerType(
5313 getASTContext(),
5314 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5315 .GetEncoding(count);
5316 case clang::Type::Decltype:
5317 return CompilerType(
5318 getASTContext(),
5319 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5320 .GetEncoding(count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005321 case clang::Type::DependentSizedArray:
5322 case clang::Type::DependentSizedExtVector:
5323 case clang::Type::UnresolvedUsing:
5324 case clang::Type::Attributed:
5325 case clang::Type::TemplateTypeParm:
5326 case clang::Type::SubstTemplateTypeParm:
5327 case clang::Type::SubstTemplateTypeParmPack:
5328 case clang::Type::InjectedClassName:
5329 case clang::Type::DependentName:
5330 case clang::Type::DependentTemplateSpecialization:
5331 case clang::Type::PackExpansion:
5332 case clang::Type::ObjCObject:
5333
Kate Stoneb9c1b512016-09-06 20:57:50 +00005334 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005335 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005336 case clang::Type::Atomic:
5337 case clang::Type::Adjusted:
5338 case clang::Type::Pipe:
5339 break;
5340
5341 // pointer type decayed from an array or function type.
5342 case clang::Type::Decayed:
5343 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005344 case clang::Type::ObjCTypeParam:
5345 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005346
5347 case clang::Type::DependentAddressSpace:
5348 break;
Krasimir Georgiev435e76a2019-05-07 13:59:30 +00005349 case clang::Type::MacroQualified:
5350 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005351 }
5352 count = 0;
5353 return lldb::eEncodingInvalid;
5354}
5355
5356lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5357 if (!type)
5358 return lldb::eFormatDefault;
5359
5360 clang::QualType qual_type(GetCanonicalQualType(type));
5361
5362 switch (qual_type->getTypeClass()) {
5363 case clang::Type::UnaryTransform:
5364 break;
5365
5366 case clang::Type::FunctionNoProto:
5367 case clang::Type::FunctionProto:
5368 break;
5369
5370 case clang::Type::IncompleteArray:
5371 case clang::Type::VariableArray:
5372 break;
5373
5374 case clang::Type::ConstantArray:
5375 return lldb::eFormatVoid; // no value
5376
Fangrui Song8f284882018-07-13 22:40:40 +00005377 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005378 case clang::Type::ExtVector:
5379 case clang::Type::Vector:
5380 break;
5381
5382 case clang::Type::Builtin:
5383 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5384 // default: assert(0 && "Unknown builtin type!");
5385 case clang::BuiltinType::UnknownAny:
5386 case clang::BuiltinType::Void:
5387 case clang::BuiltinType::BoundMember:
5388 break;
5389
5390 case clang::BuiltinType::Bool:
5391 return lldb::eFormatBoolean;
5392 case clang::BuiltinType::Char_S:
5393 case clang::BuiltinType::SChar:
5394 case clang::BuiltinType::WChar_S:
5395 case clang::BuiltinType::Char_U:
5396 case clang::BuiltinType::UChar:
5397 case clang::BuiltinType::WChar_U:
5398 return lldb::eFormatChar;
5399 case clang::BuiltinType::Char16:
5400 return lldb::eFormatUnicode16;
5401 case clang::BuiltinType::Char32:
5402 return lldb::eFormatUnicode32;
5403 case clang::BuiltinType::UShort:
5404 return lldb::eFormatUnsigned;
5405 case clang::BuiltinType::Short:
5406 return lldb::eFormatDecimal;
5407 case clang::BuiltinType::UInt:
5408 return lldb::eFormatUnsigned;
5409 case clang::BuiltinType::Int:
5410 return lldb::eFormatDecimal;
5411 case clang::BuiltinType::ULong:
5412 return lldb::eFormatUnsigned;
5413 case clang::BuiltinType::Long:
5414 return lldb::eFormatDecimal;
5415 case clang::BuiltinType::ULongLong:
5416 return lldb::eFormatUnsigned;
5417 case clang::BuiltinType::LongLong:
5418 return lldb::eFormatDecimal;
5419 case clang::BuiltinType::UInt128:
5420 return lldb::eFormatUnsigned;
5421 case clang::BuiltinType::Int128:
5422 return lldb::eFormatDecimal;
5423 case clang::BuiltinType::Half:
5424 case clang::BuiltinType::Float:
5425 case clang::BuiltinType::Double:
5426 case clang::BuiltinType::LongDouble:
5427 return lldb::eFormatFloat;
5428 default:
5429 return lldb::eFormatHex;
5430 }
5431 break;
5432 case clang::Type::ObjCObjectPointer:
5433 return lldb::eFormatHex;
5434 case clang::Type::BlockPointer:
5435 return lldb::eFormatHex;
5436 case clang::Type::Pointer:
5437 return lldb::eFormatHex;
5438 case clang::Type::LValueReference:
5439 case clang::Type::RValueReference:
5440 return lldb::eFormatHex;
5441 case clang::Type::MemberPointer:
5442 break;
5443 case clang::Type::Complex: {
5444 if (qual_type->isComplexType())
5445 return lldb::eFormatComplex;
5446 else
5447 return lldb::eFormatComplexInteger;
5448 }
5449 case clang::Type::ObjCInterface:
5450 break;
5451 case clang::Type::Record:
5452 break;
5453 case clang::Type::Enum:
5454 return lldb::eFormatEnum;
5455 case clang::Type::Typedef:
5456 return CompilerType(getASTContext(),
5457 llvm::cast<clang::TypedefType>(qual_type)
5458 ->getDecl()
5459 ->getUnderlyingType())
5460 .GetFormat();
5461 case clang::Type::Auto:
5462 return CompilerType(getASTContext(),
5463 llvm::cast<clang::AutoType>(qual_type)->desugar())
5464 .GetFormat();
5465 case clang::Type::Paren:
5466 return CompilerType(getASTContext(),
5467 llvm::cast<clang::ParenType>(qual_type)->desugar())
5468 .GetFormat();
5469 case clang::Type::Elaborated:
5470 return CompilerType(
5471 getASTContext(),
5472 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5473 .GetFormat();
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005474 case clang::Type::TypeOfExpr:
5475 return CompilerType(getASTContext(),
5476 llvm::cast<clang::TypeOfExprType>(qual_type)
5477 ->getUnderlyingExpr()
5478 ->getType())
5479 .GetFormat();
5480 case clang::Type::TypeOf:
5481 return CompilerType(
5482 getASTContext(),
5483 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5484 .GetFormat();
5485 case clang::Type::Decltype:
5486 return CompilerType(
5487 getASTContext(),
5488 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5489 .GetFormat();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005490 case clang::Type::DependentSizedArray:
5491 case clang::Type::DependentSizedExtVector:
5492 case clang::Type::UnresolvedUsing:
5493 case clang::Type::Attributed:
5494 case clang::Type::TemplateTypeParm:
5495 case clang::Type::SubstTemplateTypeParm:
5496 case clang::Type::SubstTemplateTypeParmPack:
5497 case clang::Type::InjectedClassName:
5498 case clang::Type::DependentName:
5499 case clang::Type::DependentTemplateSpecialization:
5500 case clang::Type::PackExpansion:
5501 case clang::Type::ObjCObject:
5502
Kate Stoneb9c1b512016-09-06 20:57:50 +00005503 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005504 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005505 case clang::Type::Atomic:
5506 case clang::Type::Adjusted:
5507 case clang::Type::Pipe:
5508 break;
5509
5510 // pointer type decayed from an array or function type.
5511 case clang::Type::Decayed:
5512 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005513 case clang::Type::ObjCTypeParam:
5514 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005515
5516 case clang::Type::DependentAddressSpace:
5517 break;
Krasimir Georgiev435e76a2019-05-07 13:59:30 +00005518 case clang::Type::MacroQualified:
5519 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005520 }
5521 // We don't know hot to display this type...
5522 return lldb::eFormatBytes;
5523}
5524
5525static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5526 bool check_superclass) {
5527 while (class_interface_decl) {
5528 if (class_interface_decl->ivar_size() > 0)
5529 return true;
5530
5531 if (check_superclass)
5532 class_interface_decl = class_interface_decl->getSuperClass();
5533 else
5534 break;
5535 }
5536 return false;
5537}
5538
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00005539static Optional<SymbolFile::ArrayInfo>
Adrian Prantleca07c52018-11-05 20:49:07 +00005540GetDynamicArrayInfo(ClangASTContext &ast, SymbolFile *sym_file,
5541 clang::QualType qual_type,
5542 const ExecutionContext *exe_ctx) {
5543 if (qual_type->isIncompleteArrayType())
5544 if (auto *metadata = ast.GetMetadata(qual_type.getAsOpaquePtr()))
Pavel Labathffec31e2018-12-28 13:34:44 +00005545 return sym_file->GetDynamicArrayInfoForUID(metadata->GetUserID(),
5546 exe_ctx);
Adrian Prantleca07c52018-11-05 20:49:07 +00005547 return llvm::None;
5548}
5549
Kate Stoneb9c1b512016-09-06 20:57:50 +00005550uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
Adrian Prantleca07c52018-11-05 20:49:07 +00005551 bool omit_empty_base_classes,
5552 const ExecutionContext *exe_ctx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005553 if (!type)
5554 return 0;
5555
5556 uint32_t num_children = 0;
5557 clang::QualType qual_type(GetQualType(type));
5558 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5559 switch (type_class) {
5560 case clang::Type::Builtin:
5561 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5562 case clang::BuiltinType::ObjCId: // child is Class
5563 case clang::BuiltinType::ObjCClass: // child is Class
5564 num_children = 1;
5565 break;
5566
5567 default:
5568 break;
5569 }
5570 break;
5571
5572 case clang::Type::Complex:
5573 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005574 case clang::Type::Record:
5575 if (GetCompleteQualType(getASTContext(), qual_type)) {
5576 const clang::RecordType *record_type =
5577 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5578 const clang::RecordDecl *record_decl = record_type->getDecl();
5579 assert(record_decl);
5580 const clang::CXXRecordDecl *cxx_record_decl =
5581 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5582 if (cxx_record_decl) {
5583 if (omit_empty_base_classes) {
Adrian Prantl05097242018-04-30 16:49:04 +00005584 // Check each base classes to see if it or any of its base classes
5585 // contain any fields. This can help limit the noise in variable
5586 // views by not having to show base classes that contain no members.
Kate Stoneb9c1b512016-09-06 20:57:50 +00005587 clang::CXXRecordDecl::base_class_const_iterator base_class,
5588 base_class_end;
5589 for (base_class = cxx_record_decl->bases_begin(),
5590 base_class_end = cxx_record_decl->bases_end();
5591 base_class != base_class_end; ++base_class) {
5592 const clang::CXXRecordDecl *base_class_decl =
5593 llvm::cast<clang::CXXRecordDecl>(
5594 base_class->getType()
5595 ->getAs<clang::RecordType>()
5596 ->getDecl());
5597
5598 // Skip empty base classes
Jonas Devliegherea6682a42018-12-15 00:15:33 +00005599 if (!ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00005600 continue;
5601
5602 num_children++;
5603 }
5604 } else {
5605 // Include all base classes
5606 num_children += cxx_record_decl->getNumBases();
5607 }
5608 }
5609 clang::RecordDecl::field_iterator field, field_end;
5610 for (field = record_decl->field_begin(),
5611 field_end = record_decl->field_end();
5612 field != field_end; ++field)
5613 ++num_children;
5614 }
5615 break;
5616
5617 case clang::Type::ObjCObject:
5618 case clang::Type::ObjCInterface:
5619 if (GetCompleteQualType(getASTContext(), qual_type)) {
5620 const clang::ObjCObjectType *objc_class_type =
5621 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5622 assert(objc_class_type);
5623 if (objc_class_type) {
5624 clang::ObjCInterfaceDecl *class_interface_decl =
5625 objc_class_type->getInterface();
5626
5627 if (class_interface_decl) {
5628
5629 clang::ObjCInterfaceDecl *superclass_interface_decl =
5630 class_interface_decl->getSuperClass();
5631 if (superclass_interface_decl) {
5632 if (omit_empty_base_classes) {
5633 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5634 ++num_children;
5635 } else
5636 ++num_children;
5637 }
5638
5639 num_children += class_interface_decl->ivar_size();
5640 }
5641 }
5642 }
5643 break;
5644
5645 case clang::Type::ObjCObjectPointer: {
5646 const clang::ObjCObjectPointerType *pointer_type =
5647 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5648 clang::QualType pointee_type = pointer_type->getPointeeType();
5649 uint32_t num_pointee_children =
5650 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005651 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005652 // If this type points to a simple type, then it has 1 child
5653 if (num_pointee_children == 0)
5654 num_children = 1;
5655 else
5656 num_children = num_pointee_children;
5657 } break;
5658
5659 case clang::Type::Vector:
5660 case clang::Type::ExtVector:
5661 num_children =
5662 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5663 break;
5664
5665 case clang::Type::ConstantArray:
5666 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5667 ->getSize()
5668 .getLimitedValue();
5669 break;
Adrian Prantleca07c52018-11-05 20:49:07 +00005670 case clang::Type::IncompleteArray:
5671 if (auto array_info =
5672 GetDynamicArrayInfo(*this, GetSymbolFile(), qual_type, exe_ctx))
5673 // Only 1-dimensional arrays are supported.
5674 num_children = array_info->element_orders.size()
5675 ? array_info->element_orders.back()
5676 : 0;
5677 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005678
5679 case clang::Type::Pointer: {
5680 const clang::PointerType *pointer_type =
5681 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5682 clang::QualType pointee_type(pointer_type->getPointeeType());
5683 uint32_t num_pointee_children =
5684 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005685 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005686 if (num_pointee_children == 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00005687 // We have a pointer to a pointee type that claims it has no children. We
5688 // will want to look at
Kate Stoneb9c1b512016-09-06 20:57:50 +00005689 num_children = GetNumPointeeChildren(pointee_type);
5690 } else
5691 num_children = num_pointee_children;
5692 } break;
5693
5694 case clang::Type::LValueReference:
5695 case clang::Type::RValueReference: {
5696 const clang::ReferenceType *reference_type =
5697 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5698 clang::QualType pointee_type = reference_type->getPointeeType();
5699 uint32_t num_pointee_children =
5700 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005701 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005702 // If this type points to a simple type, then it has 1 child
5703 if (num_pointee_children == 0)
5704 num_children = 1;
5705 else
5706 num_children = num_pointee_children;
5707 } break;
5708
5709 case clang::Type::Typedef:
5710 num_children =
5711 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5712 ->getDecl()
5713 ->getUnderlyingType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005714 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005715 break;
5716
5717 case clang::Type::Auto:
5718 num_children =
5719 CompilerType(getASTContext(),
5720 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005721 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005722 break;
5723
5724 case clang::Type::Elaborated:
5725 num_children =
5726 CompilerType(
5727 getASTContext(),
5728 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005729 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005730 break;
5731
5732 case clang::Type::Paren:
5733 num_children =
5734 CompilerType(getASTContext(),
5735 llvm::cast<clang::ParenType>(qual_type)->desugar())
Adrian Prantleca07c52018-11-05 20:49:07 +00005736 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005737 break;
5738 default:
5739 break;
5740 }
5741 return num_children;
5742}
5743
Adrian Prantl0e4c4822019-03-06 21:22:25 +00005744CompilerType ClangASTContext::GetBuiltinTypeByName(ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005745 return GetBasicType(GetBasicTypeEnumeration(name));
Greg Clayton56939cb2015-09-17 22:23:34 +00005746}
5747
Greg Claytond8d4a572015-08-11 21:38:15 +00005748lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00005749ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5750 if (type) {
5751 clang::QualType qual_type(GetQualType(type));
5752 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5753 if (type_class == clang::Type::Builtin) {
5754 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5755 case clang::BuiltinType::Void:
5756 return eBasicTypeVoid;
5757 case clang::BuiltinType::Bool:
5758 return eBasicTypeBool;
5759 case clang::BuiltinType::Char_S:
5760 return eBasicTypeSignedChar;
5761 case clang::BuiltinType::Char_U:
5762 return eBasicTypeUnsignedChar;
5763 case clang::BuiltinType::Char16:
5764 return eBasicTypeChar16;
5765 case clang::BuiltinType::Char32:
5766 return eBasicTypeChar32;
5767 case clang::BuiltinType::UChar:
5768 return eBasicTypeUnsignedChar;
5769 case clang::BuiltinType::SChar:
5770 return eBasicTypeSignedChar;
5771 case clang::BuiltinType::WChar_S:
5772 return eBasicTypeSignedWChar;
5773 case clang::BuiltinType::WChar_U:
5774 return eBasicTypeUnsignedWChar;
5775 case clang::BuiltinType::Short:
5776 return eBasicTypeShort;
5777 case clang::BuiltinType::UShort:
5778 return eBasicTypeUnsignedShort;
5779 case clang::BuiltinType::Int:
5780 return eBasicTypeInt;
5781 case clang::BuiltinType::UInt:
5782 return eBasicTypeUnsignedInt;
5783 case clang::BuiltinType::Long:
5784 return eBasicTypeLong;
5785 case clang::BuiltinType::ULong:
5786 return eBasicTypeUnsignedLong;
5787 case clang::BuiltinType::LongLong:
5788 return eBasicTypeLongLong;
5789 case clang::BuiltinType::ULongLong:
5790 return eBasicTypeUnsignedLongLong;
5791 case clang::BuiltinType::Int128:
5792 return eBasicTypeInt128;
5793 case clang::BuiltinType::UInt128:
5794 return eBasicTypeUnsignedInt128;
5795
5796 case clang::BuiltinType::Half:
5797 return eBasicTypeHalf;
5798 case clang::BuiltinType::Float:
5799 return eBasicTypeFloat;
5800 case clang::BuiltinType::Double:
5801 return eBasicTypeDouble;
5802 case clang::BuiltinType::LongDouble:
5803 return eBasicTypeLongDouble;
5804
5805 case clang::BuiltinType::NullPtr:
5806 return eBasicTypeNullPtr;
5807 case clang::BuiltinType::ObjCId:
5808 return eBasicTypeObjCID;
5809 case clang::BuiltinType::ObjCClass:
5810 return eBasicTypeObjCClass;
5811 case clang::BuiltinType::ObjCSel:
5812 return eBasicTypeObjCSel;
5813 default:
5814 return eBasicTypeOther;
5815 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005816 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005817 }
5818 return eBasicTypeInvalid;
Greg Claytond8d4a572015-08-11 21:38:15 +00005819}
5820
Kate Stoneb9c1b512016-09-06 20:57:50 +00005821void ClangASTContext::ForEachEnumerator(
5822 lldb::opaque_compiler_type_t type,
5823 std::function<bool(const CompilerType &integer_type,
Adrian Prantl0e4c4822019-03-06 21:22:25 +00005824 ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00005825 const llvm::APSInt &value)> const &callback) {
5826 const clang::EnumType *enum_type =
5827 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5828 if (enum_type) {
5829 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5830 if (enum_decl) {
5831 CompilerType integer_type(this,
5832 enum_decl->getIntegerType().getAsOpaquePtr());
Greg Clayton99558cc42015-08-24 23:46:31 +00005833
Kate Stoneb9c1b512016-09-06 20:57:50 +00005834 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5835 for (enum_pos = enum_decl->enumerator_begin(),
5836 enum_end_pos = enum_decl->enumerator_end();
5837 enum_pos != enum_end_pos; ++enum_pos) {
5838 ConstString name(enum_pos->getNameAsString().c_str());
5839 if (!callback(integer_type, name, enum_pos->getInitVal()))
5840 break;
5841 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005842 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005843 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005844}
5845
Greg Claytond8d4a572015-08-11 21:38:15 +00005846#pragma mark Aggregate Types
5847
Kate Stoneb9c1b512016-09-06 20:57:50 +00005848uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5849 if (!type)
5850 return 0;
Enrico Granata36f51e42015-12-18 22:41:25 +00005851
Kate Stoneb9c1b512016-09-06 20:57:50 +00005852 uint32_t count = 0;
5853 clang::QualType qual_type(GetCanonicalQualType(type));
5854 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5855 switch (type_class) {
5856 case clang::Type::Record:
5857 if (GetCompleteType(type)) {
5858 const clang::RecordType *record_type =
5859 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5860 if (record_type) {
5861 clang::RecordDecl *record_decl = record_type->getDecl();
5862 if (record_decl) {
5863 uint32_t field_idx = 0;
5864 clang::RecordDecl::field_iterator field, field_end;
5865 for (field = record_decl->field_begin(),
5866 field_end = record_decl->field_end();
5867 field != field_end; ++field)
5868 ++field_idx;
5869 count = field_idx;
5870 }
5871 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005872 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005873 break;
5874
5875 case clang::Type::Typedef:
5876 count =
5877 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5878 ->getDecl()
5879 ->getUnderlyingType())
5880 .GetNumFields();
5881 break;
5882
5883 case clang::Type::Auto:
5884 count =
5885 CompilerType(getASTContext(),
5886 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5887 .GetNumFields();
5888 break;
5889
5890 case clang::Type::Elaborated:
5891 count = CompilerType(
5892 getASTContext(),
5893 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5894 .GetNumFields();
5895 break;
5896
5897 case clang::Type::Paren:
5898 count = CompilerType(getASTContext(),
5899 llvm::cast<clang::ParenType>(qual_type)->desugar())
5900 .GetNumFields();
5901 break;
5902
Sean Callananf9c622a2016-09-30 18:44:43 +00005903 case clang::Type::ObjCObjectPointer: {
5904 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005905 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005906 const clang::ObjCInterfaceType *objc_interface_type =
5907 objc_class_type->getInterfaceType();
5908 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005909 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5910 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005911 clang::ObjCInterfaceDecl *class_interface_decl =
5912 objc_interface_type->getDecl();
5913 if (class_interface_decl) {
5914 count = class_interface_decl->ivar_size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005915 }
5916 }
5917 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005918 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005919
5920 case clang::Type::ObjCObject:
5921 case clang::Type::ObjCInterface:
5922 if (GetCompleteType(type)) {
5923 const clang::ObjCObjectType *objc_class_type =
5924 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5925 if (objc_class_type) {
5926 clang::ObjCInterfaceDecl *class_interface_decl =
5927 objc_class_type->getInterface();
5928
5929 if (class_interface_decl)
5930 count = class_interface_decl->ivar_size();
5931 }
5932 }
5933 break;
5934
5935 default:
5936 break;
5937 }
5938 return count;
Greg Claytond8d4a572015-08-11 21:38:15 +00005939}
5940
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005941static lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005942GetObjCFieldAtIndex(clang::ASTContext *ast,
5943 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5944 std::string &name, uint64_t *bit_offset_ptr,
5945 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5946 if (class_interface_decl) {
5947 if (idx < (class_interface_decl->ivar_size())) {
5948 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5949 ivar_end = class_interface_decl->ivar_end();
5950 uint32_t ivar_idx = 0;
5951
5952 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5953 ++ivar_pos, ++ivar_idx) {
5954 if (ivar_idx == idx) {
5955 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5956
5957 clang::QualType ivar_qual_type(ivar_decl->getType());
5958
5959 name.assign(ivar_decl->getNameAsString());
5960
5961 if (bit_offset_ptr) {
5962 const clang::ASTRecordLayout &interface_layout =
5963 ast->getASTObjCInterfaceLayout(class_interface_decl);
5964 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5965 }
5966
5967 const bool is_bitfield = ivar_pos->isBitField();
5968
5969 if (bitfield_bit_size_ptr) {
5970 *bitfield_bit_size_ptr = 0;
5971
5972 if (is_bitfield && ast) {
5973 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
Hans Wennborg30ce9622018-11-28 14:30:18 +00005974 clang::Expr::EvalResult result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005975 if (bitfield_bit_size_expr &&
Hans Wennborg30ce9622018-11-28 14:30:18 +00005976 bitfield_bit_size_expr->EvaluateAsInt(result, *ast)) {
5977 llvm::APSInt bitfield_apsint = result.Val.getInt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005978 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5979 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005980 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005981 }
5982 if (is_bitfield_ptr)
5983 *is_bitfield_ptr = is_bitfield;
5984
5985 return ivar_qual_type.getAsOpaquePtr();
Greg Claytond8d4a572015-08-11 21:38:15 +00005986 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005987 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005988 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005989 }
5990 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00005991}
5992
Kate Stoneb9c1b512016-09-06 20:57:50 +00005993CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5994 size_t idx, std::string &name,
5995 uint64_t *bit_offset_ptr,
5996 uint32_t *bitfield_bit_size_ptr,
5997 bool *is_bitfield_ptr) {
5998 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005999 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006000
6001 clang::QualType qual_type(GetCanonicalQualType(type));
6002 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6003 switch (type_class) {
6004 case clang::Type::Record:
6005 if (GetCompleteType(type)) {
6006 const clang::RecordType *record_type =
6007 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6008 const clang::RecordDecl *record_decl = record_type->getDecl();
6009 uint32_t field_idx = 0;
6010 clang::RecordDecl::field_iterator field, field_end;
6011 for (field = record_decl->field_begin(),
6012 field_end = record_decl->field_end();
6013 field != field_end; ++field, ++field_idx) {
6014 if (idx == field_idx) {
6015 // Print the member type if requested
6016 // Print the member name and equal sign
6017 name.assign(field->getNameAsString());
6018
6019 // Figure out the type byte size (field_type_info.first) and
6020 // alignment (field_type_info.second) from the AST context.
6021 if (bit_offset_ptr) {
6022 const clang::ASTRecordLayout &record_layout =
6023 getASTContext()->getASTRecordLayout(record_decl);
6024 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
6025 }
6026
6027 const bool is_bitfield = field->isBitField();
6028
6029 if (bitfield_bit_size_ptr) {
6030 *bitfield_bit_size_ptr = 0;
6031
6032 if (is_bitfield) {
6033 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
Hans Wennborg30ce9622018-11-28 14:30:18 +00006034 clang::Expr::EvalResult result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006035 if (bitfield_bit_size_expr &&
Hans Wennborg30ce9622018-11-28 14:30:18 +00006036 bitfield_bit_size_expr->EvaluateAsInt(result,
Kate Stoneb9c1b512016-09-06 20:57:50 +00006037 *getASTContext())) {
Hans Wennborg30ce9622018-11-28 14:30:18 +00006038 llvm::APSInt bitfield_apsint = result.Val.getInt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006039 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
6040 }
6041 }
6042 }
6043 if (is_bitfield_ptr)
6044 *is_bitfield_ptr = is_bitfield;
6045
6046 return CompilerType(getASTContext(), field->getType());
6047 }
6048 }
6049 }
6050 break;
6051
Sean Callananf9c622a2016-09-30 18:44:43 +00006052 case clang::Type::ObjCObjectPointer: {
6053 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00006054 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00006055 const clang::ObjCInterfaceType *objc_interface_type =
6056 objc_class_type->getInterfaceType();
6057 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00006058 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
6059 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00006060 clang::ObjCInterfaceDecl *class_interface_decl =
6061 objc_interface_type->getDecl();
6062 if (class_interface_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006063 return CompilerType(
6064 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
6065 idx, name, bit_offset_ptr,
6066 bitfield_bit_size_ptr, is_bitfield_ptr));
6067 }
6068 }
6069 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00006070 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006071
6072 case clang::Type::ObjCObject:
6073 case clang::Type::ObjCInterface:
6074 if (GetCompleteType(type)) {
6075 const clang::ObjCObjectType *objc_class_type =
6076 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6077 assert(objc_class_type);
6078 if (objc_class_type) {
6079 clang::ObjCInterfaceDecl *class_interface_decl =
6080 objc_class_type->getInterface();
6081 return CompilerType(
6082 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
6083 idx, name, bit_offset_ptr,
6084 bitfield_bit_size_ptr, is_bitfield_ptr));
6085 }
6086 }
6087 break;
6088
6089 case clang::Type::Typedef:
6090 return CompilerType(getASTContext(),
6091 llvm::cast<clang::TypedefType>(qual_type)
6092 ->getDecl()
6093 ->getUnderlyingType())
6094 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6095 is_bitfield_ptr);
6096
6097 case clang::Type::Auto:
6098 return CompilerType(
6099 getASTContext(),
6100 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
6101 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6102 is_bitfield_ptr);
6103
6104 case clang::Type::Elaborated:
6105 return CompilerType(
6106 getASTContext(),
6107 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
6108 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6109 is_bitfield_ptr);
6110
6111 case clang::Type::Paren:
6112 return CompilerType(getASTContext(),
6113 llvm::cast<clang::ParenType>(qual_type)->desugar())
6114 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6115 is_bitfield_ptr);
6116
6117 default:
6118 break;
6119 }
6120 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006121}
6122
Greg Clayton99558cc42015-08-24 23:46:31 +00006123uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006124ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
6125 uint32_t count = 0;
6126 clang::QualType qual_type(GetCanonicalQualType(type));
6127 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6128 switch (type_class) {
6129 case clang::Type::Record:
6130 if (GetCompleteType(type)) {
6131 const clang::CXXRecordDecl *cxx_record_decl =
6132 qual_type->getAsCXXRecordDecl();
6133 if (cxx_record_decl)
6134 count = cxx_record_decl->getNumBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006135 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006136 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006137
Kate Stoneb9c1b512016-09-06 20:57:50 +00006138 case clang::Type::ObjCObjectPointer:
6139 count = GetPointeeType(type).GetNumDirectBaseClasses();
6140 break;
6141
6142 case clang::Type::ObjCObject:
6143 if (GetCompleteType(type)) {
6144 const clang::ObjCObjectType *objc_class_type =
6145 qual_type->getAsObjCQualifiedInterfaceType();
6146 if (objc_class_type) {
6147 clang::ObjCInterfaceDecl *class_interface_decl =
6148 objc_class_type->getInterface();
6149
6150 if (class_interface_decl && class_interface_decl->getSuperClass())
6151 count = 1;
6152 }
6153 }
6154 break;
6155 case clang::Type::ObjCInterface:
6156 if (GetCompleteType(type)) {
6157 const clang::ObjCInterfaceType *objc_interface_type =
6158 qual_type->getAs<clang::ObjCInterfaceType>();
6159 if (objc_interface_type) {
6160 clang::ObjCInterfaceDecl *class_interface_decl =
6161 objc_interface_type->getInterface();
6162
6163 if (class_interface_decl && class_interface_decl->getSuperClass())
6164 count = 1;
6165 }
6166 }
6167 break;
6168
6169 case clang::Type::Typedef:
6170 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6171 ->getDecl()
6172 ->getUnderlyingType()
6173 .getAsOpaquePtr());
6174 break;
6175
6176 case clang::Type::Auto:
6177 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6178 ->getDeducedType()
6179 .getAsOpaquePtr());
6180 break;
6181
6182 case clang::Type::Elaborated:
6183 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6184 ->getNamedType()
6185 .getAsOpaquePtr());
6186 break;
6187
6188 case clang::Type::Paren:
6189 return GetNumDirectBaseClasses(
6190 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6191
6192 default:
6193 break;
6194 }
6195 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006196}
6197
6198uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006199ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
6200 uint32_t count = 0;
6201 clang::QualType qual_type(GetCanonicalQualType(type));
6202 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6203 switch (type_class) {
6204 case clang::Type::Record:
6205 if (GetCompleteType(type)) {
6206 const clang::CXXRecordDecl *cxx_record_decl =
6207 qual_type->getAsCXXRecordDecl();
6208 if (cxx_record_decl)
6209 count = cxx_record_decl->getNumVBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006210 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006211 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006212
Kate Stoneb9c1b512016-09-06 20:57:50 +00006213 case clang::Type::Typedef:
6214 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6215 ->getDecl()
6216 ->getUnderlyingType()
6217 .getAsOpaquePtr());
6218 break;
6219
6220 case clang::Type::Auto:
6221 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6222 ->getDeducedType()
6223 .getAsOpaquePtr());
6224 break;
6225
6226 case clang::Type::Elaborated:
6227 count =
6228 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6229 ->getNamedType()
6230 .getAsOpaquePtr());
6231 break;
6232
6233 case clang::Type::Paren:
6234 count = GetNumVirtualBaseClasses(
6235 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6236 break;
6237
6238 default:
6239 break;
6240 }
6241 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006242}
6243
Kate Stoneb9c1b512016-09-06 20:57:50 +00006244CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
6245 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6246 clang::QualType qual_type(GetCanonicalQualType(type));
6247 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6248 switch (type_class) {
6249 case clang::Type::Record:
6250 if (GetCompleteType(type)) {
6251 const clang::CXXRecordDecl *cxx_record_decl =
6252 qual_type->getAsCXXRecordDecl();
6253 if (cxx_record_decl) {
6254 uint32_t curr_idx = 0;
6255 clang::CXXRecordDecl::base_class_const_iterator base_class,
6256 base_class_end;
6257 for (base_class = cxx_record_decl->bases_begin(),
6258 base_class_end = cxx_record_decl->bases_end();
6259 base_class != base_class_end; ++base_class, ++curr_idx) {
6260 if (curr_idx == idx) {
6261 if (bit_offset_ptr) {
6262 const clang::ASTRecordLayout &record_layout =
6263 getASTContext()->getASTRecordLayout(cxx_record_decl);
6264 const clang::CXXRecordDecl *base_class_decl =
6265 llvm::cast<clang::CXXRecordDecl>(
6266 base_class->getType()
6267 ->getAs<clang::RecordType>()
6268 ->getDecl());
6269 if (base_class->isVirtual())
6270 *bit_offset_ptr =
6271 record_layout.getVBaseClassOffset(base_class_decl)
6272 .getQuantity() *
6273 8;
6274 else
6275 *bit_offset_ptr =
6276 record_layout.getBaseClassOffset(base_class_decl)
6277 .getQuantity() *
6278 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006279 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006280 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6281 }
6282 }
6283 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006284 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006285 break;
6286
6287 case clang::Type::ObjCObjectPointer:
6288 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6289
6290 case clang::Type::ObjCObject:
6291 if (idx == 0 && GetCompleteType(type)) {
6292 const clang::ObjCObjectType *objc_class_type =
6293 qual_type->getAsObjCQualifiedInterfaceType();
6294 if (objc_class_type) {
6295 clang::ObjCInterfaceDecl *class_interface_decl =
6296 objc_class_type->getInterface();
6297
6298 if (class_interface_decl) {
6299 clang::ObjCInterfaceDecl *superclass_interface_decl =
6300 class_interface_decl->getSuperClass();
6301 if (superclass_interface_decl) {
6302 if (bit_offset_ptr)
6303 *bit_offset_ptr = 0;
6304 return CompilerType(getASTContext(),
6305 getASTContext()->getObjCInterfaceType(
6306 superclass_interface_decl));
6307 }
6308 }
6309 }
6310 }
6311 break;
6312 case clang::Type::ObjCInterface:
6313 if (idx == 0 && GetCompleteType(type)) {
6314 const clang::ObjCObjectType *objc_interface_type =
6315 qual_type->getAs<clang::ObjCInterfaceType>();
6316 if (objc_interface_type) {
6317 clang::ObjCInterfaceDecl *class_interface_decl =
6318 objc_interface_type->getInterface();
6319
6320 if (class_interface_decl) {
6321 clang::ObjCInterfaceDecl *superclass_interface_decl =
6322 class_interface_decl->getSuperClass();
6323 if (superclass_interface_decl) {
6324 if (bit_offset_ptr)
6325 *bit_offset_ptr = 0;
6326 return CompilerType(getASTContext(),
6327 getASTContext()->getObjCInterfaceType(
6328 superclass_interface_decl));
6329 }
6330 }
6331 }
6332 }
6333 break;
6334
6335 case clang::Type::Typedef:
6336 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6337 ->getDecl()
6338 ->getUnderlyingType()
6339 .getAsOpaquePtr(),
6340 idx, bit_offset_ptr);
6341
6342 case clang::Type::Auto:
6343 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6344 ->getDeducedType()
6345 .getAsOpaquePtr(),
6346 idx, bit_offset_ptr);
6347
6348 case clang::Type::Elaborated:
6349 return GetDirectBaseClassAtIndex(
6350 llvm::cast<clang::ElaboratedType>(qual_type)
6351 ->getNamedType()
6352 .getAsOpaquePtr(),
6353 idx, bit_offset_ptr);
6354
6355 case clang::Type::Paren:
6356 return GetDirectBaseClassAtIndex(
6357 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6358 idx, bit_offset_ptr);
6359
6360 default:
6361 break;
6362 }
6363 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006364}
6365
Kate Stoneb9c1b512016-09-06 20:57:50 +00006366CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6367 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6368 clang::QualType qual_type(GetCanonicalQualType(type));
6369 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6370 switch (type_class) {
6371 case clang::Type::Record:
6372 if (GetCompleteType(type)) {
6373 const clang::CXXRecordDecl *cxx_record_decl =
6374 qual_type->getAsCXXRecordDecl();
6375 if (cxx_record_decl) {
6376 uint32_t curr_idx = 0;
6377 clang::CXXRecordDecl::base_class_const_iterator base_class,
6378 base_class_end;
6379 for (base_class = cxx_record_decl->vbases_begin(),
6380 base_class_end = cxx_record_decl->vbases_end();
6381 base_class != base_class_end; ++base_class, ++curr_idx) {
6382 if (curr_idx == idx) {
6383 if (bit_offset_ptr) {
6384 const clang::ASTRecordLayout &record_layout =
6385 getASTContext()->getASTRecordLayout(cxx_record_decl);
6386 const clang::CXXRecordDecl *base_class_decl =
6387 llvm::cast<clang::CXXRecordDecl>(
6388 base_class->getType()
6389 ->getAs<clang::RecordType>()
6390 ->getDecl());
6391 *bit_offset_ptr =
6392 record_layout.getVBaseClassOffset(base_class_decl)
6393 .getQuantity() *
6394 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006395 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006396 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6397 }
6398 }
6399 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006400 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006401 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006402
Kate Stoneb9c1b512016-09-06 20:57:50 +00006403 case clang::Type::Typedef:
6404 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6405 ->getDecl()
6406 ->getUnderlyingType()
6407 .getAsOpaquePtr(),
6408 idx, bit_offset_ptr);
6409
6410 case clang::Type::Auto:
6411 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6412 ->getDeducedType()
6413 .getAsOpaquePtr(),
6414 idx, bit_offset_ptr);
6415
6416 case clang::Type::Elaborated:
6417 return GetVirtualBaseClassAtIndex(
6418 llvm::cast<clang::ElaboratedType>(qual_type)
6419 ->getNamedType()
6420 .getAsOpaquePtr(),
6421 idx, bit_offset_ptr);
6422
6423 case clang::Type::Paren:
6424 return GetVirtualBaseClassAtIndex(
6425 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6426 idx, bit_offset_ptr);
6427
6428 default:
6429 break;
6430 }
6431 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006432}
6433
Greg Claytond8d4a572015-08-11 21:38:15 +00006434// If a pointer to a pointee type (the clang_type arg) says that it has no
6435// children, then we either need to trust it, or override it and return a
6436// different result. For example, an "int *" has one child that is an integer,
6437// but a function pointer doesn't have any children. Likewise if a Record type
6438// claims it has no children, then there really is nothing to show.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006439uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6440 if (type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00006441 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006442
6443 clang::QualType qual_type(type.getCanonicalType());
6444 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6445 switch (type_class) {
6446 case clang::Type::Builtin:
6447 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6448 case clang::BuiltinType::UnknownAny:
6449 case clang::BuiltinType::Void:
6450 case clang::BuiltinType::NullPtr:
6451 case clang::BuiltinType::OCLEvent:
6452 case clang::BuiltinType::OCLImage1dRO:
6453 case clang::BuiltinType::OCLImage1dWO:
6454 case clang::BuiltinType::OCLImage1dRW:
6455 case clang::BuiltinType::OCLImage1dArrayRO:
6456 case clang::BuiltinType::OCLImage1dArrayWO:
6457 case clang::BuiltinType::OCLImage1dArrayRW:
6458 case clang::BuiltinType::OCLImage1dBufferRO:
6459 case clang::BuiltinType::OCLImage1dBufferWO:
6460 case clang::BuiltinType::OCLImage1dBufferRW:
6461 case clang::BuiltinType::OCLImage2dRO:
6462 case clang::BuiltinType::OCLImage2dWO:
6463 case clang::BuiltinType::OCLImage2dRW:
6464 case clang::BuiltinType::OCLImage2dArrayRO:
6465 case clang::BuiltinType::OCLImage2dArrayWO:
6466 case clang::BuiltinType::OCLImage2dArrayRW:
6467 case clang::BuiltinType::OCLImage3dRO:
6468 case clang::BuiltinType::OCLImage3dWO:
6469 case clang::BuiltinType::OCLImage3dRW:
6470 case clang::BuiltinType::OCLSampler:
6471 return 0;
6472 case clang::BuiltinType::Bool:
6473 case clang::BuiltinType::Char_U:
6474 case clang::BuiltinType::UChar:
6475 case clang::BuiltinType::WChar_U:
6476 case clang::BuiltinType::Char16:
6477 case clang::BuiltinType::Char32:
6478 case clang::BuiltinType::UShort:
6479 case clang::BuiltinType::UInt:
6480 case clang::BuiltinType::ULong:
6481 case clang::BuiltinType::ULongLong:
6482 case clang::BuiltinType::UInt128:
6483 case clang::BuiltinType::Char_S:
6484 case clang::BuiltinType::SChar:
6485 case clang::BuiltinType::WChar_S:
6486 case clang::BuiltinType::Short:
6487 case clang::BuiltinType::Int:
6488 case clang::BuiltinType::Long:
6489 case clang::BuiltinType::LongLong:
6490 case clang::BuiltinType::Int128:
6491 case clang::BuiltinType::Float:
6492 case clang::BuiltinType::Double:
6493 case clang::BuiltinType::LongDouble:
6494 case clang::BuiltinType::Dependent:
6495 case clang::BuiltinType::Overload:
6496 case clang::BuiltinType::ObjCId:
6497 case clang::BuiltinType::ObjCClass:
6498 case clang::BuiltinType::ObjCSel:
6499 case clang::BuiltinType::BoundMember:
6500 case clang::BuiltinType::Half:
6501 case clang::BuiltinType::ARCUnbridgedCast:
6502 case clang::BuiltinType::PseudoObject:
6503 case clang::BuiltinType::BuiltinFn:
6504 case clang::BuiltinType::OMPArraySection:
6505 return 1;
6506 default:
6507 return 0;
6508 }
6509 break;
6510
6511 case clang::Type::Complex:
6512 return 1;
6513 case clang::Type::Pointer:
6514 return 1;
6515 case clang::Type::BlockPointer:
6516 return 0; // If block pointers don't have debug info, then no children for
6517 // them
6518 case clang::Type::LValueReference:
6519 return 1;
6520 case clang::Type::RValueReference:
6521 return 1;
6522 case clang::Type::MemberPointer:
6523 return 0;
6524 case clang::Type::ConstantArray:
6525 return 0;
6526 case clang::Type::IncompleteArray:
6527 return 0;
6528 case clang::Type::VariableArray:
6529 return 0;
6530 case clang::Type::DependentSizedArray:
6531 return 0;
6532 case clang::Type::DependentSizedExtVector:
6533 return 0;
6534 case clang::Type::Vector:
6535 return 0;
6536 case clang::Type::ExtVector:
6537 return 0;
6538 case clang::Type::FunctionProto:
6539 return 0; // When we function pointers, they have no children...
6540 case clang::Type::FunctionNoProto:
6541 return 0; // When we function pointers, they have no children...
6542 case clang::Type::UnresolvedUsing:
6543 return 0;
6544 case clang::Type::Paren:
6545 return GetNumPointeeChildren(
6546 llvm::cast<clang::ParenType>(qual_type)->desugar());
6547 case clang::Type::Typedef:
6548 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6549 ->getDecl()
6550 ->getUnderlyingType());
6551 case clang::Type::Auto:
6552 return GetNumPointeeChildren(
6553 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6554 case clang::Type::Elaborated:
6555 return GetNumPointeeChildren(
6556 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6557 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006558 return GetNumPointeeChildren(llvm::cast<clang::TypeOfExprType>(qual_type)
6559 ->getUnderlyingExpr()
6560 ->getType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006561 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006562 return GetNumPointeeChildren(
6563 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006564 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006565 return GetNumPointeeChildren(
6566 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006567 case clang::Type::Record:
6568 return 0;
6569 case clang::Type::Enum:
6570 return 1;
6571 case clang::Type::TemplateTypeParm:
6572 return 1;
6573 case clang::Type::SubstTemplateTypeParm:
6574 return 1;
6575 case clang::Type::TemplateSpecialization:
6576 return 1;
6577 case clang::Type::InjectedClassName:
6578 return 0;
6579 case clang::Type::DependentName:
6580 return 1;
6581 case clang::Type::DependentTemplateSpecialization:
6582 return 1;
6583 case clang::Type::ObjCObject:
6584 return 0;
6585 case clang::Type::ObjCInterface:
6586 return 0;
6587 case clang::Type::ObjCObjectPointer:
6588 return 1;
6589 default:
6590 break;
6591 }
6592 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006593}
6594
Kate Stoneb9c1b512016-09-06 20:57:50 +00006595CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6596 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6597 bool transparent_pointers, bool omit_empty_base_classes,
6598 bool ignore_array_bounds, std::string &child_name,
6599 uint32_t &child_byte_size, int32_t &child_byte_offset,
6600 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6601 bool &child_is_base_class, bool &child_is_deref_of_parent,
6602 ValueObject *valobj, uint64_t &language_flags) {
6603 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006604 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006605
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006606 auto get_exe_scope = [&exe_ctx]() {
6607 return exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr;
6608 };
6609
Kate Stoneb9c1b512016-09-06 20:57:50 +00006610 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6611 const clang::Type::TypeClass parent_type_class =
6612 parent_qual_type->getTypeClass();
6613 child_bitfield_bit_size = 0;
6614 child_bitfield_bit_offset = 0;
6615 child_is_base_class = false;
6616 language_flags = 0;
6617
Adrian Prantleca07c52018-11-05 20:49:07 +00006618 const bool idx_is_valid =
6619 idx < GetNumChildren(type, omit_empty_base_classes, exe_ctx);
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00006620 int32_t bit_offset;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006621 switch (parent_type_class) {
6622 case clang::Type::Builtin:
6623 if (idx_is_valid) {
6624 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6625 case clang::BuiltinType::ObjCId:
6626 case clang::BuiltinType::ObjCClass:
6627 child_name = "isa";
6628 child_byte_size =
6629 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6630 CHAR_BIT;
6631 return CompilerType(getASTContext(),
6632 getASTContext()->ObjCBuiltinClassTy);
6633
6634 default:
6635 break;
6636 }
6637 }
6638 break;
6639
6640 case clang::Type::Record:
6641 if (idx_is_valid && GetCompleteType(type)) {
6642 const clang::RecordType *record_type =
6643 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6644 const clang::RecordDecl *record_decl = record_type->getDecl();
6645 assert(record_decl);
6646 const clang::ASTRecordLayout &record_layout =
6647 getASTContext()->getASTRecordLayout(record_decl);
6648 uint32_t child_idx = 0;
6649
6650 const clang::CXXRecordDecl *cxx_record_decl =
6651 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6652 if (cxx_record_decl) {
6653 // We might have base classes to print out first
6654 clang::CXXRecordDecl::base_class_const_iterator base_class,
6655 base_class_end;
6656 for (base_class = cxx_record_decl->bases_begin(),
6657 base_class_end = cxx_record_decl->bases_end();
6658 base_class != base_class_end; ++base_class) {
6659 const clang::CXXRecordDecl *base_class_decl = nullptr;
6660
6661 // Skip empty base classes
6662 if (omit_empty_base_classes) {
6663 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6664 base_class->getType()->getAs<clang::RecordType>()->getDecl());
Jonas Devliegherea6682a42018-12-15 00:15:33 +00006665 if (!ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00006666 continue;
6667 }
6668
6669 if (idx == child_idx) {
6670 if (base_class_decl == nullptr)
6671 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6672 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6673
6674 if (base_class->isVirtual()) {
6675 bool handled = false;
6676 if (valobj) {
Aleksandr Urakov1dc51db2018-11-12 16:23:50 +00006677 clang::VTableContextBase *vtable_ctx =
6678 getASTContext()->getVTableContext();
6679 if (vtable_ctx)
6680 handled = GetVBaseBitOffset(*vtable_ctx, *valobj,
6681 record_layout, cxx_record_decl,
6682 base_class_decl, bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006683 }
6684 if (!handled)
6685 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6686 .getQuantity() *
6687 8;
6688 } else
6689 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6690 .getQuantity() *
6691 8;
6692
6693 // Base classes should be a multiple of 8 bits in size
6694 child_byte_offset = bit_offset / 8;
6695 CompilerType base_class_clang_type(getASTContext(),
6696 base_class->getType());
6697 child_name = base_class_clang_type.GetTypeName().AsCString("");
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006698 Optional<uint64_t> size =
6699 base_class_clang_type.GetBitSize(get_exe_scope());
Adrian Prantld963a7c2019-01-15 18:07:52 +00006700 if (!size)
6701 return {};
6702 uint64_t base_class_clang_type_bit_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006703
6704 // Base classes bit sizes should be a multiple of 8 bits in size
6705 assert(base_class_clang_type_bit_size % 8 == 0);
6706 child_byte_size = base_class_clang_type_bit_size / 8;
6707 child_is_base_class = true;
6708 return base_class_clang_type;
6709 }
6710 // We don't increment the child index in the for loop since we might
6711 // be skipping empty base classes
6712 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006713 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006714 }
6715 // Make sure index is in range...
6716 uint32_t field_idx = 0;
6717 clang::RecordDecl::field_iterator field, field_end;
6718 for (field = record_decl->field_begin(),
6719 field_end = record_decl->field_end();
6720 field != field_end; ++field, ++field_idx, ++child_idx) {
6721 if (idx == child_idx) {
6722 // Print the member type if requested
6723 // Print the member name and equal sign
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006724 child_name.assign(field->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006725
6726 // Figure out the type byte size (field_type_info.first) and
6727 // alignment (field_type_info.second) from the AST context.
6728 CompilerType field_clang_type(getASTContext(), field->getType());
6729 assert(field_idx < record_layout.getFieldCount());
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006730 Optional<uint64_t> size =
6731 field_clang_type.GetByteSize(get_exe_scope());
Adrian Prantld963a7c2019-01-15 18:07:52 +00006732 if (!size)
6733 return {};
6734 child_byte_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006735 const uint32_t child_bit_size = child_byte_size * 8;
6736
6737 // Figure out the field offset within the current struct/union/class
6738 // type
6739 bit_offset = record_layout.getFieldOffset(field_idx);
6740 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6741 child_bitfield_bit_size)) {
6742 child_bitfield_bit_offset = bit_offset % child_bit_size;
6743 const uint32_t child_bit_offset =
6744 bit_offset - child_bitfield_bit_offset;
6745 child_byte_offset = child_bit_offset / 8;
6746 } else {
6747 child_byte_offset = bit_offset / 8;
6748 }
6749
6750 return field_clang_type;
6751 }
6752 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006753 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006754 break;
6755
6756 case clang::Type::ObjCObject:
6757 case clang::Type::ObjCInterface:
6758 if (idx_is_valid && GetCompleteType(type)) {
6759 const clang::ObjCObjectType *objc_class_type =
6760 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6761 assert(objc_class_type);
6762 if (objc_class_type) {
6763 uint32_t child_idx = 0;
6764 clang::ObjCInterfaceDecl *class_interface_decl =
6765 objc_class_type->getInterface();
6766
6767 if (class_interface_decl) {
6768
6769 const clang::ASTRecordLayout &interface_layout =
6770 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6771 clang::ObjCInterfaceDecl *superclass_interface_decl =
6772 class_interface_decl->getSuperClass();
6773 if (superclass_interface_decl) {
6774 if (omit_empty_base_classes) {
6775 CompilerType base_class_clang_type(
6776 getASTContext(), getASTContext()->getObjCInterfaceType(
6777 superclass_interface_decl));
Adrian Prantleca07c52018-11-05 20:49:07 +00006778 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes,
6779 exe_ctx) > 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006780 if (idx == 0) {
6781 clang::QualType ivar_qual_type(
6782 getASTContext()->getObjCInterfaceType(
6783 superclass_interface_decl));
6784
6785 child_name.assign(
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006786 superclass_interface_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006787
6788 clang::TypeInfo ivar_type_info =
6789 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6790
6791 child_byte_size = ivar_type_info.Width / 8;
6792 child_byte_offset = 0;
6793 child_is_base_class = true;
6794
6795 return CompilerType(getASTContext(), ivar_qual_type);
6796 }
6797
6798 ++child_idx;
6799 }
6800 } else
6801 ++child_idx;
6802 }
6803
6804 const uint32_t superclass_idx = child_idx;
6805
6806 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6807 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6808 ivar_end = class_interface_decl->ivar_end();
6809
6810 for (ivar_pos = class_interface_decl->ivar_begin();
6811 ivar_pos != ivar_end; ++ivar_pos) {
6812 if (child_idx == idx) {
6813 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6814
6815 clang::QualType ivar_qual_type(ivar_decl->getType());
6816
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006817 child_name.assign(ivar_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006818
6819 clang::TypeInfo ivar_type_info =
6820 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6821
6822 child_byte_size = ivar_type_info.Width / 8;
6823
6824 // Figure out the field offset within the current
Adrian Prantl05097242018-04-30 16:49:04 +00006825 // struct/union/class type For ObjC objects, we can't trust the
6826 // bit offset we get from the Clang AST, since that doesn't
6827 // account for the space taken up by unbacked properties, or
6828 // from the changing size of base classes that are newer than
6829 // this class. So if we have a process around that we can ask
6830 // about this object, do so.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006831 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6832 Process *process = nullptr;
6833 if (exe_ctx)
6834 process = exe_ctx->GetProcessPtr();
6835 if (process) {
6836 ObjCLanguageRuntime *objc_runtime =
6837 process->GetObjCLanguageRuntime();
6838 if (objc_runtime != nullptr) {
6839 CompilerType parent_ast_type(getASTContext(),
6840 parent_qual_type);
6841 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6842 parent_ast_type, ivar_decl->getNameAsString().c_str());
6843 }
6844 }
6845
Aleksandr Urakovff701722018-08-20 05:59:27 +00006846 // Setting this to INT32_MAX to make sure we don't compute it
Kate Stoneb9c1b512016-09-06 20:57:50 +00006847 // twice...
Aleksandr Urakov53459482018-08-17 07:28:24 +00006848 bit_offset = INT32_MAX;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006849
6850 if (child_byte_offset ==
6851 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6852 bit_offset = interface_layout.getFieldOffset(child_idx -
6853 superclass_idx);
6854 child_byte_offset = bit_offset / 8;
6855 }
6856
6857 // Note, the ObjC Ivar Byte offset is just that, it doesn't
Adrian Prantl05097242018-04-30 16:49:04 +00006858 // account for the bit offset of a bitfield within its
6859 // containing object. So regardless of where we get the byte
Kate Stoneb9c1b512016-09-06 20:57:50 +00006860 // offset from, we still need to get the bit offset for
6861 // bitfields from the layout.
6862
6863 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6864 child_bitfield_bit_size)) {
Aleksandr Urakov53459482018-08-17 07:28:24 +00006865 if (bit_offset == INT32_MAX)
Kate Stoneb9c1b512016-09-06 20:57:50 +00006866 bit_offset = interface_layout.getFieldOffset(
6867 child_idx - superclass_idx);
6868
6869 child_bitfield_bit_offset = bit_offset % 8;
6870 }
6871 return CompilerType(getASTContext(), ivar_qual_type);
6872 }
6873 ++child_idx;
6874 }
6875 }
6876 }
6877 }
6878 }
6879 break;
6880
6881 case clang::Type::ObjCObjectPointer:
6882 if (idx_is_valid) {
6883 CompilerType pointee_clang_type(GetPointeeType(type));
6884
6885 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6886 child_is_deref_of_parent = false;
6887 bool tmp_child_is_deref_of_parent = false;
6888 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6889 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6890 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6891 child_bitfield_bit_size, child_bitfield_bit_offset,
6892 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6893 language_flags);
6894 } else {
6895 child_is_deref_of_parent = true;
6896 const char *parent_name =
Konrad Kleine248a1302019-05-23 11:14:47 +00006897 valobj ? valobj->GetName().GetCString() : nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006898 if (parent_name) {
6899 child_name.assign(1, '*');
6900 child_name += parent_name;
6901 }
6902
6903 // We have a pointer to an simple type
6904 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006905 if (Optional<uint64_t> size =
6906 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006907 child_byte_size = *size;
6908 child_byte_offset = 0;
6909 return pointee_clang_type;
6910 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006911 }
6912 }
6913 }
6914 break;
6915
6916 case clang::Type::Vector:
6917 case clang::Type::ExtVector:
6918 if (idx_is_valid) {
6919 const clang::VectorType *array =
6920 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6921 if (array) {
6922 CompilerType element_type(getASTContext(), array->getElementType());
6923 if (element_type.GetCompleteType()) {
6924 char element_name[64];
6925 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6926 static_cast<uint64_t>(idx));
6927 child_name.assign(element_name);
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006928 if (Optional<uint64_t> size =
6929 element_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006930 child_byte_size = *size;
6931 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6932 return element_type;
6933 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006934 }
6935 }
6936 }
6937 break;
6938
6939 case clang::Type::ConstantArray:
6940 case clang::Type::IncompleteArray:
6941 if (ignore_array_bounds || idx_is_valid) {
6942 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6943 if (array) {
6944 CompilerType element_type(getASTContext(), array->getElementType());
6945 if (element_type.GetCompleteType()) {
Zachary Turner827d5d72016-12-16 04:27:00 +00006946 child_name = llvm::formatv("[{0}]", idx);
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006947 if (Optional<uint64_t> size =
6948 element_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006949 child_byte_size = *size;
6950 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6951 return element_type;
6952 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006953 }
6954 }
6955 }
6956 break;
6957
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006958 case clang::Type::Pointer: {
6959 CompilerType pointee_clang_type(GetPointeeType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00006960
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006961 // Don't dereference "void *" pointers
6962 if (pointee_clang_type.IsVoidType())
6963 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006964
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006965 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6966 child_is_deref_of_parent = false;
6967 bool tmp_child_is_deref_of_parent = false;
6968 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6969 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6970 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6971 child_bitfield_bit_size, child_bitfield_bit_offset,
6972 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6973 language_flags);
6974 } else {
6975 child_is_deref_of_parent = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006976
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006977 const char *parent_name =
Konrad Kleine248a1302019-05-23 11:14:47 +00006978 valobj ? valobj->GetName().GetCString() : nullptr;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006979 if (parent_name) {
6980 child_name.assign(1, '*');
6981 child_name += parent_name;
6982 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006983
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006984 // We have a pointer to an simple type
6985 if (idx == 0) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006986 if (Optional<uint64_t> size =
6987 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006988 child_byte_size = *size;
6989 child_byte_offset = 0;
6990 return pointee_clang_type;
6991 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006992 }
6993 }
6994 break;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006995 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006996
6997 case clang::Type::LValueReference:
6998 case clang::Type::RValueReference:
6999 if (idx_is_valid) {
7000 const clang::ReferenceType *reference_type =
7001 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
7002 CompilerType pointee_clang_type(getASTContext(),
7003 reference_type->getPointeeType());
7004 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
7005 child_is_deref_of_parent = false;
7006 bool tmp_child_is_deref_of_parent = false;
7007 return pointee_clang_type.GetChildCompilerTypeAtIndex(
7008 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7009 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7010 child_bitfield_bit_size, child_bitfield_bit_offset,
7011 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
7012 language_flags);
7013 } else {
7014 const char *parent_name =
Konrad Kleine248a1302019-05-23 11:14:47 +00007015 valobj ? valobj->GetName().GetCString() : nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007016 if (parent_name) {
7017 child_name.assign(1, '&');
7018 child_name += parent_name;
7019 }
7020
7021 // We have a pointer to an simple type
7022 if (idx == 0) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00007023 if (Optional<uint64_t> size =
7024 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00007025 child_byte_size = *size;
7026 child_byte_offset = 0;
7027 return pointee_clang_type;
7028 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007029 }
7030 }
7031 }
7032 break;
7033
7034 case clang::Type::Typedef: {
7035 CompilerType typedefed_clang_type(
7036 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)
7037 ->getDecl()
7038 ->getUnderlyingType());
7039 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
7040 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7041 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7042 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7043 child_is_deref_of_parent, valobj, language_flags);
7044 } break;
7045
7046 case clang::Type::Auto: {
7047 CompilerType elaborated_clang_type(
7048 getASTContext(),
7049 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
7050 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
7051 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7052 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7053 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7054 child_is_deref_of_parent, valobj, language_flags);
7055 }
7056
7057 case clang::Type::Elaborated: {
7058 CompilerType elaborated_clang_type(
7059 getASTContext(),
7060 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
7061 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
7062 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7063 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7064 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7065 child_is_deref_of_parent, valobj, language_flags);
7066 }
7067
7068 case clang::Type::Paren: {
7069 CompilerType paren_clang_type(
7070 getASTContext(),
7071 llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
7072 return paren_clang_type.GetChildCompilerTypeAtIndex(
7073 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7074 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7075 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7076 child_is_deref_of_parent, valobj, language_flags);
7077 }
7078
7079 default:
7080 break;
7081 }
7082 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007083}
7084
Kate Stoneb9c1b512016-09-06 20:57:50 +00007085static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
7086 const clang::CXXBaseSpecifier *base_spec,
7087 bool omit_empty_base_classes) {
7088 uint32_t child_idx = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007089
Kate Stoneb9c1b512016-09-06 20:57:50 +00007090 const clang::CXXRecordDecl *cxx_record_decl =
7091 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7092
7093 // const char *super_name = record_decl->getNameAsCString();
7094 // const char *base_name =
7095 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
7096 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
7097 //
7098 if (cxx_record_decl) {
7099 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
7100 for (base_class = cxx_record_decl->bases_begin(),
7101 base_class_end = cxx_record_decl->bases_end();
7102 base_class != base_class_end; ++base_class) {
7103 if (omit_empty_base_classes) {
7104 if (BaseSpecifierIsEmpty(base_class))
7105 continue;
7106 }
7107
7108 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
7109 // super_name, base_name,
7110 // child_idx,
7111 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
7112 //
7113 //
7114 if (base_class == base_spec)
7115 return child_idx;
7116 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00007117 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007118 }
7119
7120 return UINT32_MAX;
7121}
7122
7123static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
7124 clang::NamedDecl *canonical_decl,
7125 bool omit_empty_base_classes) {
7126 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
7127 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
7128 omit_empty_base_classes);
7129
7130 clang::RecordDecl::field_iterator field, field_end;
7131 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
7132 field != field_end; ++field, ++child_idx) {
7133 if (field->getCanonicalDecl() == canonical_decl)
7134 return child_idx;
7135 }
7136
7137 return UINT32_MAX;
Greg Claytond8d4a572015-08-11 21:38:15 +00007138}
7139
7140// Look for a child member (doesn't include base classes, but it does include
Adrian Prantl05097242018-04-30 16:49:04 +00007141// their members) in the type hierarchy. Returns an index path into
7142// "clang_type" on how to reach the appropriate member.
Greg Claytond8d4a572015-08-11 21:38:15 +00007143//
7144// class A
7145// {
7146// public:
7147// int m_a;
7148// int m_b;
7149// };
7150//
7151// class B
7152// {
7153// };
7154//
7155// class C :
7156// public B,
7157// public A
7158// {
7159// };
7160//
7161// If we have a clang type that describes "class C", and we wanted to looked
7162// "m_b" in it:
7163//
Kate Stoneb9c1b512016-09-06 20:57:50 +00007164// With omit_empty_base_classes == false we would get an integer array back
Adrian Prantl05097242018-04-30 16:49:04 +00007165// with: { 1, 1 } The first index 1 is the child index for "class A" within
7166// class C The second index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007167//
Adrian Prantl05097242018-04-30 16:49:04 +00007168// With omit_empty_base_classes == true we would get an integer array back
7169// with: { 0, 1 } The first index 0 is the child index for "class A" within
7170// class C (since class B doesn't have any members it doesn't count) The second
7171// index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007172
Kate Stoneb9c1b512016-09-06 20:57:50 +00007173size_t ClangASTContext::GetIndexOfChildMemberWithName(
7174 lldb::opaque_compiler_type_t type, const char *name,
7175 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
7176 if (type && name && name[0]) {
7177 clang::QualType qual_type(GetCanonicalQualType(type));
7178 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7179 switch (type_class) {
7180 case clang::Type::Record:
7181 if (GetCompleteType(type)) {
7182 const clang::RecordType *record_type =
7183 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7184 const clang::RecordDecl *record_decl = record_type->getDecl();
Enrico Granata36f51e42015-12-18 22:41:25 +00007185
Kate Stoneb9c1b512016-09-06 20:57:50 +00007186 assert(record_decl);
7187 uint32_t child_idx = 0;
7188
7189 const clang::CXXRecordDecl *cxx_record_decl =
7190 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7191
7192 // Try and find a field that matches NAME
7193 clang::RecordDecl::field_iterator field, field_end;
7194 llvm::StringRef name_sref(name);
7195 for (field = record_decl->field_begin(),
7196 field_end = record_decl->field_end();
7197 field != field_end; ++field, ++child_idx) {
7198 llvm::StringRef field_name = field->getName();
7199 if (field_name.empty()) {
7200 CompilerType field_type(getASTContext(), field->getType());
7201 child_indexes.push_back(child_idx);
7202 if (field_type.GetIndexOfChildMemberWithName(
7203 name, omit_empty_base_classes, child_indexes))
7204 return child_indexes.size();
7205 child_indexes.pop_back();
7206
7207 } else if (field_name.equals(name_sref)) {
7208 // We have to add on the number of base classes to this index!
7209 child_indexes.push_back(
7210 child_idx + ClangASTContext::GetNumBaseClasses(
7211 cxx_record_decl, omit_empty_base_classes));
7212 return child_indexes.size();
7213 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007214 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007215
Kate Stoneb9c1b512016-09-06 20:57:50 +00007216 if (cxx_record_decl) {
7217 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7218
7219 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7220
7221 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7222 // Didn't find things easily, lets let clang do its thang...
7223 clang::IdentifierInfo &ident_ref =
7224 getASTContext()->Idents.get(name_sref);
7225 clang::DeclarationName decl_name(&ident_ref);
7226
7227 clang::CXXBasePaths paths;
7228 if (cxx_record_decl->lookupInBases(
7229 [decl_name](const clang::CXXBaseSpecifier *specifier,
7230 clang::CXXBasePath &path) {
7231 return clang::CXXRecordDecl::FindOrdinaryMember(
7232 specifier, path, decl_name);
7233 },
7234 paths)) {
7235 clang::CXXBasePaths::const_paths_iterator path,
7236 path_end = paths.end();
7237 for (path = paths.begin(); path != path_end; ++path) {
7238 const size_t num_path_elements = path->size();
7239 for (size_t e = 0; e < num_path_elements; ++e) {
7240 clang::CXXBasePathElement elem = (*path)[e];
7241
7242 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7243 omit_empty_base_classes);
7244 if (child_idx == UINT32_MAX) {
7245 child_indexes.clear();
7246 return 0;
7247 } else {
7248 child_indexes.push_back(child_idx);
7249 parent_record_decl = llvm::cast<clang::RecordDecl>(
7250 elem.Base->getType()
7251 ->getAs<clang::RecordType>()
7252 ->getDecl());
7253 }
7254 }
7255 for (clang::NamedDecl *path_decl : path->Decls) {
7256 child_idx = GetIndexForRecordChild(
7257 parent_record_decl, path_decl, omit_empty_base_classes);
7258 if (child_idx == UINT32_MAX) {
7259 child_indexes.clear();
7260 return 0;
7261 } else {
7262 child_indexes.push_back(child_idx);
7263 }
7264 }
7265 }
7266 return child_indexes.size();
7267 }
7268 }
7269 }
7270 break;
7271
7272 case clang::Type::ObjCObject:
7273 case clang::Type::ObjCInterface:
7274 if (GetCompleteType(type)) {
7275 llvm::StringRef name_sref(name);
7276 const clang::ObjCObjectType *objc_class_type =
7277 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7278 assert(objc_class_type);
7279 if (objc_class_type) {
7280 uint32_t child_idx = 0;
7281 clang::ObjCInterfaceDecl *class_interface_decl =
7282 objc_class_type->getInterface();
7283
7284 if (class_interface_decl) {
7285 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7286 ivar_end = class_interface_decl->ivar_end();
7287 clang::ObjCInterfaceDecl *superclass_interface_decl =
7288 class_interface_decl->getSuperClass();
7289
7290 for (ivar_pos = class_interface_decl->ivar_begin();
7291 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7292 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7293
7294 if (ivar_decl->getName().equals(name_sref)) {
7295 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7296 (omit_empty_base_classes &&
7297 ObjCDeclHasIVars(superclass_interface_decl, true)))
7298 ++child_idx;
7299
7300 child_indexes.push_back(child_idx);
7301 return child_indexes.size();
7302 }
7303 }
7304
7305 if (superclass_interface_decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00007306 // The super class index is always zero for ObjC classes, so we
7307 // push it onto the child indexes in case we find an ivar in our
7308 // superclass...
Kate Stoneb9c1b512016-09-06 20:57:50 +00007309 child_indexes.push_back(0);
7310
7311 CompilerType superclass_clang_type(
7312 getASTContext(), getASTContext()->getObjCInterfaceType(
7313 superclass_interface_decl));
7314 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7315 name, omit_empty_base_classes, child_indexes)) {
Adrian Prantl05097242018-04-30 16:49:04 +00007316 // We did find an ivar in a superclass so just return the
7317 // results!
Kate Stoneb9c1b512016-09-06 20:57:50 +00007318 return child_indexes.size();
7319 }
7320
Adrian Prantl05097242018-04-30 16:49:04 +00007321 // We didn't find an ivar matching "name" in our superclass, pop
7322 // the superclass zero index that we pushed on above.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007323 child_indexes.pop_back();
7324 }
7325 }
7326 }
7327 }
7328 break;
7329
7330 case clang::Type::ObjCObjectPointer: {
7331 CompilerType objc_object_clang_type(
7332 getASTContext(),
7333 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7334 ->getPointeeType());
7335 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7336 name, omit_empty_base_classes, child_indexes);
7337 } break;
7338
7339 case clang::Type::ConstantArray: {
7340 // const clang::ConstantArrayType *array =
7341 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7342 // const uint64_t element_count =
7343 // array->getSize().getLimitedValue();
7344 //
7345 // if (idx < element_count)
7346 // {
7347 // std::pair<uint64_t, unsigned> field_type_info =
7348 // ast->getTypeInfo(array->getElementType());
7349 //
7350 // char element_name[32];
7351 // ::snprintf (element_name, sizeof (element_name),
7352 // "%s[%u]", parent_name ? parent_name : "", idx);
7353 //
7354 // child_name.assign(element_name);
7355 // assert(field_type_info.first % 8 == 0);
7356 // child_byte_size = field_type_info.first / 8;
7357 // child_byte_offset = idx * child_byte_size;
7358 // return array->getElementType().getAsOpaquePtr();
7359 // }
7360 } break;
7361
7362 // case clang::Type::MemberPointerType:
7363 // {
7364 // MemberPointerType *mem_ptr_type =
7365 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7366 // clang::QualType pointee_type =
7367 // mem_ptr_type->getPointeeType();
7368 //
7369 // if (ClangASTContext::IsAggregateType
7370 // (pointee_type.getAsOpaquePtr()))
7371 // {
7372 // return GetIndexOfChildWithName (ast,
7373 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7374 // name);
7375 // }
7376 // }
7377 // break;
7378 //
7379 case clang::Type::LValueReference:
7380 case clang::Type::RValueReference: {
7381 const clang::ReferenceType *reference_type =
7382 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7383 clang::QualType pointee_type(reference_type->getPointeeType());
7384 CompilerType pointee_clang_type(getASTContext(), pointee_type);
7385
7386 if (pointee_clang_type.IsAggregateType()) {
7387 return pointee_clang_type.GetIndexOfChildMemberWithName(
7388 name, omit_empty_base_classes, child_indexes);
7389 }
7390 } break;
7391
7392 case clang::Type::Pointer: {
7393 CompilerType pointee_clang_type(GetPointeeType(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::Typedef:
7402 return CompilerType(getASTContext(),
7403 llvm::cast<clang::TypedefType>(qual_type)
7404 ->getDecl()
7405 ->getUnderlyingType())
7406 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7407 child_indexes);
7408
7409 case clang::Type::Auto:
7410 return CompilerType(
7411 getASTContext(),
7412 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7413 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7414 child_indexes);
7415
7416 case clang::Type::Elaborated:
7417 return CompilerType(
7418 getASTContext(),
7419 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7420 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7421 child_indexes);
7422
7423 case clang::Type::Paren:
7424 return CompilerType(getASTContext(),
7425 llvm::cast<clang::ParenType>(qual_type)->desugar())
7426 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7427 child_indexes);
7428
7429 default:
7430 break;
7431 }
7432 }
7433 return 0;
7434}
Greg Claytond8d4a572015-08-11 21:38:15 +00007435
7436// Get the index of the child of "clang_type" whose name matches. This function
7437// doesn't descend into the children, but only looks one level deep and name
7438// matches can include base class names.
7439
7440uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007441ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7442 const char *name,
7443 bool omit_empty_base_classes) {
7444 if (type && name && name[0]) {
7445 clang::QualType qual_type(GetCanonicalQualType(type));
Enrico Granata36f51e42015-12-18 22:41:25 +00007446
Kate Stoneb9c1b512016-09-06 20:57:50 +00007447 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7448
7449 switch (type_class) {
7450 case clang::Type::Record:
7451 if (GetCompleteType(type)) {
7452 const clang::RecordType *record_type =
7453 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7454 const clang::RecordDecl *record_decl = record_type->getDecl();
7455
7456 assert(record_decl);
7457 uint32_t child_idx = 0;
7458
7459 const clang::CXXRecordDecl *cxx_record_decl =
7460 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7461
7462 if (cxx_record_decl) {
7463 clang::CXXRecordDecl::base_class_const_iterator base_class,
7464 base_class_end;
7465 for (base_class = cxx_record_decl->bases_begin(),
7466 base_class_end = cxx_record_decl->bases_end();
7467 base_class != base_class_end; ++base_class) {
7468 // Skip empty base classes
7469 clang::CXXRecordDecl *base_class_decl =
7470 llvm::cast<clang::CXXRecordDecl>(
7471 base_class->getType()
7472 ->getAs<clang::RecordType>()
7473 ->getDecl());
7474 if (omit_empty_base_classes &&
Jonas Devliegherea6682a42018-12-15 00:15:33 +00007475 !ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00007476 continue;
7477
7478 CompilerType base_class_clang_type(getASTContext(),
7479 base_class->getType());
7480 std::string base_class_type_name(
7481 base_class_clang_type.GetTypeName().AsCString(""));
Jonas Devlieghere8d20cfd2018-12-21 22:46:10 +00007482 if (base_class_type_name == name)
Kate Stoneb9c1b512016-09-06 20:57:50 +00007483 return child_idx;
7484 ++child_idx;
7485 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007486 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007487
Kate Stoneb9c1b512016-09-06 20:57:50 +00007488 // Try and find a field that matches NAME
7489 clang::RecordDecl::field_iterator field, field_end;
7490 llvm::StringRef name_sref(name);
7491 for (field = record_decl->field_begin(),
7492 field_end = record_decl->field_end();
7493 field != field_end; ++field, ++child_idx) {
7494 if (field->getName().equals(name_sref))
7495 return child_idx;
7496 }
7497 }
7498 break;
7499
7500 case clang::Type::ObjCObject:
7501 case clang::Type::ObjCInterface:
7502 if (GetCompleteType(type)) {
7503 llvm::StringRef name_sref(name);
7504 const clang::ObjCObjectType *objc_class_type =
7505 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7506 assert(objc_class_type);
7507 if (objc_class_type) {
7508 uint32_t child_idx = 0;
7509 clang::ObjCInterfaceDecl *class_interface_decl =
7510 objc_class_type->getInterface();
7511
7512 if (class_interface_decl) {
7513 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7514 ivar_end = class_interface_decl->ivar_end();
7515 clang::ObjCInterfaceDecl *superclass_interface_decl =
7516 class_interface_decl->getSuperClass();
7517
7518 for (ivar_pos = class_interface_decl->ivar_begin();
7519 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7520 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7521
7522 if (ivar_decl->getName().equals(name_sref)) {
7523 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7524 (omit_empty_base_classes &&
7525 ObjCDeclHasIVars(superclass_interface_decl, true)))
7526 ++child_idx;
7527
7528 return child_idx;
7529 }
7530 }
7531
7532 if (superclass_interface_decl) {
7533 if (superclass_interface_decl->getName().equals(name_sref))
7534 return 0;
7535 }
7536 }
7537 }
7538 }
7539 break;
7540
7541 case clang::Type::ObjCObjectPointer: {
7542 CompilerType pointee_clang_type(
7543 getASTContext(),
7544 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7545 ->getPointeeType());
7546 return pointee_clang_type.GetIndexOfChildWithName(
7547 name, omit_empty_base_classes);
7548 } break;
7549
7550 case clang::Type::ConstantArray: {
7551 // const clang::ConstantArrayType *array =
7552 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7553 // const uint64_t element_count =
7554 // array->getSize().getLimitedValue();
7555 //
7556 // if (idx < element_count)
7557 // {
7558 // std::pair<uint64_t, unsigned> field_type_info =
7559 // ast->getTypeInfo(array->getElementType());
7560 //
7561 // char element_name[32];
7562 // ::snprintf (element_name, sizeof (element_name),
7563 // "%s[%u]", parent_name ? parent_name : "", idx);
7564 //
7565 // child_name.assign(element_name);
7566 // assert(field_type_info.first % 8 == 0);
7567 // child_byte_size = field_type_info.first / 8;
7568 // child_byte_offset = idx * child_byte_size;
7569 // return array->getElementType().getAsOpaquePtr();
7570 // }
7571 } break;
7572
7573 // case clang::Type::MemberPointerType:
7574 // {
7575 // MemberPointerType *mem_ptr_type =
7576 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7577 // clang::QualType pointee_type =
7578 // mem_ptr_type->getPointeeType();
7579 //
7580 // if (ClangASTContext::IsAggregateType
7581 // (pointee_type.getAsOpaquePtr()))
7582 // {
7583 // return GetIndexOfChildWithName (ast,
7584 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7585 // name);
7586 // }
7587 // }
7588 // break;
7589 //
7590 case clang::Type::LValueReference:
7591 case clang::Type::RValueReference: {
7592 const clang::ReferenceType *reference_type =
7593 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7594 CompilerType pointee_type(getASTContext(),
7595 reference_type->getPointeeType());
7596
7597 if (pointee_type.IsAggregateType()) {
7598 return pointee_type.GetIndexOfChildWithName(name,
7599 omit_empty_base_classes);
7600 }
7601 } break;
7602
7603 case clang::Type::Pointer: {
7604 const clang::PointerType *pointer_type =
7605 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7606 CompilerType pointee_type(getASTContext(),
7607 pointer_type->getPointeeType());
7608
7609 if (pointee_type.IsAggregateType()) {
7610 return pointee_type.GetIndexOfChildWithName(name,
7611 omit_empty_base_classes);
7612 } else {
7613 // if (parent_name)
7614 // {
7615 // child_name.assign(1, '*');
7616 // child_name += parent_name;
7617 // }
7618 //
7619 // // We have a pointer to an simple type
7620 // if (idx == 0)
7621 // {
7622 // std::pair<uint64_t, unsigned> clang_type_info
7623 // = ast->getTypeInfo(pointee_type);
7624 // assert(clang_type_info.first % 8 == 0);
7625 // child_byte_size = clang_type_info.first / 8;
7626 // child_byte_offset = 0;
7627 // return pointee_type.getAsOpaquePtr();
7628 // }
7629 }
7630 } break;
7631
7632 case clang::Type::Auto:
7633 return CompilerType(
7634 getASTContext(),
7635 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7636 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7637
7638 case clang::Type::Elaborated:
7639 return CompilerType(
7640 getASTContext(),
7641 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7642 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7643
7644 case clang::Type::Paren:
7645 return CompilerType(getASTContext(),
7646 llvm::cast<clang::ParenType>(qual_type)->desugar())
7647 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7648
7649 case clang::Type::Typedef:
7650 return CompilerType(getASTContext(),
7651 llvm::cast<clang::TypedefType>(qual_type)
7652 ->getDecl()
7653 ->getUnderlyingType())
7654 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7655
7656 default:
7657 break;
7658 }
7659 }
7660 return UINT32_MAX;
7661}
Greg Claytond8d4a572015-08-11 21:38:15 +00007662
7663size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007664ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7665 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007666 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007667
Kate Stoneb9c1b512016-09-06 20:57:50 +00007668 clang::QualType qual_type(GetCanonicalQualType(type));
7669 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7670 switch (type_class) {
7671 case clang::Type::Record:
7672 if (GetCompleteType(type)) {
7673 const clang::CXXRecordDecl *cxx_record_decl =
7674 qual_type->getAsCXXRecordDecl();
7675 if (cxx_record_decl) {
7676 const clang::ClassTemplateSpecializationDecl *template_decl =
7677 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7678 cxx_record_decl);
7679 if (template_decl)
7680 return template_decl->getTemplateArgs().size();
7681 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007682 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007683 break;
7684
7685 case clang::Type::Typedef:
7686 return (CompilerType(getASTContext(),
7687 llvm::cast<clang::TypedefType>(qual_type)
7688 ->getDecl()
7689 ->getUnderlyingType()))
7690 .GetNumTemplateArguments();
7691
7692 case clang::Type::Auto:
7693 return (CompilerType(
7694 getASTContext(),
7695 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7696 .GetNumTemplateArguments();
7697
7698 case clang::Type::Elaborated:
7699 return (CompilerType(
7700 getASTContext(),
7701 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7702 .GetNumTemplateArguments();
7703
7704 case clang::Type::Paren:
7705 return (CompilerType(getASTContext(),
7706 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7707 .GetNumTemplateArguments();
7708
7709 default:
7710 break;
7711 }
7712
7713 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007714}
7715
Pavel Labath769b21e2017-11-13 14:26:21 +00007716const clang::ClassTemplateSpecializationDecl *
7717ClangASTContext::GetAsTemplateSpecialization(
7718 lldb::opaque_compiler_type_t type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00007719 if (!type)
Pavel Labath769b21e2017-11-13 14:26:21 +00007720 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007721
7722 clang::QualType qual_type(GetCanonicalQualType(type));
7723 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7724 switch (type_class) {
Pavel Labath769b21e2017-11-13 14:26:21 +00007725 case clang::Type::Record: {
7726 if (! GetCompleteType(type))
7727 return nullptr;
7728 const clang::CXXRecordDecl *cxx_record_decl =
7729 qual_type->getAsCXXRecordDecl();
7730 if (!cxx_record_decl)
7731 return nullptr;
7732 return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7733 cxx_record_decl);
7734 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007735
7736 case clang::Type::Typedef:
Pavel Labath769b21e2017-11-13 14:26:21 +00007737 return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type)
7738 ->getDecl()
7739 ->getUnderlyingType()
7740 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007741
7742 case clang::Type::Auto:
Pavel Labath769b21e2017-11-13 14:26:21 +00007743 return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type)
7744 ->getDeducedType()
7745 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007746
7747 case clang::Type::Elaborated:
Pavel Labath769b21e2017-11-13 14:26:21 +00007748 return GetAsTemplateSpecialization(
7749 llvm::cast<clang::ElaboratedType>(qual_type)
7750 ->getNamedType()
7751 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007752
7753 case clang::Type::Paren:
Pavel Labath769b21e2017-11-13 14:26:21 +00007754 return GetAsTemplateSpecialization(
7755 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007756
7757 default:
Pavel Labath769b21e2017-11-13 14:26:21 +00007758 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007759 }
Pavel Labath769b21e2017-11-13 14:26:21 +00007760}
7761
7762lldb::TemplateArgumentKind
7763ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
7764 size_t arg_idx) {
7765 const clang::ClassTemplateSpecializationDecl *template_decl =
7766 GetAsTemplateSpecialization(type);
7767 if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size())
7768 return eTemplateArgumentKindNull;
7769
7770 switch (template_decl->getTemplateArgs()[arg_idx].getKind()) {
7771 case clang::TemplateArgument::Null:
7772 return eTemplateArgumentKindNull;
7773
7774 case clang::TemplateArgument::NullPtr:
7775 return eTemplateArgumentKindNullPtr;
7776
7777 case clang::TemplateArgument::Type:
7778 return eTemplateArgumentKindType;
7779
7780 case clang::TemplateArgument::Declaration:
7781 return eTemplateArgumentKindDeclaration;
7782
7783 case clang::TemplateArgument::Integral:
7784 return eTemplateArgumentKindIntegral;
7785
7786 case clang::TemplateArgument::Template:
7787 return eTemplateArgumentKindTemplate;
7788
7789 case clang::TemplateArgument::TemplateExpansion:
7790 return eTemplateArgumentKindTemplateExpansion;
7791
7792 case clang::TemplateArgument::Expression:
7793 return eTemplateArgumentKindExpression;
7794
7795 case clang::TemplateArgument::Pack:
7796 return eTemplateArgumentKindPack;
7797 }
7798 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
7799}
7800
7801CompilerType
7802ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
7803 size_t idx) {
7804 const clang::ClassTemplateSpecializationDecl *template_decl =
7805 GetAsTemplateSpecialization(type);
7806 if (!template_decl || idx >= template_decl->getTemplateArgs().size())
7807 return CompilerType();
7808
7809 const clang::TemplateArgument &template_arg =
7810 template_decl->getTemplateArgs()[idx];
7811 if (template_arg.getKind() != clang::TemplateArgument::Type)
7812 return CompilerType();
7813
7814 return CompilerType(getASTContext(), template_arg.getAsType());
7815}
7816
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00007817Optional<CompilerType::IntegralTemplateArgument>
Pavel Labath769b21e2017-11-13 14:26:21 +00007818ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
7819 size_t idx) {
7820 const clang::ClassTemplateSpecializationDecl *template_decl =
7821 GetAsTemplateSpecialization(type);
7822 if (! template_decl || idx >= template_decl->getTemplateArgs().size())
Pavel Labathf59056f2017-11-30 10:16:54 +00007823 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007824
7825 const clang::TemplateArgument &template_arg =
7826 template_decl->getTemplateArgs()[idx];
7827 if (template_arg.getKind() != clang::TemplateArgument::Integral)
Pavel Labathf59056f2017-11-30 10:16:54 +00007828 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007829
Pavel Labathf59056f2017-11-30 10:16:54 +00007830 return {{template_arg.getAsIntegral(),
7831 CompilerType(getASTContext(), template_arg.getIntegralType())}};
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007832}
7833
Kate Stoneb9c1b512016-09-06 20:57:50 +00007834CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7835 if (type)
7836 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7837 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007838}
7839
Kate Stoneb9c1b512016-09-06 20:57:50 +00007840clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7841 const clang::EnumType *enutype =
7842 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7843 if (enutype)
7844 return enutype->getDecl();
Konrad Kleine248a1302019-05-23 11:14:47 +00007845 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007846}
7847
7848clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7849 const clang::RecordType *record_type =
7850 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7851 if (record_type)
7852 return record_type->getDecl();
7853 return nullptr;
7854}
7855
7856clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
Zachary Turner1639c6b2018-12-17 16:15:13 +00007857 return ClangUtil::GetAsTagDecl(type);
Greg Claytone6b36cd2015-12-08 01:02:08 +00007858}
7859
Aleksandr Urakov709426b2018-09-10 08:08:43 +00007860clang::TypedefNameDecl *
7861ClangASTContext::GetAsTypedefDecl(const CompilerType &type) {
7862 const clang::TypedefType *typedef_type =
7863 llvm::dyn_cast<clang::TypedefType>(ClangUtil::GetQualType(type));
7864 if (typedef_type)
7865 return typedef_type->getDecl();
7866 return nullptr;
7867}
7868
Greg Claytond8d4a572015-08-11 21:38:15 +00007869clang::CXXRecordDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007870ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7871 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
Greg Claytond8d4a572015-08-11 21:38:15 +00007872}
7873
7874clang::ObjCInterfaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007875ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7876 const clang::ObjCObjectType *objc_class_type =
7877 llvm::dyn_cast<clang::ObjCObjectType>(
7878 ClangUtil::GetCanonicalQualType(type));
7879 if (objc_class_type)
7880 return objc_class_type->getInterface();
7881 return nullptr;
7882}
7883
7884clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007885 const CompilerType &type, llvm::StringRef name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00007886 const CompilerType &field_clang_type, AccessType access,
7887 uint32_t bitfield_bit_size) {
7888 if (!type.IsValid() || !field_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00007889 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007890 ClangASTContext *ast =
7891 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7892 if (!ast)
7893 return nullptr;
7894 clang::ASTContext *clang_ast = ast->getASTContext();
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007895 clang::IdentifierInfo *ident = nullptr;
7896 if (!name.empty())
7897 ident = &clang_ast->Idents.get(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00007898
7899 clang::FieldDecl *field = nullptr;
7900
7901 clang::Expr *bit_width = nullptr;
7902 if (bitfield_bit_size != 0) {
7903 llvm::APInt bitfield_bit_size_apint(
7904 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7905 bit_width = new (*clang_ast)
7906 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7907 clang_ast->IntTy, clang::SourceLocation());
7908 }
7909
7910 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7911 if (record_decl) {
7912 field = clang::FieldDecl::Create(
7913 *clang_ast, record_decl, clang::SourceLocation(),
7914 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007915 ident, // Identifier
7916 ClangUtil::GetQualType(field_clang_type), // Field type
7917 nullptr, // TInfo *
7918 bit_width, // BitWidth
7919 false, // Mutable
7920 clang::ICIS_NoInit); // HasInit
Kate Stoneb9c1b512016-09-06 20:57:50 +00007921
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007922 if (name.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +00007923 // Determine whether this field corresponds to an anonymous struct or
7924 // union.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007925 if (const clang::TagType *TagT =
7926 field->getType()->getAs<clang::TagType>()) {
7927 if (clang::RecordDecl *Rec =
7928 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7929 if (!Rec->getDeclName()) {
7930 Rec->setAnonymousStructOrUnion(true);
7931 field->setImplicit();
7932 }
7933 }
7934 }
7935
7936 if (field) {
7937 field->setAccess(
7938 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7939
7940 record_decl->addDecl(field);
7941
7942#ifdef LLDB_CONFIGURATION_DEBUG
7943 VerifyDecl(field);
7944#endif
7945 }
7946 } else {
7947 clang::ObjCInterfaceDecl *class_interface_decl =
7948 ast->GetAsObjCInterfaceDecl(type);
7949
7950 if (class_interface_decl) {
7951 const bool is_synthesized = false;
7952
7953 field_clang_type.GetCompleteType();
7954
7955 field = clang::ObjCIvarDecl::Create(
7956 *clang_ast, class_interface_decl, clang::SourceLocation(),
7957 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007958 ident, // Identifier
7959 ClangUtil::GetQualType(field_clang_type), // Field type
7960 nullptr, // TypeSourceInfo *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007961 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7962 is_synthesized);
7963
7964 if (field) {
7965 class_interface_decl->addDecl(field);
7966
7967#ifdef LLDB_CONFIGURATION_DEBUG
7968 VerifyDecl(field);
7969#endif
7970 }
7971 }
7972 }
7973 return field;
Greg Claytond8d4a572015-08-11 21:38:15 +00007974}
7975
Kate Stoneb9c1b512016-09-06 20:57:50 +00007976void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7977 if (!type)
7978 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007979
Kate Stoneb9c1b512016-09-06 20:57:50 +00007980 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7981 if (!ast)
7982 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007983
Kate Stoneb9c1b512016-09-06 20:57:50 +00007984 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007985
Kate Stoneb9c1b512016-09-06 20:57:50 +00007986 if (!record_decl)
7987 return;
7988
7989 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7990
7991 IndirectFieldVector indirect_fields;
7992 clang::RecordDecl::field_iterator field_pos;
7993 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7994 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7995 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7996 last_field_pos = field_pos++) {
7997 if (field_pos->isAnonymousStructOrUnion()) {
7998 clang::QualType field_qual_type = field_pos->getType();
7999
8000 const clang::RecordType *field_record_type =
8001 field_qual_type->getAs<clang::RecordType>();
8002
8003 if (!field_record_type)
8004 continue;
8005
8006 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
8007
8008 if (!field_record_decl)
8009 continue;
8010
8011 for (clang::RecordDecl::decl_iterator
8012 di = field_record_decl->decls_begin(),
8013 de = field_record_decl->decls_end();
8014 di != de; ++di) {
8015 if (clang::FieldDecl *nested_field_decl =
8016 llvm::dyn_cast<clang::FieldDecl>(*di)) {
8017 clang::NamedDecl **chain =
8018 new (*ast->getASTContext()) clang::NamedDecl *[2];
8019 chain[0] = *field_pos;
8020 chain[1] = nested_field_decl;
8021 clang::IndirectFieldDecl *indirect_field =
8022 clang::IndirectFieldDecl::Create(
8023 *ast->getASTContext(), record_decl, clang::SourceLocation(),
8024 nested_field_decl->getIdentifier(),
8025 nested_field_decl->getType(), {chain, 2});
8026
8027 indirect_field->setImplicit();
8028
8029 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
8030 field_pos->getAccess(), nested_field_decl->getAccess()));
8031
8032 indirect_fields.push_back(indirect_field);
8033 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
8034 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
8035 size_t nested_chain_size =
8036 nested_indirect_field_decl->getChainingSize();
8037 clang::NamedDecl **chain = new (*ast->getASTContext())
8038 clang::NamedDecl *[nested_chain_size + 1];
8039 chain[0] = *field_pos;
8040
8041 int chain_index = 1;
8042 for (clang::IndirectFieldDecl::chain_iterator
8043 nci = nested_indirect_field_decl->chain_begin(),
8044 nce = nested_indirect_field_decl->chain_end();
8045 nci < nce; ++nci) {
8046 chain[chain_index] = *nci;
8047 chain_index++;
8048 }
8049
8050 clang::IndirectFieldDecl *indirect_field =
8051 clang::IndirectFieldDecl::Create(
8052 *ast->getASTContext(), record_decl, clang::SourceLocation(),
8053 nested_indirect_field_decl->getIdentifier(),
8054 nested_indirect_field_decl->getType(),
8055 {chain, nested_chain_size + 1});
8056
8057 indirect_field->setImplicit();
8058
8059 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
8060 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
8061
8062 indirect_fields.push_back(indirect_field);
Greg Claytond8d4a572015-08-11 21:38:15 +00008063 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008064 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008065 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008066 }
8067
Adrian Prantl05097242018-04-30 16:49:04 +00008068 // Check the last field to see if it has an incomplete array type as its last
8069 // member and if it does, the tell the record decl about it
Kate Stoneb9c1b512016-09-06 20:57:50 +00008070 if (last_field_pos != field_end_pos) {
8071 if (last_field_pos->getType()->isIncompleteArrayType())
8072 record_decl->hasFlexibleArrayMember();
8073 }
8074
8075 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
8076 ife = indirect_fields.end();
8077 ifi < ife; ++ifi) {
8078 record_decl->addDecl(*ifi);
8079 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008080}
8081
Kate Stoneb9c1b512016-09-06 20:57:50 +00008082void ClangASTContext::SetIsPacked(const CompilerType &type) {
8083 if (type) {
8084 ClangASTContext *ast =
8085 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8086 if (ast) {
8087 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
8088
8089 if (!record_decl)
Greg Claytonf73034f2015-09-08 18:15:05 +00008090 return;
8091
Kate Stoneb9c1b512016-09-06 20:57:50 +00008092 record_decl->addAttr(
8093 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
Greg Claytond8d4a572015-08-11 21:38:15 +00008094 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008095 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008096}
8097
Kate Stoneb9c1b512016-09-06 20:57:50 +00008098clang::VarDecl *ClangASTContext::AddVariableToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008099 const CompilerType &type, llvm::StringRef name,
8100 const CompilerType &var_type, AccessType access) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00008101 if (!type.IsValid() || !var_type.IsValid())
8102 return nullptr;
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008103
Kate Stoneb9c1b512016-09-06 20:57:50 +00008104 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8105 if (!ast)
8106 return nullptr;
8107
8108 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008109 if (!record_decl)
8110 return nullptr;
8111
8112 clang::VarDecl *var_decl = nullptr;
8113 clang::IdentifierInfo *ident = nullptr;
8114 if (!name.empty())
8115 ident = &ast->getASTContext()->Idents.get(name);
8116
8117 var_decl = clang::VarDecl::Create(
8118 *ast->getASTContext(), // ASTContext &
8119 record_decl, // DeclContext *
8120 clang::SourceLocation(), // clang::SourceLocation StartLoc
8121 clang::SourceLocation(), // clang::SourceLocation IdLoc
8122 ident, // clang::IdentifierInfo *
8123 ClangUtil::GetQualType(var_type), // Variable clang::QualType
8124 nullptr, // TypeSourceInfo *
8125 clang::SC_Static); // StorageClass
8126 if (!var_decl)
8127 return nullptr;
8128
8129 var_decl->setAccess(
8130 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
8131 record_decl->addDecl(var_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008132
Greg Claytond8d4a572015-08-11 21:38:15 +00008133#ifdef LLDB_CONFIGURATION_DEBUG
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008134 VerifyDecl(var_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008135#endif
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008136
Kate Stoneb9c1b512016-09-06 20:57:50 +00008137 return var_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008138}
8139
Kate Stoneb9c1b512016-09-06 20:57:50 +00008140clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
Davide Italiano675767a2018-03-27 19:40:50 +00008141 lldb::opaque_compiler_type_t type, const char *name, const char *mangled_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008142 const CompilerType &method_clang_type, lldb::AccessType access,
8143 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
8144 bool is_attr_used, bool is_artificial) {
8145 if (!type || !method_clang_type.IsValid() || name == nullptr ||
8146 name[0] == '\0')
8147 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008148
Kate Stoneb9c1b512016-09-06 20:57:50 +00008149 clang::QualType record_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008150
Kate Stoneb9c1b512016-09-06 20:57:50 +00008151 clang::CXXRecordDecl *cxx_record_decl =
8152 record_qual_type->getAsCXXRecordDecl();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008153
Kate Stoneb9c1b512016-09-06 20:57:50 +00008154 if (cxx_record_decl == nullptr)
8155 return nullptr;
8156
8157 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8158
8159 clang::CXXMethodDecl *cxx_method_decl = nullptr;
8160
8161 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
8162
8163 const clang::FunctionType *function_type =
8164 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
8165
8166 if (function_type == nullptr)
8167 return nullptr;
8168
8169 const clang::FunctionProtoType *method_function_prototype(
8170 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
8171
8172 if (!method_function_prototype)
8173 return nullptr;
8174
8175 unsigned int num_params = method_function_prototype->getNumParams();
8176
8177 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
8178 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
8179
8180 if (is_artificial)
8181 return nullptr; // skip everything artificial
8182
Richard Smith36851a62019-05-09 04:40:57 +00008183 const clang::ExplicitSpecifier explicit_spec(
8184 nullptr /*expr*/, is_explicit
8185 ? clang::ExplicitSpecKind::ResolvedTrue
8186 : clang::ExplicitSpecKind::ResolvedFalse);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008187 if (name[0] == '~') {
8188 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
8189 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8190 clang::DeclarationNameInfo(
8191 getASTContext()->DeclarationNames.getCXXDestructorName(
8192 getASTContext()->getCanonicalType(record_qual_type)),
8193 clang::SourceLocation()),
8194 method_qual_type, nullptr, is_inline, is_artificial);
8195 cxx_method_decl = cxx_dtor_decl;
8196 } else if (decl_name == cxx_record_decl->getDeclName()) {
8197 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
8198 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8199 clang::DeclarationNameInfo(
8200 getASTContext()->DeclarationNames.getCXXConstructorName(
8201 getASTContext()->getCanonicalType(record_qual_type)),
8202 clang::SourceLocation()),
8203 method_qual_type,
8204 nullptr, // TypeSourceInfo *
Richard Smith36851a62019-05-09 04:40:57 +00008205 explicit_spec, is_inline, is_artificial, false /*is_constexpr*/);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008206 cxx_method_decl = cxx_ctor_decl;
8207 } else {
8208 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
8209 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
8210
8211 if (IsOperator(name, op_kind)) {
8212 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
Adrian Prantl05097242018-04-30 16:49:04 +00008213 // Check the number of operator parameters. Sometimes we have seen bad
8214 // DWARF that doesn't correctly describe operators and if we try to
8215 // create a method and add it to the class, clang will assert and
8216 // crash, so we need to make sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00008217 const bool is_method = true;
8218 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
8219 is_method, op_kind, num_params))
8220 return nullptr;
8221 cxx_method_decl = clang::CXXMethodDecl::Create(
8222 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8223 clang::DeclarationNameInfo(
8224 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
8225 clang::SourceLocation()),
8226 method_qual_type,
8227 nullptr, // TypeSourceInfo *
8228 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
8229 } else if (num_params == 0) {
8230 // Conversion operators don't take params...
8231 cxx_method_decl = clang::CXXConversionDecl::Create(
8232 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8233 clang::DeclarationNameInfo(
8234 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
8235 getASTContext()->getCanonicalType(
8236 function_type->getReturnType())),
8237 clang::SourceLocation()),
8238 method_qual_type,
8239 nullptr, // TypeSourceInfo *
Richard Smith36851a62019-05-09 04:40:57 +00008240 is_inline, explicit_spec, false /*is_constexpr*/,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008241 clang::SourceLocation());
8242 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008243 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008244
8245 if (cxx_method_decl == nullptr) {
8246 cxx_method_decl = clang::CXXMethodDecl::Create(
8247 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8248 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8249 method_qual_type,
8250 nullptr, // TypeSourceInfo *
8251 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
Greg Claytond8d4a572015-08-11 21:38:15 +00008252 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008253 }
8254
8255 clang::AccessSpecifier access_specifier =
8256 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8257
8258 cxx_method_decl->setAccess(access_specifier);
8259 cxx_method_decl->setVirtualAsWritten(is_virtual);
8260
8261 if (is_attr_used)
8262 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8263
Konrad Kleine248a1302019-05-23 11:14:47 +00008264 if (mangled_name != nullptr) {
Davide Italiano675767a2018-03-27 19:40:50 +00008265 cxx_method_decl->addAttr(
8266 clang::AsmLabelAttr::CreateImplicit(*getASTContext(), mangled_name));
8267 }
8268
Kate Stoneb9c1b512016-09-06 20:57:50 +00008269 // Populate the method decl with parameter decls
8270
8271 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8272
8273 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8274 params.push_back(clang::ParmVarDecl::Create(
8275 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8276 clang::SourceLocation(),
8277 nullptr, // anonymous
8278 method_function_prototype->getParamType(param_index), nullptr,
8279 clang::SC_None, nullptr));
8280 }
8281
8282 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8283
8284 cxx_record_decl->addDecl(cxx_method_decl);
8285
8286 // Sometimes the debug info will mention a constructor (default/copy/move),
8287 // destructor, or assignment operator (copy/move) but there won't be any
8288 // version of this in the code. So we check if the function was artificially
8289 // generated and if it is trivial and this lets the compiler/backend know
8290 // that it can inline the IR for these when it needs to and we can avoid a
8291 // "missing function" error when running expressions.
8292
8293 if (is_artificial) {
8294 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8295 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8296 (cxx_ctor_decl->isCopyConstructor() &&
8297 cxx_record_decl->hasTrivialCopyConstructor()) ||
8298 (cxx_ctor_decl->isMoveConstructor() &&
8299 cxx_record_decl->hasTrivialMoveConstructor()))) {
8300 cxx_ctor_decl->setDefaulted();
8301 cxx_ctor_decl->setTrivial(true);
8302 } else if (cxx_dtor_decl) {
8303 if (cxx_record_decl->hasTrivialDestructor()) {
8304 cxx_dtor_decl->setDefaulted();
8305 cxx_dtor_decl->setTrivial(true);
8306 }
8307 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8308 cxx_record_decl->hasTrivialCopyAssignment()) ||
8309 (cxx_method_decl->isMoveAssignmentOperator() &&
8310 cxx_record_decl->hasTrivialMoveAssignment())) {
8311 cxx_method_decl->setDefaulted();
8312 cxx_method_decl->setTrivial(true);
Greg Claytond8d4a572015-08-11 21:38:15 +00008313 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008314 }
8315
Greg Claytond8d4a572015-08-11 21:38:15 +00008316#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008317 VerifyDecl(cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008318#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008319
Kate Stoneb9c1b512016-09-06 20:57:50 +00008320 // printf ("decl->isPolymorphic() = %i\n",
8321 // cxx_record_decl->isPolymorphic());
8322 // printf ("decl->isAggregate() = %i\n",
8323 // cxx_record_decl->isAggregate());
8324 // printf ("decl->isPOD() = %i\n",
8325 // cxx_record_decl->isPOD());
8326 // printf ("decl->isEmpty() = %i\n",
8327 // cxx_record_decl->isEmpty());
8328 // printf ("decl->isAbstract() = %i\n",
8329 // cxx_record_decl->isAbstract());
8330 // printf ("decl->hasTrivialConstructor() = %i\n",
8331 // cxx_record_decl->hasTrivialConstructor());
8332 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8333 // cxx_record_decl->hasTrivialCopyConstructor());
8334 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8335 // cxx_record_decl->hasTrivialCopyAssignment());
8336 // printf ("decl->hasTrivialDestructor() = %i\n",
8337 // cxx_record_decl->hasTrivialDestructor());
8338 return cxx_method_decl;
8339}
Greg Claytond8d4a572015-08-11 21:38:15 +00008340
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00008341void ClangASTContext::AddMethodOverridesForCXXRecordType(
8342 lldb::opaque_compiler_type_t type) {
8343 if (auto *record = GetAsCXXRecordDecl(type))
8344 for (auto *method : record->methods())
8345 addOverridesForMethod(method);
8346}
8347
Greg Claytond8d4a572015-08-11 21:38:15 +00008348#pragma mark C++ Base Classes
8349
Zachary Turner970f38e2018-10-25 20:44:56 +00008350std::unique_ptr<clang::CXXBaseSpecifier>
Kate Stoneb9c1b512016-09-06 20:57:50 +00008351ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8352 AccessType access, bool is_virtual,
8353 bool base_of_class) {
Zachary Turner970f38e2018-10-25 20:44:56 +00008354 if (!type)
8355 return nullptr;
8356
8357 return llvm::make_unique<clang::CXXBaseSpecifier>(
8358 clang::SourceRange(), is_virtual, base_of_class,
8359 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8360 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8361 clang::SourceLocation());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008362}
8363
Zachary Turner970f38e2018-10-25 20:44:56 +00008364bool ClangASTContext::TransferBaseClasses(
Kate Stoneb9c1b512016-09-06 20:57:50 +00008365 lldb::opaque_compiler_type_t type,
Zachary Turner970f38e2018-10-25 20:44:56 +00008366 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases) {
8367 if (!type)
8368 return false;
8369 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8370 if (!cxx_record_decl)
8371 return false;
8372 std::vector<clang::CXXBaseSpecifier *> raw_bases;
8373 raw_bases.reserve(bases.size());
8374
8375 // Clang will make a copy of them, so it's ok that we pass pointers that we're
8376 // about to destroy.
8377 for (auto &b : bases)
8378 raw_bases.push_back(b.get());
8379 cxx_record_decl->setBases(raw_bases.data(), raw_bases.size());
8380 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008381}
8382
8383bool ClangASTContext::SetObjCSuperClass(
8384 const CompilerType &type, const CompilerType &superclass_clang_type) {
8385 ClangASTContext *ast =
8386 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8387 if (!ast)
8388 return false;
8389 clang::ASTContext *clang_ast = ast->getASTContext();
8390
8391 if (type && superclass_clang_type.IsValid() &&
8392 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8393 clang::ObjCInterfaceDecl *class_interface_decl =
8394 GetAsObjCInterfaceDecl(type);
8395 clang::ObjCInterfaceDecl *super_interface_decl =
8396 GetAsObjCInterfaceDecl(superclass_clang_type);
8397 if (class_interface_decl && super_interface_decl) {
8398 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8399 clang_ast->getObjCInterfaceType(super_interface_decl)));
8400 return true;
8401 }
8402 }
8403 return false;
8404}
8405
8406bool ClangASTContext::AddObjCClassProperty(
8407 const CompilerType &type, const char *property_name,
8408 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8409 const char *property_setter_name, const char *property_getter_name,
8410 uint32_t property_attributes, ClangASTMetadata *metadata) {
8411 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8412 property_name[0] == '\0')
8413 return false;
8414 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8415 if (!ast)
8416 return false;
8417 clang::ASTContext *clang_ast = ast->getASTContext();
8418
8419 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8420
8421 if (class_interface_decl) {
8422 CompilerType property_clang_type_to_access;
8423
8424 if (property_clang_type.IsValid())
8425 property_clang_type_to_access = property_clang_type;
8426 else if (ivar_decl)
8427 property_clang_type_to_access =
8428 CompilerType(clang_ast, ivar_decl->getType());
8429
8430 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8431 clang::TypeSourceInfo *prop_type_source;
8432 if (ivar_decl)
8433 prop_type_source =
8434 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8435 else
8436 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8437 ClangUtil::GetQualType(property_clang_type));
8438
8439 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8440 *clang_ast, class_interface_decl,
8441 clang::SourceLocation(), // Source Location
8442 &clang_ast->Idents.get(property_name),
8443 clang::SourceLocation(), // Source Location for AT
8444 clang::SourceLocation(), // Source location for (
8445 ivar_decl ? ivar_decl->getType()
8446 : ClangUtil::GetQualType(property_clang_type),
8447 prop_type_source);
8448
8449 if (property_decl) {
8450 if (metadata)
8451 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8452
8453 class_interface_decl->addDecl(property_decl);
8454
8455 clang::Selector setter_sel, getter_sel;
8456
8457 if (property_setter_name != nullptr) {
8458 std::string property_setter_no_colon(
8459 property_setter_name, strlen(property_setter_name) - 1);
8460 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008461 &clang_ast->Idents.get(property_setter_no_colon);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008462 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8463 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8464 std::string setter_sel_string("set");
8465 setter_sel_string.push_back(::toupper(property_name[0]));
8466 setter_sel_string.append(&property_name[1]);
8467 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008468 &clang_ast->Idents.get(setter_sel_string);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008469 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8470 }
8471 property_decl->setSetterName(setter_sel);
8472 property_decl->setPropertyAttributes(
8473 clang::ObjCPropertyDecl::OBJC_PR_setter);
8474
8475 if (property_getter_name != nullptr) {
8476 clang::IdentifierInfo *getter_ident =
8477 &clang_ast->Idents.get(property_getter_name);
8478 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8479 } else {
8480 clang::IdentifierInfo *getter_ident =
8481 &clang_ast->Idents.get(property_name);
8482 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8483 }
8484 property_decl->setGetterName(getter_sel);
8485 property_decl->setPropertyAttributes(
8486 clang::ObjCPropertyDecl::OBJC_PR_getter);
8487
8488 if (ivar_decl)
8489 property_decl->setPropertyIvarDecl(ivar_decl);
8490
8491 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8492 property_decl->setPropertyAttributes(
8493 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8494 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8495 property_decl->setPropertyAttributes(
8496 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8497 if (property_attributes & DW_APPLE_PROPERTY_assign)
8498 property_decl->setPropertyAttributes(
8499 clang::ObjCPropertyDecl::OBJC_PR_assign);
8500 if (property_attributes & DW_APPLE_PROPERTY_retain)
8501 property_decl->setPropertyAttributes(
8502 clang::ObjCPropertyDecl::OBJC_PR_retain);
8503 if (property_attributes & DW_APPLE_PROPERTY_copy)
8504 property_decl->setPropertyAttributes(
8505 clang::ObjCPropertyDecl::OBJC_PR_copy);
8506 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8507 property_decl->setPropertyAttributes(
8508 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8509 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8510 property_decl->setPropertyAttributes(
8511 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8512 if (property_attributes &
8513 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8514 property_decl->setPropertyAttributes(
8515 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8516 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8517 property_decl->setPropertyAttributes(
8518 clang::ObjCPropertyDecl::OBJC_PR_class);
8519
8520 const bool isInstance =
8521 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8522
8523 if (!getter_sel.isNull() &&
8524 !(isInstance
8525 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8526 : class_interface_decl->lookupClassMethod(getter_sel))) {
8527 const bool isVariadic = false;
8528 const bool isSynthesized = false;
8529 const bool isImplicitlyDeclared = true;
8530 const bool isDefined = false;
8531 const clang::ObjCMethodDecl::ImplementationControl impControl =
8532 clang::ObjCMethodDecl::None;
8533 const bool HasRelatedResultType = false;
8534
8535 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8536 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8537 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8538 nullptr, class_interface_decl, isInstance, isVariadic,
8539 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8540 HasRelatedResultType);
8541
8542 if (getter && metadata)
8543 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8544
8545 if (getter) {
8546 getter->setMethodParams(*clang_ast,
8547 llvm::ArrayRef<clang::ParmVarDecl *>(),
8548 llvm::ArrayRef<clang::SourceLocation>());
8549
8550 class_interface_decl->addDecl(getter);
8551 }
8552 }
8553
8554 if (!setter_sel.isNull() &&
8555 !(isInstance
8556 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8557 : class_interface_decl->lookupClassMethod(setter_sel))) {
8558 clang::QualType result_type = clang_ast->VoidTy;
8559 const bool isVariadic = false;
8560 const bool isSynthesized = false;
8561 const bool isImplicitlyDeclared = true;
8562 const bool isDefined = false;
8563 const clang::ObjCMethodDecl::ImplementationControl impControl =
8564 clang::ObjCMethodDecl::None;
8565 const bool HasRelatedResultType = false;
8566
8567 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8568 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8569 setter_sel, result_type, nullptr, class_interface_decl,
8570 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8571 isDefined, impControl, HasRelatedResultType);
8572
8573 if (setter && metadata)
8574 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8575
8576 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8577
8578 params.push_back(clang::ParmVarDecl::Create(
8579 *clang_ast, setter, clang::SourceLocation(),
8580 clang::SourceLocation(),
8581 nullptr, // anonymous
8582 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8583 clang::SC_Auto, nullptr));
8584
8585 if (setter) {
8586 setter->setMethodParams(
8587 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8588 llvm::ArrayRef<clang::SourceLocation>());
8589
8590 class_interface_decl->addDecl(setter);
8591 }
8592 }
8593
8594 return true;
8595 }
8596 }
8597 }
8598 return false;
8599}
8600
8601bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8602 bool check_superclass) {
8603 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8604 if (class_interface_decl)
8605 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8606 return false;
8607}
8608
8609clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8610 const CompilerType &type,
8611 const char *name, // the full symbol name as seen in the symbol table
8612 // (lldb::opaque_compiler_type_t type, "-[NString
8613 // stringWithCString:]")
8614 const CompilerType &method_clang_type, lldb::AccessType access,
8615 bool is_artificial, bool is_variadic) {
8616 if (!type || !method_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008617 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008618
Kate Stoneb9c1b512016-09-06 20:57:50 +00008619 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8620
8621 if (class_interface_decl == nullptr)
8622 return nullptr;
8623 ClangASTContext *lldb_ast =
8624 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8625 if (lldb_ast == nullptr)
8626 return nullptr;
8627 clang::ASTContext *ast = lldb_ast->getASTContext();
8628
8629 const char *selector_start = ::strchr(name, ' ');
8630 if (selector_start == nullptr)
8631 return nullptr;
8632
8633 selector_start++;
8634 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8635
8636 size_t len = 0;
8637 const char *start;
8638 // printf ("name = '%s'\n", name);
8639
8640 unsigned num_selectors_with_args = 0;
8641 for (start = selector_start; start && *start != '\0' && *start != ']';
8642 start += len) {
8643 len = ::strcspn(start, ":]");
8644 bool has_arg = (start[len] == ':');
8645 if (has_arg)
8646 ++num_selectors_with_args;
8647 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8648 if (has_arg)
8649 len += 1;
8650 }
8651
8652 if (selector_idents.size() == 0)
8653 return nullptr;
8654
8655 clang::Selector method_selector = ast->Selectors.getSelector(
8656 num_selectors_with_args ? selector_idents.size() : 0,
8657 selector_idents.data());
8658
8659 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8660
8661 // Populate the method decl with parameter decls
8662 const clang::Type *method_type(method_qual_type.getTypePtr());
8663
8664 if (method_type == nullptr)
8665 return nullptr;
8666
8667 const clang::FunctionProtoType *method_function_prototype(
8668 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8669
8670 if (!method_function_prototype)
8671 return nullptr;
8672
8673 bool is_synthesized = false;
8674 bool is_defined = false;
8675 clang::ObjCMethodDecl::ImplementationControl imp_control =
8676 clang::ObjCMethodDecl::None;
8677
8678 const unsigned num_args = method_function_prototype->getNumParams();
8679
8680 if (num_args != num_selectors_with_args)
8681 return nullptr; // some debug information is corrupt. We are not going to
8682 // deal with it.
8683
8684 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8685 *ast,
8686 clang::SourceLocation(), // beginLoc,
8687 clang::SourceLocation(), // endLoc,
8688 method_selector, method_function_prototype->getReturnType(),
8689 nullptr, // TypeSourceInfo *ResultTInfo,
8690 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8691 ClangUtil::GetQualType(type)),
8692 name[0] == '-', is_variadic, is_synthesized,
8693 true, // is_implicitly_declared; we force this to true because we don't
8694 // have source locations
8695 is_defined, imp_control, false /*has_related_result_type*/);
8696
8697 if (objc_method_decl == nullptr)
8698 return nullptr;
8699
8700 if (num_args > 0) {
8701 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8702
8703 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8704 params.push_back(clang::ParmVarDecl::Create(
8705 *ast, objc_method_decl, clang::SourceLocation(),
8706 clang::SourceLocation(),
8707 nullptr, // anonymous
8708 method_function_prototype->getParamType(param_index), nullptr,
8709 clang::SC_Auto, nullptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00008710 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008711
Kate Stoneb9c1b512016-09-06 20:57:50 +00008712 objc_method_decl->setMethodParams(
8713 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8714 llvm::ArrayRef<clang::SourceLocation>());
8715 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008716
Kate Stoneb9c1b512016-09-06 20:57:50 +00008717 class_interface_decl->addDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008718
Greg Claytond8d4a572015-08-11 21:38:15 +00008719#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008720 VerifyDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008721#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00008722
8723 return objc_method_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008724}
8725
Kate Stoneb9c1b512016-09-06 20:57:50 +00008726bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8727 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00008728 return false;
Greg Claytone6b36cd2015-12-08 01:02:08 +00008729
Kate Stoneb9c1b512016-09-06 20:57:50 +00008730 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008731
Kate Stoneb9c1b512016-09-06 20:57:50 +00008732 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8733 switch (type_class) {
8734 case clang::Type::Record: {
8735 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8736 if (cxx_record_decl)
8737 return cxx_record_decl->hasExternalLexicalStorage() ||
8738 cxx_record_decl->hasExternalVisibleStorage();
8739 } break;
Enrico Granata36f51e42015-12-18 22:41:25 +00008740
Kate Stoneb9c1b512016-09-06 20:57:50 +00008741 case clang::Type::Enum: {
8742 clang::EnumDecl *enum_decl =
8743 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8744 if (enum_decl)
8745 return enum_decl->hasExternalLexicalStorage() ||
8746 enum_decl->hasExternalVisibleStorage();
8747 } break;
8748
8749 case clang::Type::ObjCObject:
8750 case clang::Type::ObjCInterface: {
8751 const clang::ObjCObjectType *objc_class_type =
8752 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8753 assert(objc_class_type);
8754 if (objc_class_type) {
8755 clang::ObjCInterfaceDecl *class_interface_decl =
8756 objc_class_type->getInterface();
8757
8758 if (class_interface_decl)
8759 return class_interface_decl->hasExternalLexicalStorage() ||
8760 class_interface_decl->hasExternalVisibleStorage();
Greg Claytond8d4a572015-08-11 21:38:15 +00008761 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008762 } break;
8763
8764 case clang::Type::Typedef:
8765 return GetHasExternalStorage(CompilerType(
8766 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8767 ->getDecl()
8768 ->getUnderlyingType()
8769 .getAsOpaquePtr()));
8770
8771 case clang::Type::Auto:
8772 return GetHasExternalStorage(CompilerType(
8773 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8774 ->getDeducedType()
8775 .getAsOpaquePtr()));
8776
8777 case clang::Type::Elaborated:
8778 return GetHasExternalStorage(CompilerType(
8779 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8780 ->getNamedType()
8781 .getAsOpaquePtr()));
8782
8783 case clang::Type::Paren:
8784 return GetHasExternalStorage(CompilerType(
8785 type.GetTypeSystem(),
8786 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8787
8788 default:
8789 break;
8790 }
8791 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008792}
8793
Kate Stoneb9c1b512016-09-06 20:57:50 +00008794bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8795 bool has_extern) {
8796 if (!type)
8797 return false;
8798
8799 clang::QualType qual_type(GetCanonicalQualType(type));
8800
8801 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8802 switch (type_class) {
8803 case clang::Type::Record: {
8804 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8805 if (cxx_record_decl) {
8806 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8807 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8808 return true;
8809 }
8810 } break;
8811
8812 case clang::Type::Enum: {
8813 clang::EnumDecl *enum_decl =
8814 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8815 if (enum_decl) {
8816 enum_decl->setHasExternalLexicalStorage(has_extern);
8817 enum_decl->setHasExternalVisibleStorage(has_extern);
8818 return true;
8819 }
8820 } break;
8821
8822 case clang::Type::ObjCObject:
8823 case clang::Type::ObjCInterface: {
8824 const clang::ObjCObjectType *objc_class_type =
8825 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8826 assert(objc_class_type);
8827 if (objc_class_type) {
8828 clang::ObjCInterfaceDecl *class_interface_decl =
8829 objc_class_type->getInterface();
8830
8831 if (class_interface_decl) {
8832 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8833 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8834 return true;
8835 }
8836 }
8837 } break;
8838
8839 case clang::Type::Typedef:
8840 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8841 ->getDecl()
8842 ->getUnderlyingType()
8843 .getAsOpaquePtr(),
8844 has_extern);
8845
8846 case clang::Type::Auto:
8847 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8848 ->getDeducedType()
8849 .getAsOpaquePtr(),
8850 has_extern);
8851
8852 case clang::Type::Elaborated:
8853 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8854 ->getNamedType()
8855 .getAsOpaquePtr(),
8856 has_extern);
8857
8858 case clang::Type::Paren:
8859 return SetHasExternalStorage(
8860 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8861 has_extern);
8862
8863 default:
8864 break;
8865 }
8866 return false;
8867}
Greg Claytond8d4a572015-08-11 21:38:15 +00008868
8869#pragma mark TagDecl
8870
Kate Stoneb9c1b512016-09-06 20:57:50 +00008871bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8872 clang::QualType qual_type(ClangUtil::GetQualType(type));
8873 if (!qual_type.isNull()) {
8874 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8875 if (tag_type) {
8876 clang::TagDecl *tag_decl = tag_type->getDecl();
8877 if (tag_decl) {
8878 tag_decl->startDefinition();
8879 return true;
8880 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008881 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008882
8883 const clang::ObjCObjectType *object_type =
8884 qual_type->getAs<clang::ObjCObjectType>();
8885 if (object_type) {
8886 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8887 if (interface_decl) {
8888 interface_decl->startDefinition();
8889 return true;
8890 }
8891 }
8892 }
8893 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008894}
8895
Kate Stoneb9c1b512016-09-06 20:57:50 +00008896bool ClangASTContext::CompleteTagDeclarationDefinition(
8897 const CompilerType &type) {
8898 clang::QualType qual_type(ClangUtil::GetQualType(type));
8899 if (!qual_type.isNull()) {
8900 // Make sure we use the same methodology as
Adrian Prantl05097242018-04-30 16:49:04 +00008901 // ClangASTContext::StartTagDeclarationDefinition() as to how we start/end
8902 // the definition. Previously we were calling
Kate Stoneb9c1b512016-09-06 20:57:50 +00008903 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8904 if (tag_type) {
8905 clang::TagDecl *tag_decl = tag_type->getDecl();
8906 if (tag_decl) {
8907 clang::CXXRecordDecl *cxx_record_decl =
8908 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8909
8910 if (cxx_record_decl) {
8911 if (!cxx_record_decl->isCompleteDefinition())
8912 cxx_record_decl->completeDefinition();
8913 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8914 cxx_record_decl->setHasExternalLexicalStorage(false);
8915 cxx_record_decl->setHasExternalVisibleStorage(false);
8916 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008917 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008918 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008919 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008920
8921 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8922
8923 if (enutype) {
8924 clang::EnumDecl *enum_decl = enutype->getDecl();
8925
8926 if (enum_decl) {
8927 if (!enum_decl->isCompleteDefinition()) {
8928 ClangASTContext *lldb_ast =
8929 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8930 if (lldb_ast == nullptr)
8931 return false;
8932 clang::ASTContext *ast = lldb_ast->getASTContext();
8933
8934 /// TODO This really needs to be fixed.
8935
8936 QualType integer_type(enum_decl->getIntegerType());
8937 if (!integer_type.isNull()) {
8938 unsigned NumPositiveBits = 1;
8939 unsigned NumNegativeBits = 0;
8940
8941 clang::QualType promotion_qual_type;
8942 // If the enum integer type is less than an integer in bit width,
8943 // then we must promote it to an integer size.
8944 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8945 ast->getTypeSize(ast->IntTy)) {
8946 if (enum_decl->getIntegerType()->isSignedIntegerType())
8947 promotion_qual_type = ast->IntTy;
8948 else
8949 promotion_qual_type = ast->UnsignedIntTy;
8950 } else
8951 promotion_qual_type = enum_decl->getIntegerType();
8952
8953 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8954 promotion_qual_type, NumPositiveBits,
8955 NumNegativeBits);
8956 }
8957 }
8958 return true;
8959 }
8960 }
8961 }
8962 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008963}
8964
Aleksandr Urakov709426b2018-09-10 08:08:43 +00008965clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008966 const CompilerType &enum_type, const Declaration &decl, const char *name,
Zachary Turner1639c6b2018-12-17 16:15:13 +00008967 const llvm::APSInt &value) {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008968
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008969 if (!enum_type || ConstString(name).IsEmpty())
8970 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008971
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008972 lldbassert(enum_type.GetTypeSystem() == static_cast<TypeSystem *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +00008973
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008974 lldb::opaque_compiler_type_t enum_opaque_compiler_type =
8975 enum_type.GetOpaqueQualType();
8976
8977 if (!enum_opaque_compiler_type)
8978 return nullptr;
8979
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008980 clang::QualType enum_qual_type(
8981 GetCanonicalQualType(enum_opaque_compiler_type));
8982
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008983 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8984
8985 if (!clang_type)
8986 return nullptr;
8987
8988 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8989
8990 if (!enutype)
8991 return nullptr;
8992
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008993 clang::EnumConstantDecl *enumerator_decl = clang::EnumConstantDecl::Create(
8994 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8995 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier
Zachary Turner1639c6b2018-12-17 16:15:13 +00008996 clang::QualType(enutype, 0), nullptr, value);
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008997
8998 if (!enumerator_decl)
8999 return nullptr;
9000
9001 enutype->getDecl()->addDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009002
9003#ifdef LLDB_CONFIGURATION_DEBUG
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009004 VerifyDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009005#endif
9006
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009007 return enumerator_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00009008}
9009
Zachary Turner1639c6b2018-12-17 16:15:13 +00009010clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
9011 const CompilerType &enum_type, const Declaration &decl, const char *name,
9012 int64_t enum_value, uint32_t enum_value_bit_size) {
9013 CompilerType underlying_type =
9014 GetEnumerationIntegerType(enum_type.GetOpaqueQualType());
9015 bool is_signed = false;
9016 underlying_type.IsIntegerType(is_signed);
9017
9018 llvm::APSInt value(enum_value_bit_size, is_signed);
9019 value = enum_value;
9020
9021 return AddEnumerationValueToEnumerationType(enum_type, decl, name, value);
9022}
9023
Greg Claytona1e5dc82015-08-11 22:53:00 +00009024CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00009025ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
9026 clang::QualType enum_qual_type(GetCanonicalQualType(type));
9027 const clang::Type *clang_type = enum_qual_type.getTypePtr();
9028 if (clang_type) {
9029 const clang::EnumType *enutype =
9030 llvm::dyn_cast<clang::EnumType>(clang_type);
9031 if (enutype) {
9032 clang::EnumDecl *enum_decl = enutype->getDecl();
9033 if (enum_decl)
9034 return CompilerType(getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00009035 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009036 }
9037 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00009038}
9039
Kate Stoneb9c1b512016-09-06 20:57:50 +00009040CompilerType
9041ClangASTContext::CreateMemberPointerType(const CompilerType &type,
9042 const CompilerType &pointee_type) {
9043 if (type && pointee_type.IsValid() &&
9044 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
9045 ClangASTContext *ast =
9046 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
9047 if (!ast)
9048 return CompilerType();
9049 return CompilerType(ast->getASTContext(),
9050 ast->getASTContext()->getMemberPointerType(
9051 ClangUtil::GetQualType(pointee_type),
9052 ClangUtil::GetQualType(type).getTypePtr()));
9053 }
9054 return CompilerType();
9055}
Greg Claytond8d4a572015-08-11 21:38:15 +00009056
9057size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00009058ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
9059 const char *s, uint8_t *dst,
9060 size_t dst_size) {
9061 if (type) {
9062 clang::QualType qual_type(GetCanonicalQualType(type));
9063 uint32_t count = 0;
9064 bool is_complex = false;
9065 if (IsFloatingPointType(type, count, is_complex)) {
9066 // TODO: handle complex and vector types
9067 if (count != 1)
9068 return false;
9069
9070 llvm::StringRef s_sref(s);
9071 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type),
9072 s_sref);
9073
9074 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type);
9075 const uint64_t byte_size = bit_size / 8;
9076 if (dst_size >= byte_size) {
9077 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(
9078 llvm::NextPowerOf2(byte_size) * 8);
Zachary Turner97206d52017-05-12 04:51:55 +00009079 lldb_private::Status get_data_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009080 if (scalar.GetAsMemoryData(dst, byte_size,
9081 lldb_private::endian::InlHostByteOrder(),
9082 get_data_error))
9083 return byte_size;
9084 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009085 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009086 }
9087 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00009088}
9089
Greg Claytond8d4a572015-08-11 21:38:15 +00009090// Dumping types
Greg Claytond8d4a572015-08-11 21:38:15 +00009091#define DEPTH_INCREMENT 2
9092
Adrian Prantl0c72a422019-03-07 20:20:02 +00009093#ifndef NDEBUG
9094LLVM_DUMP_METHOD void
9095ClangASTContext::dump(lldb::opaque_compiler_type_t type) const {
9096 if (!type)
9097 return;
9098 clang::QualType qual_type(GetQualType(type));
9099 qual_type.dump();
9100}
9101#endif
9102
Zachary Turner49110232018-11-05 17:40:28 +00009103void ClangASTContext::Dump(Stream &s) {
Zachary Turner115209e2018-11-05 19:25:39 +00009104 Decl *tu = Decl::castFromDeclContext(GetTranslationUnitDecl());
Zachary Turner49110232018-11-05 17:40:28 +00009105 tu->dump(s.AsRawOstream());
9106}
9107
Kate Stoneb9c1b512016-09-06 20:57:50 +00009108void ClangASTContext::DumpValue(
9109 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
Zachary Turner29cb8682017-03-03 20:57:05 +00009110 lldb::Format format, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009111 lldb::offset_t data_byte_offset, size_t data_byte_size,
9112 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
9113 bool show_summary, bool verbose, uint32_t depth) {
9114 if (!type)
9115 return;
9116
9117 clang::QualType qual_type(GetQualType(type));
9118 switch (qual_type->getTypeClass()) {
9119 case clang::Type::Record:
9120 if (GetCompleteType(type)) {
9121 const clang::RecordType *record_type =
9122 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9123 const clang::RecordDecl *record_decl = record_type->getDecl();
9124 assert(record_decl);
9125 uint32_t field_bit_offset = 0;
9126 uint32_t field_byte_offset = 0;
9127 const clang::ASTRecordLayout &record_layout =
9128 getASTContext()->getASTRecordLayout(record_decl);
9129 uint32_t child_idx = 0;
9130
9131 const clang::CXXRecordDecl *cxx_record_decl =
9132 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9133 if (cxx_record_decl) {
9134 // We might have base classes to print out first
9135 clang::CXXRecordDecl::base_class_const_iterator base_class,
9136 base_class_end;
9137 for (base_class = cxx_record_decl->bases_begin(),
9138 base_class_end = cxx_record_decl->bases_end();
9139 base_class != base_class_end; ++base_class) {
9140 const clang::CXXRecordDecl *base_class_decl =
9141 llvm::cast<clang::CXXRecordDecl>(
9142 base_class->getType()->getAs<clang::RecordType>()->getDecl());
9143
9144 // Skip empty base classes
Jonas Devliegherea6682a42018-12-15 00:15:33 +00009145 if (!verbose && !ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00009146 continue;
9147
9148 if (base_class->isVirtual())
9149 field_bit_offset =
9150 record_layout.getVBaseClassOffset(base_class_decl)
9151 .getQuantity() *
9152 8;
9153 else
9154 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
9155 .getQuantity() *
9156 8;
9157 field_byte_offset = field_bit_offset / 8;
9158 assert(field_bit_offset % 8 == 0);
9159 if (child_idx == 0)
9160 s->PutChar('{');
9161 else
9162 s->PutChar(',');
9163
9164 clang::QualType base_class_qual_type = base_class->getType();
9165 std::string base_class_type_name(base_class_qual_type.getAsString());
9166
9167 // Indent and print the base class type name
Zachary Turner827d5d72016-12-16 04:27:00 +00009168 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
9169 base_class_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009170
9171 clang::TypeInfo base_class_type_info =
9172 getASTContext()->getTypeInfo(base_class_qual_type);
9173
9174 // Dump the value of the member
9175 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
9176 base_clang_type.DumpValue(
9177 exe_ctx,
9178 s, // Stream to dump to
9179 base_clang_type
9180 .GetFormat(), // The format with which to display the member
9181 data, // Data buffer containing all bytes for this type
9182 data_byte_offset + field_byte_offset, // Offset into "data" where
9183 // to grab value from
9184 base_class_type_info.Width / 8, // Size of this type in bytes
9185 0, // Bitfield bit size
9186 0, // Bitfield bit offset
9187 show_types, // Boolean indicating if we should show the variable
9188 // types
9189 show_summary, // Boolean indicating if we should show a summary
9190 // for the current type
9191 verbose, // Verbose output?
9192 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9193 // children
9194
9195 ++child_idx;
9196 }
9197 }
9198 uint32_t field_idx = 0;
9199 clang::RecordDecl::field_iterator field, field_end;
9200 for (field = record_decl->field_begin(),
9201 field_end = record_decl->field_end();
9202 field != field_end; ++field, ++field_idx, ++child_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009203 // Print the starting squiggly bracket (if this is the first member) or
9204 // comma (for member 2 and beyond) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009205 if (child_idx == 0)
9206 s->PutChar('{');
9207 else
9208 s->PutChar(',');
9209
9210 // Indent
9211 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
9212
9213 clang::QualType field_type = field->getType();
9214 // Print the member type if requested
Adrian Prantl05097242018-04-30 16:49:04 +00009215 // Figure out the type byte size (field_type_info.first) and alignment
9216 // (field_type_info.second) from the AST context.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009217 clang::TypeInfo field_type_info =
9218 getASTContext()->getTypeInfo(field_type);
9219 assert(field_idx < record_layout.getFieldCount());
9220 // Figure out the field offset within the current struct/union/class
9221 // type
9222 field_bit_offset = record_layout.getFieldOffset(field_idx);
9223 field_byte_offset = field_bit_offset / 8;
9224 uint32_t field_bitfield_bit_size = 0;
9225 uint32_t field_bitfield_bit_offset = 0;
9226 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
9227 field_bitfield_bit_size))
9228 field_bitfield_bit_offset = field_bit_offset % 8;
9229
9230 if (show_types) {
9231 std::string field_type_name(field_type.getAsString());
9232 if (field_bitfield_bit_size > 0)
9233 s->Printf("(%s:%u) ", field_type_name.c_str(),
9234 field_bitfield_bit_size);
9235 else
9236 s->Printf("(%s) ", field_type_name.c_str());
9237 }
9238 // Print the member name and equal sign
9239 s->Printf("%s = ", field->getNameAsString().c_str());
9240
9241 // Dump the value of the member
9242 CompilerType field_clang_type(getASTContext(), field_type);
9243 field_clang_type.DumpValue(
9244 exe_ctx,
9245 s, // Stream to dump to
9246 field_clang_type
9247 .GetFormat(), // The format with which to display the member
9248 data, // Data buffer containing all bytes for this type
9249 data_byte_offset + field_byte_offset, // Offset into "data" where to
9250 // grab value from
9251 field_type_info.Width / 8, // Size of this type in bytes
9252 field_bitfield_bit_size, // Bitfield bit size
9253 field_bitfield_bit_offset, // Bitfield bit offset
9254 show_types, // Boolean indicating if we should show the variable
9255 // types
9256 show_summary, // Boolean indicating if we should show a summary for
9257 // the current type
9258 verbose, // Verbose output?
9259 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9260 // children
9261 }
9262
9263 // Indent the trailing squiggly bracket
9264 if (child_idx > 0)
9265 s->Printf("\n%*s}", depth, "");
9266 }
9267 return;
9268
9269 case clang::Type::Enum:
9270 if (GetCompleteType(type)) {
9271 const clang::EnumType *enutype =
9272 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9273 const clang::EnumDecl *enum_decl = enutype->getDecl();
9274 assert(enum_decl);
9275 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9276 lldb::offset_t offset = data_byte_offset;
9277 const int64_t enum_value = data.GetMaxU64Bitfield(
9278 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
9279 for (enum_pos = enum_decl->enumerator_begin(),
9280 enum_end_pos = enum_decl->enumerator_end();
9281 enum_pos != enum_end_pos; ++enum_pos) {
9282 if (enum_pos->getInitVal() == enum_value) {
9283 s->Printf("%s", enum_pos->getNameAsString().c_str());
9284 return;
9285 }
9286 }
Adrian Prantl05097242018-04-30 16:49:04 +00009287 // If we have gotten here we didn't get find the enumerator in the enum
9288 // decl, so just print the integer.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009289 s->Printf("%" PRIi64, enum_value);
9290 }
9291 return;
9292
9293 case clang::Type::ConstantArray: {
9294 const clang::ConstantArrayType *array =
9295 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9296 bool is_array_of_characters = false;
9297 clang::QualType element_qual_type = array->getElementType();
9298
9299 const clang::Type *canonical_type =
9300 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9301 if (canonical_type)
9302 is_array_of_characters = canonical_type->isCharType();
9303
9304 const uint64_t element_count = array->getSize().getLimitedValue();
9305
9306 clang::TypeInfo field_type_info =
9307 getASTContext()->getTypeInfo(element_qual_type);
9308
9309 uint32_t element_idx = 0;
9310 uint32_t element_offset = 0;
9311 uint64_t element_byte_size = field_type_info.Width / 8;
9312 uint32_t element_stride = element_byte_size;
9313
9314 if (is_array_of_characters) {
9315 s->PutChar('"');
Zachary Turner29cb8682017-03-03 20:57:05 +00009316 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9317 element_byte_size, element_count, UINT32_MAX,
9318 LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009319 s->PutChar('"');
9320 return;
9321 } else {
9322 CompilerType element_clang_type(getASTContext(), element_qual_type);
9323 lldb::Format element_format = element_clang_type.GetFormat();
9324
9325 for (element_idx = 0; element_idx < element_count; ++element_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009326 // Print the starting squiggly bracket (if this is the first member) or
9327 // comman (for member 2 and beyong) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009328 if (element_idx == 0)
9329 s->PutChar('{');
9330 else
9331 s->PutChar(',');
9332
9333 // Indent and print the index
9334 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9335
9336 // Figure out the field offset within the current struct/union/class
9337 // type
9338 element_offset = element_idx * element_stride;
9339
9340 // Dump the value of the member
9341 element_clang_type.DumpValue(
9342 exe_ctx,
9343 s, // Stream to dump to
9344 element_format, // The format with which to display the element
9345 data, // Data buffer containing all bytes for this type
9346 data_byte_offset +
9347 element_offset, // Offset into "data" where to grab value from
9348 element_byte_size, // Size of this type in bytes
9349 0, // Bitfield bit size
9350 0, // Bitfield bit offset
9351 show_types, // Boolean indicating if we should show the variable
9352 // types
9353 show_summary, // Boolean indicating if we should show a summary for
9354 // the current type
9355 verbose, // Verbose output?
9356 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9357 // children
9358 }
9359
9360 // Indent the trailing squiggly bracket
9361 if (element_idx > 0)
9362 s->Printf("\n%*s}", depth, "");
9363 }
9364 }
9365 return;
9366
9367 case clang::Type::Typedef: {
9368 clang::QualType typedef_qual_type =
9369 llvm::cast<clang::TypedefType>(qual_type)
9370 ->getDecl()
9371 ->getUnderlyingType();
9372
9373 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9374 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9375 clang::TypeInfo typedef_type_info =
9376 getASTContext()->getTypeInfo(typedef_qual_type);
9377 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9378
9379 return typedef_clang_type.DumpValue(
9380 exe_ctx,
9381 s, // Stream to dump to
9382 typedef_format, // The format with which to display the element
9383 data, // Data buffer containing all bytes for this type
9384 data_byte_offset, // Offset into "data" where to grab value from
9385 typedef_byte_size, // Size of this type in bytes
9386 bitfield_bit_size, // Bitfield bit size
9387 bitfield_bit_offset, // Bitfield bit offset
9388 show_types, // Boolean indicating if we should show the variable types
9389 show_summary, // Boolean indicating if we should show a summary for the
9390 // current type
9391 verbose, // Verbose output?
9392 depth); // Scope depth for any types that have children
9393 } break;
9394
9395 case clang::Type::Auto: {
9396 clang::QualType elaborated_qual_type =
9397 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9398 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9399 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9400 clang::TypeInfo elaborated_type_info =
9401 getASTContext()->getTypeInfo(elaborated_qual_type);
9402 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9403
9404 return elaborated_clang_type.DumpValue(
9405 exe_ctx,
9406 s, // Stream to dump to
9407 elaborated_format, // The format with which to display the element
9408 data, // Data buffer containing all bytes for this type
9409 data_byte_offset, // Offset into "data" where to grab value from
9410 elaborated_byte_size, // Size of this type in bytes
9411 bitfield_bit_size, // Bitfield bit size
9412 bitfield_bit_offset, // Bitfield bit offset
9413 show_types, // Boolean indicating if we should show the variable types
9414 show_summary, // Boolean indicating if we should show a summary for the
9415 // current type
9416 verbose, // Verbose output?
9417 depth); // Scope depth for any types that have children
9418 } break;
9419
9420 case clang::Type::Elaborated: {
9421 clang::QualType elaborated_qual_type =
9422 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9423 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9424 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9425 clang::TypeInfo elaborated_type_info =
9426 getASTContext()->getTypeInfo(elaborated_qual_type);
9427 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9428
9429 return elaborated_clang_type.DumpValue(
9430 exe_ctx,
9431 s, // Stream to dump to
9432 elaborated_format, // The format with which to display the element
9433 data, // Data buffer containing all bytes for this type
9434 data_byte_offset, // Offset into "data" where to grab value from
9435 elaborated_byte_size, // Size of this type in bytes
9436 bitfield_bit_size, // Bitfield bit size
9437 bitfield_bit_offset, // Bitfield bit offset
9438 show_types, // Boolean indicating if we should show the variable types
9439 show_summary, // Boolean indicating if we should show a summary for the
9440 // current type
9441 verbose, // Verbose output?
9442 depth); // Scope depth for any types that have children
9443 } break;
9444
9445 case clang::Type::Paren: {
9446 clang::QualType desugar_qual_type =
9447 llvm::cast<clang::ParenType>(qual_type)->desugar();
9448 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type);
9449
9450 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9451 clang::TypeInfo desugar_type_info =
9452 getASTContext()->getTypeInfo(desugar_qual_type);
9453 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9454
9455 return desugar_clang_type.DumpValue(
9456 exe_ctx,
9457 s, // Stream to dump to
9458 desugar_format, // The format with which to display the element
9459 data, // Data buffer containing all bytes for this type
9460 data_byte_offset, // Offset into "data" where to grab value from
9461 desugar_byte_size, // Size of this type in bytes
9462 bitfield_bit_size, // Bitfield bit size
9463 bitfield_bit_offset, // Bitfield bit offset
9464 show_types, // Boolean indicating if we should show the variable types
9465 show_summary, // Boolean indicating if we should show a summary for the
9466 // current type
9467 verbose, // Verbose output?
9468 depth); // Scope depth for any types that have children
9469 } break;
9470
9471 default:
9472 // We are down to a scalar type that we just need to display.
Zachary Turner29cb8682017-03-03 20:57:05 +00009473 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9474 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9475 bitfield_bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009476
9477 if (show_summary)
9478 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9479 break;
9480 }
9481}
9482
9483bool ClangASTContext::DumpTypeValue(
9484 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
Zachary Turner29cb8682017-03-03 20:57:05 +00009485 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9486 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009487 ExecutionContextScope *exe_scope) {
9488 if (!type)
9489 return false;
9490 if (IsAggregateType(type)) {
9491 return false;
9492 } else {
Greg Claytond8d4a572015-08-11 21:38:15 +00009493 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009494
9495 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9496 switch (type_class) {
9497 case clang::Type::Typedef: {
9498 clang::QualType typedef_qual_type =
9499 llvm::cast<clang::TypedefType>(qual_type)
9500 ->getDecl()
9501 ->getUnderlyingType();
9502 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9503 if (format == eFormatDefault)
9504 format = typedef_clang_type.GetFormat();
9505 clang::TypeInfo typedef_type_info =
9506 getASTContext()->getTypeInfo(typedef_qual_type);
9507 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9508
9509 return typedef_clang_type.DumpTypeValue(
9510 s,
9511 format, // The format with which to display the element
9512 data, // Data buffer containing all bytes for this type
9513 byte_offset, // Offset into "data" where to grab value from
9514 typedef_byte_size, // Size of this type in bytes
9515 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9516 // treat as a bitfield
9517 bitfield_bit_offset, // Offset in bits of a bitfield value if
9518 // bitfield_bit_size != 0
9519 exe_scope);
9520 } break;
9521
9522 case clang::Type::Enum:
Adrian Prantl05097242018-04-30 16:49:04 +00009523 // If our format is enum or default, show the enumeration value as its
9524 // enumeration string value, else just display it as requested.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009525 if ((format == eFormatEnum || format == eFormatDefault) &&
9526 GetCompleteType(type)) {
9527 const clang::EnumType *enutype =
9528 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9529 const clang::EnumDecl *enum_decl = enutype->getDecl();
9530 assert(enum_decl);
9531 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9532 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9533 lldb::offset_t offset = byte_offset;
9534 if (is_signed) {
9535 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9536 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9537 for (enum_pos = enum_decl->enumerator_begin(),
9538 enum_end_pos = enum_decl->enumerator_end();
9539 enum_pos != enum_end_pos; ++enum_pos) {
9540 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009541 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009542 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009543 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009544 }
9545 // If we have gotten here we didn't get find the enumerator in the
9546 // enum decl, so just print the integer.
9547 s->Printf("%" PRIi64, enum_svalue);
9548 } else {
9549 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9550 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9551 for (enum_pos = enum_decl->enumerator_begin(),
9552 enum_end_pos = enum_decl->enumerator_end();
9553 enum_pos != enum_end_pos; ++enum_pos) {
9554 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009555 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009556 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009557 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009558 }
9559 // If we have gotten here we didn't get find the enumerator in the
9560 // enum decl, so just print the integer.
9561 s->Printf("%" PRIu64, enum_uvalue);
Greg Claytond8d4a572015-08-11 21:38:15 +00009562 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009563 return true;
9564 }
9565 // format was not enum, just fall through and dump the value as
9566 // requested....
9567 LLVM_FALLTHROUGH;
9568
9569 default:
9570 // We are down to a scalar type that we just need to display.
9571 {
9572 uint32_t item_count = 1;
9573 // A few formats, we might need to modify our size and count for
9574 // depending
9575 // on how we are trying to display the value...
9576 switch (format) {
Greg Claytond8d4a572015-08-11 21:38:15 +00009577 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00009578 case eFormatBoolean:
9579 case eFormatBinary:
9580 case eFormatComplex:
9581 case eFormatCString: // NULL terminated C strings
9582 case eFormatDecimal:
9583 case eFormatEnum:
9584 case eFormatHex:
9585 case eFormatHexUppercase:
9586 case eFormatFloat:
9587 case eFormatOctal:
9588 case eFormatOSType:
9589 case eFormatUnsigned:
9590 case eFormatPointer:
9591 case eFormatVectorOfChar:
9592 case eFormatVectorOfSInt8:
9593 case eFormatVectorOfUInt8:
9594 case eFormatVectorOfSInt16:
9595 case eFormatVectorOfUInt16:
9596 case eFormatVectorOfSInt32:
9597 case eFormatVectorOfUInt32:
9598 case eFormatVectorOfSInt64:
9599 case eFormatVectorOfUInt64:
9600 case eFormatVectorOfFloat32:
9601 case eFormatVectorOfFloat64:
9602 case eFormatVectorOfUInt128:
9603 break;
9604
9605 case eFormatChar:
9606 case eFormatCharPrintable:
9607 case eFormatCharArray:
9608 case eFormatBytes:
9609 case eFormatBytesWithASCII:
9610 item_count = byte_size;
9611 byte_size = 1;
9612 break;
9613
9614 case eFormatUnicode16:
9615 item_count = byte_size / 2;
9616 byte_size = 2;
9617 break;
9618
9619 case eFormatUnicode32:
9620 item_count = byte_size / 4;
9621 byte_size = 4;
9622 break;
9623 }
Zachary Turner29cb8682017-03-03 20:57:05 +00009624 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9625 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
9626 bitfield_bit_size, bitfield_bit_offset,
9627 exe_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009628 }
9629 break;
9630 }
9631 }
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00009632 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009633}
9634
9635void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9636 ExecutionContext *exe_ctx, Stream *s,
9637 const lldb_private::DataExtractor &data,
9638 lldb::offset_t data_byte_offset,
9639 size_t data_byte_size) {
9640 uint32_t length = 0;
9641 if (IsCStringType(type, length)) {
9642 if (exe_ctx) {
9643 Process *process = exe_ctx->GetProcessPtr();
9644 if (process) {
9645 lldb::offset_t offset = data_byte_offset;
9646 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9647 std::vector<uint8_t> buf;
9648 if (length > 0)
9649 buf.resize(length);
9650 else
9651 buf.resize(256);
9652
Zachary Turner29cb8682017-03-03 20:57:05 +00009653 DataExtractor cstr_data(&buf.front(), buf.size(),
9654 process->GetByteOrder(), 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009655 buf.back() = '\0';
9656 size_t bytes_read;
9657 size_t total_cstr_len = 0;
Zachary Turner97206d52017-05-12 04:51:55 +00009658 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009659 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9660 buf.size(), error)) > 0) {
9661 const size_t len = strlen((const char *)&buf.front());
9662 if (len == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00009663 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009664 if (total_cstr_len == 0)
9665 s->PutCString(" \"");
Zachary Turner29cb8682017-03-03 20:57:05 +00009666 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9667 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009668 total_cstr_len += len;
9669 if (len < buf.size())
9670 break;
9671 pointer_address += total_cstr_len;
Greg Claytond8d4a572015-08-11 21:38:15 +00009672 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009673 if (total_cstr_len > 0)
9674 s->PutChar('"');
9675 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009676 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009677 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009678}
9679
Kate Stoneb9c1b512016-09-06 20:57:50 +00009680void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9681 StreamFile s(stdout, false);
9682 DumpTypeDescription(type, &s);
9683 ClangASTMetadata *metadata =
9684 ClangASTContext::GetMetadata(getASTContext(), type);
9685 if (metadata) {
9686 metadata->Dump(&s);
9687 }
9688}
Greg Claytond8d4a572015-08-11 21:38:15 +00009689
Kate Stoneb9c1b512016-09-06 20:57:50 +00009690void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9691 Stream *s) {
9692 if (type) {
9693 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00009694
Kate Stoneb9c1b512016-09-06 20:57:50 +00009695 llvm::SmallVector<char, 1024> buf;
9696 llvm::raw_svector_ostream llvm_ostrm(buf);
9697
9698 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9699 switch (type_class) {
9700 case clang::Type::ObjCObject:
9701 case clang::Type::ObjCInterface: {
9702 GetCompleteType(type);
9703
9704 const clang::ObjCObjectType *objc_class_type =
9705 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9706 assert(objc_class_type);
9707 if (objc_class_type) {
9708 clang::ObjCInterfaceDecl *class_interface_decl =
9709 objc_class_type->getInterface();
9710 if (class_interface_decl) {
9711 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9712 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
Greg Claytond8d4a572015-08-11 21:38:15 +00009713 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009714 }
9715 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00009716
Kate Stoneb9c1b512016-09-06 20:57:50 +00009717 case clang::Type::Typedef: {
9718 const clang::TypedefType *typedef_type =
9719 qual_type->getAs<clang::TypedefType>();
9720 if (typedef_type) {
9721 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9722 std::string clang_typedef_name(
9723 typedef_decl->getQualifiedNameAsString());
9724 if (!clang_typedef_name.empty()) {
9725 s->PutCString("typedef ");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009726 s->PutCString(clang_typedef_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00009727 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009728 }
9729 } break;
9730
9731 case clang::Type::Auto:
9732 CompilerType(getASTContext(),
9733 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
9734 .DumpTypeDescription(s);
9735 return;
9736
9737 case clang::Type::Elaborated:
9738 CompilerType(getASTContext(),
9739 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
9740 .DumpTypeDescription(s);
9741 return;
9742
9743 case clang::Type::Paren:
9744 CompilerType(getASTContext(),
9745 llvm::cast<clang::ParenType>(qual_type)->desugar())
9746 .DumpTypeDescription(s);
9747 return;
9748
9749 case clang::Type::Record: {
9750 GetCompleteType(type);
9751
9752 const clang::RecordType *record_type =
9753 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9754 const clang::RecordDecl *record_decl = record_type->getDecl();
9755 const clang::CXXRecordDecl *cxx_record_decl =
9756 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9757
9758 if (cxx_record_decl)
9759 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9760 s->GetIndentLevel());
9761 else
9762 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9763 s->GetIndentLevel());
9764 } break;
9765
9766 default: {
9767 const clang::TagType *tag_type =
9768 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9769 if (tag_type) {
9770 clang::TagDecl *tag_decl = tag_type->getDecl();
9771 if (tag_decl)
9772 tag_decl->print(llvm_ostrm, 0);
9773 } else {
9774 std::string clang_type_name(qual_type.getAsString());
9775 if (!clang_type_name.empty())
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009776 s->PutCString(clang_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009777 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009778 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00009779 }
9780
Kate Stoneb9c1b512016-09-06 20:57:50 +00009781 if (buf.size() > 0) {
9782 s->Write(buf.data(), buf.size());
Greg Clayton8b4edba2015-08-14 20:02:05 +00009783 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009784 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00009785}
9786
Kate Stoneb9c1b512016-09-06 20:57:50 +00009787void ClangASTContext::DumpTypeName(const CompilerType &type) {
9788 if (ClangUtil::IsClangType(type)) {
9789 clang::QualType qual_type(
9790 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9791
9792 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9793 switch (type_class) {
9794 case clang::Type::Record: {
9795 const clang::CXXRecordDecl *cxx_record_decl =
9796 qual_type->getAsCXXRecordDecl();
9797 if (cxx_record_decl)
9798 printf("class %s", cxx_record_decl->getName().str().c_str());
9799 } break;
9800
9801 case clang::Type::Enum: {
9802 clang::EnumDecl *enum_decl =
9803 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9804 if (enum_decl) {
9805 printf("enum %s", enum_decl->getName().str().c_str());
9806 }
9807 } break;
9808
9809 case clang::Type::ObjCObject:
9810 case clang::Type::ObjCInterface: {
9811 const clang::ObjCObjectType *objc_class_type =
9812 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9813 if (objc_class_type) {
9814 clang::ObjCInterfaceDecl *class_interface_decl =
9815 objc_class_type->getInterface();
Adrian Prantl05097242018-04-30 16:49:04 +00009816 // We currently can't complete objective C types through the newly
9817 // added ASTContext because it only supports TagDecl objects right
9818 // now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00009819 if (class_interface_decl)
9820 printf("@class %s", class_interface_decl->getName().str().c_str());
9821 }
9822 } break;
9823
9824 case clang::Type::Typedef:
9825 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9826 ->getDecl()
9827 ->getName()
9828 .str()
9829 .c_str());
9830 break;
9831
9832 case clang::Type::Auto:
9833 printf("auto ");
9834 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9835 llvm::cast<clang::AutoType>(qual_type)
9836 ->getDeducedType()
9837 .getAsOpaquePtr()));
9838
9839 case clang::Type::Elaborated:
9840 printf("elaborated ");
9841 return DumpTypeName(CompilerType(
9842 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9843 ->getNamedType()
9844 .getAsOpaquePtr()));
9845
9846 case clang::Type::Paren:
9847 printf("paren ");
9848 return DumpTypeName(CompilerType(
9849 type.GetTypeSystem(),
9850 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9851
9852 default:
9853 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9854 break;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009855 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009856 }
Greg Clayton6dc8d582015-08-18 22:32:36 +00009857}
9858
Kate Stoneb9c1b512016-09-06 20:57:50 +00009859clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9860 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9861 const char *parent_name, int tag_decl_kind,
9862 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9863 if (template_param_infos.IsValid()) {
9864 std::string template_basename(parent_name);
9865 template_basename.erase(template_basename.find('<'));
9866
9867 return CreateClassTemplateDecl(decl_ctx, access_type,
9868 template_basename.c_str(), tag_decl_kind,
9869 template_param_infos);
9870 }
Konrad Kleine248a1302019-05-23 11:14:47 +00009871 return nullptr;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009872}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009873
Kate Stoneb9c1b512016-09-06 20:57:50 +00009874void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9875 ClangASTContext *ast = (ClangASTContext *)baton;
9876 SymbolFile *sym_file = ast->GetSymbolFile();
9877 if (sym_file) {
9878 CompilerType clang_type = GetTypeForDecl(decl);
9879 if (clang_type)
9880 sym_file->CompleteType(clang_type);
9881 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00009882}
9883
Kate Stoneb9c1b512016-09-06 20:57:50 +00009884void ClangASTContext::CompleteObjCInterfaceDecl(
9885 void *baton, clang::ObjCInterfaceDecl *decl) {
9886 ClangASTContext *ast = (ClangASTContext *)baton;
9887 SymbolFile *sym_file = ast->GetSymbolFile();
9888 if (sym_file) {
9889 CompilerType clang_type = GetTypeForDecl(decl);
9890 if (clang_type)
9891 sym_file->CompleteType(clang_type);
9892 }
Zachary Turner42dff792016-04-15 00:21:26 +00009893}
Greg Clayton261ac3f2015-08-28 01:01:03 +00009894
Kate Stoneb9c1b512016-09-06 20:57:50 +00009895DWARFASTParser *ClangASTContext::GetDWARFParser() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009896 if (!m_dwarf_ast_parser_up)
9897 m_dwarf_ast_parser_up.reset(new DWARFASTParserClang(*this));
9898 return m_dwarf_ast_parser_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00009899}
9900
9901PDBASTParser *ClangASTContext::GetPDBParser() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009902 if (!m_pdb_ast_parser_up)
9903 m_pdb_ast_parser_up.reset(new PDBASTParser(*this));
9904 return m_pdb_ast_parser_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00009905}
9906
9907bool ClangASTContext::LayoutRecordType(
9908 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9909 uint64_t &alignment,
9910 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9911 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9912 &base_offsets,
9913 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9914 &vbase_offsets) {
9915 ClangASTContext *ast = (ClangASTContext *)baton;
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00009916 lldb_private::ClangASTImporter *importer = nullptr;
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009917 if (ast->m_dwarf_ast_parser_up)
9918 importer = &ast->m_dwarf_ast_parser_up->GetClangASTImporter();
9919 if (!importer && ast->m_pdb_ast_parser_up)
9920 importer = &ast->m_pdb_ast_parser_up->GetClangASTImporter();
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00009921 if (!importer)
9922 return false;
9923
9924 return importer->LayoutRecordType(record_decl, bit_size, alignment,
9925 field_offsets, base_offsets, vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009926}
9927
Paul Hermand628cbb2015-09-15 23:44:17 +00009928// CompilerDecl override functions
Paul Hermand628cbb2015-09-15 23:44:17 +00009929
Kate Stoneb9c1b512016-09-06 20:57:50 +00009930ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9931 if (opaque_decl) {
9932 clang::NamedDecl *nd =
9933 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9934 if (nd != nullptr)
9935 return ConstString(nd->getDeclName().getAsString());
9936 }
9937 return ConstString();
Paul Hermand628cbb2015-09-15 23:44:17 +00009938}
9939
Kate Stoneb9c1b512016-09-06 20:57:50 +00009940ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9941 if (opaque_decl) {
9942 clang::NamedDecl *nd =
9943 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9944 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9945 clang::MangleContext *mc = getMangleContext();
9946 if (mc && mc->shouldMangleCXXName(nd)) {
9947 llvm::SmallVector<char, 1024> buf;
9948 llvm::raw_svector_ostream llvm_ostrm(buf);
9949 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9950 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9951 Ctor_Complete, llvm_ostrm);
9952 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9953 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9954 Dtor_Complete, llvm_ostrm);
9955 } else {
9956 mc->mangleName(nd, llvm_ostrm);
Greg Claytonfe689042015-11-10 17:47:04 +00009957 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009958 if (buf.size() > 0)
9959 return ConstString(buf.data(), buf.size());
9960 }
Greg Claytonfe689042015-11-10 17:47:04 +00009961 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009962 }
9963 return ConstString();
Greg Claytonfe689042015-11-10 17:47:04 +00009964}
9965
Kate Stoneb9c1b512016-09-06 20:57:50 +00009966CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9967 if (opaque_decl)
9968 return CompilerDeclContext(this,
9969 ((clang::Decl *)opaque_decl)->getDeclContext());
9970 else
9971 return CompilerDeclContext();
Greg Claytonfe689042015-11-10 17:47:04 +00009972}
9973
Kate Stoneb9c1b512016-09-06 20:57:50 +00009974CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9975 if (clang::FunctionDecl *func_decl =
9976 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9977 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9978 if (clang::ObjCMethodDecl *objc_method =
9979 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9980 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9981 else
Greg Claytonfe689042015-11-10 17:47:04 +00009982 return CompilerType();
9983}
9984
Kate Stoneb9c1b512016-09-06 20:57:50 +00009985size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9986 if (clang::FunctionDecl *func_decl =
9987 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9988 return func_decl->param_size();
9989 if (clang::ObjCMethodDecl *objc_method =
9990 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9991 return objc_method->param_size();
9992 else
9993 return 0;
9994}
9995
9996CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
9997 size_t idx) {
9998 if (clang::FunctionDecl *func_decl =
9999 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
10000 if (idx < func_decl->param_size()) {
10001 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
10002 if (var_decl)
10003 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
10004 }
10005 } else if (clang::ObjCMethodDecl *objc_method =
10006 llvm::dyn_cast<clang::ObjCMethodDecl>(
10007 (clang::Decl *)opaque_decl)) {
10008 if (idx < objc_method->param_size())
10009 return CompilerType(
10010 this,
10011 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
10012 }
10013 return CompilerType();
10014}
10015
Greg Clayton99558cc42015-08-24 23:46:31 +000010016// CompilerDeclContext functions
Greg Clayton99558cc42015-08-24 23:46:31 +000010017
Kate Stoneb9c1b512016-09-06 20:57:50 +000010018std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
10019 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
10020 std::vector<CompilerDecl> found_decls;
10021 if (opaque_decl_ctx) {
10022 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
10023 std::set<DeclContext *> searched;
10024 std::multimap<DeclContext *, DeclContext *> search_queue;
10025 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +000010026
Kate Stoneb9c1b512016-09-06 20:57:50 +000010027 for (clang::DeclContext *decl_context = root_decl_ctx;
10028 decl_context != nullptr && found_decls.empty();
10029 decl_context = decl_context->getParent()) {
10030 search_queue.insert(std::make_pair(decl_context, decl_context));
Paul Hermand628cbb2015-09-15 23:44:17 +000010031
Kate Stoneb9c1b512016-09-06 20:57:50 +000010032 for (auto it = search_queue.find(decl_context); it != search_queue.end();
10033 it++) {
10034 if (!searched.insert(it->second).second)
10035 continue;
10036 symbol_file->ParseDeclsForContext(
10037 CompilerDeclContext(this, it->second));
Paul Hermanea188fc2015-09-16 18:48:30 +000010038
Kate Stoneb9c1b512016-09-06 20:57:50 +000010039 for (clang::Decl *child : it->second->decls()) {
10040 if (clang::UsingDirectiveDecl *ud =
10041 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10042 if (ignore_using_decls)
10043 continue;
10044 clang::DeclContext *from = ud->getCommonAncestor();
10045 if (searched.find(ud->getNominatedNamespace()) == searched.end())
10046 search_queue.insert(
10047 std::make_pair(from, ud->getNominatedNamespace()));
10048 } else if (clang::UsingDecl *ud =
10049 llvm::dyn_cast<clang::UsingDecl>(child)) {
10050 if (ignore_using_decls)
10051 continue;
10052 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10053 clang::Decl *target = usd->getTargetDecl();
10054 if (clang::NamedDecl *nd =
10055 llvm::dyn_cast<clang::NamedDecl>(target)) {
10056 IdentifierInfo *ii = nd->getIdentifier();
10057 if (ii != nullptr &&
10058 ii->getName().equals(name.AsCString(nullptr)))
10059 found_decls.push_back(CompilerDecl(this, nd));
10060 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010061 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010062 } else if (clang::NamedDecl *nd =
10063 llvm::dyn_cast<clang::NamedDecl>(child)) {
10064 IdentifierInfo *ii = nd->getIdentifier();
10065 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
10066 found_decls.push_back(CompilerDecl(this, nd));
10067 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010068 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010069 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010070 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010071 }
10072 return found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +000010073}
10074
Dawn Perchikb5925782015-12-12 19:31:41 +000010075// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010076// and return the number of levels it took to find it, or
Adrian Prantl05097242018-04-30 16:49:04 +000010077// LLDB_INVALID_DECL_LEVEL if not found. If the decl was imported via a using
10078// declaration, its name and/or type, if set, will be used to check that the
10079// decl found in the scope is a match.
Dawn Perchikb5925782015-12-12 19:31:41 +000010080//
Kate Stoneb9c1b512016-09-06 20:57:50 +000010081// The optional name is required by languages (like C++) to handle using
Adrian Prantl05097242018-04-30 16:49:04 +000010082// declarations like:
Dawn Perchikb5925782015-12-12 19:31:41 +000010083//
10084// void poo();
10085// namespace ns {
10086// void foo();
10087// void goo();
10088// }
10089// void bar() {
10090// using ns::foo;
10091// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
10092// // LLDB_INVALID_DECL_LEVEL for 'goo'.
10093// }
10094//
10095// The optional type is useful in the case that there's a specific overload
10096// that we're looking for that might otherwise be shadowed, like:
10097//
10098// void foo(int);
10099// namespace ns {
10100// void foo();
10101// }
10102// void bar() {
10103// using ns::foo;
10104// // CountDeclLevels returns 0 for { 'foo', void() },
10105// // 1 for { 'foo', void(int) }, and
10106// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
10107// }
10108//
10109// NOTE: Because file statics are at the TranslationUnit along with globals, a
Kate Stoneb9c1b512016-09-06 20:57:50 +000010110// function at file scope will return the same level as a function at global
Adrian Prantl05097242018-04-30 16:49:04 +000010111// scope. Ideally we'd like to treat the file scope as an additional scope just
10112// below the global scope. More work needs to be done to recognise that, if
10113// the decl we're trying to look up is static, we should compare its source
10114// file with that of the current scope and return a lower number for it.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010115uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
10116 clang::DeclContext *child_decl_ctx,
10117 ConstString *child_name,
10118 CompilerType *child_type) {
10119 if (frame_decl_ctx) {
10120 std::set<DeclContext *> searched;
10121 std::multimap<DeclContext *, DeclContext *> search_queue;
10122 SymbolFile *symbol_file = GetSymbolFile();
Dawn Perchikb5925782015-12-12 19:31:41 +000010123
Kate Stoneb9c1b512016-09-06 20:57:50 +000010124 // Get the lookup scope for the decl we're trying to find.
10125 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
Dawn Perchikb5925782015-12-12 19:31:41 +000010126
Kate Stoneb9c1b512016-09-06 20:57:50 +000010127 // Look for it in our scope's decl context and its parents.
10128 uint32_t level = 0;
10129 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
10130 decl_ctx = decl_ctx->getParent()) {
10131 if (!decl_ctx->isLookupContext())
10132 continue;
10133 if (decl_ctx == parent_decl_ctx)
10134 // Found it!
10135 return level;
10136 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
10137 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
10138 it++) {
10139 if (searched.find(it->second) != searched.end())
10140 continue;
10141
10142 // Currently DWARF has one shared translation unit for all Decls at top
Adrian Prantl05097242018-04-30 16:49:04 +000010143 // level, so this would erroneously find using statements anywhere. So
10144 // don't look at the top-level translation unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010145 // TODO fix this and add a testcase that depends on it.
10146
10147 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
10148 continue;
10149
10150 searched.insert(it->second);
10151 symbol_file->ParseDeclsForContext(
10152 CompilerDeclContext(this, it->second));
10153
10154 for (clang::Decl *child : it->second->decls()) {
10155 if (clang::UsingDirectiveDecl *ud =
10156 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10157 clang::DeclContext *ns = ud->getNominatedNamespace();
10158 if (ns == parent_decl_ctx)
10159 // Found it!
10160 return level;
10161 clang::DeclContext *from = ud->getCommonAncestor();
10162 if (searched.find(ns) == searched.end())
10163 search_queue.insert(std::make_pair(from, ns));
10164 } else if (child_name) {
10165 if (clang::UsingDecl *ud =
10166 llvm::dyn_cast<clang::UsingDecl>(child)) {
10167 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10168 clang::Decl *target = usd->getTargetDecl();
10169 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
10170 if (!nd)
10171 continue;
10172 // Check names.
10173 IdentifierInfo *ii = nd->getIdentifier();
10174 if (ii == nullptr ||
10175 !ii->getName().equals(child_name->AsCString(nullptr)))
10176 continue;
10177 // Check types, if one was provided.
10178 if (child_type) {
10179 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
10180 if (!AreTypesSame(clang_type, *child_type,
10181 /*ignore_qualifiers=*/true))
10182 continue;
10183 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010184 // Found it!
10185 return level;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010186 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010187 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010188 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010189 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010190 }
10191 ++level;
Dawn Perchikb5925782015-12-12 19:31:41 +000010192 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010193 }
10194 return LLDB_INVALID_DECL_LEVEL;
Dawn Perchikb5925782015-12-12 19:31:41 +000010195}
10196
Kate Stoneb9c1b512016-09-06 20:57:50 +000010197bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
10198 if (opaque_decl_ctx)
10199 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
10200 else
Greg Clayton99558cc42015-08-24 23:46:31 +000010201 return false;
10202}
10203
Kate Stoneb9c1b512016-09-06 20:57:50 +000010204ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
10205 if (opaque_decl_ctx) {
10206 clang::NamedDecl *named_decl =
10207 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10208 if (named_decl)
10209 return ConstString(named_decl->getName());
10210 }
10211 return ConstString();
Greg Clayton99558cc42015-08-24 23:46:31 +000010212}
10213
Kate Stoneb9c1b512016-09-06 20:57:50 +000010214ConstString
10215ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
10216 if (opaque_decl_ctx) {
10217 clang::NamedDecl *named_decl =
10218 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10219 if (named_decl)
10220 return ConstString(
10221 llvm::StringRef(named_decl->getQualifiedNameAsString()));
10222 }
10223 return ConstString();
10224}
10225
10226bool ClangASTContext::DeclContextIsClassMethod(
10227 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
10228 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
10229 if (opaque_decl_ctx) {
10230 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10231 if (ObjCMethodDecl *objc_method =
10232 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
10233 if (is_instance_method_ptr)
10234 *is_instance_method_ptr = objc_method->isInstanceMethod();
10235 if (language_ptr)
10236 *language_ptr = eLanguageTypeObjC;
10237 if (language_object_name_ptr)
10238 language_object_name_ptr->SetCString("self");
10239 return true;
10240 } else if (CXXMethodDecl *cxx_method =
10241 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
10242 if (is_instance_method_ptr)
10243 *is_instance_method_ptr = cxx_method->isInstance();
10244 if (language_ptr)
10245 *language_ptr = eLanguageTypeC_plus_plus;
10246 if (language_object_name_ptr)
10247 language_object_name_ptr->SetCString("this");
10248 return true;
10249 } else if (clang::FunctionDecl *function_decl =
10250 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
10251 ClangASTMetadata *metadata =
10252 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
10253 if (metadata && metadata->HasObjectPtr()) {
10254 if (is_instance_method_ptr)
10255 *is_instance_method_ptr = true;
10256 if (language_ptr)
10257 *language_ptr = eLanguageTypeObjC;
10258 if (language_object_name_ptr)
10259 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
10260 return true;
10261 }
10262 }
10263 }
10264 return false;
10265}
10266
Raphael Isemanna9469972019-03-12 07:45:04 +000010267bool ClangASTContext::DeclContextIsContainedInLookup(
10268 void *opaque_decl_ctx, void *other_opaque_decl_ctx) {
10269 auto *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10270 auto *other = (clang::DeclContext *)other_opaque_decl_ctx;
10271
10272 do {
10273 // A decl context always includes its own contents in its lookup.
10274 if (decl_ctx == other)
10275 return true;
10276
10277 // If we have an inline namespace, then the lookup of the parent context
10278 // also includes the inline namespace contents.
10279 } while (other->isInlineNamespace() && (other = other->getParent()));
10280
10281 return false;
10282}
10283
Kate Stoneb9c1b512016-09-06 20:57:50 +000010284clang::DeclContext *
10285ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
10286 if (dc.IsClang())
10287 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
10288 return nullptr;
10289}
Greg Clayton99558cc42015-08-24 23:46:31 +000010290
10291ObjCMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010292ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
10293 if (dc.IsClang())
10294 return llvm::dyn_cast<clang::ObjCMethodDecl>(
10295 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10296 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010297}
10298
10299CXXMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010300ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10301 if (dc.IsClang())
10302 return llvm::dyn_cast<clang::CXXMethodDecl>(
10303 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10304 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010305}
10306
10307clang::FunctionDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010308ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10309 if (dc.IsClang())
10310 return llvm::dyn_cast<clang::FunctionDecl>(
10311 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10312 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010313}
10314
10315clang::NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010316ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10317 if (dc.IsClang())
10318 return llvm::dyn_cast<clang::NamespaceDecl>(
10319 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10320 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010321}
10322
10323ClangASTMetadata *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010324ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10325 const void *object) {
10326 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10327 if (ast)
10328 return ClangASTContext::GetMetadata(ast, object);
10329 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010330}
10331
10332clang::ASTContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010333ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10334 ClangASTContext *ast =
10335 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10336 if (ast)
10337 return ast->getASTContext();
10338 return nullptr;
10339}
10340
10341ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10342 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10343 m_target_wp(target.shared_from_this()),
10344 m_persistent_variables(new ClangPersistentVariables) {}
10345
10346UserExpression *ClangASTContextForExpressions::GetUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010347 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010348 Expression::ResultType desired_type,
Aleksandr Urakov40624a02019-02-05 09:14:36 +000010349 const EvaluateExpressionOptions &options,
10350 ValueObject *ctx_obj) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000010351 TargetSP target_sp = m_target_wp.lock();
10352 if (!target_sp)
Greg Clayton99558cc42015-08-24 23:46:31 +000010353 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010354
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010355 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
Aleksandr Urakov40624a02019-02-05 09:14:36 +000010356 desired_type, options, ctx_obj);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010357}
10358
Kate Stoneb9c1b512016-09-06 20:57:50 +000010359FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10360 const CompilerType &return_type, const Address &function_address,
10361 const ValueList &arg_value_list, const char *name) {
10362 TargetSP target_sp = m_target_wp.lock();
10363 if (!target_sp)
10364 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010365
Kate Stoneb9c1b512016-09-06 20:57:50 +000010366 Process *process = target_sp->GetProcessSP().get();
10367 if (!process)
10368 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010369
Kate Stoneb9c1b512016-09-06 20:57:50 +000010370 return new ClangFunctionCaller(*process, return_type, function_address,
10371 arg_value_list, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010372}
10373
10374UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010375ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10376 const char *name) {
10377 TargetSP target_sp = m_target_wp.lock();
10378 if (!target_sp)
10379 return nullptr;
10380
10381 return new ClangUtilityFunction(*target_sp.get(), text, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010382}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010383
10384PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010385ClangASTContextForExpressions::GetPersistentExpressionState() {
10386 return m_persistent_variables.get();
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010387}
Sean Callanan68e44232017-09-28 20:20:25 +000010388
10389clang::ExternalASTMerger &
10390ClangASTContextForExpressions::GetMergerUnchecked() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +000010391 lldbassert(m_scratch_ast_source_up != nullptr);
10392 return m_scratch_ast_source_up->GetMergerUnchecked();
Sean Callanan68e44232017-09-28 20:20:25 +000010393}