blob: 3670084680292b9bb7e34a94dd0979c27849c90f [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"
Rainer Orth6ca17072019-08-05 14:00:43 +000050#include "clang/Basic/LangStandard.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051#include "clang/Basic/SourceManager.h"
52#include "clang/Basic/TargetInfo.h"
53#include "clang/Basic/TargetOptions.h"
54#include "clang/Frontend/FrontendOptions.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"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000079#include "lldb/Symbol/ClangASTImporter.h"
Greg Clayton6dc8d582015-08-18 22:32:36 +000080#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000081#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000082#include "lldb/Symbol/ClangUtil.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000083#include "lldb/Symbol/ObjectFile.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000084#include "lldb/Symbol/SymbolFile.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000085#include "lldb/Target/ExecutionContext.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000086#include "lldb/Target/Language.h"
Jim Ingham151c0322015-09-15 21:13:50 +000087#include "lldb/Target/Process.h"
88#include "lldb/Target/Target.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000089#include "lldb/Utility/DataExtractor.h"
Sean Callananc530ba92016-05-02 21:15:31 +000090#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000091#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000092#include "lldb/Utility/RegularExpression.h"
Pavel Labathd821c992018-08-07 11:07:21 +000093#include "lldb/Utility/Scalar.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000094
Alex Langfordb57017102019-07-15 22:56:12 +000095#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000096#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
Zachary Turner42dff792016-04-15 00:21:26 +000097#include "Plugins/SymbolFile/PDB/PDBASTParser.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000098
Eli Friedman932197d2010-06-13 19:06:42 +000099#include <stdio.h>
100
Greg Clayton1341baf2013-07-11 23:36:31 +0000101#include <mutex>
102
Greg Claytonc86103d2010-08-05 01:57:25 +0000103using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104using namespace lldb_private;
105using namespace llvm;
106using namespace clang;
107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108namespace {
Pavel Labath65a376f2019-08-21 13:11:30 +0000109#ifdef LLDB_CONFIGURATION_DEBUG
Alex Langfordb2232a12019-08-20 22:06:13 +0000110static void VerifyDecl(clang::Decl *decl) {
111 assert(decl && "VerifyDecl called with nullptr?");
112 decl->getAccess();
113}
Pavel Labath65a376f2019-08-21 13:11:30 +0000114#endif
Alex Langfordb2232a12019-08-20 22:06:13 +0000115
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116static inline bool
117ClangASTContextSupportsLanguage(lldb::LanguageType language) {
118 return language == eLanguageTypeUnknown || // Clang is the default type system
Rainer Orth6ca17072019-08-05 14:00:43 +0000119 lldb_private::Language::LanguageIsC(language) ||
120 lldb_private::Language::LanguageIsCPlusPlus(language) ||
121 lldb_private::Language::LanguageIsObjC(language) ||
122 lldb_private::Language::LanguageIsPascal(language) ||
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 // Use Clang for Rust until there is a proper language plugin for it
124 language == eLanguageTypeRust ||
Johan Engelen04799572016-11-25 11:01:12 +0000125 language == eLanguageTypeExtRenderScript ||
126 // Use Clang for D until there is a proper language plugin for it
Bruce Mitchenerb8233f82018-11-27 05:37:27 +0000127 language == eLanguageTypeD ||
128 // Open Dylan compiler debug info is designed to be Clang-compatible
129 language == eLanguageTypeDylan;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130}
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000131
132// Checks whether m1 is an overload of m2 (as opposed to an override). This is
133// called by addOverridesForMethod to distinguish overrides (which share a
134// vtable entry) from overloads (which require distinct entries).
135bool isOverload(clang::CXXMethodDecl *m1, clang::CXXMethodDecl *m2) {
136 // FIXME: This should detect covariant return types, but currently doesn't.
137 lldbassert(&m1->getASTContext() == &m2->getASTContext() &&
138 "Methods should have the same AST context");
139 clang::ASTContext &context = m1->getASTContext();
140
141 const auto *m1Type = llvm::cast<clang::FunctionProtoType>(
142 context.getCanonicalType(m1->getType()));
143
144 const auto *m2Type = llvm::cast<clang::FunctionProtoType>(
145 context.getCanonicalType(m2->getType()));
146
147 auto compareArgTypes = [&context](const clang::QualType &m1p,
148 const clang::QualType &m2p) {
149 return context.hasSameType(m1p.getUnqualifiedType(),
150 m2p.getUnqualifiedType());
151 };
152
153 // FIXME: In C++14 and later, we can just pass m2Type->param_type_end()
154 // as a fourth parameter to std::equal().
155 return (m1->getNumParams() != m2->getNumParams()) ||
156 !std::equal(m1Type->param_type_begin(), m1Type->param_type_end(),
157 m2Type->param_type_begin(), compareArgTypes);
158}
159
160// If decl is a virtual method, walk the base classes looking for methods that
161// decl overrides. This table of overridden methods is used by IRGen to
162// determine the vtable layout for decl's parent class.
163void addOverridesForMethod(clang::CXXMethodDecl *decl) {
164 if (!decl->isVirtual())
165 return;
166
167 clang::CXXBasePaths paths;
168
169 auto find_overridden_methods =
170 [decl](const clang::CXXBaseSpecifier *specifier,
171 clang::CXXBasePath &path) {
172 if (auto *base_record = llvm::dyn_cast<clang::CXXRecordDecl>(
173 specifier->getType()->getAs<clang::RecordType>()->getDecl())) {
174
175 clang::DeclarationName name = decl->getDeclName();
176
177 // If this is a destructor, check whether the base class destructor is
178 // virtual.
179 if (name.getNameKind() == clang::DeclarationName::CXXDestructorName)
180 if (auto *baseDtorDecl = base_record->getDestructor()) {
181 if (baseDtorDecl->isVirtual()) {
182 path.Decls = baseDtorDecl;
183 return true;
184 } else
185 return false;
186 }
187
188 // Otherwise, search for name in the base class.
189 for (path.Decls = base_record->lookup(name); !path.Decls.empty();
190 path.Decls = path.Decls.slice(1)) {
191 if (auto *method_decl =
192 llvm::dyn_cast<clang::CXXMethodDecl>(path.Decls.front()))
193 if (method_decl->isVirtual() && !isOverload(decl, method_decl)) {
194 path.Decls = method_decl;
195 return true;
196 }
197 }
198 }
199
200 return false;
201 };
202
203 if (decl->getParent()->lookupInBases(find_overridden_methods, paths)) {
204 for (auto *overridden_decl : paths.found_decls())
205 decl->addOverriddenMethod(
206 llvm::cast<clang::CXXMethodDecl>(overridden_decl));
207 }
208}
Greg Clayton56939cb2015-09-17 22:23:34 +0000209}
210
Aleksandr Urakov1dc51db2018-11-12 16:23:50 +0000211static lldb::addr_t GetVTableAddress(Process &process,
212 VTableContextBase &vtable_ctx,
213 ValueObject &valobj,
214 const ASTRecordLayout &record_layout) {
215 // Retrieve type info
216 CompilerType pointee_type;
217 CompilerType this_type(valobj.GetCompilerType());
218 uint32_t type_info = this_type.GetTypeInfo(&pointee_type);
219 if (!type_info)
220 return LLDB_INVALID_ADDRESS;
221
222 // Check if it's a pointer or reference
223 bool ptr_or_ref = false;
224 if (type_info & (eTypeIsPointer | eTypeIsReference)) {
225 ptr_or_ref = true;
226 type_info = pointee_type.GetTypeInfo();
227 }
228
229 // We process only C++ classes
230 const uint32_t cpp_class = eTypeIsClass | eTypeIsCPlusPlus;
231 if ((type_info & cpp_class) != cpp_class)
232 return LLDB_INVALID_ADDRESS;
233
234 // Calculate offset to VTable pointer
235 lldb::offset_t vbtable_ptr_offset =
236 vtable_ctx.isMicrosoft() ? record_layout.getVBPtrOffset().getQuantity()
237 : 0;
238
239 if (ptr_or_ref) {
240 // We have a pointer / ref to object, so read
241 // VTable pointer from process memory
242
243 if (valobj.GetAddressTypeOfChildren() != eAddressTypeLoad)
244 return LLDB_INVALID_ADDRESS;
245
246 auto vbtable_ptr_addr = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
247 if (vbtable_ptr_addr == LLDB_INVALID_ADDRESS)
248 return LLDB_INVALID_ADDRESS;
249
250 vbtable_ptr_addr += vbtable_ptr_offset;
251
252 Status err;
253 return process.ReadPointerFromMemory(vbtable_ptr_addr, err);
254 }
255
256 // We have an object already read from process memory,
257 // so just extract VTable pointer from it
258
259 DataExtractor data;
260 Status err;
261 auto size = valobj.GetData(data, err);
262 if (err.Fail() || vbtable_ptr_offset + data.GetAddressByteSize() > size)
263 return LLDB_INVALID_ADDRESS;
264
265 return data.GetPointer(&vbtable_ptr_offset);
266}
267
268static int64_t ReadVBaseOffsetFromVTable(Process &process,
269 VTableContextBase &vtable_ctx,
270 lldb::addr_t vtable_ptr,
271 const CXXRecordDecl *cxx_record_decl,
272 const CXXRecordDecl *base_class_decl) {
273 if (vtable_ctx.isMicrosoft()) {
274 clang::MicrosoftVTableContext &msoft_vtable_ctx =
275 static_cast<clang::MicrosoftVTableContext &>(vtable_ctx);
276
277 // Get the index into the virtual base table. The
278 // index is the index in uint32_t from vbtable_ptr
279 const unsigned vbtable_index =
280 msoft_vtable_ctx.getVBTableIndex(cxx_record_decl, base_class_decl);
281 const lldb::addr_t base_offset_addr = vtable_ptr + vbtable_index * 4;
282 Status err;
283 return process.ReadSignedIntegerFromMemory(base_offset_addr, 4, INT64_MAX,
284 err);
285 }
286
287 clang::ItaniumVTableContext &itanium_vtable_ctx =
288 static_cast<clang::ItaniumVTableContext &>(vtable_ctx);
289
290 clang::CharUnits base_offset_offset =
291 itanium_vtable_ctx.getVirtualBaseOffsetOffset(cxx_record_decl,
292 base_class_decl);
293 const lldb::addr_t base_offset_addr =
294 vtable_ptr + base_offset_offset.getQuantity();
295 const uint32_t base_offset_size = process.GetAddressByteSize();
296 Status err;
297 return process.ReadSignedIntegerFromMemory(base_offset_addr, base_offset_size,
298 INT64_MAX, err);
299}
300
301static bool GetVBaseBitOffset(VTableContextBase &vtable_ctx,
302 ValueObject &valobj,
303 const ASTRecordLayout &record_layout,
304 const CXXRecordDecl *cxx_record_decl,
305 const CXXRecordDecl *base_class_decl,
306 int32_t &bit_offset) {
307 ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
308 Process *process = exe_ctx.GetProcessPtr();
309 if (!process)
310 return false;
311
312 lldb::addr_t vtable_ptr =
313 GetVTableAddress(*process, vtable_ctx, valobj, record_layout);
314 if (vtable_ptr == LLDB_INVALID_ADDRESS)
315 return false;
316
317 auto base_offset = ReadVBaseOffsetFromVTable(
318 *process, vtable_ctx, vtable_ptr, cxx_record_decl, base_class_decl);
319 if (base_offset == INT64_MAX)
320 return false;
321
322 bit_offset = base_offset * 8;
323
324 return true;
325}
326
Kate Stoneb9c1b512016-09-06 20:57:50 +0000327typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext *>
328 ClangASTMap;
Enrico Granata5d84a692014-08-19 21:46:37 +0000329
Kate Stoneb9c1b512016-09-06 20:57:50 +0000330static ClangASTMap &GetASTMap() {
331 static ClangASTMap *g_map_ptr = nullptr;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000332 static llvm::once_flag g_once_flag;
333 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000334 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
335 });
336 return *g_map_ptr;
Enrico Granata5d84a692014-08-19 21:46:37 +0000337}
338
Davide Italiano7e3ef4d2018-03-20 19:46:32 +0000339bool ClangASTContext::IsOperator(const char *name,
340 clang::OverloadedOperatorKind &op_kind) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341 if (name == nullptr || name[0] == '\0')
342 return false;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000343
344#define OPERATOR_PREFIX "operator"
345#define OPERATOR_PREFIX_LENGTH (sizeof(OPERATOR_PREFIX) - 1)
346
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347 const char *post_op_name = nullptr;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000348
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349 bool no_space = true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000350
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
352 return false;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000353
Kate Stoneb9c1b512016-09-06 20:57:50 +0000354 post_op_name = name + OPERATOR_PREFIX_LENGTH;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000355
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356 if (post_op_name[0] == ' ') {
357 post_op_name++;
358 no_space = false;
359 }
Pavel Labath1ac2b202016-08-15 14:32:32 +0000360
361#undef OPERATOR_PREFIX
362#undef OPERATOR_PREFIX_LENGTH
363
Adrian Prantl05097242018-04-30 16:49:04 +0000364 // This is an operator, set the overloaded operator kind to invalid in case
365 // this is a conversion operator...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366 op_kind = clang::NUM_OVERLOADED_OPERATORS;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000367
Kate Stoneb9c1b512016-09-06 20:57:50 +0000368 switch (post_op_name[0]) {
369 default:
370 if (no_space)
371 return false;
372 break;
373 case 'n':
374 if (no_space)
375 return false;
376 if (strcmp(post_op_name, "new") == 0)
377 op_kind = clang::OO_New;
378 else if (strcmp(post_op_name, "new[]") == 0)
379 op_kind = clang::OO_Array_New;
380 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000381
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382 case 'd':
383 if (no_space)
384 return false;
385 if (strcmp(post_op_name, "delete") == 0)
386 op_kind = clang::OO_Delete;
387 else if (strcmp(post_op_name, "delete[]") == 0)
388 op_kind = clang::OO_Array_Delete;
389 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000390
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391 case '+':
392 if (post_op_name[1] == '\0')
393 op_kind = clang::OO_Plus;
394 else if (post_op_name[2] == '\0') {
395 if (post_op_name[1] == '=')
396 op_kind = clang::OO_PlusEqual;
397 else if (post_op_name[1] == '+')
398 op_kind = clang::OO_PlusPlus;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000399 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000401
Kate Stoneb9c1b512016-09-06 20:57:50 +0000402 case '-':
403 if (post_op_name[1] == '\0')
404 op_kind = clang::OO_Minus;
405 else if (post_op_name[2] == '\0') {
406 switch (post_op_name[1]) {
407 case '=':
408 op_kind = clang::OO_MinusEqual;
409 break;
410 case '-':
411 op_kind = clang::OO_MinusMinus;
412 break;
413 case '>':
414 op_kind = clang::OO_Arrow;
415 break;
416 }
417 } else if (post_op_name[3] == '\0') {
418 if (post_op_name[2] == '*')
419 op_kind = clang::OO_ArrowStar;
420 break;
421 }
422 break;
423
424 case '*':
425 if (post_op_name[1] == '\0')
426 op_kind = clang::OO_Star;
427 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
428 op_kind = clang::OO_StarEqual;
429 break;
430
431 case '/':
432 if (post_op_name[1] == '\0')
433 op_kind = clang::OO_Slash;
434 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
435 op_kind = clang::OO_SlashEqual;
436 break;
437
438 case '%':
439 if (post_op_name[1] == '\0')
440 op_kind = clang::OO_Percent;
441 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
442 op_kind = clang::OO_PercentEqual;
443 break;
444
445 case '^':
446 if (post_op_name[1] == '\0')
447 op_kind = clang::OO_Caret;
448 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
449 op_kind = clang::OO_CaretEqual;
450 break;
451
452 case '&':
453 if (post_op_name[1] == '\0')
454 op_kind = clang::OO_Amp;
455 else if (post_op_name[2] == '\0') {
456 switch (post_op_name[1]) {
457 case '=':
458 op_kind = clang::OO_AmpEqual;
459 break;
460 case '&':
461 op_kind = clang::OO_AmpAmp;
462 break;
463 }
464 }
465 break;
466
467 case '|':
468 if (post_op_name[1] == '\0')
469 op_kind = clang::OO_Pipe;
470 else if (post_op_name[2] == '\0') {
471 switch (post_op_name[1]) {
472 case '=':
473 op_kind = clang::OO_PipeEqual;
474 break;
475 case '|':
476 op_kind = clang::OO_PipePipe;
477 break;
478 }
479 }
480 break;
481
482 case '~':
483 if (post_op_name[1] == '\0')
484 op_kind = clang::OO_Tilde;
485 break;
486
487 case '!':
488 if (post_op_name[1] == '\0')
489 op_kind = clang::OO_Exclaim;
490 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
491 op_kind = clang::OO_ExclaimEqual;
492 break;
493
494 case '=':
495 if (post_op_name[1] == '\0')
496 op_kind = clang::OO_Equal;
497 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
498 op_kind = clang::OO_EqualEqual;
499 break;
500
501 case '<':
502 if (post_op_name[1] == '\0')
503 op_kind = clang::OO_Less;
504 else if (post_op_name[2] == '\0') {
505 switch (post_op_name[1]) {
506 case '<':
507 op_kind = clang::OO_LessLess;
508 break;
509 case '=':
510 op_kind = clang::OO_LessEqual;
511 break;
512 }
513 } else if (post_op_name[3] == '\0') {
514 if (post_op_name[2] == '=')
515 op_kind = clang::OO_LessLessEqual;
516 }
517 break;
518
519 case '>':
520 if (post_op_name[1] == '\0')
521 op_kind = clang::OO_Greater;
522 else if (post_op_name[2] == '\0') {
523 switch (post_op_name[1]) {
524 case '>':
525 op_kind = clang::OO_GreaterGreater;
526 break;
527 case '=':
528 op_kind = clang::OO_GreaterEqual;
529 break;
530 }
531 } else if (post_op_name[1] == '>' && post_op_name[2] == '=' &&
532 post_op_name[3] == '\0') {
533 op_kind = clang::OO_GreaterGreaterEqual;
534 }
535 break;
536
537 case ',':
538 if (post_op_name[1] == '\0')
539 op_kind = clang::OO_Comma;
540 break;
541
542 case '(':
543 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
544 op_kind = clang::OO_Call;
545 break;
546
547 case '[':
548 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
549 op_kind = clang::OO_Subscript;
550 break;
551 }
552
553 return true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000554}
Enrico Granata5d84a692014-08-19 21:46:37 +0000555
Greg Clayton57ee3062013-07-11 22:46:58 +0000556clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +0000557ClangASTContext::ConvertAccessTypeToAccessSpecifier(AccessType access) {
558 switch (access) {
559 default:
560 break;
561 case eAccessNone:
Greg Clayton8cf05932010-07-22 18:30:50 +0000562 return AS_none;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000563 case eAccessPublic:
564 return AS_public;
565 case eAccessPrivate:
566 return AS_private;
567 case eAccessProtected:
568 return AS_protected;
569 }
570 return AS_none;
Greg Clayton8cf05932010-07-22 18:30:50 +0000571}
572
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) {
574 // FIXME: Cleanup per-file based stuff.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000575
Adrian Prantl05097242018-04-30 16:49:04 +0000576 // Set some properties which depend solely on the input kind; it would be
577 // nice to move these to the language standard, and have the driver resolve
578 // the input kind + language standard.
Rainer Orth6ca17072019-08-05 14:00:43 +0000579 if (IK.getLanguage() == clang::Language::Asm) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000580 Opts.AsmPreprocessor = 1;
Richard Smith8186cd42017-04-26 22:10:53 +0000581 } else if (IK.isObjectiveC()) {
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000582 Opts.ObjC = 1;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000583 }
584
585 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
586
587 if (LangStd == LangStandard::lang_unspecified) {
588 // Based on the base language, pick one.
Richard Smith8186cd42017-04-26 22:10:53 +0000589 switch (IK.getLanguage()) {
Rainer Orth6ca17072019-08-05 14:00:43 +0000590 case clang::Language::Unknown:
591 case clang::Language::LLVM_IR:
592 case clang::Language::RenderScript:
David Blaikiea322f362017-01-06 00:38:06 +0000593 llvm_unreachable("Invalid input kind!");
Rainer Orth6ca17072019-08-05 14:00:43 +0000594 case clang::Language::OpenCL:
Pavel Labath47168542017-04-27 08:49:19 +0000595 LangStd = LangStandard::lang_opencl10;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000596 break;
Rainer Orth6ca17072019-08-05 14:00:43 +0000597 case clang::Language::CUDA:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000598 LangStd = LangStandard::lang_cuda;
599 break;
Rainer Orth6ca17072019-08-05 14:00:43 +0000600 case clang::Language::Asm:
601 case clang::Language::C:
602 case clang::Language::ObjC:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000603 LangStd = LangStandard::lang_gnu99;
604 break;
Rainer Orth6ca17072019-08-05 14:00:43 +0000605 case clang::Language::CXX:
606 case clang::Language::ObjCXX:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000607 LangStd = LangStandard::lang_gnucxx98;
608 break;
Rainer Orth6ca17072019-08-05 14:00:43 +0000609 case clang::Language::HIP:
Benjamin Kramer0d97c222018-04-25 13:22:47 +0000610 LangStd = LangStandard::lang_hip;
611 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000612 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000613 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000614
Kate Stoneb9c1b512016-09-06 20:57:50 +0000615 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
616 Opts.LineComment = Std.hasLineComments();
617 Opts.C99 = Std.isC99();
618 Opts.CPlusPlus = Std.isCPlusPlus();
619 Opts.CPlusPlus11 = Std.isCPlusPlus11();
620 Opts.Digraphs = Std.hasDigraphs();
621 Opts.GNUMode = Std.isGNUMode();
622 Opts.GNUInline = !Std.isC99();
623 Opts.HexFloats = Std.hasHexFloats();
624 Opts.ImplicitInt = Std.hasImplicitInt();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000625
Kate Stoneb9c1b512016-09-06 20:57:50 +0000626 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000627
Kate Stoneb9c1b512016-09-06 20:57:50 +0000628 // OpenCL has some additional defaults.
Pavel Labath47168542017-04-27 08:49:19 +0000629 if (LangStd == LangStandard::lang_opencl10) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000630 Opts.OpenCL = 1;
631 Opts.AltiVec = 1;
632 Opts.CXXOperatorNames = 1;
633 Opts.LaxVectorConversions = 1;
634 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635
Kate Stoneb9c1b512016-09-06 20:57:50 +0000636 // OpenCL and C++ both have bool, true, false keywords.
637 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638
Kate Stoneb9c1b512016-09-06 20:57:50 +0000639 Opts.setValueVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000640
Adrian Prantl05097242018-04-30 16:49:04 +0000641 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs is
642 // specified, or -std is set to a conforming mode.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000643 Opts.Trigraphs = !Opts.GNUMode;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000644 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000645 Opts.OptimizeSize = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000646
Kate Stoneb9c1b512016-09-06 20:57:50 +0000647 // FIXME: Eliminate this dependency.
648 // unsigned Opt =
649 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
650 // Opts.Optimize = Opt != 0;
651 unsigned Opt = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000652
Kate Stoneb9c1b512016-09-06 20:57:50 +0000653 // This is the __NO_INLINE__ define, which just depends on things like the
654 // optimization level and -fno-inline, not actually whether the backend has
655 // inlining enabled.
656 //
657 // FIXME: This is affected by other options (-fno-inline).
658 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000659}
660
Kate Stoneb9c1b512016-09-06 20:57:50 +0000661ClangASTContext::ClangASTContext(const char *target_triple)
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000662 : TypeSystem(TypeSystem::eKindClang), m_target_triple(), m_ast_up(),
663 m_language_options_up(), m_source_manager_up(), m_diagnostics_engine_up(),
664 m_target_options_rp(), m_target_info_up(), m_identifier_table_up(),
665 m_selector_table_up(), m_builtins_up(), m_callback_tag_decl(nullptr),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000666 m_callback_objc_decl(nullptr), m_callback_baton(nullptr),
667 m_pointer_byte_size(0), m_ast_owned(false) {
668 if (target_triple && target_triple[0])
669 SetTargetTriple(target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000670}
671
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000672// Destructor
Kate Stoneb9c1b512016-09-06 20:57:50 +0000673ClangASTContext::~ClangASTContext() { Finalize(); }
674
675ConstString ClangASTContext::GetPluginNameStatic() {
676 return ConstString("clang");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000677}
678
Kate Stoneb9c1b512016-09-06 20:57:50 +0000679ConstString ClangASTContext::GetPluginName() {
680 return ClangASTContext::GetPluginNameStatic();
Greg Clayton56939cb2015-09-17 22:23:34 +0000681}
682
Kate Stoneb9c1b512016-09-06 20:57:50 +0000683uint32_t ClangASTContext::GetPluginVersion() { return 1; }
Greg Clayton56939cb2015-09-17 22:23:34 +0000684
Kate Stoneb9c1b512016-09-06 20:57:50 +0000685lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
686 lldb_private::Module *module,
687 Target *target) {
688 if (ClangASTContextSupportsLanguage(language)) {
689 ArchSpec arch;
690 if (module)
691 arch = module->GetArchitecture();
692 else if (target)
693 arch = target->GetArchitecture();
Greg Clayton56939cb2015-09-17 22:23:34 +0000694
Kate Stoneb9c1b512016-09-06 20:57:50 +0000695 if (arch.IsValid()) {
696 ArchSpec fixed_arch = arch;
697 // LLVM wants this to be set to iOS or MacOSX; if we're working on
698 // a bare-boards type image, change the triple for llvm's benefit.
699 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
700 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) {
701 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
702 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
703 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) {
704 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
705 } else {
706 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
Greg Clayton56939cb2015-09-17 22:23:34 +0000707 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000708 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000709
Kate Stoneb9c1b512016-09-06 20:57:50 +0000710 if (module) {
711 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
712 if (ast_sp) {
713 ast_sp->SetArchitecture(fixed_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000714 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000715 return ast_sp;
716 } else if (target && target->IsValid()) {
717 std::shared_ptr<ClangASTContextForExpressions> ast_sp(
718 new ClangASTContextForExpressions(*target));
719 if (ast_sp) {
720 ast_sp->SetArchitecture(fixed_arch);
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000721 ast_sp->m_scratch_ast_source_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000722 new ClangASTSource(target->shared_from_this()));
Sean Callanan68e44232017-09-28 20:20:25 +0000723 lldbassert(ast_sp->getFileManager());
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000724 ast_sp->m_scratch_ast_source_up->InstallASTContext(
Sean Callanan68e44232017-09-28 20:20:25 +0000725 *ast_sp->getASTContext(), *ast_sp->getFileManager(), true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000726 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000727 ast_sp->m_scratch_ast_source_up->CreateProxy());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000728 ast_sp->SetExternalSource(proxy_ast_source);
729 return ast_sp;
730 }
731 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000732 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000733 }
734 return lldb::TypeSystemSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000735}
736
Adrian Prantlaa97a892019-08-22 21:45:58 +0000737LanguageSet ClangASTContext::GetSupportedLanguagesForTypes() {
738 LanguageSet languages;
739 languages.Insert(lldb::eLanguageTypeC89);
740 languages.Insert(lldb::eLanguageTypeC);
741 languages.Insert(lldb::eLanguageTypeC11);
742 languages.Insert(lldb::eLanguageTypeC_plus_plus);
743 languages.Insert(lldb::eLanguageTypeC99);
744 languages.Insert(lldb::eLanguageTypeObjC);
745 languages.Insert(lldb::eLanguageTypeObjC_plus_plus);
746 languages.Insert(lldb::eLanguageTypeC_plus_plus_03);
747 languages.Insert(lldb::eLanguageTypeC_plus_plus_11);
748 languages.Insert(lldb::eLanguageTypeC11);
749 languages.Insert(lldb::eLanguageTypeC_plus_plus_14);
750 return languages;
751}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000752
Adrian Prantlaa97a892019-08-22 21:45:58 +0000753LanguageSet ClangASTContext::GetSupportedLanguagesForExpressions() {
754 LanguageSet languages;
755 languages.Insert(lldb::eLanguageTypeC_plus_plus);
756 languages.Insert(lldb::eLanguageTypeObjC_plus_plus);
757 languages.Insert(lldb::eLanguageTypeC_plus_plus_03);
758 languages.Insert(lldb::eLanguageTypeC_plus_plus_11);
759 languages.Insert(lldb::eLanguageTypeC_plus_plus_14);
760 return languages;
Enrico Granata5d84a692014-08-19 21:46:37 +0000761}
762
Kate Stoneb9c1b512016-09-06 20:57:50 +0000763void ClangASTContext::Initialize() {
Adrian Prantlaa97a892019-08-22 21:45:58 +0000764 PluginManager::RegisterPlugin(
765 GetPluginNameStatic(), "clang base AST context plug-in", CreateInstance,
766 GetSupportedLanguagesForTypes(), GetSupportedLanguagesForExpressions());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000767}
768
Kate Stoneb9c1b512016-09-06 20:57:50 +0000769void ClangASTContext::Terminate() {
770 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000771}
772
Kate Stoneb9c1b512016-09-06 20:57:50 +0000773void ClangASTContext::Finalize() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000774 if (m_ast_up) {
775 GetASTMap().Erase(m_ast_up.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000776 if (!m_ast_owned)
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000777 m_ast_up.release();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000778 }
779
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000780 m_builtins_up.reset();
781 m_selector_table_up.reset();
782 m_identifier_table_up.reset();
783 m_target_info_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000784 m_target_options_rp.reset();
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000785 m_diagnostics_engine_up.reset();
786 m_source_manager_up.reset();
787 m_language_options_up.reset();
788 m_ast_up.reset();
789 m_scratch_ast_source_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000790}
791
792void ClangASTContext::Clear() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000793 m_ast_up.reset();
794 m_language_options_up.reset();
795 m_source_manager_up.reset();
796 m_diagnostics_engine_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000797 m_target_options_rp.reset();
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000798 m_target_info_up.reset();
799 m_identifier_table_up.reset();
800 m_selector_table_up.reset();
801 m_builtins_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000802 m_pointer_byte_size = 0;
803}
804
Raphael Isemannf74a4c12019-04-30 08:41:35 +0000805void ClangASTContext::setSema(Sema *s) {
806 // Ensure that the new sema actually belongs to our ASTContext.
807 assert(s == nullptr || &s->getASTContext() == m_ast_up.get());
808 m_sema = s;
809}
810
Kate Stoneb9c1b512016-09-06 20:57:50 +0000811const char *ClangASTContext::GetTargetTriple() {
812 return m_target_triple.c_str();
813}
814
815void ClangASTContext::SetTargetTriple(const char *target_triple) {
816 Clear();
817 m_target_triple.assign(target_triple);
818}
819
820void ClangASTContext::SetArchitecture(const ArchSpec &arch) {
821 SetTargetTriple(arch.GetTriple().str().c_str());
822}
823
824bool ClangASTContext::HasExternalSource() {
825 ASTContext *ast = getASTContext();
826 if (ast)
827 return ast->getExternalSource() != nullptr;
828 return false;
829}
830
831void ClangASTContext::SetExternalSource(
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000832 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000833 ASTContext *ast = getASTContext();
834 if (ast) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000835 ast->setExternalSource(ast_source_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000836 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000837 }
838}
839
840void ClangASTContext::RemoveExternalSource() {
841 ASTContext *ast = getASTContext();
842
843 if (ast) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000844 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_up;
845 ast->setExternalSource(empty_ast_source_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000846 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000847 }
848}
849
850void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) {
851 if (!m_ast_owned) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000852 m_ast_up.release();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000853 }
854 m_ast_owned = false;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000855 m_ast_up.reset(ast_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000856 GetASTMap().Insert(ast_ctx, this);
857}
858
859ASTContext *ClangASTContext::getASTContext() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000860 if (m_ast_up == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000861 m_ast_owned = true;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000862 m_ast_up.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000863 *getIdentifierTable(), *getSelectorTable(),
864 *getBuiltinContext()));
865
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000866 m_ast_up->getDiagnostics().setClient(getDiagnosticConsumer(), false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000867
868 // This can be NULL if we don't know anything about the architecture or if
Adrian Prantl05097242018-04-30 16:49:04 +0000869 // the target for an architecture isn't enabled in the llvm/clang that we
870 // built
Kate Stoneb9c1b512016-09-06 20:57:50 +0000871 TargetInfo *target_info = getTargetInfo();
872 if (target_info)
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000873 m_ast_up->InitBuiltinTypes(*target_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000874
875 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000876 m_ast_up->getTranslationUnitDecl()->setHasExternalLexicalStorage();
877 // m_ast_up->getTranslationUnitDecl()->setHasExternalVisibleStorage();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000878 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000879
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000880 GetASTMap().Insert(m_ast_up.get(), this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000881
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000882 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_up(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000883 new ClangExternalASTSourceCallbacks(
884 ClangASTContext::CompleteTagDecl,
885 ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
886 ClangASTContext::LayoutRecordType, this));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000887 SetExternalSource(ast_source_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000888 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000889 return m_ast_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000890}
891
Kate Stoneb9c1b512016-09-06 20:57:50 +0000892ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
893 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
894 return clang_ast;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000895}
896
Kate Stoneb9c1b512016-09-06 20:57:50 +0000897Builtin::Context *ClangASTContext::getBuiltinContext() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000898 if (m_builtins_up == nullptr)
899 m_builtins_up.reset(new Builtin::Context());
900 return m_builtins_up.get();
Sean Callanan79439e82010-11-18 02:56:27 +0000901}
902
Kate Stoneb9c1b512016-09-06 20:57:50 +0000903IdentifierTable *ClangASTContext::getIdentifierTable() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000904 if (m_identifier_table_up == nullptr)
905 m_identifier_table_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000906 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000907 return m_identifier_table_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000908}
909
Kate Stoneb9c1b512016-09-06 20:57:50 +0000910LangOptions *ClangASTContext::getLanguageOptions() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000911 if (m_language_options_up == nullptr) {
912 m_language_options_up.reset(new LangOptions());
Rainer Orth6ca17072019-08-05 14:00:43 +0000913 ParseLangArgs(*m_language_options_up, clang::Language::ObjCXX,
914 GetTargetTriple());
915 // InitializeLangOptions(*m_language_options_up, Language::ObjCXX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000916 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000917 return m_language_options_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000918}
919
Kate Stoneb9c1b512016-09-06 20:57:50 +0000920SelectorTable *ClangASTContext::getSelectorTable() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000921 if (m_selector_table_up == nullptr)
922 m_selector_table_up.reset(new SelectorTable());
923 return m_selector_table_up.get();
Greg Claytonfe689042015-11-10 17:47:04 +0000924}
925
Kate Stoneb9c1b512016-09-06 20:57:50 +0000926clang::FileManager *ClangASTContext::getFileManager() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000927 if (m_file_manager_up == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000928 clang::FileSystemOptions file_system_options;
Jonas Devlieghere9764b652019-02-18 20:31:18 +0000929 m_file_manager_up.reset(new clang::FileManager(
930 file_system_options, FileSystem::Instance().GetVirtualFileSystem()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000931 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000932 return m_file_manager_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000933}
934
935clang::SourceManager *ClangASTContext::getSourceManager() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000936 if (m_source_manager_up == nullptr)
937 m_source_manager_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000938 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000939 return m_source_manager_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000940}
941
942clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000943 if (m_diagnostics_engine_up == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000944 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000945 m_diagnostics_engine_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000946 new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
947 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000948 return m_diagnostics_engine_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000949}
950
951clang::MangleContext *ClangASTContext::getMangleContext() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000952 if (m_mangle_ctx_up == nullptr)
953 m_mangle_ctx_up.reset(getASTContext()->createMangleContext());
954 return m_mangle_ctx_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000955}
956
957class NullDiagnosticConsumer : public DiagnosticConsumer {
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000958public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000959 NullDiagnosticConsumer() {
960 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
961 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000962
Kate Stoneb9c1b512016-09-06 20:57:50 +0000963 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
Raphael Isemann17566302019-05-03 10:03:28 +0000964 const clang::Diagnostic &info) override {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000965 if (m_log) {
966 llvm::SmallVector<char, 32> diag_str(10);
967 info.FormatDiagnostic(diag_str);
968 diag_str.push_back('\0');
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000969 LLDB_LOGF(m_log, "Compiler diagnostic: %s\n", diag_str.data());
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000970 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000971 }
972
973 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
974 return new NullDiagnosticConsumer();
975 }
976
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000977private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000978 Log *m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000979};
980
Kate Stoneb9c1b512016-09-06 20:57:50 +0000981DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000982 if (m_diagnostic_consumer_up == nullptr)
983 m_diagnostic_consumer_up.reset(new NullDiagnosticConsumer);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000984
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000985 return m_diagnostic_consumer_up.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000986}
987
Kate Stoneb9c1b512016-09-06 20:57:50 +0000988std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000989 if (m_target_options_rp == nullptr && !m_target_triple.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000990 m_target_options_rp = std::make_shared<clang::TargetOptions>();
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000991 if (m_target_options_rp != nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000992 m_target_options_rp->Triple = m_target_triple;
993 }
994 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000995}
996
Kate Stoneb9c1b512016-09-06 20:57:50 +0000997TargetInfo *ClangASTContext::getTargetInfo() {
998 // target_triple should be something like "x86_64-apple-macosx"
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000999 if (m_target_info_up == nullptr && !m_target_triple.empty())
1000 m_target_info_up.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001001 getTargetOptions()));
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001002 return m_target_info_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001003}
1004
1005#pragma mark Basic Types
1006
Kate Stoneb9c1b512016-09-06 20:57:50 +00001007static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
1008 ASTContext *ast, QualType qual_type) {
1009 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001010 return qual_type_bit_size == bit_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001011}
Greg Clayton56939cb2015-09-17 22:23:34 +00001012
Greg Claytona1e5dc82015-08-11 22:53:00 +00001013CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00001014ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
1015 size_t bit_size) {
1016 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
1017 getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001018}
1019
Kate Stoneb9c1b512016-09-06 20:57:50 +00001020CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
1021 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
Alex Langfordbddab072019-08-13 19:40:36 +00001022 auto *clang_ast_context = ClangASTContext::GetASTContext(ast);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001023 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001024 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001025 switch (encoding) {
1026 case eEncodingInvalid:
1027 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001028 return CompilerType(clang_ast_context, ast->VoidPtrTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001029 break;
1030
1031 case eEncodingUint:
1032 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001033 return CompilerType(clang_ast_context,
1034 ast->UnsignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001035 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001036 return CompilerType(clang_ast_context,
1037 ast->UnsignedShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001038 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001039 return CompilerType(clang_ast_context,
1040 ast->UnsignedIntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001041 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001042 return CompilerType(clang_ast_context,
1043 ast->UnsignedLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001044 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001045 return CompilerType(clang_ast_context,
1046 ast->UnsignedLongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001047 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00001048 return CompilerType(clang_ast_context,
1049 ast->UnsignedInt128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001050 break;
1051
1052 case eEncodingSint:
1053 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001054 return CompilerType(clang_ast_context,
1055 ast->SignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001056 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001057 return CompilerType(clang_ast_context, ast->ShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001058 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001059 return CompilerType(clang_ast_context, ast->IntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001060 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001061 return CompilerType(clang_ast_context, ast->LongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001062 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001063 return CompilerType(clang_ast_context, ast->LongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001064 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00001065 return CompilerType(clang_ast_context, ast->Int128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001066 break;
1067
1068 case eEncodingIEEE754:
1069 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001070 return CompilerType(clang_ast_context, ast->FloatTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001071 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001072 return CompilerType(clang_ast_context, ast->DoubleTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001073 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001074 return CompilerType(clang_ast_context,
1075 ast->LongDoubleTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001076 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001077 return CompilerType(clang_ast_context, ast->HalfTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001078 break;
1079
1080 case eEncodingVector:
1081 // Sanity check that bit_size is a multiple of 8's.
1082 if (bit_size && !(bit_size & 0x7u))
1083 return CompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00001084 clang_ast_context,
1085 ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8)
1086 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001087 break;
1088 }
1089
1090 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001091}
1092
Greg Clayton57ee3062013-07-11 22:46:58 +00001093lldb::BasicType
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001094ClangASTContext::GetBasicTypeEnumeration(ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001095 if (name) {
1096 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
1097 static TypeNameToBasicTypeMap g_type_map;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +00001098 static llvm::once_flag g_once_flag;
1099 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001100 // "void"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001101 g_type_map.Append(ConstString("void"), eBasicTypeVoid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102
1103 // "char"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001104 g_type_map.Append(ConstString("char"), eBasicTypeChar);
1105 g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar);
1106 g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar);
1107 g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar);
1108 g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar);
1109 g_type_map.Append(ConstString("unsigned wchar_t"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001110 eBasicTypeUnsignedWChar);
1111 // "short"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001112 g_type_map.Append(ConstString("short"), eBasicTypeShort);
1113 g_type_map.Append(ConstString("short int"), eBasicTypeShort);
1114 g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort);
1115 g_type_map.Append(ConstString("unsigned short int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001116 eBasicTypeUnsignedShort);
1117
1118 // "int"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001119 g_type_map.Append(ConstString("int"), eBasicTypeInt);
1120 g_type_map.Append(ConstString("signed int"), eBasicTypeInt);
1121 g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt);
1122 g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001123
1124 // "long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001125 g_type_map.Append(ConstString("long"), eBasicTypeLong);
1126 g_type_map.Append(ConstString("long int"), eBasicTypeLong);
1127 g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong);
1128 g_type_map.Append(ConstString("unsigned long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001129 eBasicTypeUnsignedLong);
1130
1131 // "long long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001132 g_type_map.Append(ConstString("long long"), eBasicTypeLongLong);
1133 g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong);
1134 g_type_map.Append(ConstString("unsigned long long"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001135 eBasicTypeUnsignedLongLong);
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001136 g_type_map.Append(ConstString("unsigned long long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001137 eBasicTypeUnsignedLongLong);
1138
1139 // "int128"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001140 g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128);
1141 g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001142
1143 // Miscellaneous
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001144 g_type_map.Append(ConstString("bool"), eBasicTypeBool);
1145 g_type_map.Append(ConstString("float"), eBasicTypeFloat);
1146 g_type_map.Append(ConstString("double"), eBasicTypeDouble);
1147 g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble);
1148 g_type_map.Append(ConstString("id"), eBasicTypeObjCID);
1149 g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel);
1150 g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001151 g_type_map.Sort();
1152 });
1153
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001154 return g_type_map.Find(name, eBasicTypeInvalid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001155 }
1156 return eBasicTypeInvalid;
Greg Clayton57ee3062013-07-11 22:46:58 +00001157}
1158
Kate Stoneb9c1b512016-09-06 20:57:50 +00001159CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001160 ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001161 if (ast) {
1162 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
1163 return ClangASTContext::GetBasicType(ast, basic_type);
1164 }
1165 return CompilerType();
1166}
1167
1168uint32_t ClangASTContext::GetPointerByteSize() {
1169 if (m_pointer_byte_size == 0)
Adrian Prantld963a7c2019-01-15 18:07:52 +00001170 if (auto size = GetBasicType(lldb::eBasicTypeVoid)
1171 .GetPointerType()
1172 .GetByteSize(nullptr))
1173 m_pointer_byte_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001174 return m_pointer_byte_size;
1175}
1176
1177CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
1178 return GetBasicType(getASTContext(), basic_type);
1179}
1180
1181CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1182 lldb::BasicType basic_type) {
1183 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001184 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001185 lldb::opaque_compiler_type_t clang_type =
1186 GetOpaqueCompilerType(ast, basic_type);
1187
1188 if (clang_type)
1189 return CompilerType(GetASTContext(ast), clang_type);
1190 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +00001191}
1192
Kate Stoneb9c1b512016-09-06 20:57:50 +00001193CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
1194 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
1195 ASTContext *ast = getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001196
Kate Stoneb9c1b512016-09-06 20:57:50 +00001197#define streq(a, b) strcmp(a, b) == 0
1198 assert(ast != nullptr);
1199 if (ast) {
1200 switch (dw_ate) {
1201 default:
1202 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001203
Kate Stoneb9c1b512016-09-06 20:57:50 +00001204 case DW_ATE_address:
1205 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001206 return CompilerType(this, ast->VoidPtrTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001207 break;
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001208
Kate Stoneb9c1b512016-09-06 20:57:50 +00001209 case DW_ATE_boolean:
1210 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001211 return CompilerType(this, ast->BoolTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001212 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001213 return CompilerType(this, ast->UnsignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001214 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001215 return CompilerType(this, ast->UnsignedShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001216 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001217 return CompilerType(this, ast->UnsignedIntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001218 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001219
Kate Stoneb9c1b512016-09-06 20:57:50 +00001220 case DW_ATE_lo_user:
1221 // This has been seen to mean DW_AT_complex_integer
1222 if (type_name) {
1223 if (::strstr(type_name, "complex")) {
1224 CompilerType complex_int_clang_type =
1225 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1226 bit_size / 2);
Alex Langfordbddab072019-08-13 19:40:36 +00001227 return CompilerType(
1228 this, ast->getComplexType(
1229 ClangUtil::GetQualType(complex_int_clang_type))
1230 .getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001231 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001232 }
1233 break;
1234
1235 case DW_ATE_complex_float:
1236 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001237 return CompilerType(this, ast->FloatComplexTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001238 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001239 return CompilerType(this, ast->DoubleComplexTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001240 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001241 return CompilerType(this, ast->LongDoubleComplexTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001242 else {
1243 CompilerType complex_float_clang_type =
1244 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1245 bit_size / 2);
Alex Langfordbddab072019-08-13 19:40:36 +00001246 return CompilerType(
1247 this, ast->getComplexType(
1248 ClangUtil::GetQualType(complex_float_clang_type))
1249 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001250 }
1251 break;
1252
1253 case DW_ATE_float:
1254 if (streq(type_name, "float") &&
1255 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001256 return CompilerType(this, ast->FloatTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001257 if (streq(type_name, "double") &&
1258 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001259 return CompilerType(this, ast->DoubleTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001260 if (streq(type_name, "long double") &&
1261 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001262 return CompilerType(this, ast->LongDoubleTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001263 // Fall back to not requiring a name match
1264 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001265 return CompilerType(this, ast->FloatTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001266 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001267 return CompilerType(this, ast->DoubleTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001268 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001269 return CompilerType(this, ast->LongDoubleTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001270 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001271 return CompilerType(this, ast->HalfTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001272 break;
1273
1274 case DW_ATE_signed:
1275 if (type_name) {
1276 if (streq(type_name, "wchar_t") &&
1277 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1278 (getTargetInfo() &&
1279 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
Alex Langfordbddab072019-08-13 19:40:36 +00001280 return CompilerType(this, ast->WCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001281 if (streq(type_name, "void") &&
1282 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001283 return CompilerType(this, ast->VoidTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001284 if (strstr(type_name, "long long") &&
1285 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001286 return CompilerType(this, ast->LongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001287 if (strstr(type_name, "long") &&
1288 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001289 return CompilerType(this, ast->LongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001290 if (strstr(type_name, "short") &&
1291 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001292 return CompilerType(this, ast->ShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001293 if (strstr(type_name, "char")) {
1294 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001295 return CompilerType(this, ast->CharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001296 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001297 return CompilerType(this, ast->SignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001298 }
1299 if (strstr(type_name, "int")) {
1300 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001301 return CompilerType(this, ast->IntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001302 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00001303 return CompilerType(this, ast->Int128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001304 }
1305 }
1306 // We weren't able to match up a type name, just search by size
1307 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001308 return CompilerType(this, ast->CharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001309 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001310 return CompilerType(this, ast->ShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001311 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001312 return CompilerType(this, ast->IntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001313 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001314 return CompilerType(this, ast->LongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001315 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001316 return CompilerType(this, ast->LongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001317 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00001318 return CompilerType(this, ast->Int128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001319 break;
1320
1321 case DW_ATE_signed_char:
1322 if (ast->getLangOpts().CharIsSigned && type_name &&
1323 streq(type_name, "char")) {
1324 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001325 return CompilerType(this, ast->CharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001326 }
1327 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001328 return CompilerType(this, ast->SignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001329 break;
1330
1331 case DW_ATE_unsigned:
1332 if (type_name) {
1333 if (streq(type_name, "wchar_t")) {
1334 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1335 if (!(getTargetInfo() &&
1336 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
Alex Langfordbddab072019-08-13 19:40:36 +00001337 return CompilerType(this, ast->WCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001338 }
1339 }
1340 if (strstr(type_name, "long long")) {
1341 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001342 return CompilerType(this, ast->UnsignedLongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001343 } else if (strstr(type_name, "long")) {
1344 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001345 return CompilerType(this, ast->UnsignedLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001346 } else if (strstr(type_name, "short")) {
1347 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001348 return CompilerType(this, ast->UnsignedShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001349 } else if (strstr(type_name, "char")) {
1350 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001351 return CompilerType(this, ast->UnsignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001352 } else if (strstr(type_name, "int")) {
1353 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001354 return CompilerType(this, ast->UnsignedIntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001355 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00001356 return CompilerType(this, ast->UnsignedInt128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001357 }
1358 }
1359 // We weren't able to match up a type name, just search by size
1360 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001361 return CompilerType(this, ast->UnsignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001362 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001363 return CompilerType(this, ast->UnsignedShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001364 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001365 return CompilerType(this, ast->UnsignedIntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001366 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001367 return CompilerType(this, ast->UnsignedLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001368 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001369 return CompilerType(this, ast->UnsignedLongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001370 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00001371 return CompilerType(this, ast->UnsignedInt128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001372 break;
1373
1374 case DW_ATE_unsigned_char:
1375 if (!ast->getLangOpts().CharIsSigned && type_name &&
1376 streq(type_name, "char")) {
1377 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001378 return CompilerType(this, ast->CharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001379 }
1380 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001381 return CompilerType(this, ast->UnsignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001382 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001383 return CompilerType(this, ast->UnsignedShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001384 break;
1385
1386 case DW_ATE_imaginary_float:
1387 break;
1388
1389 case DW_ATE_UTF:
1390 if (type_name) {
Jonas Devliegherec46d39b2019-08-21 21:30:55 +00001391 if (streq(type_name, "char16_t"))
Alex Langfordbddab072019-08-13 19:40:36 +00001392 return CompilerType(this, ast->Char16Ty.getAsOpaquePtr());
Jonas Devlieghere6c9dc122019-08-23 04:11:38 +00001393 if (streq(type_name, "char32_t"))
Alex Langfordbddab072019-08-13 19:40:36 +00001394 return CompilerType(this, ast->Char32Ty.getAsOpaquePtr());
Jonas Devlieghere6c9dc122019-08-23 04:11:38 +00001395 if (streq(type_name, "char8_t"))
Jonas Devliegherec46d39b2019-08-21 21:30:55 +00001396 return CompilerType(this, ast->Char8Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001397 }
1398 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001399 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001400 }
1401 // This assert should fire for anything that we don't catch above so we know
1402 // to fix any issues we run into.
1403 if (type_name) {
1404 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1405 "DW_TAG_base_type '%s' encoded with "
1406 "DW_ATE = 0x%x, bit_size = %u\n",
1407 type_name, dw_ate, bit_size);
1408 } else {
1409 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1410 "DW_TAG_base_type encoded with "
1411 "DW_ATE = 0x%x, bit_size = %u\n",
1412 dw_ate, bit_size);
1413 }
1414 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001415}
1416
Kate Stoneb9c1b512016-09-06 20:57:50 +00001417CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) {
1418 if (ast)
Alex Langfordbddab072019-08-13 19:40:36 +00001419 return CompilerType(ClangASTContext::GetASTContext(ast),
1420 ast->UnknownAnyTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001421 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001422}
1423
Kate Stoneb9c1b512016-09-06 20:57:50 +00001424CompilerType ClangASTContext::GetCStringType(bool is_const) {
1425 ASTContext *ast = getASTContext();
1426 QualType char_type(ast->CharTy);
1427
1428 if (is_const)
1429 char_type.addConst();
1430
Alex Langfordbddab072019-08-13 19:40:36 +00001431 return CompilerType(this, ast->getPointerType(char_type).getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001432}
1433
Zachary Turner115209e2018-11-05 19:25:39 +00001434clang::DeclContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001435ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1436 return ast->getTranslationUnitDecl();
Sean Callanan09ab4b72011-11-30 22:11:59 +00001437}
1438
Kate Stoneb9c1b512016-09-06 20:57:50 +00001439clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1440 clang::Decl *source_decl) {
1441 FileSystemOptions file_system_options;
1442 FileManager file_manager(file_system_options);
1443 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1444
Gabor Marton5ac6d492019-05-15 10:29:48 +00001445 if (llvm::Expected<clang::Decl *> ret_or_error =
1446 importer.Import(source_decl)) {
1447 return *ret_or_error;
1448 } else {
1449 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
1450 LLDB_LOG_ERROR(log, ret_or_error.takeError(), "Couldn't import decl: {0}");
1451 return nullptr;
1452 }
Greg Clayton526e5af2010-11-13 03:52:47 +00001453}
1454
Kate Stoneb9c1b512016-09-06 20:57:50 +00001455bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1456 bool ignore_qualifiers) {
1457 ClangASTContext *ast =
1458 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1459 if (!ast || ast != type2.GetTypeSystem())
1460 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001461
Kate Stoneb9c1b512016-09-06 20:57:50 +00001462 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1463 return true;
Greg Clayton55995eb2012-04-06 17:38:55 +00001464
Kate Stoneb9c1b512016-09-06 20:57:50 +00001465 QualType type1_qual = ClangUtil::GetQualType(type1);
1466 QualType type2_qual = ClangUtil::GetQualType(type2);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001467
Kate Stoneb9c1b512016-09-06 20:57:50 +00001468 if (ignore_qualifiers) {
1469 type1_qual = type1_qual.getUnqualifiedType();
1470 type2_qual = type2_qual.getUnqualifiedType();
1471 }
1472
1473 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001474}
1475
Alex Langfordcb68bd72019-08-23 06:11:32 +00001476CompilerType ClangASTContext::GetTypeForDecl(void *opaque_decl) {
1477 if (!opaque_decl)
1478 return CompilerType();
1479
1480 clang::Decl *decl = static_cast<clang::Decl *>(opaque_decl);
1481 if (auto *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl))
1482 return GetTypeForDecl(named_decl);
1483 return CompilerType();
1484}
1485
Kate Stoneb9c1b512016-09-06 20:57:50 +00001486CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1487 if (clang::ObjCInterfaceDecl *interface_decl =
1488 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1489 return GetTypeForDecl(interface_decl);
1490 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1491 return GetTypeForDecl(tag_decl);
1492 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001493}
1494
Kate Stoneb9c1b512016-09-06 20:57:50 +00001495CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001496 // No need to call the getASTContext() accessor (which can create the AST if
1497 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001498 // AST if our AST didn't already exist...
1499 ASTContext *ast = &decl->getASTContext();
1500 if (ast)
Alex Langfordbddab072019-08-13 19:40:36 +00001501 return CompilerType(ClangASTContext::GetASTContext(ast),
1502 ast->getTagDeclType(decl).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001503 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001504}
1505
Kate Stoneb9c1b512016-09-06 20:57:50 +00001506CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001507 // No need to call the getASTContext() accessor (which can create the AST if
1508 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001509 // AST if our AST didn't already exist...
1510 ASTContext *ast = &decl->getASTContext();
1511 if (ast)
Alex Langfordbddab072019-08-13 19:40:36 +00001512 return CompilerType(ClangASTContext::GetASTContext(ast),
1513 ast->getObjCInterfaceType(decl).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001514 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001515}
1516
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001517#pragma mark Structure, Unions, Classes
1518
Kate Stoneb9c1b512016-09-06 20:57:50 +00001519CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx,
1520 AccessType access_type,
1521 const char *name, int kind,
1522 LanguageType language,
1523 ClangASTMetadata *metadata) {
1524 ASTContext *ast = getASTContext();
1525 assert(ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001526
Kate Stoneb9c1b512016-09-06 20:57:50 +00001527 if (decl_ctx == nullptr)
1528 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton9e409562010-07-28 02:04:09 +00001529
Kate Stoneb9c1b512016-09-06 20:57:50 +00001530 if (language == eLanguageTypeObjC ||
1531 language == eLanguageTypeObjC_plus_plus) {
1532 bool isForwardDecl = true;
1533 bool isInternal = false;
1534 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1535 }
Greg Clayton9e409562010-07-28 02:04:09 +00001536
Kate Stoneb9c1b512016-09-06 20:57:50 +00001537 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
Adrian Prantl05097242018-04-30 16:49:04 +00001538 // we will need to update this code. I was told to currently always use the
1539 // CXXRecordDecl class since we often don't know from debug information if
1540 // something is struct or a class, so we default to always use the more
Kate Stoneb9c1b512016-09-06 20:57:50 +00001541 // complete definition just in case.
Greg Claytonc4ffd662013-03-08 01:37:30 +00001542
Shafik Yaghmour62abe492019-08-14 22:30:29 +00001543 bool has_name = name && name[0];
Greg Claytonc4ffd662013-03-08 01:37:30 +00001544
Kate Stoneb9c1b512016-09-06 20:57:50 +00001545 CXXRecordDecl *decl = CXXRecordDecl::Create(
1546 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
Shafik Yaghmour62abe492019-08-14 22:30:29 +00001547 SourceLocation(), has_name ? &ast->Idents.get(name) : nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001548
Shafik Yaghmour62abe492019-08-14 22:30:29 +00001549 if (!has_name) {
1550 // In C++ a lambda is also represented as an unnamed class. This is
1551 // different from an *anonymous class* that the user wrote:
1552 //
1553 // struct A {
1554 // // anonymous class (GNU/MSVC extension)
1555 // struct {
1556 // int x;
1557 // };
1558 // // unnamed class within a class
1559 // struct {
1560 // int y;
1561 // } B;
1562 // };
1563 //
1564 // void f() {
1565 // // unammed class outside of a class
1566 // struct {
1567 // int z;
1568 // } C;
1569 // }
1570 //
1571 // Anonymous classes is a GNU/MSVC extension that clang supports. It
1572 // requires the anonymous class be embedded within a class. So the new
1573 // heuristic verifies this condition.
1574 //
1575 // FIXME: An unnamed class within a class is also wrongly recognized as an
1576 // anonymous struct.
Pavel Labath9cb31792019-08-21 08:22:19 +00001577 if (isa<CXXRecordDecl>(decl_ctx))
Shafik Yaghmour62abe492019-08-14 22:30:29 +00001578 decl->setAnonymousStructOrUnion(true);
Shafik Yaghmour62abe492019-08-14 22:30:29 +00001579 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001580
1581 if (decl) {
1582 if (metadata)
1583 SetMetadata(ast, decl, *metadata);
1584
1585 if (access_type != eAccessNone)
1586 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1587
1588 if (decl_ctx)
1589 decl_ctx->addDecl(decl);
1590
Alex Langfordbddab072019-08-13 19:40:36 +00001591 return CompilerType(this, ast->getTagDeclType(decl).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001592 }
1593 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001594}
1595
Sean Callanan09e91ac2017-05-11 22:08:05 +00001596namespace {
1597 bool IsValueParam(const clang::TemplateArgument &argument) {
1598 return argument.getKind() == TemplateArgument::Integral;
1599 }
1600}
1601
Kate Stoneb9c1b512016-09-06 20:57:50 +00001602static TemplateParameterList *CreateTemplateParameterList(
1603 ASTContext *ast,
1604 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1605 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1606 const bool parameter_pack = false;
1607 const bool is_typename = false;
1608 const unsigned depth = 0;
Sean Callanan09e91ac2017-05-11 22:08:05 +00001609 const size_t num_template_params = template_param_infos.args.size();
1610 DeclContext *const decl_context =
1611 ast->getTranslationUnitDecl(); // Is this the right decl context?,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001612 for (size_t i = 0; i < num_template_params; ++i) {
1613 const char *name = template_param_infos.names[i];
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001614
Kate Stoneb9c1b512016-09-06 20:57:50 +00001615 IdentifierInfo *identifier_info = nullptr;
1616 if (name && name[0])
1617 identifier_info = &ast->Idents.get(name);
Sean Callanan09e91ac2017-05-11 22:08:05 +00001618 if (IsValueParam(template_param_infos.args[i])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001619 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001620 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001621 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1622 template_param_infos.args[i].getIntegralType(), parameter_pack,
1623 nullptr));
1624
1625 } else {
1626 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001627 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001628 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1629 is_typename, parameter_pack));
1630 }
1631 }
Eugene Zemtsova9d928c2017-09-29 03:15:08 +00001632
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001633 if (template_param_infos.packed_args) {
Sean Callanan09e91ac2017-05-11 22:08:05 +00001634 IdentifierInfo *identifier_info = nullptr;
1635 if (template_param_infos.pack_name && template_param_infos.pack_name[0])
1636 identifier_info = &ast->Idents.get(template_param_infos.pack_name);
1637 const bool parameter_pack_true = true;
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001638
1639 if (!template_param_infos.packed_args->args.empty() &&
1640 IsValueParam(template_param_infos.packed_args->args[0])) {
Sean Callanan09e91ac2017-05-11 22:08:05 +00001641 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001642 *ast, decl_context, SourceLocation(), SourceLocation(), depth,
1643 num_template_params, identifier_info,
Sean Callanan09e91ac2017-05-11 22:08:05 +00001644 template_param_infos.packed_args->args[0].getIntegralType(),
1645 parameter_pack_true, nullptr));
1646 } else {
1647 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001648 *ast, decl_context, SourceLocation(), SourceLocation(), depth,
1649 num_template_params, identifier_info, is_typename,
1650 parameter_pack_true));
Sean Callanan09e91ac2017-05-11 22:08:05 +00001651 }
1652 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001653 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1654 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1655 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1656 SourceLocation(), requires_clause);
1657 return template_param_list;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001658}
1659
Kate Stoneb9c1b512016-09-06 20:57:50 +00001660clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1661 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1662 const char *name, const TemplateParameterInfos &template_param_infos) {
Adrian Prantld8f460e2018-05-02 16:55:16 +00001663 // /// Create a function template node.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001664 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001665
Kate Stoneb9c1b512016-09-06 20:57:50 +00001666 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001667
Kate Stoneb9c1b512016-09-06 20:57:50 +00001668 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1669 ast, template_param_infos, template_param_decls);
1670 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1671 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1672 template_param_list, func_decl);
1673
1674 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1675 i < template_param_decl_count; ++i) {
1676 // TODO: verify which decl context we should put template_param_decls into..
1677 template_param_decls[i]->setDeclContext(func_decl);
1678 }
1679
1680 return func_tmpl_decl;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001681}
1682
Kate Stoneb9c1b512016-09-06 20:57:50 +00001683void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1684 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1685 const TemplateParameterInfos &infos) {
Shafik Yaghmoura0858e22019-07-17 20:16:13 +00001686 TemplateArgumentList *template_args_ptr =
1687 TemplateArgumentList::CreateCopy(func_decl->getASTContext(), infos.args);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001688
Shafik Yaghmoura0858e22019-07-17 20:16:13 +00001689 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl,
1690 template_args_ptr, nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001691}
1692
Kate Stoneb9c1b512016-09-06 20:57:50 +00001693ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1694 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1695 int kind, const TemplateParameterInfos &template_param_infos) {
1696 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001697
Kate Stoneb9c1b512016-09-06 20:57:50 +00001698 ClassTemplateDecl *class_template_decl = nullptr;
1699 if (decl_ctx == nullptr)
1700 decl_ctx = ast->getTranslationUnitDecl();
Greg Claytonf0705c82011-10-22 03:33:13 +00001701
Kate Stoneb9c1b512016-09-06 20:57:50 +00001702 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1703 DeclarationName decl_name(&identifier_info);
Greg Claytonf0705c82011-10-22 03:33:13 +00001704
Kate Stoneb9c1b512016-09-06 20:57:50 +00001705 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Greg Claytonf0705c82011-10-22 03:33:13 +00001706
Kate Stoneb9c1b512016-09-06 20:57:50 +00001707 for (NamedDecl *decl : result) {
1708 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001709 if (class_template_decl)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001710 return class_template_decl;
1711 }
1712
1713 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1714
1715 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1716 ast, template_param_infos, template_param_decls);
1717
1718 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1719 *ast, (TagDecl::TagKind)kind,
1720 decl_ctx, // What decl context do we use here? TU? The actual decl
1721 // context?
1722 SourceLocation(), SourceLocation(), &identifier_info);
1723
1724 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1725 i < template_param_decl_count; ++i) {
1726 template_param_decls[i]->setDeclContext(template_cxx_decl);
1727 }
1728
1729 // With templated classes, we say that a class is templated with
1730 // specializations, but that the bare class has no functions.
1731 // template_cxx_decl->startDefinition();
1732 // template_cxx_decl->completeDefinition();
1733
1734 class_template_decl = ClassTemplateDecl::Create(
1735 *ast,
1736 decl_ctx, // What decl context do we use here? TU? The actual decl
1737 // context?
Pavel Labath4294de32017-01-12 10:44:16 +00001738 SourceLocation(), decl_name, template_param_list, template_cxx_decl);
Richard Smith35b007e2019-02-15 21:48:09 +00001739 template_cxx_decl->setDescribedClassTemplate(class_template_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001740
1741 if (class_template_decl) {
1742 if (access_type != eAccessNone)
1743 class_template_decl->setAccess(
1744 ConvertAccessTypeToAccessSpecifier(access_type));
1745
1746 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1747 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1748
1749 decl_ctx->addDecl(class_template_decl);
1750
Sean Callanan5e9e1992011-10-26 01:06:27 +00001751#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001752 VerifyDecl(class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001753#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001754 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001755
Kate Stoneb9c1b512016-09-06 20:57:50 +00001756 return class_template_decl;
Greg Claytonf0705c82011-10-22 03:33:13 +00001757}
1758
Frederic Rissf4e7e522018-04-02 16:18:32 +00001759TemplateTemplateParmDecl *
1760ClangASTContext::CreateTemplateTemplateParmDecl(const char *template_name) {
1761 ASTContext *ast = getASTContext();
1762
1763 auto *decl_ctx = ast->getTranslationUnitDecl();
1764
1765 IdentifierInfo &identifier_info = ast->Idents.get(template_name);
1766 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1767
1768 ClangASTContext::TemplateParameterInfos template_param_infos;
1769 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1770 ast, template_param_infos, template_param_decls);
1771
1772 // LLDB needs to create those decls only to be able to display a
Adrian Prantl05097242018-04-30 16:49:04 +00001773 // type that includes a template template argument. Only the name matters for
1774 // this purpose, so we use dummy values for the other characterisitcs of the
1775 // type.
Frederic Rissf4e7e522018-04-02 16:18:32 +00001776 return TemplateTemplateParmDecl::Create(
1777 *ast, decl_ctx, SourceLocation(),
1778 /*Depth*/ 0, /*Position*/ 0,
1779 /*IsParameterPack*/ false, &identifier_info, template_param_list);
1780}
1781
Greg Claytonf0705c82011-10-22 03:33:13 +00001782ClassTemplateSpecializationDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001783ClangASTContext::CreateClassTemplateSpecializationDecl(
1784 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1785 const TemplateParameterInfos &template_param_infos) {
1786 ASTContext *ast = getASTContext();
Sean Callanan09e91ac2017-05-11 22:08:05 +00001787 llvm::SmallVector<clang::TemplateArgument, 2> args(
1788 template_param_infos.args.size() +
1789 (template_param_infos.packed_args ? 1 : 0));
1790 std::copy(template_param_infos.args.begin(), template_param_infos.args.end(),
1791 args.begin());
1792 if (template_param_infos.packed_args) {
1793 args[args.size() - 1] = TemplateArgument::CreatePackCopy(
1794 *ast, template_param_infos.packed_args->args);
1795 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001796 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1797 ClassTemplateSpecializationDecl::Create(
1798 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
Sean Callanan09e91ac2017-05-11 22:08:05 +00001799 SourceLocation(), class_template_decl, args,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001800 nullptr);
1801
1802 class_template_specialization_decl->setSpecializationKind(
1803 TSK_ExplicitSpecialization);
1804
1805 return class_template_specialization_decl;
1806}
1807
1808CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1809 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1810 if (class_template_specialization_decl) {
Greg Claytonf0705c82011-10-22 03:33:13 +00001811 ASTContext *ast = getASTContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001812 if (ast)
1813 return CompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00001814 this, ast->getTagDeclType(class_template_specialization_decl)
1815 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001816 }
1817 return CompilerType();
Greg Claytonf0705c82011-10-22 03:33:13 +00001818}
1819
Kate Stoneb9c1b512016-09-06 20:57:50 +00001820static inline bool check_op_param(bool is_method,
1821 clang::OverloadedOperatorKind op_kind,
1822 bool unary, bool binary,
1823 uint32_t num_params) {
1824 // Special-case call since it can take any number of operands
1825 if (op_kind == OO_Call)
1826 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001827
Kate Stoneb9c1b512016-09-06 20:57:50 +00001828 // The parameter count doesn't include "this"
1829 if (is_method)
1830 ++num_params;
1831 if (num_params == 1)
1832 return unary;
1833 if (num_params == 2)
1834 return binary;
1835 else
Greg Clayton090d0982011-06-19 03:43:27 +00001836 return false;
1837}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001838
Kate Stoneb9c1b512016-09-06 20:57:50 +00001839bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1840 bool is_method, clang::OverloadedOperatorKind op_kind,
1841 uint32_t num_params) {
1842 switch (op_kind) {
1843 default:
1844 break;
1845 // C++ standard allows any number of arguments to new/delete
1846 case OO_New:
1847 case OO_Array_New:
1848 case OO_Delete:
1849 case OO_Array_Delete:
1850 return true;
1851 }
Pavel Labath1ac2b202016-08-15 14:32:32 +00001852
Kate Stoneb9c1b512016-09-06 20:57:50 +00001853#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1854 case OO_##Name: \
1855 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1856 switch (op_kind) {
Greg Clayton090d0982011-06-19 03:43:27 +00001857#include "clang/Basic/OperatorKinds.def"
Kate Stoneb9c1b512016-09-06 20:57:50 +00001858 default:
1859 break;
1860 }
1861 return false;
Greg Clayton090d0982011-06-19 03:43:27 +00001862}
1863
Greg Clayton57ee3062013-07-11 22:46:58 +00001864clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +00001865ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1866 clang::AccessSpecifier rhs) {
1867 // Make the access equal to the stricter of the field and the nested field's
1868 // access
1869 if (lhs == AS_none || rhs == AS_none)
1870 return AS_none;
1871 if (lhs == AS_private || rhs == AS_private)
1872 return AS_private;
1873 if (lhs == AS_protected || rhs == AS_protected)
1874 return AS_protected;
1875 return AS_public;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001876}
1877
Kate Stoneb9c1b512016-09-06 20:57:50 +00001878bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1879 uint32_t &bitfield_bit_size) {
1880 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001881}
1882
Kate Stoneb9c1b512016-09-06 20:57:50 +00001883bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field,
1884 uint32_t &bitfield_bit_size) {
1885 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001886 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001887
Kate Stoneb9c1b512016-09-06 20:57:50 +00001888 if (field->isBitField()) {
1889 Expr *bit_width_expr = field->getBitWidth();
1890 if (bit_width_expr) {
1891 llvm::APSInt bit_width_apsint;
1892 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1893 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001894 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001895 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001896 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001897 }
1898 return false;
1899}
1900
1901bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1902 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001903 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001904
1905 if (!record_decl->field_empty())
1906 return true;
1907
1908 // No fields, lets check this is a CXX record and check the base classes
1909 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1910 if (cxx_record_decl) {
1911 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1912 for (base_class = cxx_record_decl->bases_begin(),
1913 base_class_end = cxx_record_decl->bases_end();
1914 base_class != base_class_end; ++base_class) {
1915 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1916 base_class->getType()->getAs<RecordType>()->getDecl());
1917 if (RecordHasFields(base_class_decl))
1918 return true;
1919 }
1920 }
1921 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001922}
1923
Adrian Prantl4e8be2c2018-06-13 16:21:24 +00001924#pragma mark Objective-C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001925
Kate Stoneb9c1b512016-09-06 20:57:50 +00001926CompilerType ClangASTContext::CreateObjCClass(const char *name,
1927 DeclContext *decl_ctx,
1928 bool isForwardDecl,
1929 bool isInternal,
1930 ClangASTMetadata *metadata) {
1931 ASTContext *ast = getASTContext();
1932 assert(ast != nullptr);
1933 assert(name && name[0]);
1934 if (decl_ctx == nullptr)
1935 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001936
Kate Stoneb9c1b512016-09-06 20:57:50 +00001937 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1938 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1939 nullptr, SourceLocation(),
1940 /*isForwardDecl,*/
1941 isInternal);
1942
1943 if (decl && metadata)
1944 SetMetadata(ast, decl, *metadata);
1945
Alex Langfordbddab072019-08-13 19:40:36 +00001946 return CompilerType(this, ast->getObjCInterfaceType(decl).getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001947}
1948
Kate Stoneb9c1b512016-09-06 20:57:50 +00001949static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001950 return !ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001951}
1952
Greg Clayton57ee3062013-07-11 22:46:58 +00001953uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001954ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1955 bool omit_empty_base_classes) {
1956 uint32_t num_bases = 0;
1957 if (cxx_record_decl) {
1958 if (omit_empty_base_classes) {
1959 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1960 for (base_class = cxx_record_decl->bases_begin(),
1961 base_class_end = cxx_record_decl->bases_end();
1962 base_class != base_class_end; ++base_class) {
1963 // Skip empty base classes
1964 if (omit_empty_base_classes) {
1965 if (BaseSpecifierIsEmpty(base_class))
1966 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001967 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001968 ++num_bases;
1969 }
1970 } else
1971 num_bases = cxx_record_decl->getNumBases();
1972 }
1973 return num_bases;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001974}
1975
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001976#pragma mark Namespace Declarations
1977
Raphael Isemanna9469972019-03-12 07:45:04 +00001978NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1979 const char *name, DeclContext *decl_ctx, bool is_inline) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001980 NamespaceDecl *namespace_decl = nullptr;
1981 ASTContext *ast = getASTContext();
1982 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1983 if (decl_ctx == nullptr)
1984 decl_ctx = translation_unit_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00001985
Kate Stoneb9c1b512016-09-06 20:57:50 +00001986 if (name) {
1987 IdentifierInfo &identifier_info = ast->Idents.get(name);
1988 DeclarationName decl_name(&identifier_info);
1989 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1990 for (NamedDecl *decl : result) {
1991 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1992 if (namespace_decl)
1993 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001994 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001995
1996 namespace_decl =
Raphael Isemanna9469972019-03-12 07:45:04 +00001997 NamespaceDecl::Create(*ast, decl_ctx, is_inline, SourceLocation(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001998 SourceLocation(), &identifier_info, nullptr);
1999
2000 decl_ctx->addDecl(namespace_decl);
2001 } else {
2002 if (decl_ctx == translation_unit_decl) {
2003 namespace_decl = translation_unit_decl->getAnonymousNamespace();
2004 if (namespace_decl)
2005 return namespace_decl;
2006
2007 namespace_decl =
2008 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
2009 SourceLocation(), nullptr, nullptr);
2010 translation_unit_decl->setAnonymousNamespace(namespace_decl);
2011 translation_unit_decl->addDecl(namespace_decl);
2012 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
2013 } else {
2014 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
2015 if (parent_namespace_decl) {
2016 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
2017 if (namespace_decl)
2018 return namespace_decl;
2019 namespace_decl =
2020 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
2021 SourceLocation(), nullptr, nullptr);
2022 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
2023 parent_namespace_decl->addDecl(namespace_decl);
2024 assert(namespace_decl ==
2025 parent_namespace_decl->getAnonymousNamespace());
2026 } else {
Raphael Isemannc4944812019-07-04 19:49:31 +00002027 assert(false && "GetUniqueNamespaceDeclaration called with no name and "
2028 "no namespace as decl_ctx");
Kate Stoneb9c1b512016-09-06 20:57:50 +00002029 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00002030 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002031 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00002032#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00002033 VerifyDecl(namespace_decl);
Greg Clayton9d3d6882011-10-31 23:51:19 +00002034#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002035 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002036}
2037
Kate Stoneb9c1b512016-09-06 20:57:50 +00002038NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
Raphael Isemanna9469972019-03-12 07:45:04 +00002039 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx,
2040 bool is_inline) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002041 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
2042 if (ast_ctx == nullptr)
2043 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +00002044
Raphael Isemanna9469972019-03-12 07:45:04 +00002045 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx, is_inline);
Siva Chandra03ff5c82016-02-05 19:10:04 +00002046}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002047
Paul Hermand628cbb2015-09-15 23:44:17 +00002048clang::BlockDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00002049ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
2050 if (ctx != nullptr) {
2051 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
2052 clang::SourceLocation());
2053 ctx->addDecl(decl);
2054 return decl;
2055 }
2056 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002057}
2058
Kate Stoneb9c1b512016-09-06 20:57:50 +00002059clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
2060 clang::DeclContext *right,
2061 clang::DeclContext *root) {
2062 if (root == nullptr)
Paul Hermanea188fc2015-09-16 18:48:30 +00002063 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002064
2065 std::set<clang::DeclContext *> path_left;
2066 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
2067 path_left.insert(d);
2068
2069 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
2070 if (path_left.find(d) != path_left.end())
2071 return d;
2072
2073 return nullptr;
Paul Hermanea188fc2015-09-16 18:48:30 +00002074}
2075
Kate Stoneb9c1b512016-09-06 20:57:50 +00002076clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
2077 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
2078 if (decl_ctx != nullptr && ns_decl != nullptr) {
2079 clang::TranslationUnitDecl *translation_unit =
2080 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
2081 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
2082 *getASTContext(), decl_ctx, clang::SourceLocation(),
2083 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
2084 clang::SourceLocation(), ns_decl,
2085 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
2086 decl_ctx->addDecl(using_decl);
2087 return using_decl;
2088 }
2089 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002090}
2091
2092clang::UsingDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00002093ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
2094 clang::NamedDecl *target) {
2095 if (current_decl_ctx != nullptr && target != nullptr) {
2096 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
2097 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
2098 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
2099 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
2100 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
2101 target);
2102 using_decl->addShadowDecl(shadow_decl);
2103 current_decl_ctx->addDecl(using_decl);
2104 return using_decl;
2105 }
2106 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002107}
2108
Kate Stoneb9c1b512016-09-06 20:57:50 +00002109clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
2110 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
2111 if (decl_context != nullptr) {
2112 clang::VarDecl *var_decl = clang::VarDecl::Create(
2113 *getASTContext(), decl_context, clang::SourceLocation(),
2114 clang::SourceLocation(),
2115 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
2116 nullptr, clang::SC_None);
2117 var_decl->setAccess(clang::AS_public);
2118 decl_context->addDecl(var_decl);
2119 return var_decl;
2120 }
2121 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002122}
2123
Zachary Turner9d8a97e2016-04-01 23:20:35 +00002124lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00002125ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
2126 lldb::BasicType basic_type) {
2127 switch (basic_type) {
2128 case eBasicTypeVoid:
2129 return ast->VoidTy.getAsOpaquePtr();
2130 case eBasicTypeChar:
2131 return ast->CharTy.getAsOpaquePtr();
2132 case eBasicTypeSignedChar:
2133 return ast->SignedCharTy.getAsOpaquePtr();
2134 case eBasicTypeUnsignedChar:
2135 return ast->UnsignedCharTy.getAsOpaquePtr();
2136 case eBasicTypeWChar:
2137 return ast->getWCharType().getAsOpaquePtr();
2138 case eBasicTypeSignedWChar:
2139 return ast->getSignedWCharType().getAsOpaquePtr();
2140 case eBasicTypeUnsignedWChar:
2141 return ast->getUnsignedWCharType().getAsOpaquePtr();
2142 case eBasicTypeChar16:
2143 return ast->Char16Ty.getAsOpaquePtr();
2144 case eBasicTypeChar32:
2145 return ast->Char32Ty.getAsOpaquePtr();
2146 case eBasicTypeShort:
2147 return ast->ShortTy.getAsOpaquePtr();
2148 case eBasicTypeUnsignedShort:
2149 return ast->UnsignedShortTy.getAsOpaquePtr();
2150 case eBasicTypeInt:
2151 return ast->IntTy.getAsOpaquePtr();
2152 case eBasicTypeUnsignedInt:
2153 return ast->UnsignedIntTy.getAsOpaquePtr();
2154 case eBasicTypeLong:
2155 return ast->LongTy.getAsOpaquePtr();
2156 case eBasicTypeUnsignedLong:
2157 return ast->UnsignedLongTy.getAsOpaquePtr();
2158 case eBasicTypeLongLong:
2159 return ast->LongLongTy.getAsOpaquePtr();
2160 case eBasicTypeUnsignedLongLong:
2161 return ast->UnsignedLongLongTy.getAsOpaquePtr();
2162 case eBasicTypeInt128:
2163 return ast->Int128Ty.getAsOpaquePtr();
2164 case eBasicTypeUnsignedInt128:
2165 return ast->UnsignedInt128Ty.getAsOpaquePtr();
2166 case eBasicTypeBool:
2167 return ast->BoolTy.getAsOpaquePtr();
2168 case eBasicTypeHalf:
2169 return ast->HalfTy.getAsOpaquePtr();
2170 case eBasicTypeFloat:
2171 return ast->FloatTy.getAsOpaquePtr();
2172 case eBasicTypeDouble:
2173 return ast->DoubleTy.getAsOpaquePtr();
2174 case eBasicTypeLongDouble:
2175 return ast->LongDoubleTy.getAsOpaquePtr();
2176 case eBasicTypeFloatComplex:
2177 return ast->FloatComplexTy.getAsOpaquePtr();
2178 case eBasicTypeDoubleComplex:
2179 return ast->DoubleComplexTy.getAsOpaquePtr();
2180 case eBasicTypeLongDoubleComplex:
2181 return ast->LongDoubleComplexTy.getAsOpaquePtr();
2182 case eBasicTypeObjCID:
2183 return ast->getObjCIdType().getAsOpaquePtr();
2184 case eBasicTypeObjCClass:
2185 return ast->getObjCClassType().getAsOpaquePtr();
2186 case eBasicTypeObjCSel:
2187 return ast->getObjCSelType().getAsOpaquePtr();
2188 case eBasicTypeNullPtr:
2189 return ast->NullPtrTy.getAsOpaquePtr();
2190 default:
2191 return nullptr;
2192 }
Zachary Turner9d8a97e2016-04-01 23:20:35 +00002193}
2194
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002195#pragma mark Function Types
2196
Pavel Labath1ac2b202016-08-15 14:32:32 +00002197clang::DeclarationName
Kate Stoneb9c1b512016-09-06 20:57:50 +00002198ClangASTContext::GetDeclarationName(const char *name,
2199 const CompilerType &function_clang_type) {
2200 if (!name || !name[0])
2201 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002202
Kate Stoneb9c1b512016-09-06 20:57:50 +00002203 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
2204 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
2205 return DeclarationName(&getASTContext()->Idents.get(
2206 name)); // Not operator, but a regular function.
Pavel Labath1ac2b202016-08-15 14:32:32 +00002207
Adrian Prantl05097242018-04-30 16:49:04 +00002208 // Check the number of operator parameters. Sometimes we have seen bad DWARF
2209 // that doesn't correctly describe operators and if we try to create a method
2210 // and add it to the class, clang will assert and crash, so we need to make
2211 // sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002212 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
2213 const clang::FunctionProtoType *function_type =
2214 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
2215 if (function_type == nullptr)
2216 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002217
Kate Stoneb9c1b512016-09-06 20:57:50 +00002218 const bool is_method = false;
2219 const unsigned int num_params = function_type->getNumParams();
2220 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
2221 is_method, op_kind, num_params))
2222 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002223
Kate Stoneb9c1b512016-09-06 20:57:50 +00002224 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
Pavel Labath1ac2b202016-08-15 14:32:32 +00002225}
2226
Kate Stoneb9c1b512016-09-06 20:57:50 +00002227FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
2228 DeclContext *decl_ctx, const char *name,
2229 const CompilerType &function_clang_type, int storage, bool is_inline) {
2230 FunctionDecl *func_decl = nullptr;
2231 ASTContext *ast = getASTContext();
2232 if (decl_ctx == nullptr)
2233 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002234
Kate Stoneb9c1b512016-09-06 20:57:50 +00002235 const bool hasWrittenPrototype = true;
2236 const bool isConstexprSpecified = false;
Greg Clayton0d551042013-06-28 21:08:47 +00002237
Kate Stoneb9c1b512016-09-06 20:57:50 +00002238 clang::DeclarationName declarationName =
2239 GetDeclarationName(name, function_clang_type);
2240 func_decl = FunctionDecl::Create(
2241 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
2242 ClangUtil::GetQualType(function_clang_type), nullptr,
2243 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002244 isConstexprSpecified ? CSK_constexpr : CSK_unspecified);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002245 if (func_decl)
2246 decl_ctx->addDecl(func_decl);
2247
Sean Callanan5e9e1992011-10-26 01:06:27 +00002248#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00002249 VerifyDecl(func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002250#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002251
2252 return func_decl;
2253}
2254
2255CompilerType ClangASTContext::CreateFunctionType(
2256 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002257 unsigned num_args, bool is_variadic, unsigned type_quals,
2258 clang::CallingConv cc) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002259 if (ast == nullptr)
2260 return CompilerType(); // invalid AST
2261
2262 if (!result_type || !ClangUtil::IsClangType(result_type))
2263 return CompilerType(); // invalid return type
2264
2265 std::vector<QualType> qual_type_args;
2266 if (num_args > 0 && args == nullptr)
2267 return CompilerType(); // invalid argument array passed in
2268
2269 // Verify that all arguments are valid and the right type
2270 for (unsigned i = 0; i < num_args; ++i) {
2271 if (args[i]) {
2272 // Make sure we have a clang type in args[i] and not a type from another
2273 // language whose name might match
2274 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2275 lldbassert(is_clang_type);
2276 if (is_clang_type)
2277 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2278 else
2279 return CompilerType(); // invalid argument type (must be a clang type)
2280 } else
2281 return CompilerType(); // invalid argument type (empty)
2282 }
2283
2284 // TODO: Detect calling convention in DWARF?
2285 FunctionProtoType::ExtProtoInfo proto_info;
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002286 proto_info.ExtInfo = cc;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002287 proto_info.Variadic = is_variadic;
2288 proto_info.ExceptionSpec = EST_None;
Mikael Nilsson8b3bf6c2018-12-13 10:17:26 +00002289 proto_info.TypeQuals = clang::Qualifiers::fromFastMask(type_quals);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002290 proto_info.RefQualifier = RQ_None;
2291
Alex Langfordbddab072019-08-13 19:40:36 +00002292 return CompilerType(ClangASTContext::GetASTContext(ast),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002293 ast->getFunctionType(ClangUtil::GetQualType(result_type),
Alex Langfordbddab072019-08-13 19:40:36 +00002294 qual_type_args, proto_info).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002295}
2296
2297ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
Zachary Turner6753d2d2018-12-12 17:17:53 +00002298 clang::DeclContext *decl_ctx, const char *name,
Shafik Yaghmourfa5c3402019-08-02 21:41:50 +00002299 const CompilerType &param_type, int storage, bool add_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002300 ASTContext *ast = getASTContext();
2301 assert(ast != nullptr);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00002302 auto *decl =
2303 ParmVarDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(),
2304 name && name[0] ? &ast->Idents.get(name) : nullptr,
2305 ClangUtil::GetQualType(param_type), nullptr,
2306 (clang::StorageClass)storage, nullptr);
Shafik Yaghmourfa5c3402019-08-02 21:41:50 +00002307 if (add_decl)
2308 decl_ctx->addDecl(decl);
2309
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00002310 return decl;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002311}
2312
2313void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2314 ParmVarDecl **params,
2315 unsigned num_params) {
2316 if (function_decl)
2317 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002318}
2319
Greg Claytona1e5dc82015-08-11 22:53:00 +00002320CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002321ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00002322 QualType block_type = m_ast_up->getBlockPointerType(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002323 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
Greg Claytonceeb5212016-05-26 22:33:25 +00002324
Kate Stoneb9c1b512016-09-06 20:57:50 +00002325 return CompilerType(this, block_type.getAsOpaquePtr());
Sean Callananc530ba92016-05-02 21:15:31 +00002326}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002327
2328#pragma mark Array Types
2329
Kate Stoneb9c1b512016-09-06 20:57:50 +00002330CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2331 size_t element_count,
2332 bool is_vector) {
2333 if (element_type.IsValid()) {
2334 ASTContext *ast = getASTContext();
2335 assert(ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002336
Kate Stoneb9c1b512016-09-06 20:57:50 +00002337 if (is_vector) {
2338 return CompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00002339 this, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2340 element_count)
2341 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002342 } else {
2343
2344 llvm::APInt ap_element_count(64, element_count);
2345 if (element_count == 0) {
Alex Langfordbddab072019-08-13 19:40:36 +00002346 return CompilerType(this, ast->getIncompleteArrayType(
2347 ClangUtil::GetQualType(element_type),
2348 clang::ArrayType::Normal, 0)
2349 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002350 } else {
Alex Langfordbddab072019-08-13 19:40:36 +00002351 return CompilerType(this, ast->getConstantArrayType(
2352 ClangUtil::GetQualType(element_type),
2353 ap_element_count,
2354 clang::ArrayType::Normal, 0)
2355 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002356 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002357 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002358 }
2359 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002360}
2361
Kate Stoneb9c1b512016-09-06 20:57:50 +00002362CompilerType ClangASTContext::CreateStructForIdentifier(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002363 ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002364 const std::initializer_list<std::pair<const char *, CompilerType>>
2365 &type_fields,
2366 bool packed) {
2367 CompilerType type;
2368 if (!type_name.IsEmpty() &&
2369 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2370 .IsValid()) {
Pavel Labathf31c9d22017-01-05 13:18:42 +00002371 lldbassert(0 && "Trying to create a type for an existing name");
Enrico Granata76b08d52014-10-29 23:08:02 +00002372 return type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002373 }
2374
2375 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2376 clang::TTK_Struct, lldb::eLanguageTypeC);
2377 StartTagDeclarationDefinition(type);
2378 for (const auto &field : type_fields)
2379 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2380 0);
2381 if (packed)
2382 SetIsPacked(type);
2383 CompleteTagDeclarationDefinition(type);
2384 return type;
Enrico Granata76b08d52014-10-29 23:08:02 +00002385}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002386
Kate Stoneb9c1b512016-09-06 20:57:50 +00002387CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002388 ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002389 const std::initializer_list<std::pair<const char *, CompilerType>>
2390 &type_fields,
2391 bool packed) {
2392 CompilerType type;
2393 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2394 return type;
Sean Callananc530ba92016-05-02 21:15:31 +00002395
Kate Stoneb9c1b512016-09-06 20:57:50 +00002396 return CreateStructForIdentifier(type_name, type_fields, packed);
Sean Callananc530ba92016-05-02 21:15:31 +00002397}
2398
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002399#pragma mark Enumeration Types
2400
Greg Claytona1e5dc82015-08-11 22:53:00 +00002401CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002402ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2403 const Declaration &decl,
Tamas Berghammer59765832017-11-07 10:39:22 +00002404 const CompilerType &integer_clang_type,
2405 bool is_scoped) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002406 // TODO: Do something intelligent with the Declaration object passed in
2407 // like maybe filling in the SourceLocation with it...
2408 ASTContext *ast = getASTContext();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002409
Kate Stoneb9c1b512016-09-06 20:57:50 +00002410 // TODO: ask about these...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002411 // const bool IsFixed = false;
2412
2413 EnumDecl *enum_decl = EnumDecl::Create(
2414 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2415 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
Tamas Berghammercf6bf4c2017-11-07 13:43:55 +00002416 is_scoped, // IsScoped
2417 is_scoped, // IsScopedUsingClassTag
2418 false); // IsFixed
Kate Stoneb9c1b512016-09-06 20:57:50 +00002419
2420 if (enum_decl) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +00002421 if (decl_ctx)
2422 decl_ctx->addDecl(enum_decl);
2423
Kate Stoneb9c1b512016-09-06 20:57:50 +00002424 // TODO: check if we should be setting the promotion type too?
2425 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2426
2427 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2428
Alex Langfordbddab072019-08-13 19:40:36 +00002429 return CompilerType(this, ast->getTagDeclType(enum_decl).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002430 }
2431 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002432}
2433
Kate Stoneb9c1b512016-09-06 20:57:50 +00002434CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2435 size_t bit_size,
2436 bool is_signed) {
2437 if (ast) {
Alex Langfordbddab072019-08-13 19:40:36 +00002438 auto *clang_ast_context = ClangASTContext::GetASTContext(ast);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002439 if (is_signed) {
2440 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002441 return CompilerType(clang_ast_context,
2442 ast->SignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002443
2444 if (bit_size == ast->getTypeSize(ast->ShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002445 return CompilerType(clang_ast_context, ast->ShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002446
2447 if (bit_size == ast->getTypeSize(ast->IntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002448 return CompilerType(clang_ast_context, ast->IntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002449
2450 if (bit_size == ast->getTypeSize(ast->LongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002451 return CompilerType(clang_ast_context, ast->LongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002452
2453 if (bit_size == ast->getTypeSize(ast->LongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002454 return CompilerType(clang_ast_context,
2455 ast->LongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002456
2457 if (bit_size == ast->getTypeSize(ast->Int128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00002458 return CompilerType(clang_ast_context, ast->Int128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002459 } else {
2460 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002461 return CompilerType(clang_ast_context,
2462 ast->UnsignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002463
2464 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002465 return CompilerType(clang_ast_context,
2466 ast->UnsignedShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002467
2468 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002469 return CompilerType(clang_ast_context,
2470 ast->UnsignedIntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002471
2472 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002473 return CompilerType(clang_ast_context,
2474 ast->UnsignedLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002475
2476 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002477 return CompilerType(clang_ast_context,
2478 ast->UnsignedLongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002479
2480 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00002481 return CompilerType(clang_ast_context,
2482 ast->UnsignedInt128Ty.getAsOpaquePtr());
Enrico Granatae8bf7492014-08-15 23:00:02 +00002483 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002484 }
2485 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002486}
2487
Kate Stoneb9c1b512016-09-06 20:57:50 +00002488CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2489 bool is_signed) {
2490 if (ast)
2491 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2492 is_signed);
2493 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002494}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002495
Kate Stoneb9c1b512016-09-06 20:57:50 +00002496void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2497 if (decl_ctx) {
2498 DumpDeclContextHiearchy(decl_ctx->getParent());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002499
Kate Stoneb9c1b512016-09-06 20:57:50 +00002500 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2501 if (named_decl) {
2502 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2503 named_decl->getDeclName().getAsString().c_str());
2504 } else {
2505 printf("%20s\n", decl_ctx->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002506 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002507 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002508}
2509
Kate Stoneb9c1b512016-09-06 20:57:50 +00002510void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2511 if (decl == nullptr)
2512 return;
2513 DumpDeclContextHiearchy(decl->getDeclContext());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002514
Kate Stoneb9c1b512016-09-06 20:57:50 +00002515 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2516 if (record_decl) {
2517 printf("%20s: %s%s\n", decl->getDeclKindName(),
2518 record_decl->getDeclName().getAsString().c_str(),
2519 record_decl->isInjectedClassName() ? " (injected class name)" : "");
Greg Claytone6b36cd2015-12-08 01:02:08 +00002520
Kate Stoneb9c1b512016-09-06 20:57:50 +00002521 } else {
2522 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2523 if (named_decl) {
2524 printf("%20s: %s\n", decl->getDeclKindName(),
2525 named_decl->getDeclName().getAsString().c_str());
2526 } else {
2527 printf("%20s\n", decl->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002528 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002529 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002530}
2531
Kate Stoneb9c1b512016-09-06 20:57:50 +00002532bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2533 clang::Decl *rhs_decl) {
2534 if (lhs_decl && rhs_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002535 // Make sure the decl kinds match first
Kate Stoneb9c1b512016-09-06 20:57:50 +00002536 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2537 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002538
Kate Stoneb9c1b512016-09-06 20:57:50 +00002539 if (lhs_decl_kind == rhs_decl_kind) {
Adrian Prantl05097242018-04-30 16:49:04 +00002540 // Now check that the decl contexts kinds are all equivalent before we
2541 // have to check any names of the decl contexts...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002542 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2543 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2544 if (lhs_decl_ctx && rhs_decl_ctx) {
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00002545 while (true) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002546 if (lhs_decl_ctx && rhs_decl_ctx) {
2547 const clang::Decl::Kind lhs_decl_ctx_kind =
2548 lhs_decl_ctx->getDeclKind();
2549 const clang::Decl::Kind rhs_decl_ctx_kind =
2550 rhs_decl_ctx->getDeclKind();
2551 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2552 lhs_decl_ctx = lhs_decl_ctx->getParent();
2553 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002554
Kate Stoneb9c1b512016-09-06 20:57:50 +00002555 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2556 break;
2557 } else
2558 return false;
2559 } else
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00002560 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002561 }
2562
Kate Stoneb9c1b512016-09-06 20:57:50 +00002563 // Now make sure the name of the decls match
Kate Stoneb9c1b512016-09-06 20:57:50 +00002564 clang::NamedDecl *lhs_named_decl =
2565 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2566 clang::NamedDecl *rhs_named_decl =
2567 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2568 if (lhs_named_decl && rhs_named_decl) {
2569 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2570 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2571 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2572 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2573 return false;
2574 } else
Greg Claytona2721472011-06-25 00:44:06 +00002575 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002576 } else
2577 return false;
Greg Claytona2721472011-06-25 00:44:06 +00002578
Adrian Prantl05097242018-04-30 16:49:04 +00002579 // We know that the decl context kinds all match, so now we need to
2580 // make sure the names match as well
Kate Stoneb9c1b512016-09-06 20:57:50 +00002581 lhs_decl_ctx = lhs_decl->getDeclContext();
2582 rhs_decl_ctx = rhs_decl->getDeclContext();
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00002583 while (true) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002584 switch (lhs_decl_ctx->getDeclKind()) {
2585 case clang::Decl::TranslationUnit:
2586 // We don't care about the translation unit names
2587 return true;
2588 default: {
2589 clang::NamedDecl *lhs_named_decl =
2590 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2591 clang::NamedDecl *rhs_named_decl =
2592 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2593 if (lhs_named_decl && rhs_named_decl) {
2594 clang::DeclarationName lhs_decl_name =
2595 lhs_named_decl->getDeclName();
2596 clang::DeclarationName rhs_decl_name =
2597 rhs_named_decl->getDeclName();
2598 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2599 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2600 return false;
2601 } else
2602 return false;
2603 } else
2604 return false;
2605 } break;
2606 }
2607 lhs_decl_ctx = lhs_decl_ctx->getParent();
2608 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytond8d4a572015-08-11 21:38:15 +00002609 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002610 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002611 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002612 }
2613 return false;
2614}
2615bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2616 clang::Decl *decl) {
2617 if (!decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002618 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002619
Kate Stoneb9c1b512016-09-06 20:57:50 +00002620 ExternalASTSource *ast_source = ast->getExternalSource();
Greg Claytond8d4a572015-08-11 21:38:15 +00002621
Kate Stoneb9c1b512016-09-06 20:57:50 +00002622 if (!ast_source)
Greg Claytond8d4a572015-08-11 21:38:15 +00002623 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002624
2625 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2626 if (tag_decl->isCompleteDefinition())
2627 return true;
2628
2629 if (!tag_decl->hasExternalLexicalStorage())
2630 return false;
2631
2632 ast_source->CompleteType(tag_decl);
2633
2634 return !tag_decl->getTypeForDecl()->isIncompleteType();
2635 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2636 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2637 if (objc_interface_decl->getDefinition())
2638 return true;
2639
2640 if (!objc_interface_decl->hasExternalLexicalStorage())
2641 return false;
2642
2643 ast_source->CompleteType(objc_interface_decl);
2644
2645 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2646 } else {
2647 return false;
2648 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002649}
2650
Kate Stoneb9c1b512016-09-06 20:57:50 +00002651void ClangASTContext::SetMetadataAsUserID(const void *object,
2652 user_id_t user_id) {
2653 ClangASTMetadata meta_data;
2654 meta_data.SetUserID(user_id);
2655 SetMetadata(object, meta_data);
Greg Clayton99558cc42015-08-24 23:46:31 +00002656}
2657
Kate Stoneb9c1b512016-09-06 20:57:50 +00002658void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2659 ClangASTMetadata &metadata) {
2660 ClangExternalASTSourceCommon *external_source =
2661 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2662
2663 if (external_source)
2664 external_source->SetMetadata(object, metadata);
2665}
2666
2667ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2668 const void *object) {
2669 ClangExternalASTSourceCommon *external_source =
2670 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2671
2672 if (external_source && external_source->HasMetadata(object))
2673 return external_source->GetMetadata(object);
2674 else
Greg Claytond8d4a572015-08-11 21:38:15 +00002675 return nullptr;
2676}
2677
Kate Stoneb9c1b512016-09-06 20:57:50 +00002678clang::DeclContext *
2679ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2680 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2681}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002682
Kate Stoneb9c1b512016-09-06 20:57:50 +00002683clang::DeclContext *
2684ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2685 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2686}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002687
Kate Stoneb9c1b512016-09-06 20:57:50 +00002688bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2689 int kind) const {
2690 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2691 if (clang_type) {
2692 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2693 if (tag_type) {
2694 clang::TagDecl *tag_decl =
2695 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2696 if (tag_decl) {
2697 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2698 return true;
2699 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002700 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002701 }
2702 return false;
2703}
2704
2705bool ClangASTContext::SetDefaultAccessForRecordFields(
2706 clang::RecordDecl *record_decl, int default_accessibility,
2707 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2708 if (record_decl) {
2709 uint32_t field_idx;
2710 clang::RecordDecl::field_iterator field, field_end;
2711 for (field = record_decl->field_begin(),
2712 field_end = record_decl->field_end(), field_idx = 0;
2713 field != field_end; ++field, ++field_idx) {
2714 // If no accessibility was assigned, assign the correct one
2715 if (field_idx < num_assigned_accessibilities &&
2716 assigned_accessibilities[field_idx] == clang::AS_none)
2717 field->setAccess((clang::AccessSpecifier)default_accessibility);
2718 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002719 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002720 }
2721 return false;
2722}
2723
2724clang::DeclContext *
2725ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2726 return GetDeclContextForType(ClangUtil::GetQualType(type));
2727}
2728
2729clang::DeclContext *
2730ClangASTContext::GetDeclContextForType(clang::QualType type) {
2731 if (type.isNull())
2732 return nullptr;
2733
2734 clang::QualType qual_type = type.getCanonicalType();
2735 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2736 switch (type_class) {
2737 case clang::Type::ObjCInterface:
2738 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2739 ->getInterface();
2740 case clang::Type::ObjCObjectPointer:
2741 return GetDeclContextForType(
2742 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2743 ->getPointeeType());
2744 case clang::Type::Record:
2745 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2746 case clang::Type::Enum:
2747 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2748 case clang::Type::Typedef:
2749 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2750 ->getDecl()
2751 ->getUnderlyingType());
2752 case clang::Type::Auto:
2753 return GetDeclContextForType(
2754 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2755 case clang::Type::Elaborated:
2756 return GetDeclContextForType(
2757 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2758 case clang::Type::Paren:
2759 return GetDeclContextForType(
2760 llvm::cast<clang::ParenType>(qual_type)->desugar());
2761 default:
2762 break;
2763 }
2764 // No DeclContext in this type...
2765 return nullptr;
2766}
2767
2768static bool GetCompleteQualType(clang::ASTContext *ast,
2769 clang::QualType qual_type,
2770 bool allow_completion = true) {
2771 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2772 switch (type_class) {
2773 case clang::Type::ConstantArray:
2774 case clang::Type::IncompleteArray:
2775 case clang::Type::VariableArray: {
2776 const clang::ArrayType *array_type =
2777 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2778
2779 if (array_type)
2780 return GetCompleteQualType(ast, array_type->getElementType(),
2781 allow_completion);
2782 } break;
2783 case clang::Type::Record: {
2784 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2785 if (cxx_record_decl) {
2786 if (cxx_record_decl->hasExternalLexicalStorage()) {
2787 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2788 const bool fields_loaded =
2789 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2790 if (is_complete && fields_loaded)
2791 return true;
2792
2793 if (!allow_completion)
2794 return false;
2795
2796 // Call the field_begin() accessor to for it to use the external source
2797 // to load the fields...
2798 clang::ExternalASTSource *external_ast_source =
2799 ast->getExternalSource();
2800 if (external_ast_source) {
2801 external_ast_source->CompleteType(cxx_record_decl);
2802 if (cxx_record_decl->isCompleteDefinition()) {
2803 cxx_record_decl->field_begin();
2804 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2805 }
2806 }
2807 }
2808 }
2809 const clang::TagType *tag_type =
2810 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2811 return !tag_type->isIncompleteType();
2812 } break;
2813
2814 case clang::Type::Enum: {
2815 const clang::TagType *tag_type =
2816 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2817 if (tag_type) {
2818 clang::TagDecl *tag_decl = tag_type->getDecl();
2819 if (tag_decl) {
2820 if (tag_decl->getDefinition())
2821 return true;
2822
2823 if (!allow_completion)
2824 return false;
2825
2826 if (tag_decl->hasExternalLexicalStorage()) {
2827 if (ast) {
2828 clang::ExternalASTSource *external_ast_source =
2829 ast->getExternalSource();
2830 if (external_ast_source) {
2831 external_ast_source->CompleteType(tag_decl);
2832 return !tag_type->isIncompleteType();
2833 }
2834 }
2835 }
2836 return false;
2837 }
2838 }
2839
2840 } break;
2841 case clang::Type::ObjCObject:
2842 case clang::Type::ObjCInterface: {
2843 const clang::ObjCObjectType *objc_class_type =
2844 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2845 if (objc_class_type) {
2846 clang::ObjCInterfaceDecl *class_interface_decl =
2847 objc_class_type->getInterface();
2848 // We currently can't complete objective C types through the newly added
Adrian Prantl05097242018-04-30 16:49:04 +00002849 // ASTContext because it only supports TagDecl objects right now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002850 if (class_interface_decl) {
2851 if (class_interface_decl->getDefinition())
2852 return true;
2853
2854 if (!allow_completion)
2855 return false;
2856
2857 if (class_interface_decl->hasExternalLexicalStorage()) {
2858 if (ast) {
2859 clang::ExternalASTSource *external_ast_source =
2860 ast->getExternalSource();
2861 if (external_ast_source) {
2862 external_ast_source->CompleteType(class_interface_decl);
2863 return !objc_class_type->isIncompleteType();
2864 }
2865 }
2866 }
2867 return false;
2868 }
2869 }
2870 } break;
2871
2872 case clang::Type::Typedef:
2873 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2874 ->getDecl()
2875 ->getUnderlyingType(),
2876 allow_completion);
2877
2878 case clang::Type::Auto:
2879 return GetCompleteQualType(
2880 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2881 allow_completion);
2882
2883 case clang::Type::Elaborated:
2884 return GetCompleteQualType(
2885 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2886 allow_completion);
2887
2888 case clang::Type::Paren:
2889 return GetCompleteQualType(
2890 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2891 allow_completion);
2892
2893 case clang::Type::Attributed:
2894 return GetCompleteQualType(
2895 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2896 allow_completion);
2897
2898 default:
2899 break;
2900 }
2901
2902 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00002903}
2904
2905static clang::ObjCIvarDecl::AccessControl
Kate Stoneb9c1b512016-09-06 20:57:50 +00002906ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2907 switch (access) {
2908 case eAccessNone:
Greg Claytond8d4a572015-08-11 21:38:15 +00002909 return clang::ObjCIvarDecl::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002910 case eAccessPublic:
2911 return clang::ObjCIvarDecl::Public;
2912 case eAccessPrivate:
2913 return clang::ObjCIvarDecl::Private;
2914 case eAccessProtected:
2915 return clang::ObjCIvarDecl::Protected;
2916 case eAccessPackage:
2917 return clang::ObjCIvarDecl::Package;
2918 }
2919 return clang::ObjCIvarDecl::None;
Greg Claytond8d4a572015-08-11 21:38:15 +00002920}
2921
Greg Claytond8d4a572015-08-11 21:38:15 +00002922// Tests
Greg Claytond8d4a572015-08-11 21:38:15 +00002923
Kate Stoneb9c1b512016-09-06 20:57:50 +00002924bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2925 clang::QualType qual_type(GetCanonicalQualType(type));
2926
2927 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2928 switch (type_class) {
2929 case clang::Type::IncompleteArray:
2930 case clang::Type::VariableArray:
2931 case clang::Type::ConstantArray:
2932 case clang::Type::ExtVector:
2933 case clang::Type::Vector:
2934 case clang::Type::Record:
2935 case clang::Type::ObjCObject:
2936 case clang::Type::ObjCInterface:
2937 return true;
2938 case clang::Type::Auto:
2939 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2940 ->getDeducedType()
2941 .getAsOpaquePtr());
2942 case clang::Type::Elaborated:
2943 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2944 ->getNamedType()
2945 .getAsOpaquePtr());
2946 case clang::Type::Typedef:
2947 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2948 ->getDecl()
2949 ->getUnderlyingType()
2950 .getAsOpaquePtr());
2951 case clang::Type::Paren:
2952 return IsAggregateType(
2953 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2954 default:
2955 break;
2956 }
2957 // The clang type does have a value
2958 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002959}
2960
Kate Stoneb9c1b512016-09-06 20:57:50 +00002961bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2962 clang::QualType qual_type(GetCanonicalQualType(type));
2963
2964 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2965 switch (type_class) {
2966 case clang::Type::Record: {
2967 if (const clang::RecordType *record_type =
2968 llvm::dyn_cast_or_null<clang::RecordType>(
2969 qual_type.getTypePtrOrNull())) {
2970 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2971 return record_decl->isAnonymousStructOrUnion();
2972 }
Enrico Granata7123e2b2015-11-07 02:06:57 +00002973 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002974 break;
2975 }
2976 case clang::Type::Auto:
2977 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2978 ->getDeducedType()
2979 .getAsOpaquePtr());
2980 case clang::Type::Elaborated:
2981 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2982 ->getNamedType()
2983 .getAsOpaquePtr());
2984 case clang::Type::Typedef:
2985 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2986 ->getDecl()
2987 ->getUnderlyingType()
2988 .getAsOpaquePtr());
2989 case clang::Type::Paren:
2990 return IsAnonymousType(
2991 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2992 default:
2993 break;
2994 }
2995 // The clang type does have a value
2996 return false;
Enrico Granata7123e2b2015-11-07 02:06:57 +00002997}
2998
Kate Stoneb9c1b512016-09-06 20:57:50 +00002999bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
3000 CompilerType *element_type_ptr,
3001 uint64_t *size, bool *is_incomplete) {
3002 clang::QualType qual_type(GetCanonicalQualType(type));
Tamas Berghammer69d0b332015-10-09 12:43:08 +00003003
Kate Stoneb9c1b512016-09-06 20:57:50 +00003004 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3005 switch (type_class) {
3006 default:
3007 break;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00003008
Kate Stoneb9c1b512016-09-06 20:57:50 +00003009 case clang::Type::ConstantArray:
Greg Claytond8d4a572015-08-11 21:38:15 +00003010 if (element_type_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003011 element_type_ptr->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003012 this, llvm::cast<clang::ConstantArrayType>(qual_type)
3013 ->getElementType()
3014 .getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003015 if (size)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003016 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
3017 ->getSize()
3018 .getLimitedValue(ULLONG_MAX);
Greg Claytond8d4a572015-08-11 21:38:15 +00003019 if (is_incomplete)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003020 *is_incomplete = false;
3021 return true;
3022
3023 case clang::Type::IncompleteArray:
3024 if (element_type_ptr)
3025 element_type_ptr->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003026 this, llvm::cast<clang::IncompleteArrayType>(qual_type)
3027 ->getElementType()
3028 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003029 if (size)
3030 *size = 0;
3031 if (is_incomplete)
3032 *is_incomplete = true;
3033 return true;
3034
3035 case clang::Type::VariableArray:
3036 if (element_type_ptr)
3037 element_type_ptr->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003038 this, llvm::cast<clang::VariableArrayType>(qual_type)
3039 ->getElementType()
3040 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003041 if (size)
3042 *size = 0;
3043 if (is_incomplete)
3044 *is_incomplete = false;
3045 return true;
3046
3047 case clang::Type::DependentSizedArray:
3048 if (element_type_ptr)
3049 element_type_ptr->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003050 this, llvm::cast<clang::DependentSizedArrayType>(qual_type)
3051 ->getElementType()
3052 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003053 if (size)
3054 *size = 0;
3055 if (is_incomplete)
3056 *is_incomplete = false;
3057 return true;
3058
3059 case clang::Type::Typedef:
3060 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
3061 ->getDecl()
3062 ->getUnderlyingType()
3063 .getAsOpaquePtr(),
3064 element_type_ptr, size, is_incomplete);
3065 case clang::Type::Auto:
3066 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
3067 ->getDeducedType()
3068 .getAsOpaquePtr(),
3069 element_type_ptr, size, is_incomplete);
3070 case clang::Type::Elaborated:
3071 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
3072 ->getNamedType()
3073 .getAsOpaquePtr(),
3074 element_type_ptr, size, is_incomplete);
3075 case clang::Type::Paren:
3076 return IsArrayType(
3077 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3078 element_type_ptr, size, is_incomplete);
3079 }
3080 if (element_type_ptr)
3081 element_type_ptr->Clear();
3082 if (size)
3083 *size = 0;
3084 if (is_incomplete)
3085 *is_incomplete = false;
3086 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003087}
3088
Kate Stoneb9c1b512016-09-06 20:57:50 +00003089bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
3090 CompilerType *element_type, uint64_t *size) {
3091 clang::QualType qual_type(GetCanonicalQualType(type));
3092
3093 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3094 switch (type_class) {
3095 case clang::Type::Vector: {
3096 const clang::VectorType *vector_type =
3097 qual_type->getAs<clang::VectorType>();
3098 if (vector_type) {
3099 if (size)
3100 *size = vector_type->getNumElements();
3101 if (element_type)
3102 *element_type =
Alex Langfordbddab072019-08-13 19:40:36 +00003103 CompilerType(this, vector_type->getElementType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003104 }
3105 return true;
3106 } break;
3107 case clang::Type::ExtVector: {
3108 const clang::ExtVectorType *ext_vector_type =
3109 qual_type->getAs<clang::ExtVectorType>();
3110 if (ext_vector_type) {
3111 if (size)
3112 *size = ext_vector_type->getNumElements();
3113 if (element_type)
3114 *element_type =
Alex Langfordbddab072019-08-13 19:40:36 +00003115 CompilerType(this, ext_vector_type->getElementType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003116 }
3117 return true;
3118 }
3119 default:
3120 break;
3121 }
3122 return false;
3123}
3124
3125bool ClangASTContext::IsRuntimeGeneratedType(
3126 lldb::opaque_compiler_type_t type) {
3127 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
3128 ->GetDeclContextForType(GetQualType(type));
3129 if (!decl_ctx)
3130 return false;
3131
3132 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
3133 return false;
3134
3135 clang::ObjCInterfaceDecl *result_iface_decl =
3136 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
3137
3138 ClangASTMetadata *ast_metadata =
3139 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
3140 if (!ast_metadata)
3141 return false;
3142 return (ast_metadata->GetISAPtr() != 0);
3143}
3144
3145bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
3146 return GetQualType(type).getUnqualifiedType()->isCharType();
3147}
3148
3149bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
3150 const bool allow_completion = false;
3151 return GetCompleteQualType(getASTContext(), GetQualType(type),
3152 allow_completion);
3153}
3154
3155bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
3156 return GetQualType(type).isConstQualified();
3157}
3158
3159bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
3160 uint32_t &length) {
3161 CompilerType pointee_or_element_clang_type;
3162 length = 0;
3163 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
3164
3165 if (!pointee_or_element_clang_type.IsValid())
3166 return false;
3167
3168 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
3169 if (pointee_or_element_clang_type.IsCharType()) {
3170 if (type_flags.Test(eTypeIsArray)) {
Adrian Prantl05097242018-04-30 16:49:04 +00003171 // We know the size of the array and it could be a C string since it is
3172 // an array of characters
Kate Stoneb9c1b512016-09-06 20:57:50 +00003173 length = llvm::cast<clang::ConstantArrayType>(
3174 GetCanonicalQualType(type).getTypePtr())
3175 ->getSize()
3176 .getLimitedValue();
3177 }
3178 return true;
3179 }
3180 }
3181 return false;
3182}
3183
3184bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
3185 bool *is_variadic_ptr) {
3186 if (type) {
3187 clang::QualType qual_type(GetCanonicalQualType(type));
3188
3189 if (qual_type->isFunctionType()) {
3190 if (is_variadic_ptr) {
3191 const clang::FunctionProtoType *function_proto_type =
3192 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3193 if (function_proto_type)
3194 *is_variadic_ptr = function_proto_type->isVariadic();
3195 else
3196 *is_variadic_ptr = false;
3197 }
3198 return true;
3199 }
3200
Greg Claytond8d4a572015-08-11 21:38:15 +00003201 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003202 switch (type_class) {
3203 default:
3204 break;
3205 case clang::Type::Typedef:
3206 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
3207 ->getDecl()
3208 ->getUnderlyingType()
3209 .getAsOpaquePtr(),
3210 nullptr);
3211 case clang::Type::Auto:
3212 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
3213 ->getDeducedType()
3214 .getAsOpaquePtr(),
3215 nullptr);
3216 case clang::Type::Elaborated:
3217 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
3218 ->getNamedType()
3219 .getAsOpaquePtr(),
3220 nullptr);
3221 case clang::Type::Paren:
3222 return IsFunctionType(
3223 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3224 nullptr);
3225 case clang::Type::LValueReference:
3226 case clang::Type::RValueReference: {
3227 const clang::ReferenceType *reference_type =
3228 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3229 if (reference_type)
3230 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
3231 nullptr);
3232 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00003233 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003234 }
3235 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003236}
3237
3238// Used to detect "Homogeneous Floating-point Aggregates"
3239uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003240ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
3241 CompilerType *base_type_ptr) {
3242 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003243 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003244
3245 clang::QualType qual_type(GetCanonicalQualType(type));
3246 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3247 switch (type_class) {
3248 case clang::Type::Record:
3249 if (GetCompleteType(type)) {
3250 const clang::CXXRecordDecl *cxx_record_decl =
3251 qual_type->getAsCXXRecordDecl();
3252 if (cxx_record_decl) {
3253 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3254 return 0;
3255 }
3256 const clang::RecordType *record_type =
3257 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3258 if (record_type) {
3259 const clang::RecordDecl *record_decl = record_type->getDecl();
3260 if (record_decl) {
3261 // We are looking for a structure that contains only floating point
3262 // types
3263 clang::RecordDecl::field_iterator field_pos,
3264 field_end = record_decl->field_end();
3265 uint32_t num_fields = 0;
3266 bool is_hva = false;
3267 bool is_hfa = false;
3268 clang::QualType base_qual_type;
3269 uint64_t base_bitwidth = 0;
3270 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3271 ++field_pos) {
3272 clang::QualType field_qual_type = field_pos->getType();
3273 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
3274 if (field_qual_type->isFloatingType()) {
3275 if (field_qual_type->isComplexType())
3276 return 0;
3277 else {
3278 if (num_fields == 0)
3279 base_qual_type = field_qual_type;
3280 else {
3281 if (is_hva)
3282 return 0;
3283 is_hfa = true;
3284 if (field_qual_type.getTypePtr() !=
3285 base_qual_type.getTypePtr())
3286 return 0;
3287 }
3288 }
3289 } else if (field_qual_type->isVectorType() ||
3290 field_qual_type->isExtVectorType()) {
3291 if (num_fields == 0) {
3292 base_qual_type = field_qual_type;
3293 base_bitwidth = field_bitwidth;
3294 } else {
3295 if (is_hfa)
3296 return 0;
3297 is_hva = true;
3298 if (base_bitwidth != field_bitwidth)
3299 return 0;
3300 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3301 return 0;
3302 }
3303 } else
3304 return 0;
3305 ++num_fields;
3306 }
3307 if (base_type_ptr)
Alex Langfordbddab072019-08-13 19:40:36 +00003308 *base_type_ptr = CompilerType(this, base_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003309 return num_fields;
3310 }
3311 }
3312 }
3313 break;
3314
3315 case clang::Type::Typedef:
3316 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3317 ->getDecl()
3318 ->getUnderlyingType()
3319 .getAsOpaquePtr(),
3320 base_type_ptr);
3321
3322 case clang::Type::Auto:
3323 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3324 ->getDeducedType()
3325 .getAsOpaquePtr(),
3326 base_type_ptr);
3327
3328 case clang::Type::Elaborated:
3329 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3330 ->getNamedType()
3331 .getAsOpaquePtr(),
3332 base_type_ptr);
3333 default:
3334 break;
3335 }
3336 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003337}
3338
Kate Stoneb9c1b512016-09-06 20:57:50 +00003339size_t ClangASTContext::GetNumberOfFunctionArguments(
3340 lldb::opaque_compiler_type_t type) {
3341 if (type) {
3342 clang::QualType qual_type(GetCanonicalQualType(type));
3343 const clang::FunctionProtoType *func =
3344 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3345 if (func)
3346 return func->getNumParams();
3347 }
3348 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003349}
3350
Greg Claytona1e5dc82015-08-11 22:53:00 +00003351CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00003352ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3353 const size_t index) {
3354 if (type) {
Greg Claytond8d4a572015-08-11 21:38:15 +00003355 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003356 const clang::FunctionProtoType *func =
3357 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3358 if (func) {
3359 if (index < func->getNumParams())
Alex Langfordbddab072019-08-13 19:40:36 +00003360 return CompilerType(this, func->getParamType(index).getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003361 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003362 }
3363 return CompilerType();
3364}
3365
3366bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3367 if (type) {
3368 clang::QualType qual_type(GetCanonicalQualType(type));
3369
3370 if (qual_type->isFunctionPointerType())
3371 return true;
3372
3373 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3374 switch (type_class) {
3375 default:
3376 break;
3377 case clang::Type::Typedef:
3378 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3379 ->getDecl()
3380 ->getUnderlyingType()
3381 .getAsOpaquePtr());
3382 case clang::Type::Auto:
3383 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3384 ->getDeducedType()
3385 .getAsOpaquePtr());
3386 case clang::Type::Elaborated:
3387 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3388 ->getNamedType()
3389 .getAsOpaquePtr());
3390 case clang::Type::Paren:
3391 return IsFunctionPointerType(
3392 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3393
3394 case clang::Type::LValueReference:
3395 case clang::Type::RValueReference: {
3396 const clang::ReferenceType *reference_type =
3397 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3398 if (reference_type)
3399 return IsFunctionPointerType(
3400 reference_type->getPointeeType().getAsOpaquePtr());
3401 } break;
3402 }
3403 }
3404 return false;
3405}
3406
3407bool ClangASTContext::IsBlockPointerType(
3408 lldb::opaque_compiler_type_t type,
3409 CompilerType *function_pointer_type_ptr) {
3410 if (type) {
3411 clang::QualType qual_type(GetCanonicalQualType(type));
3412
3413 if (qual_type->isBlockPointerType()) {
3414 if (function_pointer_type_ptr) {
3415 const clang::BlockPointerType *block_pointer_type =
3416 qual_type->getAs<clang::BlockPointerType>();
3417 QualType pointee_type = block_pointer_type->getPointeeType();
Jonas Devlieghered5b44032019-02-13 06:25:41 +00003418 QualType function_pointer_type = m_ast_up->getPointerType(pointee_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003419 *function_pointer_type_ptr =
Alex Langfordbddab072019-08-13 19:40:36 +00003420 CompilerType(this, function_pointer_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003421 }
3422 return true;
3423 }
3424
3425 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3426 switch (type_class) {
3427 default:
3428 break;
3429 case clang::Type::Typedef:
3430 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3431 ->getDecl()
3432 ->getUnderlyingType()
3433 .getAsOpaquePtr(),
3434 function_pointer_type_ptr);
3435 case clang::Type::Auto:
3436 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3437 ->getDeducedType()
3438 .getAsOpaquePtr(),
3439 function_pointer_type_ptr);
3440 case clang::Type::Elaborated:
3441 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3442 ->getNamedType()
3443 .getAsOpaquePtr(),
3444 function_pointer_type_ptr);
3445 case clang::Type::Paren:
3446 return IsBlockPointerType(
3447 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3448 function_pointer_type_ptr);
3449
3450 case clang::Type::LValueReference:
3451 case clang::Type::RValueReference: {
3452 const clang::ReferenceType *reference_type =
3453 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3454 if (reference_type)
3455 return IsBlockPointerType(
3456 reference_type->getPointeeType().getAsOpaquePtr(),
3457 function_pointer_type_ptr);
3458 } break;
3459 }
3460 }
3461 return false;
3462}
3463
3464bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3465 bool &is_signed) {
3466 if (!type)
3467 return false;
3468
3469 clang::QualType qual_type(GetCanonicalQualType(type));
3470 const clang::BuiltinType *builtin_type =
3471 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3472
3473 if (builtin_type) {
3474 if (builtin_type->isInteger()) {
3475 is_signed = builtin_type->isSignedInteger();
3476 return true;
3477 }
3478 }
3479
3480 return false;
3481}
3482
3483bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3484 bool &is_signed) {
3485 if (type) {
3486 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3487 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3488
3489 if (enum_type) {
3490 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3491 is_signed);
3492 return true;
3493 }
3494 }
3495
3496 return false;
3497}
3498
3499bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3500 CompilerType *pointee_type) {
3501 if (type) {
3502 clang::QualType qual_type(GetCanonicalQualType(type));
3503 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3504 switch (type_class) {
3505 case clang::Type::Builtin:
3506 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3507 default:
3508 break;
3509 case clang::BuiltinType::ObjCId:
3510 case clang::BuiltinType::ObjCClass:
3511 return true;
3512 }
3513 return false;
3514 case clang::Type::ObjCObjectPointer:
3515 if (pointee_type)
3516 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003517 this, llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3518 ->getPointeeType()
3519 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003520 return true;
3521 case clang::Type::BlockPointer:
3522 if (pointee_type)
3523 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003524 this, llvm::cast<clang::BlockPointerType>(qual_type)
3525 ->getPointeeType()
3526 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003527 return true;
3528 case clang::Type::Pointer:
3529 if (pointee_type)
Alex Langfordbddab072019-08-13 19:40:36 +00003530 pointee_type->SetCompilerType(this,
3531 llvm::cast<clang::PointerType>(qual_type)
3532 ->getPointeeType()
3533 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003534 return true;
3535 case clang::Type::MemberPointer:
3536 if (pointee_type)
3537 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003538 this, llvm::cast<clang::MemberPointerType>(qual_type)
3539 ->getPointeeType()
3540 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003541 return true;
3542 case clang::Type::Typedef:
3543 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3544 ->getDecl()
3545 ->getUnderlyingType()
3546 .getAsOpaquePtr(),
3547 pointee_type);
3548 case clang::Type::Auto:
3549 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3550 ->getDeducedType()
3551 .getAsOpaquePtr(),
3552 pointee_type);
3553 case clang::Type::Elaborated:
3554 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3555 ->getNamedType()
3556 .getAsOpaquePtr(),
3557 pointee_type);
3558 case clang::Type::Paren:
3559 return IsPointerType(
3560 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3561 pointee_type);
3562 default:
3563 break;
3564 }
3565 }
3566 if (pointee_type)
3567 pointee_type->Clear();
3568 return false;
3569}
3570
3571bool ClangASTContext::IsPointerOrReferenceType(
3572 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3573 if (type) {
3574 clang::QualType qual_type(GetCanonicalQualType(type));
3575 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3576 switch (type_class) {
3577 case clang::Type::Builtin:
3578 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3579 default:
3580 break;
3581 case clang::BuiltinType::ObjCId:
3582 case clang::BuiltinType::ObjCClass:
3583 return true;
3584 }
3585 return false;
3586 case clang::Type::ObjCObjectPointer:
3587 if (pointee_type)
3588 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003589 this, llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3590 ->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003591 return true;
3592 case clang::Type::BlockPointer:
3593 if (pointee_type)
3594 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003595 this, llvm::cast<clang::BlockPointerType>(qual_type)
3596 ->getPointeeType()
3597 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003598 return true;
3599 case clang::Type::Pointer:
3600 if (pointee_type)
Alex Langfordbddab072019-08-13 19:40:36 +00003601 pointee_type->SetCompilerType(this,
3602 llvm::cast<clang::PointerType>(qual_type)
3603 ->getPointeeType()
3604 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003605 return true;
3606 case clang::Type::MemberPointer:
3607 if (pointee_type)
3608 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003609 this, llvm::cast<clang::MemberPointerType>(qual_type)
3610 ->getPointeeType()
3611 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003612 return true;
3613 case clang::Type::LValueReference:
3614 if (pointee_type)
3615 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003616 this, llvm::cast<clang::LValueReferenceType>(qual_type)
3617 ->desugar()
3618 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003619 return true;
3620 case clang::Type::RValueReference:
3621 if (pointee_type)
3622 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003623 this, llvm::cast<clang::RValueReferenceType>(qual_type)
3624 ->desugar()
3625 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003626 return true;
3627 case clang::Type::Typedef:
3628 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3629 ->getDecl()
3630 ->getUnderlyingType()
3631 .getAsOpaquePtr(),
3632 pointee_type);
3633 case clang::Type::Auto:
3634 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3635 ->getDeducedType()
3636 .getAsOpaquePtr(),
3637 pointee_type);
3638 case clang::Type::Elaborated:
3639 return IsPointerOrReferenceType(
3640 llvm::cast<clang::ElaboratedType>(qual_type)
3641 ->getNamedType()
3642 .getAsOpaquePtr(),
3643 pointee_type);
3644 case clang::Type::Paren:
3645 return IsPointerOrReferenceType(
3646 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3647 pointee_type);
3648 default:
3649 break;
3650 }
3651 }
3652 if (pointee_type)
3653 pointee_type->Clear();
3654 return false;
3655}
3656
3657bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3658 CompilerType *pointee_type,
3659 bool *is_rvalue) {
3660 if (type) {
3661 clang::QualType qual_type(GetCanonicalQualType(type));
3662 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3663
3664 switch (type_class) {
3665 case clang::Type::LValueReference:
3666 if (pointee_type)
3667 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003668 this, llvm::cast<clang::LValueReferenceType>(qual_type)
3669 ->desugar()
3670 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003671 if (is_rvalue)
3672 *is_rvalue = false;
3673 return true;
3674 case clang::Type::RValueReference:
3675 if (pointee_type)
3676 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003677 this, llvm::cast<clang::RValueReferenceType>(qual_type)
3678 ->desugar()
3679 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003680 if (is_rvalue)
3681 *is_rvalue = true;
3682 return true;
3683 case clang::Type::Typedef:
3684 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3685 ->getDecl()
3686 ->getUnderlyingType()
3687 .getAsOpaquePtr(),
3688 pointee_type, is_rvalue);
3689 case clang::Type::Auto:
3690 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3691 ->getDeducedType()
3692 .getAsOpaquePtr(),
3693 pointee_type, is_rvalue);
3694 case clang::Type::Elaborated:
3695 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3696 ->getNamedType()
3697 .getAsOpaquePtr(),
3698 pointee_type, is_rvalue);
3699 case clang::Type::Paren:
3700 return IsReferenceType(
3701 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3702 pointee_type, is_rvalue);
3703
3704 default:
3705 break;
3706 }
3707 }
3708 if (pointee_type)
3709 pointee_type->Clear();
3710 return false;
3711}
3712
3713bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3714 uint32_t &count, bool &is_complex) {
3715 if (type) {
3716 clang::QualType qual_type(GetCanonicalQualType(type));
3717
3718 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3719 qual_type->getCanonicalTypeInternal())) {
3720 clang::BuiltinType::Kind kind = BT->getKind();
3721 if (kind >= clang::BuiltinType::Float &&
3722 kind <= clang::BuiltinType::LongDouble) {
3723 count = 1;
3724 is_complex = false;
3725 return true;
3726 }
3727 } else if (const clang::ComplexType *CT =
3728 llvm::dyn_cast<clang::ComplexType>(
3729 qual_type->getCanonicalTypeInternal())) {
3730 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3731 is_complex)) {
3732 count = 2;
3733 is_complex = true;
3734 return true;
3735 }
3736 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3737 qual_type->getCanonicalTypeInternal())) {
3738 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3739 is_complex)) {
3740 count = VT->getNumElements();
3741 is_complex = false;
3742 return true;
3743 }
3744 }
3745 }
3746 count = 0;
3747 is_complex = false;
3748 return false;
3749}
3750
3751bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3752 if (!type)
3753 return false;
3754
3755 clang::QualType qual_type(GetQualType(type));
3756 const clang::TagType *tag_type =
3757 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3758 if (tag_type) {
3759 clang::TagDecl *tag_decl = tag_type->getDecl();
3760 if (tag_decl)
3761 return tag_decl->isCompleteDefinition();
3762 return false;
3763 } else {
3764 const clang::ObjCObjectType *objc_class_type =
3765 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3766 if (objc_class_type) {
3767 clang::ObjCInterfaceDecl *class_interface_decl =
3768 objc_class_type->getInterface();
3769 if (class_interface_decl)
3770 return class_interface_decl->getDefinition() != nullptr;
3771 return false;
3772 }
3773 }
3774 return true;
3775}
3776
3777bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
3778 if (type) {
3779 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3780
3781 const clang::ObjCObjectPointerType *obj_pointer_type =
3782 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3783
3784 if (obj_pointer_type)
3785 return obj_pointer_type->isObjCClassType();
3786 }
3787 return false;
3788}
3789
3790bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3791 if (ClangUtil::IsClangType(type))
3792 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3793 return false;
3794}
3795
3796bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3797 if (!type)
3798 return false;
3799 clang::QualType qual_type(GetCanonicalQualType(type));
3800 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3801 return (type_class == clang::Type::Record);
3802}
3803
3804bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3805 if (!type)
3806 return false;
3807 clang::QualType qual_type(GetCanonicalQualType(type));
3808 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3809 return (type_class == clang::Type::Enum);
3810}
3811
3812bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3813 if (type) {
3814 clang::QualType qual_type(GetCanonicalQualType(type));
3815 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3816 switch (type_class) {
3817 case clang::Type::Record:
3818 if (GetCompleteType(type)) {
3819 const clang::RecordType *record_type =
3820 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3821 const clang::RecordDecl *record_decl = record_type->getDecl();
3822 if (record_decl) {
3823 const clang::CXXRecordDecl *cxx_record_decl =
3824 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3825 if (cxx_record_decl)
3826 return cxx_record_decl->isPolymorphic();
Greg Claytond8d4a572015-08-11 21:38:15 +00003827 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003828 }
3829 break;
3830
3831 default:
3832 break;
3833 }
3834 }
3835 return false;
3836}
3837
3838bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3839 CompilerType *dynamic_pointee_type,
3840 bool check_cplusplus,
3841 bool check_objc) {
3842 clang::QualType pointee_qual_type;
3843 if (type) {
3844 clang::QualType qual_type(GetCanonicalQualType(type));
3845 bool success = false;
3846 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3847 switch (type_class) {
3848 case clang::Type::Builtin:
3849 if (check_objc &&
3850 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3851 clang::BuiltinType::ObjCId) {
3852 if (dynamic_pointee_type)
3853 dynamic_pointee_type->SetCompilerType(this, type);
3854 return true;
3855 }
3856 break;
3857
3858 case clang::Type::ObjCObjectPointer:
3859 if (check_objc) {
3860 if (auto objc_pointee_type =
3861 qual_type->getPointeeType().getTypePtrOrNull()) {
3862 if (auto objc_object_type =
3863 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3864 objc_pointee_type)) {
3865 if (objc_object_type->isObjCClass())
3866 return false;
3867 }
3868 }
3869 if (dynamic_pointee_type)
3870 dynamic_pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003871 this, llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3872 ->getPointeeType()
3873 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003874 return true;
3875 }
3876 break;
3877
3878 case clang::Type::Pointer:
3879 pointee_qual_type =
3880 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3881 success = true;
3882 break;
3883
3884 case clang::Type::LValueReference:
3885 case clang::Type::RValueReference:
3886 pointee_qual_type =
3887 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3888 success = true;
3889 break;
3890
3891 case clang::Type::Typedef:
3892 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3893 ->getDecl()
3894 ->getUnderlyingType()
3895 .getAsOpaquePtr(),
3896 dynamic_pointee_type, check_cplusplus,
3897 check_objc);
3898
3899 case clang::Type::Auto:
3900 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3901 ->getDeducedType()
3902 .getAsOpaquePtr(),
3903 dynamic_pointee_type, check_cplusplus,
3904 check_objc);
3905
3906 case clang::Type::Elaborated:
3907 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3908 ->getNamedType()
3909 .getAsOpaquePtr(),
3910 dynamic_pointee_type, check_cplusplus,
3911 check_objc);
3912
3913 case clang::Type::Paren:
3914 return IsPossibleDynamicType(
3915 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3916 dynamic_pointee_type, check_cplusplus, check_objc);
3917 default:
3918 break;
3919 }
3920
3921 if (success) {
3922 // Check to make sure what we are pointing too is a possible dynamic C++
Adrian Prantl05097242018-04-30 16:49:04 +00003923 // type We currently accept any "void *" (in case we have a class that
3924 // has been watered down to an opaque pointer) and virtual C++ classes.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003925 const clang::Type::TypeClass pointee_type_class =
3926 pointee_qual_type.getCanonicalType()->getTypeClass();
3927 switch (pointee_type_class) {
3928 case clang::Type::Builtin:
3929 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3930 case clang::BuiltinType::UnknownAny:
3931 case clang::BuiltinType::Void:
3932 if (dynamic_pointee_type)
Alex Langfordbddab072019-08-13 19:40:36 +00003933 dynamic_pointee_type->SetCompilerType(
3934 this, pointee_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003935 return true;
3936 default:
3937 break;
3938 }
3939 break;
3940
3941 case clang::Type::Record:
3942 if (check_cplusplus) {
3943 clang::CXXRecordDecl *cxx_record_decl =
3944 pointee_qual_type->getAsCXXRecordDecl();
3945 if (cxx_record_decl) {
3946 bool is_complete = cxx_record_decl->isCompleteDefinition();
3947
3948 if (is_complete)
3949 success = cxx_record_decl->isDynamicClass();
3950 else {
3951 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3952 getASTContext(), cxx_record_decl);
3953 if (metadata)
3954 success = metadata->GetIsDynamicCXXType();
3955 else {
Alex Langfordbddab072019-08-13 19:40:36 +00003956 is_complete =
3957 CompilerType(this, pointee_qual_type.getAsOpaquePtr())
3958 .GetCompleteType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003959 if (is_complete)
3960 success = cxx_record_decl->isDynamicClass();
3961 else
3962 success = false;
3963 }
3964 }
3965
3966 if (success) {
3967 if (dynamic_pointee_type)
Alex Langfordbddab072019-08-13 19:40:36 +00003968 dynamic_pointee_type->SetCompilerType(
3969 this, pointee_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003970 return true;
3971 }
3972 }
3973 }
3974 break;
3975
3976 case clang::Type::ObjCObject:
3977 case clang::Type::ObjCInterface:
3978 if (check_objc) {
3979 if (dynamic_pointee_type)
Alex Langfordbddab072019-08-13 19:40:36 +00003980 dynamic_pointee_type->SetCompilerType(
3981 this, pointee_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003982 return true;
3983 }
3984 break;
3985
3986 default:
3987 break;
3988 }
3989 }
3990 }
3991 if (dynamic_pointee_type)
3992 dynamic_pointee_type->Clear();
3993 return false;
3994}
3995
3996bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3997 if (!type)
3998 return false;
3999
4000 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
4001}
4002
4003bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
4004 if (!type)
4005 return false;
4006 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
4007}
4008
4009bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
4010 if (!type)
4011 return false;
4012 return GetCanonicalQualType(type)->isVoidType();
4013}
4014
Alex Langforda03e2b22019-06-04 19:29:59 +00004015bool ClangASTContext::CanPassInRegisters(const CompilerType &type) {
4016 if (auto *record_decl =
4017 ClangASTContext::GetAsRecordDecl(type)) {
4018 return record_decl->canPassInRegisters();
4019 }
4020 return false;
4021}
4022
Kate Stoneb9c1b512016-09-06 20:57:50 +00004023bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
4024 return ClangASTContextSupportsLanguage(language);
4025}
4026
4027bool ClangASTContext::GetCXXClassName(const CompilerType &type,
4028 std::string &class_name) {
4029 if (type) {
4030 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
4031 if (!qual_type.isNull()) {
4032 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
4033 if (cxx_record_decl) {
4034 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
4035 return true;
4036 }
4037 }
4038 }
4039 class_name.clear();
4040 return false;
4041}
4042
4043bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
4044 if (!type)
4045 return false;
4046
4047 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Jonas Devliegherea6682a42018-12-15 00:15:33 +00004048 return !qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004049}
4050
4051bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
4052 if (!type)
4053 return false;
4054 clang::QualType qual_type(GetCanonicalQualType(type));
4055 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
4056 if (tag_type)
4057 return tag_type->isBeingDefined();
4058 return false;
4059}
4060
4061bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
4062 CompilerType *class_type_ptr) {
4063 if (!type)
4064 return false;
4065
4066 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
4067
4068 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
4069 if (class_type_ptr) {
4070 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
4071 const clang::ObjCObjectPointerType *obj_pointer_type =
4072 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
4073 if (obj_pointer_type == nullptr)
4074 class_type_ptr->Clear();
4075 else
4076 class_type_ptr->SetCompilerType(
4077 type.GetTypeSystem(),
4078 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
4079 .getAsOpaquePtr());
4080 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004081 }
4082 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004083 }
4084 if (class_type_ptr)
4085 class_type_ptr->Clear();
4086 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00004087}
4088
Kate Stoneb9c1b512016-09-06 20:57:50 +00004089bool ClangASTContext::GetObjCClassName(const CompilerType &type,
4090 std::string &class_name) {
4091 if (!type)
4092 return false;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00004093
Kate Stoneb9c1b512016-09-06 20:57:50 +00004094 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
4095
4096 const clang::ObjCObjectType *object_type =
4097 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
4098 if (object_type) {
4099 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
4100 if (interface) {
4101 class_name = interface->getNameAsString();
4102 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00004103 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004104 }
4105 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00004106}
4107
Greg Claytond8d4a572015-08-11 21:38:15 +00004108// Type Completion
Greg Claytond8d4a572015-08-11 21:38:15 +00004109
Kate Stoneb9c1b512016-09-06 20:57:50 +00004110bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
4111 if (!type)
4112 return false;
4113 const bool allow_completion = true;
4114 return GetCompleteQualType(getASTContext(), GetQualType(type),
4115 allow_completion);
Greg Claytond8d4a572015-08-11 21:38:15 +00004116}
4117
Kate Stoneb9c1b512016-09-06 20:57:50 +00004118ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
4119 std::string type_name;
4120 if (type) {
4121 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
4122 clang::QualType qual_type(GetQualType(type));
4123 printing_policy.SuppressTagKeyword = true;
4124 const clang::TypedefType *typedef_type =
4125 qual_type->getAs<clang::TypedefType>();
4126 if (typedef_type) {
4127 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
4128 type_name = typedef_decl->getQualifiedNameAsString();
4129 } else {
4130 type_name = qual_type.getAsString(printing_policy);
Greg Claytond8d4a572015-08-11 21:38:15 +00004131 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004132 }
4133 return ConstString(type_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00004134}
4135
4136uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004137ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
4138 CompilerType *pointee_or_element_clang_type) {
4139 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004140 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004141
4142 if (pointee_or_element_clang_type)
4143 pointee_or_element_clang_type->Clear();
4144
4145 clang::QualType qual_type(GetQualType(type));
4146
4147 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4148 switch (type_class) {
Sean Callananddf802a2017-06-02 01:24:18 +00004149 case clang::Type::Attributed:
4150 return GetTypeInfo(
4151 qual_type->getAs<clang::AttributedType>()
4152 ->getModifiedType().getAsOpaquePtr(),
4153 pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004154 case clang::Type::Builtin: {
4155 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
4156 qual_type->getCanonicalTypeInternal());
4157
4158 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
4159 switch (builtin_type->getKind()) {
4160 case clang::BuiltinType::ObjCId:
4161 case clang::BuiltinType::ObjCClass:
4162 if (pointee_or_element_clang_type)
4163 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004164 this, getASTContext()->ObjCBuiltinClassTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004165 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
4166 break;
4167
4168 case clang::BuiltinType::ObjCSel:
4169 if (pointee_or_element_clang_type)
Alex Langfordbddab072019-08-13 19:40:36 +00004170 pointee_or_element_clang_type->SetCompilerType(
4171 this, getASTContext()->CharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004172 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
4173 break;
4174
4175 case clang::BuiltinType::Bool:
4176 case clang::BuiltinType::Char_U:
4177 case clang::BuiltinType::UChar:
4178 case clang::BuiltinType::WChar_U:
4179 case clang::BuiltinType::Char16:
4180 case clang::BuiltinType::Char32:
4181 case clang::BuiltinType::UShort:
4182 case clang::BuiltinType::UInt:
4183 case clang::BuiltinType::ULong:
4184 case clang::BuiltinType::ULongLong:
4185 case clang::BuiltinType::UInt128:
4186 case clang::BuiltinType::Char_S:
4187 case clang::BuiltinType::SChar:
4188 case clang::BuiltinType::WChar_S:
4189 case clang::BuiltinType::Short:
4190 case clang::BuiltinType::Int:
4191 case clang::BuiltinType::Long:
4192 case clang::BuiltinType::LongLong:
4193 case clang::BuiltinType::Int128:
4194 case clang::BuiltinType::Float:
4195 case clang::BuiltinType::Double:
4196 case clang::BuiltinType::LongDouble:
4197 builtin_type_flags |= eTypeIsScalar;
4198 if (builtin_type->isInteger()) {
4199 builtin_type_flags |= eTypeIsInteger;
4200 if (builtin_type->isSignedInteger())
4201 builtin_type_flags |= eTypeIsSigned;
4202 } else if (builtin_type->isFloatingPoint())
4203 builtin_type_flags |= eTypeIsFloat;
4204 break;
4205 default:
4206 break;
4207 }
4208 return builtin_type_flags;
4209 }
4210
4211 case clang::Type::BlockPointer:
4212 if (pointee_or_element_clang_type)
4213 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004214 this, qual_type->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004215 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
4216
4217 case clang::Type::Complex: {
4218 uint32_t complex_type_flags =
4219 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
4220 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
4221 qual_type->getCanonicalTypeInternal());
4222 if (complex_type) {
4223 clang::QualType complex_element_type(complex_type->getElementType());
4224 if (complex_element_type->isIntegerType())
4225 complex_type_flags |= eTypeIsFloat;
4226 else if (complex_element_type->isFloatingType())
4227 complex_type_flags |= eTypeIsInteger;
4228 }
4229 return complex_type_flags;
4230 } break;
4231
4232 case clang::Type::ConstantArray:
4233 case clang::Type::DependentSizedArray:
4234 case clang::Type::IncompleteArray:
4235 case clang::Type::VariableArray:
4236 if (pointee_or_element_clang_type)
4237 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004238 this, llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
4239 ->getElementType()
4240 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004241 return eTypeHasChildren | eTypeIsArray;
4242
4243 case clang::Type::DependentName:
4244 return 0;
4245 case clang::Type::DependentSizedExtVector:
4246 return eTypeHasChildren | eTypeIsVector;
4247 case clang::Type::DependentTemplateSpecialization:
4248 return eTypeIsTemplate;
4249 case clang::Type::Decltype:
Alex Langfordbddab072019-08-13 19:40:36 +00004250 return CompilerType(this, llvm::cast<clang::DecltypeType>(qual_type)
4251 ->getUnderlyingType()
4252 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004253 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004254
4255 case clang::Type::Enum:
4256 if (pointee_or_element_clang_type)
4257 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004258 this, llvm::cast<clang::EnumType>(qual_type)
4259 ->getDecl()
4260 ->getIntegerType()
4261 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004262 return eTypeIsEnumeration | eTypeHasValue;
4263
4264 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00004265 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
4266 ->getDeducedType()
4267 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004268 .GetTypeInfo(pointee_or_element_clang_type);
4269 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00004270 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
4271 ->getNamedType()
4272 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004273 .GetTypeInfo(pointee_or_element_clang_type);
4274 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00004275 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
4276 ->desugar()
4277 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004278 .GetTypeInfo(pointee_or_element_clang_type);
4279
4280 case clang::Type::FunctionProto:
4281 return eTypeIsFuncPrototype | eTypeHasValue;
4282 case clang::Type::FunctionNoProto:
4283 return eTypeIsFuncPrototype | eTypeHasValue;
4284 case clang::Type::InjectedClassName:
4285 return 0;
4286
4287 case clang::Type::LValueReference:
4288 case clang::Type::RValueReference:
4289 if (pointee_or_element_clang_type)
4290 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004291 this, llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4292 ->getPointeeType()
4293 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004294 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4295
4296 case clang::Type::MemberPointer:
4297 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4298
4299 case clang::Type::ObjCObjectPointer:
4300 if (pointee_or_element_clang_type)
4301 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004302 this, qual_type->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004303 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4304 eTypeHasValue;
4305
4306 case clang::Type::ObjCObject:
4307 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4308 case clang::Type::ObjCInterface:
4309 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4310
4311 case clang::Type::Pointer:
4312 if (pointee_or_element_clang_type)
4313 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004314 this, qual_type->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004315 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4316
4317 case clang::Type::Record:
4318 if (qual_type->getAsCXXRecordDecl())
4319 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4320 else
4321 return eTypeHasChildren | eTypeIsStructUnion;
4322 break;
4323 case clang::Type::SubstTemplateTypeParm:
4324 return eTypeIsTemplate;
4325 case clang::Type::TemplateTypeParm:
4326 return eTypeIsTemplate;
4327 case clang::Type::TemplateSpecialization:
4328 return eTypeIsTemplate;
4329
4330 case clang::Type::Typedef:
4331 return eTypeIsTypedef |
Alex Langfordbddab072019-08-13 19:40:36 +00004332 CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
4333 ->getDecl()
4334 ->getUnderlyingType()
4335 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004336 .GetTypeInfo(pointee_or_element_clang_type);
4337 case clang::Type::TypeOfExpr:
Alex Langfordbddab072019-08-13 19:40:36 +00004338 return CompilerType(this, llvm::cast<clang::TypeOfExprType>(qual_type)
4339 ->getUnderlyingExpr()
4340 ->getType()
4341 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004342 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004343 case clang::Type::TypeOf:
Alex Langfordbddab072019-08-13 19:40:36 +00004344 return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type)
4345 ->getUnderlyingType()
4346 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004347 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004348 case clang::Type::UnresolvedUsing:
4349 return 0;
4350
4351 case clang::Type::ExtVector:
4352 case clang::Type::Vector: {
4353 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4354 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4355 qual_type->getCanonicalTypeInternal());
4356 if (vector_type) {
4357 if (vector_type->isIntegerType())
4358 vector_type_flags |= eTypeIsFloat;
4359 else if (vector_type->isFloatingType())
4360 vector_type_flags |= eTypeIsInteger;
4361 }
4362 return vector_type_flags;
4363 }
4364 default:
4365 return 0;
4366 }
4367 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004368}
4369
Greg Claytond8d4a572015-08-11 21:38:15 +00004370lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004371ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4372 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004373 return lldb::eLanguageTypeC;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004374
4375 // If the type is a reference, then resolve it to what it refers to first:
4376 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4377 if (qual_type->isAnyPointerType()) {
4378 if (qual_type->isObjCObjectPointerType())
4379 return lldb::eLanguageTypeObjC;
Adrian Prantl1db0f0c2019-05-02 23:07:23 +00004380 if (qual_type->getPointeeCXXRecordDecl())
4381 return lldb::eLanguageTypeC_plus_plus;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004382
4383 clang::QualType pointee_type(qual_type->getPointeeType());
Adrian Prantl1db0f0c2019-05-02 23:07:23 +00004384 if (pointee_type->getPointeeCXXRecordDecl())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004385 return lldb::eLanguageTypeC_plus_plus;
4386 if (pointee_type->isObjCObjectOrInterfaceType())
4387 return lldb::eLanguageTypeObjC;
4388 if (pointee_type->isObjCClassType())
4389 return lldb::eLanguageTypeObjC;
4390 if (pointee_type.getTypePtr() ==
4391 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4392 return lldb::eLanguageTypeObjC;
4393 } else {
4394 if (qual_type->isObjCObjectOrInterfaceType())
4395 return lldb::eLanguageTypeObjC;
4396 if (qual_type->getAsCXXRecordDecl())
4397 return lldb::eLanguageTypeC_plus_plus;
4398 switch (qual_type->getTypeClass()) {
4399 default:
4400 break;
4401 case clang::Type::Builtin:
4402 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4403 default:
4404 case clang::BuiltinType::Void:
4405 case clang::BuiltinType::Bool:
4406 case clang::BuiltinType::Char_U:
4407 case clang::BuiltinType::UChar:
4408 case clang::BuiltinType::WChar_U:
4409 case clang::BuiltinType::Char16:
4410 case clang::BuiltinType::Char32:
4411 case clang::BuiltinType::UShort:
4412 case clang::BuiltinType::UInt:
4413 case clang::BuiltinType::ULong:
4414 case clang::BuiltinType::ULongLong:
4415 case clang::BuiltinType::UInt128:
4416 case clang::BuiltinType::Char_S:
4417 case clang::BuiltinType::SChar:
4418 case clang::BuiltinType::WChar_S:
4419 case clang::BuiltinType::Short:
4420 case clang::BuiltinType::Int:
4421 case clang::BuiltinType::Long:
4422 case clang::BuiltinType::LongLong:
4423 case clang::BuiltinType::Int128:
4424 case clang::BuiltinType::Float:
4425 case clang::BuiltinType::Double:
4426 case clang::BuiltinType::LongDouble:
4427 break;
4428
4429 case clang::BuiltinType::NullPtr:
4430 return eLanguageTypeC_plus_plus;
4431
4432 case clang::BuiltinType::ObjCId:
4433 case clang::BuiltinType::ObjCClass:
4434 case clang::BuiltinType::ObjCSel:
4435 return eLanguageTypeObjC;
4436
4437 case clang::BuiltinType::Dependent:
4438 case clang::BuiltinType::Overload:
4439 case clang::BuiltinType::BoundMember:
4440 case clang::BuiltinType::UnknownAny:
4441 break;
4442 }
4443 break;
4444 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00004445 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
4446 ->getDecl()
4447 ->getUnderlyingType()
4448 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004449 .GetMinimumLanguage();
4450 }
4451 }
4452 return lldb::eLanguageTypeC;
Greg Claytond8d4a572015-08-11 21:38:15 +00004453}
4454
4455lldb::TypeClass
Kate Stoneb9c1b512016-09-06 20:57:50 +00004456ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4457 if (!type)
4458 return lldb::eTypeClassInvalid;
4459
4460 clang::QualType qual_type(GetQualType(type));
4461
4462 switch (qual_type->getTypeClass()) {
4463 case clang::Type::UnaryTransform:
4464 break;
4465 case clang::Type::FunctionNoProto:
4466 return lldb::eTypeClassFunction;
4467 case clang::Type::FunctionProto:
4468 return lldb::eTypeClassFunction;
4469 case clang::Type::IncompleteArray:
4470 return lldb::eTypeClassArray;
4471 case clang::Type::VariableArray:
4472 return lldb::eTypeClassArray;
4473 case clang::Type::ConstantArray:
4474 return lldb::eTypeClassArray;
4475 case clang::Type::DependentSizedArray:
4476 return lldb::eTypeClassArray;
4477 case clang::Type::DependentSizedExtVector:
4478 return lldb::eTypeClassVector;
Fangrui Song8f284882018-07-13 22:40:40 +00004479 case clang::Type::DependentVector:
4480 return lldb::eTypeClassVector;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004481 case clang::Type::ExtVector:
4482 return lldb::eTypeClassVector;
4483 case clang::Type::Vector:
4484 return lldb::eTypeClassVector;
4485 case clang::Type::Builtin:
4486 return lldb::eTypeClassBuiltin;
4487 case clang::Type::ObjCObjectPointer:
4488 return lldb::eTypeClassObjCObjectPointer;
4489 case clang::Type::BlockPointer:
4490 return lldb::eTypeClassBlockPointer;
4491 case clang::Type::Pointer:
4492 return lldb::eTypeClassPointer;
4493 case clang::Type::LValueReference:
4494 return lldb::eTypeClassReference;
4495 case clang::Type::RValueReference:
4496 return lldb::eTypeClassReference;
4497 case clang::Type::MemberPointer:
4498 return lldb::eTypeClassMemberPointer;
4499 case clang::Type::Complex:
4500 if (qual_type->isComplexType())
4501 return lldb::eTypeClassComplexFloat;
4502 else
4503 return lldb::eTypeClassComplexInteger;
4504 case clang::Type::ObjCObject:
4505 return lldb::eTypeClassObjCObject;
4506 case clang::Type::ObjCInterface:
4507 return lldb::eTypeClassObjCInterface;
4508 case clang::Type::Record: {
4509 const clang::RecordType *record_type =
4510 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4511 const clang::RecordDecl *record_decl = record_type->getDecl();
4512 if (record_decl->isUnion())
4513 return lldb::eTypeClassUnion;
4514 else if (record_decl->isStruct())
4515 return lldb::eTypeClassStruct;
4516 else
4517 return lldb::eTypeClassClass;
4518 } break;
4519 case clang::Type::Enum:
4520 return lldb::eTypeClassEnumeration;
4521 case clang::Type::Typedef:
4522 return lldb::eTypeClassTypedef;
4523 case clang::Type::UnresolvedUsing:
4524 break;
4525 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00004526 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
4527 ->desugar()
4528 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004529 .GetTypeClass();
4530 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00004531 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
4532 ->getDeducedType()
4533 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004534 .GetTypeClass();
4535 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00004536 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
4537 ->getNamedType()
4538 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004539 .GetTypeClass();
4540
4541 case clang::Type::Attributed:
4542 break;
4543 case clang::Type::TemplateTypeParm:
4544 break;
4545 case clang::Type::SubstTemplateTypeParm:
4546 break;
4547 case clang::Type::SubstTemplateTypeParmPack:
4548 break;
4549 case clang::Type::InjectedClassName:
4550 break;
4551 case clang::Type::DependentName:
4552 break;
4553 case clang::Type::DependentTemplateSpecialization:
4554 break;
4555 case clang::Type::PackExpansion:
4556 break;
4557
4558 case clang::Type::TypeOfExpr:
Alex Langfordbddab072019-08-13 19:40:36 +00004559 return CompilerType(this, llvm::cast<clang::TypeOfExprType>(qual_type)
4560 ->getUnderlyingExpr()
4561 ->getType()
4562 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004563 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004564 case clang::Type::TypeOf:
Alex Langfordbddab072019-08-13 19:40:36 +00004565 return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type)
4566 ->getUnderlyingType()
4567 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004568 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004569 case clang::Type::Decltype:
Alex Langfordbddab072019-08-13 19:40:36 +00004570 return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type)
4571 ->getUnderlyingType()
4572 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004573 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004574 case clang::Type::TemplateSpecialization:
4575 break;
Pavel Labath4f19fce22017-02-17 13:39:50 +00004576 case clang::Type::DeducedTemplateSpecialization:
4577 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004578 case clang::Type::Atomic:
4579 break;
4580 case clang::Type::Pipe:
4581 break;
4582
4583 // pointer type decayed from an array or function type.
4584 case clang::Type::Decayed:
4585 break;
4586 case clang::Type::Adjusted:
4587 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00004588 case clang::Type::ObjCTypeParam:
4589 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00004590
4591 case clang::Type::DependentAddressSpace:
4592 break;
Krasimir Georgiev435e76a2019-05-07 13:59:30 +00004593 case clang::Type::MacroQualified:
4594 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004595 }
4596 // We don't know hot to display this type...
4597 return lldb::eTypeClassOther;
Greg Claytond8d4a572015-08-11 21:38:15 +00004598}
4599
Kate Stoneb9c1b512016-09-06 20:57:50 +00004600unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4601 if (type)
4602 return GetQualType(type).getQualifiers().getCVRQualifiers();
4603 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004604}
4605
Greg Claytond8d4a572015-08-11 21:38:15 +00004606// Creating related types
Greg Claytond8d4a572015-08-11 21:38:15 +00004607
Greg Claytona1e5dc82015-08-11 22:53:00 +00004608CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004609ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4610 uint64_t *stride) {
4611 if (type) {
4612 clang::QualType qual_type(GetCanonicalQualType(type));
4613
4614 const clang::Type *array_eletype =
4615 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4616
4617 if (!array_eletype)
4618 return CompilerType();
4619
Alex Langfordbddab072019-08-13 19:40:36 +00004620 CompilerType element_type(
4621 this, array_eletype->getCanonicalTypeUnqualified().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004622
4623 // TODO: the real stride will be >= this value.. find the real one!
4624 if (stride)
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00004625 if (Optional<uint64_t> size = element_type.GetByteSize(nullptr))
Adrian Prantld963a7c2019-01-15 18:07:52 +00004626 *stride = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004627
4628 return element_type;
4629 }
4630 return CompilerType();
4631}
4632
4633CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4634 uint64_t size) {
4635 if (type) {
4636 clang::QualType qual_type(GetCanonicalQualType(type));
4637 if (clang::ASTContext *ast_ctx = getASTContext()) {
4638 if (size != 0)
4639 return CompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004640 this, ast_ctx
4641 ->getConstantArrayType(
4642 qual_type, llvm::APInt(64, size),
4643 clang::ArrayType::ArraySizeModifier::Normal, 0)
4644 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004645 else
4646 return CompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004647 this,
4648 ast_ctx
4649 ->getIncompleteArrayType(
4650 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0)
4651 .getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00004652 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004653 }
4654
4655 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004656}
4657
Greg Claytona1e5dc82015-08-11 22:53:00 +00004658CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004659ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4660 if (type)
Alex Langfordbddab072019-08-13 19:40:36 +00004661 return CompilerType(this, GetCanonicalQualType(type).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004662 return CompilerType();
4663}
4664
4665static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4666 clang::QualType qual_type) {
4667 if (qual_type->isPointerType())
4668 qual_type = ast->getPointerType(
4669 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4670 else
4671 qual_type = qual_type.getUnqualifiedType();
4672 qual_type.removeLocalConst();
4673 qual_type.removeLocalRestrict();
4674 qual_type.removeLocalVolatile();
4675 return qual_type;
Enrico Granata639392f2016-08-30 20:39:58 +00004676}
4677
4678CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004679ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4680 if (type)
4681 return CompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004682 this,
4683 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004684 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004685}
4686
Kate Stoneb9c1b512016-09-06 20:57:50 +00004687int ClangASTContext::GetFunctionArgumentCount(
4688 lldb::opaque_compiler_type_t type) {
4689 if (type) {
4690 const clang::FunctionProtoType *func =
4691 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4692 if (func)
4693 return func->getNumParams();
4694 }
4695 return -1;
4696}
4697
4698CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4699 lldb::opaque_compiler_type_t type, size_t idx) {
4700 if (type) {
4701 const clang::FunctionProtoType *func =
4702 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4703 if (func) {
4704 const uint32_t num_args = func->getNumParams();
4705 if (idx < num_args)
Alex Langfordbddab072019-08-13 19:40:36 +00004706 return CompilerType(this, func->getParamType(idx).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004707 }
4708 }
4709 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004710}
4711
Greg Claytona1e5dc82015-08-11 22:53:00 +00004712CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004713ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4714 if (type) {
4715 clang::QualType qual_type(GetQualType(type));
4716 const clang::FunctionProtoType *func =
4717 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4718 if (func)
Alex Langfordbddab072019-08-13 19:40:36 +00004719 return CompilerType(this, func->getReturnType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004720 }
4721 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004722}
4723
4724size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004725ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4726 size_t num_functions = 0;
4727 if (type) {
4728 clang::QualType qual_type(GetCanonicalQualType(type));
4729 switch (qual_type->getTypeClass()) {
4730 case clang::Type::Record:
4731 if (GetCompleteQualType(getASTContext(), qual_type)) {
4732 const clang::RecordType *record_type =
4733 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4734 const clang::RecordDecl *record_decl = record_type->getDecl();
4735 assert(record_decl);
4736 const clang::CXXRecordDecl *cxx_record_decl =
4737 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4738 if (cxx_record_decl)
4739 num_functions = std::distance(cxx_record_decl->method_begin(),
4740 cxx_record_decl->method_end());
4741 }
4742 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004743
Sean Callananf9c622a2016-09-30 18:44:43 +00004744 case clang::Type::ObjCObjectPointer: {
4745 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004746 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004747 const clang::ObjCInterfaceType *objc_interface_type =
4748 objc_class_type->getInterfaceType();
4749 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004750 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4751 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004752 clang::ObjCInterfaceDecl *class_interface_decl =
4753 objc_interface_type->getDecl();
4754 if (class_interface_decl) {
4755 num_functions = std::distance(class_interface_decl->meth_begin(),
4756 class_interface_decl->meth_end());
Greg Claytond8d4a572015-08-11 21:38:15 +00004757 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004758 }
4759 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004760 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004761
4762 case clang::Type::ObjCObject:
4763 case clang::Type::ObjCInterface:
4764 if (GetCompleteType(type)) {
4765 const clang::ObjCObjectType *objc_class_type =
4766 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4767 if (objc_class_type) {
4768 clang::ObjCInterfaceDecl *class_interface_decl =
4769 objc_class_type->getInterface();
4770 if (class_interface_decl)
4771 num_functions = std::distance(class_interface_decl->meth_begin(),
4772 class_interface_decl->meth_end());
4773 }
4774 }
4775 break;
4776
4777 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00004778 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
4779 ->getDecl()
4780 ->getUnderlyingType()
4781 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004782 .GetNumMemberFunctions();
4783
4784 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00004785 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
4786 ->getDeducedType()
4787 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004788 .GetNumMemberFunctions();
4789
4790 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00004791 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
4792 ->getNamedType()
4793 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004794 .GetNumMemberFunctions();
4795
4796 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00004797 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
4798 ->desugar()
4799 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004800 .GetNumMemberFunctions();
4801
4802 default:
4803 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004804 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004805 }
4806 return num_functions;
Greg Claytond8d4a572015-08-11 21:38:15 +00004807}
4808
4809TypeMemberFunctionImpl
Kate Stoneb9c1b512016-09-06 20:57:50 +00004810ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4811 size_t idx) {
4812 std::string name;
4813 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4814 CompilerType clang_type;
4815 CompilerDecl clang_decl;
4816 if (type) {
4817 clang::QualType qual_type(GetCanonicalQualType(type));
4818 switch (qual_type->getTypeClass()) {
4819 case clang::Type::Record:
4820 if (GetCompleteQualType(getASTContext(), qual_type)) {
4821 const clang::RecordType *record_type =
4822 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4823 const clang::RecordDecl *record_decl = record_type->getDecl();
4824 assert(record_decl);
4825 const clang::CXXRecordDecl *cxx_record_decl =
4826 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4827 if (cxx_record_decl) {
4828 auto method_iter = cxx_record_decl->method_begin();
4829 auto method_end = cxx_record_decl->method_end();
4830 if (idx <
4831 static_cast<size_t>(std::distance(method_iter, method_end))) {
4832 std::advance(method_iter, idx);
4833 clang::CXXMethodDecl *cxx_method_decl =
4834 method_iter->getCanonicalDecl();
4835 if (cxx_method_decl) {
4836 name = cxx_method_decl->getDeclName().getAsString();
4837 if (cxx_method_decl->isStatic())
4838 kind = lldb::eMemberFunctionKindStaticMethod;
4839 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4840 kind = lldb::eMemberFunctionKindConstructor;
4841 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4842 kind = lldb::eMemberFunctionKindDestructor;
4843 else
4844 kind = lldb::eMemberFunctionKindInstanceMethod;
4845 clang_type = CompilerType(
4846 this, cxx_method_decl->getType().getAsOpaquePtr());
4847 clang_decl = CompilerDecl(this, cxx_method_decl);
4848 }
4849 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004850 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004851 }
4852 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004853
Sean Callananf9c622a2016-09-30 18:44:43 +00004854 case clang::Type::ObjCObjectPointer: {
4855 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004856 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004857 const clang::ObjCInterfaceType *objc_interface_type =
4858 objc_class_type->getInterfaceType();
4859 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004860 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4861 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004862 clang::ObjCInterfaceDecl *class_interface_decl =
4863 objc_interface_type->getDecl();
4864 if (class_interface_decl) {
4865 auto method_iter = class_interface_decl->meth_begin();
4866 auto method_end = class_interface_decl->meth_end();
4867 if (idx <
4868 static_cast<size_t>(std::distance(method_iter, method_end))) {
4869 std::advance(method_iter, idx);
4870 clang::ObjCMethodDecl *objc_method_decl =
4871 method_iter->getCanonicalDecl();
4872 if (objc_method_decl) {
4873 clang_decl = CompilerDecl(this, objc_method_decl);
4874 name = objc_method_decl->getSelector().getAsString();
4875 if (objc_method_decl->isClassMethod())
4876 kind = lldb::eMemberFunctionKindStaticMethod;
4877 else
4878 kind = lldb::eMemberFunctionKindInstanceMethod;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004879 }
4880 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004881 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004882 }
4883 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004884 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004885
Kate Stoneb9c1b512016-09-06 20:57:50 +00004886 case clang::Type::ObjCObject:
4887 case clang::Type::ObjCInterface:
4888 if (GetCompleteType(type)) {
4889 const clang::ObjCObjectType *objc_class_type =
4890 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4891 if (objc_class_type) {
4892 clang::ObjCInterfaceDecl *class_interface_decl =
4893 objc_class_type->getInterface();
4894 if (class_interface_decl) {
4895 auto method_iter = class_interface_decl->meth_begin();
4896 auto method_end = class_interface_decl->meth_end();
4897 if (idx <
4898 static_cast<size_t>(std::distance(method_iter, method_end))) {
4899 std::advance(method_iter, idx);
4900 clang::ObjCMethodDecl *objc_method_decl =
4901 method_iter->getCanonicalDecl();
4902 if (objc_method_decl) {
4903 clang_decl = CompilerDecl(this, objc_method_decl);
4904 name = objc_method_decl->getSelector().getAsString();
4905 if (objc_method_decl->isClassMethod())
4906 kind = lldb::eMemberFunctionKindStaticMethod;
4907 else
4908 kind = lldb::eMemberFunctionKindInstanceMethod;
4909 }
4910 }
4911 }
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004912 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004913 }
4914 break;
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004915
Kate Stoneb9c1b512016-09-06 20:57:50 +00004916 case clang::Type::Typedef:
4917 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4918 ->getDecl()
4919 ->getUnderlyingType()
4920 .getAsOpaquePtr(),
4921 idx);
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004922
Kate Stoneb9c1b512016-09-06 20:57:50 +00004923 case clang::Type::Auto:
4924 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4925 ->getDeducedType()
4926 .getAsOpaquePtr(),
4927 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004928
Kate Stoneb9c1b512016-09-06 20:57:50 +00004929 case clang::Type::Elaborated:
4930 return GetMemberFunctionAtIndex(
4931 llvm::cast<clang::ElaboratedType>(qual_type)
4932 ->getNamedType()
4933 .getAsOpaquePtr(),
4934 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004935
Kate Stoneb9c1b512016-09-06 20:57:50 +00004936 case clang::Type::Paren:
4937 return GetMemberFunctionAtIndex(
4938 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4939 idx);
4940
4941 default:
4942 break;
Greg Clayton56939cb2015-09-17 22:23:34 +00004943 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004944 }
Greg Clayton56939cb2015-09-17 22:23:34 +00004945
Kate Stoneb9c1b512016-09-06 20:57:50 +00004946 if (kind == eMemberFunctionKindUnknown)
4947 return TypeMemberFunctionImpl();
4948 else
4949 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
Greg Clayton56939cb2015-09-17 22:23:34 +00004950}
4951
Greg Claytona1e5dc82015-08-11 22:53:00 +00004952CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004953ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4954 if (type)
Alex Langfordbddab072019-08-13 19:40:36 +00004955 return CompilerType(
4956 this, GetQualType(type).getNonReferenceType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004957 return CompilerType();
4958}
4959
4960CompilerType ClangASTContext::CreateTypedefType(
4961 const CompilerType &type, const char *typedef_name,
4962 const CompilerDeclContext &compiler_decl_ctx) {
4963 if (type && typedef_name && typedef_name[0]) {
4964 ClangASTContext *ast =
4965 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4966 if (!ast)
4967 return CompilerType();
4968 clang::ASTContext *clang_ast = ast->getASTContext();
4969 clang::QualType qual_type(ClangUtil::GetQualType(type));
4970
4971 clang::DeclContext *decl_ctx =
4972 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4973 if (decl_ctx == nullptr)
4974 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4975
4976 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4977 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4978 &clang_ast->Idents.get(typedef_name),
4979 clang_ast->getTrivialTypeSourceInfo(qual_type));
4980
4981 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4982
Aleksandr Urakov709426b2018-09-10 08:08:43 +00004983 decl_ctx->addDecl(decl);
4984
Kate Stoneb9c1b512016-09-06 20:57:50 +00004985 // Get a uniqued clang::QualType for the typedef decl type
Alex Langfordbddab072019-08-13 19:40:36 +00004986 return CompilerType(ast, clang_ast->getTypedefType(decl).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004987 }
4988 return CompilerType();
4989}
4990
4991CompilerType
4992ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4993 if (type) {
4994 clang::QualType qual_type(GetQualType(type));
Alex Langfordbddab072019-08-13 19:40:36 +00004995 return CompilerType(
4996 this, qual_type.getTypePtr()->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004997 }
4998 return CompilerType();
4999}
5000
5001CompilerType
5002ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
5003 if (type) {
5004 clang::QualType qual_type(GetQualType(type));
5005
5006 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5007 switch (type_class) {
5008 case clang::Type::ObjCObject:
5009 case clang::Type::ObjCInterface:
Alex Langfordbddab072019-08-13 19:40:36 +00005010 return CompilerType(this, getASTContext()
5011 ->getObjCObjectPointerType(qual_type)
5012 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00005013
5014 default:
Alex Langfordbddab072019-08-13 19:40:36 +00005015 return CompilerType(
5016 this, getASTContext()->getPointerType(qual_type).getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00005017 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005018 }
5019 return CompilerType();
5020}
5021
5022CompilerType
5023ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
5024 if (type)
5025 return CompilerType(this, getASTContext()
5026 ->getLValueReferenceType(GetQualType(type))
5027 .getAsOpaquePtr());
5028 else
Greg Claytona1e5dc82015-08-11 22:53:00 +00005029 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005030}
5031
Kate Stoneb9c1b512016-09-06 20:57:50 +00005032CompilerType
5033ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
5034 if (type)
5035 return CompilerType(this, getASTContext()
5036 ->getRValueReferenceType(GetQualType(type))
5037 .getAsOpaquePtr());
5038 else
5039 return CompilerType();
5040}
5041
5042CompilerType
5043ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
5044 if (type) {
5045 clang::QualType result(GetQualType(type));
5046 result.addConst();
5047 return CompilerType(this, result.getAsOpaquePtr());
5048 }
5049 return CompilerType();
5050}
5051
5052CompilerType
5053ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
5054 if (type) {
5055 clang::QualType result(GetQualType(type));
5056 result.addVolatile();
5057 return CompilerType(this, result.getAsOpaquePtr());
5058 }
5059 return CompilerType();
5060}
5061
5062CompilerType
5063ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
5064 if (type) {
5065 clang::QualType result(GetQualType(type));
5066 result.addRestrict();
5067 return CompilerType(this, result.getAsOpaquePtr());
5068 }
5069 return CompilerType();
5070}
5071
5072CompilerType
5073ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
5074 const char *typedef_name,
5075 const CompilerDeclContext &compiler_decl_ctx) {
5076 if (type) {
5077 clang::ASTContext *clang_ast = getASTContext();
5078 clang::QualType qual_type(GetQualType(type));
5079
5080 clang::DeclContext *decl_ctx =
5081 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
5082 if (decl_ctx == nullptr)
5083 decl_ctx = getASTContext()->getTranslationUnitDecl();
5084
5085 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
5086 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
5087 &clang_ast->Idents.get(typedef_name),
5088 clang_ast->getTrivialTypeSourceInfo(qual_type));
5089
5090 clang::TagDecl *tdecl = nullptr;
5091 if (!qual_type.isNull()) {
5092 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
5093 tdecl = rt->getDecl();
5094 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
5095 tdecl = et->getDecl();
5096 }
5097
5098 // Check whether this declaration is an anonymous struct, union, or enum,
Adrian Prantl05097242018-04-30 16:49:04 +00005099 // hidden behind a typedef. If so, we try to check whether we have a
5100 // typedef tag to attach to the original record declaration
Kate Stoneb9c1b512016-09-06 20:57:50 +00005101 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
5102 tdecl->setTypedefNameForAnonDecl(decl);
5103
5104 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
5105
5106 // Get a uniqued clang::QualType for the typedef decl type
5107 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
5108 }
5109 return CompilerType();
5110}
5111
5112CompilerType
5113ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
5114 if (type) {
5115 const clang::TypedefType *typedef_type =
5116 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
5117 if (typedef_type)
Alex Langfordbddab072019-08-13 19:40:36 +00005118 return CompilerType(
5119 this, typedef_type->getDecl()->getUnderlyingType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00005120 }
5121 return CompilerType();
5122}
Greg Claytond8d4a572015-08-11 21:38:15 +00005123
Greg Claytond8d4a572015-08-11 21:38:15 +00005124// Create related types using the current type's AST
Greg Claytond8d4a572015-08-11 21:38:15 +00005125
Kate Stoneb9c1b512016-09-06 20:57:50 +00005126CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
5127 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005128}
Greg Claytond8d4a572015-08-11 21:38:15 +00005129// Exploring the type
Greg Claytond8d4a572015-08-11 21:38:15 +00005130
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005131Optional<uint64_t>
5132ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
5133 ExecutionContextScope *exe_scope) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005134 if (GetCompleteType(type)) {
Greg Claytond8d4a572015-08-11 21:38:15 +00005135 clang::QualType qual_type(GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00005136 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005137 switch (type_class) {
5138 case clang::Type::Record:
5139 if (GetCompleteType(type))
5140 return getASTContext()->getTypeSize(qual_type);
5141 else
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005142 return None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005143 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00005144
Kate Stoneb9c1b512016-09-06 20:57:50 +00005145 case clang::Type::ObjCInterface:
5146 case clang::Type::ObjCObject: {
5147 ExecutionContext exe_ctx(exe_scope);
5148 Process *process = exe_ctx.GetProcessPtr();
5149 if (process) {
Alex Langforde823bbe2019-06-10 20:53:23 +00005150 ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(*process);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005151 if (objc_runtime) {
5152 uint64_t bit_size = 0;
5153 if (objc_runtime->GetTypeBitSize(
Alex Langfordbddab072019-08-13 19:40:36 +00005154 CompilerType(this, qual_type.getAsOpaquePtr()), bit_size))
Kate Stoneb9c1b512016-09-06 20:57:50 +00005155 return bit_size;
5156 }
5157 } else {
5158 static bool g_printed = false;
5159 if (!g_printed) {
5160 StreamString s;
5161 DumpTypeDescription(type, &s);
5162
5163 llvm::outs() << "warning: trying to determine the size of type ";
5164 llvm::outs() << s.GetString() << "\n";
5165 llvm::outs() << "without a valid ExecutionContext. this is not "
5166 "reliable. please file a bug against LLDB.\n";
5167 llvm::outs() << "backtrace:\n";
5168 llvm::sys::PrintStackTrace(llvm::outs());
5169 llvm::outs() << "\n";
5170 g_printed = true;
5171 }
5172 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005173 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005174 LLVM_FALLTHROUGH;
5175 default:
5176 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
5177 if (bit_size == 0) {
5178 if (qual_type->isIncompleteArrayType())
5179 return getASTContext()->getTypeSize(
5180 qual_type->getArrayElementTypeNoTypeQual()
5181 ->getCanonicalTypeUnqualified());
5182 }
5183 if (qual_type->isObjCObjectOrInterfaceType())
5184 return bit_size +
5185 getASTContext()->getTypeSize(
5186 getASTContext()->ObjCBuiltinClassTy);
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005187 // Function types actually have a size of 0, that's not an error.
5188 if (qual_type->isFunctionProtoType())
5189 return bit_size;
5190 if (bit_size)
5191 return bit_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005192 }
5193 }
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005194 return None;
Greg Claytond8d4a572015-08-11 21:38:15 +00005195}
5196
Davide Italiano36f13e42019-08-12 20:03:19 +00005197llvm::Optional<size_t>
Davide Italiano7f9bbe02019-08-12 21:49:54 +00005198ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type,
5199 ExecutionContextScope *exe_scope) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005200 if (GetCompleteType(type))
5201 return getASTContext()->getTypeAlign(GetQualType(type));
Davide Italiano36f13e42019-08-12 20:03:19 +00005202 return {};
Kate Stoneb9c1b512016-09-06 20:57:50 +00005203}
5204
5205lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
5206 uint64_t &count) {
5207 if (!type)
5208 return lldb::eEncodingInvalid;
5209
5210 count = 1;
5211 clang::QualType qual_type(GetCanonicalQualType(type));
5212
5213 switch (qual_type->getTypeClass()) {
5214 case clang::Type::UnaryTransform:
5215 break;
5216
5217 case clang::Type::FunctionNoProto:
5218 case clang::Type::FunctionProto:
5219 break;
5220
5221 case clang::Type::IncompleteArray:
5222 case clang::Type::VariableArray:
5223 break;
5224
5225 case clang::Type::ConstantArray:
5226 break;
5227
Fangrui Song8f284882018-07-13 22:40:40 +00005228 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005229 case clang::Type::ExtVector:
5230 case clang::Type::Vector:
5231 // TODO: Set this to more than one???
5232 break;
5233
5234 case clang::Type::Builtin:
5235 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5236 case clang::BuiltinType::Void:
5237 break;
5238
5239 case clang::BuiltinType::Bool:
5240 case clang::BuiltinType::Char_S:
5241 case clang::BuiltinType::SChar:
5242 case clang::BuiltinType::WChar_S:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005243 case clang::BuiltinType::Short:
5244 case clang::BuiltinType::Int:
5245 case clang::BuiltinType::Long:
5246 case clang::BuiltinType::LongLong:
5247 case clang::BuiltinType::Int128:
5248 return lldb::eEncodingSint;
5249
5250 case clang::BuiltinType::Char_U:
5251 case clang::BuiltinType::UChar:
5252 case clang::BuiltinType::WChar_U:
Richard Smith51d12d82018-05-02 02:43:22 +00005253 case clang::BuiltinType::Char8:
5254 case clang::BuiltinType::Char16:
5255 case clang::BuiltinType::Char32:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005256 case clang::BuiltinType::UShort:
5257 case clang::BuiltinType::UInt:
5258 case clang::BuiltinType::ULong:
5259 case clang::BuiltinType::ULongLong:
5260 case clang::BuiltinType::UInt128:
5261 return lldb::eEncodingUint;
5262
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005263 // Fixed point types. Note that they are currently ignored.
5264 case clang::BuiltinType::ShortAccum:
5265 case clang::BuiltinType::Accum:
5266 case clang::BuiltinType::LongAccum:
5267 case clang::BuiltinType::UShortAccum:
5268 case clang::BuiltinType::UAccum:
5269 case clang::BuiltinType::ULongAccum:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005270 case clang::BuiltinType::ShortFract:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005271 case clang::BuiltinType::Fract:
5272 case clang::BuiltinType::LongFract:
5273 case clang::BuiltinType::UShortFract:
5274 case clang::BuiltinType::UFract:
5275 case clang::BuiltinType::ULongFract:
5276 case clang::BuiltinType::SatShortAccum:
5277 case clang::BuiltinType::SatAccum:
5278 case clang::BuiltinType::SatLongAccum:
5279 case clang::BuiltinType::SatUShortAccum:
5280 case clang::BuiltinType::SatUAccum:
5281 case clang::BuiltinType::SatULongAccum:
5282 case clang::BuiltinType::SatShortFract:
5283 case clang::BuiltinType::SatFract:
5284 case clang::BuiltinType::SatLongFract:
5285 case clang::BuiltinType::SatUShortFract:
5286 case clang::BuiltinType::SatUFract:
5287 case clang::BuiltinType::SatULongFract:
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005288 break;
5289
Kate Stoneb9c1b512016-09-06 20:57:50 +00005290 case clang::BuiltinType::Half:
5291 case clang::BuiltinType::Float:
Ted Woodward4355c7c2017-09-20 19:16:53 +00005292 case clang::BuiltinType::Float16:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005293 case clang::BuiltinType::Float128:
5294 case clang::BuiltinType::Double:
5295 case clang::BuiltinType::LongDouble:
5296 return lldb::eEncodingIEEE754;
5297
5298 case clang::BuiltinType::ObjCClass:
5299 case clang::BuiltinType::ObjCId:
5300 case clang::BuiltinType::ObjCSel:
5301 return lldb::eEncodingUint;
5302
5303 case clang::BuiltinType::NullPtr:
5304 return lldb::eEncodingUint;
5305
5306 case clang::BuiltinType::Kind::ARCUnbridgedCast:
5307 case clang::BuiltinType::Kind::BoundMember:
5308 case clang::BuiltinType::Kind::BuiltinFn:
5309 case clang::BuiltinType::Kind::Dependent:
5310 case clang::BuiltinType::Kind::OCLClkEvent:
5311 case clang::BuiltinType::Kind::OCLEvent:
5312 case clang::BuiltinType::Kind::OCLImage1dRO:
5313 case clang::BuiltinType::Kind::OCLImage1dWO:
5314 case clang::BuiltinType::Kind::OCLImage1dRW:
5315 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
5316 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
5317 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
5318 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
5319 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
5320 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
5321 case clang::BuiltinType::Kind::OCLImage2dRO:
5322 case clang::BuiltinType::Kind::OCLImage2dWO:
5323 case clang::BuiltinType::Kind::OCLImage2dRW:
5324 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
5325 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
5326 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
5327 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
5328 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
5329 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
5330 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5331 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5332 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5333 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5334 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5335 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5336 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5337 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5338 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5339 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5340 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5341 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5342 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5343 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5344 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5345 case clang::BuiltinType::Kind::OCLImage3dRO:
5346 case clang::BuiltinType::Kind::OCLImage3dWO:
5347 case clang::BuiltinType::Kind::OCLImage3dRW:
5348 case clang::BuiltinType::Kind::OCLQueue:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005349 case clang::BuiltinType::Kind::OCLReserveID:
5350 case clang::BuiltinType::Kind::OCLSampler:
5351 case clang::BuiltinType::Kind::OMPArraySection:
5352 case clang::BuiltinType::Kind::Overload:
5353 case clang::BuiltinType::Kind::PseudoObject:
5354 case clang::BuiltinType::Kind::UnknownAny:
5355 break;
Jorge Gorbe Moyaa6e6c182018-11-08 22:04:58 +00005356
5357 case clang::BuiltinType::OCLIntelSubgroupAVCMcePayload:
5358 case clang::BuiltinType::OCLIntelSubgroupAVCImePayload:
5359 case clang::BuiltinType::OCLIntelSubgroupAVCRefPayload:
5360 case clang::BuiltinType::OCLIntelSubgroupAVCSicPayload:
5361 case clang::BuiltinType::OCLIntelSubgroupAVCMceResult:
5362 case clang::BuiltinType::OCLIntelSubgroupAVCImeResult:
5363 case clang::BuiltinType::OCLIntelSubgroupAVCRefResult:
5364 case clang::BuiltinType::OCLIntelSubgroupAVCSicResult:
5365 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultSingleRefStreamout:
5366 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultDualRefStreamout:
5367 case clang::BuiltinType::OCLIntelSubgroupAVCImeSingleRefStreamin:
5368 case clang::BuiltinType::OCLIntelSubgroupAVCImeDualRefStreamin:
5369 break;
Raphael Isemann339b5d12019-08-09 09:58:47 +00005370
5371 case clang::BuiltinType::SveBool:
5372 case clang::BuiltinType::SveInt8:
5373 case clang::BuiltinType::SveInt16:
5374 case clang::BuiltinType::SveInt32:
5375 case clang::BuiltinType::SveInt64:
5376 case clang::BuiltinType::SveUint8:
5377 case clang::BuiltinType::SveUint16:
5378 case clang::BuiltinType::SveUint32:
5379 case clang::BuiltinType::SveUint64:
5380 case clang::BuiltinType::SveFloat16:
5381 case clang::BuiltinType::SveFloat32:
5382 case clang::BuiltinType::SveFloat64:
5383 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005384 }
5385 break;
Adrian Prantl05097242018-04-30 16:49:04 +00005386 // All pointer types are represented as unsigned integer encodings. We may
5387 // nee to add a eEncodingPointer if we ever need to know the difference
Kate Stoneb9c1b512016-09-06 20:57:50 +00005388 case clang::Type::ObjCObjectPointer:
5389 case clang::Type::BlockPointer:
5390 case clang::Type::Pointer:
5391 case clang::Type::LValueReference:
5392 case clang::Type::RValueReference:
5393 case clang::Type::MemberPointer:
5394 return lldb::eEncodingUint;
5395 case clang::Type::Complex: {
5396 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5397 if (qual_type->isComplexType())
5398 encoding = lldb::eEncodingIEEE754;
5399 else {
5400 const clang::ComplexType *complex_type =
5401 qual_type->getAsComplexIntegerType();
5402 if (complex_type)
Alex Langfordbddab072019-08-13 19:40:36 +00005403 encoding =
5404 CompilerType(this, complex_type->getElementType().getAsOpaquePtr())
5405 .GetEncoding(count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005406 else
5407 encoding = lldb::eEncodingSint;
5408 }
5409 count = 2;
5410 return encoding;
5411 }
5412
5413 case clang::Type::ObjCInterface:
5414 break;
5415 case clang::Type::Record:
5416 break;
5417 case clang::Type::Enum:
5418 return lldb::eEncodingSint;
5419 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00005420 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
5421 ->getDecl()
5422 ->getUnderlyingType()
5423 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005424 .GetEncoding(count);
5425
5426 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00005427 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
5428 ->getDeducedType()
5429 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005430 .GetEncoding(count);
5431
5432 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00005433 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
5434 ->getNamedType()
5435 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005436 .GetEncoding(count);
5437
5438 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00005439 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
5440 ->desugar()
5441 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005442 .GetEncoding(count);
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005443 case clang::Type::TypeOfExpr:
Alex Langfordbddab072019-08-13 19:40:36 +00005444 return CompilerType(this, llvm::cast<clang::TypeOfExprType>(qual_type)
5445 ->getUnderlyingExpr()
5446 ->getType()
5447 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005448 .GetEncoding(count);
5449 case clang::Type::TypeOf:
Alex Langfordbddab072019-08-13 19:40:36 +00005450 return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type)
5451 ->getUnderlyingType()
5452 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005453 .GetEncoding(count);
5454 case clang::Type::Decltype:
Alex Langfordbddab072019-08-13 19:40:36 +00005455 return CompilerType(this, llvm::cast<clang::DecltypeType>(qual_type)
5456 ->getUnderlyingType()
5457 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005458 .GetEncoding(count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005459 case clang::Type::DependentSizedArray:
5460 case clang::Type::DependentSizedExtVector:
5461 case clang::Type::UnresolvedUsing:
5462 case clang::Type::Attributed:
5463 case clang::Type::TemplateTypeParm:
5464 case clang::Type::SubstTemplateTypeParm:
5465 case clang::Type::SubstTemplateTypeParmPack:
5466 case clang::Type::InjectedClassName:
5467 case clang::Type::DependentName:
5468 case clang::Type::DependentTemplateSpecialization:
5469 case clang::Type::PackExpansion:
5470 case clang::Type::ObjCObject:
5471
Kate Stoneb9c1b512016-09-06 20:57:50 +00005472 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005473 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005474 case clang::Type::Atomic:
5475 case clang::Type::Adjusted:
5476 case clang::Type::Pipe:
5477 break;
5478
5479 // pointer type decayed from an array or function type.
5480 case clang::Type::Decayed:
5481 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005482 case clang::Type::ObjCTypeParam:
5483 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005484
5485 case clang::Type::DependentAddressSpace:
5486 break;
Krasimir Georgiev435e76a2019-05-07 13:59:30 +00005487 case clang::Type::MacroQualified:
5488 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005489 }
5490 count = 0;
5491 return lldb::eEncodingInvalid;
5492}
5493
5494lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5495 if (!type)
5496 return lldb::eFormatDefault;
5497
5498 clang::QualType qual_type(GetCanonicalQualType(type));
5499
5500 switch (qual_type->getTypeClass()) {
5501 case clang::Type::UnaryTransform:
5502 break;
5503
5504 case clang::Type::FunctionNoProto:
5505 case clang::Type::FunctionProto:
5506 break;
5507
5508 case clang::Type::IncompleteArray:
5509 case clang::Type::VariableArray:
5510 break;
5511
5512 case clang::Type::ConstantArray:
5513 return lldb::eFormatVoid; // no value
5514
Fangrui Song8f284882018-07-13 22:40:40 +00005515 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005516 case clang::Type::ExtVector:
5517 case clang::Type::Vector:
5518 break;
5519
5520 case clang::Type::Builtin:
5521 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5522 // default: assert(0 && "Unknown builtin type!");
5523 case clang::BuiltinType::UnknownAny:
5524 case clang::BuiltinType::Void:
5525 case clang::BuiltinType::BoundMember:
5526 break;
5527
5528 case clang::BuiltinType::Bool:
5529 return lldb::eFormatBoolean;
5530 case clang::BuiltinType::Char_S:
5531 case clang::BuiltinType::SChar:
5532 case clang::BuiltinType::WChar_S:
5533 case clang::BuiltinType::Char_U:
5534 case clang::BuiltinType::UChar:
5535 case clang::BuiltinType::WChar_U:
5536 return lldb::eFormatChar;
5537 case clang::BuiltinType::Char16:
5538 return lldb::eFormatUnicode16;
5539 case clang::BuiltinType::Char32:
5540 return lldb::eFormatUnicode32;
5541 case clang::BuiltinType::UShort:
5542 return lldb::eFormatUnsigned;
5543 case clang::BuiltinType::Short:
5544 return lldb::eFormatDecimal;
5545 case clang::BuiltinType::UInt:
5546 return lldb::eFormatUnsigned;
5547 case clang::BuiltinType::Int:
5548 return lldb::eFormatDecimal;
5549 case clang::BuiltinType::ULong:
5550 return lldb::eFormatUnsigned;
5551 case clang::BuiltinType::Long:
5552 return lldb::eFormatDecimal;
5553 case clang::BuiltinType::ULongLong:
5554 return lldb::eFormatUnsigned;
5555 case clang::BuiltinType::LongLong:
5556 return lldb::eFormatDecimal;
5557 case clang::BuiltinType::UInt128:
5558 return lldb::eFormatUnsigned;
5559 case clang::BuiltinType::Int128:
5560 return lldb::eFormatDecimal;
5561 case clang::BuiltinType::Half:
5562 case clang::BuiltinType::Float:
5563 case clang::BuiltinType::Double:
5564 case clang::BuiltinType::LongDouble:
5565 return lldb::eFormatFloat;
5566 default:
5567 return lldb::eFormatHex;
5568 }
5569 break;
5570 case clang::Type::ObjCObjectPointer:
5571 return lldb::eFormatHex;
5572 case clang::Type::BlockPointer:
5573 return lldb::eFormatHex;
5574 case clang::Type::Pointer:
5575 return lldb::eFormatHex;
5576 case clang::Type::LValueReference:
5577 case clang::Type::RValueReference:
5578 return lldb::eFormatHex;
5579 case clang::Type::MemberPointer:
5580 break;
5581 case clang::Type::Complex: {
5582 if (qual_type->isComplexType())
5583 return lldb::eFormatComplex;
5584 else
5585 return lldb::eFormatComplexInteger;
5586 }
5587 case clang::Type::ObjCInterface:
5588 break;
5589 case clang::Type::Record:
5590 break;
5591 case clang::Type::Enum:
5592 return lldb::eFormatEnum;
5593 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00005594 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
5595 ->getDecl()
5596 ->getUnderlyingType()
5597 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005598 .GetFormat();
5599 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00005600 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
5601 ->desugar()
5602 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005603 .GetFormat();
5604 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00005605 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
5606 ->desugar()
5607 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005608 .GetFormat();
5609 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00005610 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
5611 ->getNamedType()
5612 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005613 .GetFormat();
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005614 case clang::Type::TypeOfExpr:
Alex Langfordbddab072019-08-13 19:40:36 +00005615 return CompilerType(this, llvm::cast<clang::TypeOfExprType>(qual_type)
5616 ->getUnderlyingExpr()
5617 ->getType()
5618 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005619 .GetFormat();
5620 case clang::Type::TypeOf:
Alex Langfordbddab072019-08-13 19:40:36 +00005621 return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type)
5622 ->getUnderlyingType()
5623 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005624 .GetFormat();
5625 case clang::Type::Decltype:
Alex Langfordbddab072019-08-13 19:40:36 +00005626 return CompilerType(this, llvm::cast<clang::DecltypeType>(qual_type)
5627 ->getUnderlyingType()
5628 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005629 .GetFormat();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005630 case clang::Type::DependentSizedArray:
5631 case clang::Type::DependentSizedExtVector:
5632 case clang::Type::UnresolvedUsing:
5633 case clang::Type::Attributed:
5634 case clang::Type::TemplateTypeParm:
5635 case clang::Type::SubstTemplateTypeParm:
5636 case clang::Type::SubstTemplateTypeParmPack:
5637 case clang::Type::InjectedClassName:
5638 case clang::Type::DependentName:
5639 case clang::Type::DependentTemplateSpecialization:
5640 case clang::Type::PackExpansion:
5641 case clang::Type::ObjCObject:
5642
Kate Stoneb9c1b512016-09-06 20:57:50 +00005643 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005644 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005645 case clang::Type::Atomic:
5646 case clang::Type::Adjusted:
5647 case clang::Type::Pipe:
5648 break;
5649
5650 // pointer type decayed from an array or function type.
5651 case clang::Type::Decayed:
5652 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005653 case clang::Type::ObjCTypeParam:
5654 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005655
5656 case clang::Type::DependentAddressSpace:
5657 break;
Krasimir Georgiev435e76a2019-05-07 13:59:30 +00005658 case clang::Type::MacroQualified:
5659 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005660 }
5661 // We don't know hot to display this type...
5662 return lldb::eFormatBytes;
5663}
5664
5665static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5666 bool check_superclass) {
5667 while (class_interface_decl) {
5668 if (class_interface_decl->ivar_size() > 0)
5669 return true;
5670
5671 if (check_superclass)
5672 class_interface_decl = class_interface_decl->getSuperClass();
5673 else
5674 break;
5675 }
5676 return false;
5677}
5678
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00005679static Optional<SymbolFile::ArrayInfo>
Adrian Prantleca07c52018-11-05 20:49:07 +00005680GetDynamicArrayInfo(ClangASTContext &ast, SymbolFile *sym_file,
5681 clang::QualType qual_type,
5682 const ExecutionContext *exe_ctx) {
5683 if (qual_type->isIncompleteArrayType())
5684 if (auto *metadata = ast.GetMetadata(qual_type.getAsOpaquePtr()))
Pavel Labathffec31e2018-12-28 13:34:44 +00005685 return sym_file->GetDynamicArrayInfoForUID(metadata->GetUserID(),
5686 exe_ctx);
Adrian Prantleca07c52018-11-05 20:49:07 +00005687 return llvm::None;
5688}
5689
Kate Stoneb9c1b512016-09-06 20:57:50 +00005690uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
Adrian Prantleca07c52018-11-05 20:49:07 +00005691 bool omit_empty_base_classes,
5692 const ExecutionContext *exe_ctx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005693 if (!type)
5694 return 0;
5695
5696 uint32_t num_children = 0;
5697 clang::QualType qual_type(GetQualType(type));
5698 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5699 switch (type_class) {
5700 case clang::Type::Builtin:
5701 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5702 case clang::BuiltinType::ObjCId: // child is Class
5703 case clang::BuiltinType::ObjCClass: // child is Class
5704 num_children = 1;
5705 break;
5706
5707 default:
5708 break;
5709 }
5710 break;
5711
5712 case clang::Type::Complex:
5713 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005714 case clang::Type::Record:
5715 if (GetCompleteQualType(getASTContext(), qual_type)) {
5716 const clang::RecordType *record_type =
5717 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5718 const clang::RecordDecl *record_decl = record_type->getDecl();
5719 assert(record_decl);
5720 const clang::CXXRecordDecl *cxx_record_decl =
5721 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5722 if (cxx_record_decl) {
5723 if (omit_empty_base_classes) {
Adrian Prantl05097242018-04-30 16:49:04 +00005724 // Check each base classes to see if it or any of its base classes
5725 // contain any fields. This can help limit the noise in variable
5726 // views by not having to show base classes that contain no members.
Kate Stoneb9c1b512016-09-06 20:57:50 +00005727 clang::CXXRecordDecl::base_class_const_iterator base_class,
5728 base_class_end;
5729 for (base_class = cxx_record_decl->bases_begin(),
5730 base_class_end = cxx_record_decl->bases_end();
5731 base_class != base_class_end; ++base_class) {
5732 const clang::CXXRecordDecl *base_class_decl =
5733 llvm::cast<clang::CXXRecordDecl>(
5734 base_class->getType()
5735 ->getAs<clang::RecordType>()
5736 ->getDecl());
5737
5738 // Skip empty base classes
Jonas Devliegherea6682a42018-12-15 00:15:33 +00005739 if (!ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00005740 continue;
5741
5742 num_children++;
5743 }
5744 } else {
5745 // Include all base classes
5746 num_children += cxx_record_decl->getNumBases();
5747 }
5748 }
5749 clang::RecordDecl::field_iterator field, field_end;
5750 for (field = record_decl->field_begin(),
5751 field_end = record_decl->field_end();
5752 field != field_end; ++field)
5753 ++num_children;
5754 }
5755 break;
5756
5757 case clang::Type::ObjCObject:
5758 case clang::Type::ObjCInterface:
5759 if (GetCompleteQualType(getASTContext(), qual_type)) {
5760 const clang::ObjCObjectType *objc_class_type =
5761 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5762 assert(objc_class_type);
5763 if (objc_class_type) {
5764 clang::ObjCInterfaceDecl *class_interface_decl =
5765 objc_class_type->getInterface();
5766
5767 if (class_interface_decl) {
5768
5769 clang::ObjCInterfaceDecl *superclass_interface_decl =
5770 class_interface_decl->getSuperClass();
5771 if (superclass_interface_decl) {
5772 if (omit_empty_base_classes) {
5773 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5774 ++num_children;
5775 } else
5776 ++num_children;
5777 }
5778
5779 num_children += class_interface_decl->ivar_size();
5780 }
5781 }
5782 }
5783 break;
5784
5785 case clang::Type::ObjCObjectPointer: {
5786 const clang::ObjCObjectPointerType *pointer_type =
5787 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5788 clang::QualType pointee_type = pointer_type->getPointeeType();
5789 uint32_t num_pointee_children =
Alex Langfordbddab072019-08-13 19:40:36 +00005790 CompilerType(this, pointee_type.getAsOpaquePtr())
Adrian Prantleca07c52018-11-05 20:49:07 +00005791 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005792 // If this type points to a simple type, then it has 1 child
5793 if (num_pointee_children == 0)
5794 num_children = 1;
5795 else
5796 num_children = num_pointee_children;
5797 } break;
5798
5799 case clang::Type::Vector:
5800 case clang::Type::ExtVector:
5801 num_children =
5802 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5803 break;
5804
5805 case clang::Type::ConstantArray:
5806 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5807 ->getSize()
5808 .getLimitedValue();
5809 break;
Adrian Prantleca07c52018-11-05 20:49:07 +00005810 case clang::Type::IncompleteArray:
5811 if (auto array_info =
5812 GetDynamicArrayInfo(*this, GetSymbolFile(), qual_type, exe_ctx))
5813 // Only 1-dimensional arrays are supported.
5814 num_children = array_info->element_orders.size()
5815 ? array_info->element_orders.back()
5816 : 0;
5817 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005818
5819 case clang::Type::Pointer: {
5820 const clang::PointerType *pointer_type =
5821 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5822 clang::QualType pointee_type(pointer_type->getPointeeType());
5823 uint32_t num_pointee_children =
Alex Langfordbddab072019-08-13 19:40:36 +00005824 CompilerType(this, pointee_type.getAsOpaquePtr())
Adrian Prantleca07c52018-11-05 20:49:07 +00005825 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005826 if (num_pointee_children == 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00005827 // We have a pointer to a pointee type that claims it has no children. We
5828 // will want to look at
Kate Stoneb9c1b512016-09-06 20:57:50 +00005829 num_children = GetNumPointeeChildren(pointee_type);
5830 } else
5831 num_children = num_pointee_children;
5832 } break;
5833
5834 case clang::Type::LValueReference:
5835 case clang::Type::RValueReference: {
5836 const clang::ReferenceType *reference_type =
5837 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5838 clang::QualType pointee_type = reference_type->getPointeeType();
5839 uint32_t num_pointee_children =
Alex Langfordbddab072019-08-13 19:40:36 +00005840 CompilerType(this, pointee_type.getAsOpaquePtr())
Adrian Prantleca07c52018-11-05 20:49:07 +00005841 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005842 // If this type points to a simple type, then it has 1 child
5843 if (num_pointee_children == 0)
5844 num_children = 1;
5845 else
5846 num_children = num_pointee_children;
5847 } break;
5848
5849 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00005850 num_children = CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
Kate Stoneb9c1b512016-09-06 20:57:50 +00005851 ->getDecl()
Alex Langfordbddab072019-08-13 19:40:36 +00005852 ->getUnderlyingType()
5853 .getAsOpaquePtr())
5854 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005855 break;
5856
5857 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00005858 num_children = CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
5859 ->getDeducedType()
5860 .getAsOpaquePtr())
5861 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005862 break;
5863
5864 case clang::Type::Elaborated:
5865 num_children =
Alex Langfordbddab072019-08-13 19:40:36 +00005866 CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
5867 ->getNamedType()
5868 .getAsOpaquePtr())
Adrian Prantleca07c52018-11-05 20:49:07 +00005869 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005870 break;
5871
5872 case clang::Type::Paren:
5873 num_children =
Alex Langfordbddab072019-08-13 19:40:36 +00005874 CompilerType(
5875 this,
5876 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr())
Adrian Prantleca07c52018-11-05 20:49:07 +00005877 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005878 break;
5879 default:
5880 break;
5881 }
5882 return num_children;
5883}
5884
Adrian Prantl0e4c4822019-03-06 21:22:25 +00005885CompilerType ClangASTContext::GetBuiltinTypeByName(ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005886 return GetBasicType(GetBasicTypeEnumeration(name));
Greg Clayton56939cb2015-09-17 22:23:34 +00005887}
5888
Greg Claytond8d4a572015-08-11 21:38:15 +00005889lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00005890ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5891 if (type) {
5892 clang::QualType qual_type(GetQualType(type));
5893 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5894 if (type_class == clang::Type::Builtin) {
5895 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5896 case clang::BuiltinType::Void:
5897 return eBasicTypeVoid;
5898 case clang::BuiltinType::Bool:
5899 return eBasicTypeBool;
5900 case clang::BuiltinType::Char_S:
5901 return eBasicTypeSignedChar;
5902 case clang::BuiltinType::Char_U:
5903 return eBasicTypeUnsignedChar;
5904 case clang::BuiltinType::Char16:
5905 return eBasicTypeChar16;
5906 case clang::BuiltinType::Char32:
5907 return eBasicTypeChar32;
5908 case clang::BuiltinType::UChar:
5909 return eBasicTypeUnsignedChar;
5910 case clang::BuiltinType::SChar:
5911 return eBasicTypeSignedChar;
5912 case clang::BuiltinType::WChar_S:
5913 return eBasicTypeSignedWChar;
5914 case clang::BuiltinType::WChar_U:
5915 return eBasicTypeUnsignedWChar;
5916 case clang::BuiltinType::Short:
5917 return eBasicTypeShort;
5918 case clang::BuiltinType::UShort:
5919 return eBasicTypeUnsignedShort;
5920 case clang::BuiltinType::Int:
5921 return eBasicTypeInt;
5922 case clang::BuiltinType::UInt:
5923 return eBasicTypeUnsignedInt;
5924 case clang::BuiltinType::Long:
5925 return eBasicTypeLong;
5926 case clang::BuiltinType::ULong:
5927 return eBasicTypeUnsignedLong;
5928 case clang::BuiltinType::LongLong:
5929 return eBasicTypeLongLong;
5930 case clang::BuiltinType::ULongLong:
5931 return eBasicTypeUnsignedLongLong;
5932 case clang::BuiltinType::Int128:
5933 return eBasicTypeInt128;
5934 case clang::BuiltinType::UInt128:
5935 return eBasicTypeUnsignedInt128;
5936
5937 case clang::BuiltinType::Half:
5938 return eBasicTypeHalf;
5939 case clang::BuiltinType::Float:
5940 return eBasicTypeFloat;
5941 case clang::BuiltinType::Double:
5942 return eBasicTypeDouble;
5943 case clang::BuiltinType::LongDouble:
5944 return eBasicTypeLongDouble;
5945
5946 case clang::BuiltinType::NullPtr:
5947 return eBasicTypeNullPtr;
5948 case clang::BuiltinType::ObjCId:
5949 return eBasicTypeObjCID;
5950 case clang::BuiltinType::ObjCClass:
5951 return eBasicTypeObjCClass;
5952 case clang::BuiltinType::ObjCSel:
5953 return eBasicTypeObjCSel;
5954 default:
5955 return eBasicTypeOther;
5956 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005957 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005958 }
5959 return eBasicTypeInvalid;
Greg Claytond8d4a572015-08-11 21:38:15 +00005960}
5961
Kate Stoneb9c1b512016-09-06 20:57:50 +00005962void ClangASTContext::ForEachEnumerator(
5963 lldb::opaque_compiler_type_t type,
5964 std::function<bool(const CompilerType &integer_type,
Adrian Prantl0e4c4822019-03-06 21:22:25 +00005965 ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00005966 const llvm::APSInt &value)> const &callback) {
5967 const clang::EnumType *enum_type =
5968 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5969 if (enum_type) {
5970 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5971 if (enum_decl) {
5972 CompilerType integer_type(this,
5973 enum_decl->getIntegerType().getAsOpaquePtr());
Greg Clayton99558cc42015-08-24 23:46:31 +00005974
Kate Stoneb9c1b512016-09-06 20:57:50 +00005975 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5976 for (enum_pos = enum_decl->enumerator_begin(),
5977 enum_end_pos = enum_decl->enumerator_end();
5978 enum_pos != enum_end_pos; ++enum_pos) {
5979 ConstString name(enum_pos->getNameAsString().c_str());
5980 if (!callback(integer_type, name, enum_pos->getInitVal()))
5981 break;
5982 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005983 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005984 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005985}
5986
Greg Claytond8d4a572015-08-11 21:38:15 +00005987#pragma mark Aggregate Types
5988
Kate Stoneb9c1b512016-09-06 20:57:50 +00005989uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5990 if (!type)
5991 return 0;
Enrico Granata36f51e42015-12-18 22:41:25 +00005992
Kate Stoneb9c1b512016-09-06 20:57:50 +00005993 uint32_t count = 0;
5994 clang::QualType qual_type(GetCanonicalQualType(type));
5995 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5996 switch (type_class) {
5997 case clang::Type::Record:
5998 if (GetCompleteType(type)) {
5999 const clang::RecordType *record_type =
6000 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
6001 if (record_type) {
6002 clang::RecordDecl *record_decl = record_type->getDecl();
6003 if (record_decl) {
6004 uint32_t field_idx = 0;
6005 clang::RecordDecl::field_iterator field, field_end;
6006 for (field = record_decl->field_begin(),
6007 field_end = record_decl->field_end();
6008 field != field_end; ++field)
6009 ++field_idx;
6010 count = field_idx;
6011 }
6012 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006013 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006014 break;
6015
6016 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00006017 count = CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
6018 ->getDecl()
6019 ->getUnderlyingType()
6020 .getAsOpaquePtr())
6021 .GetNumFields();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006022 break;
6023
6024 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00006025 count = CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
6026 ->getDeducedType()
6027 .getAsOpaquePtr())
6028 .GetNumFields();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006029 break;
6030
6031 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00006032 count = CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
6033 ->getNamedType()
6034 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00006035 .GetNumFields();
6036 break;
6037
6038 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00006039 count =
6040 CompilerType(
6041 this,
6042 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr())
6043 .GetNumFields();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006044 break;
6045
Sean Callananf9c622a2016-09-30 18:44:43 +00006046 case clang::Type::ObjCObjectPointer: {
6047 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00006048 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00006049 const clang::ObjCInterfaceType *objc_interface_type =
6050 objc_class_type->getInterfaceType();
6051 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00006052 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
6053 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00006054 clang::ObjCInterfaceDecl *class_interface_decl =
6055 objc_interface_type->getDecl();
6056 if (class_interface_decl) {
6057 count = class_interface_decl->ivar_size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006058 }
6059 }
6060 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00006061 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006062
6063 case clang::Type::ObjCObject:
6064 case clang::Type::ObjCInterface:
6065 if (GetCompleteType(type)) {
6066 const clang::ObjCObjectType *objc_class_type =
6067 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6068 if (objc_class_type) {
6069 clang::ObjCInterfaceDecl *class_interface_decl =
6070 objc_class_type->getInterface();
6071
6072 if (class_interface_decl)
6073 count = class_interface_decl->ivar_size();
6074 }
6075 }
6076 break;
6077
6078 default:
6079 break;
6080 }
6081 return count;
Greg Claytond8d4a572015-08-11 21:38:15 +00006082}
6083
Bruce Mitchener48ea9002015-09-23 00:18:24 +00006084static lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006085GetObjCFieldAtIndex(clang::ASTContext *ast,
6086 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
6087 std::string &name, uint64_t *bit_offset_ptr,
6088 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
6089 if (class_interface_decl) {
6090 if (idx < (class_interface_decl->ivar_size())) {
6091 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6092 ivar_end = class_interface_decl->ivar_end();
6093 uint32_t ivar_idx = 0;
6094
6095 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
6096 ++ivar_pos, ++ivar_idx) {
6097 if (ivar_idx == idx) {
6098 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6099
6100 clang::QualType ivar_qual_type(ivar_decl->getType());
6101
6102 name.assign(ivar_decl->getNameAsString());
6103
6104 if (bit_offset_ptr) {
6105 const clang::ASTRecordLayout &interface_layout =
6106 ast->getASTObjCInterfaceLayout(class_interface_decl);
6107 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
6108 }
6109
6110 const bool is_bitfield = ivar_pos->isBitField();
6111
6112 if (bitfield_bit_size_ptr) {
6113 *bitfield_bit_size_ptr = 0;
6114
6115 if (is_bitfield && ast) {
6116 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
Hans Wennborg30ce9622018-11-28 14:30:18 +00006117 clang::Expr::EvalResult result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006118 if (bitfield_bit_size_expr &&
Hans Wennborg30ce9622018-11-28 14:30:18 +00006119 bitfield_bit_size_expr->EvaluateAsInt(result, *ast)) {
6120 llvm::APSInt bitfield_apsint = result.Val.getInt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006121 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
6122 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006123 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006124 }
6125 if (is_bitfield_ptr)
6126 *is_bitfield_ptr = is_bitfield;
6127
6128 return ivar_qual_type.getAsOpaquePtr();
Greg Claytond8d4a572015-08-11 21:38:15 +00006129 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006130 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006131 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006132 }
6133 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00006134}
6135
Kate Stoneb9c1b512016-09-06 20:57:50 +00006136CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
6137 size_t idx, std::string &name,
6138 uint64_t *bit_offset_ptr,
6139 uint32_t *bitfield_bit_size_ptr,
6140 bool *is_bitfield_ptr) {
6141 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006142 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006143
6144 clang::QualType qual_type(GetCanonicalQualType(type));
6145 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6146 switch (type_class) {
6147 case clang::Type::Record:
6148 if (GetCompleteType(type)) {
6149 const clang::RecordType *record_type =
6150 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6151 const clang::RecordDecl *record_decl = record_type->getDecl();
6152 uint32_t field_idx = 0;
6153 clang::RecordDecl::field_iterator field, field_end;
6154 for (field = record_decl->field_begin(),
6155 field_end = record_decl->field_end();
6156 field != field_end; ++field, ++field_idx) {
6157 if (idx == field_idx) {
6158 // Print the member type if requested
6159 // Print the member name and equal sign
6160 name.assign(field->getNameAsString());
6161
6162 // Figure out the type byte size (field_type_info.first) and
6163 // alignment (field_type_info.second) from the AST context.
6164 if (bit_offset_ptr) {
6165 const clang::ASTRecordLayout &record_layout =
6166 getASTContext()->getASTRecordLayout(record_decl);
6167 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
6168 }
6169
6170 const bool is_bitfield = field->isBitField();
6171
6172 if (bitfield_bit_size_ptr) {
6173 *bitfield_bit_size_ptr = 0;
6174
6175 if (is_bitfield) {
6176 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
Hans Wennborg30ce9622018-11-28 14:30:18 +00006177 clang::Expr::EvalResult result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006178 if (bitfield_bit_size_expr &&
Hans Wennborg30ce9622018-11-28 14:30:18 +00006179 bitfield_bit_size_expr->EvaluateAsInt(result,
Kate Stoneb9c1b512016-09-06 20:57:50 +00006180 *getASTContext())) {
Hans Wennborg30ce9622018-11-28 14:30:18 +00006181 llvm::APSInt bitfield_apsint = result.Val.getInt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006182 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
6183 }
6184 }
6185 }
6186 if (is_bitfield_ptr)
6187 *is_bitfield_ptr = is_bitfield;
6188
Alex Langfordbddab072019-08-13 19:40:36 +00006189 return CompilerType(this, field->getType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006190 }
6191 }
6192 }
6193 break;
6194
Sean Callananf9c622a2016-09-30 18:44:43 +00006195 case clang::Type::ObjCObjectPointer: {
6196 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00006197 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00006198 const clang::ObjCInterfaceType *objc_interface_type =
6199 objc_class_type->getInterfaceType();
6200 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00006201 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
6202 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00006203 clang::ObjCInterfaceDecl *class_interface_decl =
6204 objc_interface_type->getDecl();
6205 if (class_interface_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006206 return CompilerType(
6207 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
6208 idx, name, bit_offset_ptr,
6209 bitfield_bit_size_ptr, is_bitfield_ptr));
6210 }
6211 }
6212 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00006213 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006214
6215 case clang::Type::ObjCObject:
6216 case clang::Type::ObjCInterface:
6217 if (GetCompleteType(type)) {
6218 const clang::ObjCObjectType *objc_class_type =
6219 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6220 assert(objc_class_type);
6221 if (objc_class_type) {
6222 clang::ObjCInterfaceDecl *class_interface_decl =
6223 objc_class_type->getInterface();
6224 return CompilerType(
6225 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
6226 idx, name, bit_offset_ptr,
6227 bitfield_bit_size_ptr, is_bitfield_ptr));
6228 }
6229 }
6230 break;
6231
6232 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00006233 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
6234 ->getDecl()
6235 ->getUnderlyingType()
6236 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00006237 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6238 is_bitfield_ptr);
6239
6240 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00006241 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
6242 ->getDeducedType()
6243 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00006244 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6245 is_bitfield_ptr);
6246
6247 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00006248 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
6249 ->getNamedType()
6250 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00006251 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6252 is_bitfield_ptr);
6253
6254 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00006255 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
6256 ->desugar()
6257 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00006258 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6259 is_bitfield_ptr);
6260
6261 default:
6262 break;
6263 }
6264 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006265}
6266
Greg Clayton99558cc42015-08-24 23:46:31 +00006267uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006268ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
6269 uint32_t count = 0;
6270 clang::QualType qual_type(GetCanonicalQualType(type));
6271 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6272 switch (type_class) {
6273 case clang::Type::Record:
6274 if (GetCompleteType(type)) {
6275 const clang::CXXRecordDecl *cxx_record_decl =
6276 qual_type->getAsCXXRecordDecl();
6277 if (cxx_record_decl)
6278 count = cxx_record_decl->getNumBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006279 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006280 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006281
Kate Stoneb9c1b512016-09-06 20:57:50 +00006282 case clang::Type::ObjCObjectPointer:
6283 count = GetPointeeType(type).GetNumDirectBaseClasses();
6284 break;
6285
6286 case clang::Type::ObjCObject:
6287 if (GetCompleteType(type)) {
6288 const clang::ObjCObjectType *objc_class_type =
6289 qual_type->getAsObjCQualifiedInterfaceType();
6290 if (objc_class_type) {
6291 clang::ObjCInterfaceDecl *class_interface_decl =
6292 objc_class_type->getInterface();
6293
6294 if (class_interface_decl && class_interface_decl->getSuperClass())
6295 count = 1;
6296 }
6297 }
6298 break;
6299 case clang::Type::ObjCInterface:
6300 if (GetCompleteType(type)) {
6301 const clang::ObjCInterfaceType *objc_interface_type =
6302 qual_type->getAs<clang::ObjCInterfaceType>();
6303 if (objc_interface_type) {
6304 clang::ObjCInterfaceDecl *class_interface_decl =
6305 objc_interface_type->getInterface();
6306
6307 if (class_interface_decl && class_interface_decl->getSuperClass())
6308 count = 1;
6309 }
6310 }
6311 break;
6312
6313 case clang::Type::Typedef:
6314 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6315 ->getDecl()
6316 ->getUnderlyingType()
6317 .getAsOpaquePtr());
6318 break;
6319
6320 case clang::Type::Auto:
6321 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6322 ->getDeducedType()
6323 .getAsOpaquePtr());
6324 break;
6325
6326 case clang::Type::Elaborated:
6327 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6328 ->getNamedType()
6329 .getAsOpaquePtr());
6330 break;
6331
6332 case clang::Type::Paren:
6333 return GetNumDirectBaseClasses(
6334 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6335
6336 default:
6337 break;
6338 }
6339 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006340}
6341
6342uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006343ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
6344 uint32_t count = 0;
6345 clang::QualType qual_type(GetCanonicalQualType(type));
6346 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6347 switch (type_class) {
6348 case clang::Type::Record:
6349 if (GetCompleteType(type)) {
6350 const clang::CXXRecordDecl *cxx_record_decl =
6351 qual_type->getAsCXXRecordDecl();
6352 if (cxx_record_decl)
6353 count = cxx_record_decl->getNumVBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006354 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006355 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006356
Kate Stoneb9c1b512016-09-06 20:57:50 +00006357 case clang::Type::Typedef:
6358 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6359 ->getDecl()
6360 ->getUnderlyingType()
6361 .getAsOpaquePtr());
6362 break;
6363
6364 case clang::Type::Auto:
6365 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6366 ->getDeducedType()
6367 .getAsOpaquePtr());
6368 break;
6369
6370 case clang::Type::Elaborated:
6371 count =
6372 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6373 ->getNamedType()
6374 .getAsOpaquePtr());
6375 break;
6376
6377 case clang::Type::Paren:
6378 count = GetNumVirtualBaseClasses(
6379 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6380 break;
6381
6382 default:
6383 break;
6384 }
6385 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006386}
6387
Kate Stoneb9c1b512016-09-06 20:57:50 +00006388CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
6389 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6390 clang::QualType qual_type(GetCanonicalQualType(type));
6391 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6392 switch (type_class) {
6393 case clang::Type::Record:
6394 if (GetCompleteType(type)) {
6395 const clang::CXXRecordDecl *cxx_record_decl =
6396 qual_type->getAsCXXRecordDecl();
6397 if (cxx_record_decl) {
6398 uint32_t curr_idx = 0;
6399 clang::CXXRecordDecl::base_class_const_iterator base_class,
6400 base_class_end;
6401 for (base_class = cxx_record_decl->bases_begin(),
6402 base_class_end = cxx_record_decl->bases_end();
6403 base_class != base_class_end; ++base_class, ++curr_idx) {
6404 if (curr_idx == idx) {
6405 if (bit_offset_ptr) {
6406 const clang::ASTRecordLayout &record_layout =
6407 getASTContext()->getASTRecordLayout(cxx_record_decl);
6408 const clang::CXXRecordDecl *base_class_decl =
6409 llvm::cast<clang::CXXRecordDecl>(
6410 base_class->getType()
6411 ->getAs<clang::RecordType>()
6412 ->getDecl());
6413 if (base_class->isVirtual())
6414 *bit_offset_ptr =
6415 record_layout.getVBaseClassOffset(base_class_decl)
6416 .getQuantity() *
6417 8;
6418 else
6419 *bit_offset_ptr =
6420 record_layout.getBaseClassOffset(base_class_decl)
6421 .getQuantity() *
6422 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006423 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006424 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6425 }
6426 }
6427 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006428 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006429 break;
6430
6431 case clang::Type::ObjCObjectPointer:
6432 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6433
6434 case clang::Type::ObjCObject:
6435 if (idx == 0 && GetCompleteType(type)) {
6436 const clang::ObjCObjectType *objc_class_type =
6437 qual_type->getAsObjCQualifiedInterfaceType();
6438 if (objc_class_type) {
6439 clang::ObjCInterfaceDecl *class_interface_decl =
6440 objc_class_type->getInterface();
6441
6442 if (class_interface_decl) {
6443 clang::ObjCInterfaceDecl *superclass_interface_decl =
6444 class_interface_decl->getSuperClass();
6445 if (superclass_interface_decl) {
6446 if (bit_offset_ptr)
6447 *bit_offset_ptr = 0;
Alex Langfordbddab072019-08-13 19:40:36 +00006448 return CompilerType(this,
Kate Stoneb9c1b512016-09-06 20:57:50 +00006449 getASTContext()->getObjCInterfaceType(
Alex Langfordbddab072019-08-13 19:40:36 +00006450 superclass_interface_decl).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006451 }
6452 }
6453 }
6454 }
6455 break;
6456 case clang::Type::ObjCInterface:
6457 if (idx == 0 && GetCompleteType(type)) {
6458 const clang::ObjCObjectType *objc_interface_type =
6459 qual_type->getAs<clang::ObjCInterfaceType>();
6460 if (objc_interface_type) {
6461 clang::ObjCInterfaceDecl *class_interface_decl =
6462 objc_interface_type->getInterface();
6463
6464 if (class_interface_decl) {
6465 clang::ObjCInterfaceDecl *superclass_interface_decl =
6466 class_interface_decl->getSuperClass();
6467 if (superclass_interface_decl) {
6468 if (bit_offset_ptr)
6469 *bit_offset_ptr = 0;
Alex Langfordbddab072019-08-13 19:40:36 +00006470 return CompilerType(
6471 this, getASTContext()
6472 ->getObjCInterfaceType(superclass_interface_decl)
6473 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006474 }
6475 }
6476 }
6477 }
6478 break;
6479
6480 case clang::Type::Typedef:
6481 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6482 ->getDecl()
6483 ->getUnderlyingType()
6484 .getAsOpaquePtr(),
6485 idx, bit_offset_ptr);
6486
6487 case clang::Type::Auto:
6488 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6489 ->getDeducedType()
6490 .getAsOpaquePtr(),
6491 idx, bit_offset_ptr);
6492
6493 case clang::Type::Elaborated:
6494 return GetDirectBaseClassAtIndex(
6495 llvm::cast<clang::ElaboratedType>(qual_type)
6496 ->getNamedType()
6497 .getAsOpaquePtr(),
6498 idx, bit_offset_ptr);
6499
6500 case clang::Type::Paren:
6501 return GetDirectBaseClassAtIndex(
6502 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6503 idx, bit_offset_ptr);
6504
6505 default:
6506 break;
6507 }
6508 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006509}
6510
Kate Stoneb9c1b512016-09-06 20:57:50 +00006511CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6512 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6513 clang::QualType qual_type(GetCanonicalQualType(type));
6514 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6515 switch (type_class) {
6516 case clang::Type::Record:
6517 if (GetCompleteType(type)) {
6518 const clang::CXXRecordDecl *cxx_record_decl =
6519 qual_type->getAsCXXRecordDecl();
6520 if (cxx_record_decl) {
6521 uint32_t curr_idx = 0;
6522 clang::CXXRecordDecl::base_class_const_iterator base_class,
6523 base_class_end;
6524 for (base_class = cxx_record_decl->vbases_begin(),
6525 base_class_end = cxx_record_decl->vbases_end();
6526 base_class != base_class_end; ++base_class, ++curr_idx) {
6527 if (curr_idx == idx) {
6528 if (bit_offset_ptr) {
6529 const clang::ASTRecordLayout &record_layout =
6530 getASTContext()->getASTRecordLayout(cxx_record_decl);
6531 const clang::CXXRecordDecl *base_class_decl =
6532 llvm::cast<clang::CXXRecordDecl>(
6533 base_class->getType()
6534 ->getAs<clang::RecordType>()
6535 ->getDecl());
6536 *bit_offset_ptr =
6537 record_layout.getVBaseClassOffset(base_class_decl)
6538 .getQuantity() *
6539 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006540 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006541 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6542 }
6543 }
6544 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006545 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006546 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006547
Kate Stoneb9c1b512016-09-06 20:57:50 +00006548 case clang::Type::Typedef:
6549 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6550 ->getDecl()
6551 ->getUnderlyingType()
6552 .getAsOpaquePtr(),
6553 idx, bit_offset_ptr);
6554
6555 case clang::Type::Auto:
6556 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6557 ->getDeducedType()
6558 .getAsOpaquePtr(),
6559 idx, bit_offset_ptr);
6560
6561 case clang::Type::Elaborated:
6562 return GetVirtualBaseClassAtIndex(
6563 llvm::cast<clang::ElaboratedType>(qual_type)
6564 ->getNamedType()
6565 .getAsOpaquePtr(),
6566 idx, bit_offset_ptr);
6567
6568 case clang::Type::Paren:
6569 return GetVirtualBaseClassAtIndex(
6570 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6571 idx, bit_offset_ptr);
6572
6573 default:
6574 break;
6575 }
6576 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006577}
6578
Greg Claytond8d4a572015-08-11 21:38:15 +00006579// If a pointer to a pointee type (the clang_type arg) says that it has no
6580// children, then we either need to trust it, or override it and return a
6581// different result. For example, an "int *" has one child that is an integer,
6582// but a function pointer doesn't have any children. Likewise if a Record type
6583// claims it has no children, then there really is nothing to show.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006584uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6585 if (type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00006586 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006587
6588 clang::QualType qual_type(type.getCanonicalType());
6589 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6590 switch (type_class) {
6591 case clang::Type::Builtin:
6592 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6593 case clang::BuiltinType::UnknownAny:
6594 case clang::BuiltinType::Void:
6595 case clang::BuiltinType::NullPtr:
6596 case clang::BuiltinType::OCLEvent:
6597 case clang::BuiltinType::OCLImage1dRO:
6598 case clang::BuiltinType::OCLImage1dWO:
6599 case clang::BuiltinType::OCLImage1dRW:
6600 case clang::BuiltinType::OCLImage1dArrayRO:
6601 case clang::BuiltinType::OCLImage1dArrayWO:
6602 case clang::BuiltinType::OCLImage1dArrayRW:
6603 case clang::BuiltinType::OCLImage1dBufferRO:
6604 case clang::BuiltinType::OCLImage1dBufferWO:
6605 case clang::BuiltinType::OCLImage1dBufferRW:
6606 case clang::BuiltinType::OCLImage2dRO:
6607 case clang::BuiltinType::OCLImage2dWO:
6608 case clang::BuiltinType::OCLImage2dRW:
6609 case clang::BuiltinType::OCLImage2dArrayRO:
6610 case clang::BuiltinType::OCLImage2dArrayWO:
6611 case clang::BuiltinType::OCLImage2dArrayRW:
6612 case clang::BuiltinType::OCLImage3dRO:
6613 case clang::BuiltinType::OCLImage3dWO:
6614 case clang::BuiltinType::OCLImage3dRW:
6615 case clang::BuiltinType::OCLSampler:
6616 return 0;
6617 case clang::BuiltinType::Bool:
6618 case clang::BuiltinType::Char_U:
6619 case clang::BuiltinType::UChar:
6620 case clang::BuiltinType::WChar_U:
6621 case clang::BuiltinType::Char16:
6622 case clang::BuiltinType::Char32:
6623 case clang::BuiltinType::UShort:
6624 case clang::BuiltinType::UInt:
6625 case clang::BuiltinType::ULong:
6626 case clang::BuiltinType::ULongLong:
6627 case clang::BuiltinType::UInt128:
6628 case clang::BuiltinType::Char_S:
6629 case clang::BuiltinType::SChar:
6630 case clang::BuiltinType::WChar_S:
6631 case clang::BuiltinType::Short:
6632 case clang::BuiltinType::Int:
6633 case clang::BuiltinType::Long:
6634 case clang::BuiltinType::LongLong:
6635 case clang::BuiltinType::Int128:
6636 case clang::BuiltinType::Float:
6637 case clang::BuiltinType::Double:
6638 case clang::BuiltinType::LongDouble:
6639 case clang::BuiltinType::Dependent:
6640 case clang::BuiltinType::Overload:
6641 case clang::BuiltinType::ObjCId:
6642 case clang::BuiltinType::ObjCClass:
6643 case clang::BuiltinType::ObjCSel:
6644 case clang::BuiltinType::BoundMember:
6645 case clang::BuiltinType::Half:
6646 case clang::BuiltinType::ARCUnbridgedCast:
6647 case clang::BuiltinType::PseudoObject:
6648 case clang::BuiltinType::BuiltinFn:
6649 case clang::BuiltinType::OMPArraySection:
6650 return 1;
6651 default:
6652 return 0;
6653 }
6654 break;
6655
6656 case clang::Type::Complex:
6657 return 1;
6658 case clang::Type::Pointer:
6659 return 1;
6660 case clang::Type::BlockPointer:
6661 return 0; // If block pointers don't have debug info, then no children for
6662 // them
6663 case clang::Type::LValueReference:
6664 return 1;
6665 case clang::Type::RValueReference:
6666 return 1;
6667 case clang::Type::MemberPointer:
6668 return 0;
6669 case clang::Type::ConstantArray:
6670 return 0;
6671 case clang::Type::IncompleteArray:
6672 return 0;
6673 case clang::Type::VariableArray:
6674 return 0;
6675 case clang::Type::DependentSizedArray:
6676 return 0;
6677 case clang::Type::DependentSizedExtVector:
6678 return 0;
6679 case clang::Type::Vector:
6680 return 0;
6681 case clang::Type::ExtVector:
6682 return 0;
6683 case clang::Type::FunctionProto:
6684 return 0; // When we function pointers, they have no children...
6685 case clang::Type::FunctionNoProto:
6686 return 0; // When we function pointers, they have no children...
6687 case clang::Type::UnresolvedUsing:
6688 return 0;
6689 case clang::Type::Paren:
6690 return GetNumPointeeChildren(
6691 llvm::cast<clang::ParenType>(qual_type)->desugar());
6692 case clang::Type::Typedef:
6693 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6694 ->getDecl()
6695 ->getUnderlyingType());
6696 case clang::Type::Auto:
6697 return GetNumPointeeChildren(
6698 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6699 case clang::Type::Elaborated:
6700 return GetNumPointeeChildren(
6701 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6702 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006703 return GetNumPointeeChildren(llvm::cast<clang::TypeOfExprType>(qual_type)
6704 ->getUnderlyingExpr()
6705 ->getType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006706 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006707 return GetNumPointeeChildren(
6708 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006709 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006710 return GetNumPointeeChildren(
6711 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006712 case clang::Type::Record:
6713 return 0;
6714 case clang::Type::Enum:
6715 return 1;
6716 case clang::Type::TemplateTypeParm:
6717 return 1;
6718 case clang::Type::SubstTemplateTypeParm:
6719 return 1;
6720 case clang::Type::TemplateSpecialization:
6721 return 1;
6722 case clang::Type::InjectedClassName:
6723 return 0;
6724 case clang::Type::DependentName:
6725 return 1;
6726 case clang::Type::DependentTemplateSpecialization:
6727 return 1;
6728 case clang::Type::ObjCObject:
6729 return 0;
6730 case clang::Type::ObjCInterface:
6731 return 0;
6732 case clang::Type::ObjCObjectPointer:
6733 return 1;
6734 default:
6735 break;
6736 }
6737 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006738}
6739
Kate Stoneb9c1b512016-09-06 20:57:50 +00006740CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6741 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6742 bool transparent_pointers, bool omit_empty_base_classes,
6743 bool ignore_array_bounds, std::string &child_name,
6744 uint32_t &child_byte_size, int32_t &child_byte_offset,
6745 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6746 bool &child_is_base_class, bool &child_is_deref_of_parent,
6747 ValueObject *valobj, uint64_t &language_flags) {
6748 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006749 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006750
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006751 auto get_exe_scope = [&exe_ctx]() {
6752 return exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr;
6753 };
6754
Kate Stoneb9c1b512016-09-06 20:57:50 +00006755 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6756 const clang::Type::TypeClass parent_type_class =
6757 parent_qual_type->getTypeClass();
6758 child_bitfield_bit_size = 0;
6759 child_bitfield_bit_offset = 0;
6760 child_is_base_class = false;
6761 language_flags = 0;
6762
Adrian Prantleca07c52018-11-05 20:49:07 +00006763 const bool idx_is_valid =
6764 idx < GetNumChildren(type, omit_empty_base_classes, exe_ctx);
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00006765 int32_t bit_offset;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006766 switch (parent_type_class) {
6767 case clang::Type::Builtin:
6768 if (idx_is_valid) {
6769 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6770 case clang::BuiltinType::ObjCId:
6771 case clang::BuiltinType::ObjCClass:
6772 child_name = "isa";
6773 child_byte_size =
6774 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6775 CHAR_BIT;
Alex Langfordbddab072019-08-13 19:40:36 +00006776 return CompilerType(
6777 this, getASTContext()->ObjCBuiltinClassTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006778
6779 default:
6780 break;
6781 }
6782 }
6783 break;
6784
6785 case clang::Type::Record:
6786 if (idx_is_valid && GetCompleteType(type)) {
6787 const clang::RecordType *record_type =
6788 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6789 const clang::RecordDecl *record_decl = record_type->getDecl();
6790 assert(record_decl);
6791 const clang::ASTRecordLayout &record_layout =
6792 getASTContext()->getASTRecordLayout(record_decl);
6793 uint32_t child_idx = 0;
6794
6795 const clang::CXXRecordDecl *cxx_record_decl =
6796 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6797 if (cxx_record_decl) {
6798 // We might have base classes to print out first
6799 clang::CXXRecordDecl::base_class_const_iterator base_class,
6800 base_class_end;
6801 for (base_class = cxx_record_decl->bases_begin(),
6802 base_class_end = cxx_record_decl->bases_end();
6803 base_class != base_class_end; ++base_class) {
6804 const clang::CXXRecordDecl *base_class_decl = nullptr;
6805
6806 // Skip empty base classes
6807 if (omit_empty_base_classes) {
6808 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6809 base_class->getType()->getAs<clang::RecordType>()->getDecl());
Jonas Devliegherea6682a42018-12-15 00:15:33 +00006810 if (!ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00006811 continue;
6812 }
6813
6814 if (idx == child_idx) {
6815 if (base_class_decl == nullptr)
6816 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6817 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6818
6819 if (base_class->isVirtual()) {
6820 bool handled = false;
6821 if (valobj) {
Aleksandr Urakov1dc51db2018-11-12 16:23:50 +00006822 clang::VTableContextBase *vtable_ctx =
6823 getASTContext()->getVTableContext();
6824 if (vtable_ctx)
6825 handled = GetVBaseBitOffset(*vtable_ctx, *valobj,
6826 record_layout, cxx_record_decl,
6827 base_class_decl, bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006828 }
6829 if (!handled)
6830 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6831 .getQuantity() *
6832 8;
6833 } else
6834 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6835 .getQuantity() *
6836 8;
6837
6838 // Base classes should be a multiple of 8 bits in size
6839 child_byte_offset = bit_offset / 8;
Alex Langfordbddab072019-08-13 19:40:36 +00006840 CompilerType base_class_clang_type(
6841 this, base_class->getType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006842 child_name = base_class_clang_type.GetTypeName().AsCString("");
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006843 Optional<uint64_t> size =
6844 base_class_clang_type.GetBitSize(get_exe_scope());
Adrian Prantld963a7c2019-01-15 18:07:52 +00006845 if (!size)
6846 return {};
6847 uint64_t base_class_clang_type_bit_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006848
6849 // Base classes bit sizes should be a multiple of 8 bits in size
6850 assert(base_class_clang_type_bit_size % 8 == 0);
6851 child_byte_size = base_class_clang_type_bit_size / 8;
6852 child_is_base_class = true;
6853 return base_class_clang_type;
6854 }
6855 // We don't increment the child index in the for loop since we might
6856 // be skipping empty base classes
6857 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006858 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006859 }
6860 // Make sure index is in range...
6861 uint32_t field_idx = 0;
6862 clang::RecordDecl::field_iterator field, field_end;
6863 for (field = record_decl->field_begin(),
6864 field_end = record_decl->field_end();
6865 field != field_end; ++field, ++field_idx, ++child_idx) {
6866 if (idx == child_idx) {
6867 // Print the member type if requested
6868 // Print the member name and equal sign
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006869 child_name.assign(field->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006870
6871 // Figure out the type byte size (field_type_info.first) and
6872 // alignment (field_type_info.second) from the AST context.
Alex Langfordbddab072019-08-13 19:40:36 +00006873 CompilerType field_clang_type(this,
6874 field->getType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006875 assert(field_idx < record_layout.getFieldCount());
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006876 Optional<uint64_t> size =
6877 field_clang_type.GetByteSize(get_exe_scope());
Adrian Prantld963a7c2019-01-15 18:07:52 +00006878 if (!size)
6879 return {};
6880 child_byte_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006881 const uint32_t child_bit_size = child_byte_size * 8;
6882
6883 // Figure out the field offset within the current struct/union/class
6884 // type
6885 bit_offset = record_layout.getFieldOffset(field_idx);
6886 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6887 child_bitfield_bit_size)) {
6888 child_bitfield_bit_offset = bit_offset % child_bit_size;
6889 const uint32_t child_bit_offset =
6890 bit_offset - child_bitfield_bit_offset;
6891 child_byte_offset = child_bit_offset / 8;
6892 } else {
6893 child_byte_offset = bit_offset / 8;
6894 }
6895
6896 return field_clang_type;
6897 }
6898 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006899 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006900 break;
6901
6902 case clang::Type::ObjCObject:
6903 case clang::Type::ObjCInterface:
6904 if (idx_is_valid && GetCompleteType(type)) {
6905 const clang::ObjCObjectType *objc_class_type =
6906 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6907 assert(objc_class_type);
6908 if (objc_class_type) {
6909 uint32_t child_idx = 0;
6910 clang::ObjCInterfaceDecl *class_interface_decl =
6911 objc_class_type->getInterface();
6912
6913 if (class_interface_decl) {
6914
6915 const clang::ASTRecordLayout &interface_layout =
6916 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6917 clang::ObjCInterfaceDecl *superclass_interface_decl =
6918 class_interface_decl->getSuperClass();
6919 if (superclass_interface_decl) {
6920 if (omit_empty_base_classes) {
6921 CompilerType base_class_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00006922 this, getASTContext()
6923 ->getObjCInterfaceType(superclass_interface_decl)
6924 .getAsOpaquePtr());
Adrian Prantleca07c52018-11-05 20:49:07 +00006925 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes,
6926 exe_ctx) > 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006927 if (idx == 0) {
6928 clang::QualType ivar_qual_type(
6929 getASTContext()->getObjCInterfaceType(
6930 superclass_interface_decl));
6931
6932 child_name.assign(
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006933 superclass_interface_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006934
6935 clang::TypeInfo ivar_type_info =
6936 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6937
6938 child_byte_size = ivar_type_info.Width / 8;
6939 child_byte_offset = 0;
6940 child_is_base_class = true;
6941
Alex Langfordbddab072019-08-13 19:40:36 +00006942 return CompilerType(this, ivar_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006943 }
6944
6945 ++child_idx;
6946 }
6947 } else
6948 ++child_idx;
6949 }
6950
6951 const uint32_t superclass_idx = child_idx;
6952
6953 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6954 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6955 ivar_end = class_interface_decl->ivar_end();
6956
6957 for (ivar_pos = class_interface_decl->ivar_begin();
6958 ivar_pos != ivar_end; ++ivar_pos) {
6959 if (child_idx == idx) {
6960 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6961
6962 clang::QualType ivar_qual_type(ivar_decl->getType());
6963
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006964 child_name.assign(ivar_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006965
6966 clang::TypeInfo ivar_type_info =
6967 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6968
6969 child_byte_size = ivar_type_info.Width / 8;
6970
6971 // Figure out the field offset within the current
Adrian Prantl05097242018-04-30 16:49:04 +00006972 // struct/union/class type For ObjC objects, we can't trust the
6973 // bit offset we get from the Clang AST, since that doesn't
6974 // account for the space taken up by unbacked properties, or
6975 // from the changing size of base classes that are newer than
6976 // this class. So if we have a process around that we can ask
6977 // about this object, do so.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006978 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6979 Process *process = nullptr;
6980 if (exe_ctx)
6981 process = exe_ctx->GetProcessPtr();
6982 if (process) {
6983 ObjCLanguageRuntime *objc_runtime =
Alex Langforde823bbe2019-06-10 20:53:23 +00006984 ObjCLanguageRuntime::Get(*process);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006985 if (objc_runtime != nullptr) {
Alex Langfordbddab072019-08-13 19:40:36 +00006986 CompilerType parent_ast_type(
6987 this, parent_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006988 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6989 parent_ast_type, ivar_decl->getNameAsString().c_str());
6990 }
6991 }
6992
Aleksandr Urakovff701722018-08-20 05:59:27 +00006993 // Setting this to INT32_MAX to make sure we don't compute it
Kate Stoneb9c1b512016-09-06 20:57:50 +00006994 // twice...
Aleksandr Urakov53459482018-08-17 07:28:24 +00006995 bit_offset = INT32_MAX;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006996
6997 if (child_byte_offset ==
6998 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6999 bit_offset = interface_layout.getFieldOffset(child_idx -
7000 superclass_idx);
7001 child_byte_offset = bit_offset / 8;
7002 }
7003
7004 // Note, the ObjC Ivar Byte offset is just that, it doesn't
Adrian Prantl05097242018-04-30 16:49:04 +00007005 // account for the bit offset of a bitfield within its
7006 // containing object. So regardless of where we get the byte
Kate Stoneb9c1b512016-09-06 20:57:50 +00007007 // offset from, we still need to get the bit offset for
7008 // bitfields from the layout.
7009
7010 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
7011 child_bitfield_bit_size)) {
Aleksandr Urakov53459482018-08-17 07:28:24 +00007012 if (bit_offset == INT32_MAX)
Kate Stoneb9c1b512016-09-06 20:57:50 +00007013 bit_offset = interface_layout.getFieldOffset(
7014 child_idx - superclass_idx);
7015
7016 child_bitfield_bit_offset = bit_offset % 8;
7017 }
Alex Langfordbddab072019-08-13 19:40:36 +00007018 return CompilerType(this, ivar_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007019 }
7020 ++child_idx;
7021 }
7022 }
7023 }
7024 }
7025 }
7026 break;
7027
7028 case clang::Type::ObjCObjectPointer:
7029 if (idx_is_valid) {
7030 CompilerType pointee_clang_type(GetPointeeType(type));
7031
7032 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
7033 child_is_deref_of_parent = false;
7034 bool tmp_child_is_deref_of_parent = false;
7035 return pointee_clang_type.GetChildCompilerTypeAtIndex(
7036 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7037 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7038 child_bitfield_bit_size, child_bitfield_bit_offset,
7039 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
7040 language_flags);
7041 } else {
7042 child_is_deref_of_parent = true;
7043 const char *parent_name =
Konrad Kleine248a1302019-05-23 11:14:47 +00007044 valobj ? valobj->GetName().GetCString() : nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007045 if (parent_name) {
7046 child_name.assign(1, '*');
7047 child_name += parent_name;
7048 }
7049
7050 // We have a pointer to an simple type
7051 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00007052 if (Optional<uint64_t> size =
7053 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00007054 child_byte_size = *size;
7055 child_byte_offset = 0;
7056 return pointee_clang_type;
7057 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007058 }
7059 }
7060 }
7061 break;
7062
7063 case clang::Type::Vector:
7064 case clang::Type::ExtVector:
7065 if (idx_is_valid) {
7066 const clang::VectorType *array =
7067 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
7068 if (array) {
Alex Langfordbddab072019-08-13 19:40:36 +00007069 CompilerType element_type(this,
7070 array->getElementType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007071 if (element_type.GetCompleteType()) {
7072 char element_name[64];
7073 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
7074 static_cast<uint64_t>(idx));
7075 child_name.assign(element_name);
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00007076 if (Optional<uint64_t> size =
7077 element_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00007078 child_byte_size = *size;
7079 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
7080 return element_type;
7081 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007082 }
7083 }
7084 }
7085 break;
7086
7087 case clang::Type::ConstantArray:
7088 case clang::Type::IncompleteArray:
7089 if (ignore_array_bounds || idx_is_valid) {
7090 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
7091 if (array) {
Alex Langfordbddab072019-08-13 19:40:36 +00007092 CompilerType element_type(this,
7093 array->getElementType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007094 if (element_type.GetCompleteType()) {
Zachary Turner827d5d72016-12-16 04:27:00 +00007095 child_name = llvm::formatv("[{0}]", idx);
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00007096 if (Optional<uint64_t> size =
7097 element_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00007098 child_byte_size = *size;
7099 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
7100 return element_type;
7101 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007102 }
7103 }
7104 }
7105 break;
7106
Tamas Berghammer1c62e032017-01-07 16:39:07 +00007107 case clang::Type::Pointer: {
7108 CompilerType pointee_clang_type(GetPointeeType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00007109
Tamas Berghammer1c62e032017-01-07 16:39:07 +00007110 // Don't dereference "void *" pointers
7111 if (pointee_clang_type.IsVoidType())
7112 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00007113
Tamas Berghammer1c62e032017-01-07 16:39:07 +00007114 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
7115 child_is_deref_of_parent = false;
7116 bool tmp_child_is_deref_of_parent = false;
7117 return pointee_clang_type.GetChildCompilerTypeAtIndex(
7118 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7119 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7120 child_bitfield_bit_size, child_bitfield_bit_offset,
7121 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
7122 language_flags);
7123 } else {
7124 child_is_deref_of_parent = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007125
Tamas Berghammer1c62e032017-01-07 16:39:07 +00007126 const char *parent_name =
Konrad Kleine248a1302019-05-23 11:14:47 +00007127 valobj ? valobj->GetName().GetCString() : nullptr;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00007128 if (parent_name) {
7129 child_name.assign(1, '*');
7130 child_name += parent_name;
7131 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007132
Tamas Berghammer1c62e032017-01-07 16:39:07 +00007133 // We have a pointer to an simple type
7134 if (idx == 0) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00007135 if (Optional<uint64_t> size =
7136 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00007137 child_byte_size = *size;
7138 child_byte_offset = 0;
7139 return pointee_clang_type;
7140 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007141 }
7142 }
7143 break;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00007144 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007145
7146 case clang::Type::LValueReference:
7147 case clang::Type::RValueReference:
7148 if (idx_is_valid) {
7149 const clang::ReferenceType *reference_type =
7150 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
Alex Langfordbddab072019-08-13 19:40:36 +00007151 CompilerType pointee_clang_type(
7152 this, reference_type->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007153 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
7154 child_is_deref_of_parent = false;
7155 bool tmp_child_is_deref_of_parent = false;
7156 return pointee_clang_type.GetChildCompilerTypeAtIndex(
7157 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7158 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7159 child_bitfield_bit_size, child_bitfield_bit_offset,
7160 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
7161 language_flags);
7162 } else {
7163 const char *parent_name =
Konrad Kleine248a1302019-05-23 11:14:47 +00007164 valobj ? valobj->GetName().GetCString() : nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007165 if (parent_name) {
7166 child_name.assign(1, '&');
7167 child_name += parent_name;
7168 }
7169
7170 // We have a pointer to an simple type
7171 if (idx == 0) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00007172 if (Optional<uint64_t> size =
7173 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00007174 child_byte_size = *size;
7175 child_byte_offset = 0;
7176 return pointee_clang_type;
7177 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007178 }
7179 }
7180 }
7181 break;
7182
7183 case clang::Type::Typedef: {
7184 CompilerType typedefed_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00007185 this, llvm::cast<clang::TypedefType>(parent_qual_type)
7186 ->getDecl()
7187 ->getUnderlyingType()
7188 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007189 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
7190 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7191 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7192 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7193 child_is_deref_of_parent, valobj, language_flags);
7194 } break;
7195
7196 case clang::Type::Auto: {
7197 CompilerType elaborated_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00007198 this, llvm::cast<clang::AutoType>(parent_qual_type)
7199 ->getDeducedType()
7200 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007201 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
7202 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7203 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7204 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7205 child_is_deref_of_parent, valobj, language_flags);
7206 }
7207
7208 case clang::Type::Elaborated: {
7209 CompilerType elaborated_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00007210 this, llvm::cast<clang::ElaboratedType>(parent_qual_type)
7211 ->getNamedType()
7212 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007213 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
7214 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7215 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7216 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7217 child_is_deref_of_parent, valobj, language_flags);
7218 }
7219
7220 case clang::Type::Paren: {
Alex Langfordbddab072019-08-13 19:40:36 +00007221 CompilerType paren_clang_type(this,
7222 llvm::cast<clang::ParenType>(parent_qual_type)
7223 ->desugar()
7224 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007225 return paren_clang_type.GetChildCompilerTypeAtIndex(
7226 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7227 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7228 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7229 child_is_deref_of_parent, valobj, language_flags);
7230 }
7231
7232 default:
7233 break;
7234 }
7235 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007236}
7237
Kate Stoneb9c1b512016-09-06 20:57:50 +00007238static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
7239 const clang::CXXBaseSpecifier *base_spec,
7240 bool omit_empty_base_classes) {
7241 uint32_t child_idx = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007242
Kate Stoneb9c1b512016-09-06 20:57:50 +00007243 const clang::CXXRecordDecl *cxx_record_decl =
7244 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7245
7246 // const char *super_name = record_decl->getNameAsCString();
7247 // const char *base_name =
7248 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
7249 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
7250 //
7251 if (cxx_record_decl) {
7252 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
7253 for (base_class = cxx_record_decl->bases_begin(),
7254 base_class_end = cxx_record_decl->bases_end();
7255 base_class != base_class_end; ++base_class) {
7256 if (omit_empty_base_classes) {
7257 if (BaseSpecifierIsEmpty(base_class))
7258 continue;
7259 }
7260
7261 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
7262 // super_name, base_name,
7263 // child_idx,
7264 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
7265 //
7266 //
7267 if (base_class == base_spec)
7268 return child_idx;
7269 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00007270 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007271 }
7272
7273 return UINT32_MAX;
7274}
7275
7276static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
7277 clang::NamedDecl *canonical_decl,
7278 bool omit_empty_base_classes) {
7279 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
7280 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
7281 omit_empty_base_classes);
7282
7283 clang::RecordDecl::field_iterator field, field_end;
7284 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
7285 field != field_end; ++field, ++child_idx) {
7286 if (field->getCanonicalDecl() == canonical_decl)
7287 return child_idx;
7288 }
7289
7290 return UINT32_MAX;
Greg Claytond8d4a572015-08-11 21:38:15 +00007291}
7292
7293// Look for a child member (doesn't include base classes, but it does include
Adrian Prantl05097242018-04-30 16:49:04 +00007294// their members) in the type hierarchy. Returns an index path into
7295// "clang_type" on how to reach the appropriate member.
Greg Claytond8d4a572015-08-11 21:38:15 +00007296//
7297// class A
7298// {
7299// public:
7300// int m_a;
7301// int m_b;
7302// };
7303//
7304// class B
7305// {
7306// };
7307//
7308// class C :
7309// public B,
7310// public A
7311// {
7312// };
7313//
7314// If we have a clang type that describes "class C", and we wanted to looked
7315// "m_b" in it:
7316//
Kate Stoneb9c1b512016-09-06 20:57:50 +00007317// With omit_empty_base_classes == false we would get an integer array back
Adrian Prantl05097242018-04-30 16:49:04 +00007318// with: { 1, 1 } The first index 1 is the child index for "class A" within
7319// class C The second index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007320//
Adrian Prantl05097242018-04-30 16:49:04 +00007321// With omit_empty_base_classes == true we would get an integer array back
7322// with: { 0, 1 } The first index 0 is the child index for "class A" within
7323// class C (since class B doesn't have any members it doesn't count) The second
7324// index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007325
Kate Stoneb9c1b512016-09-06 20:57:50 +00007326size_t ClangASTContext::GetIndexOfChildMemberWithName(
7327 lldb::opaque_compiler_type_t type, const char *name,
7328 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
7329 if (type && name && name[0]) {
7330 clang::QualType qual_type(GetCanonicalQualType(type));
7331 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7332 switch (type_class) {
7333 case clang::Type::Record:
7334 if (GetCompleteType(type)) {
7335 const clang::RecordType *record_type =
7336 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7337 const clang::RecordDecl *record_decl = record_type->getDecl();
Enrico Granata36f51e42015-12-18 22:41:25 +00007338
Kate Stoneb9c1b512016-09-06 20:57:50 +00007339 assert(record_decl);
7340 uint32_t child_idx = 0;
7341
7342 const clang::CXXRecordDecl *cxx_record_decl =
7343 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7344
7345 // Try and find a field that matches NAME
7346 clang::RecordDecl::field_iterator field, field_end;
7347 llvm::StringRef name_sref(name);
7348 for (field = record_decl->field_begin(),
7349 field_end = record_decl->field_end();
7350 field != field_end; ++field, ++child_idx) {
7351 llvm::StringRef field_name = field->getName();
7352 if (field_name.empty()) {
Alex Langfordbddab072019-08-13 19:40:36 +00007353 CompilerType field_type(this, field->getType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007354 child_indexes.push_back(child_idx);
7355 if (field_type.GetIndexOfChildMemberWithName(
7356 name, omit_empty_base_classes, child_indexes))
7357 return child_indexes.size();
7358 child_indexes.pop_back();
7359
7360 } else if (field_name.equals(name_sref)) {
7361 // We have to add on the number of base classes to this index!
7362 child_indexes.push_back(
7363 child_idx + ClangASTContext::GetNumBaseClasses(
7364 cxx_record_decl, omit_empty_base_classes));
7365 return child_indexes.size();
7366 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007367 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007368
Kate Stoneb9c1b512016-09-06 20:57:50 +00007369 if (cxx_record_decl) {
7370 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7371
7372 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7373
7374 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7375 // Didn't find things easily, lets let clang do its thang...
7376 clang::IdentifierInfo &ident_ref =
7377 getASTContext()->Idents.get(name_sref);
7378 clang::DeclarationName decl_name(&ident_ref);
7379
7380 clang::CXXBasePaths paths;
7381 if (cxx_record_decl->lookupInBases(
7382 [decl_name](const clang::CXXBaseSpecifier *specifier,
7383 clang::CXXBasePath &path) {
7384 return clang::CXXRecordDecl::FindOrdinaryMember(
7385 specifier, path, decl_name);
7386 },
7387 paths)) {
7388 clang::CXXBasePaths::const_paths_iterator path,
7389 path_end = paths.end();
7390 for (path = paths.begin(); path != path_end; ++path) {
7391 const size_t num_path_elements = path->size();
7392 for (size_t e = 0; e < num_path_elements; ++e) {
7393 clang::CXXBasePathElement elem = (*path)[e];
7394
7395 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7396 omit_empty_base_classes);
7397 if (child_idx == UINT32_MAX) {
7398 child_indexes.clear();
7399 return 0;
7400 } else {
7401 child_indexes.push_back(child_idx);
7402 parent_record_decl = llvm::cast<clang::RecordDecl>(
7403 elem.Base->getType()
7404 ->getAs<clang::RecordType>()
7405 ->getDecl());
7406 }
7407 }
7408 for (clang::NamedDecl *path_decl : path->Decls) {
7409 child_idx = GetIndexForRecordChild(
7410 parent_record_decl, path_decl, omit_empty_base_classes);
7411 if (child_idx == UINT32_MAX) {
7412 child_indexes.clear();
7413 return 0;
7414 } else {
7415 child_indexes.push_back(child_idx);
7416 }
7417 }
7418 }
7419 return child_indexes.size();
7420 }
7421 }
7422 }
7423 break;
7424
7425 case clang::Type::ObjCObject:
7426 case clang::Type::ObjCInterface:
7427 if (GetCompleteType(type)) {
7428 llvm::StringRef name_sref(name);
7429 const clang::ObjCObjectType *objc_class_type =
7430 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7431 assert(objc_class_type);
7432 if (objc_class_type) {
7433 uint32_t child_idx = 0;
7434 clang::ObjCInterfaceDecl *class_interface_decl =
7435 objc_class_type->getInterface();
7436
7437 if (class_interface_decl) {
7438 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7439 ivar_end = class_interface_decl->ivar_end();
7440 clang::ObjCInterfaceDecl *superclass_interface_decl =
7441 class_interface_decl->getSuperClass();
7442
7443 for (ivar_pos = class_interface_decl->ivar_begin();
7444 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7445 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7446
7447 if (ivar_decl->getName().equals(name_sref)) {
7448 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7449 (omit_empty_base_classes &&
7450 ObjCDeclHasIVars(superclass_interface_decl, true)))
7451 ++child_idx;
7452
7453 child_indexes.push_back(child_idx);
7454 return child_indexes.size();
7455 }
7456 }
7457
7458 if (superclass_interface_decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00007459 // The super class index is always zero for ObjC classes, so we
7460 // push it onto the child indexes in case we find an ivar in our
7461 // superclass...
Kate Stoneb9c1b512016-09-06 20:57:50 +00007462 child_indexes.push_back(0);
7463
7464 CompilerType superclass_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00007465 this, getASTContext()
7466 ->getObjCInterfaceType(superclass_interface_decl)
7467 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007468 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7469 name, omit_empty_base_classes, child_indexes)) {
Adrian Prantl05097242018-04-30 16:49:04 +00007470 // We did find an ivar in a superclass so just return the
7471 // results!
Kate Stoneb9c1b512016-09-06 20:57:50 +00007472 return child_indexes.size();
7473 }
7474
Adrian Prantl05097242018-04-30 16:49:04 +00007475 // We didn't find an ivar matching "name" in our superclass, pop
7476 // the superclass zero index that we pushed on above.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007477 child_indexes.pop_back();
7478 }
7479 }
7480 }
7481 }
7482 break;
7483
7484 case clang::Type::ObjCObjectPointer: {
7485 CompilerType objc_object_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00007486 this, llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7487 ->getPointeeType()
7488 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007489 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7490 name, omit_empty_base_classes, child_indexes);
7491 } break;
7492
7493 case clang::Type::ConstantArray: {
7494 // const clang::ConstantArrayType *array =
7495 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7496 // const uint64_t element_count =
7497 // array->getSize().getLimitedValue();
7498 //
7499 // if (idx < element_count)
7500 // {
7501 // std::pair<uint64_t, unsigned> field_type_info =
7502 // ast->getTypeInfo(array->getElementType());
7503 //
7504 // char element_name[32];
7505 // ::snprintf (element_name, sizeof (element_name),
7506 // "%s[%u]", parent_name ? parent_name : "", idx);
7507 //
7508 // child_name.assign(element_name);
7509 // assert(field_type_info.first % 8 == 0);
7510 // child_byte_size = field_type_info.first / 8;
7511 // child_byte_offset = idx * child_byte_size;
7512 // return array->getElementType().getAsOpaquePtr();
7513 // }
7514 } break;
7515
7516 // case clang::Type::MemberPointerType:
7517 // {
7518 // MemberPointerType *mem_ptr_type =
7519 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7520 // clang::QualType pointee_type =
7521 // mem_ptr_type->getPointeeType();
7522 //
7523 // if (ClangASTContext::IsAggregateType
7524 // (pointee_type.getAsOpaquePtr()))
7525 // {
7526 // return GetIndexOfChildWithName (ast,
7527 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7528 // name);
7529 // }
7530 // }
7531 // break;
7532 //
7533 case clang::Type::LValueReference:
7534 case clang::Type::RValueReference: {
7535 const clang::ReferenceType *reference_type =
7536 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7537 clang::QualType pointee_type(reference_type->getPointeeType());
Alex Langfordbddab072019-08-13 19:40:36 +00007538 CompilerType pointee_clang_type(this, pointee_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007539
7540 if (pointee_clang_type.IsAggregateType()) {
7541 return pointee_clang_type.GetIndexOfChildMemberWithName(
7542 name, omit_empty_base_classes, child_indexes);
7543 }
7544 } break;
7545
7546 case clang::Type::Pointer: {
7547 CompilerType pointee_clang_type(GetPointeeType(type));
7548
7549 if (pointee_clang_type.IsAggregateType()) {
7550 return pointee_clang_type.GetIndexOfChildMemberWithName(
7551 name, omit_empty_base_classes, child_indexes);
7552 }
7553 } break;
7554
7555 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00007556 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
7557 ->getDecl()
7558 ->getUnderlyingType()
7559 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007560 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7561 child_indexes);
7562
7563 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00007564 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
7565 ->getDeducedType()
7566 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007567 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7568 child_indexes);
7569
7570 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00007571 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
7572 ->getNamedType()
7573 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007574 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7575 child_indexes);
7576
7577 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00007578 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
7579 ->desugar()
7580 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007581 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7582 child_indexes);
7583
7584 default:
7585 break;
7586 }
7587 }
7588 return 0;
7589}
Greg Claytond8d4a572015-08-11 21:38:15 +00007590
7591// Get the index of the child of "clang_type" whose name matches. This function
7592// doesn't descend into the children, but only looks one level deep and name
7593// matches can include base class names.
7594
7595uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007596ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7597 const char *name,
7598 bool omit_empty_base_classes) {
7599 if (type && name && name[0]) {
7600 clang::QualType qual_type(GetCanonicalQualType(type));
Enrico Granata36f51e42015-12-18 22:41:25 +00007601
Kate Stoneb9c1b512016-09-06 20:57:50 +00007602 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7603
7604 switch (type_class) {
7605 case clang::Type::Record:
7606 if (GetCompleteType(type)) {
7607 const clang::RecordType *record_type =
7608 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7609 const clang::RecordDecl *record_decl = record_type->getDecl();
7610
7611 assert(record_decl);
7612 uint32_t child_idx = 0;
7613
7614 const clang::CXXRecordDecl *cxx_record_decl =
7615 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7616
7617 if (cxx_record_decl) {
7618 clang::CXXRecordDecl::base_class_const_iterator base_class,
7619 base_class_end;
7620 for (base_class = cxx_record_decl->bases_begin(),
7621 base_class_end = cxx_record_decl->bases_end();
7622 base_class != base_class_end; ++base_class) {
7623 // Skip empty base classes
7624 clang::CXXRecordDecl *base_class_decl =
7625 llvm::cast<clang::CXXRecordDecl>(
7626 base_class->getType()
7627 ->getAs<clang::RecordType>()
7628 ->getDecl());
7629 if (omit_empty_base_classes &&
Jonas Devliegherea6682a42018-12-15 00:15:33 +00007630 !ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00007631 continue;
7632
Alex Langfordbddab072019-08-13 19:40:36 +00007633 CompilerType base_class_clang_type(
7634 this, base_class->getType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007635 std::string base_class_type_name(
7636 base_class_clang_type.GetTypeName().AsCString(""));
Jonas Devlieghere8d20cfd2018-12-21 22:46:10 +00007637 if (base_class_type_name == name)
Kate Stoneb9c1b512016-09-06 20:57:50 +00007638 return child_idx;
7639 ++child_idx;
7640 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007641 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007642
Kate Stoneb9c1b512016-09-06 20:57:50 +00007643 // Try and find a field that matches NAME
7644 clang::RecordDecl::field_iterator field, field_end;
7645 llvm::StringRef name_sref(name);
7646 for (field = record_decl->field_begin(),
7647 field_end = record_decl->field_end();
7648 field != field_end; ++field, ++child_idx) {
7649 if (field->getName().equals(name_sref))
7650 return child_idx;
7651 }
7652 }
7653 break;
7654
7655 case clang::Type::ObjCObject:
7656 case clang::Type::ObjCInterface:
7657 if (GetCompleteType(type)) {
7658 llvm::StringRef name_sref(name);
7659 const clang::ObjCObjectType *objc_class_type =
7660 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7661 assert(objc_class_type);
7662 if (objc_class_type) {
7663 uint32_t child_idx = 0;
7664 clang::ObjCInterfaceDecl *class_interface_decl =
7665 objc_class_type->getInterface();
7666
7667 if (class_interface_decl) {
7668 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7669 ivar_end = class_interface_decl->ivar_end();
7670 clang::ObjCInterfaceDecl *superclass_interface_decl =
7671 class_interface_decl->getSuperClass();
7672
7673 for (ivar_pos = class_interface_decl->ivar_begin();
7674 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7675 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7676
7677 if (ivar_decl->getName().equals(name_sref)) {
7678 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7679 (omit_empty_base_classes &&
7680 ObjCDeclHasIVars(superclass_interface_decl, true)))
7681 ++child_idx;
7682
7683 return child_idx;
7684 }
7685 }
7686
7687 if (superclass_interface_decl) {
7688 if (superclass_interface_decl->getName().equals(name_sref))
7689 return 0;
7690 }
7691 }
7692 }
7693 }
7694 break;
7695
7696 case clang::Type::ObjCObjectPointer: {
7697 CompilerType pointee_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00007698 this, llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7699 ->getPointeeType()
7700 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007701 return pointee_clang_type.GetIndexOfChildWithName(
7702 name, omit_empty_base_classes);
7703 } break;
7704
7705 case clang::Type::ConstantArray: {
7706 // const clang::ConstantArrayType *array =
7707 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7708 // const uint64_t element_count =
7709 // array->getSize().getLimitedValue();
7710 //
7711 // if (idx < element_count)
7712 // {
7713 // std::pair<uint64_t, unsigned> field_type_info =
7714 // ast->getTypeInfo(array->getElementType());
7715 //
7716 // char element_name[32];
7717 // ::snprintf (element_name, sizeof (element_name),
7718 // "%s[%u]", parent_name ? parent_name : "", idx);
7719 //
7720 // child_name.assign(element_name);
7721 // assert(field_type_info.first % 8 == 0);
7722 // child_byte_size = field_type_info.first / 8;
7723 // child_byte_offset = idx * child_byte_size;
7724 // return array->getElementType().getAsOpaquePtr();
7725 // }
7726 } break;
7727
7728 // case clang::Type::MemberPointerType:
7729 // {
7730 // MemberPointerType *mem_ptr_type =
7731 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7732 // clang::QualType pointee_type =
7733 // mem_ptr_type->getPointeeType();
7734 //
7735 // if (ClangASTContext::IsAggregateType
7736 // (pointee_type.getAsOpaquePtr()))
7737 // {
7738 // return GetIndexOfChildWithName (ast,
7739 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7740 // name);
7741 // }
7742 // }
7743 // break;
7744 //
7745 case clang::Type::LValueReference:
7746 case clang::Type::RValueReference: {
7747 const clang::ReferenceType *reference_type =
7748 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
Alex Langfordbddab072019-08-13 19:40:36 +00007749 CompilerType pointee_type(
7750 this, reference_type->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007751
7752 if (pointee_type.IsAggregateType()) {
7753 return pointee_type.GetIndexOfChildWithName(name,
7754 omit_empty_base_classes);
7755 }
7756 } break;
7757
7758 case clang::Type::Pointer: {
7759 const clang::PointerType *pointer_type =
7760 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
Alex Langfordbddab072019-08-13 19:40:36 +00007761 CompilerType pointee_type(
7762 this, pointer_type->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007763
7764 if (pointee_type.IsAggregateType()) {
7765 return pointee_type.GetIndexOfChildWithName(name,
7766 omit_empty_base_classes);
7767 } else {
7768 // if (parent_name)
7769 // {
7770 // child_name.assign(1, '*');
7771 // child_name += parent_name;
7772 // }
7773 //
7774 // // We have a pointer to an simple type
7775 // if (idx == 0)
7776 // {
7777 // std::pair<uint64_t, unsigned> clang_type_info
7778 // = ast->getTypeInfo(pointee_type);
7779 // assert(clang_type_info.first % 8 == 0);
7780 // child_byte_size = clang_type_info.first / 8;
7781 // child_byte_offset = 0;
7782 // return pointee_type.getAsOpaquePtr();
7783 // }
7784 }
7785 } break;
7786
7787 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00007788 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
7789 ->getDeducedType()
7790 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007791 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7792
7793 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00007794 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
7795 ->getNamedType()
7796 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007797 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7798
7799 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00007800 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
7801 ->desugar()
7802 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007803 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7804
7805 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00007806 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
7807 ->getDecl()
7808 ->getUnderlyingType()
7809 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007810 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7811
7812 default:
7813 break;
7814 }
7815 }
7816 return UINT32_MAX;
7817}
Greg Claytond8d4a572015-08-11 21:38:15 +00007818
7819size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007820ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7821 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007822 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007823
Kate Stoneb9c1b512016-09-06 20:57:50 +00007824 clang::QualType qual_type(GetCanonicalQualType(type));
7825 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7826 switch (type_class) {
7827 case clang::Type::Record:
7828 if (GetCompleteType(type)) {
7829 const clang::CXXRecordDecl *cxx_record_decl =
7830 qual_type->getAsCXXRecordDecl();
7831 if (cxx_record_decl) {
7832 const clang::ClassTemplateSpecializationDecl *template_decl =
7833 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7834 cxx_record_decl);
7835 if (template_decl)
7836 return template_decl->getTemplateArgs().size();
7837 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007838 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007839 break;
7840
7841 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00007842 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
7843 ->getDecl()
7844 ->getUnderlyingType()
7845 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007846 .GetNumTemplateArguments();
7847
7848 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00007849 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
7850 ->getDeducedType()
7851 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007852 .GetNumTemplateArguments();
7853
7854 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00007855 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
7856 ->getNamedType()
7857 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007858 .GetNumTemplateArguments();
7859
7860 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00007861 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
7862 ->desugar()
7863 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007864 .GetNumTemplateArguments();
7865
7866 default:
7867 break;
7868 }
7869
7870 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007871}
7872
Pavel Labath769b21e2017-11-13 14:26:21 +00007873const clang::ClassTemplateSpecializationDecl *
7874ClangASTContext::GetAsTemplateSpecialization(
7875 lldb::opaque_compiler_type_t type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00007876 if (!type)
Pavel Labath769b21e2017-11-13 14:26:21 +00007877 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007878
7879 clang::QualType qual_type(GetCanonicalQualType(type));
7880 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7881 switch (type_class) {
Pavel Labath769b21e2017-11-13 14:26:21 +00007882 case clang::Type::Record: {
7883 if (! GetCompleteType(type))
7884 return nullptr;
7885 const clang::CXXRecordDecl *cxx_record_decl =
7886 qual_type->getAsCXXRecordDecl();
7887 if (!cxx_record_decl)
7888 return nullptr;
7889 return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7890 cxx_record_decl);
7891 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007892
7893 case clang::Type::Typedef:
Pavel Labath769b21e2017-11-13 14:26:21 +00007894 return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type)
7895 ->getDecl()
7896 ->getUnderlyingType()
7897 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007898
7899 case clang::Type::Auto:
Pavel Labath769b21e2017-11-13 14:26:21 +00007900 return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type)
7901 ->getDeducedType()
7902 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007903
7904 case clang::Type::Elaborated:
Pavel Labath769b21e2017-11-13 14:26:21 +00007905 return GetAsTemplateSpecialization(
7906 llvm::cast<clang::ElaboratedType>(qual_type)
7907 ->getNamedType()
7908 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007909
7910 case clang::Type::Paren:
Pavel Labath769b21e2017-11-13 14:26:21 +00007911 return GetAsTemplateSpecialization(
7912 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007913
7914 default:
Pavel Labath769b21e2017-11-13 14:26:21 +00007915 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007916 }
Pavel Labath769b21e2017-11-13 14:26:21 +00007917}
7918
7919lldb::TemplateArgumentKind
7920ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
7921 size_t arg_idx) {
7922 const clang::ClassTemplateSpecializationDecl *template_decl =
7923 GetAsTemplateSpecialization(type);
7924 if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size())
7925 return eTemplateArgumentKindNull;
7926
7927 switch (template_decl->getTemplateArgs()[arg_idx].getKind()) {
7928 case clang::TemplateArgument::Null:
7929 return eTemplateArgumentKindNull;
7930
7931 case clang::TemplateArgument::NullPtr:
7932 return eTemplateArgumentKindNullPtr;
7933
7934 case clang::TemplateArgument::Type:
7935 return eTemplateArgumentKindType;
7936
7937 case clang::TemplateArgument::Declaration:
7938 return eTemplateArgumentKindDeclaration;
7939
7940 case clang::TemplateArgument::Integral:
7941 return eTemplateArgumentKindIntegral;
7942
7943 case clang::TemplateArgument::Template:
7944 return eTemplateArgumentKindTemplate;
7945
7946 case clang::TemplateArgument::TemplateExpansion:
7947 return eTemplateArgumentKindTemplateExpansion;
7948
7949 case clang::TemplateArgument::Expression:
7950 return eTemplateArgumentKindExpression;
7951
7952 case clang::TemplateArgument::Pack:
7953 return eTemplateArgumentKindPack;
7954 }
7955 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
7956}
7957
7958CompilerType
7959ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
7960 size_t idx) {
7961 const clang::ClassTemplateSpecializationDecl *template_decl =
7962 GetAsTemplateSpecialization(type);
7963 if (!template_decl || idx >= template_decl->getTemplateArgs().size())
7964 return CompilerType();
7965
7966 const clang::TemplateArgument &template_arg =
7967 template_decl->getTemplateArgs()[idx];
7968 if (template_arg.getKind() != clang::TemplateArgument::Type)
7969 return CompilerType();
7970
Alex Langfordbddab072019-08-13 19:40:36 +00007971 return CompilerType(this, template_arg.getAsType().getAsOpaquePtr());
Pavel Labath769b21e2017-11-13 14:26:21 +00007972}
7973
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00007974Optional<CompilerType::IntegralTemplateArgument>
Pavel Labath769b21e2017-11-13 14:26:21 +00007975ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
7976 size_t idx) {
7977 const clang::ClassTemplateSpecializationDecl *template_decl =
7978 GetAsTemplateSpecialization(type);
7979 if (! template_decl || idx >= template_decl->getTemplateArgs().size())
Pavel Labathf59056f2017-11-30 10:16:54 +00007980 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007981
7982 const clang::TemplateArgument &template_arg =
7983 template_decl->getTemplateArgs()[idx];
7984 if (template_arg.getKind() != clang::TemplateArgument::Integral)
Pavel Labathf59056f2017-11-30 10:16:54 +00007985 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007986
Alex Langfordbddab072019-08-13 19:40:36 +00007987 return {
7988 {template_arg.getAsIntegral(),
7989 CompilerType(this, template_arg.getIntegralType().getAsOpaquePtr())}};
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007990}
7991
Kate Stoneb9c1b512016-09-06 20:57:50 +00007992CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7993 if (type)
7994 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7995 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007996}
7997
Kate Stoneb9c1b512016-09-06 20:57:50 +00007998clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7999 const clang::EnumType *enutype =
8000 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
8001 if (enutype)
8002 return enutype->getDecl();
Konrad Kleine248a1302019-05-23 11:14:47 +00008003 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008004}
8005
8006clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
8007 const clang::RecordType *record_type =
8008 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
8009 if (record_type)
8010 return record_type->getDecl();
8011 return nullptr;
8012}
8013
8014clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
Zachary Turner1639c6b2018-12-17 16:15:13 +00008015 return ClangUtil::GetAsTagDecl(type);
Greg Claytone6b36cd2015-12-08 01:02:08 +00008016}
8017
Aleksandr Urakov709426b2018-09-10 08:08:43 +00008018clang::TypedefNameDecl *
8019ClangASTContext::GetAsTypedefDecl(const CompilerType &type) {
8020 const clang::TypedefType *typedef_type =
8021 llvm::dyn_cast<clang::TypedefType>(ClangUtil::GetQualType(type));
8022 if (typedef_type)
8023 return typedef_type->getDecl();
8024 return nullptr;
8025}
8026
Greg Claytond8d4a572015-08-11 21:38:15 +00008027clang::CXXRecordDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00008028ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
8029 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
Greg Claytond8d4a572015-08-11 21:38:15 +00008030}
8031
8032clang::ObjCInterfaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00008033ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
8034 const clang::ObjCObjectType *objc_class_type =
8035 llvm::dyn_cast<clang::ObjCObjectType>(
8036 ClangUtil::GetCanonicalQualType(type));
8037 if (objc_class_type)
8038 return objc_class_type->getInterface();
8039 return nullptr;
8040}
8041
8042clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008043 const CompilerType &type, llvm::StringRef name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008044 const CompilerType &field_clang_type, AccessType access,
8045 uint32_t bitfield_bit_size) {
8046 if (!type.IsValid() || !field_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008047 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008048 ClangASTContext *ast =
8049 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8050 if (!ast)
8051 return nullptr;
8052 clang::ASTContext *clang_ast = ast->getASTContext();
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008053 clang::IdentifierInfo *ident = nullptr;
8054 if (!name.empty())
8055 ident = &clang_ast->Idents.get(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008056
8057 clang::FieldDecl *field = nullptr;
8058
8059 clang::Expr *bit_width = nullptr;
8060 if (bitfield_bit_size != 0) {
8061 llvm::APInt bitfield_bit_size_apint(
8062 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
8063 bit_width = new (*clang_ast)
8064 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
8065 clang_ast->IntTy, clang::SourceLocation());
8066 }
8067
8068 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
8069 if (record_decl) {
8070 field = clang::FieldDecl::Create(
8071 *clang_ast, record_decl, clang::SourceLocation(),
8072 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008073 ident, // Identifier
8074 ClangUtil::GetQualType(field_clang_type), // Field type
8075 nullptr, // TInfo *
8076 bit_width, // BitWidth
8077 false, // Mutable
8078 clang::ICIS_NoInit); // HasInit
Kate Stoneb9c1b512016-09-06 20:57:50 +00008079
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008080 if (name.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +00008081 // Determine whether this field corresponds to an anonymous struct or
8082 // union.
Kate Stoneb9c1b512016-09-06 20:57:50 +00008083 if (const clang::TagType *TagT =
8084 field->getType()->getAs<clang::TagType>()) {
8085 if (clang::RecordDecl *Rec =
8086 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
8087 if (!Rec->getDeclName()) {
8088 Rec->setAnonymousStructOrUnion(true);
8089 field->setImplicit();
8090 }
8091 }
8092 }
8093
8094 if (field) {
8095 field->setAccess(
8096 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
8097
8098 record_decl->addDecl(field);
8099
8100#ifdef LLDB_CONFIGURATION_DEBUG
8101 VerifyDecl(field);
8102#endif
8103 }
8104 } else {
8105 clang::ObjCInterfaceDecl *class_interface_decl =
8106 ast->GetAsObjCInterfaceDecl(type);
8107
8108 if (class_interface_decl) {
8109 const bool is_synthesized = false;
8110
8111 field_clang_type.GetCompleteType();
8112
8113 field = clang::ObjCIvarDecl::Create(
8114 *clang_ast, class_interface_decl, clang::SourceLocation(),
8115 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008116 ident, // Identifier
8117 ClangUtil::GetQualType(field_clang_type), // Field type
8118 nullptr, // TypeSourceInfo *
Kate Stoneb9c1b512016-09-06 20:57:50 +00008119 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
8120 is_synthesized);
8121
8122 if (field) {
8123 class_interface_decl->addDecl(field);
8124
8125#ifdef LLDB_CONFIGURATION_DEBUG
8126 VerifyDecl(field);
8127#endif
8128 }
8129 }
8130 }
8131 return field;
Greg Claytond8d4a572015-08-11 21:38:15 +00008132}
8133
Kate Stoneb9c1b512016-09-06 20:57:50 +00008134void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
8135 if (!type)
8136 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008137
Kate Stoneb9c1b512016-09-06 20:57:50 +00008138 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8139 if (!ast)
8140 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008141
Kate Stoneb9c1b512016-09-06 20:57:50 +00008142 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008143
Kate Stoneb9c1b512016-09-06 20:57:50 +00008144 if (!record_decl)
8145 return;
8146
8147 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
8148
8149 IndirectFieldVector indirect_fields;
8150 clang::RecordDecl::field_iterator field_pos;
8151 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
8152 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
8153 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
8154 last_field_pos = field_pos++) {
8155 if (field_pos->isAnonymousStructOrUnion()) {
8156 clang::QualType field_qual_type = field_pos->getType();
8157
8158 const clang::RecordType *field_record_type =
8159 field_qual_type->getAs<clang::RecordType>();
8160
8161 if (!field_record_type)
8162 continue;
8163
8164 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
8165
8166 if (!field_record_decl)
8167 continue;
8168
8169 for (clang::RecordDecl::decl_iterator
8170 di = field_record_decl->decls_begin(),
8171 de = field_record_decl->decls_end();
8172 di != de; ++di) {
8173 if (clang::FieldDecl *nested_field_decl =
8174 llvm::dyn_cast<clang::FieldDecl>(*di)) {
8175 clang::NamedDecl **chain =
8176 new (*ast->getASTContext()) clang::NamedDecl *[2];
8177 chain[0] = *field_pos;
8178 chain[1] = nested_field_decl;
8179 clang::IndirectFieldDecl *indirect_field =
8180 clang::IndirectFieldDecl::Create(
8181 *ast->getASTContext(), record_decl, clang::SourceLocation(),
8182 nested_field_decl->getIdentifier(),
8183 nested_field_decl->getType(), {chain, 2});
8184
8185 indirect_field->setImplicit();
8186
8187 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
8188 field_pos->getAccess(), nested_field_decl->getAccess()));
8189
8190 indirect_fields.push_back(indirect_field);
8191 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
8192 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
8193 size_t nested_chain_size =
8194 nested_indirect_field_decl->getChainingSize();
8195 clang::NamedDecl **chain = new (*ast->getASTContext())
8196 clang::NamedDecl *[nested_chain_size + 1];
8197 chain[0] = *field_pos;
8198
8199 int chain_index = 1;
8200 for (clang::IndirectFieldDecl::chain_iterator
8201 nci = nested_indirect_field_decl->chain_begin(),
8202 nce = nested_indirect_field_decl->chain_end();
8203 nci < nce; ++nci) {
8204 chain[chain_index] = *nci;
8205 chain_index++;
8206 }
8207
8208 clang::IndirectFieldDecl *indirect_field =
8209 clang::IndirectFieldDecl::Create(
8210 *ast->getASTContext(), record_decl, clang::SourceLocation(),
8211 nested_indirect_field_decl->getIdentifier(),
8212 nested_indirect_field_decl->getType(),
8213 {chain, nested_chain_size + 1});
8214
8215 indirect_field->setImplicit();
8216
8217 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
8218 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
8219
8220 indirect_fields.push_back(indirect_field);
Greg Claytond8d4a572015-08-11 21:38:15 +00008221 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008222 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008223 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008224 }
8225
Adrian Prantl05097242018-04-30 16:49:04 +00008226 // Check the last field to see if it has an incomplete array type as its last
8227 // member and if it does, the tell the record decl about it
Kate Stoneb9c1b512016-09-06 20:57:50 +00008228 if (last_field_pos != field_end_pos) {
8229 if (last_field_pos->getType()->isIncompleteArrayType())
8230 record_decl->hasFlexibleArrayMember();
8231 }
8232
8233 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
8234 ife = indirect_fields.end();
8235 ifi < ife; ++ifi) {
8236 record_decl->addDecl(*ifi);
8237 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008238}
8239
Kate Stoneb9c1b512016-09-06 20:57:50 +00008240void ClangASTContext::SetIsPacked(const CompilerType &type) {
8241 if (type) {
8242 ClangASTContext *ast =
8243 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8244 if (ast) {
8245 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
8246
8247 if (!record_decl)
Greg Claytonf73034f2015-09-08 18:15:05 +00008248 return;
8249
Kate Stoneb9c1b512016-09-06 20:57:50 +00008250 record_decl->addAttr(
8251 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
Greg Claytond8d4a572015-08-11 21:38:15 +00008252 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008253 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008254}
8255
Kate Stoneb9c1b512016-09-06 20:57:50 +00008256clang::VarDecl *ClangASTContext::AddVariableToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008257 const CompilerType &type, llvm::StringRef name,
8258 const CompilerType &var_type, AccessType access) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00008259 if (!type.IsValid() || !var_type.IsValid())
8260 return nullptr;
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008261
Kate Stoneb9c1b512016-09-06 20:57:50 +00008262 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8263 if (!ast)
8264 return nullptr;
8265
8266 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008267 if (!record_decl)
8268 return nullptr;
8269
8270 clang::VarDecl *var_decl = nullptr;
8271 clang::IdentifierInfo *ident = nullptr;
8272 if (!name.empty())
8273 ident = &ast->getASTContext()->Idents.get(name);
8274
8275 var_decl = clang::VarDecl::Create(
8276 *ast->getASTContext(), // ASTContext &
8277 record_decl, // DeclContext *
8278 clang::SourceLocation(), // clang::SourceLocation StartLoc
8279 clang::SourceLocation(), // clang::SourceLocation IdLoc
8280 ident, // clang::IdentifierInfo *
8281 ClangUtil::GetQualType(var_type), // Variable clang::QualType
8282 nullptr, // TypeSourceInfo *
8283 clang::SC_Static); // StorageClass
8284 if (!var_decl)
8285 return nullptr;
8286
8287 var_decl->setAccess(
8288 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
8289 record_decl->addDecl(var_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008290
Greg Claytond8d4a572015-08-11 21:38:15 +00008291#ifdef LLDB_CONFIGURATION_DEBUG
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008292 VerifyDecl(var_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008293#endif
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008294
Kate Stoneb9c1b512016-09-06 20:57:50 +00008295 return var_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008296}
8297
Kate Stoneb9c1b512016-09-06 20:57:50 +00008298clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
Davide Italiano675767a2018-03-27 19:40:50 +00008299 lldb::opaque_compiler_type_t type, const char *name, const char *mangled_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008300 const CompilerType &method_clang_type, lldb::AccessType access,
8301 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
8302 bool is_attr_used, bool is_artificial) {
8303 if (!type || !method_clang_type.IsValid() || name == nullptr ||
8304 name[0] == '\0')
8305 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008306
Kate Stoneb9c1b512016-09-06 20:57:50 +00008307 clang::QualType record_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008308
Kate Stoneb9c1b512016-09-06 20:57:50 +00008309 clang::CXXRecordDecl *cxx_record_decl =
8310 record_qual_type->getAsCXXRecordDecl();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008311
Kate Stoneb9c1b512016-09-06 20:57:50 +00008312 if (cxx_record_decl == nullptr)
8313 return nullptr;
8314
8315 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8316
8317 clang::CXXMethodDecl *cxx_method_decl = nullptr;
8318
8319 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
8320
8321 const clang::FunctionType *function_type =
8322 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
8323
8324 if (function_type == nullptr)
8325 return nullptr;
8326
8327 const clang::FunctionProtoType *method_function_prototype(
8328 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
8329
8330 if (!method_function_prototype)
8331 return nullptr;
8332
8333 unsigned int num_params = method_function_prototype->getNumParams();
8334
8335 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
8336 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
8337
8338 if (is_artificial)
8339 return nullptr; // skip everything artificial
8340
Richard Smith36851a62019-05-09 04:40:57 +00008341 const clang::ExplicitSpecifier explicit_spec(
8342 nullptr /*expr*/, is_explicit
8343 ? clang::ExplicitSpecKind::ResolvedTrue
8344 : clang::ExplicitSpecKind::ResolvedFalse);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008345 if (name[0] == '~') {
8346 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
8347 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8348 clang::DeclarationNameInfo(
8349 getASTContext()->DeclarationNames.getCXXDestructorName(
8350 getASTContext()->getCanonicalType(record_qual_type)),
8351 clang::SourceLocation()),
8352 method_qual_type, nullptr, is_inline, is_artificial);
8353 cxx_method_decl = cxx_dtor_decl;
8354 } else if (decl_name == cxx_record_decl->getDeclName()) {
8355 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
8356 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8357 clang::DeclarationNameInfo(
8358 getASTContext()->DeclarationNames.getCXXConstructorName(
8359 getASTContext()->getCanonicalType(record_qual_type)),
8360 clang::SourceLocation()),
8361 method_qual_type,
8362 nullptr, // TypeSourceInfo *
Gauthier Harnisch796ed032019-06-14 08:56:20 +00008363 explicit_spec, is_inline, is_artificial, CSK_unspecified);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008364 cxx_method_decl = cxx_ctor_decl;
8365 } else {
8366 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
8367 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
8368
8369 if (IsOperator(name, op_kind)) {
8370 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
Adrian Prantl05097242018-04-30 16:49:04 +00008371 // Check the number of operator parameters. Sometimes we have seen bad
8372 // DWARF that doesn't correctly describe operators and if we try to
8373 // create a method and add it to the class, clang will assert and
8374 // crash, so we need to make sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00008375 const bool is_method = true;
8376 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
8377 is_method, op_kind, num_params))
8378 return nullptr;
8379 cxx_method_decl = clang::CXXMethodDecl::Create(
8380 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8381 clang::DeclarationNameInfo(
8382 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
8383 clang::SourceLocation()),
8384 method_qual_type,
8385 nullptr, // TypeSourceInfo *
Gauthier Harnisch796ed032019-06-14 08:56:20 +00008386 SC, is_inline, CSK_unspecified, clang::SourceLocation());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008387 } else if (num_params == 0) {
8388 // Conversion operators don't take params...
8389 cxx_method_decl = clang::CXXConversionDecl::Create(
8390 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8391 clang::DeclarationNameInfo(
8392 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
8393 getASTContext()->getCanonicalType(
8394 function_type->getReturnType())),
8395 clang::SourceLocation()),
8396 method_qual_type,
8397 nullptr, // TypeSourceInfo *
Gauthier Harnisch796ed032019-06-14 08:56:20 +00008398 is_inline, explicit_spec, CSK_unspecified,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008399 clang::SourceLocation());
8400 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008401 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008402
8403 if (cxx_method_decl == nullptr) {
8404 cxx_method_decl = clang::CXXMethodDecl::Create(
8405 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8406 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8407 method_qual_type,
8408 nullptr, // TypeSourceInfo *
Gauthier Harnisch796ed032019-06-14 08:56:20 +00008409 SC, is_inline, CSK_unspecified, clang::SourceLocation());
Greg Claytond8d4a572015-08-11 21:38:15 +00008410 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008411 }
8412
8413 clang::AccessSpecifier access_specifier =
8414 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8415
8416 cxx_method_decl->setAccess(access_specifier);
8417 cxx_method_decl->setVirtualAsWritten(is_virtual);
8418
8419 if (is_attr_used)
8420 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8421
Konrad Kleine248a1302019-05-23 11:14:47 +00008422 if (mangled_name != nullptr) {
Davide Italiano675767a2018-03-27 19:40:50 +00008423 cxx_method_decl->addAttr(
8424 clang::AsmLabelAttr::CreateImplicit(*getASTContext(), mangled_name));
8425 }
8426
Kate Stoneb9c1b512016-09-06 20:57:50 +00008427 // Populate the method decl with parameter decls
8428
8429 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8430
8431 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8432 params.push_back(clang::ParmVarDecl::Create(
8433 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8434 clang::SourceLocation(),
8435 nullptr, // anonymous
8436 method_function_prototype->getParamType(param_index), nullptr,
8437 clang::SC_None, nullptr));
8438 }
8439
8440 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8441
8442 cxx_record_decl->addDecl(cxx_method_decl);
8443
8444 // Sometimes the debug info will mention a constructor (default/copy/move),
8445 // destructor, or assignment operator (copy/move) but there won't be any
8446 // version of this in the code. So we check if the function was artificially
8447 // generated and if it is trivial and this lets the compiler/backend know
8448 // that it can inline the IR for these when it needs to and we can avoid a
8449 // "missing function" error when running expressions.
8450
8451 if (is_artificial) {
8452 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8453 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8454 (cxx_ctor_decl->isCopyConstructor() &&
8455 cxx_record_decl->hasTrivialCopyConstructor()) ||
8456 (cxx_ctor_decl->isMoveConstructor() &&
8457 cxx_record_decl->hasTrivialMoveConstructor()))) {
8458 cxx_ctor_decl->setDefaulted();
8459 cxx_ctor_decl->setTrivial(true);
8460 } else if (cxx_dtor_decl) {
8461 if (cxx_record_decl->hasTrivialDestructor()) {
8462 cxx_dtor_decl->setDefaulted();
8463 cxx_dtor_decl->setTrivial(true);
8464 }
8465 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8466 cxx_record_decl->hasTrivialCopyAssignment()) ||
8467 (cxx_method_decl->isMoveAssignmentOperator() &&
8468 cxx_record_decl->hasTrivialMoveAssignment())) {
8469 cxx_method_decl->setDefaulted();
8470 cxx_method_decl->setTrivial(true);
Greg Claytond8d4a572015-08-11 21:38:15 +00008471 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008472 }
8473
Greg Claytond8d4a572015-08-11 21:38:15 +00008474#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008475 VerifyDecl(cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008476#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008477
Kate Stoneb9c1b512016-09-06 20:57:50 +00008478 return cxx_method_decl;
8479}
Greg Claytond8d4a572015-08-11 21:38:15 +00008480
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00008481void ClangASTContext::AddMethodOverridesForCXXRecordType(
8482 lldb::opaque_compiler_type_t type) {
8483 if (auto *record = GetAsCXXRecordDecl(type))
8484 for (auto *method : record->methods())
8485 addOverridesForMethod(method);
8486}
8487
Greg Claytond8d4a572015-08-11 21:38:15 +00008488#pragma mark C++ Base Classes
8489
Zachary Turner970f38e2018-10-25 20:44:56 +00008490std::unique_ptr<clang::CXXBaseSpecifier>
Kate Stoneb9c1b512016-09-06 20:57:50 +00008491ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8492 AccessType access, bool is_virtual,
8493 bool base_of_class) {
Zachary Turner970f38e2018-10-25 20:44:56 +00008494 if (!type)
8495 return nullptr;
8496
Jonas Devliegherea8f3ae72019-08-14 22:19:23 +00008497 return std::make_unique<clang::CXXBaseSpecifier>(
Zachary Turner970f38e2018-10-25 20:44:56 +00008498 clang::SourceRange(), is_virtual, base_of_class,
8499 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8500 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8501 clang::SourceLocation());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008502}
8503
Zachary Turner970f38e2018-10-25 20:44:56 +00008504bool ClangASTContext::TransferBaseClasses(
Kate Stoneb9c1b512016-09-06 20:57:50 +00008505 lldb::opaque_compiler_type_t type,
Zachary Turner970f38e2018-10-25 20:44:56 +00008506 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases) {
8507 if (!type)
8508 return false;
8509 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8510 if (!cxx_record_decl)
8511 return false;
8512 std::vector<clang::CXXBaseSpecifier *> raw_bases;
8513 raw_bases.reserve(bases.size());
8514
8515 // Clang will make a copy of them, so it's ok that we pass pointers that we're
8516 // about to destroy.
8517 for (auto &b : bases)
8518 raw_bases.push_back(b.get());
8519 cxx_record_decl->setBases(raw_bases.data(), raw_bases.size());
8520 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008521}
8522
8523bool ClangASTContext::SetObjCSuperClass(
8524 const CompilerType &type, const CompilerType &superclass_clang_type) {
8525 ClangASTContext *ast =
8526 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8527 if (!ast)
8528 return false;
8529 clang::ASTContext *clang_ast = ast->getASTContext();
8530
8531 if (type && superclass_clang_type.IsValid() &&
8532 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8533 clang::ObjCInterfaceDecl *class_interface_decl =
8534 GetAsObjCInterfaceDecl(type);
8535 clang::ObjCInterfaceDecl *super_interface_decl =
8536 GetAsObjCInterfaceDecl(superclass_clang_type);
8537 if (class_interface_decl && super_interface_decl) {
8538 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8539 clang_ast->getObjCInterfaceType(super_interface_decl)));
8540 return true;
8541 }
8542 }
8543 return false;
8544}
8545
8546bool ClangASTContext::AddObjCClassProperty(
8547 const CompilerType &type, const char *property_name,
8548 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8549 const char *property_setter_name, const char *property_getter_name,
8550 uint32_t property_attributes, ClangASTMetadata *metadata) {
8551 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8552 property_name[0] == '\0')
8553 return false;
8554 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8555 if (!ast)
8556 return false;
8557 clang::ASTContext *clang_ast = ast->getASTContext();
8558
8559 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8560
8561 if (class_interface_decl) {
8562 CompilerType property_clang_type_to_access;
8563
8564 if (property_clang_type.IsValid())
8565 property_clang_type_to_access = property_clang_type;
8566 else if (ivar_decl)
8567 property_clang_type_to_access =
Alex Langfordbddab072019-08-13 19:40:36 +00008568 CompilerType(ast, ivar_decl->getType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008569
8570 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8571 clang::TypeSourceInfo *prop_type_source;
8572 if (ivar_decl)
8573 prop_type_source =
8574 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8575 else
8576 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8577 ClangUtil::GetQualType(property_clang_type));
8578
8579 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8580 *clang_ast, class_interface_decl,
8581 clang::SourceLocation(), // Source Location
8582 &clang_ast->Idents.get(property_name),
8583 clang::SourceLocation(), // Source Location for AT
8584 clang::SourceLocation(), // Source location for (
8585 ivar_decl ? ivar_decl->getType()
8586 : ClangUtil::GetQualType(property_clang_type),
8587 prop_type_source);
8588
8589 if (property_decl) {
8590 if (metadata)
8591 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8592
8593 class_interface_decl->addDecl(property_decl);
8594
8595 clang::Selector setter_sel, getter_sel;
8596
8597 if (property_setter_name != nullptr) {
8598 std::string property_setter_no_colon(
8599 property_setter_name, strlen(property_setter_name) - 1);
8600 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008601 &clang_ast->Idents.get(property_setter_no_colon);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008602 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8603 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8604 std::string setter_sel_string("set");
8605 setter_sel_string.push_back(::toupper(property_name[0]));
8606 setter_sel_string.append(&property_name[1]);
8607 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008608 &clang_ast->Idents.get(setter_sel_string);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008609 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8610 }
8611 property_decl->setSetterName(setter_sel);
8612 property_decl->setPropertyAttributes(
8613 clang::ObjCPropertyDecl::OBJC_PR_setter);
8614
8615 if (property_getter_name != nullptr) {
8616 clang::IdentifierInfo *getter_ident =
8617 &clang_ast->Idents.get(property_getter_name);
8618 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8619 } else {
8620 clang::IdentifierInfo *getter_ident =
8621 &clang_ast->Idents.get(property_name);
8622 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8623 }
8624 property_decl->setGetterName(getter_sel);
8625 property_decl->setPropertyAttributes(
8626 clang::ObjCPropertyDecl::OBJC_PR_getter);
8627
8628 if (ivar_decl)
8629 property_decl->setPropertyIvarDecl(ivar_decl);
8630
8631 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8632 property_decl->setPropertyAttributes(
8633 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8634 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8635 property_decl->setPropertyAttributes(
8636 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8637 if (property_attributes & DW_APPLE_PROPERTY_assign)
8638 property_decl->setPropertyAttributes(
8639 clang::ObjCPropertyDecl::OBJC_PR_assign);
8640 if (property_attributes & DW_APPLE_PROPERTY_retain)
8641 property_decl->setPropertyAttributes(
8642 clang::ObjCPropertyDecl::OBJC_PR_retain);
8643 if (property_attributes & DW_APPLE_PROPERTY_copy)
8644 property_decl->setPropertyAttributes(
8645 clang::ObjCPropertyDecl::OBJC_PR_copy);
8646 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8647 property_decl->setPropertyAttributes(
8648 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8649 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8650 property_decl->setPropertyAttributes(
8651 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8652 if (property_attributes &
8653 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8654 property_decl->setPropertyAttributes(
8655 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8656 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8657 property_decl->setPropertyAttributes(
8658 clang::ObjCPropertyDecl::OBJC_PR_class);
8659
8660 const bool isInstance =
8661 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8662
8663 if (!getter_sel.isNull() &&
8664 !(isInstance
8665 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8666 : class_interface_decl->lookupClassMethod(getter_sel))) {
8667 const bool isVariadic = false;
8668 const bool isSynthesized = false;
8669 const bool isImplicitlyDeclared = true;
8670 const bool isDefined = false;
8671 const clang::ObjCMethodDecl::ImplementationControl impControl =
8672 clang::ObjCMethodDecl::None;
8673 const bool HasRelatedResultType = false;
8674
8675 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8676 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8677 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8678 nullptr, class_interface_decl, isInstance, isVariadic,
8679 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8680 HasRelatedResultType);
8681
8682 if (getter && metadata)
8683 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8684
8685 if (getter) {
8686 getter->setMethodParams(*clang_ast,
8687 llvm::ArrayRef<clang::ParmVarDecl *>(),
8688 llvm::ArrayRef<clang::SourceLocation>());
8689
8690 class_interface_decl->addDecl(getter);
8691 }
8692 }
8693
8694 if (!setter_sel.isNull() &&
8695 !(isInstance
8696 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8697 : class_interface_decl->lookupClassMethod(setter_sel))) {
8698 clang::QualType result_type = clang_ast->VoidTy;
8699 const bool isVariadic = false;
8700 const bool isSynthesized = false;
8701 const bool isImplicitlyDeclared = true;
8702 const bool isDefined = false;
8703 const clang::ObjCMethodDecl::ImplementationControl impControl =
8704 clang::ObjCMethodDecl::None;
8705 const bool HasRelatedResultType = false;
8706
8707 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8708 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8709 setter_sel, result_type, nullptr, class_interface_decl,
8710 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8711 isDefined, impControl, HasRelatedResultType);
8712
8713 if (setter && metadata)
8714 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8715
8716 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8717
8718 params.push_back(clang::ParmVarDecl::Create(
8719 *clang_ast, setter, clang::SourceLocation(),
8720 clang::SourceLocation(),
8721 nullptr, // anonymous
8722 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8723 clang::SC_Auto, nullptr));
8724
8725 if (setter) {
8726 setter->setMethodParams(
8727 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8728 llvm::ArrayRef<clang::SourceLocation>());
8729
8730 class_interface_decl->addDecl(setter);
8731 }
8732 }
8733
8734 return true;
8735 }
8736 }
8737 }
8738 return false;
8739}
8740
8741bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8742 bool check_superclass) {
8743 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8744 if (class_interface_decl)
8745 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8746 return false;
8747}
8748
8749clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8750 const CompilerType &type,
8751 const char *name, // the full symbol name as seen in the symbol table
8752 // (lldb::opaque_compiler_type_t type, "-[NString
8753 // stringWithCString:]")
8754 const CompilerType &method_clang_type, lldb::AccessType access,
8755 bool is_artificial, bool is_variadic) {
8756 if (!type || !method_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008757 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008758
Kate Stoneb9c1b512016-09-06 20:57:50 +00008759 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8760
8761 if (class_interface_decl == nullptr)
8762 return nullptr;
8763 ClangASTContext *lldb_ast =
8764 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8765 if (lldb_ast == nullptr)
8766 return nullptr;
8767 clang::ASTContext *ast = lldb_ast->getASTContext();
8768
8769 const char *selector_start = ::strchr(name, ' ');
8770 if (selector_start == nullptr)
8771 return nullptr;
8772
8773 selector_start++;
8774 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8775
8776 size_t len = 0;
8777 const char *start;
8778 // printf ("name = '%s'\n", name);
8779
8780 unsigned num_selectors_with_args = 0;
8781 for (start = selector_start; start && *start != '\0' && *start != ']';
8782 start += len) {
8783 len = ::strcspn(start, ":]");
8784 bool has_arg = (start[len] == ':');
8785 if (has_arg)
8786 ++num_selectors_with_args;
8787 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8788 if (has_arg)
8789 len += 1;
8790 }
8791
8792 if (selector_idents.size() == 0)
8793 return nullptr;
8794
8795 clang::Selector method_selector = ast->Selectors.getSelector(
8796 num_selectors_with_args ? selector_idents.size() : 0,
8797 selector_idents.data());
8798
8799 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8800
8801 // Populate the method decl with parameter decls
8802 const clang::Type *method_type(method_qual_type.getTypePtr());
8803
8804 if (method_type == nullptr)
8805 return nullptr;
8806
8807 const clang::FunctionProtoType *method_function_prototype(
8808 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8809
8810 if (!method_function_prototype)
8811 return nullptr;
8812
8813 bool is_synthesized = false;
8814 bool is_defined = false;
8815 clang::ObjCMethodDecl::ImplementationControl imp_control =
8816 clang::ObjCMethodDecl::None;
8817
8818 const unsigned num_args = method_function_prototype->getNumParams();
8819
8820 if (num_args != num_selectors_with_args)
8821 return nullptr; // some debug information is corrupt. We are not going to
8822 // deal with it.
8823
8824 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8825 *ast,
8826 clang::SourceLocation(), // beginLoc,
8827 clang::SourceLocation(), // endLoc,
8828 method_selector, method_function_prototype->getReturnType(),
8829 nullptr, // TypeSourceInfo *ResultTInfo,
8830 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8831 ClangUtil::GetQualType(type)),
8832 name[0] == '-', is_variadic, is_synthesized,
8833 true, // is_implicitly_declared; we force this to true because we don't
8834 // have source locations
8835 is_defined, imp_control, false /*has_related_result_type*/);
8836
8837 if (objc_method_decl == nullptr)
8838 return nullptr;
8839
8840 if (num_args > 0) {
8841 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8842
8843 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8844 params.push_back(clang::ParmVarDecl::Create(
8845 *ast, objc_method_decl, clang::SourceLocation(),
8846 clang::SourceLocation(),
8847 nullptr, // anonymous
8848 method_function_prototype->getParamType(param_index), nullptr,
8849 clang::SC_Auto, nullptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00008850 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008851
Kate Stoneb9c1b512016-09-06 20:57:50 +00008852 objc_method_decl->setMethodParams(
8853 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8854 llvm::ArrayRef<clang::SourceLocation>());
8855 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008856
Kate Stoneb9c1b512016-09-06 20:57:50 +00008857 class_interface_decl->addDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008858
Greg Claytond8d4a572015-08-11 21:38:15 +00008859#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008860 VerifyDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008861#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00008862
8863 return objc_method_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008864}
8865
Kate Stoneb9c1b512016-09-06 20:57:50 +00008866bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8867 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00008868 return false;
Greg Claytone6b36cd2015-12-08 01:02:08 +00008869
Kate Stoneb9c1b512016-09-06 20:57:50 +00008870 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008871
Kate Stoneb9c1b512016-09-06 20:57:50 +00008872 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8873 switch (type_class) {
8874 case clang::Type::Record: {
8875 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8876 if (cxx_record_decl)
8877 return cxx_record_decl->hasExternalLexicalStorage() ||
8878 cxx_record_decl->hasExternalVisibleStorage();
8879 } break;
Enrico Granata36f51e42015-12-18 22:41:25 +00008880
Kate Stoneb9c1b512016-09-06 20:57:50 +00008881 case clang::Type::Enum: {
8882 clang::EnumDecl *enum_decl =
8883 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8884 if (enum_decl)
8885 return enum_decl->hasExternalLexicalStorage() ||
8886 enum_decl->hasExternalVisibleStorage();
8887 } break;
8888
8889 case clang::Type::ObjCObject:
8890 case clang::Type::ObjCInterface: {
8891 const clang::ObjCObjectType *objc_class_type =
8892 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8893 assert(objc_class_type);
8894 if (objc_class_type) {
8895 clang::ObjCInterfaceDecl *class_interface_decl =
8896 objc_class_type->getInterface();
8897
8898 if (class_interface_decl)
8899 return class_interface_decl->hasExternalLexicalStorage() ||
8900 class_interface_decl->hasExternalVisibleStorage();
Greg Claytond8d4a572015-08-11 21:38:15 +00008901 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008902 } break;
8903
8904 case clang::Type::Typedef:
8905 return GetHasExternalStorage(CompilerType(
8906 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8907 ->getDecl()
8908 ->getUnderlyingType()
8909 .getAsOpaquePtr()));
8910
8911 case clang::Type::Auto:
8912 return GetHasExternalStorage(CompilerType(
8913 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8914 ->getDeducedType()
8915 .getAsOpaquePtr()));
8916
8917 case clang::Type::Elaborated:
8918 return GetHasExternalStorage(CompilerType(
8919 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8920 ->getNamedType()
8921 .getAsOpaquePtr()));
8922
8923 case clang::Type::Paren:
8924 return GetHasExternalStorage(CompilerType(
8925 type.GetTypeSystem(),
8926 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8927
8928 default:
8929 break;
8930 }
8931 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008932}
8933
Kate Stoneb9c1b512016-09-06 20:57:50 +00008934bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8935 bool has_extern) {
8936 if (!type)
8937 return false;
8938
8939 clang::QualType qual_type(GetCanonicalQualType(type));
8940
8941 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8942 switch (type_class) {
8943 case clang::Type::Record: {
8944 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8945 if (cxx_record_decl) {
8946 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8947 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8948 return true;
8949 }
8950 } break;
8951
8952 case clang::Type::Enum: {
8953 clang::EnumDecl *enum_decl =
8954 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8955 if (enum_decl) {
8956 enum_decl->setHasExternalLexicalStorage(has_extern);
8957 enum_decl->setHasExternalVisibleStorage(has_extern);
8958 return true;
8959 }
8960 } break;
8961
8962 case clang::Type::ObjCObject:
8963 case clang::Type::ObjCInterface: {
8964 const clang::ObjCObjectType *objc_class_type =
8965 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8966 assert(objc_class_type);
8967 if (objc_class_type) {
8968 clang::ObjCInterfaceDecl *class_interface_decl =
8969 objc_class_type->getInterface();
8970
8971 if (class_interface_decl) {
8972 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8973 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8974 return true;
8975 }
8976 }
8977 } break;
8978
8979 case clang::Type::Typedef:
8980 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8981 ->getDecl()
8982 ->getUnderlyingType()
8983 .getAsOpaquePtr(),
8984 has_extern);
8985
8986 case clang::Type::Auto:
8987 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8988 ->getDeducedType()
8989 .getAsOpaquePtr(),
8990 has_extern);
8991
8992 case clang::Type::Elaborated:
8993 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8994 ->getNamedType()
8995 .getAsOpaquePtr(),
8996 has_extern);
8997
8998 case clang::Type::Paren:
8999 return SetHasExternalStorage(
9000 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
9001 has_extern);
9002
9003 default:
9004 break;
9005 }
9006 return false;
9007}
Greg Claytond8d4a572015-08-11 21:38:15 +00009008
9009#pragma mark TagDecl
9010
Kate Stoneb9c1b512016-09-06 20:57:50 +00009011bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
9012 clang::QualType qual_type(ClangUtil::GetQualType(type));
9013 if (!qual_type.isNull()) {
9014 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
9015 if (tag_type) {
9016 clang::TagDecl *tag_decl = tag_type->getDecl();
9017 if (tag_decl) {
9018 tag_decl->startDefinition();
9019 return true;
9020 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009021 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009022
9023 const clang::ObjCObjectType *object_type =
9024 qual_type->getAs<clang::ObjCObjectType>();
9025 if (object_type) {
9026 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
9027 if (interface_decl) {
9028 interface_decl->startDefinition();
9029 return true;
9030 }
9031 }
9032 }
9033 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00009034}
9035
Kate Stoneb9c1b512016-09-06 20:57:50 +00009036bool ClangASTContext::CompleteTagDeclarationDefinition(
9037 const CompilerType &type) {
9038 clang::QualType qual_type(ClangUtil::GetQualType(type));
9039 if (!qual_type.isNull()) {
9040 // Make sure we use the same methodology as
Adrian Prantl05097242018-04-30 16:49:04 +00009041 // ClangASTContext::StartTagDeclarationDefinition() as to how we start/end
9042 // the definition. Previously we were calling
Kate Stoneb9c1b512016-09-06 20:57:50 +00009043 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
9044 if (tag_type) {
9045 clang::TagDecl *tag_decl = tag_type->getDecl();
9046 if (tag_decl) {
9047 clang::CXXRecordDecl *cxx_record_decl =
9048 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
9049
9050 if (cxx_record_decl) {
9051 if (!cxx_record_decl->isCompleteDefinition())
9052 cxx_record_decl->completeDefinition();
9053 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
9054 cxx_record_decl->setHasExternalLexicalStorage(false);
9055 cxx_record_decl->setHasExternalVisibleStorage(false);
9056 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009057 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009058 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009059 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009060
9061 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
9062
9063 if (enutype) {
9064 clang::EnumDecl *enum_decl = enutype->getDecl();
9065
9066 if (enum_decl) {
9067 if (!enum_decl->isCompleteDefinition()) {
9068 ClangASTContext *lldb_ast =
9069 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
9070 if (lldb_ast == nullptr)
9071 return false;
9072 clang::ASTContext *ast = lldb_ast->getASTContext();
9073
9074 /// TODO This really needs to be fixed.
9075
9076 QualType integer_type(enum_decl->getIntegerType());
9077 if (!integer_type.isNull()) {
9078 unsigned NumPositiveBits = 1;
9079 unsigned NumNegativeBits = 0;
9080
9081 clang::QualType promotion_qual_type;
9082 // If the enum integer type is less than an integer in bit width,
9083 // then we must promote it to an integer size.
9084 if (ast->getTypeSize(enum_decl->getIntegerType()) <
9085 ast->getTypeSize(ast->IntTy)) {
9086 if (enum_decl->getIntegerType()->isSignedIntegerType())
9087 promotion_qual_type = ast->IntTy;
9088 else
9089 promotion_qual_type = ast->UnsignedIntTy;
9090 } else
9091 promotion_qual_type = enum_decl->getIntegerType();
9092
9093 enum_decl->completeDefinition(enum_decl->getIntegerType(),
9094 promotion_qual_type, NumPositiveBits,
9095 NumNegativeBits);
9096 }
9097 }
9098 return true;
9099 }
9100 }
9101 }
9102 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00009103}
9104
Aleksandr Urakov709426b2018-09-10 08:08:43 +00009105clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009106 const CompilerType &enum_type, const Declaration &decl, const char *name,
Zachary Turner1639c6b2018-12-17 16:15:13 +00009107 const llvm::APSInt &value) {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00009108
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009109 if (!enum_type || ConstString(name).IsEmpty())
9110 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009111
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009112 lldbassert(enum_type.GetTypeSystem() == static_cast<TypeSystem *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009113
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009114 lldb::opaque_compiler_type_t enum_opaque_compiler_type =
9115 enum_type.GetOpaqueQualType();
9116
9117 if (!enum_opaque_compiler_type)
9118 return nullptr;
9119
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009120 clang::QualType enum_qual_type(
9121 GetCanonicalQualType(enum_opaque_compiler_type));
9122
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009123 const clang::Type *clang_type = enum_qual_type.getTypePtr();
9124
9125 if (!clang_type)
9126 return nullptr;
9127
9128 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
9129
9130 if (!enutype)
9131 return nullptr;
9132
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009133 clang::EnumConstantDecl *enumerator_decl = clang::EnumConstantDecl::Create(
9134 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
9135 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier
Zachary Turner1639c6b2018-12-17 16:15:13 +00009136 clang::QualType(enutype, 0), nullptr, value);
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009137
9138 if (!enumerator_decl)
9139 return nullptr;
9140
9141 enutype->getDecl()->addDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009142
9143#ifdef LLDB_CONFIGURATION_DEBUG
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009144 VerifyDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009145#endif
9146
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009147 return enumerator_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00009148}
9149
Zachary Turner1639c6b2018-12-17 16:15:13 +00009150clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
9151 const CompilerType &enum_type, const Declaration &decl, const char *name,
9152 int64_t enum_value, uint32_t enum_value_bit_size) {
9153 CompilerType underlying_type =
9154 GetEnumerationIntegerType(enum_type.GetOpaqueQualType());
9155 bool is_signed = false;
9156 underlying_type.IsIntegerType(is_signed);
9157
9158 llvm::APSInt value(enum_value_bit_size, is_signed);
9159 value = enum_value;
9160
9161 return AddEnumerationValueToEnumerationType(enum_type, decl, name, value);
9162}
9163
Greg Claytona1e5dc82015-08-11 22:53:00 +00009164CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00009165ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
9166 clang::QualType enum_qual_type(GetCanonicalQualType(type));
9167 const clang::Type *clang_type = enum_qual_type.getTypePtr();
9168 if (clang_type) {
9169 const clang::EnumType *enutype =
9170 llvm::dyn_cast<clang::EnumType>(clang_type);
9171 if (enutype) {
9172 clang::EnumDecl *enum_decl = enutype->getDecl();
9173 if (enum_decl)
Alex Langfordbddab072019-08-13 19:40:36 +00009174 return CompilerType(this, enum_decl->getIntegerType().getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00009175 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009176 }
9177 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00009178}
9179
Kate Stoneb9c1b512016-09-06 20:57:50 +00009180CompilerType
9181ClangASTContext::CreateMemberPointerType(const CompilerType &type,
9182 const CompilerType &pointee_type) {
9183 if (type && pointee_type.IsValid() &&
9184 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
9185 ClangASTContext *ast =
9186 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
9187 if (!ast)
9188 return CompilerType();
Alex Langfordbddab072019-08-13 19:40:36 +00009189 return CompilerType(ast, ast->getASTContext()
9190 ->getMemberPointerType(
9191 ClangUtil::GetQualType(pointee_type),
9192 ClangUtil::GetQualType(type).getTypePtr())
9193 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009194 }
9195 return CompilerType();
9196}
Greg Claytond8d4a572015-08-11 21:38:15 +00009197
Greg Claytond8d4a572015-08-11 21:38:15 +00009198// Dumping types
Greg Claytond8d4a572015-08-11 21:38:15 +00009199#define DEPTH_INCREMENT 2
9200
Adrian Prantl0c72a422019-03-07 20:20:02 +00009201#ifndef NDEBUG
9202LLVM_DUMP_METHOD void
9203ClangASTContext::dump(lldb::opaque_compiler_type_t type) const {
9204 if (!type)
9205 return;
9206 clang::QualType qual_type(GetQualType(type));
9207 qual_type.dump();
9208}
9209#endif
9210
Zachary Turner49110232018-11-05 17:40:28 +00009211void ClangASTContext::Dump(Stream &s) {
Zachary Turner115209e2018-11-05 19:25:39 +00009212 Decl *tu = Decl::castFromDeclContext(GetTranslationUnitDecl());
Zachary Turner49110232018-11-05 17:40:28 +00009213 tu->dump(s.AsRawOstream());
9214}
9215
Kate Stoneb9c1b512016-09-06 20:57:50 +00009216void ClangASTContext::DumpValue(
9217 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
Zachary Turner29cb8682017-03-03 20:57:05 +00009218 lldb::Format format, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009219 lldb::offset_t data_byte_offset, size_t data_byte_size,
9220 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
9221 bool show_summary, bool verbose, uint32_t depth) {
9222 if (!type)
9223 return;
9224
9225 clang::QualType qual_type(GetQualType(type));
9226 switch (qual_type->getTypeClass()) {
9227 case clang::Type::Record:
9228 if (GetCompleteType(type)) {
9229 const clang::RecordType *record_type =
9230 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9231 const clang::RecordDecl *record_decl = record_type->getDecl();
9232 assert(record_decl);
9233 uint32_t field_bit_offset = 0;
9234 uint32_t field_byte_offset = 0;
9235 const clang::ASTRecordLayout &record_layout =
9236 getASTContext()->getASTRecordLayout(record_decl);
9237 uint32_t child_idx = 0;
9238
9239 const clang::CXXRecordDecl *cxx_record_decl =
9240 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9241 if (cxx_record_decl) {
9242 // We might have base classes to print out first
9243 clang::CXXRecordDecl::base_class_const_iterator base_class,
9244 base_class_end;
9245 for (base_class = cxx_record_decl->bases_begin(),
9246 base_class_end = cxx_record_decl->bases_end();
9247 base_class != base_class_end; ++base_class) {
9248 const clang::CXXRecordDecl *base_class_decl =
9249 llvm::cast<clang::CXXRecordDecl>(
9250 base_class->getType()->getAs<clang::RecordType>()->getDecl());
9251
9252 // Skip empty base classes
Jonas Devliegherea6682a42018-12-15 00:15:33 +00009253 if (!verbose && !ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00009254 continue;
9255
9256 if (base_class->isVirtual())
9257 field_bit_offset =
9258 record_layout.getVBaseClassOffset(base_class_decl)
9259 .getQuantity() *
9260 8;
9261 else
9262 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
9263 .getQuantity() *
9264 8;
9265 field_byte_offset = field_bit_offset / 8;
9266 assert(field_bit_offset % 8 == 0);
9267 if (child_idx == 0)
9268 s->PutChar('{');
9269 else
9270 s->PutChar(',');
9271
9272 clang::QualType base_class_qual_type = base_class->getType();
9273 std::string base_class_type_name(base_class_qual_type.getAsString());
9274
9275 // Indent and print the base class type name
Zachary Turner827d5d72016-12-16 04:27:00 +00009276 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
9277 base_class_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009278
9279 clang::TypeInfo base_class_type_info =
9280 getASTContext()->getTypeInfo(base_class_qual_type);
9281
9282 // Dump the value of the member
Alex Langfordbddab072019-08-13 19:40:36 +00009283 CompilerType base_clang_type(this,
9284 base_class_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009285 base_clang_type.DumpValue(
9286 exe_ctx,
9287 s, // Stream to dump to
9288 base_clang_type
9289 .GetFormat(), // The format with which to display the member
9290 data, // Data buffer containing all bytes for this type
9291 data_byte_offset + field_byte_offset, // Offset into "data" where
9292 // to grab value from
9293 base_class_type_info.Width / 8, // Size of this type in bytes
9294 0, // Bitfield bit size
9295 0, // Bitfield bit offset
9296 show_types, // Boolean indicating if we should show the variable
9297 // types
9298 show_summary, // Boolean indicating if we should show a summary
9299 // for the current type
9300 verbose, // Verbose output?
9301 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9302 // children
9303
9304 ++child_idx;
9305 }
9306 }
9307 uint32_t field_idx = 0;
9308 clang::RecordDecl::field_iterator field, field_end;
9309 for (field = record_decl->field_begin(),
9310 field_end = record_decl->field_end();
9311 field != field_end; ++field, ++field_idx, ++child_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009312 // Print the starting squiggly bracket (if this is the first member) or
9313 // comma (for member 2 and beyond) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009314 if (child_idx == 0)
9315 s->PutChar('{');
9316 else
9317 s->PutChar(',');
9318
9319 // Indent
9320 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
9321
9322 clang::QualType field_type = field->getType();
9323 // Print the member type if requested
Adrian Prantl05097242018-04-30 16:49:04 +00009324 // Figure out the type byte size (field_type_info.first) and alignment
9325 // (field_type_info.second) from the AST context.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009326 clang::TypeInfo field_type_info =
9327 getASTContext()->getTypeInfo(field_type);
9328 assert(field_idx < record_layout.getFieldCount());
9329 // Figure out the field offset within the current struct/union/class
9330 // type
9331 field_bit_offset = record_layout.getFieldOffset(field_idx);
9332 field_byte_offset = field_bit_offset / 8;
9333 uint32_t field_bitfield_bit_size = 0;
9334 uint32_t field_bitfield_bit_offset = 0;
9335 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
9336 field_bitfield_bit_size))
9337 field_bitfield_bit_offset = field_bit_offset % 8;
9338
9339 if (show_types) {
9340 std::string field_type_name(field_type.getAsString());
9341 if (field_bitfield_bit_size > 0)
9342 s->Printf("(%s:%u) ", field_type_name.c_str(),
9343 field_bitfield_bit_size);
9344 else
9345 s->Printf("(%s) ", field_type_name.c_str());
9346 }
9347 // Print the member name and equal sign
9348 s->Printf("%s = ", field->getNameAsString().c_str());
9349
9350 // Dump the value of the member
Alex Langfordbddab072019-08-13 19:40:36 +00009351 CompilerType field_clang_type(this, field_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009352 field_clang_type.DumpValue(
9353 exe_ctx,
9354 s, // Stream to dump to
9355 field_clang_type
9356 .GetFormat(), // The format with which to display the member
9357 data, // Data buffer containing all bytes for this type
9358 data_byte_offset + field_byte_offset, // Offset into "data" where to
9359 // grab value from
9360 field_type_info.Width / 8, // Size of this type in bytes
9361 field_bitfield_bit_size, // Bitfield bit size
9362 field_bitfield_bit_offset, // Bitfield bit offset
9363 show_types, // Boolean indicating if we should show the variable
9364 // types
9365 show_summary, // Boolean indicating if we should show a summary for
9366 // the current type
9367 verbose, // Verbose output?
9368 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9369 // children
9370 }
9371
9372 // Indent the trailing squiggly bracket
9373 if (child_idx > 0)
9374 s->Printf("\n%*s}", depth, "");
9375 }
9376 return;
9377
9378 case clang::Type::Enum:
9379 if (GetCompleteType(type)) {
9380 const clang::EnumType *enutype =
9381 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9382 const clang::EnumDecl *enum_decl = enutype->getDecl();
9383 assert(enum_decl);
9384 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9385 lldb::offset_t offset = data_byte_offset;
9386 const int64_t enum_value = data.GetMaxU64Bitfield(
9387 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
9388 for (enum_pos = enum_decl->enumerator_begin(),
9389 enum_end_pos = enum_decl->enumerator_end();
9390 enum_pos != enum_end_pos; ++enum_pos) {
9391 if (enum_pos->getInitVal() == enum_value) {
9392 s->Printf("%s", enum_pos->getNameAsString().c_str());
9393 return;
9394 }
9395 }
Adrian Prantl05097242018-04-30 16:49:04 +00009396 // If we have gotten here we didn't get find the enumerator in the enum
9397 // decl, so just print the integer.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009398 s->Printf("%" PRIi64, enum_value);
9399 }
9400 return;
9401
9402 case clang::Type::ConstantArray: {
9403 const clang::ConstantArrayType *array =
9404 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9405 bool is_array_of_characters = false;
9406 clang::QualType element_qual_type = array->getElementType();
9407
9408 const clang::Type *canonical_type =
9409 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9410 if (canonical_type)
9411 is_array_of_characters = canonical_type->isCharType();
9412
9413 const uint64_t element_count = array->getSize().getLimitedValue();
9414
9415 clang::TypeInfo field_type_info =
9416 getASTContext()->getTypeInfo(element_qual_type);
9417
9418 uint32_t element_idx = 0;
9419 uint32_t element_offset = 0;
9420 uint64_t element_byte_size = field_type_info.Width / 8;
9421 uint32_t element_stride = element_byte_size;
9422
9423 if (is_array_of_characters) {
9424 s->PutChar('"');
Zachary Turner29cb8682017-03-03 20:57:05 +00009425 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9426 element_byte_size, element_count, UINT32_MAX,
9427 LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009428 s->PutChar('"');
9429 return;
9430 } else {
Alex Langfordbddab072019-08-13 19:40:36 +00009431 CompilerType element_clang_type(this, element_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009432 lldb::Format element_format = element_clang_type.GetFormat();
9433
9434 for (element_idx = 0; element_idx < element_count; ++element_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009435 // Print the starting squiggly bracket (if this is the first member) or
9436 // comman (for member 2 and beyong) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009437 if (element_idx == 0)
9438 s->PutChar('{');
9439 else
9440 s->PutChar(',');
9441
9442 // Indent and print the index
9443 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9444
9445 // Figure out the field offset within the current struct/union/class
9446 // type
9447 element_offset = element_idx * element_stride;
9448
9449 // Dump the value of the member
9450 element_clang_type.DumpValue(
9451 exe_ctx,
9452 s, // Stream to dump to
9453 element_format, // The format with which to display the element
9454 data, // Data buffer containing all bytes for this type
9455 data_byte_offset +
9456 element_offset, // Offset into "data" where to grab value from
9457 element_byte_size, // Size of this type in bytes
9458 0, // Bitfield bit size
9459 0, // Bitfield bit offset
9460 show_types, // Boolean indicating if we should show the variable
9461 // types
9462 show_summary, // Boolean indicating if we should show a summary for
9463 // the current type
9464 verbose, // Verbose output?
9465 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9466 // children
9467 }
9468
9469 // Indent the trailing squiggly bracket
9470 if (element_idx > 0)
9471 s->Printf("\n%*s}", depth, "");
9472 }
9473 }
9474 return;
9475
9476 case clang::Type::Typedef: {
9477 clang::QualType typedef_qual_type =
9478 llvm::cast<clang::TypedefType>(qual_type)
9479 ->getDecl()
9480 ->getUnderlyingType();
9481
Alex Langfordbddab072019-08-13 19:40:36 +00009482 CompilerType typedef_clang_type(this, typedef_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009483 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9484 clang::TypeInfo typedef_type_info =
9485 getASTContext()->getTypeInfo(typedef_qual_type);
9486 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9487
9488 return typedef_clang_type.DumpValue(
9489 exe_ctx,
9490 s, // Stream to dump to
9491 typedef_format, // The format with which to display the element
9492 data, // Data buffer containing all bytes for this type
9493 data_byte_offset, // Offset into "data" where to grab value from
9494 typedef_byte_size, // Size of this type in bytes
9495 bitfield_bit_size, // Bitfield bit size
9496 bitfield_bit_offset, // Bitfield bit offset
9497 show_types, // Boolean indicating if we should show the variable types
9498 show_summary, // Boolean indicating if we should show a summary for the
9499 // current type
9500 verbose, // Verbose output?
9501 depth); // Scope depth for any types that have children
9502 } break;
9503
9504 case clang::Type::Auto: {
9505 clang::QualType elaborated_qual_type =
9506 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
Alex Langfordbddab072019-08-13 19:40:36 +00009507 CompilerType elaborated_clang_type(this,
9508 elaborated_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009509 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9510 clang::TypeInfo elaborated_type_info =
9511 getASTContext()->getTypeInfo(elaborated_qual_type);
9512 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9513
9514 return elaborated_clang_type.DumpValue(
9515 exe_ctx,
9516 s, // Stream to dump to
9517 elaborated_format, // The format with which to display the element
9518 data, // Data buffer containing all bytes for this type
9519 data_byte_offset, // Offset into "data" where to grab value from
9520 elaborated_byte_size, // Size of this type in bytes
9521 bitfield_bit_size, // Bitfield bit size
9522 bitfield_bit_offset, // Bitfield bit offset
9523 show_types, // Boolean indicating if we should show the variable types
9524 show_summary, // Boolean indicating if we should show a summary for the
9525 // current type
9526 verbose, // Verbose output?
9527 depth); // Scope depth for any types that have children
9528 } break;
9529
9530 case clang::Type::Elaborated: {
9531 clang::QualType elaborated_qual_type =
9532 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
Alex Langfordbddab072019-08-13 19:40:36 +00009533 CompilerType elaborated_clang_type(this,
9534 elaborated_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009535 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9536 clang::TypeInfo elaborated_type_info =
9537 getASTContext()->getTypeInfo(elaborated_qual_type);
9538 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9539
9540 return elaborated_clang_type.DumpValue(
9541 exe_ctx,
9542 s, // Stream to dump to
9543 elaborated_format, // The format with which to display the element
9544 data, // Data buffer containing all bytes for this type
9545 data_byte_offset, // Offset into "data" where to grab value from
9546 elaborated_byte_size, // Size of this type in bytes
9547 bitfield_bit_size, // Bitfield bit size
9548 bitfield_bit_offset, // Bitfield bit offset
9549 show_types, // Boolean indicating if we should show the variable types
9550 show_summary, // Boolean indicating if we should show a summary for the
9551 // current type
9552 verbose, // Verbose output?
9553 depth); // Scope depth for any types that have children
9554 } break;
9555
9556 case clang::Type::Paren: {
9557 clang::QualType desugar_qual_type =
9558 llvm::cast<clang::ParenType>(qual_type)->desugar();
Alex Langfordbddab072019-08-13 19:40:36 +00009559 CompilerType desugar_clang_type(this, desugar_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009560
9561 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9562 clang::TypeInfo desugar_type_info =
9563 getASTContext()->getTypeInfo(desugar_qual_type);
9564 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9565
9566 return desugar_clang_type.DumpValue(
9567 exe_ctx,
9568 s, // Stream to dump to
9569 desugar_format, // The format with which to display the element
9570 data, // Data buffer containing all bytes for this type
9571 data_byte_offset, // Offset into "data" where to grab value from
9572 desugar_byte_size, // Size of this type in bytes
9573 bitfield_bit_size, // Bitfield bit size
9574 bitfield_bit_offset, // Bitfield bit offset
9575 show_types, // Boolean indicating if we should show the variable types
9576 show_summary, // Boolean indicating if we should show a summary for the
9577 // current type
9578 verbose, // Verbose output?
9579 depth); // Scope depth for any types that have children
9580 } break;
9581
9582 default:
9583 // We are down to a scalar type that we just need to display.
Zachary Turner29cb8682017-03-03 20:57:05 +00009584 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9585 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9586 bitfield_bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009587
9588 if (show_summary)
9589 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9590 break;
9591 }
9592}
9593
9594bool ClangASTContext::DumpTypeValue(
9595 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
Zachary Turner29cb8682017-03-03 20:57:05 +00009596 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9597 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009598 ExecutionContextScope *exe_scope) {
9599 if (!type)
9600 return false;
9601 if (IsAggregateType(type)) {
9602 return false;
9603 } else {
Greg Claytond8d4a572015-08-11 21:38:15 +00009604 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009605
9606 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9607 switch (type_class) {
9608 case clang::Type::Typedef: {
9609 clang::QualType typedef_qual_type =
9610 llvm::cast<clang::TypedefType>(qual_type)
9611 ->getDecl()
9612 ->getUnderlyingType();
Alex Langfordbddab072019-08-13 19:40:36 +00009613 CompilerType typedef_clang_type(this, typedef_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009614 if (format == eFormatDefault)
9615 format = typedef_clang_type.GetFormat();
9616 clang::TypeInfo typedef_type_info =
9617 getASTContext()->getTypeInfo(typedef_qual_type);
9618 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9619
9620 return typedef_clang_type.DumpTypeValue(
9621 s,
9622 format, // The format with which to display the element
9623 data, // Data buffer containing all bytes for this type
9624 byte_offset, // Offset into "data" where to grab value from
9625 typedef_byte_size, // Size of this type in bytes
9626 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9627 // treat as a bitfield
9628 bitfield_bit_offset, // Offset in bits of a bitfield value if
9629 // bitfield_bit_size != 0
9630 exe_scope);
9631 } break;
9632
9633 case clang::Type::Enum:
Adrian Prantl05097242018-04-30 16:49:04 +00009634 // If our format is enum or default, show the enumeration value as its
9635 // enumeration string value, else just display it as requested.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009636 if ((format == eFormatEnum || format == eFormatDefault) &&
9637 GetCompleteType(type)) {
9638 const clang::EnumType *enutype =
9639 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9640 const clang::EnumDecl *enum_decl = enutype->getDecl();
9641 assert(enum_decl);
9642 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9643 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9644 lldb::offset_t offset = byte_offset;
9645 if (is_signed) {
9646 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9647 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9648 for (enum_pos = enum_decl->enumerator_begin(),
9649 enum_end_pos = enum_decl->enumerator_end();
9650 enum_pos != enum_end_pos; ++enum_pos) {
9651 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009652 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009653 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009654 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009655 }
9656 // If we have gotten here we didn't get find the enumerator in the
9657 // enum decl, so just print the integer.
9658 s->Printf("%" PRIi64, enum_svalue);
9659 } else {
9660 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9661 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9662 for (enum_pos = enum_decl->enumerator_begin(),
9663 enum_end_pos = enum_decl->enumerator_end();
9664 enum_pos != enum_end_pos; ++enum_pos) {
9665 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009666 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009667 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009668 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009669 }
9670 // If we have gotten here we didn't get find the enumerator in the
9671 // enum decl, so just print the integer.
9672 s->Printf("%" PRIu64, enum_uvalue);
Greg Claytond8d4a572015-08-11 21:38:15 +00009673 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009674 return true;
9675 }
9676 // format was not enum, just fall through and dump the value as
9677 // requested....
9678 LLVM_FALLTHROUGH;
9679
9680 default:
9681 // We are down to a scalar type that we just need to display.
9682 {
9683 uint32_t item_count = 1;
9684 // A few formats, we might need to modify our size and count for
9685 // depending
9686 // on how we are trying to display the value...
9687 switch (format) {
Greg Claytond8d4a572015-08-11 21:38:15 +00009688 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00009689 case eFormatBoolean:
9690 case eFormatBinary:
9691 case eFormatComplex:
9692 case eFormatCString: // NULL terminated C strings
9693 case eFormatDecimal:
9694 case eFormatEnum:
9695 case eFormatHex:
9696 case eFormatHexUppercase:
9697 case eFormatFloat:
9698 case eFormatOctal:
9699 case eFormatOSType:
9700 case eFormatUnsigned:
9701 case eFormatPointer:
9702 case eFormatVectorOfChar:
9703 case eFormatVectorOfSInt8:
9704 case eFormatVectorOfUInt8:
9705 case eFormatVectorOfSInt16:
9706 case eFormatVectorOfUInt16:
9707 case eFormatVectorOfSInt32:
9708 case eFormatVectorOfUInt32:
9709 case eFormatVectorOfSInt64:
9710 case eFormatVectorOfUInt64:
9711 case eFormatVectorOfFloat32:
9712 case eFormatVectorOfFloat64:
9713 case eFormatVectorOfUInt128:
9714 break;
9715
9716 case eFormatChar:
9717 case eFormatCharPrintable:
9718 case eFormatCharArray:
9719 case eFormatBytes:
9720 case eFormatBytesWithASCII:
9721 item_count = byte_size;
9722 byte_size = 1;
9723 break;
9724
9725 case eFormatUnicode16:
9726 item_count = byte_size / 2;
9727 byte_size = 2;
9728 break;
9729
9730 case eFormatUnicode32:
9731 item_count = byte_size / 4;
9732 byte_size = 4;
9733 break;
9734 }
Zachary Turner29cb8682017-03-03 20:57:05 +00009735 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9736 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
9737 bitfield_bit_size, bitfield_bit_offset,
9738 exe_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009739 }
9740 break;
9741 }
9742 }
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00009743 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009744}
9745
9746void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9747 ExecutionContext *exe_ctx, Stream *s,
9748 const lldb_private::DataExtractor &data,
9749 lldb::offset_t data_byte_offset,
9750 size_t data_byte_size) {
9751 uint32_t length = 0;
9752 if (IsCStringType(type, length)) {
9753 if (exe_ctx) {
9754 Process *process = exe_ctx->GetProcessPtr();
9755 if (process) {
9756 lldb::offset_t offset = data_byte_offset;
9757 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9758 std::vector<uint8_t> buf;
9759 if (length > 0)
9760 buf.resize(length);
9761 else
9762 buf.resize(256);
9763
Zachary Turner29cb8682017-03-03 20:57:05 +00009764 DataExtractor cstr_data(&buf.front(), buf.size(),
9765 process->GetByteOrder(), 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009766 buf.back() = '\0';
9767 size_t bytes_read;
9768 size_t total_cstr_len = 0;
Zachary Turner97206d52017-05-12 04:51:55 +00009769 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009770 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9771 buf.size(), error)) > 0) {
9772 const size_t len = strlen((const char *)&buf.front());
9773 if (len == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00009774 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009775 if (total_cstr_len == 0)
9776 s->PutCString(" \"");
Zachary Turner29cb8682017-03-03 20:57:05 +00009777 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9778 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009779 total_cstr_len += len;
9780 if (len < buf.size())
9781 break;
9782 pointer_address += total_cstr_len;
Greg Claytond8d4a572015-08-11 21:38:15 +00009783 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009784 if (total_cstr_len > 0)
9785 s->PutChar('"');
9786 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009787 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009788 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009789}
9790
Kate Stoneb9c1b512016-09-06 20:57:50 +00009791void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9792 StreamFile s(stdout, false);
9793 DumpTypeDescription(type, &s);
9794 ClangASTMetadata *metadata =
9795 ClangASTContext::GetMetadata(getASTContext(), type);
9796 if (metadata) {
9797 metadata->Dump(&s);
9798 }
9799}
Greg Claytond8d4a572015-08-11 21:38:15 +00009800
Kate Stoneb9c1b512016-09-06 20:57:50 +00009801void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9802 Stream *s) {
9803 if (type) {
9804 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00009805
Kate Stoneb9c1b512016-09-06 20:57:50 +00009806 llvm::SmallVector<char, 1024> buf;
9807 llvm::raw_svector_ostream llvm_ostrm(buf);
9808
9809 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9810 switch (type_class) {
9811 case clang::Type::ObjCObject:
9812 case clang::Type::ObjCInterface: {
9813 GetCompleteType(type);
9814
9815 const clang::ObjCObjectType *objc_class_type =
9816 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9817 assert(objc_class_type);
9818 if (objc_class_type) {
9819 clang::ObjCInterfaceDecl *class_interface_decl =
9820 objc_class_type->getInterface();
9821 if (class_interface_decl) {
9822 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9823 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
Greg Claytond8d4a572015-08-11 21:38:15 +00009824 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009825 }
9826 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00009827
Kate Stoneb9c1b512016-09-06 20:57:50 +00009828 case clang::Type::Typedef: {
9829 const clang::TypedefType *typedef_type =
9830 qual_type->getAs<clang::TypedefType>();
9831 if (typedef_type) {
9832 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9833 std::string clang_typedef_name(
9834 typedef_decl->getQualifiedNameAsString());
9835 if (!clang_typedef_name.empty()) {
9836 s->PutCString("typedef ");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009837 s->PutCString(clang_typedef_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00009838 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009839 }
9840 } break;
9841
9842 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00009843 CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
9844 ->getDeducedType()
9845 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00009846 .DumpTypeDescription(s);
9847 return;
9848
9849 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00009850 CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
9851 ->getNamedType()
9852 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00009853 .DumpTypeDescription(s);
9854 return;
9855
9856 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00009857 CompilerType(
9858 this,
9859 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00009860 .DumpTypeDescription(s);
9861 return;
9862
9863 case clang::Type::Record: {
9864 GetCompleteType(type);
9865
9866 const clang::RecordType *record_type =
9867 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9868 const clang::RecordDecl *record_decl = record_type->getDecl();
9869 const clang::CXXRecordDecl *cxx_record_decl =
9870 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9871
9872 if (cxx_record_decl)
9873 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9874 s->GetIndentLevel());
9875 else
9876 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9877 s->GetIndentLevel());
9878 } break;
9879
9880 default: {
9881 const clang::TagType *tag_type =
9882 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9883 if (tag_type) {
9884 clang::TagDecl *tag_decl = tag_type->getDecl();
9885 if (tag_decl)
9886 tag_decl->print(llvm_ostrm, 0);
9887 } else {
9888 std::string clang_type_name(qual_type.getAsString());
9889 if (!clang_type_name.empty())
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009890 s->PutCString(clang_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009891 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009892 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00009893 }
9894
Kate Stoneb9c1b512016-09-06 20:57:50 +00009895 if (buf.size() > 0) {
9896 s->Write(buf.data(), buf.size());
Greg Clayton8b4edba2015-08-14 20:02:05 +00009897 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009898 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00009899}
9900
Kate Stoneb9c1b512016-09-06 20:57:50 +00009901void ClangASTContext::DumpTypeName(const CompilerType &type) {
9902 if (ClangUtil::IsClangType(type)) {
9903 clang::QualType qual_type(
9904 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9905
9906 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9907 switch (type_class) {
9908 case clang::Type::Record: {
9909 const clang::CXXRecordDecl *cxx_record_decl =
9910 qual_type->getAsCXXRecordDecl();
9911 if (cxx_record_decl)
9912 printf("class %s", cxx_record_decl->getName().str().c_str());
9913 } break;
9914
9915 case clang::Type::Enum: {
9916 clang::EnumDecl *enum_decl =
9917 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9918 if (enum_decl) {
9919 printf("enum %s", enum_decl->getName().str().c_str());
9920 }
9921 } break;
9922
9923 case clang::Type::ObjCObject:
9924 case clang::Type::ObjCInterface: {
9925 const clang::ObjCObjectType *objc_class_type =
9926 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9927 if (objc_class_type) {
9928 clang::ObjCInterfaceDecl *class_interface_decl =
9929 objc_class_type->getInterface();
Adrian Prantl05097242018-04-30 16:49:04 +00009930 // We currently can't complete objective C types through the newly
9931 // added ASTContext because it only supports TagDecl objects right
9932 // now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00009933 if (class_interface_decl)
9934 printf("@class %s", class_interface_decl->getName().str().c_str());
9935 }
9936 } break;
9937
9938 case clang::Type::Typedef:
9939 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9940 ->getDecl()
9941 ->getName()
9942 .str()
9943 .c_str());
9944 break;
9945
9946 case clang::Type::Auto:
9947 printf("auto ");
9948 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9949 llvm::cast<clang::AutoType>(qual_type)
9950 ->getDeducedType()
9951 .getAsOpaquePtr()));
9952
9953 case clang::Type::Elaborated:
9954 printf("elaborated ");
9955 return DumpTypeName(CompilerType(
9956 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9957 ->getNamedType()
9958 .getAsOpaquePtr()));
9959
9960 case clang::Type::Paren:
9961 printf("paren ");
9962 return DumpTypeName(CompilerType(
9963 type.GetTypeSystem(),
9964 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9965
9966 default:
9967 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9968 break;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009969 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009970 }
Greg Clayton6dc8d582015-08-18 22:32:36 +00009971}
9972
Kate Stoneb9c1b512016-09-06 20:57:50 +00009973clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9974 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9975 const char *parent_name, int tag_decl_kind,
9976 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9977 if (template_param_infos.IsValid()) {
9978 std::string template_basename(parent_name);
9979 template_basename.erase(template_basename.find('<'));
9980
9981 return CreateClassTemplateDecl(decl_ctx, access_type,
9982 template_basename.c_str(), tag_decl_kind,
9983 template_param_infos);
9984 }
Konrad Kleine248a1302019-05-23 11:14:47 +00009985 return nullptr;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009986}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009987
Kate Stoneb9c1b512016-09-06 20:57:50 +00009988void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9989 ClangASTContext *ast = (ClangASTContext *)baton;
9990 SymbolFile *sym_file = ast->GetSymbolFile();
9991 if (sym_file) {
9992 CompilerType clang_type = GetTypeForDecl(decl);
9993 if (clang_type)
9994 sym_file->CompleteType(clang_type);
9995 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00009996}
9997
Kate Stoneb9c1b512016-09-06 20:57:50 +00009998void ClangASTContext::CompleteObjCInterfaceDecl(
9999 void *baton, clang::ObjCInterfaceDecl *decl) {
10000 ClangASTContext *ast = (ClangASTContext *)baton;
10001 SymbolFile *sym_file = ast->GetSymbolFile();
10002 if (sym_file) {
10003 CompilerType clang_type = GetTypeForDecl(decl);
10004 if (clang_type)
10005 sym_file->CompleteType(clang_type);
10006 }
Zachary Turner42dff792016-04-15 00:21:26 +000010007}
Greg Clayton261ac3f2015-08-28 01:01:03 +000010008
Kate Stoneb9c1b512016-09-06 20:57:50 +000010009DWARFASTParser *ClangASTContext::GetDWARFParser() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +000010010 if (!m_dwarf_ast_parser_up)
10011 m_dwarf_ast_parser_up.reset(new DWARFASTParserClang(*this));
10012 return m_dwarf_ast_parser_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +000010013}
10014
10015PDBASTParser *ClangASTContext::GetPDBParser() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +000010016 if (!m_pdb_ast_parser_up)
10017 m_pdb_ast_parser_up.reset(new PDBASTParser(*this));
10018 return m_pdb_ast_parser_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +000010019}
10020
10021bool ClangASTContext::LayoutRecordType(
10022 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
10023 uint64_t &alignment,
10024 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
10025 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
10026 &base_offsets,
10027 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
10028 &vbase_offsets) {
10029 ClangASTContext *ast = (ClangASTContext *)baton;
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +000010030 lldb_private::ClangASTImporter *importer = nullptr;
Jonas Devlieghered5b44032019-02-13 06:25:41 +000010031 if (ast->m_dwarf_ast_parser_up)
10032 importer = &ast->m_dwarf_ast_parser_up->GetClangASTImporter();
10033 if (!importer && ast->m_pdb_ast_parser_up)
10034 importer = &ast->m_pdb_ast_parser_up->GetClangASTImporter();
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +000010035 if (!importer)
10036 return false;
10037
10038 return importer->LayoutRecordType(record_decl, bit_size, alignment,
10039 field_offsets, base_offsets, vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010040}
10041
Paul Hermand628cbb2015-09-15 23:44:17 +000010042// CompilerDecl override functions
Paul Hermand628cbb2015-09-15 23:44:17 +000010043
Kate Stoneb9c1b512016-09-06 20:57:50 +000010044ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
10045 if (opaque_decl) {
10046 clang::NamedDecl *nd =
10047 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
10048 if (nd != nullptr)
10049 return ConstString(nd->getDeclName().getAsString());
10050 }
10051 return ConstString();
Paul Hermand628cbb2015-09-15 23:44:17 +000010052}
10053
Kate Stoneb9c1b512016-09-06 20:57:50 +000010054ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
10055 if (opaque_decl) {
10056 clang::NamedDecl *nd =
10057 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
10058 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
10059 clang::MangleContext *mc = getMangleContext();
10060 if (mc && mc->shouldMangleCXXName(nd)) {
10061 llvm::SmallVector<char, 1024> buf;
10062 llvm::raw_svector_ostream llvm_ostrm(buf);
10063 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
10064 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
10065 Ctor_Complete, llvm_ostrm);
10066 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
10067 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
10068 Dtor_Complete, llvm_ostrm);
10069 } else {
10070 mc->mangleName(nd, llvm_ostrm);
Greg Claytonfe689042015-11-10 17:47:04 +000010071 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010072 if (buf.size() > 0)
10073 return ConstString(buf.data(), buf.size());
10074 }
Greg Claytonfe689042015-11-10 17:47:04 +000010075 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010076 }
10077 return ConstString();
Greg Claytonfe689042015-11-10 17:47:04 +000010078}
10079
Kate Stoneb9c1b512016-09-06 20:57:50 +000010080CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
10081 if (opaque_decl)
10082 return CompilerDeclContext(this,
10083 ((clang::Decl *)opaque_decl)->getDeclContext());
10084 else
10085 return CompilerDeclContext();
Greg Claytonfe689042015-11-10 17:47:04 +000010086}
10087
Kate Stoneb9c1b512016-09-06 20:57:50 +000010088CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
10089 if (clang::FunctionDecl *func_decl =
10090 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
10091 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
10092 if (clang::ObjCMethodDecl *objc_method =
10093 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
10094 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
10095 else
Greg Claytonfe689042015-11-10 17:47:04 +000010096 return CompilerType();
10097}
10098
Kate Stoneb9c1b512016-09-06 20:57:50 +000010099size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
10100 if (clang::FunctionDecl *func_decl =
10101 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
10102 return func_decl->param_size();
10103 if (clang::ObjCMethodDecl *objc_method =
10104 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
10105 return objc_method->param_size();
10106 else
10107 return 0;
10108}
10109
10110CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
10111 size_t idx) {
10112 if (clang::FunctionDecl *func_decl =
10113 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
10114 if (idx < func_decl->param_size()) {
10115 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
10116 if (var_decl)
10117 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
10118 }
10119 } else if (clang::ObjCMethodDecl *objc_method =
10120 llvm::dyn_cast<clang::ObjCMethodDecl>(
10121 (clang::Decl *)opaque_decl)) {
10122 if (idx < objc_method->param_size())
10123 return CompilerType(
10124 this,
10125 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
10126 }
10127 return CompilerType();
10128}
10129
Greg Clayton99558cc42015-08-24 23:46:31 +000010130// CompilerDeclContext functions
Greg Clayton99558cc42015-08-24 23:46:31 +000010131
Kate Stoneb9c1b512016-09-06 20:57:50 +000010132std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
10133 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
10134 std::vector<CompilerDecl> found_decls;
10135 if (opaque_decl_ctx) {
10136 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
10137 std::set<DeclContext *> searched;
10138 std::multimap<DeclContext *, DeclContext *> search_queue;
10139 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +000010140
Kate Stoneb9c1b512016-09-06 20:57:50 +000010141 for (clang::DeclContext *decl_context = root_decl_ctx;
10142 decl_context != nullptr && found_decls.empty();
10143 decl_context = decl_context->getParent()) {
10144 search_queue.insert(std::make_pair(decl_context, decl_context));
Paul Hermand628cbb2015-09-15 23:44:17 +000010145
Kate Stoneb9c1b512016-09-06 20:57:50 +000010146 for (auto it = search_queue.find(decl_context); it != search_queue.end();
10147 it++) {
10148 if (!searched.insert(it->second).second)
10149 continue;
10150 symbol_file->ParseDeclsForContext(
10151 CompilerDeclContext(this, it->second));
Paul Hermanea188fc2015-09-16 18:48:30 +000010152
Kate Stoneb9c1b512016-09-06 20:57:50 +000010153 for (clang::Decl *child : it->second->decls()) {
10154 if (clang::UsingDirectiveDecl *ud =
10155 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10156 if (ignore_using_decls)
10157 continue;
10158 clang::DeclContext *from = ud->getCommonAncestor();
10159 if (searched.find(ud->getNominatedNamespace()) == searched.end())
10160 search_queue.insert(
10161 std::make_pair(from, ud->getNominatedNamespace()));
10162 } else if (clang::UsingDecl *ud =
10163 llvm::dyn_cast<clang::UsingDecl>(child)) {
10164 if (ignore_using_decls)
10165 continue;
10166 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10167 clang::Decl *target = usd->getTargetDecl();
10168 if (clang::NamedDecl *nd =
10169 llvm::dyn_cast<clang::NamedDecl>(target)) {
10170 IdentifierInfo *ii = nd->getIdentifier();
10171 if (ii != nullptr &&
10172 ii->getName().equals(name.AsCString(nullptr)))
10173 found_decls.push_back(CompilerDecl(this, nd));
10174 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010175 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010176 } else if (clang::NamedDecl *nd =
10177 llvm::dyn_cast<clang::NamedDecl>(child)) {
10178 IdentifierInfo *ii = nd->getIdentifier();
10179 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
10180 found_decls.push_back(CompilerDecl(this, nd));
10181 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010182 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010183 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010184 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010185 }
10186 return found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +000010187}
10188
Dawn Perchikb5925782015-12-12 19:31:41 +000010189// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010190// and return the number of levels it took to find it, or
Adrian Prantl05097242018-04-30 16:49:04 +000010191// LLDB_INVALID_DECL_LEVEL if not found. If the decl was imported via a using
10192// declaration, its name and/or type, if set, will be used to check that the
10193// decl found in the scope is a match.
Dawn Perchikb5925782015-12-12 19:31:41 +000010194//
Kate Stoneb9c1b512016-09-06 20:57:50 +000010195// The optional name is required by languages (like C++) to handle using
Adrian Prantl05097242018-04-30 16:49:04 +000010196// declarations like:
Dawn Perchikb5925782015-12-12 19:31:41 +000010197//
10198// void poo();
10199// namespace ns {
10200// void foo();
10201// void goo();
10202// }
10203// void bar() {
10204// using ns::foo;
10205// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
10206// // LLDB_INVALID_DECL_LEVEL for 'goo'.
10207// }
10208//
10209// The optional type is useful in the case that there's a specific overload
10210// that we're looking for that might otherwise be shadowed, like:
10211//
10212// void foo(int);
10213// namespace ns {
10214// void foo();
10215// }
10216// void bar() {
10217// using ns::foo;
10218// // CountDeclLevels returns 0 for { 'foo', void() },
10219// // 1 for { 'foo', void(int) }, and
10220// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
10221// }
10222//
10223// NOTE: Because file statics are at the TranslationUnit along with globals, a
Kate Stoneb9c1b512016-09-06 20:57:50 +000010224// function at file scope will return the same level as a function at global
Adrian Prantl05097242018-04-30 16:49:04 +000010225// scope. Ideally we'd like to treat the file scope as an additional scope just
10226// below the global scope. More work needs to be done to recognise that, if
10227// the decl we're trying to look up is static, we should compare its source
10228// file with that of the current scope and return a lower number for it.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010229uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
10230 clang::DeclContext *child_decl_ctx,
10231 ConstString *child_name,
10232 CompilerType *child_type) {
10233 if (frame_decl_ctx) {
10234 std::set<DeclContext *> searched;
10235 std::multimap<DeclContext *, DeclContext *> search_queue;
10236 SymbolFile *symbol_file = GetSymbolFile();
Dawn Perchikb5925782015-12-12 19:31:41 +000010237
Kate Stoneb9c1b512016-09-06 20:57:50 +000010238 // Get the lookup scope for the decl we're trying to find.
10239 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
Dawn Perchikb5925782015-12-12 19:31:41 +000010240
Kate Stoneb9c1b512016-09-06 20:57:50 +000010241 // Look for it in our scope's decl context and its parents.
10242 uint32_t level = 0;
10243 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
10244 decl_ctx = decl_ctx->getParent()) {
10245 if (!decl_ctx->isLookupContext())
10246 continue;
10247 if (decl_ctx == parent_decl_ctx)
10248 // Found it!
10249 return level;
10250 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
10251 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
10252 it++) {
10253 if (searched.find(it->second) != searched.end())
10254 continue;
10255
10256 // Currently DWARF has one shared translation unit for all Decls at top
Adrian Prantl05097242018-04-30 16:49:04 +000010257 // level, so this would erroneously find using statements anywhere. So
10258 // don't look at the top-level translation unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010259 // TODO fix this and add a testcase that depends on it.
10260
10261 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
10262 continue;
10263
10264 searched.insert(it->second);
10265 symbol_file->ParseDeclsForContext(
10266 CompilerDeclContext(this, it->second));
10267
10268 for (clang::Decl *child : it->second->decls()) {
10269 if (clang::UsingDirectiveDecl *ud =
10270 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10271 clang::DeclContext *ns = ud->getNominatedNamespace();
10272 if (ns == parent_decl_ctx)
10273 // Found it!
10274 return level;
10275 clang::DeclContext *from = ud->getCommonAncestor();
10276 if (searched.find(ns) == searched.end())
10277 search_queue.insert(std::make_pair(from, ns));
10278 } else if (child_name) {
10279 if (clang::UsingDecl *ud =
10280 llvm::dyn_cast<clang::UsingDecl>(child)) {
10281 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10282 clang::Decl *target = usd->getTargetDecl();
10283 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
10284 if (!nd)
10285 continue;
10286 // Check names.
10287 IdentifierInfo *ii = nd->getIdentifier();
10288 if (ii == nullptr ||
10289 !ii->getName().equals(child_name->AsCString(nullptr)))
10290 continue;
10291 // Check types, if one was provided.
10292 if (child_type) {
10293 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
10294 if (!AreTypesSame(clang_type, *child_type,
10295 /*ignore_qualifiers=*/true))
10296 continue;
10297 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010298 // Found it!
10299 return level;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010300 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010301 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010302 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010303 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010304 }
10305 ++level;
Dawn Perchikb5925782015-12-12 19:31:41 +000010306 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010307 }
10308 return LLDB_INVALID_DECL_LEVEL;
Dawn Perchikb5925782015-12-12 19:31:41 +000010309}
10310
Kate Stoneb9c1b512016-09-06 20:57:50 +000010311bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
10312 if (opaque_decl_ctx)
10313 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
10314 else
Greg Clayton99558cc42015-08-24 23:46:31 +000010315 return false;
10316}
10317
Kate Stoneb9c1b512016-09-06 20:57:50 +000010318ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
10319 if (opaque_decl_ctx) {
10320 clang::NamedDecl *named_decl =
10321 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10322 if (named_decl)
10323 return ConstString(named_decl->getName());
10324 }
10325 return ConstString();
Greg Clayton99558cc42015-08-24 23:46:31 +000010326}
10327
Kate Stoneb9c1b512016-09-06 20:57:50 +000010328ConstString
10329ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
10330 if (opaque_decl_ctx) {
10331 clang::NamedDecl *named_decl =
10332 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10333 if (named_decl)
10334 return ConstString(
10335 llvm::StringRef(named_decl->getQualifiedNameAsString()));
10336 }
10337 return ConstString();
10338}
10339
10340bool ClangASTContext::DeclContextIsClassMethod(
10341 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
10342 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
10343 if (opaque_decl_ctx) {
10344 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10345 if (ObjCMethodDecl *objc_method =
10346 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
10347 if (is_instance_method_ptr)
10348 *is_instance_method_ptr = objc_method->isInstanceMethod();
10349 if (language_ptr)
10350 *language_ptr = eLanguageTypeObjC;
10351 if (language_object_name_ptr)
10352 language_object_name_ptr->SetCString("self");
10353 return true;
10354 } else if (CXXMethodDecl *cxx_method =
10355 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
10356 if (is_instance_method_ptr)
10357 *is_instance_method_ptr = cxx_method->isInstance();
10358 if (language_ptr)
10359 *language_ptr = eLanguageTypeC_plus_plus;
10360 if (language_object_name_ptr)
10361 language_object_name_ptr->SetCString("this");
10362 return true;
10363 } else if (clang::FunctionDecl *function_decl =
10364 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
10365 ClangASTMetadata *metadata =
10366 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
10367 if (metadata && metadata->HasObjectPtr()) {
10368 if (is_instance_method_ptr)
10369 *is_instance_method_ptr = true;
10370 if (language_ptr)
10371 *language_ptr = eLanguageTypeObjC;
10372 if (language_object_name_ptr)
10373 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
10374 return true;
10375 }
10376 }
10377 }
10378 return false;
10379}
10380
Raphael Isemanna9469972019-03-12 07:45:04 +000010381bool ClangASTContext::DeclContextIsContainedInLookup(
10382 void *opaque_decl_ctx, void *other_opaque_decl_ctx) {
10383 auto *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10384 auto *other = (clang::DeclContext *)other_opaque_decl_ctx;
10385
10386 do {
10387 // A decl context always includes its own contents in its lookup.
10388 if (decl_ctx == other)
10389 return true;
10390
10391 // If we have an inline namespace, then the lookup of the parent context
10392 // also includes the inline namespace contents.
10393 } while (other->isInlineNamespace() && (other = other->getParent()));
10394
10395 return false;
10396}
10397
Kate Stoneb9c1b512016-09-06 20:57:50 +000010398clang::DeclContext *
10399ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
10400 if (dc.IsClang())
10401 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
10402 return nullptr;
10403}
Greg Clayton99558cc42015-08-24 23:46:31 +000010404
10405ObjCMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010406ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
10407 if (dc.IsClang())
10408 return llvm::dyn_cast<clang::ObjCMethodDecl>(
10409 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10410 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010411}
10412
10413CXXMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010414ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10415 if (dc.IsClang())
10416 return llvm::dyn_cast<clang::CXXMethodDecl>(
10417 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10418 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010419}
10420
10421clang::FunctionDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010422ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10423 if (dc.IsClang())
10424 return llvm::dyn_cast<clang::FunctionDecl>(
10425 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10426 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010427}
10428
10429clang::NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010430ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10431 if (dc.IsClang())
10432 return llvm::dyn_cast<clang::NamespaceDecl>(
10433 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10434 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010435}
10436
10437ClangASTMetadata *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010438ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10439 const void *object) {
10440 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10441 if (ast)
10442 return ClangASTContext::GetMetadata(ast, object);
10443 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010444}
10445
10446clang::ASTContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010447ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10448 ClangASTContext *ast =
10449 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10450 if (ast)
10451 return ast->getASTContext();
10452 return nullptr;
10453}
10454
10455ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10456 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10457 m_target_wp(target.shared_from_this()),
10458 m_persistent_variables(new ClangPersistentVariables) {}
10459
10460UserExpression *ClangASTContextForExpressions::GetUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010461 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010462 Expression::ResultType desired_type,
Aleksandr Urakov40624a02019-02-05 09:14:36 +000010463 const EvaluateExpressionOptions &options,
10464 ValueObject *ctx_obj) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000010465 TargetSP target_sp = m_target_wp.lock();
10466 if (!target_sp)
Greg Clayton99558cc42015-08-24 23:46:31 +000010467 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010468
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010469 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
Aleksandr Urakov40624a02019-02-05 09:14:36 +000010470 desired_type, options, ctx_obj);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010471}
10472
Kate Stoneb9c1b512016-09-06 20:57:50 +000010473FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10474 const CompilerType &return_type, const Address &function_address,
10475 const ValueList &arg_value_list, const char *name) {
10476 TargetSP target_sp = m_target_wp.lock();
10477 if (!target_sp)
10478 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010479
Kate Stoneb9c1b512016-09-06 20:57:50 +000010480 Process *process = target_sp->GetProcessSP().get();
10481 if (!process)
10482 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010483
Kate Stoneb9c1b512016-09-06 20:57:50 +000010484 return new ClangFunctionCaller(*process, return_type, function_address,
10485 arg_value_list, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010486}
10487
10488UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010489ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10490 const char *name) {
10491 TargetSP target_sp = m_target_wp.lock();
10492 if (!target_sp)
10493 return nullptr;
10494
10495 return new ClangUtilityFunction(*target_sp.get(), text, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010496}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010497
10498PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010499ClangASTContextForExpressions::GetPersistentExpressionState() {
10500 return m_persistent_variables.get();
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010501}
Sean Callanan68e44232017-09-28 20:20:25 +000010502
10503clang::ExternalASTMerger &
10504ClangASTContextForExpressions::GetMergerUnchecked() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +000010505 lldbassert(m_scratch_ast_source_up != nullptr);
10506 return m_scratch_ast_source_up->GetMergerUnchecked();
Sean Callanan68e44232017-09-28 20:20:25 +000010507}