blob: 205523355ce0b67079289e39ad538ad651eff81e [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ClangASTContext.cpp -------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Eli Friedman932197d2010-06-13 19:06:42 +00009#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010
Zachary Turner827d5d72016-12-16 04:27:00 +000011#include "llvm/Support/FormatAdapters.h"
12#include "llvm/Support/FormatVariadic.h"
13
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000014#include <mutex>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include <string>
Sean Callananfe38c852015-10-08 23:07:53 +000016#include <vector>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017
Greg Clayton6beaaa62011-01-17 03:46:26 +000018
Kate Stoneb9c1b512016-09-06 20:57:50 +000019// Clang headers like to use NDEBUG inside of them to enable/disable debug
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +000020// related features using "#ifndef NDEBUG" preprocessor blocks to do one thing
Greg Clayton6beaaa62011-01-17 03:46:26 +000021// or another. This is bad because it means that if clang was built in release
22// mode, it assumes that you are building in release mode which is not always
23// the case. You can end up with functions that are defined as empty in header
24// files when NDEBUG is not defined, and this can cause link errors with the
25// clang .a files that you have since you might be missing functions in the .a
26// file. So we have to define NDEBUG when including clang headers to avoid any
27// mismatches. This is covered by rdar://problem/8691220
28
Sean Callanan3b1d4f62011-10-26 17:46:51 +000029#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
Greg Clayton6beaaa62011-01-17 03:46:26 +000030#define LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000031#define NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000032// Need to include assert.h so it is as clang would expect it to be (disabled)
33#include <assert.h>
34#endif
35
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "clang/AST/ASTContext.h"
37#include "clang/AST/ASTImporter.h"
Greg Claytonf74c4032012-12-03 18:29:55 +000038#include "clang/AST/Attr.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "clang/AST/CXXInheritance.h"
Greg Clayton8cf05932010-07-22 18:30:50 +000040#include "clang/AST/DeclObjC.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000041#include "clang/AST/DeclTemplate.h"
Greg Claytonfe689042015-11-10 17:47:04 +000042#include "clang/AST/Mangle.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "clang/AST/RecordLayout.h"
44#include "clang/AST/Type.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000045#include "clang/AST/VTableBuilder.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046#include "clang/Basic/Builtins.h"
Sean Callanan7e2863b2012-02-06 21:28:03 +000047#include "clang/Basic/Diagnostic.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048#include "clang/Basic/FileManager.h"
Sean Callanan79439e82010-11-18 02:56:27 +000049#include "clang/Basic/FileSystemOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050#include "clang/Basic/SourceManager.h"
51#include "clang/Basic/TargetInfo.h"
52#include "clang/Basic/TargetOptions.h"
53#include "clang/Frontend/FrontendOptions.h"
54#include "clang/Frontend/LangStandard.h"
Raphael Isemannf74a4c12019-04-30 08:41:35 +000055#include "clang/Sema/Sema.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000056
57#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000058#undef NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000059#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
60// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
61#include <assert.h>
62#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063
Greg Claytond8d4a572015-08-11 21:38:15 +000064#include "llvm/Support/Signals.h"
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000065#include "llvm/Support/Threading.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000066
Zachary Turnerd133f6a2016-03-28 22:53:41 +000067#include "Plugins/ExpressionParser/Clang/ClangFunctionCaller.h"
68#include "Plugins/ExpressionParser/Clang/ClangUserExpression.h"
69#include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000070#include "lldb/Utility/ArchSpec.h"
Zachary Turner01c32432017-02-14 19:06:07 +000071#include "lldb/Utility/Flags.h"
72
Zachary Turner29cb8682017-03-03 20:57:05 +000073#include "lldb/Core/DumpDataExtractor.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000074#include "lldb/Core/Module.h"
75#include "lldb/Core/PluginManager.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000076#include "lldb/Core/StreamFile.h"
Enrico Granata2267ad42014-09-16 17:28:40 +000077#include "lldb/Core/ThreadSafeDenseMap.h"
Greg Clayton57ee3062013-07-11 22:46:58 +000078#include "lldb/Core/UniqueCStringMap.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000079#include "lldb/Symbol/ClangASTContext.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000080#include "lldb/Symbol/ClangASTImporter.h"
Greg Clayton6dc8d582015-08-18 22:32:36 +000081#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000082#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000083#include "lldb/Symbol/ClangUtil.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000084#include "lldb/Symbol/ObjectFile.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000085#include "lldb/Symbol/SymbolFile.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000086#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000087#include "lldb/Target/ExecutionContext.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000088#include "lldb/Target/Language.h"
Jim Ingham151c0322015-09-15 21:13:50 +000089#include "lldb/Target/Process.h"
90#include "lldb/Target/Target.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000091#include "lldb/Utility/DataExtractor.h"
Sean Callananc530ba92016-05-02 21:15:31 +000092#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000093#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000094#include "lldb/Utility/RegularExpression.h"
Pavel Labathd821c992018-08-07 11:07:21 +000095#include "lldb/Utility/Scalar.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000096
Alex Langfordb57017102019-07-15 22:56:12 +000097#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000098#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
Zachary Turner42dff792016-04-15 00:21:26 +000099#include "Plugins/SymbolFile/PDB/PDBASTParser.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +0000100
Eli Friedman932197d2010-06-13 19:06:42 +0000101#include <stdio.h>
102
Greg Clayton1341baf2013-07-11 23:36:31 +0000103#include <mutex>
104
Greg Claytonc86103d2010-08-05 01:57:25 +0000105using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106using namespace lldb_private;
107using namespace llvm;
108using namespace clang;
109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110namespace {
111static inline bool
112ClangASTContextSupportsLanguage(lldb::LanguageType language) {
113 return language == eLanguageTypeUnknown || // Clang is the default type system
114 Language::LanguageIsC(language) ||
115 Language::LanguageIsCPlusPlus(language) ||
116 Language::LanguageIsObjC(language) ||
117 Language::LanguageIsPascal(language) ||
118 // Use Clang for Rust until there is a proper language plugin for it
119 language == eLanguageTypeRust ||
Johan Engelen04799572016-11-25 11:01:12 +0000120 language == eLanguageTypeExtRenderScript ||
121 // Use Clang for D until there is a proper language plugin for it
Bruce Mitchenerb8233f82018-11-27 05:37:27 +0000122 language == eLanguageTypeD ||
123 // Open Dylan compiler debug info is designed to be Clang-compatible
124 language == eLanguageTypeDylan;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125}
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000126
127// Checks whether m1 is an overload of m2 (as opposed to an override). This is
128// called by addOverridesForMethod to distinguish overrides (which share a
129// vtable entry) from overloads (which require distinct entries).
130bool isOverload(clang::CXXMethodDecl *m1, clang::CXXMethodDecl *m2) {
131 // FIXME: This should detect covariant return types, but currently doesn't.
132 lldbassert(&m1->getASTContext() == &m2->getASTContext() &&
133 "Methods should have the same AST context");
134 clang::ASTContext &context = m1->getASTContext();
135
136 const auto *m1Type = llvm::cast<clang::FunctionProtoType>(
137 context.getCanonicalType(m1->getType()));
138
139 const auto *m2Type = llvm::cast<clang::FunctionProtoType>(
140 context.getCanonicalType(m2->getType()));
141
142 auto compareArgTypes = [&context](const clang::QualType &m1p,
143 const clang::QualType &m2p) {
144 return context.hasSameType(m1p.getUnqualifiedType(),
145 m2p.getUnqualifiedType());
146 };
147
148 // FIXME: In C++14 and later, we can just pass m2Type->param_type_end()
149 // as a fourth parameter to std::equal().
150 return (m1->getNumParams() != m2->getNumParams()) ||
151 !std::equal(m1Type->param_type_begin(), m1Type->param_type_end(),
152 m2Type->param_type_begin(), compareArgTypes);
153}
154
155// If decl is a virtual method, walk the base classes looking for methods that
156// decl overrides. This table of overridden methods is used by IRGen to
157// determine the vtable layout for decl's parent class.
158void addOverridesForMethod(clang::CXXMethodDecl *decl) {
159 if (!decl->isVirtual())
160 return;
161
162 clang::CXXBasePaths paths;
163
164 auto find_overridden_methods =
165 [decl](const clang::CXXBaseSpecifier *specifier,
166 clang::CXXBasePath &path) {
167 if (auto *base_record = llvm::dyn_cast<clang::CXXRecordDecl>(
168 specifier->getType()->getAs<clang::RecordType>()->getDecl())) {
169
170 clang::DeclarationName name = decl->getDeclName();
171
172 // If this is a destructor, check whether the base class destructor is
173 // virtual.
174 if (name.getNameKind() == clang::DeclarationName::CXXDestructorName)
175 if (auto *baseDtorDecl = base_record->getDestructor()) {
176 if (baseDtorDecl->isVirtual()) {
177 path.Decls = baseDtorDecl;
178 return true;
179 } else
180 return false;
181 }
182
183 // Otherwise, search for name in the base class.
184 for (path.Decls = base_record->lookup(name); !path.Decls.empty();
185 path.Decls = path.Decls.slice(1)) {
186 if (auto *method_decl =
187 llvm::dyn_cast<clang::CXXMethodDecl>(path.Decls.front()))
188 if (method_decl->isVirtual() && !isOverload(decl, method_decl)) {
189 path.Decls = method_decl;
190 return true;
191 }
192 }
193 }
194
195 return false;
196 };
197
198 if (decl->getParent()->lookupInBases(find_overridden_methods, paths)) {
199 for (auto *overridden_decl : paths.found_decls())
200 decl->addOverriddenMethod(
201 llvm::cast<clang::CXXMethodDecl>(overridden_decl));
202 }
203}
Greg Clayton56939cb2015-09-17 22:23:34 +0000204}
205
Aleksandr Urakov1dc51db2018-11-12 16:23:50 +0000206static lldb::addr_t GetVTableAddress(Process &process,
207 VTableContextBase &vtable_ctx,
208 ValueObject &valobj,
209 const ASTRecordLayout &record_layout) {
210 // Retrieve type info
211 CompilerType pointee_type;
212 CompilerType this_type(valobj.GetCompilerType());
213 uint32_t type_info = this_type.GetTypeInfo(&pointee_type);
214 if (!type_info)
215 return LLDB_INVALID_ADDRESS;
216
217 // Check if it's a pointer or reference
218 bool ptr_or_ref = false;
219 if (type_info & (eTypeIsPointer | eTypeIsReference)) {
220 ptr_or_ref = true;
221 type_info = pointee_type.GetTypeInfo();
222 }
223
224 // We process only C++ classes
225 const uint32_t cpp_class = eTypeIsClass | eTypeIsCPlusPlus;
226 if ((type_info & cpp_class) != cpp_class)
227 return LLDB_INVALID_ADDRESS;
228
229 // Calculate offset to VTable pointer
230 lldb::offset_t vbtable_ptr_offset =
231 vtable_ctx.isMicrosoft() ? record_layout.getVBPtrOffset().getQuantity()
232 : 0;
233
234 if (ptr_or_ref) {
235 // We have a pointer / ref to object, so read
236 // VTable pointer from process memory
237
238 if (valobj.GetAddressTypeOfChildren() != eAddressTypeLoad)
239 return LLDB_INVALID_ADDRESS;
240
241 auto vbtable_ptr_addr = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
242 if (vbtable_ptr_addr == LLDB_INVALID_ADDRESS)
243 return LLDB_INVALID_ADDRESS;
244
245 vbtable_ptr_addr += vbtable_ptr_offset;
246
247 Status err;
248 return process.ReadPointerFromMemory(vbtable_ptr_addr, err);
249 }
250
251 // We have an object already read from process memory,
252 // so just extract VTable pointer from it
253
254 DataExtractor data;
255 Status err;
256 auto size = valobj.GetData(data, err);
257 if (err.Fail() || vbtable_ptr_offset + data.GetAddressByteSize() > size)
258 return LLDB_INVALID_ADDRESS;
259
260 return data.GetPointer(&vbtable_ptr_offset);
261}
262
263static int64_t ReadVBaseOffsetFromVTable(Process &process,
264 VTableContextBase &vtable_ctx,
265 lldb::addr_t vtable_ptr,
266 const CXXRecordDecl *cxx_record_decl,
267 const CXXRecordDecl *base_class_decl) {
268 if (vtable_ctx.isMicrosoft()) {
269 clang::MicrosoftVTableContext &msoft_vtable_ctx =
270 static_cast<clang::MicrosoftVTableContext &>(vtable_ctx);
271
272 // Get the index into the virtual base table. The
273 // index is the index in uint32_t from vbtable_ptr
274 const unsigned vbtable_index =
275 msoft_vtable_ctx.getVBTableIndex(cxx_record_decl, base_class_decl);
276 const lldb::addr_t base_offset_addr = vtable_ptr + vbtable_index * 4;
277 Status err;
278 return process.ReadSignedIntegerFromMemory(base_offset_addr, 4, INT64_MAX,
279 err);
280 }
281
282 clang::ItaniumVTableContext &itanium_vtable_ctx =
283 static_cast<clang::ItaniumVTableContext &>(vtable_ctx);
284
285 clang::CharUnits base_offset_offset =
286 itanium_vtable_ctx.getVirtualBaseOffsetOffset(cxx_record_decl,
287 base_class_decl);
288 const lldb::addr_t base_offset_addr =
289 vtable_ptr + base_offset_offset.getQuantity();
290 const uint32_t base_offset_size = process.GetAddressByteSize();
291 Status err;
292 return process.ReadSignedIntegerFromMemory(base_offset_addr, base_offset_size,
293 INT64_MAX, err);
294}
295
296static bool GetVBaseBitOffset(VTableContextBase &vtable_ctx,
297 ValueObject &valobj,
298 const ASTRecordLayout &record_layout,
299 const CXXRecordDecl *cxx_record_decl,
300 const CXXRecordDecl *base_class_decl,
301 int32_t &bit_offset) {
302 ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
303 Process *process = exe_ctx.GetProcessPtr();
304 if (!process)
305 return false;
306
307 lldb::addr_t vtable_ptr =
308 GetVTableAddress(*process, vtable_ctx, valobj, record_layout);
309 if (vtable_ptr == LLDB_INVALID_ADDRESS)
310 return false;
311
312 auto base_offset = ReadVBaseOffsetFromVTable(
313 *process, vtable_ctx, vtable_ptr, cxx_record_decl, base_class_decl);
314 if (base_offset == INT64_MAX)
315 return false;
316
317 bit_offset = base_offset * 8;
318
319 return true;
320}
321
Kate Stoneb9c1b512016-09-06 20:57:50 +0000322typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext *>
323 ClangASTMap;
Enrico Granata5d84a692014-08-19 21:46:37 +0000324
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325static ClangASTMap &GetASTMap() {
326 static ClangASTMap *g_map_ptr = nullptr;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000327 static llvm::once_flag g_once_flag;
328 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
330 });
331 return *g_map_ptr;
Enrico Granata5d84a692014-08-19 21:46:37 +0000332}
333
Davide Italiano7e3ef4d2018-03-20 19:46:32 +0000334bool ClangASTContext::IsOperator(const char *name,
335 clang::OverloadedOperatorKind &op_kind) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336 if (name == nullptr || name[0] == '\0')
337 return false;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000338
339#define OPERATOR_PREFIX "operator"
340#define OPERATOR_PREFIX_LENGTH (sizeof(OPERATOR_PREFIX) - 1)
341
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342 const char *post_op_name = nullptr;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000343
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344 bool no_space = true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000345
Kate Stoneb9c1b512016-09-06 20:57:50 +0000346 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
347 return false;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000348
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349 post_op_name = name + OPERATOR_PREFIX_LENGTH;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000350
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351 if (post_op_name[0] == ' ') {
352 post_op_name++;
353 no_space = false;
354 }
Pavel Labath1ac2b202016-08-15 14:32:32 +0000355
356#undef OPERATOR_PREFIX
357#undef OPERATOR_PREFIX_LENGTH
358
Adrian Prantl05097242018-04-30 16:49:04 +0000359 // This is an operator, set the overloaded operator kind to invalid in case
360 // this is a conversion operator...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361 op_kind = clang::NUM_OVERLOADED_OPERATORS;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000362
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363 switch (post_op_name[0]) {
364 default:
365 if (no_space)
366 return false;
367 break;
368 case 'n':
369 if (no_space)
370 return false;
371 if (strcmp(post_op_name, "new") == 0)
372 op_kind = clang::OO_New;
373 else if (strcmp(post_op_name, "new[]") == 0)
374 op_kind = clang::OO_Array_New;
375 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000376
Kate Stoneb9c1b512016-09-06 20:57:50 +0000377 case 'd':
378 if (no_space)
379 return false;
380 if (strcmp(post_op_name, "delete") == 0)
381 op_kind = clang::OO_Delete;
382 else if (strcmp(post_op_name, "delete[]") == 0)
383 op_kind = clang::OO_Array_Delete;
384 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000385
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386 case '+':
387 if (post_op_name[1] == '\0')
388 op_kind = clang::OO_Plus;
389 else if (post_op_name[2] == '\0') {
390 if (post_op_name[1] == '=')
391 op_kind = clang::OO_PlusEqual;
392 else if (post_op_name[1] == '+')
393 op_kind = clang::OO_PlusPlus;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000394 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000396
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397 case '-':
398 if (post_op_name[1] == '\0')
399 op_kind = clang::OO_Minus;
400 else if (post_op_name[2] == '\0') {
401 switch (post_op_name[1]) {
402 case '=':
403 op_kind = clang::OO_MinusEqual;
404 break;
405 case '-':
406 op_kind = clang::OO_MinusMinus;
407 break;
408 case '>':
409 op_kind = clang::OO_Arrow;
410 break;
411 }
412 } else if (post_op_name[3] == '\0') {
413 if (post_op_name[2] == '*')
414 op_kind = clang::OO_ArrowStar;
415 break;
416 }
417 break;
418
419 case '*':
420 if (post_op_name[1] == '\0')
421 op_kind = clang::OO_Star;
422 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
423 op_kind = clang::OO_StarEqual;
424 break;
425
426 case '/':
427 if (post_op_name[1] == '\0')
428 op_kind = clang::OO_Slash;
429 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
430 op_kind = clang::OO_SlashEqual;
431 break;
432
433 case '%':
434 if (post_op_name[1] == '\0')
435 op_kind = clang::OO_Percent;
436 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
437 op_kind = clang::OO_PercentEqual;
438 break;
439
440 case '^':
441 if (post_op_name[1] == '\0')
442 op_kind = clang::OO_Caret;
443 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
444 op_kind = clang::OO_CaretEqual;
445 break;
446
447 case '&':
448 if (post_op_name[1] == '\0')
449 op_kind = clang::OO_Amp;
450 else if (post_op_name[2] == '\0') {
451 switch (post_op_name[1]) {
452 case '=':
453 op_kind = clang::OO_AmpEqual;
454 break;
455 case '&':
456 op_kind = clang::OO_AmpAmp;
457 break;
458 }
459 }
460 break;
461
462 case '|':
463 if (post_op_name[1] == '\0')
464 op_kind = clang::OO_Pipe;
465 else if (post_op_name[2] == '\0') {
466 switch (post_op_name[1]) {
467 case '=':
468 op_kind = clang::OO_PipeEqual;
469 break;
470 case '|':
471 op_kind = clang::OO_PipePipe;
472 break;
473 }
474 }
475 break;
476
477 case '~':
478 if (post_op_name[1] == '\0')
479 op_kind = clang::OO_Tilde;
480 break;
481
482 case '!':
483 if (post_op_name[1] == '\0')
484 op_kind = clang::OO_Exclaim;
485 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
486 op_kind = clang::OO_ExclaimEqual;
487 break;
488
489 case '=':
490 if (post_op_name[1] == '\0')
491 op_kind = clang::OO_Equal;
492 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
493 op_kind = clang::OO_EqualEqual;
494 break;
495
496 case '<':
497 if (post_op_name[1] == '\0')
498 op_kind = clang::OO_Less;
499 else if (post_op_name[2] == '\0') {
500 switch (post_op_name[1]) {
501 case '<':
502 op_kind = clang::OO_LessLess;
503 break;
504 case '=':
505 op_kind = clang::OO_LessEqual;
506 break;
507 }
508 } else if (post_op_name[3] == '\0') {
509 if (post_op_name[2] == '=')
510 op_kind = clang::OO_LessLessEqual;
511 }
512 break;
513
514 case '>':
515 if (post_op_name[1] == '\0')
516 op_kind = clang::OO_Greater;
517 else if (post_op_name[2] == '\0') {
518 switch (post_op_name[1]) {
519 case '>':
520 op_kind = clang::OO_GreaterGreater;
521 break;
522 case '=':
523 op_kind = clang::OO_GreaterEqual;
524 break;
525 }
526 } else if (post_op_name[1] == '>' && post_op_name[2] == '=' &&
527 post_op_name[3] == '\0') {
528 op_kind = clang::OO_GreaterGreaterEqual;
529 }
530 break;
531
532 case ',':
533 if (post_op_name[1] == '\0')
534 op_kind = clang::OO_Comma;
535 break;
536
537 case '(':
538 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
539 op_kind = clang::OO_Call;
540 break;
541
542 case '[':
543 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
544 op_kind = clang::OO_Subscript;
545 break;
546 }
547
548 return true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000549}
Enrico Granata5d84a692014-08-19 21:46:37 +0000550
Greg Clayton57ee3062013-07-11 22:46:58 +0000551clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +0000552ClangASTContext::ConvertAccessTypeToAccessSpecifier(AccessType access) {
553 switch (access) {
554 default:
555 break;
556 case eAccessNone:
Greg Clayton8cf05932010-07-22 18:30:50 +0000557 return AS_none;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000558 case eAccessPublic:
559 return AS_public;
560 case eAccessPrivate:
561 return AS_private;
562 case eAccessProtected:
563 return AS_protected;
564 }
565 return AS_none;
Greg Clayton8cf05932010-07-22 18:30:50 +0000566}
567
Kate Stoneb9c1b512016-09-06 20:57:50 +0000568static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) {
569 // FIXME: Cleanup per-file based stuff.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570
Adrian Prantl05097242018-04-30 16:49:04 +0000571 // Set some properties which depend solely on the input kind; it would be
572 // nice to move these to the language standard, and have the driver resolve
573 // the input kind + language standard.
Richard Smith8186cd42017-04-26 22:10:53 +0000574 if (IK.getLanguage() == InputKind::Asm) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000575 Opts.AsmPreprocessor = 1;
Richard Smith8186cd42017-04-26 22:10:53 +0000576 } else if (IK.isObjectiveC()) {
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000577 Opts.ObjC = 1;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000578 }
579
580 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
581
582 if (LangStd == LangStandard::lang_unspecified) {
583 // Based on the base language, pick one.
Richard Smith8186cd42017-04-26 22:10:53 +0000584 switch (IK.getLanguage()) {
585 case InputKind::Unknown:
586 case InputKind::LLVM_IR:
587 case InputKind::RenderScript:
David Blaikiea322f362017-01-06 00:38:06 +0000588 llvm_unreachable("Invalid input kind!");
Richard Smith8186cd42017-04-26 22:10:53 +0000589 case InputKind::OpenCL:
Pavel Labath47168542017-04-27 08:49:19 +0000590 LangStd = LangStandard::lang_opencl10;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000591 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000592 case InputKind::CUDA:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000593 LangStd = LangStandard::lang_cuda;
594 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000595 case InputKind::Asm:
596 case InputKind::C:
597 case InputKind::ObjC:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000598 LangStd = LangStandard::lang_gnu99;
599 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000600 case InputKind::CXX:
601 case InputKind::ObjCXX:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602 LangStd = LangStandard::lang_gnucxx98;
603 break;
Benjamin Kramer0d97c222018-04-25 13:22:47 +0000604 case InputKind::HIP:
605 LangStd = LangStandard::lang_hip;
606 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000607 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000608 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000609
Kate Stoneb9c1b512016-09-06 20:57:50 +0000610 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
611 Opts.LineComment = Std.hasLineComments();
612 Opts.C99 = Std.isC99();
613 Opts.CPlusPlus = Std.isCPlusPlus();
614 Opts.CPlusPlus11 = Std.isCPlusPlus11();
615 Opts.Digraphs = Std.hasDigraphs();
616 Opts.GNUMode = Std.isGNUMode();
617 Opts.GNUInline = !Std.isC99();
618 Opts.HexFloats = Std.hasHexFloats();
619 Opts.ImplicitInt = Std.hasImplicitInt();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620
Kate Stoneb9c1b512016-09-06 20:57:50 +0000621 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623 // OpenCL has some additional defaults.
Pavel Labath47168542017-04-27 08:49:19 +0000624 if (LangStd == LangStandard::lang_opencl10) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625 Opts.OpenCL = 1;
626 Opts.AltiVec = 1;
627 Opts.CXXOperatorNames = 1;
628 Opts.LaxVectorConversions = 1;
629 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000630
Kate Stoneb9c1b512016-09-06 20:57:50 +0000631 // OpenCL and C++ both have bool, true, false keywords.
632 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000633
Kate Stoneb9c1b512016-09-06 20:57:50 +0000634 Opts.setValueVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635
Adrian Prantl05097242018-04-30 16:49:04 +0000636 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs is
637 // specified, or -std is set to a conforming mode.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000638 Opts.Trigraphs = !Opts.GNUMode;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000639 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000640 Opts.OptimizeSize = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000641
Kate Stoneb9c1b512016-09-06 20:57:50 +0000642 // FIXME: Eliminate this dependency.
643 // unsigned Opt =
644 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
645 // Opts.Optimize = Opt != 0;
646 unsigned Opt = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000647
Kate Stoneb9c1b512016-09-06 20:57:50 +0000648 // This is the __NO_INLINE__ define, which just depends on things like the
649 // optimization level and -fno-inline, not actually whether the backend has
650 // inlining enabled.
651 //
652 // FIXME: This is affected by other options (-fno-inline).
653 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000654}
655
Kate Stoneb9c1b512016-09-06 20:57:50 +0000656ClangASTContext::ClangASTContext(const char *target_triple)
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000657 : TypeSystem(TypeSystem::eKindClang), m_target_triple(), m_ast_up(),
658 m_language_options_up(), m_source_manager_up(), m_diagnostics_engine_up(),
659 m_target_options_rp(), m_target_info_up(), m_identifier_table_up(),
660 m_selector_table_up(), m_builtins_up(), m_callback_tag_decl(nullptr),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000661 m_callback_objc_decl(nullptr), m_callback_baton(nullptr),
662 m_pointer_byte_size(0), m_ast_owned(false) {
663 if (target_triple && target_triple[0])
664 SetTargetTriple(target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000665}
666
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000667// Destructor
Kate Stoneb9c1b512016-09-06 20:57:50 +0000668ClangASTContext::~ClangASTContext() { Finalize(); }
669
670ConstString ClangASTContext::GetPluginNameStatic() {
671 return ConstString("clang");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000672}
673
Kate Stoneb9c1b512016-09-06 20:57:50 +0000674ConstString ClangASTContext::GetPluginName() {
675 return ClangASTContext::GetPluginNameStatic();
Greg Clayton56939cb2015-09-17 22:23:34 +0000676}
677
Kate Stoneb9c1b512016-09-06 20:57:50 +0000678uint32_t ClangASTContext::GetPluginVersion() { return 1; }
Greg Clayton56939cb2015-09-17 22:23:34 +0000679
Kate Stoneb9c1b512016-09-06 20:57:50 +0000680lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
681 lldb_private::Module *module,
682 Target *target) {
683 if (ClangASTContextSupportsLanguage(language)) {
684 ArchSpec arch;
685 if (module)
686 arch = module->GetArchitecture();
687 else if (target)
688 arch = target->GetArchitecture();
Greg Clayton56939cb2015-09-17 22:23:34 +0000689
Kate Stoneb9c1b512016-09-06 20:57:50 +0000690 if (arch.IsValid()) {
691 ArchSpec fixed_arch = arch;
692 // LLVM wants this to be set to iOS or MacOSX; if we're working on
693 // a bare-boards type image, change the triple for llvm's benefit.
694 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
695 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) {
696 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
697 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
698 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) {
699 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
700 } else {
701 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
Greg Clayton56939cb2015-09-17 22:23:34 +0000702 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000703 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000704
Kate Stoneb9c1b512016-09-06 20:57:50 +0000705 if (module) {
706 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
707 if (ast_sp) {
708 ast_sp->SetArchitecture(fixed_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000709 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000710 return ast_sp;
711 } else if (target && target->IsValid()) {
712 std::shared_ptr<ClangASTContextForExpressions> ast_sp(
713 new ClangASTContextForExpressions(*target));
714 if (ast_sp) {
715 ast_sp->SetArchitecture(fixed_arch);
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000716 ast_sp->m_scratch_ast_source_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000717 new ClangASTSource(target->shared_from_this()));
Sean Callanan68e44232017-09-28 20:20:25 +0000718 lldbassert(ast_sp->getFileManager());
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000719 ast_sp->m_scratch_ast_source_up->InstallASTContext(
Sean Callanan68e44232017-09-28 20:20:25 +0000720 *ast_sp->getASTContext(), *ast_sp->getFileManager(), true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000721 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000722 ast_sp->m_scratch_ast_source_up->CreateProxy());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000723 ast_sp->SetExternalSource(proxy_ast_source);
724 return ast_sp;
725 }
726 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000728 }
729 return lldb::TypeSystemSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000730}
731
Kate Stoneb9c1b512016-09-06 20:57:50 +0000732void ClangASTContext::EnumerateSupportedLanguages(
733 std::set<lldb::LanguageType> &languages_for_types,
734 std::set<lldb::LanguageType> &languages_for_expressions) {
735 static std::vector<lldb::LanguageType> s_supported_languages_for_types(
736 {lldb::eLanguageTypeC89, lldb::eLanguageTypeC, lldb::eLanguageTypeC11,
737 lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeC99,
738 lldb::eLanguageTypeObjC, lldb::eLanguageTypeObjC_plus_plus,
739 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
740 lldb::eLanguageTypeC11, lldb::eLanguageTypeC_plus_plus_14});
741
742 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions(
743 {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC_plus_plus,
744 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
745 lldb::eLanguageTypeC_plus_plus_14});
746
747 languages_for_types.insert(s_supported_languages_for_types.begin(),
748 s_supported_languages_for_types.end());
749 languages_for_expressions.insert(
750 s_supported_languages_for_expressions.begin(),
751 s_supported_languages_for_expressions.end());
Enrico Granata5d84a692014-08-19 21:46:37 +0000752}
753
Kate Stoneb9c1b512016-09-06 20:57:50 +0000754void ClangASTContext::Initialize() {
755 PluginManager::RegisterPlugin(GetPluginNameStatic(),
756 "clang base AST context plug-in",
757 CreateInstance, EnumerateSupportedLanguages);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000758}
759
Kate Stoneb9c1b512016-09-06 20:57:50 +0000760void ClangASTContext::Terminate() {
761 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000762}
763
Kate Stoneb9c1b512016-09-06 20:57:50 +0000764void ClangASTContext::Finalize() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000765 if (m_ast_up) {
766 GetASTMap().Erase(m_ast_up.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000767 if (!m_ast_owned)
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000768 m_ast_up.release();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000769 }
770
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000771 m_builtins_up.reset();
772 m_selector_table_up.reset();
773 m_identifier_table_up.reset();
774 m_target_info_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000775 m_target_options_rp.reset();
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000776 m_diagnostics_engine_up.reset();
777 m_source_manager_up.reset();
778 m_language_options_up.reset();
779 m_ast_up.reset();
780 m_scratch_ast_source_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000781}
782
783void ClangASTContext::Clear() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000784 m_ast_up.reset();
785 m_language_options_up.reset();
786 m_source_manager_up.reset();
787 m_diagnostics_engine_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000788 m_target_options_rp.reset();
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000789 m_target_info_up.reset();
790 m_identifier_table_up.reset();
791 m_selector_table_up.reset();
792 m_builtins_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000793 m_pointer_byte_size = 0;
794}
795
Raphael Isemannf74a4c12019-04-30 08:41:35 +0000796void ClangASTContext::setSema(Sema *s) {
797 // Ensure that the new sema actually belongs to our ASTContext.
798 assert(s == nullptr || &s->getASTContext() == m_ast_up.get());
799 m_sema = s;
800}
801
Kate Stoneb9c1b512016-09-06 20:57:50 +0000802const char *ClangASTContext::GetTargetTriple() {
803 return m_target_triple.c_str();
804}
805
806void ClangASTContext::SetTargetTriple(const char *target_triple) {
807 Clear();
808 m_target_triple.assign(target_triple);
809}
810
811void ClangASTContext::SetArchitecture(const ArchSpec &arch) {
812 SetTargetTriple(arch.GetTriple().str().c_str());
813}
814
815bool ClangASTContext::HasExternalSource() {
816 ASTContext *ast = getASTContext();
817 if (ast)
818 return ast->getExternalSource() != nullptr;
819 return false;
820}
821
822void ClangASTContext::SetExternalSource(
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000823 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000824 ASTContext *ast = getASTContext();
825 if (ast) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000826 ast->setExternalSource(ast_source_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000827 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000828 }
829}
830
831void ClangASTContext::RemoveExternalSource() {
832 ASTContext *ast = getASTContext();
833
834 if (ast) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000835 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_up;
836 ast->setExternalSource(empty_ast_source_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000837 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000838 }
839}
840
841void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) {
842 if (!m_ast_owned) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000843 m_ast_up.release();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000844 }
845 m_ast_owned = false;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000846 m_ast_up.reset(ast_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000847 GetASTMap().Insert(ast_ctx, this);
848}
849
850ASTContext *ClangASTContext::getASTContext() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000851 if (m_ast_up == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000852 m_ast_owned = true;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000853 m_ast_up.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000854 *getIdentifierTable(), *getSelectorTable(),
855 *getBuiltinContext()));
856
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000857 m_ast_up->getDiagnostics().setClient(getDiagnosticConsumer(), false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000858
859 // This can be NULL if we don't know anything about the architecture or if
Adrian Prantl05097242018-04-30 16:49:04 +0000860 // the target for an architecture isn't enabled in the llvm/clang that we
861 // built
Kate Stoneb9c1b512016-09-06 20:57:50 +0000862 TargetInfo *target_info = getTargetInfo();
863 if (target_info)
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000864 m_ast_up->InitBuiltinTypes(*target_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000865
866 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000867 m_ast_up->getTranslationUnitDecl()->setHasExternalLexicalStorage();
868 // m_ast_up->getTranslationUnitDecl()->setHasExternalVisibleStorage();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000869 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000870
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000871 GetASTMap().Insert(m_ast_up.get(), this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000872
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000873 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_up(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000874 new ClangExternalASTSourceCallbacks(
875 ClangASTContext::CompleteTagDecl,
876 ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
877 ClangASTContext::LayoutRecordType, this));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000878 SetExternalSource(ast_source_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000879 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000880 return m_ast_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000881}
882
Kate Stoneb9c1b512016-09-06 20:57:50 +0000883ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
884 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
885 return clang_ast;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000886}
887
Kate Stoneb9c1b512016-09-06 20:57:50 +0000888Builtin::Context *ClangASTContext::getBuiltinContext() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000889 if (m_builtins_up == nullptr)
890 m_builtins_up.reset(new Builtin::Context());
891 return m_builtins_up.get();
Sean Callanan79439e82010-11-18 02:56:27 +0000892}
893
Kate Stoneb9c1b512016-09-06 20:57:50 +0000894IdentifierTable *ClangASTContext::getIdentifierTable() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000895 if (m_identifier_table_up == nullptr)
896 m_identifier_table_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000897 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000898 return m_identifier_table_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000899}
900
Kate Stoneb9c1b512016-09-06 20:57:50 +0000901LangOptions *ClangASTContext::getLanguageOptions() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000902 if (m_language_options_up == nullptr) {
903 m_language_options_up.reset(new LangOptions());
904 ParseLangArgs(*m_language_options_up, InputKind::ObjCXX, GetTargetTriple());
905 // InitializeLangOptions(*m_language_options_up, InputKind::ObjCXX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000906 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000907 return m_language_options_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000908}
909
Kate Stoneb9c1b512016-09-06 20:57:50 +0000910SelectorTable *ClangASTContext::getSelectorTable() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000911 if (m_selector_table_up == nullptr)
912 m_selector_table_up.reset(new SelectorTable());
913 return m_selector_table_up.get();
Greg Claytonfe689042015-11-10 17:47:04 +0000914}
915
Kate Stoneb9c1b512016-09-06 20:57:50 +0000916clang::FileManager *ClangASTContext::getFileManager() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000917 if (m_file_manager_up == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000918 clang::FileSystemOptions file_system_options;
Jonas Devlieghere9764b652019-02-18 20:31:18 +0000919 m_file_manager_up.reset(new clang::FileManager(
920 file_system_options, FileSystem::Instance().GetVirtualFileSystem()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000921 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000922 return m_file_manager_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000923}
924
925clang::SourceManager *ClangASTContext::getSourceManager() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000926 if (m_source_manager_up == nullptr)
927 m_source_manager_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000928 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000929 return m_source_manager_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000930}
931
932clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000933 if (m_diagnostics_engine_up == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000934 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000935 m_diagnostics_engine_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000936 new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
937 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000938 return m_diagnostics_engine_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000939}
940
941clang::MangleContext *ClangASTContext::getMangleContext() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000942 if (m_mangle_ctx_up == nullptr)
943 m_mangle_ctx_up.reset(getASTContext()->createMangleContext());
944 return m_mangle_ctx_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000945}
946
947class NullDiagnosticConsumer : public DiagnosticConsumer {
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000948public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000949 NullDiagnosticConsumer() {
950 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
951 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000952
Kate Stoneb9c1b512016-09-06 20:57:50 +0000953 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
Raphael Isemann17566302019-05-03 10:03:28 +0000954 const clang::Diagnostic &info) override {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000955 if (m_log) {
956 llvm::SmallVector<char, 32> diag_str(10);
957 info.FormatDiagnostic(diag_str);
958 diag_str.push_back('\0');
959 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000960 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000961 }
962
963 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
964 return new NullDiagnosticConsumer();
965 }
966
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000967private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000968 Log *m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000969};
970
Kate Stoneb9c1b512016-09-06 20:57:50 +0000971DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000972 if (m_diagnostic_consumer_up == nullptr)
973 m_diagnostic_consumer_up.reset(new NullDiagnosticConsumer);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000974
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000975 return m_diagnostic_consumer_up.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000976}
977
Kate Stoneb9c1b512016-09-06 20:57:50 +0000978std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000979 if (m_target_options_rp == nullptr && !m_target_triple.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000980 m_target_options_rp = std::make_shared<clang::TargetOptions>();
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000981 if (m_target_options_rp != nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000982 m_target_options_rp->Triple = m_target_triple;
983 }
984 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000985}
986
Kate Stoneb9c1b512016-09-06 20:57:50 +0000987TargetInfo *ClangASTContext::getTargetInfo() {
988 // target_triple should be something like "x86_64-apple-macosx"
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000989 if (m_target_info_up == nullptr && !m_target_triple.empty())
990 m_target_info_up.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000991 getTargetOptions()));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000992 return m_target_info_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000993}
994
995#pragma mark Basic Types
996
Kate Stoneb9c1b512016-09-06 20:57:50 +0000997static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
998 ASTContext *ast, QualType qual_type) {
999 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001000 return qual_type_bit_size == bit_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001001}
Greg Clayton56939cb2015-09-17 22:23:34 +00001002
Greg Claytona1e5dc82015-08-11 22:53:00 +00001003CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00001004ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
1005 size_t bit_size) {
1006 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
1007 getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001008}
1009
Kate Stoneb9c1b512016-09-06 20:57:50 +00001010CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
1011 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
1012 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001013 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001014 switch (encoding) {
1015 case eEncodingInvalid:
1016 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1017 return CompilerType(ast, ast->VoidPtrTy);
1018 break;
1019
1020 case eEncodingUint:
1021 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1022 return CompilerType(ast, ast->UnsignedCharTy);
1023 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1024 return CompilerType(ast, ast->UnsignedShortTy);
1025 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1026 return CompilerType(ast, ast->UnsignedIntTy);
1027 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1028 return CompilerType(ast, ast->UnsignedLongTy);
1029 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1030 return CompilerType(ast, ast->UnsignedLongLongTy);
1031 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1032 return CompilerType(ast, ast->UnsignedInt128Ty);
1033 break;
1034
1035 case eEncodingSint:
1036 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1037 return CompilerType(ast, ast->SignedCharTy);
1038 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1039 return CompilerType(ast, ast->ShortTy);
1040 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1041 return CompilerType(ast, ast->IntTy);
1042 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1043 return CompilerType(ast, ast->LongTy);
1044 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1045 return CompilerType(ast, ast->LongLongTy);
1046 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1047 return CompilerType(ast, ast->Int128Ty);
1048 break;
1049
1050 case eEncodingIEEE754:
1051 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1052 return CompilerType(ast, ast->FloatTy);
1053 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1054 return CompilerType(ast, ast->DoubleTy);
1055 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1056 return CompilerType(ast, ast->LongDoubleTy);
1057 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1058 return CompilerType(ast, ast->HalfTy);
1059 break;
1060
1061 case eEncodingVector:
1062 // Sanity check that bit_size is a multiple of 8's.
1063 if (bit_size && !(bit_size & 0x7u))
1064 return CompilerType(
1065 ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8));
1066 break;
1067 }
1068
1069 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001070}
1071
Greg Clayton57ee3062013-07-11 22:46:58 +00001072lldb::BasicType
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001073ClangASTContext::GetBasicTypeEnumeration(ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001074 if (name) {
1075 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
1076 static TypeNameToBasicTypeMap g_type_map;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +00001077 static llvm::once_flag g_once_flag;
1078 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001079 // "void"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001080 g_type_map.Append(ConstString("void"), eBasicTypeVoid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001081
1082 // "char"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001083 g_type_map.Append(ConstString("char"), eBasicTypeChar);
1084 g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar);
1085 g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar);
1086 g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar);
1087 g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar);
1088 g_type_map.Append(ConstString("unsigned wchar_t"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001089 eBasicTypeUnsignedWChar);
1090 // "short"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001091 g_type_map.Append(ConstString("short"), eBasicTypeShort);
1092 g_type_map.Append(ConstString("short int"), eBasicTypeShort);
1093 g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort);
1094 g_type_map.Append(ConstString("unsigned short int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001095 eBasicTypeUnsignedShort);
1096
1097 // "int"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001098 g_type_map.Append(ConstString("int"), eBasicTypeInt);
1099 g_type_map.Append(ConstString("signed int"), eBasicTypeInt);
1100 g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt);
1101 g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102
1103 // "long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001104 g_type_map.Append(ConstString("long"), eBasicTypeLong);
1105 g_type_map.Append(ConstString("long int"), eBasicTypeLong);
1106 g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong);
1107 g_type_map.Append(ConstString("unsigned long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001108 eBasicTypeUnsignedLong);
1109
1110 // "long long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001111 g_type_map.Append(ConstString("long long"), eBasicTypeLongLong);
1112 g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong);
1113 g_type_map.Append(ConstString("unsigned long long"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001114 eBasicTypeUnsignedLongLong);
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001115 g_type_map.Append(ConstString("unsigned long long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001116 eBasicTypeUnsignedLongLong);
1117
1118 // "int128"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001119 g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128);
1120 g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001121
1122 // Miscellaneous
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001123 g_type_map.Append(ConstString("bool"), eBasicTypeBool);
1124 g_type_map.Append(ConstString("float"), eBasicTypeFloat);
1125 g_type_map.Append(ConstString("double"), eBasicTypeDouble);
1126 g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble);
1127 g_type_map.Append(ConstString("id"), eBasicTypeObjCID);
1128 g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel);
1129 g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001130 g_type_map.Sort();
1131 });
1132
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001133 return g_type_map.Find(name, eBasicTypeInvalid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001134 }
1135 return eBasicTypeInvalid;
Greg Clayton57ee3062013-07-11 22:46:58 +00001136}
1137
Kate Stoneb9c1b512016-09-06 20:57:50 +00001138CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001139 ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001140 if (ast) {
1141 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
1142 return ClangASTContext::GetBasicType(ast, basic_type);
1143 }
1144 return CompilerType();
1145}
1146
1147uint32_t ClangASTContext::GetPointerByteSize() {
1148 if (m_pointer_byte_size == 0)
Adrian Prantld963a7c2019-01-15 18:07:52 +00001149 if (auto size = GetBasicType(lldb::eBasicTypeVoid)
1150 .GetPointerType()
1151 .GetByteSize(nullptr))
1152 m_pointer_byte_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001153 return m_pointer_byte_size;
1154}
1155
1156CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
1157 return GetBasicType(getASTContext(), basic_type);
1158}
1159
1160CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1161 lldb::BasicType basic_type) {
1162 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001163 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001164 lldb::opaque_compiler_type_t clang_type =
1165 GetOpaqueCompilerType(ast, basic_type);
1166
1167 if (clang_type)
1168 return CompilerType(GetASTContext(ast), clang_type);
1169 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +00001170}
1171
Kate Stoneb9c1b512016-09-06 20:57:50 +00001172CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
1173 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
1174 ASTContext *ast = getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001175
Kate Stoneb9c1b512016-09-06 20:57:50 +00001176#define streq(a, b) strcmp(a, b) == 0
1177 assert(ast != nullptr);
1178 if (ast) {
1179 switch (dw_ate) {
1180 default:
1181 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001182
Kate Stoneb9c1b512016-09-06 20:57:50 +00001183 case DW_ATE_address:
1184 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1185 return CompilerType(ast, ast->VoidPtrTy);
1186 break;
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001187
Kate Stoneb9c1b512016-09-06 20:57:50 +00001188 case DW_ATE_boolean:
1189 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
1190 return CompilerType(ast, ast->BoolTy);
1191 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1192 return CompilerType(ast, ast->UnsignedCharTy);
1193 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1194 return CompilerType(ast, ast->UnsignedShortTy);
1195 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1196 return CompilerType(ast, ast->UnsignedIntTy);
1197 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001198
Kate Stoneb9c1b512016-09-06 20:57:50 +00001199 case DW_ATE_lo_user:
1200 // This has been seen to mean DW_AT_complex_integer
1201 if (type_name) {
1202 if (::strstr(type_name, "complex")) {
1203 CompilerType complex_int_clang_type =
1204 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1205 bit_size / 2);
1206 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1207 complex_int_clang_type)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001208 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001209 }
1210 break;
1211
1212 case DW_ATE_complex_float:
1213 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
1214 return CompilerType(ast, ast->FloatComplexTy);
1215 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
1216 return CompilerType(ast, ast->DoubleComplexTy);
1217 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
1218 return CompilerType(ast, ast->LongDoubleComplexTy);
1219 else {
1220 CompilerType complex_float_clang_type =
1221 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1222 bit_size / 2);
1223 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1224 complex_float_clang_type)));
1225 }
1226 break;
1227
1228 case DW_ATE_float:
1229 if (streq(type_name, "float") &&
1230 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1231 return CompilerType(ast, ast->FloatTy);
1232 if (streq(type_name, "double") &&
1233 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1234 return CompilerType(ast, ast->DoubleTy);
1235 if (streq(type_name, "long double") &&
1236 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1237 return CompilerType(ast, ast->LongDoubleTy);
1238 // Fall back to not requiring a name match
1239 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1240 return CompilerType(ast, ast->FloatTy);
1241 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1242 return CompilerType(ast, ast->DoubleTy);
1243 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1244 return CompilerType(ast, ast->LongDoubleTy);
1245 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1246 return CompilerType(ast, ast->HalfTy);
1247 break;
1248
1249 case DW_ATE_signed:
1250 if (type_name) {
1251 if (streq(type_name, "wchar_t") &&
1252 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1253 (getTargetInfo() &&
1254 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1255 return CompilerType(ast, ast->WCharTy);
1256 if (streq(type_name, "void") &&
1257 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
1258 return CompilerType(ast, ast->VoidTy);
1259 if (strstr(type_name, "long long") &&
1260 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1261 return CompilerType(ast, ast->LongLongTy);
1262 if (strstr(type_name, "long") &&
1263 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1264 return CompilerType(ast, ast->LongTy);
1265 if (strstr(type_name, "short") &&
1266 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1267 return CompilerType(ast, ast->ShortTy);
1268 if (strstr(type_name, "char")) {
1269 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1270 return CompilerType(ast, ast->CharTy);
1271 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1272 return CompilerType(ast, ast->SignedCharTy);
1273 }
1274 if (strstr(type_name, "int")) {
1275 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1276 return CompilerType(ast, ast->IntTy);
1277 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1278 return CompilerType(ast, ast->Int128Ty);
1279 }
1280 }
1281 // We weren't able to match up a type name, just search by size
1282 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1283 return CompilerType(ast, ast->CharTy);
1284 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1285 return CompilerType(ast, ast->ShortTy);
1286 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1287 return CompilerType(ast, ast->IntTy);
1288 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1289 return CompilerType(ast, ast->LongTy);
1290 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1291 return CompilerType(ast, ast->LongLongTy);
1292 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1293 return CompilerType(ast, ast->Int128Ty);
1294 break;
1295
1296 case DW_ATE_signed_char:
1297 if (ast->getLangOpts().CharIsSigned && type_name &&
1298 streq(type_name, "char")) {
1299 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1300 return CompilerType(ast, ast->CharTy);
1301 }
1302 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1303 return CompilerType(ast, ast->SignedCharTy);
1304 break;
1305
1306 case DW_ATE_unsigned:
1307 if (type_name) {
1308 if (streq(type_name, "wchar_t")) {
1309 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1310 if (!(getTargetInfo() &&
1311 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1312 return CompilerType(ast, ast->WCharTy);
1313 }
1314 }
1315 if (strstr(type_name, "long long")) {
1316 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1317 return CompilerType(ast, ast->UnsignedLongLongTy);
1318 } else if (strstr(type_name, "long")) {
1319 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1320 return CompilerType(ast, ast->UnsignedLongTy);
1321 } else if (strstr(type_name, "short")) {
1322 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1323 return CompilerType(ast, ast->UnsignedShortTy);
1324 } else if (strstr(type_name, "char")) {
1325 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1326 return CompilerType(ast, ast->UnsignedCharTy);
1327 } else if (strstr(type_name, "int")) {
1328 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1329 return CompilerType(ast, ast->UnsignedIntTy);
1330 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1331 return CompilerType(ast, ast->UnsignedInt128Ty);
1332 }
1333 }
1334 // We weren't able to match up a type name, just search by size
1335 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1336 return CompilerType(ast, ast->UnsignedCharTy);
1337 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1338 return CompilerType(ast, ast->UnsignedShortTy);
1339 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1340 return CompilerType(ast, ast->UnsignedIntTy);
1341 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1342 return CompilerType(ast, ast->UnsignedLongTy);
1343 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1344 return CompilerType(ast, ast->UnsignedLongLongTy);
1345 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1346 return CompilerType(ast, ast->UnsignedInt128Ty);
1347 break;
1348
1349 case DW_ATE_unsigned_char:
1350 if (!ast->getLangOpts().CharIsSigned && type_name &&
1351 streq(type_name, "char")) {
1352 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1353 return CompilerType(ast, ast->CharTy);
1354 }
1355 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1356 return CompilerType(ast, ast->UnsignedCharTy);
1357 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1358 return CompilerType(ast, ast->UnsignedShortTy);
1359 break;
1360
1361 case DW_ATE_imaginary_float:
1362 break;
1363
1364 case DW_ATE_UTF:
1365 if (type_name) {
1366 if (streq(type_name, "char16_t")) {
1367 return CompilerType(ast, ast->Char16Ty);
1368 } else if (streq(type_name, "char32_t")) {
1369 return CompilerType(ast, ast->Char32Ty);
1370 }
1371 }
1372 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001373 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001374 }
1375 // This assert should fire for anything that we don't catch above so we know
1376 // to fix any issues we run into.
1377 if (type_name) {
1378 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1379 "DW_TAG_base_type '%s' encoded with "
1380 "DW_ATE = 0x%x, bit_size = %u\n",
1381 type_name, dw_ate, bit_size);
1382 } else {
1383 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1384 "DW_TAG_base_type encoded with "
1385 "DW_ATE = 0x%x, bit_size = %u\n",
1386 dw_ate, bit_size);
1387 }
1388 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001389}
1390
Kate Stoneb9c1b512016-09-06 20:57:50 +00001391CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) {
1392 if (ast)
1393 return CompilerType(ast, ast->UnknownAnyTy);
1394 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001395}
1396
Kate Stoneb9c1b512016-09-06 20:57:50 +00001397CompilerType ClangASTContext::GetCStringType(bool is_const) {
1398 ASTContext *ast = getASTContext();
1399 QualType char_type(ast->CharTy);
1400
1401 if (is_const)
1402 char_type.addConst();
1403
1404 return CompilerType(ast, ast->getPointerType(char_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001405}
1406
Zachary Turner115209e2018-11-05 19:25:39 +00001407clang::DeclContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001408ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1409 return ast->getTranslationUnitDecl();
Sean Callanan09ab4b72011-11-30 22:11:59 +00001410}
1411
Kate Stoneb9c1b512016-09-06 20:57:50 +00001412clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1413 clang::Decl *source_decl) {
1414 FileSystemOptions file_system_options;
1415 FileManager file_manager(file_system_options);
1416 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1417
Gabor Marton5ac6d492019-05-15 10:29:48 +00001418 if (llvm::Expected<clang::Decl *> ret_or_error =
1419 importer.Import(source_decl)) {
1420 return *ret_or_error;
1421 } else {
1422 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
1423 LLDB_LOG_ERROR(log, ret_or_error.takeError(), "Couldn't import decl: {0}");
1424 return nullptr;
1425 }
Greg Clayton526e5af2010-11-13 03:52:47 +00001426}
1427
Kate Stoneb9c1b512016-09-06 20:57:50 +00001428bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1429 bool ignore_qualifiers) {
1430 ClangASTContext *ast =
1431 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1432 if (!ast || ast != type2.GetTypeSystem())
1433 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001434
Kate Stoneb9c1b512016-09-06 20:57:50 +00001435 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1436 return true;
Greg Clayton55995eb2012-04-06 17:38:55 +00001437
Kate Stoneb9c1b512016-09-06 20:57:50 +00001438 QualType type1_qual = ClangUtil::GetQualType(type1);
1439 QualType type2_qual = ClangUtil::GetQualType(type2);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001440
Kate Stoneb9c1b512016-09-06 20:57:50 +00001441 if (ignore_qualifiers) {
1442 type1_qual = type1_qual.getUnqualifiedType();
1443 type2_qual = type2_qual.getUnqualifiedType();
1444 }
1445
1446 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001447}
1448
Kate Stoneb9c1b512016-09-06 20:57:50 +00001449CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1450 if (clang::ObjCInterfaceDecl *interface_decl =
1451 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1452 return GetTypeForDecl(interface_decl);
1453 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1454 return GetTypeForDecl(tag_decl);
1455 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001456}
1457
Kate Stoneb9c1b512016-09-06 20:57:50 +00001458CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001459 // No need to call the getASTContext() accessor (which can create the AST if
1460 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001461 // AST if our AST didn't already exist...
1462 ASTContext *ast = &decl->getASTContext();
1463 if (ast)
1464 return CompilerType(ast, ast->getTagDeclType(decl));
1465 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001466}
1467
Kate Stoneb9c1b512016-09-06 20:57:50 +00001468CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001469 // No need to call the getASTContext() accessor (which can create the AST if
1470 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001471 // AST if our AST didn't already exist...
1472 ASTContext *ast = &decl->getASTContext();
1473 if (ast)
1474 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1475 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001476}
1477
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001478#pragma mark Structure, Unions, Classes
1479
Kate Stoneb9c1b512016-09-06 20:57:50 +00001480CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx,
1481 AccessType access_type,
1482 const char *name, int kind,
1483 LanguageType language,
1484 ClangASTMetadata *metadata) {
1485 ASTContext *ast = getASTContext();
1486 assert(ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001487
Kate Stoneb9c1b512016-09-06 20:57:50 +00001488 if (decl_ctx == nullptr)
1489 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton9e409562010-07-28 02:04:09 +00001490
Kate Stoneb9c1b512016-09-06 20:57:50 +00001491 if (language == eLanguageTypeObjC ||
1492 language == eLanguageTypeObjC_plus_plus) {
1493 bool isForwardDecl = true;
1494 bool isInternal = false;
1495 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1496 }
Greg Clayton9e409562010-07-28 02:04:09 +00001497
Kate Stoneb9c1b512016-09-06 20:57:50 +00001498 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
Adrian Prantl05097242018-04-30 16:49:04 +00001499 // we will need to update this code. I was told to currently always use the
1500 // CXXRecordDecl class since we often don't know from debug information if
1501 // something is struct or a class, so we default to always use the more
Kate Stoneb9c1b512016-09-06 20:57:50 +00001502 // complete definition just in case.
Greg Claytonc4ffd662013-03-08 01:37:30 +00001503
Kate Stoneb9c1b512016-09-06 20:57:50 +00001504 bool is_anonymous = (!name) || (!name[0]);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001505
Kate Stoneb9c1b512016-09-06 20:57:50 +00001506 CXXRecordDecl *decl = CXXRecordDecl::Create(
1507 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1508 SourceLocation(), is_anonymous ? nullptr : &ast->Idents.get(name));
1509
1510 if (is_anonymous)
1511 decl->setAnonymousStructOrUnion(true);
1512
1513 if (decl) {
1514 if (metadata)
1515 SetMetadata(ast, decl, *metadata);
1516
1517 if (access_type != eAccessNone)
1518 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1519
1520 if (decl_ctx)
1521 decl_ctx->addDecl(decl);
1522
1523 return CompilerType(ast, ast->getTagDeclType(decl));
1524 }
1525 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001526}
1527
Sean Callanan09e91ac2017-05-11 22:08:05 +00001528namespace {
1529 bool IsValueParam(const clang::TemplateArgument &argument) {
1530 return argument.getKind() == TemplateArgument::Integral;
1531 }
1532}
1533
Kate Stoneb9c1b512016-09-06 20:57:50 +00001534static TemplateParameterList *CreateTemplateParameterList(
1535 ASTContext *ast,
1536 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1537 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1538 const bool parameter_pack = false;
1539 const bool is_typename = false;
1540 const unsigned depth = 0;
Sean Callanan09e91ac2017-05-11 22:08:05 +00001541 const size_t num_template_params = template_param_infos.args.size();
1542 DeclContext *const decl_context =
1543 ast->getTranslationUnitDecl(); // Is this the right decl context?,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001544 for (size_t i = 0; i < num_template_params; ++i) {
1545 const char *name = template_param_infos.names[i];
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001546
Kate Stoneb9c1b512016-09-06 20:57:50 +00001547 IdentifierInfo *identifier_info = nullptr;
1548 if (name && name[0])
1549 identifier_info = &ast->Idents.get(name);
Sean Callanan09e91ac2017-05-11 22:08:05 +00001550 if (IsValueParam(template_param_infos.args[i])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001551 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001552 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001553 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1554 template_param_infos.args[i].getIntegralType(), parameter_pack,
1555 nullptr));
1556
1557 } else {
1558 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001559 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001560 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1561 is_typename, parameter_pack));
1562 }
1563 }
Eugene Zemtsova9d928c2017-09-29 03:15:08 +00001564
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001565 if (template_param_infos.packed_args) {
Sean Callanan09e91ac2017-05-11 22:08:05 +00001566 IdentifierInfo *identifier_info = nullptr;
1567 if (template_param_infos.pack_name && template_param_infos.pack_name[0])
1568 identifier_info = &ast->Idents.get(template_param_infos.pack_name);
1569 const bool parameter_pack_true = true;
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001570
1571 if (!template_param_infos.packed_args->args.empty() &&
1572 IsValueParam(template_param_infos.packed_args->args[0])) {
Sean Callanan09e91ac2017-05-11 22:08:05 +00001573 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001574 *ast, decl_context, SourceLocation(), SourceLocation(), depth,
1575 num_template_params, identifier_info,
Sean Callanan09e91ac2017-05-11 22:08:05 +00001576 template_param_infos.packed_args->args[0].getIntegralType(),
1577 parameter_pack_true, nullptr));
1578 } else {
1579 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001580 *ast, decl_context, SourceLocation(), SourceLocation(), depth,
1581 num_template_params, identifier_info, is_typename,
1582 parameter_pack_true));
Sean Callanan09e91ac2017-05-11 22:08:05 +00001583 }
1584 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001585 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1586 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1587 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1588 SourceLocation(), requires_clause);
1589 return template_param_list;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001590}
1591
Kate Stoneb9c1b512016-09-06 20:57:50 +00001592clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1593 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1594 const char *name, const TemplateParameterInfos &template_param_infos) {
Adrian Prantld8f460e2018-05-02 16:55:16 +00001595 // /// Create a function template node.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001596 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001597
Kate Stoneb9c1b512016-09-06 20:57:50 +00001598 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001599
Kate Stoneb9c1b512016-09-06 20:57:50 +00001600 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1601 ast, template_param_infos, template_param_decls);
1602 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1603 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1604 template_param_list, func_decl);
1605
1606 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1607 i < template_param_decl_count; ++i) {
1608 // TODO: verify which decl context we should put template_param_decls into..
1609 template_param_decls[i]->setDeclContext(func_decl);
1610 }
1611
1612 return func_tmpl_decl;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001613}
1614
Kate Stoneb9c1b512016-09-06 20:57:50 +00001615void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1616 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1617 const TemplateParameterInfos &infos) {
Shafik Yaghmoura0858e22019-07-17 20:16:13 +00001618 TemplateArgumentList *template_args_ptr =
1619 TemplateArgumentList::CreateCopy(func_decl->getASTContext(), infos.args);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001620
Shafik Yaghmoura0858e22019-07-17 20:16:13 +00001621 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl,
1622 template_args_ptr, nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001623}
1624
Kate Stoneb9c1b512016-09-06 20:57:50 +00001625ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1626 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1627 int kind, const TemplateParameterInfos &template_param_infos) {
1628 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001629
Kate Stoneb9c1b512016-09-06 20:57:50 +00001630 ClassTemplateDecl *class_template_decl = nullptr;
1631 if (decl_ctx == nullptr)
1632 decl_ctx = ast->getTranslationUnitDecl();
Greg Claytonf0705c82011-10-22 03:33:13 +00001633
Kate Stoneb9c1b512016-09-06 20:57:50 +00001634 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1635 DeclarationName decl_name(&identifier_info);
Greg Claytonf0705c82011-10-22 03:33:13 +00001636
Kate Stoneb9c1b512016-09-06 20:57:50 +00001637 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Greg Claytonf0705c82011-10-22 03:33:13 +00001638
Kate Stoneb9c1b512016-09-06 20:57:50 +00001639 for (NamedDecl *decl : result) {
1640 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001641 if (class_template_decl)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001642 return class_template_decl;
1643 }
1644
1645 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1646
1647 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1648 ast, template_param_infos, template_param_decls);
1649
1650 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1651 *ast, (TagDecl::TagKind)kind,
1652 decl_ctx, // What decl context do we use here? TU? The actual decl
1653 // context?
1654 SourceLocation(), SourceLocation(), &identifier_info);
1655
1656 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1657 i < template_param_decl_count; ++i) {
1658 template_param_decls[i]->setDeclContext(template_cxx_decl);
1659 }
1660
1661 // With templated classes, we say that a class is templated with
1662 // specializations, but that the bare class has no functions.
1663 // template_cxx_decl->startDefinition();
1664 // template_cxx_decl->completeDefinition();
1665
1666 class_template_decl = ClassTemplateDecl::Create(
1667 *ast,
1668 decl_ctx, // What decl context do we use here? TU? The actual decl
1669 // context?
Pavel Labath4294de32017-01-12 10:44:16 +00001670 SourceLocation(), decl_name, template_param_list, template_cxx_decl);
Richard Smith35b007e2019-02-15 21:48:09 +00001671 template_cxx_decl->setDescribedClassTemplate(class_template_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001672
1673 if (class_template_decl) {
1674 if (access_type != eAccessNone)
1675 class_template_decl->setAccess(
1676 ConvertAccessTypeToAccessSpecifier(access_type));
1677
1678 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1679 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1680
1681 decl_ctx->addDecl(class_template_decl);
1682
Sean Callanan5e9e1992011-10-26 01:06:27 +00001683#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001684 VerifyDecl(class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001685#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001686 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001687
Kate Stoneb9c1b512016-09-06 20:57:50 +00001688 return class_template_decl;
Greg Claytonf0705c82011-10-22 03:33:13 +00001689}
1690
Frederic Rissf4e7e522018-04-02 16:18:32 +00001691TemplateTemplateParmDecl *
1692ClangASTContext::CreateTemplateTemplateParmDecl(const char *template_name) {
1693 ASTContext *ast = getASTContext();
1694
1695 auto *decl_ctx = ast->getTranslationUnitDecl();
1696
1697 IdentifierInfo &identifier_info = ast->Idents.get(template_name);
1698 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1699
1700 ClangASTContext::TemplateParameterInfos template_param_infos;
1701 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1702 ast, template_param_infos, template_param_decls);
1703
1704 // LLDB needs to create those decls only to be able to display a
Adrian Prantl05097242018-04-30 16:49:04 +00001705 // type that includes a template template argument. Only the name matters for
1706 // this purpose, so we use dummy values for the other characterisitcs of the
1707 // type.
Frederic Rissf4e7e522018-04-02 16:18:32 +00001708 return TemplateTemplateParmDecl::Create(
1709 *ast, decl_ctx, SourceLocation(),
1710 /*Depth*/ 0, /*Position*/ 0,
1711 /*IsParameterPack*/ false, &identifier_info, template_param_list);
1712}
1713
Greg Claytonf0705c82011-10-22 03:33:13 +00001714ClassTemplateSpecializationDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001715ClangASTContext::CreateClassTemplateSpecializationDecl(
1716 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1717 const TemplateParameterInfos &template_param_infos) {
1718 ASTContext *ast = getASTContext();
Sean Callanan09e91ac2017-05-11 22:08:05 +00001719 llvm::SmallVector<clang::TemplateArgument, 2> args(
1720 template_param_infos.args.size() +
1721 (template_param_infos.packed_args ? 1 : 0));
1722 std::copy(template_param_infos.args.begin(), template_param_infos.args.end(),
1723 args.begin());
1724 if (template_param_infos.packed_args) {
1725 args[args.size() - 1] = TemplateArgument::CreatePackCopy(
1726 *ast, template_param_infos.packed_args->args);
1727 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001728 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1729 ClassTemplateSpecializationDecl::Create(
1730 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
Sean Callanan09e91ac2017-05-11 22:08:05 +00001731 SourceLocation(), class_template_decl, args,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001732 nullptr);
1733
1734 class_template_specialization_decl->setSpecializationKind(
1735 TSK_ExplicitSpecialization);
1736
1737 return class_template_specialization_decl;
1738}
1739
1740CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1741 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1742 if (class_template_specialization_decl) {
Greg Claytonf0705c82011-10-22 03:33:13 +00001743 ASTContext *ast = getASTContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001744 if (ast)
1745 return CompilerType(
1746 ast, ast->getTagDeclType(class_template_specialization_decl));
1747 }
1748 return CompilerType();
Greg Claytonf0705c82011-10-22 03:33:13 +00001749}
1750
Kate Stoneb9c1b512016-09-06 20:57:50 +00001751static inline bool check_op_param(bool is_method,
1752 clang::OverloadedOperatorKind op_kind,
1753 bool unary, bool binary,
1754 uint32_t num_params) {
1755 // Special-case call since it can take any number of operands
1756 if (op_kind == OO_Call)
1757 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001758
Kate Stoneb9c1b512016-09-06 20:57:50 +00001759 // The parameter count doesn't include "this"
1760 if (is_method)
1761 ++num_params;
1762 if (num_params == 1)
1763 return unary;
1764 if (num_params == 2)
1765 return binary;
1766 else
Greg Clayton090d0982011-06-19 03:43:27 +00001767 return false;
1768}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001769
Kate Stoneb9c1b512016-09-06 20:57:50 +00001770bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1771 bool is_method, clang::OverloadedOperatorKind op_kind,
1772 uint32_t num_params) {
1773 switch (op_kind) {
1774 default:
1775 break;
1776 // C++ standard allows any number of arguments to new/delete
1777 case OO_New:
1778 case OO_Array_New:
1779 case OO_Delete:
1780 case OO_Array_Delete:
1781 return true;
1782 }
Pavel Labath1ac2b202016-08-15 14:32:32 +00001783
Kate Stoneb9c1b512016-09-06 20:57:50 +00001784#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1785 case OO_##Name: \
1786 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1787 switch (op_kind) {
Greg Clayton090d0982011-06-19 03:43:27 +00001788#include "clang/Basic/OperatorKinds.def"
Kate Stoneb9c1b512016-09-06 20:57:50 +00001789 default:
1790 break;
1791 }
1792 return false;
Greg Clayton090d0982011-06-19 03:43:27 +00001793}
1794
Greg Clayton57ee3062013-07-11 22:46:58 +00001795clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +00001796ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1797 clang::AccessSpecifier rhs) {
1798 // Make the access equal to the stricter of the field and the nested field's
1799 // access
1800 if (lhs == AS_none || rhs == AS_none)
1801 return AS_none;
1802 if (lhs == AS_private || rhs == AS_private)
1803 return AS_private;
1804 if (lhs == AS_protected || rhs == AS_protected)
1805 return AS_protected;
1806 return AS_public;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001807}
1808
Kate Stoneb9c1b512016-09-06 20:57:50 +00001809bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1810 uint32_t &bitfield_bit_size) {
1811 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001812}
1813
Kate Stoneb9c1b512016-09-06 20:57:50 +00001814bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field,
1815 uint32_t &bitfield_bit_size) {
1816 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001817 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001818
Kate Stoneb9c1b512016-09-06 20:57:50 +00001819 if (field->isBitField()) {
1820 Expr *bit_width_expr = field->getBitWidth();
1821 if (bit_width_expr) {
1822 llvm::APSInt bit_width_apsint;
1823 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1824 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001825 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001826 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001827 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001828 }
1829 return false;
1830}
1831
1832bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1833 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001834 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001835
1836 if (!record_decl->field_empty())
1837 return true;
1838
1839 // No fields, lets check this is a CXX record and check the base classes
1840 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1841 if (cxx_record_decl) {
1842 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1843 for (base_class = cxx_record_decl->bases_begin(),
1844 base_class_end = cxx_record_decl->bases_end();
1845 base_class != base_class_end; ++base_class) {
1846 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1847 base_class->getType()->getAs<RecordType>()->getDecl());
1848 if (RecordHasFields(base_class_decl))
1849 return true;
1850 }
1851 }
1852 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001853}
1854
Adrian Prantl4e8be2c2018-06-13 16:21:24 +00001855#pragma mark Objective-C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001856
Kate Stoneb9c1b512016-09-06 20:57:50 +00001857CompilerType ClangASTContext::CreateObjCClass(const char *name,
1858 DeclContext *decl_ctx,
1859 bool isForwardDecl,
1860 bool isInternal,
1861 ClangASTMetadata *metadata) {
1862 ASTContext *ast = getASTContext();
1863 assert(ast != nullptr);
1864 assert(name && name[0]);
1865 if (decl_ctx == nullptr)
1866 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001867
Kate Stoneb9c1b512016-09-06 20:57:50 +00001868 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1869 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1870 nullptr, SourceLocation(),
1871 /*isForwardDecl,*/
1872 isInternal);
1873
1874 if (decl && metadata)
1875 SetMetadata(ast, decl, *metadata);
1876
1877 return CompilerType(ast, ast->getObjCInterfaceType(decl));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001878}
1879
Kate Stoneb9c1b512016-09-06 20:57:50 +00001880static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001881 return !ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001882}
1883
Greg Clayton57ee3062013-07-11 22:46:58 +00001884uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001885ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1886 bool omit_empty_base_classes) {
1887 uint32_t num_bases = 0;
1888 if (cxx_record_decl) {
1889 if (omit_empty_base_classes) {
1890 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1891 for (base_class = cxx_record_decl->bases_begin(),
1892 base_class_end = cxx_record_decl->bases_end();
1893 base_class != base_class_end; ++base_class) {
1894 // Skip empty base classes
1895 if (omit_empty_base_classes) {
1896 if (BaseSpecifierIsEmpty(base_class))
1897 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001898 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001899 ++num_bases;
1900 }
1901 } else
1902 num_bases = cxx_record_decl->getNumBases();
1903 }
1904 return num_bases;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001905}
1906
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001907#pragma mark Namespace Declarations
1908
Raphael Isemanna9469972019-03-12 07:45:04 +00001909NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1910 const char *name, DeclContext *decl_ctx, bool is_inline) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001911 NamespaceDecl *namespace_decl = nullptr;
1912 ASTContext *ast = getASTContext();
1913 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1914 if (decl_ctx == nullptr)
1915 decl_ctx = translation_unit_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00001916
Kate Stoneb9c1b512016-09-06 20:57:50 +00001917 if (name) {
1918 IdentifierInfo &identifier_info = ast->Idents.get(name);
1919 DeclarationName decl_name(&identifier_info);
1920 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1921 for (NamedDecl *decl : result) {
1922 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1923 if (namespace_decl)
1924 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001925 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001926
1927 namespace_decl =
Raphael Isemanna9469972019-03-12 07:45:04 +00001928 NamespaceDecl::Create(*ast, decl_ctx, is_inline, SourceLocation(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001929 SourceLocation(), &identifier_info, nullptr);
1930
1931 decl_ctx->addDecl(namespace_decl);
1932 } else {
1933 if (decl_ctx == translation_unit_decl) {
1934 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1935 if (namespace_decl)
1936 return namespace_decl;
1937
1938 namespace_decl =
1939 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1940 SourceLocation(), nullptr, nullptr);
1941 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1942 translation_unit_decl->addDecl(namespace_decl);
1943 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
1944 } else {
1945 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1946 if (parent_namespace_decl) {
1947 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1948 if (namespace_decl)
1949 return namespace_decl;
1950 namespace_decl =
1951 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1952 SourceLocation(), nullptr, nullptr);
1953 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1954 parent_namespace_decl->addDecl(namespace_decl);
1955 assert(namespace_decl ==
1956 parent_namespace_decl->getAnonymousNamespace());
1957 } else {
Raphael Isemannc4944812019-07-04 19:49:31 +00001958 assert(false && "GetUniqueNamespaceDeclaration called with no name and "
1959 "no namespace as decl_ctx");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001960 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001961 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001962 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001963#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001964 VerifyDecl(namespace_decl);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001965#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001966 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001967}
1968
Kate Stoneb9c1b512016-09-06 20:57:50 +00001969NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
Raphael Isemanna9469972019-03-12 07:45:04 +00001970 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx,
1971 bool is_inline) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001972 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1973 if (ast_ctx == nullptr)
1974 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +00001975
Raphael Isemanna9469972019-03-12 07:45:04 +00001976 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx, is_inline);
Siva Chandra03ff5c82016-02-05 19:10:04 +00001977}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001978
Paul Hermand628cbb2015-09-15 23:44:17 +00001979clang::BlockDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001980ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1981 if (ctx != nullptr) {
1982 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1983 clang::SourceLocation());
1984 ctx->addDecl(decl);
1985 return decl;
1986 }
1987 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001988}
1989
Kate Stoneb9c1b512016-09-06 20:57:50 +00001990clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1991 clang::DeclContext *right,
1992 clang::DeclContext *root) {
1993 if (root == nullptr)
Paul Hermanea188fc2015-09-16 18:48:30 +00001994 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001995
1996 std::set<clang::DeclContext *> path_left;
1997 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1998 path_left.insert(d);
1999
2000 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
2001 if (path_left.find(d) != path_left.end())
2002 return d;
2003
2004 return nullptr;
Paul Hermanea188fc2015-09-16 18:48:30 +00002005}
2006
Kate Stoneb9c1b512016-09-06 20:57:50 +00002007clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
2008 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
2009 if (decl_ctx != nullptr && ns_decl != nullptr) {
2010 clang::TranslationUnitDecl *translation_unit =
2011 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
2012 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
2013 *getASTContext(), decl_ctx, clang::SourceLocation(),
2014 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
2015 clang::SourceLocation(), ns_decl,
2016 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
2017 decl_ctx->addDecl(using_decl);
2018 return using_decl;
2019 }
2020 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002021}
2022
2023clang::UsingDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00002024ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
2025 clang::NamedDecl *target) {
2026 if (current_decl_ctx != nullptr && target != nullptr) {
2027 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
2028 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
2029 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
2030 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
2031 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
2032 target);
2033 using_decl->addShadowDecl(shadow_decl);
2034 current_decl_ctx->addDecl(using_decl);
2035 return using_decl;
2036 }
2037 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002038}
2039
Kate Stoneb9c1b512016-09-06 20:57:50 +00002040clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
2041 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
2042 if (decl_context != nullptr) {
2043 clang::VarDecl *var_decl = clang::VarDecl::Create(
2044 *getASTContext(), decl_context, clang::SourceLocation(),
2045 clang::SourceLocation(),
2046 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
2047 nullptr, clang::SC_None);
2048 var_decl->setAccess(clang::AS_public);
2049 decl_context->addDecl(var_decl);
2050 return var_decl;
2051 }
2052 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002053}
2054
Zachary Turner9d8a97e2016-04-01 23:20:35 +00002055lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00002056ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
2057 lldb::BasicType basic_type) {
2058 switch (basic_type) {
2059 case eBasicTypeVoid:
2060 return ast->VoidTy.getAsOpaquePtr();
2061 case eBasicTypeChar:
2062 return ast->CharTy.getAsOpaquePtr();
2063 case eBasicTypeSignedChar:
2064 return ast->SignedCharTy.getAsOpaquePtr();
2065 case eBasicTypeUnsignedChar:
2066 return ast->UnsignedCharTy.getAsOpaquePtr();
2067 case eBasicTypeWChar:
2068 return ast->getWCharType().getAsOpaquePtr();
2069 case eBasicTypeSignedWChar:
2070 return ast->getSignedWCharType().getAsOpaquePtr();
2071 case eBasicTypeUnsignedWChar:
2072 return ast->getUnsignedWCharType().getAsOpaquePtr();
2073 case eBasicTypeChar16:
2074 return ast->Char16Ty.getAsOpaquePtr();
2075 case eBasicTypeChar32:
2076 return ast->Char32Ty.getAsOpaquePtr();
2077 case eBasicTypeShort:
2078 return ast->ShortTy.getAsOpaquePtr();
2079 case eBasicTypeUnsignedShort:
2080 return ast->UnsignedShortTy.getAsOpaquePtr();
2081 case eBasicTypeInt:
2082 return ast->IntTy.getAsOpaquePtr();
2083 case eBasicTypeUnsignedInt:
2084 return ast->UnsignedIntTy.getAsOpaquePtr();
2085 case eBasicTypeLong:
2086 return ast->LongTy.getAsOpaquePtr();
2087 case eBasicTypeUnsignedLong:
2088 return ast->UnsignedLongTy.getAsOpaquePtr();
2089 case eBasicTypeLongLong:
2090 return ast->LongLongTy.getAsOpaquePtr();
2091 case eBasicTypeUnsignedLongLong:
2092 return ast->UnsignedLongLongTy.getAsOpaquePtr();
2093 case eBasicTypeInt128:
2094 return ast->Int128Ty.getAsOpaquePtr();
2095 case eBasicTypeUnsignedInt128:
2096 return ast->UnsignedInt128Ty.getAsOpaquePtr();
2097 case eBasicTypeBool:
2098 return ast->BoolTy.getAsOpaquePtr();
2099 case eBasicTypeHalf:
2100 return ast->HalfTy.getAsOpaquePtr();
2101 case eBasicTypeFloat:
2102 return ast->FloatTy.getAsOpaquePtr();
2103 case eBasicTypeDouble:
2104 return ast->DoubleTy.getAsOpaquePtr();
2105 case eBasicTypeLongDouble:
2106 return ast->LongDoubleTy.getAsOpaquePtr();
2107 case eBasicTypeFloatComplex:
2108 return ast->FloatComplexTy.getAsOpaquePtr();
2109 case eBasicTypeDoubleComplex:
2110 return ast->DoubleComplexTy.getAsOpaquePtr();
2111 case eBasicTypeLongDoubleComplex:
2112 return ast->LongDoubleComplexTy.getAsOpaquePtr();
2113 case eBasicTypeObjCID:
2114 return ast->getObjCIdType().getAsOpaquePtr();
2115 case eBasicTypeObjCClass:
2116 return ast->getObjCClassType().getAsOpaquePtr();
2117 case eBasicTypeObjCSel:
2118 return ast->getObjCSelType().getAsOpaquePtr();
2119 case eBasicTypeNullPtr:
2120 return ast->NullPtrTy.getAsOpaquePtr();
2121 default:
2122 return nullptr;
2123 }
Zachary Turner9d8a97e2016-04-01 23:20:35 +00002124}
2125
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002126#pragma mark Function Types
2127
Pavel Labath1ac2b202016-08-15 14:32:32 +00002128clang::DeclarationName
Kate Stoneb9c1b512016-09-06 20:57:50 +00002129ClangASTContext::GetDeclarationName(const char *name,
2130 const CompilerType &function_clang_type) {
2131 if (!name || !name[0])
2132 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002133
Kate Stoneb9c1b512016-09-06 20:57:50 +00002134 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
2135 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
2136 return DeclarationName(&getASTContext()->Idents.get(
2137 name)); // Not operator, but a regular function.
Pavel Labath1ac2b202016-08-15 14:32:32 +00002138
Adrian Prantl05097242018-04-30 16:49:04 +00002139 // Check the number of operator parameters. Sometimes we have seen bad DWARF
2140 // that doesn't correctly describe operators and if we try to create a method
2141 // and add it to the class, clang will assert and crash, so we need to make
2142 // sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002143 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
2144 const clang::FunctionProtoType *function_type =
2145 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
2146 if (function_type == nullptr)
2147 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002148
Kate Stoneb9c1b512016-09-06 20:57:50 +00002149 const bool is_method = false;
2150 const unsigned int num_params = function_type->getNumParams();
2151 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
2152 is_method, op_kind, num_params))
2153 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002154
Kate Stoneb9c1b512016-09-06 20:57:50 +00002155 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
Pavel Labath1ac2b202016-08-15 14:32:32 +00002156}
2157
Kate Stoneb9c1b512016-09-06 20:57:50 +00002158FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
2159 DeclContext *decl_ctx, const char *name,
2160 const CompilerType &function_clang_type, int storage, bool is_inline) {
2161 FunctionDecl *func_decl = nullptr;
2162 ASTContext *ast = getASTContext();
2163 if (decl_ctx == nullptr)
2164 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002165
Kate Stoneb9c1b512016-09-06 20:57:50 +00002166 const bool hasWrittenPrototype = true;
2167 const bool isConstexprSpecified = false;
Greg Clayton0d551042013-06-28 21:08:47 +00002168
Kate Stoneb9c1b512016-09-06 20:57:50 +00002169 clang::DeclarationName declarationName =
2170 GetDeclarationName(name, function_clang_type);
2171 func_decl = FunctionDecl::Create(
2172 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
2173 ClangUtil::GetQualType(function_clang_type), nullptr,
2174 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002175 isConstexprSpecified ? CSK_constexpr : CSK_unspecified);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002176 if (func_decl)
2177 decl_ctx->addDecl(func_decl);
2178
Sean Callanan5e9e1992011-10-26 01:06:27 +00002179#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00002180 VerifyDecl(func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002181#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002182
2183 return func_decl;
2184}
2185
2186CompilerType ClangASTContext::CreateFunctionType(
2187 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002188 unsigned num_args, bool is_variadic, unsigned type_quals,
2189 clang::CallingConv cc) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002190 if (ast == nullptr)
2191 return CompilerType(); // invalid AST
2192
2193 if (!result_type || !ClangUtil::IsClangType(result_type))
2194 return CompilerType(); // invalid return type
2195
2196 std::vector<QualType> qual_type_args;
2197 if (num_args > 0 && args == nullptr)
2198 return CompilerType(); // invalid argument array passed in
2199
2200 // Verify that all arguments are valid and the right type
2201 for (unsigned i = 0; i < num_args; ++i) {
2202 if (args[i]) {
2203 // Make sure we have a clang type in args[i] and not a type from another
2204 // language whose name might match
2205 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2206 lldbassert(is_clang_type);
2207 if (is_clang_type)
2208 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2209 else
2210 return CompilerType(); // invalid argument type (must be a clang type)
2211 } else
2212 return CompilerType(); // invalid argument type (empty)
2213 }
2214
2215 // TODO: Detect calling convention in DWARF?
2216 FunctionProtoType::ExtProtoInfo proto_info;
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002217 proto_info.ExtInfo = cc;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002218 proto_info.Variadic = is_variadic;
2219 proto_info.ExceptionSpec = EST_None;
Mikael Nilsson8b3bf6c2018-12-13 10:17:26 +00002220 proto_info.TypeQuals = clang::Qualifiers::fromFastMask(type_quals);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002221 proto_info.RefQualifier = RQ_None;
2222
2223 return CompilerType(ast,
2224 ast->getFunctionType(ClangUtil::GetQualType(result_type),
2225 qual_type_args, proto_info));
2226}
2227
2228ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
Zachary Turner6753d2d2018-12-12 17:17:53 +00002229 clang::DeclContext *decl_ctx, const char *name,
2230 const CompilerType &param_type, int storage) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002231 ASTContext *ast = getASTContext();
2232 assert(ast != nullptr);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00002233 auto *decl =
2234 ParmVarDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(),
2235 name && name[0] ? &ast->Idents.get(name) : nullptr,
2236 ClangUtil::GetQualType(param_type), nullptr,
2237 (clang::StorageClass)storage, nullptr);
2238 decl_ctx->addDecl(decl);
2239 return decl;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002240}
2241
2242void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2243 ParmVarDecl **params,
2244 unsigned num_params) {
2245 if (function_decl)
2246 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002247}
2248
Greg Claytona1e5dc82015-08-11 22:53:00 +00002249CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002250ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00002251 QualType block_type = m_ast_up->getBlockPointerType(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002252 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
Greg Claytonceeb5212016-05-26 22:33:25 +00002253
Kate Stoneb9c1b512016-09-06 20:57:50 +00002254 return CompilerType(this, block_type.getAsOpaquePtr());
Sean Callananc530ba92016-05-02 21:15:31 +00002255}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002256
2257#pragma mark Array Types
2258
Kate Stoneb9c1b512016-09-06 20:57:50 +00002259CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2260 size_t element_count,
2261 bool is_vector) {
2262 if (element_type.IsValid()) {
2263 ASTContext *ast = getASTContext();
2264 assert(ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002265
Kate Stoneb9c1b512016-09-06 20:57:50 +00002266 if (is_vector) {
2267 return CompilerType(
2268 ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2269 element_count));
2270 } else {
2271
2272 llvm::APInt ap_element_count(64, element_count);
2273 if (element_count == 0) {
2274 return CompilerType(ast, ast->getIncompleteArrayType(
2275 ClangUtil::GetQualType(element_type),
2276 clang::ArrayType::Normal, 0));
2277 } else {
2278 return CompilerType(
2279 ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2280 ap_element_count,
2281 clang::ArrayType::Normal, 0));
2282 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002283 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002284 }
2285 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002286}
2287
Kate Stoneb9c1b512016-09-06 20:57:50 +00002288CompilerType ClangASTContext::CreateStructForIdentifier(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002289 ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002290 const std::initializer_list<std::pair<const char *, CompilerType>>
2291 &type_fields,
2292 bool packed) {
2293 CompilerType type;
2294 if (!type_name.IsEmpty() &&
2295 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2296 .IsValid()) {
Pavel Labathf31c9d22017-01-05 13:18:42 +00002297 lldbassert(0 && "Trying to create a type for an existing name");
Enrico Granata76b08d52014-10-29 23:08:02 +00002298 return type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002299 }
2300
2301 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2302 clang::TTK_Struct, lldb::eLanguageTypeC);
2303 StartTagDeclarationDefinition(type);
2304 for (const auto &field : type_fields)
2305 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2306 0);
2307 if (packed)
2308 SetIsPacked(type);
2309 CompleteTagDeclarationDefinition(type);
2310 return type;
Enrico Granata76b08d52014-10-29 23:08:02 +00002311}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002312
Kate Stoneb9c1b512016-09-06 20:57:50 +00002313CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002314 ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002315 const std::initializer_list<std::pair<const char *, CompilerType>>
2316 &type_fields,
2317 bool packed) {
2318 CompilerType type;
2319 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2320 return type;
Sean Callananc530ba92016-05-02 21:15:31 +00002321
Kate Stoneb9c1b512016-09-06 20:57:50 +00002322 return CreateStructForIdentifier(type_name, type_fields, packed);
Sean Callananc530ba92016-05-02 21:15:31 +00002323}
2324
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002325#pragma mark Enumeration Types
2326
Greg Claytona1e5dc82015-08-11 22:53:00 +00002327CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002328ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2329 const Declaration &decl,
Tamas Berghammer59765832017-11-07 10:39:22 +00002330 const CompilerType &integer_clang_type,
2331 bool is_scoped) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002332 // TODO: Do something intelligent with the Declaration object passed in
2333 // like maybe filling in the SourceLocation with it...
2334 ASTContext *ast = getASTContext();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002335
Kate Stoneb9c1b512016-09-06 20:57:50 +00002336 // TODO: ask about these...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002337 // const bool IsFixed = false;
2338
2339 EnumDecl *enum_decl = EnumDecl::Create(
2340 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2341 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
Tamas Berghammercf6bf4c2017-11-07 13:43:55 +00002342 is_scoped, // IsScoped
2343 is_scoped, // IsScopedUsingClassTag
2344 false); // IsFixed
Kate Stoneb9c1b512016-09-06 20:57:50 +00002345
2346 if (enum_decl) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +00002347 if (decl_ctx)
2348 decl_ctx->addDecl(enum_decl);
2349
Kate Stoneb9c1b512016-09-06 20:57:50 +00002350 // TODO: check if we should be setting the promotion type too?
2351 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2352
2353 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2354
2355 return CompilerType(ast, ast->getTagDeclType(enum_decl));
2356 }
2357 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002358}
2359
Kate Stoneb9c1b512016-09-06 20:57:50 +00002360CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2361 size_t bit_size,
2362 bool is_signed) {
2363 if (ast) {
2364 if (is_signed) {
2365 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
2366 return CompilerType(ast, ast->SignedCharTy);
2367
2368 if (bit_size == ast->getTypeSize(ast->ShortTy))
2369 return CompilerType(ast, ast->ShortTy);
2370
2371 if (bit_size == ast->getTypeSize(ast->IntTy))
2372 return CompilerType(ast, ast->IntTy);
2373
2374 if (bit_size == ast->getTypeSize(ast->LongTy))
2375 return CompilerType(ast, ast->LongTy);
2376
2377 if (bit_size == ast->getTypeSize(ast->LongLongTy))
2378 return CompilerType(ast, ast->LongLongTy);
2379
2380 if (bit_size == ast->getTypeSize(ast->Int128Ty))
2381 return CompilerType(ast, ast->Int128Ty);
2382 } else {
2383 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
2384 return CompilerType(ast, ast->UnsignedCharTy);
2385
2386 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
2387 return CompilerType(ast, ast->UnsignedShortTy);
2388
2389 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
2390 return CompilerType(ast, ast->UnsignedIntTy);
2391
2392 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
2393 return CompilerType(ast, ast->UnsignedLongTy);
2394
2395 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
2396 return CompilerType(ast, ast->UnsignedLongLongTy);
2397
2398 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
2399 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002400 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002401 }
2402 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002403}
2404
Kate Stoneb9c1b512016-09-06 20:57:50 +00002405CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2406 bool is_signed) {
2407 if (ast)
2408 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2409 is_signed);
2410 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002411}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002412
Kate Stoneb9c1b512016-09-06 20:57:50 +00002413void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2414 if (decl_ctx) {
2415 DumpDeclContextHiearchy(decl_ctx->getParent());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002416
Kate Stoneb9c1b512016-09-06 20:57:50 +00002417 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2418 if (named_decl) {
2419 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2420 named_decl->getDeclName().getAsString().c_str());
2421 } else {
2422 printf("%20s\n", decl_ctx->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002423 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002424 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002425}
2426
Kate Stoneb9c1b512016-09-06 20:57:50 +00002427void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2428 if (decl == nullptr)
2429 return;
2430 DumpDeclContextHiearchy(decl->getDeclContext());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002431
Kate Stoneb9c1b512016-09-06 20:57:50 +00002432 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2433 if (record_decl) {
2434 printf("%20s: %s%s\n", decl->getDeclKindName(),
2435 record_decl->getDeclName().getAsString().c_str(),
2436 record_decl->isInjectedClassName() ? " (injected class name)" : "");
Greg Claytone6b36cd2015-12-08 01:02:08 +00002437
Kate Stoneb9c1b512016-09-06 20:57:50 +00002438 } else {
2439 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2440 if (named_decl) {
2441 printf("%20s: %s\n", decl->getDeclKindName(),
2442 named_decl->getDeclName().getAsString().c_str());
2443 } else {
2444 printf("%20s\n", decl->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002445 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002446 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002447}
2448
Kate Stoneb9c1b512016-09-06 20:57:50 +00002449bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2450 clang::Decl *rhs_decl) {
2451 if (lhs_decl && rhs_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002452 // Make sure the decl kinds match first
Kate Stoneb9c1b512016-09-06 20:57:50 +00002453 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2454 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002455
Kate Stoneb9c1b512016-09-06 20:57:50 +00002456 if (lhs_decl_kind == rhs_decl_kind) {
Adrian Prantl05097242018-04-30 16:49:04 +00002457 // Now check that the decl contexts kinds are all equivalent before we
2458 // have to check any names of the decl contexts...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002459 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2460 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2461 if (lhs_decl_ctx && rhs_decl_ctx) {
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00002462 while (true) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002463 if (lhs_decl_ctx && rhs_decl_ctx) {
2464 const clang::Decl::Kind lhs_decl_ctx_kind =
2465 lhs_decl_ctx->getDeclKind();
2466 const clang::Decl::Kind rhs_decl_ctx_kind =
2467 rhs_decl_ctx->getDeclKind();
2468 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2469 lhs_decl_ctx = lhs_decl_ctx->getParent();
2470 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002471
Kate Stoneb9c1b512016-09-06 20:57:50 +00002472 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2473 break;
2474 } else
2475 return false;
2476 } else
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00002477 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002478 }
2479
Kate Stoneb9c1b512016-09-06 20:57:50 +00002480 // Now make sure the name of the decls match
Kate Stoneb9c1b512016-09-06 20:57:50 +00002481 clang::NamedDecl *lhs_named_decl =
2482 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2483 clang::NamedDecl *rhs_named_decl =
2484 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2485 if (lhs_named_decl && rhs_named_decl) {
2486 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2487 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2488 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2489 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2490 return false;
2491 } else
Greg Claytona2721472011-06-25 00:44:06 +00002492 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002493 } else
2494 return false;
Greg Claytona2721472011-06-25 00:44:06 +00002495
Adrian Prantl05097242018-04-30 16:49:04 +00002496 // We know that the decl context kinds all match, so now we need to
2497 // make sure the names match as well
Kate Stoneb9c1b512016-09-06 20:57:50 +00002498 lhs_decl_ctx = lhs_decl->getDeclContext();
2499 rhs_decl_ctx = rhs_decl->getDeclContext();
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00002500 while (true) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002501 switch (lhs_decl_ctx->getDeclKind()) {
2502 case clang::Decl::TranslationUnit:
2503 // We don't care about the translation unit names
2504 return true;
2505 default: {
2506 clang::NamedDecl *lhs_named_decl =
2507 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2508 clang::NamedDecl *rhs_named_decl =
2509 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2510 if (lhs_named_decl && rhs_named_decl) {
2511 clang::DeclarationName lhs_decl_name =
2512 lhs_named_decl->getDeclName();
2513 clang::DeclarationName rhs_decl_name =
2514 rhs_named_decl->getDeclName();
2515 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2516 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2517 return false;
2518 } else
2519 return false;
2520 } else
2521 return false;
2522 } break;
2523 }
2524 lhs_decl_ctx = lhs_decl_ctx->getParent();
2525 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytond8d4a572015-08-11 21:38:15 +00002526 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002527 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002528 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002529 }
2530 return false;
2531}
2532bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2533 clang::Decl *decl) {
2534 if (!decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002535 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002536
Kate Stoneb9c1b512016-09-06 20:57:50 +00002537 ExternalASTSource *ast_source = ast->getExternalSource();
Greg Claytond8d4a572015-08-11 21:38:15 +00002538
Kate Stoneb9c1b512016-09-06 20:57:50 +00002539 if (!ast_source)
Greg Claytond8d4a572015-08-11 21:38:15 +00002540 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002541
2542 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2543 if (tag_decl->isCompleteDefinition())
2544 return true;
2545
2546 if (!tag_decl->hasExternalLexicalStorage())
2547 return false;
2548
2549 ast_source->CompleteType(tag_decl);
2550
2551 return !tag_decl->getTypeForDecl()->isIncompleteType();
2552 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2553 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2554 if (objc_interface_decl->getDefinition())
2555 return true;
2556
2557 if (!objc_interface_decl->hasExternalLexicalStorage())
2558 return false;
2559
2560 ast_source->CompleteType(objc_interface_decl);
2561
2562 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2563 } else {
2564 return false;
2565 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002566}
2567
Kate Stoneb9c1b512016-09-06 20:57:50 +00002568void ClangASTContext::SetMetadataAsUserID(const void *object,
2569 user_id_t user_id) {
2570 ClangASTMetadata meta_data;
2571 meta_data.SetUserID(user_id);
2572 SetMetadata(object, meta_data);
Greg Clayton99558cc42015-08-24 23:46:31 +00002573}
2574
Kate Stoneb9c1b512016-09-06 20:57:50 +00002575void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2576 ClangASTMetadata &metadata) {
2577 ClangExternalASTSourceCommon *external_source =
2578 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2579
2580 if (external_source)
2581 external_source->SetMetadata(object, metadata);
2582}
2583
2584ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2585 const void *object) {
2586 ClangExternalASTSourceCommon *external_source =
2587 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2588
2589 if (external_source && external_source->HasMetadata(object))
2590 return external_source->GetMetadata(object);
2591 else
Greg Claytond8d4a572015-08-11 21:38:15 +00002592 return nullptr;
2593}
2594
Kate Stoneb9c1b512016-09-06 20:57:50 +00002595clang::DeclContext *
2596ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2597 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2598}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002599
Kate Stoneb9c1b512016-09-06 20:57:50 +00002600clang::DeclContext *
2601ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2602 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2603}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002604
Kate Stoneb9c1b512016-09-06 20:57:50 +00002605bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2606 int kind) const {
2607 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2608 if (clang_type) {
2609 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2610 if (tag_type) {
2611 clang::TagDecl *tag_decl =
2612 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2613 if (tag_decl) {
2614 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2615 return true;
2616 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002617 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002618 }
2619 return false;
2620}
2621
2622bool ClangASTContext::SetDefaultAccessForRecordFields(
2623 clang::RecordDecl *record_decl, int default_accessibility,
2624 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2625 if (record_decl) {
2626 uint32_t field_idx;
2627 clang::RecordDecl::field_iterator field, field_end;
2628 for (field = record_decl->field_begin(),
2629 field_end = record_decl->field_end(), field_idx = 0;
2630 field != field_end; ++field, ++field_idx) {
2631 // If no accessibility was assigned, assign the correct one
2632 if (field_idx < num_assigned_accessibilities &&
2633 assigned_accessibilities[field_idx] == clang::AS_none)
2634 field->setAccess((clang::AccessSpecifier)default_accessibility);
2635 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002636 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002637 }
2638 return false;
2639}
2640
2641clang::DeclContext *
2642ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2643 return GetDeclContextForType(ClangUtil::GetQualType(type));
2644}
2645
2646clang::DeclContext *
2647ClangASTContext::GetDeclContextForType(clang::QualType type) {
2648 if (type.isNull())
2649 return nullptr;
2650
2651 clang::QualType qual_type = type.getCanonicalType();
2652 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2653 switch (type_class) {
2654 case clang::Type::ObjCInterface:
2655 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2656 ->getInterface();
2657 case clang::Type::ObjCObjectPointer:
2658 return GetDeclContextForType(
2659 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2660 ->getPointeeType());
2661 case clang::Type::Record:
2662 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2663 case clang::Type::Enum:
2664 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2665 case clang::Type::Typedef:
2666 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2667 ->getDecl()
2668 ->getUnderlyingType());
2669 case clang::Type::Auto:
2670 return GetDeclContextForType(
2671 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2672 case clang::Type::Elaborated:
2673 return GetDeclContextForType(
2674 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2675 case clang::Type::Paren:
2676 return GetDeclContextForType(
2677 llvm::cast<clang::ParenType>(qual_type)->desugar());
2678 default:
2679 break;
2680 }
2681 // No DeclContext in this type...
2682 return nullptr;
2683}
2684
2685static bool GetCompleteQualType(clang::ASTContext *ast,
2686 clang::QualType qual_type,
2687 bool allow_completion = true) {
2688 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2689 switch (type_class) {
2690 case clang::Type::ConstantArray:
2691 case clang::Type::IncompleteArray:
2692 case clang::Type::VariableArray: {
2693 const clang::ArrayType *array_type =
2694 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2695
2696 if (array_type)
2697 return GetCompleteQualType(ast, array_type->getElementType(),
2698 allow_completion);
2699 } break;
2700 case clang::Type::Record: {
2701 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2702 if (cxx_record_decl) {
2703 if (cxx_record_decl->hasExternalLexicalStorage()) {
2704 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2705 const bool fields_loaded =
2706 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2707 if (is_complete && fields_loaded)
2708 return true;
2709
2710 if (!allow_completion)
2711 return false;
2712
2713 // Call the field_begin() accessor to for it to use the external source
2714 // to load the fields...
2715 clang::ExternalASTSource *external_ast_source =
2716 ast->getExternalSource();
2717 if (external_ast_source) {
2718 external_ast_source->CompleteType(cxx_record_decl);
2719 if (cxx_record_decl->isCompleteDefinition()) {
2720 cxx_record_decl->field_begin();
2721 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2722 }
2723 }
2724 }
2725 }
2726 const clang::TagType *tag_type =
2727 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2728 return !tag_type->isIncompleteType();
2729 } break;
2730
2731 case clang::Type::Enum: {
2732 const clang::TagType *tag_type =
2733 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2734 if (tag_type) {
2735 clang::TagDecl *tag_decl = tag_type->getDecl();
2736 if (tag_decl) {
2737 if (tag_decl->getDefinition())
2738 return true;
2739
2740 if (!allow_completion)
2741 return false;
2742
2743 if (tag_decl->hasExternalLexicalStorage()) {
2744 if (ast) {
2745 clang::ExternalASTSource *external_ast_source =
2746 ast->getExternalSource();
2747 if (external_ast_source) {
2748 external_ast_source->CompleteType(tag_decl);
2749 return !tag_type->isIncompleteType();
2750 }
2751 }
2752 }
2753 return false;
2754 }
2755 }
2756
2757 } break;
2758 case clang::Type::ObjCObject:
2759 case clang::Type::ObjCInterface: {
2760 const clang::ObjCObjectType *objc_class_type =
2761 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2762 if (objc_class_type) {
2763 clang::ObjCInterfaceDecl *class_interface_decl =
2764 objc_class_type->getInterface();
2765 // We currently can't complete objective C types through the newly added
Adrian Prantl05097242018-04-30 16:49:04 +00002766 // ASTContext because it only supports TagDecl objects right now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002767 if (class_interface_decl) {
2768 if (class_interface_decl->getDefinition())
2769 return true;
2770
2771 if (!allow_completion)
2772 return false;
2773
2774 if (class_interface_decl->hasExternalLexicalStorage()) {
2775 if (ast) {
2776 clang::ExternalASTSource *external_ast_source =
2777 ast->getExternalSource();
2778 if (external_ast_source) {
2779 external_ast_source->CompleteType(class_interface_decl);
2780 return !objc_class_type->isIncompleteType();
2781 }
2782 }
2783 }
2784 return false;
2785 }
2786 }
2787 } break;
2788
2789 case clang::Type::Typedef:
2790 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2791 ->getDecl()
2792 ->getUnderlyingType(),
2793 allow_completion);
2794
2795 case clang::Type::Auto:
2796 return GetCompleteQualType(
2797 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2798 allow_completion);
2799
2800 case clang::Type::Elaborated:
2801 return GetCompleteQualType(
2802 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2803 allow_completion);
2804
2805 case clang::Type::Paren:
2806 return GetCompleteQualType(
2807 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2808 allow_completion);
2809
2810 case clang::Type::Attributed:
2811 return GetCompleteQualType(
2812 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2813 allow_completion);
2814
2815 default:
2816 break;
2817 }
2818
2819 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00002820}
2821
2822static clang::ObjCIvarDecl::AccessControl
Kate Stoneb9c1b512016-09-06 20:57:50 +00002823ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2824 switch (access) {
2825 case eAccessNone:
Greg Claytond8d4a572015-08-11 21:38:15 +00002826 return clang::ObjCIvarDecl::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002827 case eAccessPublic:
2828 return clang::ObjCIvarDecl::Public;
2829 case eAccessPrivate:
2830 return clang::ObjCIvarDecl::Private;
2831 case eAccessProtected:
2832 return clang::ObjCIvarDecl::Protected;
2833 case eAccessPackage:
2834 return clang::ObjCIvarDecl::Package;
2835 }
2836 return clang::ObjCIvarDecl::None;
Greg Claytond8d4a572015-08-11 21:38:15 +00002837}
2838
Greg Claytond8d4a572015-08-11 21:38:15 +00002839// Tests
Greg Claytond8d4a572015-08-11 21:38:15 +00002840
Kate Stoneb9c1b512016-09-06 20:57:50 +00002841bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2842 clang::QualType qual_type(GetCanonicalQualType(type));
2843
2844 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2845 switch (type_class) {
2846 case clang::Type::IncompleteArray:
2847 case clang::Type::VariableArray:
2848 case clang::Type::ConstantArray:
2849 case clang::Type::ExtVector:
2850 case clang::Type::Vector:
2851 case clang::Type::Record:
2852 case clang::Type::ObjCObject:
2853 case clang::Type::ObjCInterface:
2854 return true;
2855 case clang::Type::Auto:
2856 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2857 ->getDeducedType()
2858 .getAsOpaquePtr());
2859 case clang::Type::Elaborated:
2860 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2861 ->getNamedType()
2862 .getAsOpaquePtr());
2863 case clang::Type::Typedef:
2864 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2865 ->getDecl()
2866 ->getUnderlyingType()
2867 .getAsOpaquePtr());
2868 case clang::Type::Paren:
2869 return IsAggregateType(
2870 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2871 default:
2872 break;
2873 }
2874 // The clang type does have a value
2875 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002876}
2877
Kate Stoneb9c1b512016-09-06 20:57:50 +00002878bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2879 clang::QualType qual_type(GetCanonicalQualType(type));
2880
2881 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2882 switch (type_class) {
2883 case clang::Type::Record: {
2884 if (const clang::RecordType *record_type =
2885 llvm::dyn_cast_or_null<clang::RecordType>(
2886 qual_type.getTypePtrOrNull())) {
2887 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2888 return record_decl->isAnonymousStructOrUnion();
2889 }
Enrico Granata7123e2b2015-11-07 02:06:57 +00002890 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002891 break;
2892 }
2893 case clang::Type::Auto:
2894 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2895 ->getDeducedType()
2896 .getAsOpaquePtr());
2897 case clang::Type::Elaborated:
2898 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2899 ->getNamedType()
2900 .getAsOpaquePtr());
2901 case clang::Type::Typedef:
2902 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2903 ->getDecl()
2904 ->getUnderlyingType()
2905 .getAsOpaquePtr());
2906 case clang::Type::Paren:
2907 return IsAnonymousType(
2908 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2909 default:
2910 break;
2911 }
2912 // The clang type does have a value
2913 return false;
Enrico Granata7123e2b2015-11-07 02:06:57 +00002914}
2915
Kate Stoneb9c1b512016-09-06 20:57:50 +00002916bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2917 CompilerType *element_type_ptr,
2918 uint64_t *size, bool *is_incomplete) {
2919 clang::QualType qual_type(GetCanonicalQualType(type));
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002920
Kate Stoneb9c1b512016-09-06 20:57:50 +00002921 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2922 switch (type_class) {
2923 default:
2924 break;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002925
Kate Stoneb9c1b512016-09-06 20:57:50 +00002926 case clang::Type::ConstantArray:
Greg Claytond8d4a572015-08-11 21:38:15 +00002927 if (element_type_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002928 element_type_ptr->SetCompilerType(
2929 getASTContext(),
2930 llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002931 if (size)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002932 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2933 ->getSize()
2934 .getLimitedValue(ULLONG_MAX);
Greg Claytond8d4a572015-08-11 21:38:15 +00002935 if (is_incomplete)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002936 *is_incomplete = false;
2937 return true;
2938
2939 case clang::Type::IncompleteArray:
2940 if (element_type_ptr)
2941 element_type_ptr->SetCompilerType(
2942 getASTContext(),
2943 llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2944 if (size)
2945 *size = 0;
2946 if (is_incomplete)
2947 *is_incomplete = true;
2948 return true;
2949
2950 case clang::Type::VariableArray:
2951 if (element_type_ptr)
2952 element_type_ptr->SetCompilerType(
2953 getASTContext(),
2954 llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2955 if (size)
2956 *size = 0;
2957 if (is_incomplete)
2958 *is_incomplete = false;
2959 return true;
2960
2961 case clang::Type::DependentSizedArray:
2962 if (element_type_ptr)
2963 element_type_ptr->SetCompilerType(
2964 getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)
2965 ->getElementType());
2966 if (size)
2967 *size = 0;
2968 if (is_incomplete)
2969 *is_incomplete = false;
2970 return true;
2971
2972 case clang::Type::Typedef:
2973 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2974 ->getDecl()
2975 ->getUnderlyingType()
2976 .getAsOpaquePtr(),
2977 element_type_ptr, size, is_incomplete);
2978 case clang::Type::Auto:
2979 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2980 ->getDeducedType()
2981 .getAsOpaquePtr(),
2982 element_type_ptr, size, is_incomplete);
2983 case clang::Type::Elaborated:
2984 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2985 ->getNamedType()
2986 .getAsOpaquePtr(),
2987 element_type_ptr, size, is_incomplete);
2988 case clang::Type::Paren:
2989 return IsArrayType(
2990 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2991 element_type_ptr, size, is_incomplete);
2992 }
2993 if (element_type_ptr)
2994 element_type_ptr->Clear();
2995 if (size)
2996 *size = 0;
2997 if (is_incomplete)
2998 *is_incomplete = false;
2999 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003000}
3001
Kate Stoneb9c1b512016-09-06 20:57:50 +00003002bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
3003 CompilerType *element_type, uint64_t *size) {
3004 clang::QualType qual_type(GetCanonicalQualType(type));
3005
3006 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3007 switch (type_class) {
3008 case clang::Type::Vector: {
3009 const clang::VectorType *vector_type =
3010 qual_type->getAs<clang::VectorType>();
3011 if (vector_type) {
3012 if (size)
3013 *size = vector_type->getNumElements();
3014 if (element_type)
3015 *element_type =
3016 CompilerType(getASTContext(), vector_type->getElementType());
3017 }
3018 return true;
3019 } break;
3020 case clang::Type::ExtVector: {
3021 const clang::ExtVectorType *ext_vector_type =
3022 qual_type->getAs<clang::ExtVectorType>();
3023 if (ext_vector_type) {
3024 if (size)
3025 *size = ext_vector_type->getNumElements();
3026 if (element_type)
3027 *element_type =
3028 CompilerType(getASTContext(), ext_vector_type->getElementType());
3029 }
3030 return true;
3031 }
3032 default:
3033 break;
3034 }
3035 return false;
3036}
3037
3038bool ClangASTContext::IsRuntimeGeneratedType(
3039 lldb::opaque_compiler_type_t type) {
3040 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
3041 ->GetDeclContextForType(GetQualType(type));
3042 if (!decl_ctx)
3043 return false;
3044
3045 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
3046 return false;
3047
3048 clang::ObjCInterfaceDecl *result_iface_decl =
3049 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
3050
3051 ClangASTMetadata *ast_metadata =
3052 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
3053 if (!ast_metadata)
3054 return false;
3055 return (ast_metadata->GetISAPtr() != 0);
3056}
3057
3058bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
3059 return GetQualType(type).getUnqualifiedType()->isCharType();
3060}
3061
3062bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
3063 const bool allow_completion = false;
3064 return GetCompleteQualType(getASTContext(), GetQualType(type),
3065 allow_completion);
3066}
3067
3068bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
3069 return GetQualType(type).isConstQualified();
3070}
3071
3072bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
3073 uint32_t &length) {
3074 CompilerType pointee_or_element_clang_type;
3075 length = 0;
3076 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
3077
3078 if (!pointee_or_element_clang_type.IsValid())
3079 return false;
3080
3081 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
3082 if (pointee_or_element_clang_type.IsCharType()) {
3083 if (type_flags.Test(eTypeIsArray)) {
Adrian Prantl05097242018-04-30 16:49:04 +00003084 // We know the size of the array and it could be a C string since it is
3085 // an array of characters
Kate Stoneb9c1b512016-09-06 20:57:50 +00003086 length = llvm::cast<clang::ConstantArrayType>(
3087 GetCanonicalQualType(type).getTypePtr())
3088 ->getSize()
3089 .getLimitedValue();
3090 }
3091 return true;
3092 }
3093 }
3094 return false;
3095}
3096
3097bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
3098 bool *is_variadic_ptr) {
3099 if (type) {
3100 clang::QualType qual_type(GetCanonicalQualType(type));
3101
3102 if (qual_type->isFunctionType()) {
3103 if (is_variadic_ptr) {
3104 const clang::FunctionProtoType *function_proto_type =
3105 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3106 if (function_proto_type)
3107 *is_variadic_ptr = function_proto_type->isVariadic();
3108 else
3109 *is_variadic_ptr = false;
3110 }
3111 return true;
3112 }
3113
Greg Claytond8d4a572015-08-11 21:38:15 +00003114 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003115 switch (type_class) {
3116 default:
3117 break;
3118 case clang::Type::Typedef:
3119 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
3120 ->getDecl()
3121 ->getUnderlyingType()
3122 .getAsOpaquePtr(),
3123 nullptr);
3124 case clang::Type::Auto:
3125 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
3126 ->getDeducedType()
3127 .getAsOpaquePtr(),
3128 nullptr);
3129 case clang::Type::Elaborated:
3130 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
3131 ->getNamedType()
3132 .getAsOpaquePtr(),
3133 nullptr);
3134 case clang::Type::Paren:
3135 return IsFunctionType(
3136 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3137 nullptr);
3138 case clang::Type::LValueReference:
3139 case clang::Type::RValueReference: {
3140 const clang::ReferenceType *reference_type =
3141 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3142 if (reference_type)
3143 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
3144 nullptr);
3145 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00003146 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003147 }
3148 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003149}
3150
3151// Used to detect "Homogeneous Floating-point Aggregates"
3152uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003153ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
3154 CompilerType *base_type_ptr) {
3155 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003156 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003157
3158 clang::QualType qual_type(GetCanonicalQualType(type));
3159 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3160 switch (type_class) {
3161 case clang::Type::Record:
3162 if (GetCompleteType(type)) {
3163 const clang::CXXRecordDecl *cxx_record_decl =
3164 qual_type->getAsCXXRecordDecl();
3165 if (cxx_record_decl) {
3166 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3167 return 0;
3168 }
3169 const clang::RecordType *record_type =
3170 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3171 if (record_type) {
3172 const clang::RecordDecl *record_decl = record_type->getDecl();
3173 if (record_decl) {
3174 // We are looking for a structure that contains only floating point
3175 // types
3176 clang::RecordDecl::field_iterator field_pos,
3177 field_end = record_decl->field_end();
3178 uint32_t num_fields = 0;
3179 bool is_hva = false;
3180 bool is_hfa = false;
3181 clang::QualType base_qual_type;
3182 uint64_t base_bitwidth = 0;
3183 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3184 ++field_pos) {
3185 clang::QualType field_qual_type = field_pos->getType();
3186 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
3187 if (field_qual_type->isFloatingType()) {
3188 if (field_qual_type->isComplexType())
3189 return 0;
3190 else {
3191 if (num_fields == 0)
3192 base_qual_type = field_qual_type;
3193 else {
3194 if (is_hva)
3195 return 0;
3196 is_hfa = true;
3197 if (field_qual_type.getTypePtr() !=
3198 base_qual_type.getTypePtr())
3199 return 0;
3200 }
3201 }
3202 } else if (field_qual_type->isVectorType() ||
3203 field_qual_type->isExtVectorType()) {
3204 if (num_fields == 0) {
3205 base_qual_type = field_qual_type;
3206 base_bitwidth = field_bitwidth;
3207 } else {
3208 if (is_hfa)
3209 return 0;
3210 is_hva = true;
3211 if (base_bitwidth != field_bitwidth)
3212 return 0;
3213 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3214 return 0;
3215 }
3216 } else
3217 return 0;
3218 ++num_fields;
3219 }
3220 if (base_type_ptr)
3221 *base_type_ptr = CompilerType(getASTContext(), base_qual_type);
3222 return num_fields;
3223 }
3224 }
3225 }
3226 break;
3227
3228 case clang::Type::Typedef:
3229 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3230 ->getDecl()
3231 ->getUnderlyingType()
3232 .getAsOpaquePtr(),
3233 base_type_ptr);
3234
3235 case clang::Type::Auto:
3236 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3237 ->getDeducedType()
3238 .getAsOpaquePtr(),
3239 base_type_ptr);
3240
3241 case clang::Type::Elaborated:
3242 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3243 ->getNamedType()
3244 .getAsOpaquePtr(),
3245 base_type_ptr);
3246 default:
3247 break;
3248 }
3249 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003250}
3251
Kate Stoneb9c1b512016-09-06 20:57:50 +00003252size_t ClangASTContext::GetNumberOfFunctionArguments(
3253 lldb::opaque_compiler_type_t type) {
3254 if (type) {
3255 clang::QualType qual_type(GetCanonicalQualType(type));
3256 const clang::FunctionProtoType *func =
3257 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3258 if (func)
3259 return func->getNumParams();
3260 }
3261 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003262}
3263
Greg Claytona1e5dc82015-08-11 22:53:00 +00003264CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00003265ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3266 const size_t index) {
3267 if (type) {
Greg Claytond8d4a572015-08-11 21:38:15 +00003268 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003269 const clang::FunctionProtoType *func =
3270 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3271 if (func) {
3272 if (index < func->getNumParams())
3273 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00003274 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003275 }
3276 return CompilerType();
3277}
3278
3279bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3280 if (type) {
3281 clang::QualType qual_type(GetCanonicalQualType(type));
3282
3283 if (qual_type->isFunctionPointerType())
3284 return true;
3285
3286 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3287 switch (type_class) {
3288 default:
3289 break;
3290 case clang::Type::Typedef:
3291 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3292 ->getDecl()
3293 ->getUnderlyingType()
3294 .getAsOpaquePtr());
3295 case clang::Type::Auto:
3296 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3297 ->getDeducedType()
3298 .getAsOpaquePtr());
3299 case clang::Type::Elaborated:
3300 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3301 ->getNamedType()
3302 .getAsOpaquePtr());
3303 case clang::Type::Paren:
3304 return IsFunctionPointerType(
3305 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3306
3307 case clang::Type::LValueReference:
3308 case clang::Type::RValueReference: {
3309 const clang::ReferenceType *reference_type =
3310 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3311 if (reference_type)
3312 return IsFunctionPointerType(
3313 reference_type->getPointeeType().getAsOpaquePtr());
3314 } break;
3315 }
3316 }
3317 return false;
3318}
3319
3320bool ClangASTContext::IsBlockPointerType(
3321 lldb::opaque_compiler_type_t type,
3322 CompilerType *function_pointer_type_ptr) {
3323 if (type) {
3324 clang::QualType qual_type(GetCanonicalQualType(type));
3325
3326 if (qual_type->isBlockPointerType()) {
3327 if (function_pointer_type_ptr) {
3328 const clang::BlockPointerType *block_pointer_type =
3329 qual_type->getAs<clang::BlockPointerType>();
3330 QualType pointee_type = block_pointer_type->getPointeeType();
Jonas Devlieghered5b44032019-02-13 06:25:41 +00003331 QualType function_pointer_type = m_ast_up->getPointerType(pointee_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003332 *function_pointer_type_ptr =
3333 CompilerType(getASTContext(), function_pointer_type);
3334 }
3335 return true;
3336 }
3337
3338 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3339 switch (type_class) {
3340 default:
3341 break;
3342 case clang::Type::Typedef:
3343 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3344 ->getDecl()
3345 ->getUnderlyingType()
3346 .getAsOpaquePtr(),
3347 function_pointer_type_ptr);
3348 case clang::Type::Auto:
3349 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3350 ->getDeducedType()
3351 .getAsOpaquePtr(),
3352 function_pointer_type_ptr);
3353 case clang::Type::Elaborated:
3354 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3355 ->getNamedType()
3356 .getAsOpaquePtr(),
3357 function_pointer_type_ptr);
3358 case clang::Type::Paren:
3359 return IsBlockPointerType(
3360 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3361 function_pointer_type_ptr);
3362
3363 case clang::Type::LValueReference:
3364 case clang::Type::RValueReference: {
3365 const clang::ReferenceType *reference_type =
3366 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3367 if (reference_type)
3368 return IsBlockPointerType(
3369 reference_type->getPointeeType().getAsOpaquePtr(),
3370 function_pointer_type_ptr);
3371 } break;
3372 }
3373 }
3374 return false;
3375}
3376
3377bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3378 bool &is_signed) {
3379 if (!type)
3380 return false;
3381
3382 clang::QualType qual_type(GetCanonicalQualType(type));
3383 const clang::BuiltinType *builtin_type =
3384 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3385
3386 if (builtin_type) {
3387 if (builtin_type->isInteger()) {
3388 is_signed = builtin_type->isSignedInteger();
3389 return true;
3390 }
3391 }
3392
3393 return false;
3394}
3395
3396bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3397 bool &is_signed) {
3398 if (type) {
3399 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3400 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3401
3402 if (enum_type) {
3403 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3404 is_signed);
3405 return true;
3406 }
3407 }
3408
3409 return false;
3410}
3411
3412bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3413 CompilerType *pointee_type) {
3414 if (type) {
3415 clang::QualType qual_type(GetCanonicalQualType(type));
3416 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3417 switch (type_class) {
3418 case clang::Type::Builtin:
3419 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3420 default:
3421 break;
3422 case clang::BuiltinType::ObjCId:
3423 case clang::BuiltinType::ObjCClass:
3424 return true;
3425 }
3426 return false;
3427 case clang::Type::ObjCObjectPointer:
3428 if (pointee_type)
3429 pointee_type->SetCompilerType(
3430 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3431 ->getPointeeType());
3432 return true;
3433 case clang::Type::BlockPointer:
3434 if (pointee_type)
3435 pointee_type->SetCompilerType(
3436 getASTContext(),
3437 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3438 return true;
3439 case clang::Type::Pointer:
3440 if (pointee_type)
3441 pointee_type->SetCompilerType(
3442 getASTContext(),
3443 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3444 return true;
3445 case clang::Type::MemberPointer:
3446 if (pointee_type)
3447 pointee_type->SetCompilerType(
3448 getASTContext(),
3449 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3450 return true;
3451 case clang::Type::Typedef:
3452 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3453 ->getDecl()
3454 ->getUnderlyingType()
3455 .getAsOpaquePtr(),
3456 pointee_type);
3457 case clang::Type::Auto:
3458 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3459 ->getDeducedType()
3460 .getAsOpaquePtr(),
3461 pointee_type);
3462 case clang::Type::Elaborated:
3463 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3464 ->getNamedType()
3465 .getAsOpaquePtr(),
3466 pointee_type);
3467 case clang::Type::Paren:
3468 return IsPointerType(
3469 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3470 pointee_type);
3471 default:
3472 break;
3473 }
3474 }
3475 if (pointee_type)
3476 pointee_type->Clear();
3477 return false;
3478}
3479
3480bool ClangASTContext::IsPointerOrReferenceType(
3481 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3482 if (type) {
3483 clang::QualType qual_type(GetCanonicalQualType(type));
3484 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3485 switch (type_class) {
3486 case clang::Type::Builtin:
3487 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3488 default:
3489 break;
3490 case clang::BuiltinType::ObjCId:
3491 case clang::BuiltinType::ObjCClass:
3492 return true;
3493 }
3494 return false;
3495 case clang::Type::ObjCObjectPointer:
3496 if (pointee_type)
3497 pointee_type->SetCompilerType(
3498 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3499 ->getPointeeType());
3500 return true;
3501 case clang::Type::BlockPointer:
3502 if (pointee_type)
3503 pointee_type->SetCompilerType(
3504 getASTContext(),
3505 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3506 return true;
3507 case clang::Type::Pointer:
3508 if (pointee_type)
3509 pointee_type->SetCompilerType(
3510 getASTContext(),
3511 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3512 return true;
3513 case clang::Type::MemberPointer:
3514 if (pointee_type)
3515 pointee_type->SetCompilerType(
3516 getASTContext(),
3517 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3518 return true;
3519 case clang::Type::LValueReference:
3520 if (pointee_type)
3521 pointee_type->SetCompilerType(
3522 getASTContext(),
3523 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3524 return true;
3525 case clang::Type::RValueReference:
3526 if (pointee_type)
3527 pointee_type->SetCompilerType(
3528 getASTContext(),
3529 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3530 return true;
3531 case clang::Type::Typedef:
3532 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3533 ->getDecl()
3534 ->getUnderlyingType()
3535 .getAsOpaquePtr(),
3536 pointee_type);
3537 case clang::Type::Auto:
3538 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3539 ->getDeducedType()
3540 .getAsOpaquePtr(),
3541 pointee_type);
3542 case clang::Type::Elaborated:
3543 return IsPointerOrReferenceType(
3544 llvm::cast<clang::ElaboratedType>(qual_type)
3545 ->getNamedType()
3546 .getAsOpaquePtr(),
3547 pointee_type);
3548 case clang::Type::Paren:
3549 return IsPointerOrReferenceType(
3550 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3551 pointee_type);
3552 default:
3553 break;
3554 }
3555 }
3556 if (pointee_type)
3557 pointee_type->Clear();
3558 return false;
3559}
3560
3561bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3562 CompilerType *pointee_type,
3563 bool *is_rvalue) {
3564 if (type) {
3565 clang::QualType qual_type(GetCanonicalQualType(type));
3566 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3567
3568 switch (type_class) {
3569 case clang::Type::LValueReference:
3570 if (pointee_type)
3571 pointee_type->SetCompilerType(
3572 getASTContext(),
3573 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3574 if (is_rvalue)
3575 *is_rvalue = false;
3576 return true;
3577 case clang::Type::RValueReference:
3578 if (pointee_type)
3579 pointee_type->SetCompilerType(
3580 getASTContext(),
3581 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3582 if (is_rvalue)
3583 *is_rvalue = true;
3584 return true;
3585 case clang::Type::Typedef:
3586 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3587 ->getDecl()
3588 ->getUnderlyingType()
3589 .getAsOpaquePtr(),
3590 pointee_type, is_rvalue);
3591 case clang::Type::Auto:
3592 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3593 ->getDeducedType()
3594 .getAsOpaquePtr(),
3595 pointee_type, is_rvalue);
3596 case clang::Type::Elaborated:
3597 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3598 ->getNamedType()
3599 .getAsOpaquePtr(),
3600 pointee_type, is_rvalue);
3601 case clang::Type::Paren:
3602 return IsReferenceType(
3603 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3604 pointee_type, is_rvalue);
3605
3606 default:
3607 break;
3608 }
3609 }
3610 if (pointee_type)
3611 pointee_type->Clear();
3612 return false;
3613}
3614
3615bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3616 uint32_t &count, bool &is_complex) {
3617 if (type) {
3618 clang::QualType qual_type(GetCanonicalQualType(type));
3619
3620 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3621 qual_type->getCanonicalTypeInternal())) {
3622 clang::BuiltinType::Kind kind = BT->getKind();
3623 if (kind >= clang::BuiltinType::Float &&
3624 kind <= clang::BuiltinType::LongDouble) {
3625 count = 1;
3626 is_complex = false;
3627 return true;
3628 }
3629 } else if (const clang::ComplexType *CT =
3630 llvm::dyn_cast<clang::ComplexType>(
3631 qual_type->getCanonicalTypeInternal())) {
3632 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3633 is_complex)) {
3634 count = 2;
3635 is_complex = true;
3636 return true;
3637 }
3638 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3639 qual_type->getCanonicalTypeInternal())) {
3640 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3641 is_complex)) {
3642 count = VT->getNumElements();
3643 is_complex = false;
3644 return true;
3645 }
3646 }
3647 }
3648 count = 0;
3649 is_complex = false;
3650 return false;
3651}
3652
3653bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3654 if (!type)
3655 return false;
3656
3657 clang::QualType qual_type(GetQualType(type));
3658 const clang::TagType *tag_type =
3659 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3660 if (tag_type) {
3661 clang::TagDecl *tag_decl = tag_type->getDecl();
3662 if (tag_decl)
3663 return tag_decl->isCompleteDefinition();
3664 return false;
3665 } else {
3666 const clang::ObjCObjectType *objc_class_type =
3667 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3668 if (objc_class_type) {
3669 clang::ObjCInterfaceDecl *class_interface_decl =
3670 objc_class_type->getInterface();
3671 if (class_interface_decl)
3672 return class_interface_decl->getDefinition() != nullptr;
3673 return false;
3674 }
3675 }
3676 return true;
3677}
3678
3679bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
3680 if (type) {
3681 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3682
3683 const clang::ObjCObjectPointerType *obj_pointer_type =
3684 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3685
3686 if (obj_pointer_type)
3687 return obj_pointer_type->isObjCClassType();
3688 }
3689 return false;
3690}
3691
3692bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3693 if (ClangUtil::IsClangType(type))
3694 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3695 return false;
3696}
3697
3698bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3699 if (!type)
3700 return false;
3701 clang::QualType qual_type(GetCanonicalQualType(type));
3702 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3703 return (type_class == clang::Type::Record);
3704}
3705
3706bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3707 if (!type)
3708 return false;
3709 clang::QualType qual_type(GetCanonicalQualType(type));
3710 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3711 return (type_class == clang::Type::Enum);
3712}
3713
3714bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3715 if (type) {
3716 clang::QualType qual_type(GetCanonicalQualType(type));
3717 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3718 switch (type_class) {
3719 case clang::Type::Record:
3720 if (GetCompleteType(type)) {
3721 const clang::RecordType *record_type =
3722 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3723 const clang::RecordDecl *record_decl = record_type->getDecl();
3724 if (record_decl) {
3725 const clang::CXXRecordDecl *cxx_record_decl =
3726 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3727 if (cxx_record_decl)
3728 return cxx_record_decl->isPolymorphic();
Greg Claytond8d4a572015-08-11 21:38:15 +00003729 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003730 }
3731 break;
3732
3733 default:
3734 break;
3735 }
3736 }
3737 return false;
3738}
3739
3740bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3741 CompilerType *dynamic_pointee_type,
3742 bool check_cplusplus,
3743 bool check_objc) {
3744 clang::QualType pointee_qual_type;
3745 if (type) {
3746 clang::QualType qual_type(GetCanonicalQualType(type));
3747 bool success = false;
3748 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3749 switch (type_class) {
3750 case clang::Type::Builtin:
3751 if (check_objc &&
3752 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3753 clang::BuiltinType::ObjCId) {
3754 if (dynamic_pointee_type)
3755 dynamic_pointee_type->SetCompilerType(this, type);
3756 return true;
3757 }
3758 break;
3759
3760 case clang::Type::ObjCObjectPointer:
3761 if (check_objc) {
3762 if (auto objc_pointee_type =
3763 qual_type->getPointeeType().getTypePtrOrNull()) {
3764 if (auto objc_object_type =
3765 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3766 objc_pointee_type)) {
3767 if (objc_object_type->isObjCClass())
3768 return false;
3769 }
3770 }
3771 if (dynamic_pointee_type)
3772 dynamic_pointee_type->SetCompilerType(
3773 getASTContext(),
3774 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3775 ->getPointeeType());
3776 return true;
3777 }
3778 break;
3779
3780 case clang::Type::Pointer:
3781 pointee_qual_type =
3782 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3783 success = true;
3784 break;
3785
3786 case clang::Type::LValueReference:
3787 case clang::Type::RValueReference:
3788 pointee_qual_type =
3789 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3790 success = true;
3791 break;
3792
3793 case clang::Type::Typedef:
3794 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3795 ->getDecl()
3796 ->getUnderlyingType()
3797 .getAsOpaquePtr(),
3798 dynamic_pointee_type, check_cplusplus,
3799 check_objc);
3800
3801 case clang::Type::Auto:
3802 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3803 ->getDeducedType()
3804 .getAsOpaquePtr(),
3805 dynamic_pointee_type, check_cplusplus,
3806 check_objc);
3807
3808 case clang::Type::Elaborated:
3809 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3810 ->getNamedType()
3811 .getAsOpaquePtr(),
3812 dynamic_pointee_type, check_cplusplus,
3813 check_objc);
3814
3815 case clang::Type::Paren:
3816 return IsPossibleDynamicType(
3817 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3818 dynamic_pointee_type, check_cplusplus, check_objc);
3819 default:
3820 break;
3821 }
3822
3823 if (success) {
3824 // Check to make sure what we are pointing too is a possible dynamic C++
Adrian Prantl05097242018-04-30 16:49:04 +00003825 // type We currently accept any "void *" (in case we have a class that
3826 // has been watered down to an opaque pointer) and virtual C++ classes.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003827 const clang::Type::TypeClass pointee_type_class =
3828 pointee_qual_type.getCanonicalType()->getTypeClass();
3829 switch (pointee_type_class) {
3830 case clang::Type::Builtin:
3831 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3832 case clang::BuiltinType::UnknownAny:
3833 case clang::BuiltinType::Void:
3834 if (dynamic_pointee_type)
3835 dynamic_pointee_type->SetCompilerType(getASTContext(),
3836 pointee_qual_type);
3837 return true;
3838 default:
3839 break;
3840 }
3841 break;
3842
3843 case clang::Type::Record:
3844 if (check_cplusplus) {
3845 clang::CXXRecordDecl *cxx_record_decl =
3846 pointee_qual_type->getAsCXXRecordDecl();
3847 if (cxx_record_decl) {
3848 bool is_complete = cxx_record_decl->isCompleteDefinition();
3849
3850 if (is_complete)
3851 success = cxx_record_decl->isDynamicClass();
3852 else {
3853 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3854 getASTContext(), cxx_record_decl);
3855 if (metadata)
3856 success = metadata->GetIsDynamicCXXType();
3857 else {
3858 is_complete = CompilerType(getASTContext(), pointee_qual_type)
3859 .GetCompleteType();
3860 if (is_complete)
3861 success = cxx_record_decl->isDynamicClass();
3862 else
3863 success = false;
3864 }
3865 }
3866
3867 if (success) {
3868 if (dynamic_pointee_type)
3869 dynamic_pointee_type->SetCompilerType(getASTContext(),
3870 pointee_qual_type);
3871 return true;
3872 }
3873 }
3874 }
3875 break;
3876
3877 case clang::Type::ObjCObject:
3878 case clang::Type::ObjCInterface:
3879 if (check_objc) {
3880 if (dynamic_pointee_type)
3881 dynamic_pointee_type->SetCompilerType(getASTContext(),
3882 pointee_qual_type);
3883 return true;
3884 }
3885 break;
3886
3887 default:
3888 break;
3889 }
3890 }
3891 }
3892 if (dynamic_pointee_type)
3893 dynamic_pointee_type->Clear();
3894 return false;
3895}
3896
3897bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3898 if (!type)
3899 return false;
3900
3901 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3902}
3903
3904bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3905 if (!type)
3906 return false;
3907 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3908}
3909
3910bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3911 if (!type)
3912 return false;
3913 return GetCanonicalQualType(type)->isVoidType();
3914}
3915
Alex Langforda03e2b22019-06-04 19:29:59 +00003916bool ClangASTContext::CanPassInRegisters(const CompilerType &type) {
3917 if (auto *record_decl =
3918 ClangASTContext::GetAsRecordDecl(type)) {
3919 return record_decl->canPassInRegisters();
3920 }
3921 return false;
3922}
3923
Kate Stoneb9c1b512016-09-06 20:57:50 +00003924bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3925 return ClangASTContextSupportsLanguage(language);
3926}
3927
3928bool ClangASTContext::GetCXXClassName(const CompilerType &type,
3929 std::string &class_name) {
3930 if (type) {
3931 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3932 if (!qual_type.isNull()) {
3933 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3934 if (cxx_record_decl) {
3935 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3936 return true;
3937 }
3938 }
3939 }
3940 class_name.clear();
3941 return false;
3942}
3943
3944bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3945 if (!type)
3946 return false;
3947
3948 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Jonas Devliegherea6682a42018-12-15 00:15:33 +00003949 return !qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003950}
3951
3952bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3953 if (!type)
3954 return false;
3955 clang::QualType qual_type(GetCanonicalQualType(type));
3956 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3957 if (tag_type)
3958 return tag_type->isBeingDefined();
3959 return false;
3960}
3961
3962bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3963 CompilerType *class_type_ptr) {
3964 if (!type)
3965 return false;
3966
3967 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3968
3969 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3970 if (class_type_ptr) {
3971 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3972 const clang::ObjCObjectPointerType *obj_pointer_type =
3973 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3974 if (obj_pointer_type == nullptr)
3975 class_type_ptr->Clear();
3976 else
3977 class_type_ptr->SetCompilerType(
3978 type.GetTypeSystem(),
3979 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3980 .getAsOpaquePtr());
3981 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003982 }
3983 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003984 }
3985 if (class_type_ptr)
3986 class_type_ptr->Clear();
3987 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003988}
3989
Kate Stoneb9c1b512016-09-06 20:57:50 +00003990bool ClangASTContext::GetObjCClassName(const CompilerType &type,
3991 std::string &class_name) {
3992 if (!type)
3993 return false;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003994
Kate Stoneb9c1b512016-09-06 20:57:50 +00003995 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3996
3997 const clang::ObjCObjectType *object_type =
3998 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3999 if (object_type) {
4000 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
4001 if (interface) {
4002 class_name = interface->getNameAsString();
4003 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00004004 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004005 }
4006 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00004007}
4008
Greg Claytond8d4a572015-08-11 21:38:15 +00004009// Type Completion
Greg Claytond8d4a572015-08-11 21:38:15 +00004010
Kate Stoneb9c1b512016-09-06 20:57:50 +00004011bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
4012 if (!type)
4013 return false;
4014 const bool allow_completion = true;
4015 return GetCompleteQualType(getASTContext(), GetQualType(type),
4016 allow_completion);
Greg Claytond8d4a572015-08-11 21:38:15 +00004017}
4018
Kate Stoneb9c1b512016-09-06 20:57:50 +00004019ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
4020 std::string type_name;
4021 if (type) {
4022 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
4023 clang::QualType qual_type(GetQualType(type));
4024 printing_policy.SuppressTagKeyword = true;
4025 const clang::TypedefType *typedef_type =
4026 qual_type->getAs<clang::TypedefType>();
4027 if (typedef_type) {
4028 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
4029 type_name = typedef_decl->getQualifiedNameAsString();
4030 } else {
4031 type_name = qual_type.getAsString(printing_policy);
Greg Claytond8d4a572015-08-11 21:38:15 +00004032 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004033 }
4034 return ConstString(type_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00004035}
4036
4037uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004038ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
4039 CompilerType *pointee_or_element_clang_type) {
4040 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004041 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004042
4043 if (pointee_or_element_clang_type)
4044 pointee_or_element_clang_type->Clear();
4045
4046 clang::QualType qual_type(GetQualType(type));
4047
4048 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4049 switch (type_class) {
Sean Callananddf802a2017-06-02 01:24:18 +00004050 case clang::Type::Attributed:
4051 return GetTypeInfo(
4052 qual_type->getAs<clang::AttributedType>()
4053 ->getModifiedType().getAsOpaquePtr(),
4054 pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004055 case clang::Type::Builtin: {
4056 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
4057 qual_type->getCanonicalTypeInternal());
4058
4059 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
4060 switch (builtin_type->getKind()) {
4061 case clang::BuiltinType::ObjCId:
4062 case clang::BuiltinType::ObjCClass:
4063 if (pointee_or_element_clang_type)
4064 pointee_or_element_clang_type->SetCompilerType(
4065 getASTContext(), getASTContext()->ObjCBuiltinClassTy);
4066 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
4067 break;
4068
4069 case clang::BuiltinType::ObjCSel:
4070 if (pointee_or_element_clang_type)
4071 pointee_or_element_clang_type->SetCompilerType(getASTContext(),
4072 getASTContext()->CharTy);
4073 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
4074 break;
4075
4076 case clang::BuiltinType::Bool:
4077 case clang::BuiltinType::Char_U:
4078 case clang::BuiltinType::UChar:
4079 case clang::BuiltinType::WChar_U:
4080 case clang::BuiltinType::Char16:
4081 case clang::BuiltinType::Char32:
4082 case clang::BuiltinType::UShort:
4083 case clang::BuiltinType::UInt:
4084 case clang::BuiltinType::ULong:
4085 case clang::BuiltinType::ULongLong:
4086 case clang::BuiltinType::UInt128:
4087 case clang::BuiltinType::Char_S:
4088 case clang::BuiltinType::SChar:
4089 case clang::BuiltinType::WChar_S:
4090 case clang::BuiltinType::Short:
4091 case clang::BuiltinType::Int:
4092 case clang::BuiltinType::Long:
4093 case clang::BuiltinType::LongLong:
4094 case clang::BuiltinType::Int128:
4095 case clang::BuiltinType::Float:
4096 case clang::BuiltinType::Double:
4097 case clang::BuiltinType::LongDouble:
4098 builtin_type_flags |= eTypeIsScalar;
4099 if (builtin_type->isInteger()) {
4100 builtin_type_flags |= eTypeIsInteger;
4101 if (builtin_type->isSignedInteger())
4102 builtin_type_flags |= eTypeIsSigned;
4103 } else if (builtin_type->isFloatingPoint())
4104 builtin_type_flags |= eTypeIsFloat;
4105 break;
4106 default:
4107 break;
4108 }
4109 return builtin_type_flags;
4110 }
4111
4112 case clang::Type::BlockPointer:
4113 if (pointee_or_element_clang_type)
4114 pointee_or_element_clang_type->SetCompilerType(
4115 getASTContext(), qual_type->getPointeeType());
4116 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
4117
4118 case clang::Type::Complex: {
4119 uint32_t complex_type_flags =
4120 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
4121 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
4122 qual_type->getCanonicalTypeInternal());
4123 if (complex_type) {
4124 clang::QualType complex_element_type(complex_type->getElementType());
4125 if (complex_element_type->isIntegerType())
4126 complex_type_flags |= eTypeIsFloat;
4127 else if (complex_element_type->isFloatingType())
4128 complex_type_flags |= eTypeIsInteger;
4129 }
4130 return complex_type_flags;
4131 } break;
4132
4133 case clang::Type::ConstantArray:
4134 case clang::Type::DependentSizedArray:
4135 case clang::Type::IncompleteArray:
4136 case clang::Type::VariableArray:
4137 if (pointee_or_element_clang_type)
4138 pointee_or_element_clang_type->SetCompilerType(
4139 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
4140 ->getElementType());
4141 return eTypeHasChildren | eTypeIsArray;
4142
4143 case clang::Type::DependentName:
4144 return 0;
4145 case clang::Type::DependentSizedExtVector:
4146 return eTypeHasChildren | eTypeIsVector;
4147 case clang::Type::DependentTemplateSpecialization:
4148 return eTypeIsTemplate;
4149 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004150 return CompilerType(
4151 getASTContext(),
4152 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
4153 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004154
4155 case clang::Type::Enum:
4156 if (pointee_or_element_clang_type)
4157 pointee_or_element_clang_type->SetCompilerType(
4158 getASTContext(),
4159 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
4160 return eTypeIsEnumeration | eTypeHasValue;
4161
4162 case clang::Type::Auto:
4163 return CompilerType(
4164 getASTContext(),
4165 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4166 .GetTypeInfo(pointee_or_element_clang_type);
4167 case clang::Type::Elaborated:
4168 return CompilerType(
4169 getASTContext(),
4170 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4171 .GetTypeInfo(pointee_or_element_clang_type);
4172 case clang::Type::Paren:
4173 return CompilerType(getASTContext(),
4174 llvm::cast<clang::ParenType>(qual_type)->desugar())
4175 .GetTypeInfo(pointee_or_element_clang_type);
4176
4177 case clang::Type::FunctionProto:
4178 return eTypeIsFuncPrototype | eTypeHasValue;
4179 case clang::Type::FunctionNoProto:
4180 return eTypeIsFuncPrototype | eTypeHasValue;
4181 case clang::Type::InjectedClassName:
4182 return 0;
4183
4184 case clang::Type::LValueReference:
4185 case clang::Type::RValueReference:
4186 if (pointee_or_element_clang_type)
4187 pointee_or_element_clang_type->SetCompilerType(
4188 getASTContext(),
4189 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4190 ->getPointeeType());
4191 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4192
4193 case clang::Type::MemberPointer:
4194 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4195
4196 case clang::Type::ObjCObjectPointer:
4197 if (pointee_or_element_clang_type)
4198 pointee_or_element_clang_type->SetCompilerType(
4199 getASTContext(), qual_type->getPointeeType());
4200 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4201 eTypeHasValue;
4202
4203 case clang::Type::ObjCObject:
4204 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4205 case clang::Type::ObjCInterface:
4206 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4207
4208 case clang::Type::Pointer:
4209 if (pointee_or_element_clang_type)
4210 pointee_or_element_clang_type->SetCompilerType(
4211 getASTContext(), qual_type->getPointeeType());
4212 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4213
4214 case clang::Type::Record:
4215 if (qual_type->getAsCXXRecordDecl())
4216 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4217 else
4218 return eTypeHasChildren | eTypeIsStructUnion;
4219 break;
4220 case clang::Type::SubstTemplateTypeParm:
4221 return eTypeIsTemplate;
4222 case clang::Type::TemplateTypeParm:
4223 return eTypeIsTemplate;
4224 case clang::Type::TemplateSpecialization:
4225 return eTypeIsTemplate;
4226
4227 case clang::Type::Typedef:
4228 return eTypeIsTypedef |
4229 CompilerType(getASTContext(),
4230 llvm::cast<clang::TypedefType>(qual_type)
4231 ->getDecl()
4232 ->getUnderlyingType())
4233 .GetTypeInfo(pointee_or_element_clang_type);
4234 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004235 return CompilerType(getASTContext(),
4236 llvm::cast<clang::TypeOfExprType>(qual_type)
4237 ->getUnderlyingExpr()
4238 ->getType())
4239 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004240 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004241 return CompilerType(
4242 getASTContext(),
4243 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4244 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004245 case clang::Type::UnresolvedUsing:
4246 return 0;
4247
4248 case clang::Type::ExtVector:
4249 case clang::Type::Vector: {
4250 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4251 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4252 qual_type->getCanonicalTypeInternal());
4253 if (vector_type) {
4254 if (vector_type->isIntegerType())
4255 vector_type_flags |= eTypeIsFloat;
4256 else if (vector_type->isFloatingType())
4257 vector_type_flags |= eTypeIsInteger;
4258 }
4259 return vector_type_flags;
4260 }
4261 default:
4262 return 0;
4263 }
4264 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004265}
4266
Greg Claytond8d4a572015-08-11 21:38:15 +00004267lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004268ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4269 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004270 return lldb::eLanguageTypeC;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004271
4272 // If the type is a reference, then resolve it to what it refers to first:
4273 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4274 if (qual_type->isAnyPointerType()) {
4275 if (qual_type->isObjCObjectPointerType())
4276 return lldb::eLanguageTypeObjC;
Adrian Prantl1db0f0c2019-05-02 23:07:23 +00004277 if (qual_type->getPointeeCXXRecordDecl())
4278 return lldb::eLanguageTypeC_plus_plus;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004279
4280 clang::QualType pointee_type(qual_type->getPointeeType());
Adrian Prantl1db0f0c2019-05-02 23:07:23 +00004281 if (pointee_type->getPointeeCXXRecordDecl())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004282 return lldb::eLanguageTypeC_plus_plus;
4283 if (pointee_type->isObjCObjectOrInterfaceType())
4284 return lldb::eLanguageTypeObjC;
4285 if (pointee_type->isObjCClassType())
4286 return lldb::eLanguageTypeObjC;
4287 if (pointee_type.getTypePtr() ==
4288 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4289 return lldb::eLanguageTypeObjC;
4290 } else {
4291 if (qual_type->isObjCObjectOrInterfaceType())
4292 return lldb::eLanguageTypeObjC;
4293 if (qual_type->getAsCXXRecordDecl())
4294 return lldb::eLanguageTypeC_plus_plus;
4295 switch (qual_type->getTypeClass()) {
4296 default:
4297 break;
4298 case clang::Type::Builtin:
4299 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4300 default:
4301 case clang::BuiltinType::Void:
4302 case clang::BuiltinType::Bool:
4303 case clang::BuiltinType::Char_U:
4304 case clang::BuiltinType::UChar:
4305 case clang::BuiltinType::WChar_U:
4306 case clang::BuiltinType::Char16:
4307 case clang::BuiltinType::Char32:
4308 case clang::BuiltinType::UShort:
4309 case clang::BuiltinType::UInt:
4310 case clang::BuiltinType::ULong:
4311 case clang::BuiltinType::ULongLong:
4312 case clang::BuiltinType::UInt128:
4313 case clang::BuiltinType::Char_S:
4314 case clang::BuiltinType::SChar:
4315 case clang::BuiltinType::WChar_S:
4316 case clang::BuiltinType::Short:
4317 case clang::BuiltinType::Int:
4318 case clang::BuiltinType::Long:
4319 case clang::BuiltinType::LongLong:
4320 case clang::BuiltinType::Int128:
4321 case clang::BuiltinType::Float:
4322 case clang::BuiltinType::Double:
4323 case clang::BuiltinType::LongDouble:
4324 break;
4325
4326 case clang::BuiltinType::NullPtr:
4327 return eLanguageTypeC_plus_plus;
4328
4329 case clang::BuiltinType::ObjCId:
4330 case clang::BuiltinType::ObjCClass:
4331 case clang::BuiltinType::ObjCSel:
4332 return eLanguageTypeObjC;
4333
4334 case clang::BuiltinType::Dependent:
4335 case clang::BuiltinType::Overload:
4336 case clang::BuiltinType::BoundMember:
4337 case clang::BuiltinType::UnknownAny:
4338 break;
4339 }
4340 break;
4341 case clang::Type::Typedef:
4342 return CompilerType(getASTContext(),
4343 llvm::cast<clang::TypedefType>(qual_type)
4344 ->getDecl()
4345 ->getUnderlyingType())
4346 .GetMinimumLanguage();
4347 }
4348 }
4349 return lldb::eLanguageTypeC;
Greg Claytond8d4a572015-08-11 21:38:15 +00004350}
4351
4352lldb::TypeClass
Kate Stoneb9c1b512016-09-06 20:57:50 +00004353ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4354 if (!type)
4355 return lldb::eTypeClassInvalid;
4356
4357 clang::QualType qual_type(GetQualType(type));
4358
4359 switch (qual_type->getTypeClass()) {
4360 case clang::Type::UnaryTransform:
4361 break;
4362 case clang::Type::FunctionNoProto:
4363 return lldb::eTypeClassFunction;
4364 case clang::Type::FunctionProto:
4365 return lldb::eTypeClassFunction;
4366 case clang::Type::IncompleteArray:
4367 return lldb::eTypeClassArray;
4368 case clang::Type::VariableArray:
4369 return lldb::eTypeClassArray;
4370 case clang::Type::ConstantArray:
4371 return lldb::eTypeClassArray;
4372 case clang::Type::DependentSizedArray:
4373 return lldb::eTypeClassArray;
4374 case clang::Type::DependentSizedExtVector:
4375 return lldb::eTypeClassVector;
Fangrui Song8f284882018-07-13 22:40:40 +00004376 case clang::Type::DependentVector:
4377 return lldb::eTypeClassVector;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004378 case clang::Type::ExtVector:
4379 return lldb::eTypeClassVector;
4380 case clang::Type::Vector:
4381 return lldb::eTypeClassVector;
4382 case clang::Type::Builtin:
4383 return lldb::eTypeClassBuiltin;
4384 case clang::Type::ObjCObjectPointer:
4385 return lldb::eTypeClassObjCObjectPointer;
4386 case clang::Type::BlockPointer:
4387 return lldb::eTypeClassBlockPointer;
4388 case clang::Type::Pointer:
4389 return lldb::eTypeClassPointer;
4390 case clang::Type::LValueReference:
4391 return lldb::eTypeClassReference;
4392 case clang::Type::RValueReference:
4393 return lldb::eTypeClassReference;
4394 case clang::Type::MemberPointer:
4395 return lldb::eTypeClassMemberPointer;
4396 case clang::Type::Complex:
4397 if (qual_type->isComplexType())
4398 return lldb::eTypeClassComplexFloat;
4399 else
4400 return lldb::eTypeClassComplexInteger;
4401 case clang::Type::ObjCObject:
4402 return lldb::eTypeClassObjCObject;
4403 case clang::Type::ObjCInterface:
4404 return lldb::eTypeClassObjCInterface;
4405 case clang::Type::Record: {
4406 const clang::RecordType *record_type =
4407 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4408 const clang::RecordDecl *record_decl = record_type->getDecl();
4409 if (record_decl->isUnion())
4410 return lldb::eTypeClassUnion;
4411 else if (record_decl->isStruct())
4412 return lldb::eTypeClassStruct;
4413 else
4414 return lldb::eTypeClassClass;
4415 } break;
4416 case clang::Type::Enum:
4417 return lldb::eTypeClassEnumeration;
4418 case clang::Type::Typedef:
4419 return lldb::eTypeClassTypedef;
4420 case clang::Type::UnresolvedUsing:
4421 break;
4422 case clang::Type::Paren:
4423 return CompilerType(getASTContext(),
4424 llvm::cast<clang::ParenType>(qual_type)->desugar())
4425 .GetTypeClass();
4426 case clang::Type::Auto:
4427 return CompilerType(
4428 getASTContext(),
4429 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4430 .GetTypeClass();
4431 case clang::Type::Elaborated:
4432 return CompilerType(
4433 getASTContext(),
4434 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4435 .GetTypeClass();
4436
4437 case clang::Type::Attributed:
4438 break;
4439 case clang::Type::TemplateTypeParm:
4440 break;
4441 case clang::Type::SubstTemplateTypeParm:
4442 break;
4443 case clang::Type::SubstTemplateTypeParmPack:
4444 break;
4445 case clang::Type::InjectedClassName:
4446 break;
4447 case clang::Type::DependentName:
4448 break;
4449 case clang::Type::DependentTemplateSpecialization:
4450 break;
4451 case clang::Type::PackExpansion:
4452 break;
4453
4454 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004455 return CompilerType(getASTContext(),
4456 llvm::cast<clang::TypeOfExprType>(qual_type)
4457 ->getUnderlyingExpr()
4458 ->getType())
4459 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004460 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004461 return CompilerType(
4462 getASTContext(),
4463 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4464 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004465 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004466 return CompilerType(
4467 getASTContext(),
4468 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4469 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004470 case clang::Type::TemplateSpecialization:
4471 break;
Pavel Labath4f19fce22017-02-17 13:39:50 +00004472 case clang::Type::DeducedTemplateSpecialization:
4473 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004474 case clang::Type::Atomic:
4475 break;
4476 case clang::Type::Pipe:
4477 break;
4478
4479 // pointer type decayed from an array or function type.
4480 case clang::Type::Decayed:
4481 break;
4482 case clang::Type::Adjusted:
4483 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00004484 case clang::Type::ObjCTypeParam:
4485 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00004486
4487 case clang::Type::DependentAddressSpace:
4488 break;
Krasimir Georgiev435e76a2019-05-07 13:59:30 +00004489 case clang::Type::MacroQualified:
4490 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004491 }
4492 // We don't know hot to display this type...
4493 return lldb::eTypeClassOther;
Greg Claytond8d4a572015-08-11 21:38:15 +00004494}
4495
Kate Stoneb9c1b512016-09-06 20:57:50 +00004496unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4497 if (type)
4498 return GetQualType(type).getQualifiers().getCVRQualifiers();
4499 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004500}
4501
Greg Claytond8d4a572015-08-11 21:38:15 +00004502// Creating related types
Greg Claytond8d4a572015-08-11 21:38:15 +00004503
Greg Claytona1e5dc82015-08-11 22:53:00 +00004504CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004505ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4506 uint64_t *stride) {
4507 if (type) {
4508 clang::QualType qual_type(GetCanonicalQualType(type));
4509
4510 const clang::Type *array_eletype =
4511 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4512
4513 if (!array_eletype)
4514 return CompilerType();
4515
4516 CompilerType element_type(getASTContext(),
4517 array_eletype->getCanonicalTypeUnqualified());
4518
4519 // TODO: the real stride will be >= this value.. find the real one!
4520 if (stride)
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00004521 if (Optional<uint64_t> size = element_type.GetByteSize(nullptr))
Adrian Prantld963a7c2019-01-15 18:07:52 +00004522 *stride = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004523
4524 return element_type;
4525 }
4526 return CompilerType();
4527}
4528
4529CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4530 uint64_t size) {
4531 if (type) {
4532 clang::QualType qual_type(GetCanonicalQualType(type));
4533 if (clang::ASTContext *ast_ctx = getASTContext()) {
4534 if (size != 0)
4535 return CompilerType(
4536 ast_ctx, ast_ctx->getConstantArrayType(
4537 qual_type, llvm::APInt(64, size),
4538 clang::ArrayType::ArraySizeModifier::Normal, 0));
4539 else
4540 return CompilerType(
4541 ast_ctx,
4542 ast_ctx->getIncompleteArrayType(
4543 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
Greg Claytond8d4a572015-08-11 21:38:15 +00004544 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004545 }
4546
4547 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004548}
4549
Greg Claytona1e5dc82015-08-11 22:53:00 +00004550CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004551ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4552 if (type)
4553 return CompilerType(getASTContext(), GetCanonicalQualType(type));
4554 return CompilerType();
4555}
4556
4557static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4558 clang::QualType qual_type) {
4559 if (qual_type->isPointerType())
4560 qual_type = ast->getPointerType(
4561 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4562 else
4563 qual_type = qual_type.getUnqualifiedType();
4564 qual_type.removeLocalConst();
4565 qual_type.removeLocalRestrict();
4566 qual_type.removeLocalVolatile();
4567 return qual_type;
Enrico Granata639392f2016-08-30 20:39:58 +00004568}
4569
4570CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004571ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4572 if (type)
4573 return CompilerType(
4574 getASTContext(),
4575 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4576 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004577}
4578
Kate Stoneb9c1b512016-09-06 20:57:50 +00004579int ClangASTContext::GetFunctionArgumentCount(
4580 lldb::opaque_compiler_type_t type) {
4581 if (type) {
4582 const clang::FunctionProtoType *func =
4583 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4584 if (func)
4585 return func->getNumParams();
4586 }
4587 return -1;
4588}
4589
4590CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4591 lldb::opaque_compiler_type_t type, size_t idx) {
4592 if (type) {
4593 const clang::FunctionProtoType *func =
4594 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4595 if (func) {
4596 const uint32_t num_args = func->getNumParams();
4597 if (idx < num_args)
4598 return CompilerType(getASTContext(), func->getParamType(idx));
4599 }
4600 }
4601 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004602}
4603
Greg Claytona1e5dc82015-08-11 22:53:00 +00004604CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004605ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4606 if (type) {
4607 clang::QualType qual_type(GetQualType(type));
4608 const clang::FunctionProtoType *func =
4609 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4610 if (func)
4611 return CompilerType(getASTContext(), func->getReturnType());
4612 }
4613 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004614}
4615
4616size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004617ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4618 size_t num_functions = 0;
4619 if (type) {
4620 clang::QualType qual_type(GetCanonicalQualType(type));
4621 switch (qual_type->getTypeClass()) {
4622 case clang::Type::Record:
4623 if (GetCompleteQualType(getASTContext(), qual_type)) {
4624 const clang::RecordType *record_type =
4625 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4626 const clang::RecordDecl *record_decl = record_type->getDecl();
4627 assert(record_decl);
4628 const clang::CXXRecordDecl *cxx_record_decl =
4629 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4630 if (cxx_record_decl)
4631 num_functions = std::distance(cxx_record_decl->method_begin(),
4632 cxx_record_decl->method_end());
4633 }
4634 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004635
Sean Callananf9c622a2016-09-30 18:44:43 +00004636 case clang::Type::ObjCObjectPointer: {
4637 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004638 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004639 const clang::ObjCInterfaceType *objc_interface_type =
4640 objc_class_type->getInterfaceType();
4641 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004642 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4643 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004644 clang::ObjCInterfaceDecl *class_interface_decl =
4645 objc_interface_type->getDecl();
4646 if (class_interface_decl) {
4647 num_functions = std::distance(class_interface_decl->meth_begin(),
4648 class_interface_decl->meth_end());
Greg Claytond8d4a572015-08-11 21:38:15 +00004649 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004650 }
4651 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004652 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004653
4654 case clang::Type::ObjCObject:
4655 case clang::Type::ObjCInterface:
4656 if (GetCompleteType(type)) {
4657 const clang::ObjCObjectType *objc_class_type =
4658 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4659 if (objc_class_type) {
4660 clang::ObjCInterfaceDecl *class_interface_decl =
4661 objc_class_type->getInterface();
4662 if (class_interface_decl)
4663 num_functions = std::distance(class_interface_decl->meth_begin(),
4664 class_interface_decl->meth_end());
4665 }
4666 }
4667 break;
4668
4669 case clang::Type::Typedef:
4670 return CompilerType(getASTContext(),
4671 llvm::cast<clang::TypedefType>(qual_type)
4672 ->getDecl()
4673 ->getUnderlyingType())
4674 .GetNumMemberFunctions();
4675
4676 case clang::Type::Auto:
4677 return CompilerType(
4678 getASTContext(),
4679 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4680 .GetNumMemberFunctions();
4681
4682 case clang::Type::Elaborated:
4683 return CompilerType(
4684 getASTContext(),
4685 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4686 .GetNumMemberFunctions();
4687
4688 case clang::Type::Paren:
4689 return CompilerType(getASTContext(),
4690 llvm::cast<clang::ParenType>(qual_type)->desugar())
4691 .GetNumMemberFunctions();
4692
4693 default:
4694 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004695 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004696 }
4697 return num_functions;
Greg Claytond8d4a572015-08-11 21:38:15 +00004698}
4699
4700TypeMemberFunctionImpl
Kate Stoneb9c1b512016-09-06 20:57:50 +00004701ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4702 size_t idx) {
4703 std::string name;
4704 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4705 CompilerType clang_type;
4706 CompilerDecl clang_decl;
4707 if (type) {
4708 clang::QualType qual_type(GetCanonicalQualType(type));
4709 switch (qual_type->getTypeClass()) {
4710 case clang::Type::Record:
4711 if (GetCompleteQualType(getASTContext(), qual_type)) {
4712 const clang::RecordType *record_type =
4713 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4714 const clang::RecordDecl *record_decl = record_type->getDecl();
4715 assert(record_decl);
4716 const clang::CXXRecordDecl *cxx_record_decl =
4717 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4718 if (cxx_record_decl) {
4719 auto method_iter = cxx_record_decl->method_begin();
4720 auto method_end = cxx_record_decl->method_end();
4721 if (idx <
4722 static_cast<size_t>(std::distance(method_iter, method_end))) {
4723 std::advance(method_iter, idx);
4724 clang::CXXMethodDecl *cxx_method_decl =
4725 method_iter->getCanonicalDecl();
4726 if (cxx_method_decl) {
4727 name = cxx_method_decl->getDeclName().getAsString();
4728 if (cxx_method_decl->isStatic())
4729 kind = lldb::eMemberFunctionKindStaticMethod;
4730 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4731 kind = lldb::eMemberFunctionKindConstructor;
4732 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4733 kind = lldb::eMemberFunctionKindDestructor;
4734 else
4735 kind = lldb::eMemberFunctionKindInstanceMethod;
4736 clang_type = CompilerType(
4737 this, cxx_method_decl->getType().getAsOpaquePtr());
4738 clang_decl = CompilerDecl(this, cxx_method_decl);
4739 }
4740 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004741 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004742 }
4743 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004744
Sean Callananf9c622a2016-09-30 18:44:43 +00004745 case clang::Type::ObjCObjectPointer: {
4746 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004747 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004748 const clang::ObjCInterfaceType *objc_interface_type =
4749 objc_class_type->getInterfaceType();
4750 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004751 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4752 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004753 clang::ObjCInterfaceDecl *class_interface_decl =
4754 objc_interface_type->getDecl();
4755 if (class_interface_decl) {
4756 auto method_iter = class_interface_decl->meth_begin();
4757 auto method_end = class_interface_decl->meth_end();
4758 if (idx <
4759 static_cast<size_t>(std::distance(method_iter, method_end))) {
4760 std::advance(method_iter, idx);
4761 clang::ObjCMethodDecl *objc_method_decl =
4762 method_iter->getCanonicalDecl();
4763 if (objc_method_decl) {
4764 clang_decl = CompilerDecl(this, objc_method_decl);
4765 name = objc_method_decl->getSelector().getAsString();
4766 if (objc_method_decl->isClassMethod())
4767 kind = lldb::eMemberFunctionKindStaticMethod;
4768 else
4769 kind = lldb::eMemberFunctionKindInstanceMethod;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004770 }
4771 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004772 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004773 }
4774 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004775 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004776
Kate Stoneb9c1b512016-09-06 20:57:50 +00004777 case clang::Type::ObjCObject:
4778 case clang::Type::ObjCInterface:
4779 if (GetCompleteType(type)) {
4780 const clang::ObjCObjectType *objc_class_type =
4781 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4782 if (objc_class_type) {
4783 clang::ObjCInterfaceDecl *class_interface_decl =
4784 objc_class_type->getInterface();
4785 if (class_interface_decl) {
4786 auto method_iter = class_interface_decl->meth_begin();
4787 auto method_end = class_interface_decl->meth_end();
4788 if (idx <
4789 static_cast<size_t>(std::distance(method_iter, method_end))) {
4790 std::advance(method_iter, idx);
4791 clang::ObjCMethodDecl *objc_method_decl =
4792 method_iter->getCanonicalDecl();
4793 if (objc_method_decl) {
4794 clang_decl = CompilerDecl(this, objc_method_decl);
4795 name = objc_method_decl->getSelector().getAsString();
4796 if (objc_method_decl->isClassMethod())
4797 kind = lldb::eMemberFunctionKindStaticMethod;
4798 else
4799 kind = lldb::eMemberFunctionKindInstanceMethod;
4800 }
4801 }
4802 }
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004803 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004804 }
4805 break;
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004806
Kate Stoneb9c1b512016-09-06 20:57:50 +00004807 case clang::Type::Typedef:
4808 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4809 ->getDecl()
4810 ->getUnderlyingType()
4811 .getAsOpaquePtr(),
4812 idx);
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004813
Kate Stoneb9c1b512016-09-06 20:57:50 +00004814 case clang::Type::Auto:
4815 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4816 ->getDeducedType()
4817 .getAsOpaquePtr(),
4818 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004819
Kate Stoneb9c1b512016-09-06 20:57:50 +00004820 case clang::Type::Elaborated:
4821 return GetMemberFunctionAtIndex(
4822 llvm::cast<clang::ElaboratedType>(qual_type)
4823 ->getNamedType()
4824 .getAsOpaquePtr(),
4825 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004826
Kate Stoneb9c1b512016-09-06 20:57:50 +00004827 case clang::Type::Paren:
4828 return GetMemberFunctionAtIndex(
4829 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4830 idx);
4831
4832 default:
4833 break;
Greg Clayton56939cb2015-09-17 22:23:34 +00004834 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004835 }
Greg Clayton56939cb2015-09-17 22:23:34 +00004836
Kate Stoneb9c1b512016-09-06 20:57:50 +00004837 if (kind == eMemberFunctionKindUnknown)
4838 return TypeMemberFunctionImpl();
4839 else
4840 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
Greg Clayton56939cb2015-09-17 22:23:34 +00004841}
4842
Greg Claytona1e5dc82015-08-11 22:53:00 +00004843CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004844ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4845 if (type)
4846 return CompilerType(getASTContext(),
4847 GetQualType(type).getNonReferenceType());
4848 return CompilerType();
4849}
4850
4851CompilerType ClangASTContext::CreateTypedefType(
4852 const CompilerType &type, const char *typedef_name,
4853 const CompilerDeclContext &compiler_decl_ctx) {
4854 if (type && typedef_name && typedef_name[0]) {
4855 ClangASTContext *ast =
4856 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4857 if (!ast)
4858 return CompilerType();
4859 clang::ASTContext *clang_ast = ast->getASTContext();
4860 clang::QualType qual_type(ClangUtil::GetQualType(type));
4861
4862 clang::DeclContext *decl_ctx =
4863 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4864 if (decl_ctx == nullptr)
4865 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4866
4867 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4868 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4869 &clang_ast->Idents.get(typedef_name),
4870 clang_ast->getTrivialTypeSourceInfo(qual_type));
4871
4872 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4873
Aleksandr Urakov709426b2018-09-10 08:08:43 +00004874 decl_ctx->addDecl(decl);
4875
Kate Stoneb9c1b512016-09-06 20:57:50 +00004876 // Get a uniqued clang::QualType for the typedef decl type
4877 return CompilerType(clang_ast, clang_ast->getTypedefType(decl));
4878 }
4879 return CompilerType();
4880}
4881
4882CompilerType
4883ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4884 if (type) {
4885 clang::QualType qual_type(GetQualType(type));
4886 return CompilerType(getASTContext(),
4887 qual_type.getTypePtr()->getPointeeType());
4888 }
4889 return CompilerType();
4890}
4891
4892CompilerType
4893ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4894 if (type) {
4895 clang::QualType qual_type(GetQualType(type));
4896
4897 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4898 switch (type_class) {
4899 case clang::Type::ObjCObject:
4900 case clang::Type::ObjCInterface:
4901 return CompilerType(getASTContext(),
4902 getASTContext()->getObjCObjectPointerType(qual_type));
4903
4904 default:
4905 return CompilerType(getASTContext(),
4906 getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004907 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004908 }
4909 return CompilerType();
4910}
4911
4912CompilerType
4913ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4914 if (type)
4915 return CompilerType(this, getASTContext()
4916 ->getLValueReferenceType(GetQualType(type))
4917 .getAsOpaquePtr());
4918 else
Greg Claytona1e5dc82015-08-11 22:53:00 +00004919 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004920}
4921
Kate Stoneb9c1b512016-09-06 20:57:50 +00004922CompilerType
4923ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4924 if (type)
4925 return CompilerType(this, getASTContext()
4926 ->getRValueReferenceType(GetQualType(type))
4927 .getAsOpaquePtr());
4928 else
4929 return CompilerType();
4930}
4931
4932CompilerType
4933ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4934 if (type) {
4935 clang::QualType result(GetQualType(type));
4936 result.addConst();
4937 return CompilerType(this, result.getAsOpaquePtr());
4938 }
4939 return CompilerType();
4940}
4941
4942CompilerType
4943ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4944 if (type) {
4945 clang::QualType result(GetQualType(type));
4946 result.addVolatile();
4947 return CompilerType(this, result.getAsOpaquePtr());
4948 }
4949 return CompilerType();
4950}
4951
4952CompilerType
4953ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4954 if (type) {
4955 clang::QualType result(GetQualType(type));
4956 result.addRestrict();
4957 return CompilerType(this, result.getAsOpaquePtr());
4958 }
4959 return CompilerType();
4960}
4961
4962CompilerType
4963ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4964 const char *typedef_name,
4965 const CompilerDeclContext &compiler_decl_ctx) {
4966 if (type) {
4967 clang::ASTContext *clang_ast = getASTContext();
4968 clang::QualType qual_type(GetQualType(type));
4969
4970 clang::DeclContext *decl_ctx =
4971 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4972 if (decl_ctx == nullptr)
4973 decl_ctx = getASTContext()->getTranslationUnitDecl();
4974
4975 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4976 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4977 &clang_ast->Idents.get(typedef_name),
4978 clang_ast->getTrivialTypeSourceInfo(qual_type));
4979
4980 clang::TagDecl *tdecl = nullptr;
4981 if (!qual_type.isNull()) {
4982 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4983 tdecl = rt->getDecl();
4984 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4985 tdecl = et->getDecl();
4986 }
4987
4988 // Check whether this declaration is an anonymous struct, union, or enum,
Adrian Prantl05097242018-04-30 16:49:04 +00004989 // hidden behind a typedef. If so, we try to check whether we have a
4990 // typedef tag to attach to the original record declaration
Kate Stoneb9c1b512016-09-06 20:57:50 +00004991 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4992 tdecl->setTypedefNameForAnonDecl(decl);
4993
4994 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4995
4996 // Get a uniqued clang::QualType for the typedef decl type
4997 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4998 }
4999 return CompilerType();
5000}
5001
5002CompilerType
5003ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
5004 if (type) {
5005 const clang::TypedefType *typedef_type =
5006 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
5007 if (typedef_type)
5008 return CompilerType(getASTContext(),
5009 typedef_type->getDecl()->getUnderlyingType());
5010 }
5011 return CompilerType();
5012}
Greg Claytond8d4a572015-08-11 21:38:15 +00005013
Greg Claytond8d4a572015-08-11 21:38:15 +00005014// Create related types using the current type's AST
Greg Claytond8d4a572015-08-11 21:38:15 +00005015
Kate Stoneb9c1b512016-09-06 20:57:50 +00005016CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
5017 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005018}
Greg Claytond8d4a572015-08-11 21:38:15 +00005019// Exploring the type
Greg Claytond8d4a572015-08-11 21:38:15 +00005020
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005021Optional<uint64_t>
5022ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
5023 ExecutionContextScope *exe_scope) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005024 if (GetCompleteType(type)) {
Greg Claytond8d4a572015-08-11 21:38:15 +00005025 clang::QualType qual_type(GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00005026 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005027 switch (type_class) {
5028 case clang::Type::Record:
5029 if (GetCompleteType(type))
5030 return getASTContext()->getTypeSize(qual_type);
5031 else
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005032 return None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005033 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00005034
Kate Stoneb9c1b512016-09-06 20:57:50 +00005035 case clang::Type::ObjCInterface:
5036 case clang::Type::ObjCObject: {
5037 ExecutionContext exe_ctx(exe_scope);
5038 Process *process = exe_ctx.GetProcessPtr();
5039 if (process) {
Alex Langforde823bbe2019-06-10 20:53:23 +00005040 ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(*process);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005041 if (objc_runtime) {
5042 uint64_t bit_size = 0;
5043 if (objc_runtime->GetTypeBitSize(
5044 CompilerType(getASTContext(), qual_type), bit_size))
5045 return bit_size;
5046 }
5047 } else {
5048 static bool g_printed = false;
5049 if (!g_printed) {
5050 StreamString s;
5051 DumpTypeDescription(type, &s);
5052
5053 llvm::outs() << "warning: trying to determine the size of type ";
5054 llvm::outs() << s.GetString() << "\n";
5055 llvm::outs() << "without a valid ExecutionContext. this is not "
5056 "reliable. please file a bug against LLDB.\n";
5057 llvm::outs() << "backtrace:\n";
5058 llvm::sys::PrintStackTrace(llvm::outs());
5059 llvm::outs() << "\n";
5060 g_printed = true;
5061 }
5062 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005063 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005064 LLVM_FALLTHROUGH;
5065 default:
5066 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
5067 if (bit_size == 0) {
5068 if (qual_type->isIncompleteArrayType())
5069 return getASTContext()->getTypeSize(
5070 qual_type->getArrayElementTypeNoTypeQual()
5071 ->getCanonicalTypeUnqualified());
5072 }
5073 if (qual_type->isObjCObjectOrInterfaceType())
5074 return bit_size +
5075 getASTContext()->getTypeSize(
5076 getASTContext()->ObjCBuiltinClassTy);
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005077 // Function types actually have a size of 0, that's not an error.
5078 if (qual_type->isFunctionProtoType())
5079 return bit_size;
5080 if (bit_size)
5081 return bit_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005082 }
5083 }
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005084 return None;
Greg Claytond8d4a572015-08-11 21:38:15 +00005085}
5086
Kate Stoneb9c1b512016-09-06 20:57:50 +00005087size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
5088 if (GetCompleteType(type))
5089 return getASTContext()->getTypeAlign(GetQualType(type));
5090 return 0;
5091}
5092
5093lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
5094 uint64_t &count) {
5095 if (!type)
5096 return lldb::eEncodingInvalid;
5097
5098 count = 1;
5099 clang::QualType qual_type(GetCanonicalQualType(type));
5100
5101 switch (qual_type->getTypeClass()) {
5102 case clang::Type::UnaryTransform:
5103 break;
5104
5105 case clang::Type::FunctionNoProto:
5106 case clang::Type::FunctionProto:
5107 break;
5108
5109 case clang::Type::IncompleteArray:
5110 case clang::Type::VariableArray:
5111 break;
5112
5113 case clang::Type::ConstantArray:
5114 break;
5115
Fangrui Song8f284882018-07-13 22:40:40 +00005116 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005117 case clang::Type::ExtVector:
5118 case clang::Type::Vector:
5119 // TODO: Set this to more than one???
5120 break;
5121
5122 case clang::Type::Builtin:
5123 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5124 case clang::BuiltinType::Void:
5125 break;
5126
5127 case clang::BuiltinType::Bool:
5128 case clang::BuiltinType::Char_S:
5129 case clang::BuiltinType::SChar:
5130 case clang::BuiltinType::WChar_S:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005131 case clang::BuiltinType::Short:
5132 case clang::BuiltinType::Int:
5133 case clang::BuiltinType::Long:
5134 case clang::BuiltinType::LongLong:
5135 case clang::BuiltinType::Int128:
5136 return lldb::eEncodingSint;
5137
5138 case clang::BuiltinType::Char_U:
5139 case clang::BuiltinType::UChar:
5140 case clang::BuiltinType::WChar_U:
Richard Smith51d12d82018-05-02 02:43:22 +00005141 case clang::BuiltinType::Char8:
5142 case clang::BuiltinType::Char16:
5143 case clang::BuiltinType::Char32:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005144 case clang::BuiltinType::UShort:
5145 case clang::BuiltinType::UInt:
5146 case clang::BuiltinType::ULong:
5147 case clang::BuiltinType::ULongLong:
5148 case clang::BuiltinType::UInt128:
5149 return lldb::eEncodingUint;
5150
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005151 // Fixed point types. Note that they are currently ignored.
5152 case clang::BuiltinType::ShortAccum:
5153 case clang::BuiltinType::Accum:
5154 case clang::BuiltinType::LongAccum:
5155 case clang::BuiltinType::UShortAccum:
5156 case clang::BuiltinType::UAccum:
5157 case clang::BuiltinType::ULongAccum:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005158 case clang::BuiltinType::ShortFract:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005159 case clang::BuiltinType::Fract:
5160 case clang::BuiltinType::LongFract:
5161 case clang::BuiltinType::UShortFract:
5162 case clang::BuiltinType::UFract:
5163 case clang::BuiltinType::ULongFract:
5164 case clang::BuiltinType::SatShortAccum:
5165 case clang::BuiltinType::SatAccum:
5166 case clang::BuiltinType::SatLongAccum:
5167 case clang::BuiltinType::SatUShortAccum:
5168 case clang::BuiltinType::SatUAccum:
5169 case clang::BuiltinType::SatULongAccum:
5170 case clang::BuiltinType::SatShortFract:
5171 case clang::BuiltinType::SatFract:
5172 case clang::BuiltinType::SatLongFract:
5173 case clang::BuiltinType::SatUShortFract:
5174 case clang::BuiltinType::SatUFract:
5175 case clang::BuiltinType::SatULongFract:
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005176 break;
5177
Kate Stoneb9c1b512016-09-06 20:57:50 +00005178 case clang::BuiltinType::Half:
5179 case clang::BuiltinType::Float:
Ted Woodward4355c7c2017-09-20 19:16:53 +00005180 case clang::BuiltinType::Float16:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005181 case clang::BuiltinType::Float128:
5182 case clang::BuiltinType::Double:
5183 case clang::BuiltinType::LongDouble:
5184 return lldb::eEncodingIEEE754;
5185
5186 case clang::BuiltinType::ObjCClass:
5187 case clang::BuiltinType::ObjCId:
5188 case clang::BuiltinType::ObjCSel:
5189 return lldb::eEncodingUint;
5190
5191 case clang::BuiltinType::NullPtr:
5192 return lldb::eEncodingUint;
5193
5194 case clang::BuiltinType::Kind::ARCUnbridgedCast:
5195 case clang::BuiltinType::Kind::BoundMember:
5196 case clang::BuiltinType::Kind::BuiltinFn:
5197 case clang::BuiltinType::Kind::Dependent:
5198 case clang::BuiltinType::Kind::OCLClkEvent:
5199 case clang::BuiltinType::Kind::OCLEvent:
5200 case clang::BuiltinType::Kind::OCLImage1dRO:
5201 case clang::BuiltinType::Kind::OCLImage1dWO:
5202 case clang::BuiltinType::Kind::OCLImage1dRW:
5203 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
5204 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
5205 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
5206 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
5207 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
5208 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
5209 case clang::BuiltinType::Kind::OCLImage2dRO:
5210 case clang::BuiltinType::Kind::OCLImage2dWO:
5211 case clang::BuiltinType::Kind::OCLImage2dRW:
5212 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
5213 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
5214 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
5215 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
5216 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
5217 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
5218 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5219 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5220 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5221 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5222 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5223 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5224 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5225 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5226 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5227 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5228 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5229 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5230 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5231 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5232 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5233 case clang::BuiltinType::Kind::OCLImage3dRO:
5234 case clang::BuiltinType::Kind::OCLImage3dWO:
5235 case clang::BuiltinType::Kind::OCLImage3dRW:
5236 case clang::BuiltinType::Kind::OCLQueue:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005237 case clang::BuiltinType::Kind::OCLReserveID:
5238 case clang::BuiltinType::Kind::OCLSampler:
5239 case clang::BuiltinType::Kind::OMPArraySection:
5240 case clang::BuiltinType::Kind::Overload:
5241 case clang::BuiltinType::Kind::PseudoObject:
5242 case clang::BuiltinType::Kind::UnknownAny:
5243 break;
Jorge Gorbe Moyaa6e6c182018-11-08 22:04:58 +00005244
5245 case clang::BuiltinType::OCLIntelSubgroupAVCMcePayload:
5246 case clang::BuiltinType::OCLIntelSubgroupAVCImePayload:
5247 case clang::BuiltinType::OCLIntelSubgroupAVCRefPayload:
5248 case clang::BuiltinType::OCLIntelSubgroupAVCSicPayload:
5249 case clang::BuiltinType::OCLIntelSubgroupAVCMceResult:
5250 case clang::BuiltinType::OCLIntelSubgroupAVCImeResult:
5251 case clang::BuiltinType::OCLIntelSubgroupAVCRefResult:
5252 case clang::BuiltinType::OCLIntelSubgroupAVCSicResult:
5253 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultSingleRefStreamout:
5254 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultDualRefStreamout:
5255 case clang::BuiltinType::OCLIntelSubgroupAVCImeSingleRefStreamin:
5256 case clang::BuiltinType::OCLIntelSubgroupAVCImeDualRefStreamin:
5257 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005258 }
5259 break;
Adrian Prantl05097242018-04-30 16:49:04 +00005260 // All pointer types are represented as unsigned integer encodings. We may
5261 // nee to add a eEncodingPointer if we ever need to know the difference
Kate Stoneb9c1b512016-09-06 20:57:50 +00005262 case clang::Type::ObjCObjectPointer:
5263 case clang::Type::BlockPointer:
5264 case clang::Type::Pointer:
5265 case clang::Type::LValueReference:
5266 case clang::Type::RValueReference:
5267 case clang::Type::MemberPointer:
5268 return lldb::eEncodingUint;
5269 case clang::Type::Complex: {
5270 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5271 if (qual_type->isComplexType())
5272 encoding = lldb::eEncodingIEEE754;
5273 else {
5274 const clang::ComplexType *complex_type =
5275 qual_type->getAsComplexIntegerType();
5276 if (complex_type)
5277 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5278 .GetEncoding(count);
5279 else
5280 encoding = lldb::eEncodingSint;
5281 }
5282 count = 2;
5283 return encoding;
5284 }
5285
5286 case clang::Type::ObjCInterface:
5287 break;
5288 case clang::Type::Record:
5289 break;
5290 case clang::Type::Enum:
5291 return lldb::eEncodingSint;
5292 case clang::Type::Typedef:
5293 return CompilerType(getASTContext(),
5294 llvm::cast<clang::TypedefType>(qual_type)
5295 ->getDecl()
5296 ->getUnderlyingType())
5297 .GetEncoding(count);
5298
5299 case clang::Type::Auto:
5300 return CompilerType(
5301 getASTContext(),
5302 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5303 .GetEncoding(count);
5304
5305 case clang::Type::Elaborated:
5306 return CompilerType(
5307 getASTContext(),
5308 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5309 .GetEncoding(count);
5310
5311 case clang::Type::Paren:
5312 return CompilerType(getASTContext(),
5313 llvm::cast<clang::ParenType>(qual_type)->desugar())
5314 .GetEncoding(count);
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005315 case clang::Type::TypeOfExpr:
5316 return CompilerType(getASTContext(),
5317 llvm::cast<clang::TypeOfExprType>(qual_type)
5318 ->getUnderlyingExpr()
5319 ->getType())
5320 .GetEncoding(count);
5321 case clang::Type::TypeOf:
5322 return CompilerType(
5323 getASTContext(),
5324 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5325 .GetEncoding(count);
5326 case clang::Type::Decltype:
5327 return CompilerType(
5328 getASTContext(),
5329 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5330 .GetEncoding(count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005331 case clang::Type::DependentSizedArray:
5332 case clang::Type::DependentSizedExtVector:
5333 case clang::Type::UnresolvedUsing:
5334 case clang::Type::Attributed:
5335 case clang::Type::TemplateTypeParm:
5336 case clang::Type::SubstTemplateTypeParm:
5337 case clang::Type::SubstTemplateTypeParmPack:
5338 case clang::Type::InjectedClassName:
5339 case clang::Type::DependentName:
5340 case clang::Type::DependentTemplateSpecialization:
5341 case clang::Type::PackExpansion:
5342 case clang::Type::ObjCObject:
5343
Kate Stoneb9c1b512016-09-06 20:57:50 +00005344 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005345 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005346 case clang::Type::Atomic:
5347 case clang::Type::Adjusted:
5348 case clang::Type::Pipe:
5349 break;
5350
5351 // pointer type decayed from an array or function type.
5352 case clang::Type::Decayed:
5353 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005354 case clang::Type::ObjCTypeParam:
5355 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005356
5357 case clang::Type::DependentAddressSpace:
5358 break;
Krasimir Georgiev435e76a2019-05-07 13:59:30 +00005359 case clang::Type::MacroQualified:
5360 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005361 }
5362 count = 0;
5363 return lldb::eEncodingInvalid;
5364}
5365
5366lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5367 if (!type)
5368 return lldb::eFormatDefault;
5369
5370 clang::QualType qual_type(GetCanonicalQualType(type));
5371
5372 switch (qual_type->getTypeClass()) {
5373 case clang::Type::UnaryTransform:
5374 break;
5375
5376 case clang::Type::FunctionNoProto:
5377 case clang::Type::FunctionProto:
5378 break;
5379
5380 case clang::Type::IncompleteArray:
5381 case clang::Type::VariableArray:
5382 break;
5383
5384 case clang::Type::ConstantArray:
5385 return lldb::eFormatVoid; // no value
5386
Fangrui Song8f284882018-07-13 22:40:40 +00005387 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005388 case clang::Type::ExtVector:
5389 case clang::Type::Vector:
5390 break;
5391
5392 case clang::Type::Builtin:
5393 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5394 // default: assert(0 && "Unknown builtin type!");
5395 case clang::BuiltinType::UnknownAny:
5396 case clang::BuiltinType::Void:
5397 case clang::BuiltinType::BoundMember:
5398 break;
5399
5400 case clang::BuiltinType::Bool:
5401 return lldb::eFormatBoolean;
5402 case clang::BuiltinType::Char_S:
5403 case clang::BuiltinType::SChar:
5404 case clang::BuiltinType::WChar_S:
5405 case clang::BuiltinType::Char_U:
5406 case clang::BuiltinType::UChar:
5407 case clang::BuiltinType::WChar_U:
5408 return lldb::eFormatChar;
5409 case clang::BuiltinType::Char16:
5410 return lldb::eFormatUnicode16;
5411 case clang::BuiltinType::Char32:
5412 return lldb::eFormatUnicode32;
5413 case clang::BuiltinType::UShort:
5414 return lldb::eFormatUnsigned;
5415 case clang::BuiltinType::Short:
5416 return lldb::eFormatDecimal;
5417 case clang::BuiltinType::UInt:
5418 return lldb::eFormatUnsigned;
5419 case clang::BuiltinType::Int:
5420 return lldb::eFormatDecimal;
5421 case clang::BuiltinType::ULong:
5422 return lldb::eFormatUnsigned;
5423 case clang::BuiltinType::Long:
5424 return lldb::eFormatDecimal;
5425 case clang::BuiltinType::ULongLong:
5426 return lldb::eFormatUnsigned;
5427 case clang::BuiltinType::LongLong:
5428 return lldb::eFormatDecimal;
5429 case clang::BuiltinType::UInt128:
5430 return lldb::eFormatUnsigned;
5431 case clang::BuiltinType::Int128:
5432 return lldb::eFormatDecimal;
5433 case clang::BuiltinType::Half:
5434 case clang::BuiltinType::Float:
5435 case clang::BuiltinType::Double:
5436 case clang::BuiltinType::LongDouble:
5437 return lldb::eFormatFloat;
5438 default:
5439 return lldb::eFormatHex;
5440 }
5441 break;
5442 case clang::Type::ObjCObjectPointer:
5443 return lldb::eFormatHex;
5444 case clang::Type::BlockPointer:
5445 return lldb::eFormatHex;
5446 case clang::Type::Pointer:
5447 return lldb::eFormatHex;
5448 case clang::Type::LValueReference:
5449 case clang::Type::RValueReference:
5450 return lldb::eFormatHex;
5451 case clang::Type::MemberPointer:
5452 break;
5453 case clang::Type::Complex: {
5454 if (qual_type->isComplexType())
5455 return lldb::eFormatComplex;
5456 else
5457 return lldb::eFormatComplexInteger;
5458 }
5459 case clang::Type::ObjCInterface:
5460 break;
5461 case clang::Type::Record:
5462 break;
5463 case clang::Type::Enum:
5464 return lldb::eFormatEnum;
5465 case clang::Type::Typedef:
5466 return CompilerType(getASTContext(),
5467 llvm::cast<clang::TypedefType>(qual_type)
5468 ->getDecl()
5469 ->getUnderlyingType())
5470 .GetFormat();
5471 case clang::Type::Auto:
5472 return CompilerType(getASTContext(),
5473 llvm::cast<clang::AutoType>(qual_type)->desugar())
5474 .GetFormat();
5475 case clang::Type::Paren:
5476 return CompilerType(getASTContext(),
5477 llvm::cast<clang::ParenType>(qual_type)->desugar())
5478 .GetFormat();
5479 case clang::Type::Elaborated:
5480 return CompilerType(
5481 getASTContext(),
5482 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5483 .GetFormat();
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005484 case clang::Type::TypeOfExpr:
5485 return CompilerType(getASTContext(),
5486 llvm::cast<clang::TypeOfExprType>(qual_type)
5487 ->getUnderlyingExpr()
5488 ->getType())
5489 .GetFormat();
5490 case clang::Type::TypeOf:
5491 return CompilerType(
5492 getASTContext(),
5493 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5494 .GetFormat();
5495 case clang::Type::Decltype:
5496 return CompilerType(
5497 getASTContext(),
5498 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5499 .GetFormat();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005500 case clang::Type::DependentSizedArray:
5501 case clang::Type::DependentSizedExtVector:
5502 case clang::Type::UnresolvedUsing:
5503 case clang::Type::Attributed:
5504 case clang::Type::TemplateTypeParm:
5505 case clang::Type::SubstTemplateTypeParm:
5506 case clang::Type::SubstTemplateTypeParmPack:
5507 case clang::Type::InjectedClassName:
5508 case clang::Type::DependentName:
5509 case clang::Type::DependentTemplateSpecialization:
5510 case clang::Type::PackExpansion:
5511 case clang::Type::ObjCObject:
5512
Kate Stoneb9c1b512016-09-06 20:57:50 +00005513 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005514 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005515 case clang::Type::Atomic:
5516 case clang::Type::Adjusted:
5517 case clang::Type::Pipe:
5518 break;
5519
5520 // pointer type decayed from an array or function type.
5521 case clang::Type::Decayed:
5522 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005523 case clang::Type::ObjCTypeParam:
5524 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005525
5526 case clang::Type::DependentAddressSpace:
5527 break;
Krasimir Georgiev435e76a2019-05-07 13:59:30 +00005528 case clang::Type::MacroQualified:
5529 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005530 }
5531 // We don't know hot to display this type...
5532 return lldb::eFormatBytes;
5533}
5534
5535static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5536 bool check_superclass) {
5537 while (class_interface_decl) {
5538 if (class_interface_decl->ivar_size() > 0)
5539 return true;
5540
5541 if (check_superclass)
5542 class_interface_decl = class_interface_decl->getSuperClass();
5543 else
5544 break;
5545 }
5546 return false;
5547}
5548
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00005549static Optional<SymbolFile::ArrayInfo>
Adrian Prantleca07c52018-11-05 20:49:07 +00005550GetDynamicArrayInfo(ClangASTContext &ast, SymbolFile *sym_file,
5551 clang::QualType qual_type,
5552 const ExecutionContext *exe_ctx) {
5553 if (qual_type->isIncompleteArrayType())
5554 if (auto *metadata = ast.GetMetadata(qual_type.getAsOpaquePtr()))
Pavel Labathffec31e2018-12-28 13:34:44 +00005555 return sym_file->GetDynamicArrayInfoForUID(metadata->GetUserID(),
5556 exe_ctx);
Adrian Prantleca07c52018-11-05 20:49:07 +00005557 return llvm::None;
5558}
5559
Kate Stoneb9c1b512016-09-06 20:57:50 +00005560uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
Adrian Prantleca07c52018-11-05 20:49:07 +00005561 bool omit_empty_base_classes,
5562 const ExecutionContext *exe_ctx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005563 if (!type)
5564 return 0;
5565
5566 uint32_t num_children = 0;
5567 clang::QualType qual_type(GetQualType(type));
5568 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5569 switch (type_class) {
5570 case clang::Type::Builtin:
5571 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5572 case clang::BuiltinType::ObjCId: // child is Class
5573 case clang::BuiltinType::ObjCClass: // child is Class
5574 num_children = 1;
5575 break;
5576
5577 default:
5578 break;
5579 }
5580 break;
5581
5582 case clang::Type::Complex:
5583 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005584 case clang::Type::Record:
5585 if (GetCompleteQualType(getASTContext(), qual_type)) {
5586 const clang::RecordType *record_type =
5587 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5588 const clang::RecordDecl *record_decl = record_type->getDecl();
5589 assert(record_decl);
5590 const clang::CXXRecordDecl *cxx_record_decl =
5591 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5592 if (cxx_record_decl) {
5593 if (omit_empty_base_classes) {
Adrian Prantl05097242018-04-30 16:49:04 +00005594 // Check each base classes to see if it or any of its base classes
5595 // contain any fields. This can help limit the noise in variable
5596 // views by not having to show base classes that contain no members.
Kate Stoneb9c1b512016-09-06 20:57:50 +00005597 clang::CXXRecordDecl::base_class_const_iterator base_class,
5598 base_class_end;
5599 for (base_class = cxx_record_decl->bases_begin(),
5600 base_class_end = cxx_record_decl->bases_end();
5601 base_class != base_class_end; ++base_class) {
5602 const clang::CXXRecordDecl *base_class_decl =
5603 llvm::cast<clang::CXXRecordDecl>(
5604 base_class->getType()
5605 ->getAs<clang::RecordType>()
5606 ->getDecl());
5607
5608 // Skip empty base classes
Jonas Devliegherea6682a42018-12-15 00:15:33 +00005609 if (!ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00005610 continue;
5611
5612 num_children++;
5613 }
5614 } else {
5615 // Include all base classes
5616 num_children += cxx_record_decl->getNumBases();
5617 }
5618 }
5619 clang::RecordDecl::field_iterator field, field_end;
5620 for (field = record_decl->field_begin(),
5621 field_end = record_decl->field_end();
5622 field != field_end; ++field)
5623 ++num_children;
5624 }
5625 break;
5626
5627 case clang::Type::ObjCObject:
5628 case clang::Type::ObjCInterface:
5629 if (GetCompleteQualType(getASTContext(), qual_type)) {
5630 const clang::ObjCObjectType *objc_class_type =
5631 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5632 assert(objc_class_type);
5633 if (objc_class_type) {
5634 clang::ObjCInterfaceDecl *class_interface_decl =
5635 objc_class_type->getInterface();
5636
5637 if (class_interface_decl) {
5638
5639 clang::ObjCInterfaceDecl *superclass_interface_decl =
5640 class_interface_decl->getSuperClass();
5641 if (superclass_interface_decl) {
5642 if (omit_empty_base_classes) {
5643 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5644 ++num_children;
5645 } else
5646 ++num_children;
5647 }
5648
5649 num_children += class_interface_decl->ivar_size();
5650 }
5651 }
5652 }
5653 break;
5654
5655 case clang::Type::ObjCObjectPointer: {
5656 const clang::ObjCObjectPointerType *pointer_type =
5657 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5658 clang::QualType pointee_type = pointer_type->getPointeeType();
5659 uint32_t num_pointee_children =
5660 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005661 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005662 // If this type points to a simple type, then it has 1 child
5663 if (num_pointee_children == 0)
5664 num_children = 1;
5665 else
5666 num_children = num_pointee_children;
5667 } break;
5668
5669 case clang::Type::Vector:
5670 case clang::Type::ExtVector:
5671 num_children =
5672 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5673 break;
5674
5675 case clang::Type::ConstantArray:
5676 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5677 ->getSize()
5678 .getLimitedValue();
5679 break;
Adrian Prantleca07c52018-11-05 20:49:07 +00005680 case clang::Type::IncompleteArray:
5681 if (auto array_info =
5682 GetDynamicArrayInfo(*this, GetSymbolFile(), qual_type, exe_ctx))
5683 // Only 1-dimensional arrays are supported.
5684 num_children = array_info->element_orders.size()
5685 ? array_info->element_orders.back()
5686 : 0;
5687 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005688
5689 case clang::Type::Pointer: {
5690 const clang::PointerType *pointer_type =
5691 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5692 clang::QualType pointee_type(pointer_type->getPointeeType());
5693 uint32_t num_pointee_children =
5694 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005695 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005696 if (num_pointee_children == 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00005697 // We have a pointer to a pointee type that claims it has no children. We
5698 // will want to look at
Kate Stoneb9c1b512016-09-06 20:57:50 +00005699 num_children = GetNumPointeeChildren(pointee_type);
5700 } else
5701 num_children = num_pointee_children;
5702 } break;
5703
5704 case clang::Type::LValueReference:
5705 case clang::Type::RValueReference: {
5706 const clang::ReferenceType *reference_type =
5707 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5708 clang::QualType pointee_type = reference_type->getPointeeType();
5709 uint32_t num_pointee_children =
5710 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005711 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005712 // If this type points to a simple type, then it has 1 child
5713 if (num_pointee_children == 0)
5714 num_children = 1;
5715 else
5716 num_children = num_pointee_children;
5717 } break;
5718
5719 case clang::Type::Typedef:
5720 num_children =
5721 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5722 ->getDecl()
5723 ->getUnderlyingType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005724 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005725 break;
5726
5727 case clang::Type::Auto:
5728 num_children =
5729 CompilerType(getASTContext(),
5730 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005731 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005732 break;
5733
5734 case clang::Type::Elaborated:
5735 num_children =
5736 CompilerType(
5737 getASTContext(),
5738 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005739 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005740 break;
5741
5742 case clang::Type::Paren:
5743 num_children =
5744 CompilerType(getASTContext(),
5745 llvm::cast<clang::ParenType>(qual_type)->desugar())
Adrian Prantleca07c52018-11-05 20:49:07 +00005746 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005747 break;
5748 default:
5749 break;
5750 }
5751 return num_children;
5752}
5753
Adrian Prantl0e4c4822019-03-06 21:22:25 +00005754CompilerType ClangASTContext::GetBuiltinTypeByName(ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005755 return GetBasicType(GetBasicTypeEnumeration(name));
Greg Clayton56939cb2015-09-17 22:23:34 +00005756}
5757
Greg Claytond8d4a572015-08-11 21:38:15 +00005758lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00005759ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5760 if (type) {
5761 clang::QualType qual_type(GetQualType(type));
5762 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5763 if (type_class == clang::Type::Builtin) {
5764 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5765 case clang::BuiltinType::Void:
5766 return eBasicTypeVoid;
5767 case clang::BuiltinType::Bool:
5768 return eBasicTypeBool;
5769 case clang::BuiltinType::Char_S:
5770 return eBasicTypeSignedChar;
5771 case clang::BuiltinType::Char_U:
5772 return eBasicTypeUnsignedChar;
5773 case clang::BuiltinType::Char16:
5774 return eBasicTypeChar16;
5775 case clang::BuiltinType::Char32:
5776 return eBasicTypeChar32;
5777 case clang::BuiltinType::UChar:
5778 return eBasicTypeUnsignedChar;
5779 case clang::BuiltinType::SChar:
5780 return eBasicTypeSignedChar;
5781 case clang::BuiltinType::WChar_S:
5782 return eBasicTypeSignedWChar;
5783 case clang::BuiltinType::WChar_U:
5784 return eBasicTypeUnsignedWChar;
5785 case clang::BuiltinType::Short:
5786 return eBasicTypeShort;
5787 case clang::BuiltinType::UShort:
5788 return eBasicTypeUnsignedShort;
5789 case clang::BuiltinType::Int:
5790 return eBasicTypeInt;
5791 case clang::BuiltinType::UInt:
5792 return eBasicTypeUnsignedInt;
5793 case clang::BuiltinType::Long:
5794 return eBasicTypeLong;
5795 case clang::BuiltinType::ULong:
5796 return eBasicTypeUnsignedLong;
5797 case clang::BuiltinType::LongLong:
5798 return eBasicTypeLongLong;
5799 case clang::BuiltinType::ULongLong:
5800 return eBasicTypeUnsignedLongLong;
5801 case clang::BuiltinType::Int128:
5802 return eBasicTypeInt128;
5803 case clang::BuiltinType::UInt128:
5804 return eBasicTypeUnsignedInt128;
5805
5806 case clang::BuiltinType::Half:
5807 return eBasicTypeHalf;
5808 case clang::BuiltinType::Float:
5809 return eBasicTypeFloat;
5810 case clang::BuiltinType::Double:
5811 return eBasicTypeDouble;
5812 case clang::BuiltinType::LongDouble:
5813 return eBasicTypeLongDouble;
5814
5815 case clang::BuiltinType::NullPtr:
5816 return eBasicTypeNullPtr;
5817 case clang::BuiltinType::ObjCId:
5818 return eBasicTypeObjCID;
5819 case clang::BuiltinType::ObjCClass:
5820 return eBasicTypeObjCClass;
5821 case clang::BuiltinType::ObjCSel:
5822 return eBasicTypeObjCSel;
5823 default:
5824 return eBasicTypeOther;
5825 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005826 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005827 }
5828 return eBasicTypeInvalid;
Greg Claytond8d4a572015-08-11 21:38:15 +00005829}
5830
Kate Stoneb9c1b512016-09-06 20:57:50 +00005831void ClangASTContext::ForEachEnumerator(
5832 lldb::opaque_compiler_type_t type,
5833 std::function<bool(const CompilerType &integer_type,
Adrian Prantl0e4c4822019-03-06 21:22:25 +00005834 ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00005835 const llvm::APSInt &value)> const &callback) {
5836 const clang::EnumType *enum_type =
5837 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5838 if (enum_type) {
5839 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5840 if (enum_decl) {
5841 CompilerType integer_type(this,
5842 enum_decl->getIntegerType().getAsOpaquePtr());
Greg Clayton99558cc42015-08-24 23:46:31 +00005843
Kate Stoneb9c1b512016-09-06 20:57:50 +00005844 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5845 for (enum_pos = enum_decl->enumerator_begin(),
5846 enum_end_pos = enum_decl->enumerator_end();
5847 enum_pos != enum_end_pos; ++enum_pos) {
5848 ConstString name(enum_pos->getNameAsString().c_str());
5849 if (!callback(integer_type, name, enum_pos->getInitVal()))
5850 break;
5851 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005852 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005853 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005854}
5855
Greg Claytond8d4a572015-08-11 21:38:15 +00005856#pragma mark Aggregate Types
5857
Kate Stoneb9c1b512016-09-06 20:57:50 +00005858uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5859 if (!type)
5860 return 0;
Enrico Granata36f51e42015-12-18 22:41:25 +00005861
Kate Stoneb9c1b512016-09-06 20:57:50 +00005862 uint32_t count = 0;
5863 clang::QualType qual_type(GetCanonicalQualType(type));
5864 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5865 switch (type_class) {
5866 case clang::Type::Record:
5867 if (GetCompleteType(type)) {
5868 const clang::RecordType *record_type =
5869 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5870 if (record_type) {
5871 clang::RecordDecl *record_decl = record_type->getDecl();
5872 if (record_decl) {
5873 uint32_t field_idx = 0;
5874 clang::RecordDecl::field_iterator field, field_end;
5875 for (field = record_decl->field_begin(),
5876 field_end = record_decl->field_end();
5877 field != field_end; ++field)
5878 ++field_idx;
5879 count = field_idx;
5880 }
5881 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005882 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005883 break;
5884
5885 case clang::Type::Typedef:
5886 count =
5887 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5888 ->getDecl()
5889 ->getUnderlyingType())
5890 .GetNumFields();
5891 break;
5892
5893 case clang::Type::Auto:
5894 count =
5895 CompilerType(getASTContext(),
5896 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5897 .GetNumFields();
5898 break;
5899
5900 case clang::Type::Elaborated:
5901 count = CompilerType(
5902 getASTContext(),
5903 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5904 .GetNumFields();
5905 break;
5906
5907 case clang::Type::Paren:
5908 count = CompilerType(getASTContext(),
5909 llvm::cast<clang::ParenType>(qual_type)->desugar())
5910 .GetNumFields();
5911 break;
5912
Sean Callananf9c622a2016-09-30 18:44:43 +00005913 case clang::Type::ObjCObjectPointer: {
5914 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005915 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005916 const clang::ObjCInterfaceType *objc_interface_type =
5917 objc_class_type->getInterfaceType();
5918 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005919 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5920 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005921 clang::ObjCInterfaceDecl *class_interface_decl =
5922 objc_interface_type->getDecl();
5923 if (class_interface_decl) {
5924 count = class_interface_decl->ivar_size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005925 }
5926 }
5927 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005928 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005929
5930 case clang::Type::ObjCObject:
5931 case clang::Type::ObjCInterface:
5932 if (GetCompleteType(type)) {
5933 const clang::ObjCObjectType *objc_class_type =
5934 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5935 if (objc_class_type) {
5936 clang::ObjCInterfaceDecl *class_interface_decl =
5937 objc_class_type->getInterface();
5938
5939 if (class_interface_decl)
5940 count = class_interface_decl->ivar_size();
5941 }
5942 }
5943 break;
5944
5945 default:
5946 break;
5947 }
5948 return count;
Greg Claytond8d4a572015-08-11 21:38:15 +00005949}
5950
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005951static lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005952GetObjCFieldAtIndex(clang::ASTContext *ast,
5953 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5954 std::string &name, uint64_t *bit_offset_ptr,
5955 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5956 if (class_interface_decl) {
5957 if (idx < (class_interface_decl->ivar_size())) {
5958 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5959 ivar_end = class_interface_decl->ivar_end();
5960 uint32_t ivar_idx = 0;
5961
5962 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5963 ++ivar_pos, ++ivar_idx) {
5964 if (ivar_idx == idx) {
5965 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5966
5967 clang::QualType ivar_qual_type(ivar_decl->getType());
5968
5969 name.assign(ivar_decl->getNameAsString());
5970
5971 if (bit_offset_ptr) {
5972 const clang::ASTRecordLayout &interface_layout =
5973 ast->getASTObjCInterfaceLayout(class_interface_decl);
5974 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5975 }
5976
5977 const bool is_bitfield = ivar_pos->isBitField();
5978
5979 if (bitfield_bit_size_ptr) {
5980 *bitfield_bit_size_ptr = 0;
5981
5982 if (is_bitfield && ast) {
5983 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
Hans Wennborg30ce9622018-11-28 14:30:18 +00005984 clang::Expr::EvalResult result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005985 if (bitfield_bit_size_expr &&
Hans Wennborg30ce9622018-11-28 14:30:18 +00005986 bitfield_bit_size_expr->EvaluateAsInt(result, *ast)) {
5987 llvm::APSInt bitfield_apsint = result.Val.getInt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005988 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5989 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005990 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005991 }
5992 if (is_bitfield_ptr)
5993 *is_bitfield_ptr = is_bitfield;
5994
5995 return ivar_qual_type.getAsOpaquePtr();
Greg Claytond8d4a572015-08-11 21:38:15 +00005996 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005997 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005998 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005999 }
6000 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00006001}
6002
Kate Stoneb9c1b512016-09-06 20:57:50 +00006003CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
6004 size_t idx, std::string &name,
6005 uint64_t *bit_offset_ptr,
6006 uint32_t *bitfield_bit_size_ptr,
6007 bool *is_bitfield_ptr) {
6008 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006009 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006010
6011 clang::QualType qual_type(GetCanonicalQualType(type));
6012 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6013 switch (type_class) {
6014 case clang::Type::Record:
6015 if (GetCompleteType(type)) {
6016 const clang::RecordType *record_type =
6017 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6018 const clang::RecordDecl *record_decl = record_type->getDecl();
6019 uint32_t field_idx = 0;
6020 clang::RecordDecl::field_iterator field, field_end;
6021 for (field = record_decl->field_begin(),
6022 field_end = record_decl->field_end();
6023 field != field_end; ++field, ++field_idx) {
6024 if (idx == field_idx) {
6025 // Print the member type if requested
6026 // Print the member name and equal sign
6027 name.assign(field->getNameAsString());
6028
6029 // Figure out the type byte size (field_type_info.first) and
6030 // alignment (field_type_info.second) from the AST context.
6031 if (bit_offset_ptr) {
6032 const clang::ASTRecordLayout &record_layout =
6033 getASTContext()->getASTRecordLayout(record_decl);
6034 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
6035 }
6036
6037 const bool is_bitfield = field->isBitField();
6038
6039 if (bitfield_bit_size_ptr) {
6040 *bitfield_bit_size_ptr = 0;
6041
6042 if (is_bitfield) {
6043 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
Hans Wennborg30ce9622018-11-28 14:30:18 +00006044 clang::Expr::EvalResult result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006045 if (bitfield_bit_size_expr &&
Hans Wennborg30ce9622018-11-28 14:30:18 +00006046 bitfield_bit_size_expr->EvaluateAsInt(result,
Kate Stoneb9c1b512016-09-06 20:57:50 +00006047 *getASTContext())) {
Hans Wennborg30ce9622018-11-28 14:30:18 +00006048 llvm::APSInt bitfield_apsint = result.Val.getInt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006049 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
6050 }
6051 }
6052 }
6053 if (is_bitfield_ptr)
6054 *is_bitfield_ptr = is_bitfield;
6055
6056 return CompilerType(getASTContext(), field->getType());
6057 }
6058 }
6059 }
6060 break;
6061
Sean Callananf9c622a2016-09-30 18:44:43 +00006062 case clang::Type::ObjCObjectPointer: {
6063 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00006064 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00006065 const clang::ObjCInterfaceType *objc_interface_type =
6066 objc_class_type->getInterfaceType();
6067 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00006068 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
6069 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00006070 clang::ObjCInterfaceDecl *class_interface_decl =
6071 objc_interface_type->getDecl();
6072 if (class_interface_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006073 return CompilerType(
6074 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
6075 idx, name, bit_offset_ptr,
6076 bitfield_bit_size_ptr, is_bitfield_ptr));
6077 }
6078 }
6079 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00006080 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006081
6082 case clang::Type::ObjCObject:
6083 case clang::Type::ObjCInterface:
6084 if (GetCompleteType(type)) {
6085 const clang::ObjCObjectType *objc_class_type =
6086 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6087 assert(objc_class_type);
6088 if (objc_class_type) {
6089 clang::ObjCInterfaceDecl *class_interface_decl =
6090 objc_class_type->getInterface();
6091 return CompilerType(
6092 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
6093 idx, name, bit_offset_ptr,
6094 bitfield_bit_size_ptr, is_bitfield_ptr));
6095 }
6096 }
6097 break;
6098
6099 case clang::Type::Typedef:
6100 return CompilerType(getASTContext(),
6101 llvm::cast<clang::TypedefType>(qual_type)
6102 ->getDecl()
6103 ->getUnderlyingType())
6104 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6105 is_bitfield_ptr);
6106
6107 case clang::Type::Auto:
6108 return CompilerType(
6109 getASTContext(),
6110 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
6111 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6112 is_bitfield_ptr);
6113
6114 case clang::Type::Elaborated:
6115 return CompilerType(
6116 getASTContext(),
6117 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
6118 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6119 is_bitfield_ptr);
6120
6121 case clang::Type::Paren:
6122 return CompilerType(getASTContext(),
6123 llvm::cast<clang::ParenType>(qual_type)->desugar())
6124 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6125 is_bitfield_ptr);
6126
6127 default:
6128 break;
6129 }
6130 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006131}
6132
Greg Clayton99558cc42015-08-24 23:46:31 +00006133uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006134ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
6135 uint32_t count = 0;
6136 clang::QualType qual_type(GetCanonicalQualType(type));
6137 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6138 switch (type_class) {
6139 case clang::Type::Record:
6140 if (GetCompleteType(type)) {
6141 const clang::CXXRecordDecl *cxx_record_decl =
6142 qual_type->getAsCXXRecordDecl();
6143 if (cxx_record_decl)
6144 count = cxx_record_decl->getNumBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006145 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006146 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006147
Kate Stoneb9c1b512016-09-06 20:57:50 +00006148 case clang::Type::ObjCObjectPointer:
6149 count = GetPointeeType(type).GetNumDirectBaseClasses();
6150 break;
6151
6152 case clang::Type::ObjCObject:
6153 if (GetCompleteType(type)) {
6154 const clang::ObjCObjectType *objc_class_type =
6155 qual_type->getAsObjCQualifiedInterfaceType();
6156 if (objc_class_type) {
6157 clang::ObjCInterfaceDecl *class_interface_decl =
6158 objc_class_type->getInterface();
6159
6160 if (class_interface_decl && class_interface_decl->getSuperClass())
6161 count = 1;
6162 }
6163 }
6164 break;
6165 case clang::Type::ObjCInterface:
6166 if (GetCompleteType(type)) {
6167 const clang::ObjCInterfaceType *objc_interface_type =
6168 qual_type->getAs<clang::ObjCInterfaceType>();
6169 if (objc_interface_type) {
6170 clang::ObjCInterfaceDecl *class_interface_decl =
6171 objc_interface_type->getInterface();
6172
6173 if (class_interface_decl && class_interface_decl->getSuperClass())
6174 count = 1;
6175 }
6176 }
6177 break;
6178
6179 case clang::Type::Typedef:
6180 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6181 ->getDecl()
6182 ->getUnderlyingType()
6183 .getAsOpaquePtr());
6184 break;
6185
6186 case clang::Type::Auto:
6187 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6188 ->getDeducedType()
6189 .getAsOpaquePtr());
6190 break;
6191
6192 case clang::Type::Elaborated:
6193 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6194 ->getNamedType()
6195 .getAsOpaquePtr());
6196 break;
6197
6198 case clang::Type::Paren:
6199 return GetNumDirectBaseClasses(
6200 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6201
6202 default:
6203 break;
6204 }
6205 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006206}
6207
6208uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006209ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
6210 uint32_t count = 0;
6211 clang::QualType qual_type(GetCanonicalQualType(type));
6212 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6213 switch (type_class) {
6214 case clang::Type::Record:
6215 if (GetCompleteType(type)) {
6216 const clang::CXXRecordDecl *cxx_record_decl =
6217 qual_type->getAsCXXRecordDecl();
6218 if (cxx_record_decl)
6219 count = cxx_record_decl->getNumVBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006220 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006221 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006222
Kate Stoneb9c1b512016-09-06 20:57:50 +00006223 case clang::Type::Typedef:
6224 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6225 ->getDecl()
6226 ->getUnderlyingType()
6227 .getAsOpaquePtr());
6228 break;
6229
6230 case clang::Type::Auto:
6231 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6232 ->getDeducedType()
6233 .getAsOpaquePtr());
6234 break;
6235
6236 case clang::Type::Elaborated:
6237 count =
6238 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6239 ->getNamedType()
6240 .getAsOpaquePtr());
6241 break;
6242
6243 case clang::Type::Paren:
6244 count = GetNumVirtualBaseClasses(
6245 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6246 break;
6247
6248 default:
6249 break;
6250 }
6251 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006252}
6253
Kate Stoneb9c1b512016-09-06 20:57:50 +00006254CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
6255 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6256 clang::QualType qual_type(GetCanonicalQualType(type));
6257 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6258 switch (type_class) {
6259 case clang::Type::Record:
6260 if (GetCompleteType(type)) {
6261 const clang::CXXRecordDecl *cxx_record_decl =
6262 qual_type->getAsCXXRecordDecl();
6263 if (cxx_record_decl) {
6264 uint32_t curr_idx = 0;
6265 clang::CXXRecordDecl::base_class_const_iterator base_class,
6266 base_class_end;
6267 for (base_class = cxx_record_decl->bases_begin(),
6268 base_class_end = cxx_record_decl->bases_end();
6269 base_class != base_class_end; ++base_class, ++curr_idx) {
6270 if (curr_idx == idx) {
6271 if (bit_offset_ptr) {
6272 const clang::ASTRecordLayout &record_layout =
6273 getASTContext()->getASTRecordLayout(cxx_record_decl);
6274 const clang::CXXRecordDecl *base_class_decl =
6275 llvm::cast<clang::CXXRecordDecl>(
6276 base_class->getType()
6277 ->getAs<clang::RecordType>()
6278 ->getDecl());
6279 if (base_class->isVirtual())
6280 *bit_offset_ptr =
6281 record_layout.getVBaseClassOffset(base_class_decl)
6282 .getQuantity() *
6283 8;
6284 else
6285 *bit_offset_ptr =
6286 record_layout.getBaseClassOffset(base_class_decl)
6287 .getQuantity() *
6288 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006289 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006290 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6291 }
6292 }
6293 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006294 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006295 break;
6296
6297 case clang::Type::ObjCObjectPointer:
6298 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6299
6300 case clang::Type::ObjCObject:
6301 if (idx == 0 && GetCompleteType(type)) {
6302 const clang::ObjCObjectType *objc_class_type =
6303 qual_type->getAsObjCQualifiedInterfaceType();
6304 if (objc_class_type) {
6305 clang::ObjCInterfaceDecl *class_interface_decl =
6306 objc_class_type->getInterface();
6307
6308 if (class_interface_decl) {
6309 clang::ObjCInterfaceDecl *superclass_interface_decl =
6310 class_interface_decl->getSuperClass();
6311 if (superclass_interface_decl) {
6312 if (bit_offset_ptr)
6313 *bit_offset_ptr = 0;
6314 return CompilerType(getASTContext(),
6315 getASTContext()->getObjCInterfaceType(
6316 superclass_interface_decl));
6317 }
6318 }
6319 }
6320 }
6321 break;
6322 case clang::Type::ObjCInterface:
6323 if (idx == 0 && GetCompleteType(type)) {
6324 const clang::ObjCObjectType *objc_interface_type =
6325 qual_type->getAs<clang::ObjCInterfaceType>();
6326 if (objc_interface_type) {
6327 clang::ObjCInterfaceDecl *class_interface_decl =
6328 objc_interface_type->getInterface();
6329
6330 if (class_interface_decl) {
6331 clang::ObjCInterfaceDecl *superclass_interface_decl =
6332 class_interface_decl->getSuperClass();
6333 if (superclass_interface_decl) {
6334 if (bit_offset_ptr)
6335 *bit_offset_ptr = 0;
6336 return CompilerType(getASTContext(),
6337 getASTContext()->getObjCInterfaceType(
6338 superclass_interface_decl));
6339 }
6340 }
6341 }
6342 }
6343 break;
6344
6345 case clang::Type::Typedef:
6346 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6347 ->getDecl()
6348 ->getUnderlyingType()
6349 .getAsOpaquePtr(),
6350 idx, bit_offset_ptr);
6351
6352 case clang::Type::Auto:
6353 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6354 ->getDeducedType()
6355 .getAsOpaquePtr(),
6356 idx, bit_offset_ptr);
6357
6358 case clang::Type::Elaborated:
6359 return GetDirectBaseClassAtIndex(
6360 llvm::cast<clang::ElaboratedType>(qual_type)
6361 ->getNamedType()
6362 .getAsOpaquePtr(),
6363 idx, bit_offset_ptr);
6364
6365 case clang::Type::Paren:
6366 return GetDirectBaseClassAtIndex(
6367 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6368 idx, bit_offset_ptr);
6369
6370 default:
6371 break;
6372 }
6373 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006374}
6375
Kate Stoneb9c1b512016-09-06 20:57:50 +00006376CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6377 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6378 clang::QualType qual_type(GetCanonicalQualType(type));
6379 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6380 switch (type_class) {
6381 case clang::Type::Record:
6382 if (GetCompleteType(type)) {
6383 const clang::CXXRecordDecl *cxx_record_decl =
6384 qual_type->getAsCXXRecordDecl();
6385 if (cxx_record_decl) {
6386 uint32_t curr_idx = 0;
6387 clang::CXXRecordDecl::base_class_const_iterator base_class,
6388 base_class_end;
6389 for (base_class = cxx_record_decl->vbases_begin(),
6390 base_class_end = cxx_record_decl->vbases_end();
6391 base_class != base_class_end; ++base_class, ++curr_idx) {
6392 if (curr_idx == idx) {
6393 if (bit_offset_ptr) {
6394 const clang::ASTRecordLayout &record_layout =
6395 getASTContext()->getASTRecordLayout(cxx_record_decl);
6396 const clang::CXXRecordDecl *base_class_decl =
6397 llvm::cast<clang::CXXRecordDecl>(
6398 base_class->getType()
6399 ->getAs<clang::RecordType>()
6400 ->getDecl());
6401 *bit_offset_ptr =
6402 record_layout.getVBaseClassOffset(base_class_decl)
6403 .getQuantity() *
6404 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006405 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006406 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6407 }
6408 }
6409 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006410 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006411 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006412
Kate Stoneb9c1b512016-09-06 20:57:50 +00006413 case clang::Type::Typedef:
6414 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6415 ->getDecl()
6416 ->getUnderlyingType()
6417 .getAsOpaquePtr(),
6418 idx, bit_offset_ptr);
6419
6420 case clang::Type::Auto:
6421 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6422 ->getDeducedType()
6423 .getAsOpaquePtr(),
6424 idx, bit_offset_ptr);
6425
6426 case clang::Type::Elaborated:
6427 return GetVirtualBaseClassAtIndex(
6428 llvm::cast<clang::ElaboratedType>(qual_type)
6429 ->getNamedType()
6430 .getAsOpaquePtr(),
6431 idx, bit_offset_ptr);
6432
6433 case clang::Type::Paren:
6434 return GetVirtualBaseClassAtIndex(
6435 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6436 idx, bit_offset_ptr);
6437
6438 default:
6439 break;
6440 }
6441 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006442}
6443
Greg Claytond8d4a572015-08-11 21:38:15 +00006444// If a pointer to a pointee type (the clang_type arg) says that it has no
6445// children, then we either need to trust it, or override it and return a
6446// different result. For example, an "int *" has one child that is an integer,
6447// but a function pointer doesn't have any children. Likewise if a Record type
6448// claims it has no children, then there really is nothing to show.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006449uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6450 if (type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00006451 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006452
6453 clang::QualType qual_type(type.getCanonicalType());
6454 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6455 switch (type_class) {
6456 case clang::Type::Builtin:
6457 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6458 case clang::BuiltinType::UnknownAny:
6459 case clang::BuiltinType::Void:
6460 case clang::BuiltinType::NullPtr:
6461 case clang::BuiltinType::OCLEvent:
6462 case clang::BuiltinType::OCLImage1dRO:
6463 case clang::BuiltinType::OCLImage1dWO:
6464 case clang::BuiltinType::OCLImage1dRW:
6465 case clang::BuiltinType::OCLImage1dArrayRO:
6466 case clang::BuiltinType::OCLImage1dArrayWO:
6467 case clang::BuiltinType::OCLImage1dArrayRW:
6468 case clang::BuiltinType::OCLImage1dBufferRO:
6469 case clang::BuiltinType::OCLImage1dBufferWO:
6470 case clang::BuiltinType::OCLImage1dBufferRW:
6471 case clang::BuiltinType::OCLImage2dRO:
6472 case clang::BuiltinType::OCLImage2dWO:
6473 case clang::BuiltinType::OCLImage2dRW:
6474 case clang::BuiltinType::OCLImage2dArrayRO:
6475 case clang::BuiltinType::OCLImage2dArrayWO:
6476 case clang::BuiltinType::OCLImage2dArrayRW:
6477 case clang::BuiltinType::OCLImage3dRO:
6478 case clang::BuiltinType::OCLImage3dWO:
6479 case clang::BuiltinType::OCLImage3dRW:
6480 case clang::BuiltinType::OCLSampler:
6481 return 0;
6482 case clang::BuiltinType::Bool:
6483 case clang::BuiltinType::Char_U:
6484 case clang::BuiltinType::UChar:
6485 case clang::BuiltinType::WChar_U:
6486 case clang::BuiltinType::Char16:
6487 case clang::BuiltinType::Char32:
6488 case clang::BuiltinType::UShort:
6489 case clang::BuiltinType::UInt:
6490 case clang::BuiltinType::ULong:
6491 case clang::BuiltinType::ULongLong:
6492 case clang::BuiltinType::UInt128:
6493 case clang::BuiltinType::Char_S:
6494 case clang::BuiltinType::SChar:
6495 case clang::BuiltinType::WChar_S:
6496 case clang::BuiltinType::Short:
6497 case clang::BuiltinType::Int:
6498 case clang::BuiltinType::Long:
6499 case clang::BuiltinType::LongLong:
6500 case clang::BuiltinType::Int128:
6501 case clang::BuiltinType::Float:
6502 case clang::BuiltinType::Double:
6503 case clang::BuiltinType::LongDouble:
6504 case clang::BuiltinType::Dependent:
6505 case clang::BuiltinType::Overload:
6506 case clang::BuiltinType::ObjCId:
6507 case clang::BuiltinType::ObjCClass:
6508 case clang::BuiltinType::ObjCSel:
6509 case clang::BuiltinType::BoundMember:
6510 case clang::BuiltinType::Half:
6511 case clang::BuiltinType::ARCUnbridgedCast:
6512 case clang::BuiltinType::PseudoObject:
6513 case clang::BuiltinType::BuiltinFn:
6514 case clang::BuiltinType::OMPArraySection:
6515 return 1;
6516 default:
6517 return 0;
6518 }
6519 break;
6520
6521 case clang::Type::Complex:
6522 return 1;
6523 case clang::Type::Pointer:
6524 return 1;
6525 case clang::Type::BlockPointer:
6526 return 0; // If block pointers don't have debug info, then no children for
6527 // them
6528 case clang::Type::LValueReference:
6529 return 1;
6530 case clang::Type::RValueReference:
6531 return 1;
6532 case clang::Type::MemberPointer:
6533 return 0;
6534 case clang::Type::ConstantArray:
6535 return 0;
6536 case clang::Type::IncompleteArray:
6537 return 0;
6538 case clang::Type::VariableArray:
6539 return 0;
6540 case clang::Type::DependentSizedArray:
6541 return 0;
6542 case clang::Type::DependentSizedExtVector:
6543 return 0;
6544 case clang::Type::Vector:
6545 return 0;
6546 case clang::Type::ExtVector:
6547 return 0;
6548 case clang::Type::FunctionProto:
6549 return 0; // When we function pointers, they have no children...
6550 case clang::Type::FunctionNoProto:
6551 return 0; // When we function pointers, they have no children...
6552 case clang::Type::UnresolvedUsing:
6553 return 0;
6554 case clang::Type::Paren:
6555 return GetNumPointeeChildren(
6556 llvm::cast<clang::ParenType>(qual_type)->desugar());
6557 case clang::Type::Typedef:
6558 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6559 ->getDecl()
6560 ->getUnderlyingType());
6561 case clang::Type::Auto:
6562 return GetNumPointeeChildren(
6563 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6564 case clang::Type::Elaborated:
6565 return GetNumPointeeChildren(
6566 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6567 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006568 return GetNumPointeeChildren(llvm::cast<clang::TypeOfExprType>(qual_type)
6569 ->getUnderlyingExpr()
6570 ->getType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006571 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006572 return GetNumPointeeChildren(
6573 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006574 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006575 return GetNumPointeeChildren(
6576 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006577 case clang::Type::Record:
6578 return 0;
6579 case clang::Type::Enum:
6580 return 1;
6581 case clang::Type::TemplateTypeParm:
6582 return 1;
6583 case clang::Type::SubstTemplateTypeParm:
6584 return 1;
6585 case clang::Type::TemplateSpecialization:
6586 return 1;
6587 case clang::Type::InjectedClassName:
6588 return 0;
6589 case clang::Type::DependentName:
6590 return 1;
6591 case clang::Type::DependentTemplateSpecialization:
6592 return 1;
6593 case clang::Type::ObjCObject:
6594 return 0;
6595 case clang::Type::ObjCInterface:
6596 return 0;
6597 case clang::Type::ObjCObjectPointer:
6598 return 1;
6599 default:
6600 break;
6601 }
6602 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006603}
6604
Kate Stoneb9c1b512016-09-06 20:57:50 +00006605CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6606 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6607 bool transparent_pointers, bool omit_empty_base_classes,
6608 bool ignore_array_bounds, std::string &child_name,
6609 uint32_t &child_byte_size, int32_t &child_byte_offset,
6610 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6611 bool &child_is_base_class, bool &child_is_deref_of_parent,
6612 ValueObject *valobj, uint64_t &language_flags) {
6613 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006614 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006615
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006616 auto get_exe_scope = [&exe_ctx]() {
6617 return exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr;
6618 };
6619
Kate Stoneb9c1b512016-09-06 20:57:50 +00006620 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6621 const clang::Type::TypeClass parent_type_class =
6622 parent_qual_type->getTypeClass();
6623 child_bitfield_bit_size = 0;
6624 child_bitfield_bit_offset = 0;
6625 child_is_base_class = false;
6626 language_flags = 0;
6627
Adrian Prantleca07c52018-11-05 20:49:07 +00006628 const bool idx_is_valid =
6629 idx < GetNumChildren(type, omit_empty_base_classes, exe_ctx);
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00006630 int32_t bit_offset;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006631 switch (parent_type_class) {
6632 case clang::Type::Builtin:
6633 if (idx_is_valid) {
6634 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6635 case clang::BuiltinType::ObjCId:
6636 case clang::BuiltinType::ObjCClass:
6637 child_name = "isa";
6638 child_byte_size =
6639 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6640 CHAR_BIT;
6641 return CompilerType(getASTContext(),
6642 getASTContext()->ObjCBuiltinClassTy);
6643
6644 default:
6645 break;
6646 }
6647 }
6648 break;
6649
6650 case clang::Type::Record:
6651 if (idx_is_valid && GetCompleteType(type)) {
6652 const clang::RecordType *record_type =
6653 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6654 const clang::RecordDecl *record_decl = record_type->getDecl();
6655 assert(record_decl);
6656 const clang::ASTRecordLayout &record_layout =
6657 getASTContext()->getASTRecordLayout(record_decl);
6658 uint32_t child_idx = 0;
6659
6660 const clang::CXXRecordDecl *cxx_record_decl =
6661 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6662 if (cxx_record_decl) {
6663 // We might have base classes to print out first
6664 clang::CXXRecordDecl::base_class_const_iterator base_class,
6665 base_class_end;
6666 for (base_class = cxx_record_decl->bases_begin(),
6667 base_class_end = cxx_record_decl->bases_end();
6668 base_class != base_class_end; ++base_class) {
6669 const clang::CXXRecordDecl *base_class_decl = nullptr;
6670
6671 // Skip empty base classes
6672 if (omit_empty_base_classes) {
6673 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6674 base_class->getType()->getAs<clang::RecordType>()->getDecl());
Jonas Devliegherea6682a42018-12-15 00:15:33 +00006675 if (!ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00006676 continue;
6677 }
6678
6679 if (idx == child_idx) {
6680 if (base_class_decl == nullptr)
6681 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6682 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6683
6684 if (base_class->isVirtual()) {
6685 bool handled = false;
6686 if (valobj) {
Aleksandr Urakov1dc51db2018-11-12 16:23:50 +00006687 clang::VTableContextBase *vtable_ctx =
6688 getASTContext()->getVTableContext();
6689 if (vtable_ctx)
6690 handled = GetVBaseBitOffset(*vtable_ctx, *valobj,
6691 record_layout, cxx_record_decl,
6692 base_class_decl, bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006693 }
6694 if (!handled)
6695 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6696 .getQuantity() *
6697 8;
6698 } else
6699 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6700 .getQuantity() *
6701 8;
6702
6703 // Base classes should be a multiple of 8 bits in size
6704 child_byte_offset = bit_offset / 8;
6705 CompilerType base_class_clang_type(getASTContext(),
6706 base_class->getType());
6707 child_name = base_class_clang_type.GetTypeName().AsCString("");
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006708 Optional<uint64_t> size =
6709 base_class_clang_type.GetBitSize(get_exe_scope());
Adrian Prantld963a7c2019-01-15 18:07:52 +00006710 if (!size)
6711 return {};
6712 uint64_t base_class_clang_type_bit_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006713
6714 // Base classes bit sizes should be a multiple of 8 bits in size
6715 assert(base_class_clang_type_bit_size % 8 == 0);
6716 child_byte_size = base_class_clang_type_bit_size / 8;
6717 child_is_base_class = true;
6718 return base_class_clang_type;
6719 }
6720 // We don't increment the child index in the for loop since we might
6721 // be skipping empty base classes
6722 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006723 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006724 }
6725 // Make sure index is in range...
6726 uint32_t field_idx = 0;
6727 clang::RecordDecl::field_iterator field, field_end;
6728 for (field = record_decl->field_begin(),
6729 field_end = record_decl->field_end();
6730 field != field_end; ++field, ++field_idx, ++child_idx) {
6731 if (idx == child_idx) {
6732 // Print the member type if requested
6733 // Print the member name and equal sign
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006734 child_name.assign(field->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006735
6736 // Figure out the type byte size (field_type_info.first) and
6737 // alignment (field_type_info.second) from the AST context.
6738 CompilerType field_clang_type(getASTContext(), field->getType());
6739 assert(field_idx < record_layout.getFieldCount());
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006740 Optional<uint64_t> size =
6741 field_clang_type.GetByteSize(get_exe_scope());
Adrian Prantld963a7c2019-01-15 18:07:52 +00006742 if (!size)
6743 return {};
6744 child_byte_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006745 const uint32_t child_bit_size = child_byte_size * 8;
6746
6747 // Figure out the field offset within the current struct/union/class
6748 // type
6749 bit_offset = record_layout.getFieldOffset(field_idx);
6750 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6751 child_bitfield_bit_size)) {
6752 child_bitfield_bit_offset = bit_offset % child_bit_size;
6753 const uint32_t child_bit_offset =
6754 bit_offset - child_bitfield_bit_offset;
6755 child_byte_offset = child_bit_offset / 8;
6756 } else {
6757 child_byte_offset = bit_offset / 8;
6758 }
6759
6760 return field_clang_type;
6761 }
6762 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006763 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006764 break;
6765
6766 case clang::Type::ObjCObject:
6767 case clang::Type::ObjCInterface:
6768 if (idx_is_valid && GetCompleteType(type)) {
6769 const clang::ObjCObjectType *objc_class_type =
6770 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6771 assert(objc_class_type);
6772 if (objc_class_type) {
6773 uint32_t child_idx = 0;
6774 clang::ObjCInterfaceDecl *class_interface_decl =
6775 objc_class_type->getInterface();
6776
6777 if (class_interface_decl) {
6778
6779 const clang::ASTRecordLayout &interface_layout =
6780 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6781 clang::ObjCInterfaceDecl *superclass_interface_decl =
6782 class_interface_decl->getSuperClass();
6783 if (superclass_interface_decl) {
6784 if (omit_empty_base_classes) {
6785 CompilerType base_class_clang_type(
6786 getASTContext(), getASTContext()->getObjCInterfaceType(
6787 superclass_interface_decl));
Adrian Prantleca07c52018-11-05 20:49:07 +00006788 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes,
6789 exe_ctx) > 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006790 if (idx == 0) {
6791 clang::QualType ivar_qual_type(
6792 getASTContext()->getObjCInterfaceType(
6793 superclass_interface_decl));
6794
6795 child_name.assign(
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006796 superclass_interface_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006797
6798 clang::TypeInfo ivar_type_info =
6799 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6800
6801 child_byte_size = ivar_type_info.Width / 8;
6802 child_byte_offset = 0;
6803 child_is_base_class = true;
6804
6805 return CompilerType(getASTContext(), ivar_qual_type);
6806 }
6807
6808 ++child_idx;
6809 }
6810 } else
6811 ++child_idx;
6812 }
6813
6814 const uint32_t superclass_idx = child_idx;
6815
6816 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6817 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6818 ivar_end = class_interface_decl->ivar_end();
6819
6820 for (ivar_pos = class_interface_decl->ivar_begin();
6821 ivar_pos != ivar_end; ++ivar_pos) {
6822 if (child_idx == idx) {
6823 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6824
6825 clang::QualType ivar_qual_type(ivar_decl->getType());
6826
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006827 child_name.assign(ivar_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006828
6829 clang::TypeInfo ivar_type_info =
6830 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6831
6832 child_byte_size = ivar_type_info.Width / 8;
6833
6834 // Figure out the field offset within the current
Adrian Prantl05097242018-04-30 16:49:04 +00006835 // struct/union/class type For ObjC objects, we can't trust the
6836 // bit offset we get from the Clang AST, since that doesn't
6837 // account for the space taken up by unbacked properties, or
6838 // from the changing size of base classes that are newer than
6839 // this class. So if we have a process around that we can ask
6840 // about this object, do so.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006841 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6842 Process *process = nullptr;
6843 if (exe_ctx)
6844 process = exe_ctx->GetProcessPtr();
6845 if (process) {
6846 ObjCLanguageRuntime *objc_runtime =
Alex Langforde823bbe2019-06-10 20:53:23 +00006847 ObjCLanguageRuntime::Get(*process);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006848 if (objc_runtime != nullptr) {
6849 CompilerType parent_ast_type(getASTContext(),
6850 parent_qual_type);
6851 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6852 parent_ast_type, ivar_decl->getNameAsString().c_str());
6853 }
6854 }
6855
Aleksandr Urakovff701722018-08-20 05:59:27 +00006856 // Setting this to INT32_MAX to make sure we don't compute it
Kate Stoneb9c1b512016-09-06 20:57:50 +00006857 // twice...
Aleksandr Urakov53459482018-08-17 07:28:24 +00006858 bit_offset = INT32_MAX;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006859
6860 if (child_byte_offset ==
6861 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6862 bit_offset = interface_layout.getFieldOffset(child_idx -
6863 superclass_idx);
6864 child_byte_offset = bit_offset / 8;
6865 }
6866
6867 // Note, the ObjC Ivar Byte offset is just that, it doesn't
Adrian Prantl05097242018-04-30 16:49:04 +00006868 // account for the bit offset of a bitfield within its
6869 // containing object. So regardless of where we get the byte
Kate Stoneb9c1b512016-09-06 20:57:50 +00006870 // offset from, we still need to get the bit offset for
6871 // bitfields from the layout.
6872
6873 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6874 child_bitfield_bit_size)) {
Aleksandr Urakov53459482018-08-17 07:28:24 +00006875 if (bit_offset == INT32_MAX)
Kate Stoneb9c1b512016-09-06 20:57:50 +00006876 bit_offset = interface_layout.getFieldOffset(
6877 child_idx - superclass_idx);
6878
6879 child_bitfield_bit_offset = bit_offset % 8;
6880 }
6881 return CompilerType(getASTContext(), ivar_qual_type);
6882 }
6883 ++child_idx;
6884 }
6885 }
6886 }
6887 }
6888 }
6889 break;
6890
6891 case clang::Type::ObjCObjectPointer:
6892 if (idx_is_valid) {
6893 CompilerType pointee_clang_type(GetPointeeType(type));
6894
6895 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6896 child_is_deref_of_parent = false;
6897 bool tmp_child_is_deref_of_parent = false;
6898 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6899 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6900 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6901 child_bitfield_bit_size, child_bitfield_bit_offset,
6902 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6903 language_flags);
6904 } else {
6905 child_is_deref_of_parent = true;
6906 const char *parent_name =
Konrad Kleine248a1302019-05-23 11:14:47 +00006907 valobj ? valobj->GetName().GetCString() : nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006908 if (parent_name) {
6909 child_name.assign(1, '*');
6910 child_name += parent_name;
6911 }
6912
6913 // We have a pointer to an simple type
6914 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006915 if (Optional<uint64_t> size =
6916 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006917 child_byte_size = *size;
6918 child_byte_offset = 0;
6919 return pointee_clang_type;
6920 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006921 }
6922 }
6923 }
6924 break;
6925
6926 case clang::Type::Vector:
6927 case clang::Type::ExtVector:
6928 if (idx_is_valid) {
6929 const clang::VectorType *array =
6930 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6931 if (array) {
6932 CompilerType element_type(getASTContext(), array->getElementType());
6933 if (element_type.GetCompleteType()) {
6934 char element_name[64];
6935 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6936 static_cast<uint64_t>(idx));
6937 child_name.assign(element_name);
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006938 if (Optional<uint64_t> size =
6939 element_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006940 child_byte_size = *size;
6941 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6942 return element_type;
6943 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006944 }
6945 }
6946 }
6947 break;
6948
6949 case clang::Type::ConstantArray:
6950 case clang::Type::IncompleteArray:
6951 if (ignore_array_bounds || idx_is_valid) {
6952 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6953 if (array) {
6954 CompilerType element_type(getASTContext(), array->getElementType());
6955 if (element_type.GetCompleteType()) {
Zachary Turner827d5d72016-12-16 04:27:00 +00006956 child_name = llvm::formatv("[{0}]", idx);
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006957 if (Optional<uint64_t> size =
6958 element_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006959 child_byte_size = *size;
6960 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6961 return element_type;
6962 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006963 }
6964 }
6965 }
6966 break;
6967
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006968 case clang::Type::Pointer: {
6969 CompilerType pointee_clang_type(GetPointeeType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00006970
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006971 // Don't dereference "void *" pointers
6972 if (pointee_clang_type.IsVoidType())
6973 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006974
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006975 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6976 child_is_deref_of_parent = false;
6977 bool tmp_child_is_deref_of_parent = false;
6978 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6979 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6980 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6981 child_bitfield_bit_size, child_bitfield_bit_offset,
6982 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6983 language_flags);
6984 } else {
6985 child_is_deref_of_parent = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006986
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006987 const char *parent_name =
Konrad Kleine248a1302019-05-23 11:14:47 +00006988 valobj ? valobj->GetName().GetCString() : nullptr;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006989 if (parent_name) {
6990 child_name.assign(1, '*');
6991 child_name += parent_name;
6992 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006993
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006994 // We have a pointer to an simple type
6995 if (idx == 0) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006996 if (Optional<uint64_t> size =
6997 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006998 child_byte_size = *size;
6999 child_byte_offset = 0;
7000 return pointee_clang_type;
7001 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007002 }
7003 }
7004 break;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00007005 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007006
7007 case clang::Type::LValueReference:
7008 case clang::Type::RValueReference:
7009 if (idx_is_valid) {
7010 const clang::ReferenceType *reference_type =
7011 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
7012 CompilerType pointee_clang_type(getASTContext(),
7013 reference_type->getPointeeType());
7014 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
7015 child_is_deref_of_parent = false;
7016 bool tmp_child_is_deref_of_parent = false;
7017 return pointee_clang_type.GetChildCompilerTypeAtIndex(
7018 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7019 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7020 child_bitfield_bit_size, child_bitfield_bit_offset,
7021 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
7022 language_flags);
7023 } else {
7024 const char *parent_name =
Konrad Kleine248a1302019-05-23 11:14:47 +00007025 valobj ? valobj->GetName().GetCString() : nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007026 if (parent_name) {
7027 child_name.assign(1, '&');
7028 child_name += parent_name;
7029 }
7030
7031 // We have a pointer to an simple type
7032 if (idx == 0) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00007033 if (Optional<uint64_t> size =
7034 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00007035 child_byte_size = *size;
7036 child_byte_offset = 0;
7037 return pointee_clang_type;
7038 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007039 }
7040 }
7041 }
7042 break;
7043
7044 case clang::Type::Typedef: {
7045 CompilerType typedefed_clang_type(
7046 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)
7047 ->getDecl()
7048 ->getUnderlyingType());
7049 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
7050 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7051 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7052 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7053 child_is_deref_of_parent, valobj, language_flags);
7054 } break;
7055
7056 case clang::Type::Auto: {
7057 CompilerType elaborated_clang_type(
7058 getASTContext(),
7059 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
7060 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
7061 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7062 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7063 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7064 child_is_deref_of_parent, valobj, language_flags);
7065 }
7066
7067 case clang::Type::Elaborated: {
7068 CompilerType elaborated_clang_type(
7069 getASTContext(),
7070 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
7071 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
7072 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7073 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7074 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7075 child_is_deref_of_parent, valobj, language_flags);
7076 }
7077
7078 case clang::Type::Paren: {
7079 CompilerType paren_clang_type(
7080 getASTContext(),
7081 llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
7082 return paren_clang_type.GetChildCompilerTypeAtIndex(
7083 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7084 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7085 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7086 child_is_deref_of_parent, valobj, language_flags);
7087 }
7088
7089 default:
7090 break;
7091 }
7092 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007093}
7094
Kate Stoneb9c1b512016-09-06 20:57:50 +00007095static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
7096 const clang::CXXBaseSpecifier *base_spec,
7097 bool omit_empty_base_classes) {
7098 uint32_t child_idx = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007099
Kate Stoneb9c1b512016-09-06 20:57:50 +00007100 const clang::CXXRecordDecl *cxx_record_decl =
7101 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7102
7103 // const char *super_name = record_decl->getNameAsCString();
7104 // const char *base_name =
7105 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
7106 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
7107 //
7108 if (cxx_record_decl) {
7109 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
7110 for (base_class = cxx_record_decl->bases_begin(),
7111 base_class_end = cxx_record_decl->bases_end();
7112 base_class != base_class_end; ++base_class) {
7113 if (omit_empty_base_classes) {
7114 if (BaseSpecifierIsEmpty(base_class))
7115 continue;
7116 }
7117
7118 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
7119 // super_name, base_name,
7120 // child_idx,
7121 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
7122 //
7123 //
7124 if (base_class == base_spec)
7125 return child_idx;
7126 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00007127 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007128 }
7129
7130 return UINT32_MAX;
7131}
7132
7133static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
7134 clang::NamedDecl *canonical_decl,
7135 bool omit_empty_base_classes) {
7136 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
7137 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
7138 omit_empty_base_classes);
7139
7140 clang::RecordDecl::field_iterator field, field_end;
7141 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
7142 field != field_end; ++field, ++child_idx) {
7143 if (field->getCanonicalDecl() == canonical_decl)
7144 return child_idx;
7145 }
7146
7147 return UINT32_MAX;
Greg Claytond8d4a572015-08-11 21:38:15 +00007148}
7149
7150// Look for a child member (doesn't include base classes, but it does include
Adrian Prantl05097242018-04-30 16:49:04 +00007151// their members) in the type hierarchy. Returns an index path into
7152// "clang_type" on how to reach the appropriate member.
Greg Claytond8d4a572015-08-11 21:38:15 +00007153//
7154// class A
7155// {
7156// public:
7157// int m_a;
7158// int m_b;
7159// };
7160//
7161// class B
7162// {
7163// };
7164//
7165// class C :
7166// public B,
7167// public A
7168// {
7169// };
7170//
7171// If we have a clang type that describes "class C", and we wanted to looked
7172// "m_b" in it:
7173//
Kate Stoneb9c1b512016-09-06 20:57:50 +00007174// With omit_empty_base_classes == false we would get an integer array back
Adrian Prantl05097242018-04-30 16:49:04 +00007175// with: { 1, 1 } The first index 1 is the child index for "class A" within
7176// class C The second index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007177//
Adrian Prantl05097242018-04-30 16:49:04 +00007178// With omit_empty_base_classes == true we would get an integer array back
7179// with: { 0, 1 } The first index 0 is the child index for "class A" within
7180// class C (since class B doesn't have any members it doesn't count) The second
7181// index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007182
Kate Stoneb9c1b512016-09-06 20:57:50 +00007183size_t ClangASTContext::GetIndexOfChildMemberWithName(
7184 lldb::opaque_compiler_type_t type, const char *name,
7185 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
7186 if (type && name && name[0]) {
7187 clang::QualType qual_type(GetCanonicalQualType(type));
7188 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7189 switch (type_class) {
7190 case clang::Type::Record:
7191 if (GetCompleteType(type)) {
7192 const clang::RecordType *record_type =
7193 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7194 const clang::RecordDecl *record_decl = record_type->getDecl();
Enrico Granata36f51e42015-12-18 22:41:25 +00007195
Kate Stoneb9c1b512016-09-06 20:57:50 +00007196 assert(record_decl);
7197 uint32_t child_idx = 0;
7198
7199 const clang::CXXRecordDecl *cxx_record_decl =
7200 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7201
7202 // Try and find a field that matches NAME
7203 clang::RecordDecl::field_iterator field, field_end;
7204 llvm::StringRef name_sref(name);
7205 for (field = record_decl->field_begin(),
7206 field_end = record_decl->field_end();
7207 field != field_end; ++field, ++child_idx) {
7208 llvm::StringRef field_name = field->getName();
7209 if (field_name.empty()) {
7210 CompilerType field_type(getASTContext(), field->getType());
7211 child_indexes.push_back(child_idx);
7212 if (field_type.GetIndexOfChildMemberWithName(
7213 name, omit_empty_base_classes, child_indexes))
7214 return child_indexes.size();
7215 child_indexes.pop_back();
7216
7217 } else if (field_name.equals(name_sref)) {
7218 // We have to add on the number of base classes to this index!
7219 child_indexes.push_back(
7220 child_idx + ClangASTContext::GetNumBaseClasses(
7221 cxx_record_decl, omit_empty_base_classes));
7222 return child_indexes.size();
7223 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007224 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007225
Kate Stoneb9c1b512016-09-06 20:57:50 +00007226 if (cxx_record_decl) {
7227 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7228
7229 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7230
7231 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7232 // Didn't find things easily, lets let clang do its thang...
7233 clang::IdentifierInfo &ident_ref =
7234 getASTContext()->Idents.get(name_sref);
7235 clang::DeclarationName decl_name(&ident_ref);
7236
7237 clang::CXXBasePaths paths;
7238 if (cxx_record_decl->lookupInBases(
7239 [decl_name](const clang::CXXBaseSpecifier *specifier,
7240 clang::CXXBasePath &path) {
7241 return clang::CXXRecordDecl::FindOrdinaryMember(
7242 specifier, path, decl_name);
7243 },
7244 paths)) {
7245 clang::CXXBasePaths::const_paths_iterator path,
7246 path_end = paths.end();
7247 for (path = paths.begin(); path != path_end; ++path) {
7248 const size_t num_path_elements = path->size();
7249 for (size_t e = 0; e < num_path_elements; ++e) {
7250 clang::CXXBasePathElement elem = (*path)[e];
7251
7252 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7253 omit_empty_base_classes);
7254 if (child_idx == UINT32_MAX) {
7255 child_indexes.clear();
7256 return 0;
7257 } else {
7258 child_indexes.push_back(child_idx);
7259 parent_record_decl = llvm::cast<clang::RecordDecl>(
7260 elem.Base->getType()
7261 ->getAs<clang::RecordType>()
7262 ->getDecl());
7263 }
7264 }
7265 for (clang::NamedDecl *path_decl : path->Decls) {
7266 child_idx = GetIndexForRecordChild(
7267 parent_record_decl, path_decl, omit_empty_base_classes);
7268 if (child_idx == UINT32_MAX) {
7269 child_indexes.clear();
7270 return 0;
7271 } else {
7272 child_indexes.push_back(child_idx);
7273 }
7274 }
7275 }
7276 return child_indexes.size();
7277 }
7278 }
7279 }
7280 break;
7281
7282 case clang::Type::ObjCObject:
7283 case clang::Type::ObjCInterface:
7284 if (GetCompleteType(type)) {
7285 llvm::StringRef name_sref(name);
7286 const clang::ObjCObjectType *objc_class_type =
7287 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7288 assert(objc_class_type);
7289 if (objc_class_type) {
7290 uint32_t child_idx = 0;
7291 clang::ObjCInterfaceDecl *class_interface_decl =
7292 objc_class_type->getInterface();
7293
7294 if (class_interface_decl) {
7295 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7296 ivar_end = class_interface_decl->ivar_end();
7297 clang::ObjCInterfaceDecl *superclass_interface_decl =
7298 class_interface_decl->getSuperClass();
7299
7300 for (ivar_pos = class_interface_decl->ivar_begin();
7301 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7302 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7303
7304 if (ivar_decl->getName().equals(name_sref)) {
7305 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7306 (omit_empty_base_classes &&
7307 ObjCDeclHasIVars(superclass_interface_decl, true)))
7308 ++child_idx;
7309
7310 child_indexes.push_back(child_idx);
7311 return child_indexes.size();
7312 }
7313 }
7314
7315 if (superclass_interface_decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00007316 // The super class index is always zero for ObjC classes, so we
7317 // push it onto the child indexes in case we find an ivar in our
7318 // superclass...
Kate Stoneb9c1b512016-09-06 20:57:50 +00007319 child_indexes.push_back(0);
7320
7321 CompilerType superclass_clang_type(
7322 getASTContext(), getASTContext()->getObjCInterfaceType(
7323 superclass_interface_decl));
7324 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7325 name, omit_empty_base_classes, child_indexes)) {
Adrian Prantl05097242018-04-30 16:49:04 +00007326 // We did find an ivar in a superclass so just return the
7327 // results!
Kate Stoneb9c1b512016-09-06 20:57:50 +00007328 return child_indexes.size();
7329 }
7330
Adrian Prantl05097242018-04-30 16:49:04 +00007331 // We didn't find an ivar matching "name" in our superclass, pop
7332 // the superclass zero index that we pushed on above.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007333 child_indexes.pop_back();
7334 }
7335 }
7336 }
7337 }
7338 break;
7339
7340 case clang::Type::ObjCObjectPointer: {
7341 CompilerType objc_object_clang_type(
7342 getASTContext(),
7343 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7344 ->getPointeeType());
7345 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7346 name, omit_empty_base_classes, child_indexes);
7347 } break;
7348
7349 case clang::Type::ConstantArray: {
7350 // const clang::ConstantArrayType *array =
7351 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7352 // const uint64_t element_count =
7353 // array->getSize().getLimitedValue();
7354 //
7355 // if (idx < element_count)
7356 // {
7357 // std::pair<uint64_t, unsigned> field_type_info =
7358 // ast->getTypeInfo(array->getElementType());
7359 //
7360 // char element_name[32];
7361 // ::snprintf (element_name, sizeof (element_name),
7362 // "%s[%u]", parent_name ? parent_name : "", idx);
7363 //
7364 // child_name.assign(element_name);
7365 // assert(field_type_info.first % 8 == 0);
7366 // child_byte_size = field_type_info.first / 8;
7367 // child_byte_offset = idx * child_byte_size;
7368 // return array->getElementType().getAsOpaquePtr();
7369 // }
7370 } break;
7371
7372 // case clang::Type::MemberPointerType:
7373 // {
7374 // MemberPointerType *mem_ptr_type =
7375 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7376 // clang::QualType pointee_type =
7377 // mem_ptr_type->getPointeeType();
7378 //
7379 // if (ClangASTContext::IsAggregateType
7380 // (pointee_type.getAsOpaquePtr()))
7381 // {
7382 // return GetIndexOfChildWithName (ast,
7383 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7384 // name);
7385 // }
7386 // }
7387 // break;
7388 //
7389 case clang::Type::LValueReference:
7390 case clang::Type::RValueReference: {
7391 const clang::ReferenceType *reference_type =
7392 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7393 clang::QualType pointee_type(reference_type->getPointeeType());
7394 CompilerType pointee_clang_type(getASTContext(), pointee_type);
7395
7396 if (pointee_clang_type.IsAggregateType()) {
7397 return pointee_clang_type.GetIndexOfChildMemberWithName(
7398 name, omit_empty_base_classes, child_indexes);
7399 }
7400 } break;
7401
7402 case clang::Type::Pointer: {
7403 CompilerType pointee_clang_type(GetPointeeType(type));
7404
7405 if (pointee_clang_type.IsAggregateType()) {
7406 return pointee_clang_type.GetIndexOfChildMemberWithName(
7407 name, omit_empty_base_classes, child_indexes);
7408 }
7409 } break;
7410
7411 case clang::Type::Typedef:
7412 return CompilerType(getASTContext(),
7413 llvm::cast<clang::TypedefType>(qual_type)
7414 ->getDecl()
7415 ->getUnderlyingType())
7416 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7417 child_indexes);
7418
7419 case clang::Type::Auto:
7420 return CompilerType(
7421 getASTContext(),
7422 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7423 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7424 child_indexes);
7425
7426 case clang::Type::Elaborated:
7427 return CompilerType(
7428 getASTContext(),
7429 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7430 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7431 child_indexes);
7432
7433 case clang::Type::Paren:
7434 return CompilerType(getASTContext(),
7435 llvm::cast<clang::ParenType>(qual_type)->desugar())
7436 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7437 child_indexes);
7438
7439 default:
7440 break;
7441 }
7442 }
7443 return 0;
7444}
Greg Claytond8d4a572015-08-11 21:38:15 +00007445
7446// Get the index of the child of "clang_type" whose name matches. This function
7447// doesn't descend into the children, but only looks one level deep and name
7448// matches can include base class names.
7449
7450uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007451ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7452 const char *name,
7453 bool omit_empty_base_classes) {
7454 if (type && name && name[0]) {
7455 clang::QualType qual_type(GetCanonicalQualType(type));
Enrico Granata36f51e42015-12-18 22:41:25 +00007456
Kate Stoneb9c1b512016-09-06 20:57:50 +00007457 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7458
7459 switch (type_class) {
7460 case clang::Type::Record:
7461 if (GetCompleteType(type)) {
7462 const clang::RecordType *record_type =
7463 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7464 const clang::RecordDecl *record_decl = record_type->getDecl();
7465
7466 assert(record_decl);
7467 uint32_t child_idx = 0;
7468
7469 const clang::CXXRecordDecl *cxx_record_decl =
7470 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7471
7472 if (cxx_record_decl) {
7473 clang::CXXRecordDecl::base_class_const_iterator base_class,
7474 base_class_end;
7475 for (base_class = cxx_record_decl->bases_begin(),
7476 base_class_end = cxx_record_decl->bases_end();
7477 base_class != base_class_end; ++base_class) {
7478 // Skip empty base classes
7479 clang::CXXRecordDecl *base_class_decl =
7480 llvm::cast<clang::CXXRecordDecl>(
7481 base_class->getType()
7482 ->getAs<clang::RecordType>()
7483 ->getDecl());
7484 if (omit_empty_base_classes &&
Jonas Devliegherea6682a42018-12-15 00:15:33 +00007485 !ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00007486 continue;
7487
7488 CompilerType base_class_clang_type(getASTContext(),
7489 base_class->getType());
7490 std::string base_class_type_name(
7491 base_class_clang_type.GetTypeName().AsCString(""));
Jonas Devlieghere8d20cfd2018-12-21 22:46:10 +00007492 if (base_class_type_name == name)
Kate Stoneb9c1b512016-09-06 20:57:50 +00007493 return child_idx;
7494 ++child_idx;
7495 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007496 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007497
Kate Stoneb9c1b512016-09-06 20:57:50 +00007498 // Try and find a field that matches NAME
7499 clang::RecordDecl::field_iterator field, field_end;
7500 llvm::StringRef name_sref(name);
7501 for (field = record_decl->field_begin(),
7502 field_end = record_decl->field_end();
7503 field != field_end; ++field, ++child_idx) {
7504 if (field->getName().equals(name_sref))
7505 return child_idx;
7506 }
7507 }
7508 break;
7509
7510 case clang::Type::ObjCObject:
7511 case clang::Type::ObjCInterface:
7512 if (GetCompleteType(type)) {
7513 llvm::StringRef name_sref(name);
7514 const clang::ObjCObjectType *objc_class_type =
7515 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7516 assert(objc_class_type);
7517 if (objc_class_type) {
7518 uint32_t child_idx = 0;
7519 clang::ObjCInterfaceDecl *class_interface_decl =
7520 objc_class_type->getInterface();
7521
7522 if (class_interface_decl) {
7523 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7524 ivar_end = class_interface_decl->ivar_end();
7525 clang::ObjCInterfaceDecl *superclass_interface_decl =
7526 class_interface_decl->getSuperClass();
7527
7528 for (ivar_pos = class_interface_decl->ivar_begin();
7529 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7530 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7531
7532 if (ivar_decl->getName().equals(name_sref)) {
7533 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7534 (omit_empty_base_classes &&
7535 ObjCDeclHasIVars(superclass_interface_decl, true)))
7536 ++child_idx;
7537
7538 return child_idx;
7539 }
7540 }
7541
7542 if (superclass_interface_decl) {
7543 if (superclass_interface_decl->getName().equals(name_sref))
7544 return 0;
7545 }
7546 }
7547 }
7548 }
7549 break;
7550
7551 case clang::Type::ObjCObjectPointer: {
7552 CompilerType pointee_clang_type(
7553 getASTContext(),
7554 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7555 ->getPointeeType());
7556 return pointee_clang_type.GetIndexOfChildWithName(
7557 name, omit_empty_base_classes);
7558 } break;
7559
7560 case clang::Type::ConstantArray: {
7561 // const clang::ConstantArrayType *array =
7562 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7563 // const uint64_t element_count =
7564 // array->getSize().getLimitedValue();
7565 //
7566 // if (idx < element_count)
7567 // {
7568 // std::pair<uint64_t, unsigned> field_type_info =
7569 // ast->getTypeInfo(array->getElementType());
7570 //
7571 // char element_name[32];
7572 // ::snprintf (element_name, sizeof (element_name),
7573 // "%s[%u]", parent_name ? parent_name : "", idx);
7574 //
7575 // child_name.assign(element_name);
7576 // assert(field_type_info.first % 8 == 0);
7577 // child_byte_size = field_type_info.first / 8;
7578 // child_byte_offset = idx * child_byte_size;
7579 // return array->getElementType().getAsOpaquePtr();
7580 // }
7581 } break;
7582
7583 // case clang::Type::MemberPointerType:
7584 // {
7585 // MemberPointerType *mem_ptr_type =
7586 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7587 // clang::QualType pointee_type =
7588 // mem_ptr_type->getPointeeType();
7589 //
7590 // if (ClangASTContext::IsAggregateType
7591 // (pointee_type.getAsOpaquePtr()))
7592 // {
7593 // return GetIndexOfChildWithName (ast,
7594 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7595 // name);
7596 // }
7597 // }
7598 // break;
7599 //
7600 case clang::Type::LValueReference:
7601 case clang::Type::RValueReference: {
7602 const clang::ReferenceType *reference_type =
7603 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7604 CompilerType pointee_type(getASTContext(),
7605 reference_type->getPointeeType());
7606
7607 if (pointee_type.IsAggregateType()) {
7608 return pointee_type.GetIndexOfChildWithName(name,
7609 omit_empty_base_classes);
7610 }
7611 } break;
7612
7613 case clang::Type::Pointer: {
7614 const clang::PointerType *pointer_type =
7615 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7616 CompilerType pointee_type(getASTContext(),
7617 pointer_type->getPointeeType());
7618
7619 if (pointee_type.IsAggregateType()) {
7620 return pointee_type.GetIndexOfChildWithName(name,
7621 omit_empty_base_classes);
7622 } else {
7623 // if (parent_name)
7624 // {
7625 // child_name.assign(1, '*');
7626 // child_name += parent_name;
7627 // }
7628 //
7629 // // We have a pointer to an simple type
7630 // if (idx == 0)
7631 // {
7632 // std::pair<uint64_t, unsigned> clang_type_info
7633 // = ast->getTypeInfo(pointee_type);
7634 // assert(clang_type_info.first % 8 == 0);
7635 // child_byte_size = clang_type_info.first / 8;
7636 // child_byte_offset = 0;
7637 // return pointee_type.getAsOpaquePtr();
7638 // }
7639 }
7640 } break;
7641
7642 case clang::Type::Auto:
7643 return CompilerType(
7644 getASTContext(),
7645 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7646 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7647
7648 case clang::Type::Elaborated:
7649 return CompilerType(
7650 getASTContext(),
7651 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7652 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7653
7654 case clang::Type::Paren:
7655 return CompilerType(getASTContext(),
7656 llvm::cast<clang::ParenType>(qual_type)->desugar())
7657 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7658
7659 case clang::Type::Typedef:
7660 return CompilerType(getASTContext(),
7661 llvm::cast<clang::TypedefType>(qual_type)
7662 ->getDecl()
7663 ->getUnderlyingType())
7664 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7665
7666 default:
7667 break;
7668 }
7669 }
7670 return UINT32_MAX;
7671}
Greg Claytond8d4a572015-08-11 21:38:15 +00007672
7673size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007674ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7675 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007676 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007677
Kate Stoneb9c1b512016-09-06 20:57:50 +00007678 clang::QualType qual_type(GetCanonicalQualType(type));
7679 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7680 switch (type_class) {
7681 case clang::Type::Record:
7682 if (GetCompleteType(type)) {
7683 const clang::CXXRecordDecl *cxx_record_decl =
7684 qual_type->getAsCXXRecordDecl();
7685 if (cxx_record_decl) {
7686 const clang::ClassTemplateSpecializationDecl *template_decl =
7687 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7688 cxx_record_decl);
7689 if (template_decl)
7690 return template_decl->getTemplateArgs().size();
7691 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007692 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007693 break;
7694
7695 case clang::Type::Typedef:
7696 return (CompilerType(getASTContext(),
7697 llvm::cast<clang::TypedefType>(qual_type)
7698 ->getDecl()
7699 ->getUnderlyingType()))
7700 .GetNumTemplateArguments();
7701
7702 case clang::Type::Auto:
7703 return (CompilerType(
7704 getASTContext(),
7705 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7706 .GetNumTemplateArguments();
7707
7708 case clang::Type::Elaborated:
7709 return (CompilerType(
7710 getASTContext(),
7711 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7712 .GetNumTemplateArguments();
7713
7714 case clang::Type::Paren:
7715 return (CompilerType(getASTContext(),
7716 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7717 .GetNumTemplateArguments();
7718
7719 default:
7720 break;
7721 }
7722
7723 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007724}
7725
Pavel Labath769b21e2017-11-13 14:26:21 +00007726const clang::ClassTemplateSpecializationDecl *
7727ClangASTContext::GetAsTemplateSpecialization(
7728 lldb::opaque_compiler_type_t type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00007729 if (!type)
Pavel Labath769b21e2017-11-13 14:26:21 +00007730 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007731
7732 clang::QualType qual_type(GetCanonicalQualType(type));
7733 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7734 switch (type_class) {
Pavel Labath769b21e2017-11-13 14:26:21 +00007735 case clang::Type::Record: {
7736 if (! GetCompleteType(type))
7737 return nullptr;
7738 const clang::CXXRecordDecl *cxx_record_decl =
7739 qual_type->getAsCXXRecordDecl();
7740 if (!cxx_record_decl)
7741 return nullptr;
7742 return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7743 cxx_record_decl);
7744 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007745
7746 case clang::Type::Typedef:
Pavel Labath769b21e2017-11-13 14:26:21 +00007747 return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type)
7748 ->getDecl()
7749 ->getUnderlyingType()
7750 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007751
7752 case clang::Type::Auto:
Pavel Labath769b21e2017-11-13 14:26:21 +00007753 return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type)
7754 ->getDeducedType()
7755 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007756
7757 case clang::Type::Elaborated:
Pavel Labath769b21e2017-11-13 14:26:21 +00007758 return GetAsTemplateSpecialization(
7759 llvm::cast<clang::ElaboratedType>(qual_type)
7760 ->getNamedType()
7761 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007762
7763 case clang::Type::Paren:
Pavel Labath769b21e2017-11-13 14:26:21 +00007764 return GetAsTemplateSpecialization(
7765 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007766
7767 default:
Pavel Labath769b21e2017-11-13 14:26:21 +00007768 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007769 }
Pavel Labath769b21e2017-11-13 14:26:21 +00007770}
7771
7772lldb::TemplateArgumentKind
7773ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
7774 size_t arg_idx) {
7775 const clang::ClassTemplateSpecializationDecl *template_decl =
7776 GetAsTemplateSpecialization(type);
7777 if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size())
7778 return eTemplateArgumentKindNull;
7779
7780 switch (template_decl->getTemplateArgs()[arg_idx].getKind()) {
7781 case clang::TemplateArgument::Null:
7782 return eTemplateArgumentKindNull;
7783
7784 case clang::TemplateArgument::NullPtr:
7785 return eTemplateArgumentKindNullPtr;
7786
7787 case clang::TemplateArgument::Type:
7788 return eTemplateArgumentKindType;
7789
7790 case clang::TemplateArgument::Declaration:
7791 return eTemplateArgumentKindDeclaration;
7792
7793 case clang::TemplateArgument::Integral:
7794 return eTemplateArgumentKindIntegral;
7795
7796 case clang::TemplateArgument::Template:
7797 return eTemplateArgumentKindTemplate;
7798
7799 case clang::TemplateArgument::TemplateExpansion:
7800 return eTemplateArgumentKindTemplateExpansion;
7801
7802 case clang::TemplateArgument::Expression:
7803 return eTemplateArgumentKindExpression;
7804
7805 case clang::TemplateArgument::Pack:
7806 return eTemplateArgumentKindPack;
7807 }
7808 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
7809}
7810
7811CompilerType
7812ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
7813 size_t idx) {
7814 const clang::ClassTemplateSpecializationDecl *template_decl =
7815 GetAsTemplateSpecialization(type);
7816 if (!template_decl || idx >= template_decl->getTemplateArgs().size())
7817 return CompilerType();
7818
7819 const clang::TemplateArgument &template_arg =
7820 template_decl->getTemplateArgs()[idx];
7821 if (template_arg.getKind() != clang::TemplateArgument::Type)
7822 return CompilerType();
7823
7824 return CompilerType(getASTContext(), template_arg.getAsType());
7825}
7826
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00007827Optional<CompilerType::IntegralTemplateArgument>
Pavel Labath769b21e2017-11-13 14:26:21 +00007828ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
7829 size_t idx) {
7830 const clang::ClassTemplateSpecializationDecl *template_decl =
7831 GetAsTemplateSpecialization(type);
7832 if (! template_decl || idx >= template_decl->getTemplateArgs().size())
Pavel Labathf59056f2017-11-30 10:16:54 +00007833 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007834
7835 const clang::TemplateArgument &template_arg =
7836 template_decl->getTemplateArgs()[idx];
7837 if (template_arg.getKind() != clang::TemplateArgument::Integral)
Pavel Labathf59056f2017-11-30 10:16:54 +00007838 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007839
Pavel Labathf59056f2017-11-30 10:16:54 +00007840 return {{template_arg.getAsIntegral(),
7841 CompilerType(getASTContext(), template_arg.getIntegralType())}};
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007842}
7843
Kate Stoneb9c1b512016-09-06 20:57:50 +00007844CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7845 if (type)
7846 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7847 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007848}
7849
Kate Stoneb9c1b512016-09-06 20:57:50 +00007850clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7851 const clang::EnumType *enutype =
7852 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7853 if (enutype)
7854 return enutype->getDecl();
Konrad Kleine248a1302019-05-23 11:14:47 +00007855 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007856}
7857
7858clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7859 const clang::RecordType *record_type =
7860 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7861 if (record_type)
7862 return record_type->getDecl();
7863 return nullptr;
7864}
7865
7866clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
Zachary Turner1639c6b2018-12-17 16:15:13 +00007867 return ClangUtil::GetAsTagDecl(type);
Greg Claytone6b36cd2015-12-08 01:02:08 +00007868}
7869
Aleksandr Urakov709426b2018-09-10 08:08:43 +00007870clang::TypedefNameDecl *
7871ClangASTContext::GetAsTypedefDecl(const CompilerType &type) {
7872 const clang::TypedefType *typedef_type =
7873 llvm::dyn_cast<clang::TypedefType>(ClangUtil::GetQualType(type));
7874 if (typedef_type)
7875 return typedef_type->getDecl();
7876 return nullptr;
7877}
7878
Greg Claytond8d4a572015-08-11 21:38:15 +00007879clang::CXXRecordDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007880ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7881 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
Greg Claytond8d4a572015-08-11 21:38:15 +00007882}
7883
7884clang::ObjCInterfaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007885ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7886 const clang::ObjCObjectType *objc_class_type =
7887 llvm::dyn_cast<clang::ObjCObjectType>(
7888 ClangUtil::GetCanonicalQualType(type));
7889 if (objc_class_type)
7890 return objc_class_type->getInterface();
7891 return nullptr;
7892}
7893
7894clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007895 const CompilerType &type, llvm::StringRef name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00007896 const CompilerType &field_clang_type, AccessType access,
7897 uint32_t bitfield_bit_size) {
7898 if (!type.IsValid() || !field_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00007899 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007900 ClangASTContext *ast =
7901 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7902 if (!ast)
7903 return nullptr;
7904 clang::ASTContext *clang_ast = ast->getASTContext();
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007905 clang::IdentifierInfo *ident = nullptr;
7906 if (!name.empty())
7907 ident = &clang_ast->Idents.get(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00007908
7909 clang::FieldDecl *field = nullptr;
7910
7911 clang::Expr *bit_width = nullptr;
7912 if (bitfield_bit_size != 0) {
7913 llvm::APInt bitfield_bit_size_apint(
7914 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7915 bit_width = new (*clang_ast)
7916 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7917 clang_ast->IntTy, clang::SourceLocation());
7918 }
7919
7920 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7921 if (record_decl) {
7922 field = clang::FieldDecl::Create(
7923 *clang_ast, record_decl, clang::SourceLocation(),
7924 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007925 ident, // Identifier
7926 ClangUtil::GetQualType(field_clang_type), // Field type
7927 nullptr, // TInfo *
7928 bit_width, // BitWidth
7929 false, // Mutable
7930 clang::ICIS_NoInit); // HasInit
Kate Stoneb9c1b512016-09-06 20:57:50 +00007931
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007932 if (name.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +00007933 // Determine whether this field corresponds to an anonymous struct or
7934 // union.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007935 if (const clang::TagType *TagT =
7936 field->getType()->getAs<clang::TagType>()) {
7937 if (clang::RecordDecl *Rec =
7938 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7939 if (!Rec->getDeclName()) {
7940 Rec->setAnonymousStructOrUnion(true);
7941 field->setImplicit();
7942 }
7943 }
7944 }
7945
7946 if (field) {
7947 field->setAccess(
7948 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7949
7950 record_decl->addDecl(field);
7951
7952#ifdef LLDB_CONFIGURATION_DEBUG
7953 VerifyDecl(field);
7954#endif
7955 }
7956 } else {
7957 clang::ObjCInterfaceDecl *class_interface_decl =
7958 ast->GetAsObjCInterfaceDecl(type);
7959
7960 if (class_interface_decl) {
7961 const bool is_synthesized = false;
7962
7963 field_clang_type.GetCompleteType();
7964
7965 field = clang::ObjCIvarDecl::Create(
7966 *clang_ast, class_interface_decl, clang::SourceLocation(),
7967 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007968 ident, // Identifier
7969 ClangUtil::GetQualType(field_clang_type), // Field type
7970 nullptr, // TypeSourceInfo *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007971 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7972 is_synthesized);
7973
7974 if (field) {
7975 class_interface_decl->addDecl(field);
7976
7977#ifdef LLDB_CONFIGURATION_DEBUG
7978 VerifyDecl(field);
7979#endif
7980 }
7981 }
7982 }
7983 return field;
Greg Claytond8d4a572015-08-11 21:38:15 +00007984}
7985
Kate Stoneb9c1b512016-09-06 20:57:50 +00007986void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7987 if (!type)
7988 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007989
Kate Stoneb9c1b512016-09-06 20:57:50 +00007990 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7991 if (!ast)
7992 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007993
Kate Stoneb9c1b512016-09-06 20:57:50 +00007994 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007995
Kate Stoneb9c1b512016-09-06 20:57:50 +00007996 if (!record_decl)
7997 return;
7998
7999 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
8000
8001 IndirectFieldVector indirect_fields;
8002 clang::RecordDecl::field_iterator field_pos;
8003 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
8004 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
8005 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
8006 last_field_pos = field_pos++) {
8007 if (field_pos->isAnonymousStructOrUnion()) {
8008 clang::QualType field_qual_type = field_pos->getType();
8009
8010 const clang::RecordType *field_record_type =
8011 field_qual_type->getAs<clang::RecordType>();
8012
8013 if (!field_record_type)
8014 continue;
8015
8016 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
8017
8018 if (!field_record_decl)
8019 continue;
8020
8021 for (clang::RecordDecl::decl_iterator
8022 di = field_record_decl->decls_begin(),
8023 de = field_record_decl->decls_end();
8024 di != de; ++di) {
8025 if (clang::FieldDecl *nested_field_decl =
8026 llvm::dyn_cast<clang::FieldDecl>(*di)) {
8027 clang::NamedDecl **chain =
8028 new (*ast->getASTContext()) clang::NamedDecl *[2];
8029 chain[0] = *field_pos;
8030 chain[1] = nested_field_decl;
8031 clang::IndirectFieldDecl *indirect_field =
8032 clang::IndirectFieldDecl::Create(
8033 *ast->getASTContext(), record_decl, clang::SourceLocation(),
8034 nested_field_decl->getIdentifier(),
8035 nested_field_decl->getType(), {chain, 2});
8036
8037 indirect_field->setImplicit();
8038
8039 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
8040 field_pos->getAccess(), nested_field_decl->getAccess()));
8041
8042 indirect_fields.push_back(indirect_field);
8043 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
8044 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
8045 size_t nested_chain_size =
8046 nested_indirect_field_decl->getChainingSize();
8047 clang::NamedDecl **chain = new (*ast->getASTContext())
8048 clang::NamedDecl *[nested_chain_size + 1];
8049 chain[0] = *field_pos;
8050
8051 int chain_index = 1;
8052 for (clang::IndirectFieldDecl::chain_iterator
8053 nci = nested_indirect_field_decl->chain_begin(),
8054 nce = nested_indirect_field_decl->chain_end();
8055 nci < nce; ++nci) {
8056 chain[chain_index] = *nci;
8057 chain_index++;
8058 }
8059
8060 clang::IndirectFieldDecl *indirect_field =
8061 clang::IndirectFieldDecl::Create(
8062 *ast->getASTContext(), record_decl, clang::SourceLocation(),
8063 nested_indirect_field_decl->getIdentifier(),
8064 nested_indirect_field_decl->getType(),
8065 {chain, nested_chain_size + 1});
8066
8067 indirect_field->setImplicit();
8068
8069 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
8070 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
8071
8072 indirect_fields.push_back(indirect_field);
Greg Claytond8d4a572015-08-11 21:38:15 +00008073 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008074 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008075 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008076 }
8077
Adrian Prantl05097242018-04-30 16:49:04 +00008078 // Check the last field to see if it has an incomplete array type as its last
8079 // member and if it does, the tell the record decl about it
Kate Stoneb9c1b512016-09-06 20:57:50 +00008080 if (last_field_pos != field_end_pos) {
8081 if (last_field_pos->getType()->isIncompleteArrayType())
8082 record_decl->hasFlexibleArrayMember();
8083 }
8084
8085 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
8086 ife = indirect_fields.end();
8087 ifi < ife; ++ifi) {
8088 record_decl->addDecl(*ifi);
8089 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008090}
8091
Kate Stoneb9c1b512016-09-06 20:57:50 +00008092void ClangASTContext::SetIsPacked(const CompilerType &type) {
8093 if (type) {
8094 ClangASTContext *ast =
8095 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8096 if (ast) {
8097 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
8098
8099 if (!record_decl)
Greg Claytonf73034f2015-09-08 18:15:05 +00008100 return;
8101
Kate Stoneb9c1b512016-09-06 20:57:50 +00008102 record_decl->addAttr(
8103 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
Greg Claytond8d4a572015-08-11 21:38:15 +00008104 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008105 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008106}
8107
Kate Stoneb9c1b512016-09-06 20:57:50 +00008108clang::VarDecl *ClangASTContext::AddVariableToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008109 const CompilerType &type, llvm::StringRef name,
8110 const CompilerType &var_type, AccessType access) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00008111 if (!type.IsValid() || !var_type.IsValid())
8112 return nullptr;
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008113
Kate Stoneb9c1b512016-09-06 20:57:50 +00008114 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8115 if (!ast)
8116 return nullptr;
8117
8118 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008119 if (!record_decl)
8120 return nullptr;
8121
8122 clang::VarDecl *var_decl = nullptr;
8123 clang::IdentifierInfo *ident = nullptr;
8124 if (!name.empty())
8125 ident = &ast->getASTContext()->Idents.get(name);
8126
8127 var_decl = clang::VarDecl::Create(
8128 *ast->getASTContext(), // ASTContext &
8129 record_decl, // DeclContext *
8130 clang::SourceLocation(), // clang::SourceLocation StartLoc
8131 clang::SourceLocation(), // clang::SourceLocation IdLoc
8132 ident, // clang::IdentifierInfo *
8133 ClangUtil::GetQualType(var_type), // Variable clang::QualType
8134 nullptr, // TypeSourceInfo *
8135 clang::SC_Static); // StorageClass
8136 if (!var_decl)
8137 return nullptr;
8138
8139 var_decl->setAccess(
8140 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
8141 record_decl->addDecl(var_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008142
Greg Claytond8d4a572015-08-11 21:38:15 +00008143#ifdef LLDB_CONFIGURATION_DEBUG
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008144 VerifyDecl(var_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008145#endif
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008146
Kate Stoneb9c1b512016-09-06 20:57:50 +00008147 return var_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008148}
8149
Kate Stoneb9c1b512016-09-06 20:57:50 +00008150clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
Davide Italiano675767a2018-03-27 19:40:50 +00008151 lldb::opaque_compiler_type_t type, const char *name, const char *mangled_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008152 const CompilerType &method_clang_type, lldb::AccessType access,
8153 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
8154 bool is_attr_used, bool is_artificial) {
8155 if (!type || !method_clang_type.IsValid() || name == nullptr ||
8156 name[0] == '\0')
8157 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008158
Kate Stoneb9c1b512016-09-06 20:57:50 +00008159 clang::QualType record_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008160
Kate Stoneb9c1b512016-09-06 20:57:50 +00008161 clang::CXXRecordDecl *cxx_record_decl =
8162 record_qual_type->getAsCXXRecordDecl();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008163
Kate Stoneb9c1b512016-09-06 20:57:50 +00008164 if (cxx_record_decl == nullptr)
8165 return nullptr;
8166
8167 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8168
8169 clang::CXXMethodDecl *cxx_method_decl = nullptr;
8170
8171 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
8172
8173 const clang::FunctionType *function_type =
8174 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
8175
8176 if (function_type == nullptr)
8177 return nullptr;
8178
8179 const clang::FunctionProtoType *method_function_prototype(
8180 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
8181
8182 if (!method_function_prototype)
8183 return nullptr;
8184
8185 unsigned int num_params = method_function_prototype->getNumParams();
8186
8187 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
8188 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
8189
8190 if (is_artificial)
8191 return nullptr; // skip everything artificial
8192
Richard Smith36851a62019-05-09 04:40:57 +00008193 const clang::ExplicitSpecifier explicit_spec(
8194 nullptr /*expr*/, is_explicit
8195 ? clang::ExplicitSpecKind::ResolvedTrue
8196 : clang::ExplicitSpecKind::ResolvedFalse);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008197 if (name[0] == '~') {
8198 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
8199 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8200 clang::DeclarationNameInfo(
8201 getASTContext()->DeclarationNames.getCXXDestructorName(
8202 getASTContext()->getCanonicalType(record_qual_type)),
8203 clang::SourceLocation()),
8204 method_qual_type, nullptr, is_inline, is_artificial);
8205 cxx_method_decl = cxx_dtor_decl;
8206 } else if (decl_name == cxx_record_decl->getDeclName()) {
8207 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
8208 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8209 clang::DeclarationNameInfo(
8210 getASTContext()->DeclarationNames.getCXXConstructorName(
8211 getASTContext()->getCanonicalType(record_qual_type)),
8212 clang::SourceLocation()),
8213 method_qual_type,
8214 nullptr, // TypeSourceInfo *
Gauthier Harnisch796ed032019-06-14 08:56:20 +00008215 explicit_spec, is_inline, is_artificial, CSK_unspecified);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008216 cxx_method_decl = cxx_ctor_decl;
8217 } else {
8218 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
8219 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
8220
8221 if (IsOperator(name, op_kind)) {
8222 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
Adrian Prantl05097242018-04-30 16:49:04 +00008223 // Check the number of operator parameters. Sometimes we have seen bad
8224 // DWARF that doesn't correctly describe operators and if we try to
8225 // create a method and add it to the class, clang will assert and
8226 // crash, so we need to make sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00008227 const bool is_method = true;
8228 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
8229 is_method, op_kind, num_params))
8230 return nullptr;
8231 cxx_method_decl = clang::CXXMethodDecl::Create(
8232 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8233 clang::DeclarationNameInfo(
8234 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
8235 clang::SourceLocation()),
8236 method_qual_type,
8237 nullptr, // TypeSourceInfo *
Gauthier Harnisch796ed032019-06-14 08:56:20 +00008238 SC, is_inline, CSK_unspecified, clang::SourceLocation());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008239 } else if (num_params == 0) {
8240 // Conversion operators don't take params...
8241 cxx_method_decl = clang::CXXConversionDecl::Create(
8242 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8243 clang::DeclarationNameInfo(
8244 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
8245 getASTContext()->getCanonicalType(
8246 function_type->getReturnType())),
8247 clang::SourceLocation()),
8248 method_qual_type,
8249 nullptr, // TypeSourceInfo *
Gauthier Harnisch796ed032019-06-14 08:56:20 +00008250 is_inline, explicit_spec, CSK_unspecified,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008251 clang::SourceLocation());
8252 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008253 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008254
8255 if (cxx_method_decl == nullptr) {
8256 cxx_method_decl = clang::CXXMethodDecl::Create(
8257 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8258 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8259 method_qual_type,
8260 nullptr, // TypeSourceInfo *
Gauthier Harnisch796ed032019-06-14 08:56:20 +00008261 SC, is_inline, CSK_unspecified, clang::SourceLocation());
Greg Claytond8d4a572015-08-11 21:38:15 +00008262 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008263 }
8264
8265 clang::AccessSpecifier access_specifier =
8266 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8267
8268 cxx_method_decl->setAccess(access_specifier);
8269 cxx_method_decl->setVirtualAsWritten(is_virtual);
8270
8271 if (is_attr_used)
8272 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8273
Konrad Kleine248a1302019-05-23 11:14:47 +00008274 if (mangled_name != nullptr) {
Davide Italiano675767a2018-03-27 19:40:50 +00008275 cxx_method_decl->addAttr(
8276 clang::AsmLabelAttr::CreateImplicit(*getASTContext(), mangled_name));
8277 }
8278
Kate Stoneb9c1b512016-09-06 20:57:50 +00008279 // Populate the method decl with parameter decls
8280
8281 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8282
8283 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8284 params.push_back(clang::ParmVarDecl::Create(
8285 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8286 clang::SourceLocation(),
8287 nullptr, // anonymous
8288 method_function_prototype->getParamType(param_index), nullptr,
8289 clang::SC_None, nullptr));
8290 }
8291
8292 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8293
8294 cxx_record_decl->addDecl(cxx_method_decl);
8295
8296 // Sometimes the debug info will mention a constructor (default/copy/move),
8297 // destructor, or assignment operator (copy/move) but there won't be any
8298 // version of this in the code. So we check if the function was artificially
8299 // generated and if it is trivial and this lets the compiler/backend know
8300 // that it can inline the IR for these when it needs to and we can avoid a
8301 // "missing function" error when running expressions.
8302
8303 if (is_artificial) {
8304 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8305 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8306 (cxx_ctor_decl->isCopyConstructor() &&
8307 cxx_record_decl->hasTrivialCopyConstructor()) ||
8308 (cxx_ctor_decl->isMoveConstructor() &&
8309 cxx_record_decl->hasTrivialMoveConstructor()))) {
8310 cxx_ctor_decl->setDefaulted();
8311 cxx_ctor_decl->setTrivial(true);
8312 } else if (cxx_dtor_decl) {
8313 if (cxx_record_decl->hasTrivialDestructor()) {
8314 cxx_dtor_decl->setDefaulted();
8315 cxx_dtor_decl->setTrivial(true);
8316 }
8317 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8318 cxx_record_decl->hasTrivialCopyAssignment()) ||
8319 (cxx_method_decl->isMoveAssignmentOperator() &&
8320 cxx_record_decl->hasTrivialMoveAssignment())) {
8321 cxx_method_decl->setDefaulted();
8322 cxx_method_decl->setTrivial(true);
Greg Claytond8d4a572015-08-11 21:38:15 +00008323 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008324 }
8325
Greg Claytond8d4a572015-08-11 21:38:15 +00008326#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008327 VerifyDecl(cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008328#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008329
Kate Stoneb9c1b512016-09-06 20:57:50 +00008330 // printf ("decl->isPolymorphic() = %i\n",
8331 // cxx_record_decl->isPolymorphic());
8332 // printf ("decl->isAggregate() = %i\n",
8333 // cxx_record_decl->isAggregate());
8334 // printf ("decl->isPOD() = %i\n",
8335 // cxx_record_decl->isPOD());
8336 // printf ("decl->isEmpty() = %i\n",
8337 // cxx_record_decl->isEmpty());
8338 // printf ("decl->isAbstract() = %i\n",
8339 // cxx_record_decl->isAbstract());
8340 // printf ("decl->hasTrivialConstructor() = %i\n",
8341 // cxx_record_decl->hasTrivialConstructor());
8342 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8343 // cxx_record_decl->hasTrivialCopyConstructor());
8344 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8345 // cxx_record_decl->hasTrivialCopyAssignment());
8346 // printf ("decl->hasTrivialDestructor() = %i\n",
8347 // cxx_record_decl->hasTrivialDestructor());
8348 return cxx_method_decl;
8349}
Greg Claytond8d4a572015-08-11 21:38:15 +00008350
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00008351void ClangASTContext::AddMethodOverridesForCXXRecordType(
8352 lldb::opaque_compiler_type_t type) {
8353 if (auto *record = GetAsCXXRecordDecl(type))
8354 for (auto *method : record->methods())
8355 addOverridesForMethod(method);
8356}
8357
Greg Claytond8d4a572015-08-11 21:38:15 +00008358#pragma mark C++ Base Classes
8359
Zachary Turner970f38e2018-10-25 20:44:56 +00008360std::unique_ptr<clang::CXXBaseSpecifier>
Kate Stoneb9c1b512016-09-06 20:57:50 +00008361ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8362 AccessType access, bool is_virtual,
8363 bool base_of_class) {
Zachary Turner970f38e2018-10-25 20:44:56 +00008364 if (!type)
8365 return nullptr;
8366
8367 return llvm::make_unique<clang::CXXBaseSpecifier>(
8368 clang::SourceRange(), is_virtual, base_of_class,
8369 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8370 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8371 clang::SourceLocation());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008372}
8373
Zachary Turner970f38e2018-10-25 20:44:56 +00008374bool ClangASTContext::TransferBaseClasses(
Kate Stoneb9c1b512016-09-06 20:57:50 +00008375 lldb::opaque_compiler_type_t type,
Zachary Turner970f38e2018-10-25 20:44:56 +00008376 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases) {
8377 if (!type)
8378 return false;
8379 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8380 if (!cxx_record_decl)
8381 return false;
8382 std::vector<clang::CXXBaseSpecifier *> raw_bases;
8383 raw_bases.reserve(bases.size());
8384
8385 // Clang will make a copy of them, so it's ok that we pass pointers that we're
8386 // about to destroy.
8387 for (auto &b : bases)
8388 raw_bases.push_back(b.get());
8389 cxx_record_decl->setBases(raw_bases.data(), raw_bases.size());
8390 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008391}
8392
8393bool ClangASTContext::SetObjCSuperClass(
8394 const CompilerType &type, const CompilerType &superclass_clang_type) {
8395 ClangASTContext *ast =
8396 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8397 if (!ast)
8398 return false;
8399 clang::ASTContext *clang_ast = ast->getASTContext();
8400
8401 if (type && superclass_clang_type.IsValid() &&
8402 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8403 clang::ObjCInterfaceDecl *class_interface_decl =
8404 GetAsObjCInterfaceDecl(type);
8405 clang::ObjCInterfaceDecl *super_interface_decl =
8406 GetAsObjCInterfaceDecl(superclass_clang_type);
8407 if (class_interface_decl && super_interface_decl) {
8408 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8409 clang_ast->getObjCInterfaceType(super_interface_decl)));
8410 return true;
8411 }
8412 }
8413 return false;
8414}
8415
8416bool ClangASTContext::AddObjCClassProperty(
8417 const CompilerType &type, const char *property_name,
8418 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8419 const char *property_setter_name, const char *property_getter_name,
8420 uint32_t property_attributes, ClangASTMetadata *metadata) {
8421 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8422 property_name[0] == '\0')
8423 return false;
8424 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8425 if (!ast)
8426 return false;
8427 clang::ASTContext *clang_ast = ast->getASTContext();
8428
8429 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8430
8431 if (class_interface_decl) {
8432 CompilerType property_clang_type_to_access;
8433
8434 if (property_clang_type.IsValid())
8435 property_clang_type_to_access = property_clang_type;
8436 else if (ivar_decl)
8437 property_clang_type_to_access =
8438 CompilerType(clang_ast, ivar_decl->getType());
8439
8440 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8441 clang::TypeSourceInfo *prop_type_source;
8442 if (ivar_decl)
8443 prop_type_source =
8444 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8445 else
8446 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8447 ClangUtil::GetQualType(property_clang_type));
8448
8449 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8450 *clang_ast, class_interface_decl,
8451 clang::SourceLocation(), // Source Location
8452 &clang_ast->Idents.get(property_name),
8453 clang::SourceLocation(), // Source Location for AT
8454 clang::SourceLocation(), // Source location for (
8455 ivar_decl ? ivar_decl->getType()
8456 : ClangUtil::GetQualType(property_clang_type),
8457 prop_type_source);
8458
8459 if (property_decl) {
8460 if (metadata)
8461 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8462
8463 class_interface_decl->addDecl(property_decl);
8464
8465 clang::Selector setter_sel, getter_sel;
8466
8467 if (property_setter_name != nullptr) {
8468 std::string property_setter_no_colon(
8469 property_setter_name, strlen(property_setter_name) - 1);
8470 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008471 &clang_ast->Idents.get(property_setter_no_colon);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008472 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8473 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8474 std::string setter_sel_string("set");
8475 setter_sel_string.push_back(::toupper(property_name[0]));
8476 setter_sel_string.append(&property_name[1]);
8477 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008478 &clang_ast->Idents.get(setter_sel_string);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008479 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8480 }
8481 property_decl->setSetterName(setter_sel);
8482 property_decl->setPropertyAttributes(
8483 clang::ObjCPropertyDecl::OBJC_PR_setter);
8484
8485 if (property_getter_name != nullptr) {
8486 clang::IdentifierInfo *getter_ident =
8487 &clang_ast->Idents.get(property_getter_name);
8488 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8489 } else {
8490 clang::IdentifierInfo *getter_ident =
8491 &clang_ast->Idents.get(property_name);
8492 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8493 }
8494 property_decl->setGetterName(getter_sel);
8495 property_decl->setPropertyAttributes(
8496 clang::ObjCPropertyDecl::OBJC_PR_getter);
8497
8498 if (ivar_decl)
8499 property_decl->setPropertyIvarDecl(ivar_decl);
8500
8501 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8502 property_decl->setPropertyAttributes(
8503 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8504 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8505 property_decl->setPropertyAttributes(
8506 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8507 if (property_attributes & DW_APPLE_PROPERTY_assign)
8508 property_decl->setPropertyAttributes(
8509 clang::ObjCPropertyDecl::OBJC_PR_assign);
8510 if (property_attributes & DW_APPLE_PROPERTY_retain)
8511 property_decl->setPropertyAttributes(
8512 clang::ObjCPropertyDecl::OBJC_PR_retain);
8513 if (property_attributes & DW_APPLE_PROPERTY_copy)
8514 property_decl->setPropertyAttributes(
8515 clang::ObjCPropertyDecl::OBJC_PR_copy);
8516 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8517 property_decl->setPropertyAttributes(
8518 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8519 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8520 property_decl->setPropertyAttributes(
8521 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8522 if (property_attributes &
8523 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8524 property_decl->setPropertyAttributes(
8525 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8526 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8527 property_decl->setPropertyAttributes(
8528 clang::ObjCPropertyDecl::OBJC_PR_class);
8529
8530 const bool isInstance =
8531 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8532
8533 if (!getter_sel.isNull() &&
8534 !(isInstance
8535 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8536 : class_interface_decl->lookupClassMethod(getter_sel))) {
8537 const bool isVariadic = false;
8538 const bool isSynthesized = false;
8539 const bool isImplicitlyDeclared = true;
8540 const bool isDefined = false;
8541 const clang::ObjCMethodDecl::ImplementationControl impControl =
8542 clang::ObjCMethodDecl::None;
8543 const bool HasRelatedResultType = false;
8544
8545 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8546 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8547 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8548 nullptr, class_interface_decl, isInstance, isVariadic,
8549 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8550 HasRelatedResultType);
8551
8552 if (getter && metadata)
8553 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8554
8555 if (getter) {
8556 getter->setMethodParams(*clang_ast,
8557 llvm::ArrayRef<clang::ParmVarDecl *>(),
8558 llvm::ArrayRef<clang::SourceLocation>());
8559
8560 class_interface_decl->addDecl(getter);
8561 }
8562 }
8563
8564 if (!setter_sel.isNull() &&
8565 !(isInstance
8566 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8567 : class_interface_decl->lookupClassMethod(setter_sel))) {
8568 clang::QualType result_type = clang_ast->VoidTy;
8569 const bool isVariadic = false;
8570 const bool isSynthesized = false;
8571 const bool isImplicitlyDeclared = true;
8572 const bool isDefined = false;
8573 const clang::ObjCMethodDecl::ImplementationControl impControl =
8574 clang::ObjCMethodDecl::None;
8575 const bool HasRelatedResultType = false;
8576
8577 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8578 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8579 setter_sel, result_type, nullptr, class_interface_decl,
8580 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8581 isDefined, impControl, HasRelatedResultType);
8582
8583 if (setter && metadata)
8584 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8585
8586 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8587
8588 params.push_back(clang::ParmVarDecl::Create(
8589 *clang_ast, setter, clang::SourceLocation(),
8590 clang::SourceLocation(),
8591 nullptr, // anonymous
8592 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8593 clang::SC_Auto, nullptr));
8594
8595 if (setter) {
8596 setter->setMethodParams(
8597 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8598 llvm::ArrayRef<clang::SourceLocation>());
8599
8600 class_interface_decl->addDecl(setter);
8601 }
8602 }
8603
8604 return true;
8605 }
8606 }
8607 }
8608 return false;
8609}
8610
8611bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8612 bool check_superclass) {
8613 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8614 if (class_interface_decl)
8615 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8616 return false;
8617}
8618
8619clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8620 const CompilerType &type,
8621 const char *name, // the full symbol name as seen in the symbol table
8622 // (lldb::opaque_compiler_type_t type, "-[NString
8623 // stringWithCString:]")
8624 const CompilerType &method_clang_type, lldb::AccessType access,
8625 bool is_artificial, bool is_variadic) {
8626 if (!type || !method_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008627 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008628
Kate Stoneb9c1b512016-09-06 20:57:50 +00008629 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8630
8631 if (class_interface_decl == nullptr)
8632 return nullptr;
8633 ClangASTContext *lldb_ast =
8634 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8635 if (lldb_ast == nullptr)
8636 return nullptr;
8637 clang::ASTContext *ast = lldb_ast->getASTContext();
8638
8639 const char *selector_start = ::strchr(name, ' ');
8640 if (selector_start == nullptr)
8641 return nullptr;
8642
8643 selector_start++;
8644 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8645
8646 size_t len = 0;
8647 const char *start;
8648 // printf ("name = '%s'\n", name);
8649
8650 unsigned num_selectors_with_args = 0;
8651 for (start = selector_start; start && *start != '\0' && *start != ']';
8652 start += len) {
8653 len = ::strcspn(start, ":]");
8654 bool has_arg = (start[len] == ':');
8655 if (has_arg)
8656 ++num_selectors_with_args;
8657 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8658 if (has_arg)
8659 len += 1;
8660 }
8661
8662 if (selector_idents.size() == 0)
8663 return nullptr;
8664
8665 clang::Selector method_selector = ast->Selectors.getSelector(
8666 num_selectors_with_args ? selector_idents.size() : 0,
8667 selector_idents.data());
8668
8669 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8670
8671 // Populate the method decl with parameter decls
8672 const clang::Type *method_type(method_qual_type.getTypePtr());
8673
8674 if (method_type == nullptr)
8675 return nullptr;
8676
8677 const clang::FunctionProtoType *method_function_prototype(
8678 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8679
8680 if (!method_function_prototype)
8681 return nullptr;
8682
8683 bool is_synthesized = false;
8684 bool is_defined = false;
8685 clang::ObjCMethodDecl::ImplementationControl imp_control =
8686 clang::ObjCMethodDecl::None;
8687
8688 const unsigned num_args = method_function_prototype->getNumParams();
8689
8690 if (num_args != num_selectors_with_args)
8691 return nullptr; // some debug information is corrupt. We are not going to
8692 // deal with it.
8693
8694 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8695 *ast,
8696 clang::SourceLocation(), // beginLoc,
8697 clang::SourceLocation(), // endLoc,
8698 method_selector, method_function_prototype->getReturnType(),
8699 nullptr, // TypeSourceInfo *ResultTInfo,
8700 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8701 ClangUtil::GetQualType(type)),
8702 name[0] == '-', is_variadic, is_synthesized,
8703 true, // is_implicitly_declared; we force this to true because we don't
8704 // have source locations
8705 is_defined, imp_control, false /*has_related_result_type*/);
8706
8707 if (objc_method_decl == nullptr)
8708 return nullptr;
8709
8710 if (num_args > 0) {
8711 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8712
8713 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8714 params.push_back(clang::ParmVarDecl::Create(
8715 *ast, objc_method_decl, clang::SourceLocation(),
8716 clang::SourceLocation(),
8717 nullptr, // anonymous
8718 method_function_prototype->getParamType(param_index), nullptr,
8719 clang::SC_Auto, nullptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00008720 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008721
Kate Stoneb9c1b512016-09-06 20:57:50 +00008722 objc_method_decl->setMethodParams(
8723 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8724 llvm::ArrayRef<clang::SourceLocation>());
8725 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008726
Kate Stoneb9c1b512016-09-06 20:57:50 +00008727 class_interface_decl->addDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008728
Greg Claytond8d4a572015-08-11 21:38:15 +00008729#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008730 VerifyDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008731#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00008732
8733 return objc_method_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008734}
8735
Kate Stoneb9c1b512016-09-06 20:57:50 +00008736bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8737 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00008738 return false;
Greg Claytone6b36cd2015-12-08 01:02:08 +00008739
Kate Stoneb9c1b512016-09-06 20:57:50 +00008740 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008741
Kate Stoneb9c1b512016-09-06 20:57:50 +00008742 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8743 switch (type_class) {
8744 case clang::Type::Record: {
8745 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8746 if (cxx_record_decl)
8747 return cxx_record_decl->hasExternalLexicalStorage() ||
8748 cxx_record_decl->hasExternalVisibleStorage();
8749 } break;
Enrico Granata36f51e42015-12-18 22:41:25 +00008750
Kate Stoneb9c1b512016-09-06 20:57:50 +00008751 case clang::Type::Enum: {
8752 clang::EnumDecl *enum_decl =
8753 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8754 if (enum_decl)
8755 return enum_decl->hasExternalLexicalStorage() ||
8756 enum_decl->hasExternalVisibleStorage();
8757 } break;
8758
8759 case clang::Type::ObjCObject:
8760 case clang::Type::ObjCInterface: {
8761 const clang::ObjCObjectType *objc_class_type =
8762 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8763 assert(objc_class_type);
8764 if (objc_class_type) {
8765 clang::ObjCInterfaceDecl *class_interface_decl =
8766 objc_class_type->getInterface();
8767
8768 if (class_interface_decl)
8769 return class_interface_decl->hasExternalLexicalStorage() ||
8770 class_interface_decl->hasExternalVisibleStorage();
Greg Claytond8d4a572015-08-11 21:38:15 +00008771 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008772 } break;
8773
8774 case clang::Type::Typedef:
8775 return GetHasExternalStorage(CompilerType(
8776 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8777 ->getDecl()
8778 ->getUnderlyingType()
8779 .getAsOpaquePtr()));
8780
8781 case clang::Type::Auto:
8782 return GetHasExternalStorage(CompilerType(
8783 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8784 ->getDeducedType()
8785 .getAsOpaquePtr()));
8786
8787 case clang::Type::Elaborated:
8788 return GetHasExternalStorage(CompilerType(
8789 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8790 ->getNamedType()
8791 .getAsOpaquePtr()));
8792
8793 case clang::Type::Paren:
8794 return GetHasExternalStorage(CompilerType(
8795 type.GetTypeSystem(),
8796 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8797
8798 default:
8799 break;
8800 }
8801 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008802}
8803
Kate Stoneb9c1b512016-09-06 20:57:50 +00008804bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8805 bool has_extern) {
8806 if (!type)
8807 return false;
8808
8809 clang::QualType qual_type(GetCanonicalQualType(type));
8810
8811 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8812 switch (type_class) {
8813 case clang::Type::Record: {
8814 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8815 if (cxx_record_decl) {
8816 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8817 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8818 return true;
8819 }
8820 } break;
8821
8822 case clang::Type::Enum: {
8823 clang::EnumDecl *enum_decl =
8824 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8825 if (enum_decl) {
8826 enum_decl->setHasExternalLexicalStorage(has_extern);
8827 enum_decl->setHasExternalVisibleStorage(has_extern);
8828 return true;
8829 }
8830 } break;
8831
8832 case clang::Type::ObjCObject:
8833 case clang::Type::ObjCInterface: {
8834 const clang::ObjCObjectType *objc_class_type =
8835 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8836 assert(objc_class_type);
8837 if (objc_class_type) {
8838 clang::ObjCInterfaceDecl *class_interface_decl =
8839 objc_class_type->getInterface();
8840
8841 if (class_interface_decl) {
8842 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8843 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8844 return true;
8845 }
8846 }
8847 } break;
8848
8849 case clang::Type::Typedef:
8850 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8851 ->getDecl()
8852 ->getUnderlyingType()
8853 .getAsOpaquePtr(),
8854 has_extern);
8855
8856 case clang::Type::Auto:
8857 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8858 ->getDeducedType()
8859 .getAsOpaquePtr(),
8860 has_extern);
8861
8862 case clang::Type::Elaborated:
8863 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8864 ->getNamedType()
8865 .getAsOpaquePtr(),
8866 has_extern);
8867
8868 case clang::Type::Paren:
8869 return SetHasExternalStorage(
8870 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8871 has_extern);
8872
8873 default:
8874 break;
8875 }
8876 return false;
8877}
Greg Claytond8d4a572015-08-11 21:38:15 +00008878
8879#pragma mark TagDecl
8880
Kate Stoneb9c1b512016-09-06 20:57:50 +00008881bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8882 clang::QualType qual_type(ClangUtil::GetQualType(type));
8883 if (!qual_type.isNull()) {
8884 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8885 if (tag_type) {
8886 clang::TagDecl *tag_decl = tag_type->getDecl();
8887 if (tag_decl) {
8888 tag_decl->startDefinition();
8889 return true;
8890 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008891 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008892
8893 const clang::ObjCObjectType *object_type =
8894 qual_type->getAs<clang::ObjCObjectType>();
8895 if (object_type) {
8896 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8897 if (interface_decl) {
8898 interface_decl->startDefinition();
8899 return true;
8900 }
8901 }
8902 }
8903 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008904}
8905
Kate Stoneb9c1b512016-09-06 20:57:50 +00008906bool ClangASTContext::CompleteTagDeclarationDefinition(
8907 const CompilerType &type) {
8908 clang::QualType qual_type(ClangUtil::GetQualType(type));
8909 if (!qual_type.isNull()) {
8910 // Make sure we use the same methodology as
Adrian Prantl05097242018-04-30 16:49:04 +00008911 // ClangASTContext::StartTagDeclarationDefinition() as to how we start/end
8912 // the definition. Previously we were calling
Kate Stoneb9c1b512016-09-06 20:57:50 +00008913 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8914 if (tag_type) {
8915 clang::TagDecl *tag_decl = tag_type->getDecl();
8916 if (tag_decl) {
8917 clang::CXXRecordDecl *cxx_record_decl =
8918 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8919
8920 if (cxx_record_decl) {
8921 if (!cxx_record_decl->isCompleteDefinition())
8922 cxx_record_decl->completeDefinition();
8923 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8924 cxx_record_decl->setHasExternalLexicalStorage(false);
8925 cxx_record_decl->setHasExternalVisibleStorage(false);
8926 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008927 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008928 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008929 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008930
8931 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8932
8933 if (enutype) {
8934 clang::EnumDecl *enum_decl = enutype->getDecl();
8935
8936 if (enum_decl) {
8937 if (!enum_decl->isCompleteDefinition()) {
8938 ClangASTContext *lldb_ast =
8939 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8940 if (lldb_ast == nullptr)
8941 return false;
8942 clang::ASTContext *ast = lldb_ast->getASTContext();
8943
8944 /// TODO This really needs to be fixed.
8945
8946 QualType integer_type(enum_decl->getIntegerType());
8947 if (!integer_type.isNull()) {
8948 unsigned NumPositiveBits = 1;
8949 unsigned NumNegativeBits = 0;
8950
8951 clang::QualType promotion_qual_type;
8952 // If the enum integer type is less than an integer in bit width,
8953 // then we must promote it to an integer size.
8954 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8955 ast->getTypeSize(ast->IntTy)) {
8956 if (enum_decl->getIntegerType()->isSignedIntegerType())
8957 promotion_qual_type = ast->IntTy;
8958 else
8959 promotion_qual_type = ast->UnsignedIntTy;
8960 } else
8961 promotion_qual_type = enum_decl->getIntegerType();
8962
8963 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8964 promotion_qual_type, NumPositiveBits,
8965 NumNegativeBits);
8966 }
8967 }
8968 return true;
8969 }
8970 }
8971 }
8972 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008973}
8974
Aleksandr Urakov709426b2018-09-10 08:08:43 +00008975clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008976 const CompilerType &enum_type, const Declaration &decl, const char *name,
Zachary Turner1639c6b2018-12-17 16:15:13 +00008977 const llvm::APSInt &value) {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008978
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008979 if (!enum_type || ConstString(name).IsEmpty())
8980 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008981
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008982 lldbassert(enum_type.GetTypeSystem() == static_cast<TypeSystem *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +00008983
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008984 lldb::opaque_compiler_type_t enum_opaque_compiler_type =
8985 enum_type.GetOpaqueQualType();
8986
8987 if (!enum_opaque_compiler_type)
8988 return nullptr;
8989
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008990 clang::QualType enum_qual_type(
8991 GetCanonicalQualType(enum_opaque_compiler_type));
8992
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008993 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8994
8995 if (!clang_type)
8996 return nullptr;
8997
8998 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8999
9000 if (!enutype)
9001 return nullptr;
9002
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009003 clang::EnumConstantDecl *enumerator_decl = clang::EnumConstantDecl::Create(
9004 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
9005 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier
Zachary Turner1639c6b2018-12-17 16:15:13 +00009006 clang::QualType(enutype, 0), nullptr, value);
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009007
9008 if (!enumerator_decl)
9009 return nullptr;
9010
9011 enutype->getDecl()->addDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009012
9013#ifdef LLDB_CONFIGURATION_DEBUG
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009014 VerifyDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009015#endif
9016
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009017 return enumerator_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00009018}
9019
Zachary Turner1639c6b2018-12-17 16:15:13 +00009020clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
9021 const CompilerType &enum_type, const Declaration &decl, const char *name,
9022 int64_t enum_value, uint32_t enum_value_bit_size) {
9023 CompilerType underlying_type =
9024 GetEnumerationIntegerType(enum_type.GetOpaqueQualType());
9025 bool is_signed = false;
9026 underlying_type.IsIntegerType(is_signed);
9027
9028 llvm::APSInt value(enum_value_bit_size, is_signed);
9029 value = enum_value;
9030
9031 return AddEnumerationValueToEnumerationType(enum_type, decl, name, value);
9032}
9033
Greg Claytona1e5dc82015-08-11 22:53:00 +00009034CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00009035ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
9036 clang::QualType enum_qual_type(GetCanonicalQualType(type));
9037 const clang::Type *clang_type = enum_qual_type.getTypePtr();
9038 if (clang_type) {
9039 const clang::EnumType *enutype =
9040 llvm::dyn_cast<clang::EnumType>(clang_type);
9041 if (enutype) {
9042 clang::EnumDecl *enum_decl = enutype->getDecl();
9043 if (enum_decl)
9044 return CompilerType(getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00009045 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009046 }
9047 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00009048}
9049
Kate Stoneb9c1b512016-09-06 20:57:50 +00009050CompilerType
9051ClangASTContext::CreateMemberPointerType(const CompilerType &type,
9052 const CompilerType &pointee_type) {
9053 if (type && pointee_type.IsValid() &&
9054 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
9055 ClangASTContext *ast =
9056 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
9057 if (!ast)
9058 return CompilerType();
9059 return CompilerType(ast->getASTContext(),
9060 ast->getASTContext()->getMemberPointerType(
9061 ClangUtil::GetQualType(pointee_type),
9062 ClangUtil::GetQualType(type).getTypePtr()));
9063 }
9064 return CompilerType();
9065}
Greg Claytond8d4a572015-08-11 21:38:15 +00009066
9067size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00009068ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
9069 const char *s, uint8_t *dst,
9070 size_t dst_size) {
9071 if (type) {
9072 clang::QualType qual_type(GetCanonicalQualType(type));
9073 uint32_t count = 0;
9074 bool is_complex = false;
9075 if (IsFloatingPointType(type, count, is_complex)) {
9076 // TODO: handle complex and vector types
9077 if (count != 1)
9078 return false;
9079
9080 llvm::StringRef s_sref(s);
9081 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type),
9082 s_sref);
9083
9084 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type);
9085 const uint64_t byte_size = bit_size / 8;
9086 if (dst_size >= byte_size) {
9087 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(
9088 llvm::NextPowerOf2(byte_size) * 8);
Zachary Turner97206d52017-05-12 04:51:55 +00009089 lldb_private::Status get_data_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009090 if (scalar.GetAsMemoryData(dst, byte_size,
9091 lldb_private::endian::InlHostByteOrder(),
9092 get_data_error))
9093 return byte_size;
9094 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009095 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009096 }
9097 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00009098}
9099
Greg Claytond8d4a572015-08-11 21:38:15 +00009100// Dumping types
Greg Claytond8d4a572015-08-11 21:38:15 +00009101#define DEPTH_INCREMENT 2
9102
Adrian Prantl0c72a422019-03-07 20:20:02 +00009103#ifndef NDEBUG
9104LLVM_DUMP_METHOD void
9105ClangASTContext::dump(lldb::opaque_compiler_type_t type) const {
9106 if (!type)
9107 return;
9108 clang::QualType qual_type(GetQualType(type));
9109 qual_type.dump();
9110}
9111#endif
9112
Zachary Turner49110232018-11-05 17:40:28 +00009113void ClangASTContext::Dump(Stream &s) {
Zachary Turner115209e2018-11-05 19:25:39 +00009114 Decl *tu = Decl::castFromDeclContext(GetTranslationUnitDecl());
Zachary Turner49110232018-11-05 17:40:28 +00009115 tu->dump(s.AsRawOstream());
9116}
9117
Kate Stoneb9c1b512016-09-06 20:57:50 +00009118void ClangASTContext::DumpValue(
9119 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
Zachary Turner29cb8682017-03-03 20:57:05 +00009120 lldb::Format format, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009121 lldb::offset_t data_byte_offset, size_t data_byte_size,
9122 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
9123 bool show_summary, bool verbose, uint32_t depth) {
9124 if (!type)
9125 return;
9126
9127 clang::QualType qual_type(GetQualType(type));
9128 switch (qual_type->getTypeClass()) {
9129 case clang::Type::Record:
9130 if (GetCompleteType(type)) {
9131 const clang::RecordType *record_type =
9132 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9133 const clang::RecordDecl *record_decl = record_type->getDecl();
9134 assert(record_decl);
9135 uint32_t field_bit_offset = 0;
9136 uint32_t field_byte_offset = 0;
9137 const clang::ASTRecordLayout &record_layout =
9138 getASTContext()->getASTRecordLayout(record_decl);
9139 uint32_t child_idx = 0;
9140
9141 const clang::CXXRecordDecl *cxx_record_decl =
9142 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9143 if (cxx_record_decl) {
9144 // We might have base classes to print out first
9145 clang::CXXRecordDecl::base_class_const_iterator base_class,
9146 base_class_end;
9147 for (base_class = cxx_record_decl->bases_begin(),
9148 base_class_end = cxx_record_decl->bases_end();
9149 base_class != base_class_end; ++base_class) {
9150 const clang::CXXRecordDecl *base_class_decl =
9151 llvm::cast<clang::CXXRecordDecl>(
9152 base_class->getType()->getAs<clang::RecordType>()->getDecl());
9153
9154 // Skip empty base classes
Jonas Devliegherea6682a42018-12-15 00:15:33 +00009155 if (!verbose && !ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00009156 continue;
9157
9158 if (base_class->isVirtual())
9159 field_bit_offset =
9160 record_layout.getVBaseClassOffset(base_class_decl)
9161 .getQuantity() *
9162 8;
9163 else
9164 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
9165 .getQuantity() *
9166 8;
9167 field_byte_offset = field_bit_offset / 8;
9168 assert(field_bit_offset % 8 == 0);
9169 if (child_idx == 0)
9170 s->PutChar('{');
9171 else
9172 s->PutChar(',');
9173
9174 clang::QualType base_class_qual_type = base_class->getType();
9175 std::string base_class_type_name(base_class_qual_type.getAsString());
9176
9177 // Indent and print the base class type name
Zachary Turner827d5d72016-12-16 04:27:00 +00009178 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
9179 base_class_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009180
9181 clang::TypeInfo base_class_type_info =
9182 getASTContext()->getTypeInfo(base_class_qual_type);
9183
9184 // Dump the value of the member
9185 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
9186 base_clang_type.DumpValue(
9187 exe_ctx,
9188 s, // Stream to dump to
9189 base_clang_type
9190 .GetFormat(), // The format with which to display the member
9191 data, // Data buffer containing all bytes for this type
9192 data_byte_offset + field_byte_offset, // Offset into "data" where
9193 // to grab value from
9194 base_class_type_info.Width / 8, // Size of this type in bytes
9195 0, // Bitfield bit size
9196 0, // Bitfield bit offset
9197 show_types, // Boolean indicating if we should show the variable
9198 // types
9199 show_summary, // Boolean indicating if we should show a summary
9200 // for the current type
9201 verbose, // Verbose output?
9202 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9203 // children
9204
9205 ++child_idx;
9206 }
9207 }
9208 uint32_t field_idx = 0;
9209 clang::RecordDecl::field_iterator field, field_end;
9210 for (field = record_decl->field_begin(),
9211 field_end = record_decl->field_end();
9212 field != field_end; ++field, ++field_idx, ++child_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009213 // Print the starting squiggly bracket (if this is the first member) or
9214 // comma (for member 2 and beyond) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009215 if (child_idx == 0)
9216 s->PutChar('{');
9217 else
9218 s->PutChar(',');
9219
9220 // Indent
9221 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
9222
9223 clang::QualType field_type = field->getType();
9224 // Print the member type if requested
Adrian Prantl05097242018-04-30 16:49:04 +00009225 // Figure out the type byte size (field_type_info.first) and alignment
9226 // (field_type_info.second) from the AST context.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009227 clang::TypeInfo field_type_info =
9228 getASTContext()->getTypeInfo(field_type);
9229 assert(field_idx < record_layout.getFieldCount());
9230 // Figure out the field offset within the current struct/union/class
9231 // type
9232 field_bit_offset = record_layout.getFieldOffset(field_idx);
9233 field_byte_offset = field_bit_offset / 8;
9234 uint32_t field_bitfield_bit_size = 0;
9235 uint32_t field_bitfield_bit_offset = 0;
9236 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
9237 field_bitfield_bit_size))
9238 field_bitfield_bit_offset = field_bit_offset % 8;
9239
9240 if (show_types) {
9241 std::string field_type_name(field_type.getAsString());
9242 if (field_bitfield_bit_size > 0)
9243 s->Printf("(%s:%u) ", field_type_name.c_str(),
9244 field_bitfield_bit_size);
9245 else
9246 s->Printf("(%s) ", field_type_name.c_str());
9247 }
9248 // Print the member name and equal sign
9249 s->Printf("%s = ", field->getNameAsString().c_str());
9250
9251 // Dump the value of the member
9252 CompilerType field_clang_type(getASTContext(), field_type);
9253 field_clang_type.DumpValue(
9254 exe_ctx,
9255 s, // Stream to dump to
9256 field_clang_type
9257 .GetFormat(), // The format with which to display the member
9258 data, // Data buffer containing all bytes for this type
9259 data_byte_offset + field_byte_offset, // Offset into "data" where to
9260 // grab value from
9261 field_type_info.Width / 8, // Size of this type in bytes
9262 field_bitfield_bit_size, // Bitfield bit size
9263 field_bitfield_bit_offset, // Bitfield bit offset
9264 show_types, // Boolean indicating if we should show the variable
9265 // types
9266 show_summary, // Boolean indicating if we should show a summary for
9267 // the current type
9268 verbose, // Verbose output?
9269 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9270 // children
9271 }
9272
9273 // Indent the trailing squiggly bracket
9274 if (child_idx > 0)
9275 s->Printf("\n%*s}", depth, "");
9276 }
9277 return;
9278
9279 case clang::Type::Enum:
9280 if (GetCompleteType(type)) {
9281 const clang::EnumType *enutype =
9282 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9283 const clang::EnumDecl *enum_decl = enutype->getDecl();
9284 assert(enum_decl);
9285 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9286 lldb::offset_t offset = data_byte_offset;
9287 const int64_t enum_value = data.GetMaxU64Bitfield(
9288 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
9289 for (enum_pos = enum_decl->enumerator_begin(),
9290 enum_end_pos = enum_decl->enumerator_end();
9291 enum_pos != enum_end_pos; ++enum_pos) {
9292 if (enum_pos->getInitVal() == enum_value) {
9293 s->Printf("%s", enum_pos->getNameAsString().c_str());
9294 return;
9295 }
9296 }
Adrian Prantl05097242018-04-30 16:49:04 +00009297 // If we have gotten here we didn't get find the enumerator in the enum
9298 // decl, so just print the integer.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009299 s->Printf("%" PRIi64, enum_value);
9300 }
9301 return;
9302
9303 case clang::Type::ConstantArray: {
9304 const clang::ConstantArrayType *array =
9305 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9306 bool is_array_of_characters = false;
9307 clang::QualType element_qual_type = array->getElementType();
9308
9309 const clang::Type *canonical_type =
9310 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9311 if (canonical_type)
9312 is_array_of_characters = canonical_type->isCharType();
9313
9314 const uint64_t element_count = array->getSize().getLimitedValue();
9315
9316 clang::TypeInfo field_type_info =
9317 getASTContext()->getTypeInfo(element_qual_type);
9318
9319 uint32_t element_idx = 0;
9320 uint32_t element_offset = 0;
9321 uint64_t element_byte_size = field_type_info.Width / 8;
9322 uint32_t element_stride = element_byte_size;
9323
9324 if (is_array_of_characters) {
9325 s->PutChar('"');
Zachary Turner29cb8682017-03-03 20:57:05 +00009326 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9327 element_byte_size, element_count, UINT32_MAX,
9328 LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009329 s->PutChar('"');
9330 return;
9331 } else {
9332 CompilerType element_clang_type(getASTContext(), element_qual_type);
9333 lldb::Format element_format = element_clang_type.GetFormat();
9334
9335 for (element_idx = 0; element_idx < element_count; ++element_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009336 // Print the starting squiggly bracket (if this is the first member) or
9337 // comman (for member 2 and beyong) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009338 if (element_idx == 0)
9339 s->PutChar('{');
9340 else
9341 s->PutChar(',');
9342
9343 // Indent and print the index
9344 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9345
9346 // Figure out the field offset within the current struct/union/class
9347 // type
9348 element_offset = element_idx * element_stride;
9349
9350 // Dump the value of the member
9351 element_clang_type.DumpValue(
9352 exe_ctx,
9353 s, // Stream to dump to
9354 element_format, // The format with which to display the element
9355 data, // Data buffer containing all bytes for this type
9356 data_byte_offset +
9357 element_offset, // Offset into "data" where to grab value from
9358 element_byte_size, // Size of this type in bytes
9359 0, // Bitfield bit size
9360 0, // Bitfield bit offset
9361 show_types, // Boolean indicating if we should show the variable
9362 // types
9363 show_summary, // Boolean indicating if we should show a summary for
9364 // the current type
9365 verbose, // Verbose output?
9366 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9367 // children
9368 }
9369
9370 // Indent the trailing squiggly bracket
9371 if (element_idx > 0)
9372 s->Printf("\n%*s}", depth, "");
9373 }
9374 }
9375 return;
9376
9377 case clang::Type::Typedef: {
9378 clang::QualType typedef_qual_type =
9379 llvm::cast<clang::TypedefType>(qual_type)
9380 ->getDecl()
9381 ->getUnderlyingType();
9382
9383 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9384 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9385 clang::TypeInfo typedef_type_info =
9386 getASTContext()->getTypeInfo(typedef_qual_type);
9387 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9388
9389 return typedef_clang_type.DumpValue(
9390 exe_ctx,
9391 s, // Stream to dump to
9392 typedef_format, // The format with which to display the element
9393 data, // Data buffer containing all bytes for this type
9394 data_byte_offset, // Offset into "data" where to grab value from
9395 typedef_byte_size, // Size of this type in bytes
9396 bitfield_bit_size, // Bitfield bit size
9397 bitfield_bit_offset, // Bitfield bit offset
9398 show_types, // Boolean indicating if we should show the variable types
9399 show_summary, // Boolean indicating if we should show a summary for the
9400 // current type
9401 verbose, // Verbose output?
9402 depth); // Scope depth for any types that have children
9403 } break;
9404
9405 case clang::Type::Auto: {
9406 clang::QualType elaborated_qual_type =
9407 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9408 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9409 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9410 clang::TypeInfo elaborated_type_info =
9411 getASTContext()->getTypeInfo(elaborated_qual_type);
9412 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9413
9414 return elaborated_clang_type.DumpValue(
9415 exe_ctx,
9416 s, // Stream to dump to
9417 elaborated_format, // The format with which to display the element
9418 data, // Data buffer containing all bytes for this type
9419 data_byte_offset, // Offset into "data" where to grab value from
9420 elaborated_byte_size, // Size of this type in bytes
9421 bitfield_bit_size, // Bitfield bit size
9422 bitfield_bit_offset, // Bitfield bit offset
9423 show_types, // Boolean indicating if we should show the variable types
9424 show_summary, // Boolean indicating if we should show a summary for the
9425 // current type
9426 verbose, // Verbose output?
9427 depth); // Scope depth for any types that have children
9428 } break;
9429
9430 case clang::Type::Elaborated: {
9431 clang::QualType elaborated_qual_type =
9432 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9433 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9434 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9435 clang::TypeInfo elaborated_type_info =
9436 getASTContext()->getTypeInfo(elaborated_qual_type);
9437 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9438
9439 return elaborated_clang_type.DumpValue(
9440 exe_ctx,
9441 s, // Stream to dump to
9442 elaborated_format, // The format with which to display the element
9443 data, // Data buffer containing all bytes for this type
9444 data_byte_offset, // Offset into "data" where to grab value from
9445 elaborated_byte_size, // Size of this type in bytes
9446 bitfield_bit_size, // Bitfield bit size
9447 bitfield_bit_offset, // Bitfield bit offset
9448 show_types, // Boolean indicating if we should show the variable types
9449 show_summary, // Boolean indicating if we should show a summary for the
9450 // current type
9451 verbose, // Verbose output?
9452 depth); // Scope depth for any types that have children
9453 } break;
9454
9455 case clang::Type::Paren: {
9456 clang::QualType desugar_qual_type =
9457 llvm::cast<clang::ParenType>(qual_type)->desugar();
9458 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type);
9459
9460 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9461 clang::TypeInfo desugar_type_info =
9462 getASTContext()->getTypeInfo(desugar_qual_type);
9463 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9464
9465 return desugar_clang_type.DumpValue(
9466 exe_ctx,
9467 s, // Stream to dump to
9468 desugar_format, // The format with which to display the element
9469 data, // Data buffer containing all bytes for this type
9470 data_byte_offset, // Offset into "data" where to grab value from
9471 desugar_byte_size, // Size of this type in bytes
9472 bitfield_bit_size, // Bitfield bit size
9473 bitfield_bit_offset, // Bitfield bit offset
9474 show_types, // Boolean indicating if we should show the variable types
9475 show_summary, // Boolean indicating if we should show a summary for the
9476 // current type
9477 verbose, // Verbose output?
9478 depth); // Scope depth for any types that have children
9479 } break;
9480
9481 default:
9482 // We are down to a scalar type that we just need to display.
Zachary Turner29cb8682017-03-03 20:57:05 +00009483 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9484 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9485 bitfield_bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009486
9487 if (show_summary)
9488 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9489 break;
9490 }
9491}
9492
9493bool ClangASTContext::DumpTypeValue(
9494 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
Zachary Turner29cb8682017-03-03 20:57:05 +00009495 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9496 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009497 ExecutionContextScope *exe_scope) {
9498 if (!type)
9499 return false;
9500 if (IsAggregateType(type)) {
9501 return false;
9502 } else {
Greg Claytond8d4a572015-08-11 21:38:15 +00009503 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009504
9505 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9506 switch (type_class) {
9507 case clang::Type::Typedef: {
9508 clang::QualType typedef_qual_type =
9509 llvm::cast<clang::TypedefType>(qual_type)
9510 ->getDecl()
9511 ->getUnderlyingType();
9512 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9513 if (format == eFormatDefault)
9514 format = typedef_clang_type.GetFormat();
9515 clang::TypeInfo typedef_type_info =
9516 getASTContext()->getTypeInfo(typedef_qual_type);
9517 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9518
9519 return typedef_clang_type.DumpTypeValue(
9520 s,
9521 format, // The format with which to display the element
9522 data, // Data buffer containing all bytes for this type
9523 byte_offset, // Offset into "data" where to grab value from
9524 typedef_byte_size, // Size of this type in bytes
9525 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9526 // treat as a bitfield
9527 bitfield_bit_offset, // Offset in bits of a bitfield value if
9528 // bitfield_bit_size != 0
9529 exe_scope);
9530 } break;
9531
9532 case clang::Type::Enum:
Adrian Prantl05097242018-04-30 16:49:04 +00009533 // If our format is enum or default, show the enumeration value as its
9534 // enumeration string value, else just display it as requested.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009535 if ((format == eFormatEnum || format == eFormatDefault) &&
9536 GetCompleteType(type)) {
9537 const clang::EnumType *enutype =
9538 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9539 const clang::EnumDecl *enum_decl = enutype->getDecl();
9540 assert(enum_decl);
9541 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9542 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9543 lldb::offset_t offset = byte_offset;
9544 if (is_signed) {
9545 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9546 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9547 for (enum_pos = enum_decl->enumerator_begin(),
9548 enum_end_pos = enum_decl->enumerator_end();
9549 enum_pos != enum_end_pos; ++enum_pos) {
9550 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009551 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009552 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009553 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009554 }
9555 // If we have gotten here we didn't get find the enumerator in the
9556 // enum decl, so just print the integer.
9557 s->Printf("%" PRIi64, enum_svalue);
9558 } else {
9559 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9560 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9561 for (enum_pos = enum_decl->enumerator_begin(),
9562 enum_end_pos = enum_decl->enumerator_end();
9563 enum_pos != enum_end_pos; ++enum_pos) {
9564 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009565 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009566 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009567 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009568 }
9569 // If we have gotten here we didn't get find the enumerator in the
9570 // enum decl, so just print the integer.
9571 s->Printf("%" PRIu64, enum_uvalue);
Greg Claytond8d4a572015-08-11 21:38:15 +00009572 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009573 return true;
9574 }
9575 // format was not enum, just fall through and dump the value as
9576 // requested....
9577 LLVM_FALLTHROUGH;
9578
9579 default:
9580 // We are down to a scalar type that we just need to display.
9581 {
9582 uint32_t item_count = 1;
9583 // A few formats, we might need to modify our size and count for
9584 // depending
9585 // on how we are trying to display the value...
9586 switch (format) {
Greg Claytond8d4a572015-08-11 21:38:15 +00009587 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00009588 case eFormatBoolean:
9589 case eFormatBinary:
9590 case eFormatComplex:
9591 case eFormatCString: // NULL terminated C strings
9592 case eFormatDecimal:
9593 case eFormatEnum:
9594 case eFormatHex:
9595 case eFormatHexUppercase:
9596 case eFormatFloat:
9597 case eFormatOctal:
9598 case eFormatOSType:
9599 case eFormatUnsigned:
9600 case eFormatPointer:
9601 case eFormatVectorOfChar:
9602 case eFormatVectorOfSInt8:
9603 case eFormatVectorOfUInt8:
9604 case eFormatVectorOfSInt16:
9605 case eFormatVectorOfUInt16:
9606 case eFormatVectorOfSInt32:
9607 case eFormatVectorOfUInt32:
9608 case eFormatVectorOfSInt64:
9609 case eFormatVectorOfUInt64:
9610 case eFormatVectorOfFloat32:
9611 case eFormatVectorOfFloat64:
9612 case eFormatVectorOfUInt128:
9613 break;
9614
9615 case eFormatChar:
9616 case eFormatCharPrintable:
9617 case eFormatCharArray:
9618 case eFormatBytes:
9619 case eFormatBytesWithASCII:
9620 item_count = byte_size;
9621 byte_size = 1;
9622 break;
9623
9624 case eFormatUnicode16:
9625 item_count = byte_size / 2;
9626 byte_size = 2;
9627 break;
9628
9629 case eFormatUnicode32:
9630 item_count = byte_size / 4;
9631 byte_size = 4;
9632 break;
9633 }
Zachary Turner29cb8682017-03-03 20:57:05 +00009634 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9635 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
9636 bitfield_bit_size, bitfield_bit_offset,
9637 exe_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009638 }
9639 break;
9640 }
9641 }
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00009642 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009643}
9644
9645void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9646 ExecutionContext *exe_ctx, Stream *s,
9647 const lldb_private::DataExtractor &data,
9648 lldb::offset_t data_byte_offset,
9649 size_t data_byte_size) {
9650 uint32_t length = 0;
9651 if (IsCStringType(type, length)) {
9652 if (exe_ctx) {
9653 Process *process = exe_ctx->GetProcessPtr();
9654 if (process) {
9655 lldb::offset_t offset = data_byte_offset;
9656 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9657 std::vector<uint8_t> buf;
9658 if (length > 0)
9659 buf.resize(length);
9660 else
9661 buf.resize(256);
9662
Zachary Turner29cb8682017-03-03 20:57:05 +00009663 DataExtractor cstr_data(&buf.front(), buf.size(),
9664 process->GetByteOrder(), 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009665 buf.back() = '\0';
9666 size_t bytes_read;
9667 size_t total_cstr_len = 0;
Zachary Turner97206d52017-05-12 04:51:55 +00009668 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009669 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9670 buf.size(), error)) > 0) {
9671 const size_t len = strlen((const char *)&buf.front());
9672 if (len == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00009673 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009674 if (total_cstr_len == 0)
9675 s->PutCString(" \"");
Zachary Turner29cb8682017-03-03 20:57:05 +00009676 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9677 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009678 total_cstr_len += len;
9679 if (len < buf.size())
9680 break;
9681 pointer_address += total_cstr_len;
Greg Claytond8d4a572015-08-11 21:38:15 +00009682 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009683 if (total_cstr_len > 0)
9684 s->PutChar('"');
9685 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009686 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009687 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009688}
9689
Kate Stoneb9c1b512016-09-06 20:57:50 +00009690void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9691 StreamFile s(stdout, false);
9692 DumpTypeDescription(type, &s);
9693 ClangASTMetadata *metadata =
9694 ClangASTContext::GetMetadata(getASTContext(), type);
9695 if (metadata) {
9696 metadata->Dump(&s);
9697 }
9698}
Greg Claytond8d4a572015-08-11 21:38:15 +00009699
Kate Stoneb9c1b512016-09-06 20:57:50 +00009700void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9701 Stream *s) {
9702 if (type) {
9703 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00009704
Kate Stoneb9c1b512016-09-06 20:57:50 +00009705 llvm::SmallVector<char, 1024> buf;
9706 llvm::raw_svector_ostream llvm_ostrm(buf);
9707
9708 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9709 switch (type_class) {
9710 case clang::Type::ObjCObject:
9711 case clang::Type::ObjCInterface: {
9712 GetCompleteType(type);
9713
9714 const clang::ObjCObjectType *objc_class_type =
9715 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9716 assert(objc_class_type);
9717 if (objc_class_type) {
9718 clang::ObjCInterfaceDecl *class_interface_decl =
9719 objc_class_type->getInterface();
9720 if (class_interface_decl) {
9721 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9722 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
Greg Claytond8d4a572015-08-11 21:38:15 +00009723 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009724 }
9725 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00009726
Kate Stoneb9c1b512016-09-06 20:57:50 +00009727 case clang::Type::Typedef: {
9728 const clang::TypedefType *typedef_type =
9729 qual_type->getAs<clang::TypedefType>();
9730 if (typedef_type) {
9731 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9732 std::string clang_typedef_name(
9733 typedef_decl->getQualifiedNameAsString());
9734 if (!clang_typedef_name.empty()) {
9735 s->PutCString("typedef ");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009736 s->PutCString(clang_typedef_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00009737 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009738 }
9739 } break;
9740
9741 case clang::Type::Auto:
9742 CompilerType(getASTContext(),
9743 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
9744 .DumpTypeDescription(s);
9745 return;
9746
9747 case clang::Type::Elaborated:
9748 CompilerType(getASTContext(),
9749 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
9750 .DumpTypeDescription(s);
9751 return;
9752
9753 case clang::Type::Paren:
9754 CompilerType(getASTContext(),
9755 llvm::cast<clang::ParenType>(qual_type)->desugar())
9756 .DumpTypeDescription(s);
9757 return;
9758
9759 case clang::Type::Record: {
9760 GetCompleteType(type);
9761
9762 const clang::RecordType *record_type =
9763 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9764 const clang::RecordDecl *record_decl = record_type->getDecl();
9765 const clang::CXXRecordDecl *cxx_record_decl =
9766 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9767
9768 if (cxx_record_decl)
9769 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9770 s->GetIndentLevel());
9771 else
9772 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9773 s->GetIndentLevel());
9774 } break;
9775
9776 default: {
9777 const clang::TagType *tag_type =
9778 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9779 if (tag_type) {
9780 clang::TagDecl *tag_decl = tag_type->getDecl();
9781 if (tag_decl)
9782 tag_decl->print(llvm_ostrm, 0);
9783 } else {
9784 std::string clang_type_name(qual_type.getAsString());
9785 if (!clang_type_name.empty())
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009786 s->PutCString(clang_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009787 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009788 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00009789 }
9790
Kate Stoneb9c1b512016-09-06 20:57:50 +00009791 if (buf.size() > 0) {
9792 s->Write(buf.data(), buf.size());
Greg Clayton8b4edba2015-08-14 20:02:05 +00009793 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009794 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00009795}
9796
Kate Stoneb9c1b512016-09-06 20:57:50 +00009797void ClangASTContext::DumpTypeName(const CompilerType &type) {
9798 if (ClangUtil::IsClangType(type)) {
9799 clang::QualType qual_type(
9800 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9801
9802 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9803 switch (type_class) {
9804 case clang::Type::Record: {
9805 const clang::CXXRecordDecl *cxx_record_decl =
9806 qual_type->getAsCXXRecordDecl();
9807 if (cxx_record_decl)
9808 printf("class %s", cxx_record_decl->getName().str().c_str());
9809 } break;
9810
9811 case clang::Type::Enum: {
9812 clang::EnumDecl *enum_decl =
9813 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9814 if (enum_decl) {
9815 printf("enum %s", enum_decl->getName().str().c_str());
9816 }
9817 } break;
9818
9819 case clang::Type::ObjCObject:
9820 case clang::Type::ObjCInterface: {
9821 const clang::ObjCObjectType *objc_class_type =
9822 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9823 if (objc_class_type) {
9824 clang::ObjCInterfaceDecl *class_interface_decl =
9825 objc_class_type->getInterface();
Adrian Prantl05097242018-04-30 16:49:04 +00009826 // We currently can't complete objective C types through the newly
9827 // added ASTContext because it only supports TagDecl objects right
9828 // now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00009829 if (class_interface_decl)
9830 printf("@class %s", class_interface_decl->getName().str().c_str());
9831 }
9832 } break;
9833
9834 case clang::Type::Typedef:
9835 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9836 ->getDecl()
9837 ->getName()
9838 .str()
9839 .c_str());
9840 break;
9841
9842 case clang::Type::Auto:
9843 printf("auto ");
9844 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9845 llvm::cast<clang::AutoType>(qual_type)
9846 ->getDeducedType()
9847 .getAsOpaquePtr()));
9848
9849 case clang::Type::Elaborated:
9850 printf("elaborated ");
9851 return DumpTypeName(CompilerType(
9852 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9853 ->getNamedType()
9854 .getAsOpaquePtr()));
9855
9856 case clang::Type::Paren:
9857 printf("paren ");
9858 return DumpTypeName(CompilerType(
9859 type.GetTypeSystem(),
9860 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9861
9862 default:
9863 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9864 break;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009865 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009866 }
Greg Clayton6dc8d582015-08-18 22:32:36 +00009867}
9868
Kate Stoneb9c1b512016-09-06 20:57:50 +00009869clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9870 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9871 const char *parent_name, int tag_decl_kind,
9872 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9873 if (template_param_infos.IsValid()) {
9874 std::string template_basename(parent_name);
9875 template_basename.erase(template_basename.find('<'));
9876
9877 return CreateClassTemplateDecl(decl_ctx, access_type,
9878 template_basename.c_str(), tag_decl_kind,
9879 template_param_infos);
9880 }
Konrad Kleine248a1302019-05-23 11:14:47 +00009881 return nullptr;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009882}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009883
Kate Stoneb9c1b512016-09-06 20:57:50 +00009884void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9885 ClangASTContext *ast = (ClangASTContext *)baton;
9886 SymbolFile *sym_file = ast->GetSymbolFile();
9887 if (sym_file) {
9888 CompilerType clang_type = GetTypeForDecl(decl);
9889 if (clang_type)
9890 sym_file->CompleteType(clang_type);
9891 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00009892}
9893
Kate Stoneb9c1b512016-09-06 20:57:50 +00009894void ClangASTContext::CompleteObjCInterfaceDecl(
9895 void *baton, clang::ObjCInterfaceDecl *decl) {
9896 ClangASTContext *ast = (ClangASTContext *)baton;
9897 SymbolFile *sym_file = ast->GetSymbolFile();
9898 if (sym_file) {
9899 CompilerType clang_type = GetTypeForDecl(decl);
9900 if (clang_type)
9901 sym_file->CompleteType(clang_type);
9902 }
Zachary Turner42dff792016-04-15 00:21:26 +00009903}
Greg Clayton261ac3f2015-08-28 01:01:03 +00009904
Kate Stoneb9c1b512016-09-06 20:57:50 +00009905DWARFASTParser *ClangASTContext::GetDWARFParser() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009906 if (!m_dwarf_ast_parser_up)
9907 m_dwarf_ast_parser_up.reset(new DWARFASTParserClang(*this));
9908 return m_dwarf_ast_parser_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00009909}
9910
9911PDBASTParser *ClangASTContext::GetPDBParser() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009912 if (!m_pdb_ast_parser_up)
9913 m_pdb_ast_parser_up.reset(new PDBASTParser(*this));
9914 return m_pdb_ast_parser_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00009915}
9916
9917bool ClangASTContext::LayoutRecordType(
9918 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9919 uint64_t &alignment,
9920 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9921 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9922 &base_offsets,
9923 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9924 &vbase_offsets) {
9925 ClangASTContext *ast = (ClangASTContext *)baton;
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00009926 lldb_private::ClangASTImporter *importer = nullptr;
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009927 if (ast->m_dwarf_ast_parser_up)
9928 importer = &ast->m_dwarf_ast_parser_up->GetClangASTImporter();
9929 if (!importer && ast->m_pdb_ast_parser_up)
9930 importer = &ast->m_pdb_ast_parser_up->GetClangASTImporter();
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00009931 if (!importer)
9932 return false;
9933
9934 return importer->LayoutRecordType(record_decl, bit_size, alignment,
9935 field_offsets, base_offsets, vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009936}
9937
Paul Hermand628cbb2015-09-15 23:44:17 +00009938// CompilerDecl override functions
Paul Hermand628cbb2015-09-15 23:44:17 +00009939
Kate Stoneb9c1b512016-09-06 20:57:50 +00009940ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9941 if (opaque_decl) {
9942 clang::NamedDecl *nd =
9943 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9944 if (nd != nullptr)
9945 return ConstString(nd->getDeclName().getAsString());
9946 }
9947 return ConstString();
Paul Hermand628cbb2015-09-15 23:44:17 +00009948}
9949
Kate Stoneb9c1b512016-09-06 20:57:50 +00009950ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9951 if (opaque_decl) {
9952 clang::NamedDecl *nd =
9953 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9954 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9955 clang::MangleContext *mc = getMangleContext();
9956 if (mc && mc->shouldMangleCXXName(nd)) {
9957 llvm::SmallVector<char, 1024> buf;
9958 llvm::raw_svector_ostream llvm_ostrm(buf);
9959 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9960 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9961 Ctor_Complete, llvm_ostrm);
9962 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9963 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9964 Dtor_Complete, llvm_ostrm);
9965 } else {
9966 mc->mangleName(nd, llvm_ostrm);
Greg Claytonfe689042015-11-10 17:47:04 +00009967 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009968 if (buf.size() > 0)
9969 return ConstString(buf.data(), buf.size());
9970 }
Greg Claytonfe689042015-11-10 17:47:04 +00009971 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009972 }
9973 return ConstString();
Greg Claytonfe689042015-11-10 17:47:04 +00009974}
9975
Kate Stoneb9c1b512016-09-06 20:57:50 +00009976CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9977 if (opaque_decl)
9978 return CompilerDeclContext(this,
9979 ((clang::Decl *)opaque_decl)->getDeclContext());
9980 else
9981 return CompilerDeclContext();
Greg Claytonfe689042015-11-10 17:47:04 +00009982}
9983
Kate Stoneb9c1b512016-09-06 20:57:50 +00009984CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9985 if (clang::FunctionDecl *func_decl =
9986 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9987 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9988 if (clang::ObjCMethodDecl *objc_method =
9989 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9990 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9991 else
Greg Claytonfe689042015-11-10 17:47:04 +00009992 return CompilerType();
9993}
9994
Kate Stoneb9c1b512016-09-06 20:57:50 +00009995size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9996 if (clang::FunctionDecl *func_decl =
9997 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9998 return func_decl->param_size();
9999 if (clang::ObjCMethodDecl *objc_method =
10000 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
10001 return objc_method->param_size();
10002 else
10003 return 0;
10004}
10005
10006CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
10007 size_t idx) {
10008 if (clang::FunctionDecl *func_decl =
10009 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
10010 if (idx < func_decl->param_size()) {
10011 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
10012 if (var_decl)
10013 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
10014 }
10015 } else if (clang::ObjCMethodDecl *objc_method =
10016 llvm::dyn_cast<clang::ObjCMethodDecl>(
10017 (clang::Decl *)opaque_decl)) {
10018 if (idx < objc_method->param_size())
10019 return CompilerType(
10020 this,
10021 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
10022 }
10023 return CompilerType();
10024}
10025
Greg Clayton99558cc42015-08-24 23:46:31 +000010026// CompilerDeclContext functions
Greg Clayton99558cc42015-08-24 23:46:31 +000010027
Kate Stoneb9c1b512016-09-06 20:57:50 +000010028std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
10029 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
10030 std::vector<CompilerDecl> found_decls;
10031 if (opaque_decl_ctx) {
10032 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
10033 std::set<DeclContext *> searched;
10034 std::multimap<DeclContext *, DeclContext *> search_queue;
10035 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +000010036
Kate Stoneb9c1b512016-09-06 20:57:50 +000010037 for (clang::DeclContext *decl_context = root_decl_ctx;
10038 decl_context != nullptr && found_decls.empty();
10039 decl_context = decl_context->getParent()) {
10040 search_queue.insert(std::make_pair(decl_context, decl_context));
Paul Hermand628cbb2015-09-15 23:44:17 +000010041
Kate Stoneb9c1b512016-09-06 20:57:50 +000010042 for (auto it = search_queue.find(decl_context); it != search_queue.end();
10043 it++) {
10044 if (!searched.insert(it->second).second)
10045 continue;
10046 symbol_file->ParseDeclsForContext(
10047 CompilerDeclContext(this, it->second));
Paul Hermanea188fc2015-09-16 18:48:30 +000010048
Kate Stoneb9c1b512016-09-06 20:57:50 +000010049 for (clang::Decl *child : it->second->decls()) {
10050 if (clang::UsingDirectiveDecl *ud =
10051 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10052 if (ignore_using_decls)
10053 continue;
10054 clang::DeclContext *from = ud->getCommonAncestor();
10055 if (searched.find(ud->getNominatedNamespace()) == searched.end())
10056 search_queue.insert(
10057 std::make_pair(from, ud->getNominatedNamespace()));
10058 } else if (clang::UsingDecl *ud =
10059 llvm::dyn_cast<clang::UsingDecl>(child)) {
10060 if (ignore_using_decls)
10061 continue;
10062 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10063 clang::Decl *target = usd->getTargetDecl();
10064 if (clang::NamedDecl *nd =
10065 llvm::dyn_cast<clang::NamedDecl>(target)) {
10066 IdentifierInfo *ii = nd->getIdentifier();
10067 if (ii != nullptr &&
10068 ii->getName().equals(name.AsCString(nullptr)))
10069 found_decls.push_back(CompilerDecl(this, nd));
10070 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010071 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010072 } else if (clang::NamedDecl *nd =
10073 llvm::dyn_cast<clang::NamedDecl>(child)) {
10074 IdentifierInfo *ii = nd->getIdentifier();
10075 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
10076 found_decls.push_back(CompilerDecl(this, nd));
10077 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010078 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010079 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010080 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010081 }
10082 return found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +000010083}
10084
Dawn Perchikb5925782015-12-12 19:31:41 +000010085// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010086// and return the number of levels it took to find it, or
Adrian Prantl05097242018-04-30 16:49:04 +000010087// LLDB_INVALID_DECL_LEVEL if not found. If the decl was imported via a using
10088// declaration, its name and/or type, if set, will be used to check that the
10089// decl found in the scope is a match.
Dawn Perchikb5925782015-12-12 19:31:41 +000010090//
Kate Stoneb9c1b512016-09-06 20:57:50 +000010091// The optional name is required by languages (like C++) to handle using
Adrian Prantl05097242018-04-30 16:49:04 +000010092// declarations like:
Dawn Perchikb5925782015-12-12 19:31:41 +000010093//
10094// void poo();
10095// namespace ns {
10096// void foo();
10097// void goo();
10098// }
10099// void bar() {
10100// using ns::foo;
10101// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
10102// // LLDB_INVALID_DECL_LEVEL for 'goo'.
10103// }
10104//
10105// The optional type is useful in the case that there's a specific overload
10106// that we're looking for that might otherwise be shadowed, like:
10107//
10108// void foo(int);
10109// namespace ns {
10110// void foo();
10111// }
10112// void bar() {
10113// using ns::foo;
10114// // CountDeclLevels returns 0 for { 'foo', void() },
10115// // 1 for { 'foo', void(int) }, and
10116// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
10117// }
10118//
10119// NOTE: Because file statics are at the TranslationUnit along with globals, a
Kate Stoneb9c1b512016-09-06 20:57:50 +000010120// function at file scope will return the same level as a function at global
Adrian Prantl05097242018-04-30 16:49:04 +000010121// scope. Ideally we'd like to treat the file scope as an additional scope just
10122// below the global scope. More work needs to be done to recognise that, if
10123// the decl we're trying to look up is static, we should compare its source
10124// file with that of the current scope and return a lower number for it.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010125uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
10126 clang::DeclContext *child_decl_ctx,
10127 ConstString *child_name,
10128 CompilerType *child_type) {
10129 if (frame_decl_ctx) {
10130 std::set<DeclContext *> searched;
10131 std::multimap<DeclContext *, DeclContext *> search_queue;
10132 SymbolFile *symbol_file = GetSymbolFile();
Dawn Perchikb5925782015-12-12 19:31:41 +000010133
Kate Stoneb9c1b512016-09-06 20:57:50 +000010134 // Get the lookup scope for the decl we're trying to find.
10135 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
Dawn Perchikb5925782015-12-12 19:31:41 +000010136
Kate Stoneb9c1b512016-09-06 20:57:50 +000010137 // Look for it in our scope's decl context and its parents.
10138 uint32_t level = 0;
10139 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
10140 decl_ctx = decl_ctx->getParent()) {
10141 if (!decl_ctx->isLookupContext())
10142 continue;
10143 if (decl_ctx == parent_decl_ctx)
10144 // Found it!
10145 return level;
10146 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
10147 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
10148 it++) {
10149 if (searched.find(it->second) != searched.end())
10150 continue;
10151
10152 // Currently DWARF has one shared translation unit for all Decls at top
Adrian Prantl05097242018-04-30 16:49:04 +000010153 // level, so this would erroneously find using statements anywhere. So
10154 // don't look at the top-level translation unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010155 // TODO fix this and add a testcase that depends on it.
10156
10157 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
10158 continue;
10159
10160 searched.insert(it->second);
10161 symbol_file->ParseDeclsForContext(
10162 CompilerDeclContext(this, it->second));
10163
10164 for (clang::Decl *child : it->second->decls()) {
10165 if (clang::UsingDirectiveDecl *ud =
10166 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10167 clang::DeclContext *ns = ud->getNominatedNamespace();
10168 if (ns == parent_decl_ctx)
10169 // Found it!
10170 return level;
10171 clang::DeclContext *from = ud->getCommonAncestor();
10172 if (searched.find(ns) == searched.end())
10173 search_queue.insert(std::make_pair(from, ns));
10174 } else if (child_name) {
10175 if (clang::UsingDecl *ud =
10176 llvm::dyn_cast<clang::UsingDecl>(child)) {
10177 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10178 clang::Decl *target = usd->getTargetDecl();
10179 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
10180 if (!nd)
10181 continue;
10182 // Check names.
10183 IdentifierInfo *ii = nd->getIdentifier();
10184 if (ii == nullptr ||
10185 !ii->getName().equals(child_name->AsCString(nullptr)))
10186 continue;
10187 // Check types, if one was provided.
10188 if (child_type) {
10189 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
10190 if (!AreTypesSame(clang_type, *child_type,
10191 /*ignore_qualifiers=*/true))
10192 continue;
10193 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010194 // Found it!
10195 return level;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010196 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010197 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010198 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010199 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010200 }
10201 ++level;
Dawn Perchikb5925782015-12-12 19:31:41 +000010202 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010203 }
10204 return LLDB_INVALID_DECL_LEVEL;
Dawn Perchikb5925782015-12-12 19:31:41 +000010205}
10206
Kate Stoneb9c1b512016-09-06 20:57:50 +000010207bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
10208 if (opaque_decl_ctx)
10209 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
10210 else
Greg Clayton99558cc42015-08-24 23:46:31 +000010211 return false;
10212}
10213
Kate Stoneb9c1b512016-09-06 20:57:50 +000010214ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
10215 if (opaque_decl_ctx) {
10216 clang::NamedDecl *named_decl =
10217 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10218 if (named_decl)
10219 return ConstString(named_decl->getName());
10220 }
10221 return ConstString();
Greg Clayton99558cc42015-08-24 23:46:31 +000010222}
10223
Kate Stoneb9c1b512016-09-06 20:57:50 +000010224ConstString
10225ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
10226 if (opaque_decl_ctx) {
10227 clang::NamedDecl *named_decl =
10228 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10229 if (named_decl)
10230 return ConstString(
10231 llvm::StringRef(named_decl->getQualifiedNameAsString()));
10232 }
10233 return ConstString();
10234}
10235
10236bool ClangASTContext::DeclContextIsClassMethod(
10237 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
10238 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
10239 if (opaque_decl_ctx) {
10240 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10241 if (ObjCMethodDecl *objc_method =
10242 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
10243 if (is_instance_method_ptr)
10244 *is_instance_method_ptr = objc_method->isInstanceMethod();
10245 if (language_ptr)
10246 *language_ptr = eLanguageTypeObjC;
10247 if (language_object_name_ptr)
10248 language_object_name_ptr->SetCString("self");
10249 return true;
10250 } else if (CXXMethodDecl *cxx_method =
10251 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
10252 if (is_instance_method_ptr)
10253 *is_instance_method_ptr = cxx_method->isInstance();
10254 if (language_ptr)
10255 *language_ptr = eLanguageTypeC_plus_plus;
10256 if (language_object_name_ptr)
10257 language_object_name_ptr->SetCString("this");
10258 return true;
10259 } else if (clang::FunctionDecl *function_decl =
10260 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
10261 ClangASTMetadata *metadata =
10262 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
10263 if (metadata && metadata->HasObjectPtr()) {
10264 if (is_instance_method_ptr)
10265 *is_instance_method_ptr = true;
10266 if (language_ptr)
10267 *language_ptr = eLanguageTypeObjC;
10268 if (language_object_name_ptr)
10269 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
10270 return true;
10271 }
10272 }
10273 }
10274 return false;
10275}
10276
Raphael Isemanna9469972019-03-12 07:45:04 +000010277bool ClangASTContext::DeclContextIsContainedInLookup(
10278 void *opaque_decl_ctx, void *other_opaque_decl_ctx) {
10279 auto *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10280 auto *other = (clang::DeclContext *)other_opaque_decl_ctx;
10281
10282 do {
10283 // A decl context always includes its own contents in its lookup.
10284 if (decl_ctx == other)
10285 return true;
10286
10287 // If we have an inline namespace, then the lookup of the parent context
10288 // also includes the inline namespace contents.
10289 } while (other->isInlineNamespace() && (other = other->getParent()));
10290
10291 return false;
10292}
10293
Kate Stoneb9c1b512016-09-06 20:57:50 +000010294clang::DeclContext *
10295ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
10296 if (dc.IsClang())
10297 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
10298 return nullptr;
10299}
Greg Clayton99558cc42015-08-24 23:46:31 +000010300
10301ObjCMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010302ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
10303 if (dc.IsClang())
10304 return llvm::dyn_cast<clang::ObjCMethodDecl>(
10305 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10306 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010307}
10308
10309CXXMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010310ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10311 if (dc.IsClang())
10312 return llvm::dyn_cast<clang::CXXMethodDecl>(
10313 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10314 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010315}
10316
10317clang::FunctionDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010318ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10319 if (dc.IsClang())
10320 return llvm::dyn_cast<clang::FunctionDecl>(
10321 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10322 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010323}
10324
10325clang::NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010326ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10327 if (dc.IsClang())
10328 return llvm::dyn_cast<clang::NamespaceDecl>(
10329 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10330 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010331}
10332
10333ClangASTMetadata *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010334ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10335 const void *object) {
10336 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10337 if (ast)
10338 return ClangASTContext::GetMetadata(ast, object);
10339 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010340}
10341
10342clang::ASTContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010343ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10344 ClangASTContext *ast =
10345 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10346 if (ast)
10347 return ast->getASTContext();
10348 return nullptr;
10349}
10350
10351ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10352 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10353 m_target_wp(target.shared_from_this()),
10354 m_persistent_variables(new ClangPersistentVariables) {}
10355
10356UserExpression *ClangASTContextForExpressions::GetUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010357 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010358 Expression::ResultType desired_type,
Aleksandr Urakov40624a02019-02-05 09:14:36 +000010359 const EvaluateExpressionOptions &options,
10360 ValueObject *ctx_obj) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000010361 TargetSP target_sp = m_target_wp.lock();
10362 if (!target_sp)
Greg Clayton99558cc42015-08-24 23:46:31 +000010363 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010364
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010365 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
Aleksandr Urakov40624a02019-02-05 09:14:36 +000010366 desired_type, options, ctx_obj);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010367}
10368
Kate Stoneb9c1b512016-09-06 20:57:50 +000010369FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10370 const CompilerType &return_type, const Address &function_address,
10371 const ValueList &arg_value_list, const char *name) {
10372 TargetSP target_sp = m_target_wp.lock();
10373 if (!target_sp)
10374 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010375
Kate Stoneb9c1b512016-09-06 20:57:50 +000010376 Process *process = target_sp->GetProcessSP().get();
10377 if (!process)
10378 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010379
Kate Stoneb9c1b512016-09-06 20:57:50 +000010380 return new ClangFunctionCaller(*process, return_type, function_address,
10381 arg_value_list, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010382}
10383
10384UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010385ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10386 const char *name) {
10387 TargetSP target_sp = m_target_wp.lock();
10388 if (!target_sp)
10389 return nullptr;
10390
10391 return new ClangUtilityFunction(*target_sp.get(), text, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010392}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010393
10394PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010395ClangASTContextForExpressions::GetPersistentExpressionState() {
10396 return m_persistent_variables.get();
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010397}
Sean Callanan68e44232017-09-28 20:20:25 +000010398
10399clang::ExternalASTMerger &
10400ClangASTContextForExpressions::GetMergerUnchecked() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +000010401 lldbassert(m_scratch_ast_source_up != nullptr);
10402 return m_scratch_ast_source_up->GetMergerUnchecked();
Sean Callanan68e44232017-09-28 20:20:25 +000010403}