blob: 726963f5a9ebffcd3866dbf554f059e2cfba5269 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ClangASTContext.cpp -------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Eli Friedman932197d2010-06-13 19:06:42 +00009#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010
Zachary Turner827d5d72016-12-16 04:27:00 +000011#include "llvm/Support/FormatAdapters.h"
12#include "llvm/Support/FormatVariadic.h"
13
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000014#include <mutex>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include <string>
Sean Callananfe38c852015-10-08 23:07:53 +000016#include <vector>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017
Greg Clayton6beaaa62011-01-17 03:46:26 +000018
Kate Stoneb9c1b512016-09-06 20:57:50 +000019// Clang headers like to use NDEBUG inside of them to enable/disable debug
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +000020// related features using "#ifndef NDEBUG" preprocessor blocks to do one thing
Greg Clayton6beaaa62011-01-17 03:46:26 +000021// or another. This is bad because it means that if clang was built in release
22// mode, it assumes that you are building in release mode which is not always
23// the case. You can end up with functions that are defined as empty in header
24// files when NDEBUG is not defined, and this can cause link errors with the
25// clang .a files that you have since you might be missing functions in the .a
26// file. So we have to define NDEBUG when including clang headers to avoid any
27// mismatches. This is covered by rdar://problem/8691220
28
Sean Callanan3b1d4f62011-10-26 17:46:51 +000029#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
Greg Clayton6beaaa62011-01-17 03:46:26 +000030#define LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000031#define NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000032// Need to include assert.h so it is as clang would expect it to be (disabled)
33#include <assert.h>
34#endif
35
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "clang/AST/ASTContext.h"
37#include "clang/AST/ASTImporter.h"
Greg Claytonf74c4032012-12-03 18:29:55 +000038#include "clang/AST/Attr.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "clang/AST/CXXInheritance.h"
Greg Clayton8cf05932010-07-22 18:30:50 +000040#include "clang/AST/DeclObjC.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000041#include "clang/AST/DeclTemplate.h"
Greg Claytonfe689042015-11-10 17:47:04 +000042#include "clang/AST/Mangle.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "clang/AST/RecordLayout.h"
44#include "clang/AST/Type.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000045#include "clang/AST/VTableBuilder.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046#include "clang/Basic/Builtins.h"
Sean Callanan7e2863b2012-02-06 21:28:03 +000047#include "clang/Basic/Diagnostic.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048#include "clang/Basic/FileManager.h"
Sean Callanan79439e82010-11-18 02:56:27 +000049#include "clang/Basic/FileSystemOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050#include "clang/Basic/SourceManager.h"
51#include "clang/Basic/TargetInfo.h"
52#include "clang/Basic/TargetOptions.h"
53#include "clang/Frontend/FrontendOptions.h"
54#include "clang/Frontend/LangStandard.h"
Raphael Isemannf74a4c12019-04-30 08:41:35 +000055#include "clang/Sema/Sema.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000056
57#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000058#undef NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000059#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
60// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
61#include <assert.h>
62#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063
Greg Claytond8d4a572015-08-11 21:38:15 +000064#include "llvm/Support/Signals.h"
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000065#include "llvm/Support/Threading.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000066
Zachary Turnerd133f6a2016-03-28 22:53:41 +000067#include "Plugins/ExpressionParser/Clang/ClangFunctionCaller.h"
68#include "Plugins/ExpressionParser/Clang/ClangUserExpression.h"
69#include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000070#include "lldb/Utility/ArchSpec.h"
Zachary Turner01c32432017-02-14 19:06:07 +000071#include "lldb/Utility/Flags.h"
72
Zachary Turner29cb8682017-03-03 20:57:05 +000073#include "lldb/Core/DumpDataExtractor.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000074#include "lldb/Core/Module.h"
75#include "lldb/Core/PluginManager.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000076#include "lldb/Core/StreamFile.h"
Enrico Granata2267ad42014-09-16 17:28:40 +000077#include "lldb/Core/ThreadSafeDenseMap.h"
Greg Clayton57ee3062013-07-11 22:46:58 +000078#include "lldb/Core/UniqueCStringMap.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000079#include "lldb/Symbol/ClangASTContext.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000080#include "lldb/Symbol/ClangASTImporter.h"
Greg Clayton6dc8d582015-08-18 22:32:36 +000081#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000082#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000083#include "lldb/Symbol/ClangUtil.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000084#include "lldb/Symbol/ObjectFile.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000085#include "lldb/Symbol/SymbolFile.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000086#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000087#include "lldb/Target/ExecutionContext.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000088#include "lldb/Target/Language.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000089#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham151c0322015-09-15 21:13:50 +000090#include "lldb/Target/Process.h"
91#include "lldb/Target/Target.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000092#include "lldb/Utility/DataExtractor.h"
Sean Callananc530ba92016-05-02 21:15:31 +000093#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000094#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000095#include "lldb/Utility/RegularExpression.h"
Pavel Labathd821c992018-08-07 11:07:21 +000096#include "lldb/Utility/Scalar.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000097
Greg Clayton261ac3f2015-08-28 01:01:03 +000098#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
Zachary Turner42dff792016-04-15 00:21:26 +000099#include "Plugins/SymbolFile/PDB/PDBASTParser.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +0000100
Eli Friedman932197d2010-06-13 19:06:42 +0000101#include <stdio.h>
102
Greg Clayton1341baf2013-07-11 23:36:31 +0000103#include <mutex>
104
Greg Claytonc86103d2010-08-05 01:57:25 +0000105using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106using namespace lldb_private;
107using namespace llvm;
108using namespace clang;
109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110namespace {
111static inline bool
112ClangASTContextSupportsLanguage(lldb::LanguageType language) {
113 return language == eLanguageTypeUnknown || // Clang is the default type system
114 Language::LanguageIsC(language) ||
115 Language::LanguageIsCPlusPlus(language) ||
116 Language::LanguageIsObjC(language) ||
117 Language::LanguageIsPascal(language) ||
118 // Use Clang for Rust until there is a proper language plugin for it
119 language == eLanguageTypeRust ||
Johan Engelen04799572016-11-25 11:01:12 +0000120 language == eLanguageTypeExtRenderScript ||
121 // Use Clang for D until there is a proper language plugin for it
Bruce Mitchenerb8233f82018-11-27 05:37:27 +0000122 language == eLanguageTypeD ||
123 // Open Dylan compiler debug info is designed to be Clang-compatible
124 language == eLanguageTypeDylan;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125}
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000126
127// Checks whether m1 is an overload of m2 (as opposed to an override). This is
128// called by addOverridesForMethod to distinguish overrides (which share a
129// vtable entry) from overloads (which require distinct entries).
130bool isOverload(clang::CXXMethodDecl *m1, clang::CXXMethodDecl *m2) {
131 // FIXME: This should detect covariant return types, but currently doesn't.
132 lldbassert(&m1->getASTContext() == &m2->getASTContext() &&
133 "Methods should have the same AST context");
134 clang::ASTContext &context = m1->getASTContext();
135
136 const auto *m1Type = llvm::cast<clang::FunctionProtoType>(
137 context.getCanonicalType(m1->getType()));
138
139 const auto *m2Type = llvm::cast<clang::FunctionProtoType>(
140 context.getCanonicalType(m2->getType()));
141
142 auto compareArgTypes = [&context](const clang::QualType &m1p,
143 const clang::QualType &m2p) {
144 return context.hasSameType(m1p.getUnqualifiedType(),
145 m2p.getUnqualifiedType());
146 };
147
148 // FIXME: In C++14 and later, we can just pass m2Type->param_type_end()
149 // as a fourth parameter to std::equal().
150 return (m1->getNumParams() != m2->getNumParams()) ||
151 !std::equal(m1Type->param_type_begin(), m1Type->param_type_end(),
152 m2Type->param_type_begin(), compareArgTypes);
153}
154
155// If decl is a virtual method, walk the base classes looking for methods that
156// decl overrides. This table of overridden methods is used by IRGen to
157// determine the vtable layout for decl's parent class.
158void addOverridesForMethod(clang::CXXMethodDecl *decl) {
159 if (!decl->isVirtual())
160 return;
161
162 clang::CXXBasePaths paths;
163
164 auto find_overridden_methods =
165 [decl](const clang::CXXBaseSpecifier *specifier,
166 clang::CXXBasePath &path) {
167 if (auto *base_record = llvm::dyn_cast<clang::CXXRecordDecl>(
168 specifier->getType()->getAs<clang::RecordType>()->getDecl())) {
169
170 clang::DeclarationName name = decl->getDeclName();
171
172 // If this is a destructor, check whether the base class destructor is
173 // virtual.
174 if (name.getNameKind() == clang::DeclarationName::CXXDestructorName)
175 if (auto *baseDtorDecl = base_record->getDestructor()) {
176 if (baseDtorDecl->isVirtual()) {
177 path.Decls = baseDtorDecl;
178 return true;
179 } else
180 return false;
181 }
182
183 // Otherwise, search for name in the base class.
184 for (path.Decls = base_record->lookup(name); !path.Decls.empty();
185 path.Decls = path.Decls.slice(1)) {
186 if (auto *method_decl =
187 llvm::dyn_cast<clang::CXXMethodDecl>(path.Decls.front()))
188 if (method_decl->isVirtual() && !isOverload(decl, method_decl)) {
189 path.Decls = method_decl;
190 return true;
191 }
192 }
193 }
194
195 return false;
196 };
197
198 if (decl->getParent()->lookupInBases(find_overridden_methods, paths)) {
199 for (auto *overridden_decl : paths.found_decls())
200 decl->addOverriddenMethod(
201 llvm::cast<clang::CXXMethodDecl>(overridden_decl));
202 }
203}
Greg Clayton56939cb2015-09-17 22:23:34 +0000204}
205
Aleksandr Urakov1dc51db2018-11-12 16:23:50 +0000206static lldb::addr_t GetVTableAddress(Process &process,
207 VTableContextBase &vtable_ctx,
208 ValueObject &valobj,
209 const ASTRecordLayout &record_layout) {
210 // Retrieve type info
211 CompilerType pointee_type;
212 CompilerType this_type(valobj.GetCompilerType());
213 uint32_t type_info = this_type.GetTypeInfo(&pointee_type);
214 if (!type_info)
215 return LLDB_INVALID_ADDRESS;
216
217 // Check if it's a pointer or reference
218 bool ptr_or_ref = false;
219 if (type_info & (eTypeIsPointer | eTypeIsReference)) {
220 ptr_or_ref = true;
221 type_info = pointee_type.GetTypeInfo();
222 }
223
224 // We process only C++ classes
225 const uint32_t cpp_class = eTypeIsClass | eTypeIsCPlusPlus;
226 if ((type_info & cpp_class) != cpp_class)
227 return LLDB_INVALID_ADDRESS;
228
229 // Calculate offset to VTable pointer
230 lldb::offset_t vbtable_ptr_offset =
231 vtable_ctx.isMicrosoft() ? record_layout.getVBPtrOffset().getQuantity()
232 : 0;
233
234 if (ptr_or_ref) {
235 // We have a pointer / ref to object, so read
236 // VTable pointer from process memory
237
238 if (valobj.GetAddressTypeOfChildren() != eAddressTypeLoad)
239 return LLDB_INVALID_ADDRESS;
240
241 auto vbtable_ptr_addr = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
242 if (vbtable_ptr_addr == LLDB_INVALID_ADDRESS)
243 return LLDB_INVALID_ADDRESS;
244
245 vbtable_ptr_addr += vbtable_ptr_offset;
246
247 Status err;
248 return process.ReadPointerFromMemory(vbtable_ptr_addr, err);
249 }
250
251 // We have an object already read from process memory,
252 // so just extract VTable pointer from it
253
254 DataExtractor data;
255 Status err;
256 auto size = valobj.GetData(data, err);
257 if (err.Fail() || vbtable_ptr_offset + data.GetAddressByteSize() > size)
258 return LLDB_INVALID_ADDRESS;
259
260 return data.GetPointer(&vbtable_ptr_offset);
261}
262
263static int64_t ReadVBaseOffsetFromVTable(Process &process,
264 VTableContextBase &vtable_ctx,
265 lldb::addr_t vtable_ptr,
266 const CXXRecordDecl *cxx_record_decl,
267 const CXXRecordDecl *base_class_decl) {
268 if (vtable_ctx.isMicrosoft()) {
269 clang::MicrosoftVTableContext &msoft_vtable_ctx =
270 static_cast<clang::MicrosoftVTableContext &>(vtable_ctx);
271
272 // Get the index into the virtual base table. The
273 // index is the index in uint32_t from vbtable_ptr
274 const unsigned vbtable_index =
275 msoft_vtable_ctx.getVBTableIndex(cxx_record_decl, base_class_decl);
276 const lldb::addr_t base_offset_addr = vtable_ptr + vbtable_index * 4;
277 Status err;
278 return process.ReadSignedIntegerFromMemory(base_offset_addr, 4, INT64_MAX,
279 err);
280 }
281
282 clang::ItaniumVTableContext &itanium_vtable_ctx =
283 static_cast<clang::ItaniumVTableContext &>(vtable_ctx);
284
285 clang::CharUnits base_offset_offset =
286 itanium_vtable_ctx.getVirtualBaseOffsetOffset(cxx_record_decl,
287 base_class_decl);
288 const lldb::addr_t base_offset_addr =
289 vtable_ptr + base_offset_offset.getQuantity();
290 const uint32_t base_offset_size = process.GetAddressByteSize();
291 Status err;
292 return process.ReadSignedIntegerFromMemory(base_offset_addr, base_offset_size,
293 INT64_MAX, err);
294}
295
296static bool GetVBaseBitOffset(VTableContextBase &vtable_ctx,
297 ValueObject &valobj,
298 const ASTRecordLayout &record_layout,
299 const CXXRecordDecl *cxx_record_decl,
300 const CXXRecordDecl *base_class_decl,
301 int32_t &bit_offset) {
302 ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
303 Process *process = exe_ctx.GetProcessPtr();
304 if (!process)
305 return false;
306
307 lldb::addr_t vtable_ptr =
308 GetVTableAddress(*process, vtable_ctx, valobj, record_layout);
309 if (vtable_ptr == LLDB_INVALID_ADDRESS)
310 return false;
311
312 auto base_offset = ReadVBaseOffsetFromVTable(
313 *process, vtable_ctx, vtable_ptr, cxx_record_decl, base_class_decl);
314 if (base_offset == INT64_MAX)
315 return false;
316
317 bit_offset = base_offset * 8;
318
319 return true;
320}
321
Kate Stoneb9c1b512016-09-06 20:57:50 +0000322typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext *>
323 ClangASTMap;
Enrico Granata5d84a692014-08-19 21:46:37 +0000324
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325static ClangASTMap &GetASTMap() {
326 static ClangASTMap *g_map_ptr = nullptr;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000327 static llvm::once_flag g_once_flag;
328 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
330 });
331 return *g_map_ptr;
Enrico Granata5d84a692014-08-19 21:46:37 +0000332}
333
Davide Italiano7e3ef4d2018-03-20 19:46:32 +0000334bool ClangASTContext::IsOperator(const char *name,
335 clang::OverloadedOperatorKind &op_kind) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336 if (name == nullptr || name[0] == '\0')
337 return false;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000338
339#define OPERATOR_PREFIX "operator"
340#define OPERATOR_PREFIX_LENGTH (sizeof(OPERATOR_PREFIX) - 1)
341
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342 const char *post_op_name = nullptr;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000343
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344 bool no_space = true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000345
Kate Stoneb9c1b512016-09-06 20:57:50 +0000346 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
347 return false;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000348
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349 post_op_name = name + OPERATOR_PREFIX_LENGTH;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000350
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351 if (post_op_name[0] == ' ') {
352 post_op_name++;
353 no_space = false;
354 }
Pavel Labath1ac2b202016-08-15 14:32:32 +0000355
356#undef OPERATOR_PREFIX
357#undef OPERATOR_PREFIX_LENGTH
358
Adrian Prantl05097242018-04-30 16:49:04 +0000359 // This is an operator, set the overloaded operator kind to invalid in case
360 // this is a conversion operator...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361 op_kind = clang::NUM_OVERLOADED_OPERATORS;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000362
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363 switch (post_op_name[0]) {
364 default:
365 if (no_space)
366 return false;
367 break;
368 case 'n':
369 if (no_space)
370 return false;
371 if (strcmp(post_op_name, "new") == 0)
372 op_kind = clang::OO_New;
373 else if (strcmp(post_op_name, "new[]") == 0)
374 op_kind = clang::OO_Array_New;
375 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000376
Kate Stoneb9c1b512016-09-06 20:57:50 +0000377 case 'd':
378 if (no_space)
379 return false;
380 if (strcmp(post_op_name, "delete") == 0)
381 op_kind = clang::OO_Delete;
382 else if (strcmp(post_op_name, "delete[]") == 0)
383 op_kind = clang::OO_Array_Delete;
384 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000385
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386 case '+':
387 if (post_op_name[1] == '\0')
388 op_kind = clang::OO_Plus;
389 else if (post_op_name[2] == '\0') {
390 if (post_op_name[1] == '=')
391 op_kind = clang::OO_PlusEqual;
392 else if (post_op_name[1] == '+')
393 op_kind = clang::OO_PlusPlus;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000394 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000396
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397 case '-':
398 if (post_op_name[1] == '\0')
399 op_kind = clang::OO_Minus;
400 else if (post_op_name[2] == '\0') {
401 switch (post_op_name[1]) {
402 case '=':
403 op_kind = clang::OO_MinusEqual;
404 break;
405 case '-':
406 op_kind = clang::OO_MinusMinus;
407 break;
408 case '>':
409 op_kind = clang::OO_Arrow;
410 break;
411 }
412 } else if (post_op_name[3] == '\0') {
413 if (post_op_name[2] == '*')
414 op_kind = clang::OO_ArrowStar;
415 break;
416 }
417 break;
418
419 case '*':
420 if (post_op_name[1] == '\0')
421 op_kind = clang::OO_Star;
422 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
423 op_kind = clang::OO_StarEqual;
424 break;
425
426 case '/':
427 if (post_op_name[1] == '\0')
428 op_kind = clang::OO_Slash;
429 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
430 op_kind = clang::OO_SlashEqual;
431 break;
432
433 case '%':
434 if (post_op_name[1] == '\0')
435 op_kind = clang::OO_Percent;
436 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
437 op_kind = clang::OO_PercentEqual;
438 break;
439
440 case '^':
441 if (post_op_name[1] == '\0')
442 op_kind = clang::OO_Caret;
443 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
444 op_kind = clang::OO_CaretEqual;
445 break;
446
447 case '&':
448 if (post_op_name[1] == '\0')
449 op_kind = clang::OO_Amp;
450 else if (post_op_name[2] == '\0') {
451 switch (post_op_name[1]) {
452 case '=':
453 op_kind = clang::OO_AmpEqual;
454 break;
455 case '&':
456 op_kind = clang::OO_AmpAmp;
457 break;
458 }
459 }
460 break;
461
462 case '|':
463 if (post_op_name[1] == '\0')
464 op_kind = clang::OO_Pipe;
465 else if (post_op_name[2] == '\0') {
466 switch (post_op_name[1]) {
467 case '=':
468 op_kind = clang::OO_PipeEqual;
469 break;
470 case '|':
471 op_kind = clang::OO_PipePipe;
472 break;
473 }
474 }
475 break;
476
477 case '~':
478 if (post_op_name[1] == '\0')
479 op_kind = clang::OO_Tilde;
480 break;
481
482 case '!':
483 if (post_op_name[1] == '\0')
484 op_kind = clang::OO_Exclaim;
485 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
486 op_kind = clang::OO_ExclaimEqual;
487 break;
488
489 case '=':
490 if (post_op_name[1] == '\0')
491 op_kind = clang::OO_Equal;
492 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
493 op_kind = clang::OO_EqualEqual;
494 break;
495
496 case '<':
497 if (post_op_name[1] == '\0')
498 op_kind = clang::OO_Less;
499 else if (post_op_name[2] == '\0') {
500 switch (post_op_name[1]) {
501 case '<':
502 op_kind = clang::OO_LessLess;
503 break;
504 case '=':
505 op_kind = clang::OO_LessEqual;
506 break;
507 }
508 } else if (post_op_name[3] == '\0') {
509 if (post_op_name[2] == '=')
510 op_kind = clang::OO_LessLessEqual;
511 }
512 break;
513
514 case '>':
515 if (post_op_name[1] == '\0')
516 op_kind = clang::OO_Greater;
517 else if (post_op_name[2] == '\0') {
518 switch (post_op_name[1]) {
519 case '>':
520 op_kind = clang::OO_GreaterGreater;
521 break;
522 case '=':
523 op_kind = clang::OO_GreaterEqual;
524 break;
525 }
526 } else if (post_op_name[1] == '>' && post_op_name[2] == '=' &&
527 post_op_name[3] == '\0') {
528 op_kind = clang::OO_GreaterGreaterEqual;
529 }
530 break;
531
532 case ',':
533 if (post_op_name[1] == '\0')
534 op_kind = clang::OO_Comma;
535 break;
536
537 case '(':
538 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
539 op_kind = clang::OO_Call;
540 break;
541
542 case '[':
543 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
544 op_kind = clang::OO_Subscript;
545 break;
546 }
547
548 return true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000549}
Enrico Granata5d84a692014-08-19 21:46:37 +0000550
Greg Clayton57ee3062013-07-11 22:46:58 +0000551clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +0000552ClangASTContext::ConvertAccessTypeToAccessSpecifier(AccessType access) {
553 switch (access) {
554 default:
555 break;
556 case eAccessNone:
Greg Clayton8cf05932010-07-22 18:30:50 +0000557 return AS_none;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000558 case eAccessPublic:
559 return AS_public;
560 case eAccessPrivate:
561 return AS_private;
562 case eAccessProtected:
563 return AS_protected;
564 }
565 return AS_none;
Greg Clayton8cf05932010-07-22 18:30:50 +0000566}
567
Kate Stoneb9c1b512016-09-06 20:57:50 +0000568static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) {
569 // FIXME: Cleanup per-file based stuff.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570
Adrian Prantl05097242018-04-30 16:49:04 +0000571 // Set some properties which depend solely on the input kind; it would be
572 // nice to move these to the language standard, and have the driver resolve
573 // the input kind + language standard.
Richard Smith8186cd42017-04-26 22:10:53 +0000574 if (IK.getLanguage() == InputKind::Asm) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000575 Opts.AsmPreprocessor = 1;
Richard Smith8186cd42017-04-26 22:10:53 +0000576 } else if (IK.isObjectiveC()) {
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000577 Opts.ObjC = 1;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000578 }
579
580 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
581
582 if (LangStd == LangStandard::lang_unspecified) {
583 // Based on the base language, pick one.
Richard Smith8186cd42017-04-26 22:10:53 +0000584 switch (IK.getLanguage()) {
585 case InputKind::Unknown:
586 case InputKind::LLVM_IR:
587 case InputKind::RenderScript:
David Blaikiea322f362017-01-06 00:38:06 +0000588 llvm_unreachable("Invalid input kind!");
Richard Smith8186cd42017-04-26 22:10:53 +0000589 case InputKind::OpenCL:
Pavel Labath47168542017-04-27 08:49:19 +0000590 LangStd = LangStandard::lang_opencl10;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000591 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000592 case InputKind::CUDA:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000593 LangStd = LangStandard::lang_cuda;
594 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000595 case InputKind::Asm:
596 case InputKind::C:
597 case InputKind::ObjC:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000598 LangStd = LangStandard::lang_gnu99;
599 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000600 case InputKind::CXX:
601 case InputKind::ObjCXX:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602 LangStd = LangStandard::lang_gnucxx98;
603 break;
Benjamin Kramer0d97c222018-04-25 13:22:47 +0000604 case InputKind::HIP:
605 LangStd = LangStandard::lang_hip;
606 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000607 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000608 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000609
Kate Stoneb9c1b512016-09-06 20:57:50 +0000610 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
611 Opts.LineComment = Std.hasLineComments();
612 Opts.C99 = Std.isC99();
613 Opts.CPlusPlus = Std.isCPlusPlus();
614 Opts.CPlusPlus11 = Std.isCPlusPlus11();
615 Opts.Digraphs = Std.hasDigraphs();
616 Opts.GNUMode = Std.isGNUMode();
617 Opts.GNUInline = !Std.isC99();
618 Opts.HexFloats = Std.hasHexFloats();
619 Opts.ImplicitInt = Std.hasImplicitInt();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620
Kate Stoneb9c1b512016-09-06 20:57:50 +0000621 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623 // OpenCL has some additional defaults.
Pavel Labath47168542017-04-27 08:49:19 +0000624 if (LangStd == LangStandard::lang_opencl10) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625 Opts.OpenCL = 1;
626 Opts.AltiVec = 1;
627 Opts.CXXOperatorNames = 1;
628 Opts.LaxVectorConversions = 1;
629 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000630
Kate Stoneb9c1b512016-09-06 20:57:50 +0000631 // OpenCL and C++ both have bool, true, false keywords.
632 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000633
Kate Stoneb9c1b512016-09-06 20:57:50 +0000634 Opts.setValueVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635
Adrian Prantl05097242018-04-30 16:49:04 +0000636 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs is
637 // specified, or -std is set to a conforming mode.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000638 Opts.Trigraphs = !Opts.GNUMode;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000639 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000640 Opts.OptimizeSize = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000641
Kate Stoneb9c1b512016-09-06 20:57:50 +0000642 // FIXME: Eliminate this dependency.
643 // unsigned Opt =
644 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
645 // Opts.Optimize = Opt != 0;
646 unsigned Opt = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000647
Kate Stoneb9c1b512016-09-06 20:57:50 +0000648 // This is the __NO_INLINE__ define, which just depends on things like the
649 // optimization level and -fno-inline, not actually whether the backend has
650 // inlining enabled.
651 //
652 // FIXME: This is affected by other options (-fno-inline).
653 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000654}
655
Kate Stoneb9c1b512016-09-06 20:57:50 +0000656ClangASTContext::ClangASTContext(const char *target_triple)
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000657 : TypeSystem(TypeSystem::eKindClang), m_target_triple(), m_ast_up(),
658 m_language_options_up(), m_source_manager_up(), m_diagnostics_engine_up(),
659 m_target_options_rp(), m_target_info_up(), m_identifier_table_up(),
660 m_selector_table_up(), m_builtins_up(), m_callback_tag_decl(nullptr),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000661 m_callback_objc_decl(nullptr), m_callback_baton(nullptr),
662 m_pointer_byte_size(0), m_ast_owned(false) {
663 if (target_triple && target_triple[0])
664 SetTargetTriple(target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000665}
666
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000667// Destructor
Kate Stoneb9c1b512016-09-06 20:57:50 +0000668ClangASTContext::~ClangASTContext() { Finalize(); }
669
670ConstString ClangASTContext::GetPluginNameStatic() {
671 return ConstString("clang");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000672}
673
Kate Stoneb9c1b512016-09-06 20:57:50 +0000674ConstString ClangASTContext::GetPluginName() {
675 return ClangASTContext::GetPluginNameStatic();
Greg Clayton56939cb2015-09-17 22:23:34 +0000676}
677
Kate Stoneb9c1b512016-09-06 20:57:50 +0000678uint32_t ClangASTContext::GetPluginVersion() { return 1; }
Greg Clayton56939cb2015-09-17 22:23:34 +0000679
Kate Stoneb9c1b512016-09-06 20:57:50 +0000680lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
681 lldb_private::Module *module,
682 Target *target) {
683 if (ClangASTContextSupportsLanguage(language)) {
684 ArchSpec arch;
685 if (module)
686 arch = module->GetArchitecture();
687 else if (target)
688 arch = target->GetArchitecture();
Greg Clayton56939cb2015-09-17 22:23:34 +0000689
Kate Stoneb9c1b512016-09-06 20:57:50 +0000690 if (arch.IsValid()) {
691 ArchSpec fixed_arch = arch;
692 // LLVM wants this to be set to iOS or MacOSX; if we're working on
693 // a bare-boards type image, change the triple for llvm's benefit.
694 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
695 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) {
696 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
697 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
698 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) {
699 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
700 } else {
701 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
Greg Clayton56939cb2015-09-17 22:23:34 +0000702 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000703 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000704
Kate Stoneb9c1b512016-09-06 20:57:50 +0000705 if (module) {
706 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
707 if (ast_sp) {
708 ast_sp->SetArchitecture(fixed_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000709 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000710 return ast_sp;
711 } else if (target && target->IsValid()) {
712 std::shared_ptr<ClangASTContextForExpressions> ast_sp(
713 new ClangASTContextForExpressions(*target));
714 if (ast_sp) {
715 ast_sp->SetArchitecture(fixed_arch);
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000716 ast_sp->m_scratch_ast_source_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000717 new ClangASTSource(target->shared_from_this()));
Sean Callanan68e44232017-09-28 20:20:25 +0000718 lldbassert(ast_sp->getFileManager());
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000719 ast_sp->m_scratch_ast_source_up->InstallASTContext(
Sean Callanan68e44232017-09-28 20:20:25 +0000720 *ast_sp->getASTContext(), *ast_sp->getFileManager(), true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000721 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000722 ast_sp->m_scratch_ast_source_up->CreateProxy());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000723 ast_sp->SetExternalSource(proxy_ast_source);
724 return ast_sp;
725 }
726 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000728 }
729 return lldb::TypeSystemSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000730}
731
Kate Stoneb9c1b512016-09-06 20:57:50 +0000732void ClangASTContext::EnumerateSupportedLanguages(
733 std::set<lldb::LanguageType> &languages_for_types,
734 std::set<lldb::LanguageType> &languages_for_expressions) {
735 static std::vector<lldb::LanguageType> s_supported_languages_for_types(
736 {lldb::eLanguageTypeC89, lldb::eLanguageTypeC, lldb::eLanguageTypeC11,
737 lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeC99,
738 lldb::eLanguageTypeObjC, lldb::eLanguageTypeObjC_plus_plus,
739 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
740 lldb::eLanguageTypeC11, lldb::eLanguageTypeC_plus_plus_14});
741
742 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions(
743 {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC_plus_plus,
744 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
745 lldb::eLanguageTypeC_plus_plus_14});
746
747 languages_for_types.insert(s_supported_languages_for_types.begin(),
748 s_supported_languages_for_types.end());
749 languages_for_expressions.insert(
750 s_supported_languages_for_expressions.begin(),
751 s_supported_languages_for_expressions.end());
Enrico Granata5d84a692014-08-19 21:46:37 +0000752}
753
Kate Stoneb9c1b512016-09-06 20:57:50 +0000754void ClangASTContext::Initialize() {
755 PluginManager::RegisterPlugin(GetPluginNameStatic(),
756 "clang base AST context plug-in",
757 CreateInstance, EnumerateSupportedLanguages);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000758}
759
Kate Stoneb9c1b512016-09-06 20:57:50 +0000760void ClangASTContext::Terminate() {
761 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000762}
763
Kate Stoneb9c1b512016-09-06 20:57:50 +0000764void ClangASTContext::Finalize() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000765 if (m_ast_up) {
766 GetASTMap().Erase(m_ast_up.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000767 if (!m_ast_owned)
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000768 m_ast_up.release();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000769 }
770
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000771 m_builtins_up.reset();
772 m_selector_table_up.reset();
773 m_identifier_table_up.reset();
774 m_target_info_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000775 m_target_options_rp.reset();
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000776 m_diagnostics_engine_up.reset();
777 m_source_manager_up.reset();
778 m_language_options_up.reset();
779 m_ast_up.reset();
780 m_scratch_ast_source_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000781}
782
783void ClangASTContext::Clear() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000784 m_ast_up.reset();
785 m_language_options_up.reset();
786 m_source_manager_up.reset();
787 m_diagnostics_engine_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000788 m_target_options_rp.reset();
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000789 m_target_info_up.reset();
790 m_identifier_table_up.reset();
791 m_selector_table_up.reset();
792 m_builtins_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000793 m_pointer_byte_size = 0;
794}
795
Raphael Isemannf74a4c12019-04-30 08:41:35 +0000796void ClangASTContext::setSema(Sema *s) {
797 // Ensure that the new sema actually belongs to our ASTContext.
798 assert(s == nullptr || &s->getASTContext() == m_ast_up.get());
799 m_sema = s;
800}
801
Kate Stoneb9c1b512016-09-06 20:57:50 +0000802const char *ClangASTContext::GetTargetTriple() {
803 return m_target_triple.c_str();
804}
805
806void ClangASTContext::SetTargetTriple(const char *target_triple) {
807 Clear();
808 m_target_triple.assign(target_triple);
809}
810
811void ClangASTContext::SetArchitecture(const ArchSpec &arch) {
812 SetTargetTriple(arch.GetTriple().str().c_str());
813}
814
815bool ClangASTContext::HasExternalSource() {
816 ASTContext *ast = getASTContext();
817 if (ast)
818 return ast->getExternalSource() != nullptr;
819 return false;
820}
821
822void ClangASTContext::SetExternalSource(
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000823 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000824 ASTContext *ast = getASTContext();
825 if (ast) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000826 ast->setExternalSource(ast_source_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000827 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000828 }
829}
830
831void ClangASTContext::RemoveExternalSource() {
832 ASTContext *ast = getASTContext();
833
834 if (ast) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000835 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_up;
836 ast->setExternalSource(empty_ast_source_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000837 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000838 }
839}
840
841void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) {
842 if (!m_ast_owned) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000843 m_ast_up.release();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000844 }
845 m_ast_owned = false;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000846 m_ast_up.reset(ast_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000847 GetASTMap().Insert(ast_ctx, this);
848}
849
850ASTContext *ClangASTContext::getASTContext() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000851 if (m_ast_up == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000852 m_ast_owned = true;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000853 m_ast_up.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000854 *getIdentifierTable(), *getSelectorTable(),
855 *getBuiltinContext()));
856
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000857 m_ast_up->getDiagnostics().setClient(getDiagnosticConsumer(), false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000858
859 // This can be NULL if we don't know anything about the architecture or if
Adrian Prantl05097242018-04-30 16:49:04 +0000860 // the target for an architecture isn't enabled in the llvm/clang that we
861 // built
Kate Stoneb9c1b512016-09-06 20:57:50 +0000862 TargetInfo *target_info = getTargetInfo();
863 if (target_info)
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000864 m_ast_up->InitBuiltinTypes(*target_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000865
866 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000867 m_ast_up->getTranslationUnitDecl()->setHasExternalLexicalStorage();
868 // m_ast_up->getTranslationUnitDecl()->setHasExternalVisibleStorage();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000869 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000870
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000871 GetASTMap().Insert(m_ast_up.get(), this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000872
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000873 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_up(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000874 new ClangExternalASTSourceCallbacks(
875 ClangASTContext::CompleteTagDecl,
876 ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
877 ClangASTContext::LayoutRecordType, this));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000878 SetExternalSource(ast_source_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000879 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000880 return m_ast_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000881}
882
Kate Stoneb9c1b512016-09-06 20:57:50 +0000883ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
884 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
885 return clang_ast;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000886}
887
Kate Stoneb9c1b512016-09-06 20:57:50 +0000888Builtin::Context *ClangASTContext::getBuiltinContext() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000889 if (m_builtins_up == nullptr)
890 m_builtins_up.reset(new Builtin::Context());
891 return m_builtins_up.get();
Sean Callanan79439e82010-11-18 02:56:27 +0000892}
893
Kate Stoneb9c1b512016-09-06 20:57:50 +0000894IdentifierTable *ClangASTContext::getIdentifierTable() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000895 if (m_identifier_table_up == nullptr)
896 m_identifier_table_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000897 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000898 return m_identifier_table_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000899}
900
Kate Stoneb9c1b512016-09-06 20:57:50 +0000901LangOptions *ClangASTContext::getLanguageOptions() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000902 if (m_language_options_up == nullptr) {
903 m_language_options_up.reset(new LangOptions());
904 ParseLangArgs(*m_language_options_up, InputKind::ObjCXX, GetTargetTriple());
905 // InitializeLangOptions(*m_language_options_up, InputKind::ObjCXX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000906 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000907 return m_language_options_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000908}
909
Kate Stoneb9c1b512016-09-06 20:57:50 +0000910SelectorTable *ClangASTContext::getSelectorTable() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000911 if (m_selector_table_up == nullptr)
912 m_selector_table_up.reset(new SelectorTable());
913 return m_selector_table_up.get();
Greg Claytonfe689042015-11-10 17:47:04 +0000914}
915
Kate Stoneb9c1b512016-09-06 20:57:50 +0000916clang::FileManager *ClangASTContext::getFileManager() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000917 if (m_file_manager_up == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000918 clang::FileSystemOptions file_system_options;
Jonas Devlieghere9764b652019-02-18 20:31:18 +0000919 m_file_manager_up.reset(new clang::FileManager(
920 file_system_options, FileSystem::Instance().GetVirtualFileSystem()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000921 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000922 return m_file_manager_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000923}
924
925clang::SourceManager *ClangASTContext::getSourceManager() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000926 if (m_source_manager_up == nullptr)
927 m_source_manager_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000928 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000929 return m_source_manager_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000930}
931
932clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000933 if (m_diagnostics_engine_up == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000934 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000935 m_diagnostics_engine_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000936 new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
937 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000938 return m_diagnostics_engine_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000939}
940
941clang::MangleContext *ClangASTContext::getMangleContext() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000942 if (m_mangle_ctx_up == nullptr)
943 m_mangle_ctx_up.reset(getASTContext()->createMangleContext());
944 return m_mangle_ctx_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000945}
946
947class NullDiagnosticConsumer : public DiagnosticConsumer {
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000948public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000949 NullDiagnosticConsumer() {
950 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
951 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000952
Kate Stoneb9c1b512016-09-06 20:57:50 +0000953 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
954 const clang::Diagnostic &info) {
955 if (m_log) {
956 llvm::SmallVector<char, 32> diag_str(10);
957 info.FormatDiagnostic(diag_str);
958 diag_str.push_back('\0');
959 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000960 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000961 }
962
963 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
964 return new NullDiagnosticConsumer();
965 }
966
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000967private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000968 Log *m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000969};
970
Kate Stoneb9c1b512016-09-06 20:57:50 +0000971DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000972 if (m_diagnostic_consumer_up == nullptr)
973 m_diagnostic_consumer_up.reset(new NullDiagnosticConsumer);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000974
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000975 return m_diagnostic_consumer_up.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000976}
977
Kate Stoneb9c1b512016-09-06 20:57:50 +0000978std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000979 if (m_target_options_rp == nullptr && !m_target_triple.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000980 m_target_options_rp = std::make_shared<clang::TargetOptions>();
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000981 if (m_target_options_rp != nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000982 m_target_options_rp->Triple = m_target_triple;
983 }
984 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000985}
986
Kate Stoneb9c1b512016-09-06 20:57:50 +0000987TargetInfo *ClangASTContext::getTargetInfo() {
988 // target_triple should be something like "x86_64-apple-macosx"
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000989 if (m_target_info_up == nullptr && !m_target_triple.empty())
990 m_target_info_up.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000991 getTargetOptions()));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000992 return m_target_info_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000993}
994
995#pragma mark Basic Types
996
Kate Stoneb9c1b512016-09-06 20:57:50 +0000997static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
998 ASTContext *ast, QualType qual_type) {
999 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001000 return qual_type_bit_size == bit_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001001}
Greg Clayton56939cb2015-09-17 22:23:34 +00001002
Greg Claytona1e5dc82015-08-11 22:53:00 +00001003CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00001004ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
1005 size_t bit_size) {
1006 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
1007 getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001008}
1009
Kate Stoneb9c1b512016-09-06 20:57:50 +00001010CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
1011 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
1012 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001013 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001014 switch (encoding) {
1015 case eEncodingInvalid:
1016 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1017 return CompilerType(ast, ast->VoidPtrTy);
1018 break;
1019
1020 case eEncodingUint:
1021 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1022 return CompilerType(ast, ast->UnsignedCharTy);
1023 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1024 return CompilerType(ast, ast->UnsignedShortTy);
1025 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1026 return CompilerType(ast, ast->UnsignedIntTy);
1027 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1028 return CompilerType(ast, ast->UnsignedLongTy);
1029 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1030 return CompilerType(ast, ast->UnsignedLongLongTy);
1031 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1032 return CompilerType(ast, ast->UnsignedInt128Ty);
1033 break;
1034
1035 case eEncodingSint:
1036 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1037 return CompilerType(ast, ast->SignedCharTy);
1038 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1039 return CompilerType(ast, ast->ShortTy);
1040 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1041 return CompilerType(ast, ast->IntTy);
1042 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1043 return CompilerType(ast, ast->LongTy);
1044 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1045 return CompilerType(ast, ast->LongLongTy);
1046 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1047 return CompilerType(ast, ast->Int128Ty);
1048 break;
1049
1050 case eEncodingIEEE754:
1051 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1052 return CompilerType(ast, ast->FloatTy);
1053 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1054 return CompilerType(ast, ast->DoubleTy);
1055 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1056 return CompilerType(ast, ast->LongDoubleTy);
1057 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1058 return CompilerType(ast, ast->HalfTy);
1059 break;
1060
1061 case eEncodingVector:
1062 // Sanity check that bit_size is a multiple of 8's.
1063 if (bit_size && !(bit_size & 0x7u))
1064 return CompilerType(
1065 ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8));
1066 break;
1067 }
1068
1069 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001070}
1071
Greg Clayton57ee3062013-07-11 22:46:58 +00001072lldb::BasicType
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001073ClangASTContext::GetBasicTypeEnumeration(ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001074 if (name) {
1075 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
1076 static TypeNameToBasicTypeMap g_type_map;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +00001077 static llvm::once_flag g_once_flag;
1078 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001079 // "void"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001080 g_type_map.Append(ConstString("void"), eBasicTypeVoid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001081
1082 // "char"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001083 g_type_map.Append(ConstString("char"), eBasicTypeChar);
1084 g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar);
1085 g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar);
1086 g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar);
1087 g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar);
1088 g_type_map.Append(ConstString("unsigned wchar_t"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001089 eBasicTypeUnsignedWChar);
1090 // "short"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001091 g_type_map.Append(ConstString("short"), eBasicTypeShort);
1092 g_type_map.Append(ConstString("short int"), eBasicTypeShort);
1093 g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort);
1094 g_type_map.Append(ConstString("unsigned short int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001095 eBasicTypeUnsignedShort);
1096
1097 // "int"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001098 g_type_map.Append(ConstString("int"), eBasicTypeInt);
1099 g_type_map.Append(ConstString("signed int"), eBasicTypeInt);
1100 g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt);
1101 g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102
1103 // "long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001104 g_type_map.Append(ConstString("long"), eBasicTypeLong);
1105 g_type_map.Append(ConstString("long int"), eBasicTypeLong);
1106 g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong);
1107 g_type_map.Append(ConstString("unsigned long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001108 eBasicTypeUnsignedLong);
1109
1110 // "long long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001111 g_type_map.Append(ConstString("long long"), eBasicTypeLongLong);
1112 g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong);
1113 g_type_map.Append(ConstString("unsigned long long"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001114 eBasicTypeUnsignedLongLong);
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001115 g_type_map.Append(ConstString("unsigned long long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001116 eBasicTypeUnsignedLongLong);
1117
1118 // "int128"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001119 g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128);
1120 g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001121
1122 // Miscellaneous
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001123 g_type_map.Append(ConstString("bool"), eBasicTypeBool);
1124 g_type_map.Append(ConstString("float"), eBasicTypeFloat);
1125 g_type_map.Append(ConstString("double"), eBasicTypeDouble);
1126 g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble);
1127 g_type_map.Append(ConstString("id"), eBasicTypeObjCID);
1128 g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel);
1129 g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001130 g_type_map.Sort();
1131 });
1132
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001133 return g_type_map.Find(name, eBasicTypeInvalid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001134 }
1135 return eBasicTypeInvalid;
Greg Clayton57ee3062013-07-11 22:46:58 +00001136}
1137
Kate Stoneb9c1b512016-09-06 20:57:50 +00001138CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001139 ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001140 if (ast) {
1141 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
1142 return ClangASTContext::GetBasicType(ast, basic_type);
1143 }
1144 return CompilerType();
1145}
1146
1147uint32_t ClangASTContext::GetPointerByteSize() {
1148 if (m_pointer_byte_size == 0)
Adrian Prantld963a7c2019-01-15 18:07:52 +00001149 if (auto size = GetBasicType(lldb::eBasicTypeVoid)
1150 .GetPointerType()
1151 .GetByteSize(nullptr))
1152 m_pointer_byte_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001153 return m_pointer_byte_size;
1154}
1155
1156CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
1157 return GetBasicType(getASTContext(), basic_type);
1158}
1159
1160CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1161 lldb::BasicType basic_type) {
1162 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001163 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001164 lldb::opaque_compiler_type_t clang_type =
1165 GetOpaqueCompilerType(ast, basic_type);
1166
1167 if (clang_type)
1168 return CompilerType(GetASTContext(ast), clang_type);
1169 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +00001170}
1171
Kate Stoneb9c1b512016-09-06 20:57:50 +00001172CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
1173 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
1174 ASTContext *ast = getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001175
Kate Stoneb9c1b512016-09-06 20:57:50 +00001176#define streq(a, b) strcmp(a, b) == 0
1177 assert(ast != nullptr);
1178 if (ast) {
1179 switch (dw_ate) {
1180 default:
1181 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001182
Kate Stoneb9c1b512016-09-06 20:57:50 +00001183 case DW_ATE_address:
1184 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1185 return CompilerType(ast, ast->VoidPtrTy);
1186 break;
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001187
Kate Stoneb9c1b512016-09-06 20:57:50 +00001188 case DW_ATE_boolean:
1189 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
1190 return CompilerType(ast, ast->BoolTy);
1191 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1192 return CompilerType(ast, ast->UnsignedCharTy);
1193 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1194 return CompilerType(ast, ast->UnsignedShortTy);
1195 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1196 return CompilerType(ast, ast->UnsignedIntTy);
1197 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001198
Kate Stoneb9c1b512016-09-06 20:57:50 +00001199 case DW_ATE_lo_user:
1200 // This has been seen to mean DW_AT_complex_integer
1201 if (type_name) {
1202 if (::strstr(type_name, "complex")) {
1203 CompilerType complex_int_clang_type =
1204 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1205 bit_size / 2);
1206 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1207 complex_int_clang_type)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001208 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001209 }
1210 break;
1211
1212 case DW_ATE_complex_float:
1213 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
1214 return CompilerType(ast, ast->FloatComplexTy);
1215 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
1216 return CompilerType(ast, ast->DoubleComplexTy);
1217 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
1218 return CompilerType(ast, ast->LongDoubleComplexTy);
1219 else {
1220 CompilerType complex_float_clang_type =
1221 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1222 bit_size / 2);
1223 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1224 complex_float_clang_type)));
1225 }
1226 break;
1227
1228 case DW_ATE_float:
1229 if (streq(type_name, "float") &&
1230 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1231 return CompilerType(ast, ast->FloatTy);
1232 if (streq(type_name, "double") &&
1233 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1234 return CompilerType(ast, ast->DoubleTy);
1235 if (streq(type_name, "long double") &&
1236 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1237 return CompilerType(ast, ast->LongDoubleTy);
1238 // Fall back to not requiring a name match
1239 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1240 return CompilerType(ast, ast->FloatTy);
1241 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1242 return CompilerType(ast, ast->DoubleTy);
1243 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1244 return CompilerType(ast, ast->LongDoubleTy);
1245 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1246 return CompilerType(ast, ast->HalfTy);
1247 break;
1248
1249 case DW_ATE_signed:
1250 if (type_name) {
1251 if (streq(type_name, "wchar_t") &&
1252 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1253 (getTargetInfo() &&
1254 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1255 return CompilerType(ast, ast->WCharTy);
1256 if (streq(type_name, "void") &&
1257 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
1258 return CompilerType(ast, ast->VoidTy);
1259 if (strstr(type_name, "long long") &&
1260 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1261 return CompilerType(ast, ast->LongLongTy);
1262 if (strstr(type_name, "long") &&
1263 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1264 return CompilerType(ast, ast->LongTy);
1265 if (strstr(type_name, "short") &&
1266 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1267 return CompilerType(ast, ast->ShortTy);
1268 if (strstr(type_name, "char")) {
1269 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1270 return CompilerType(ast, ast->CharTy);
1271 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1272 return CompilerType(ast, ast->SignedCharTy);
1273 }
1274 if (strstr(type_name, "int")) {
1275 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1276 return CompilerType(ast, ast->IntTy);
1277 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1278 return CompilerType(ast, ast->Int128Ty);
1279 }
1280 }
1281 // We weren't able to match up a type name, just search by size
1282 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1283 return CompilerType(ast, ast->CharTy);
1284 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1285 return CompilerType(ast, ast->ShortTy);
1286 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1287 return CompilerType(ast, ast->IntTy);
1288 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1289 return CompilerType(ast, ast->LongTy);
1290 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1291 return CompilerType(ast, ast->LongLongTy);
1292 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1293 return CompilerType(ast, ast->Int128Ty);
1294 break;
1295
1296 case DW_ATE_signed_char:
1297 if (ast->getLangOpts().CharIsSigned && type_name &&
1298 streq(type_name, "char")) {
1299 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1300 return CompilerType(ast, ast->CharTy);
1301 }
1302 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1303 return CompilerType(ast, ast->SignedCharTy);
1304 break;
1305
1306 case DW_ATE_unsigned:
1307 if (type_name) {
1308 if (streq(type_name, "wchar_t")) {
1309 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1310 if (!(getTargetInfo() &&
1311 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1312 return CompilerType(ast, ast->WCharTy);
1313 }
1314 }
1315 if (strstr(type_name, "long long")) {
1316 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1317 return CompilerType(ast, ast->UnsignedLongLongTy);
1318 } else if (strstr(type_name, "long")) {
1319 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1320 return CompilerType(ast, ast->UnsignedLongTy);
1321 } else if (strstr(type_name, "short")) {
1322 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1323 return CompilerType(ast, ast->UnsignedShortTy);
1324 } else if (strstr(type_name, "char")) {
1325 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1326 return CompilerType(ast, ast->UnsignedCharTy);
1327 } else if (strstr(type_name, "int")) {
1328 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1329 return CompilerType(ast, ast->UnsignedIntTy);
1330 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1331 return CompilerType(ast, ast->UnsignedInt128Ty);
1332 }
1333 }
1334 // We weren't able to match up a type name, just search by size
1335 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1336 return CompilerType(ast, ast->UnsignedCharTy);
1337 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1338 return CompilerType(ast, ast->UnsignedShortTy);
1339 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1340 return CompilerType(ast, ast->UnsignedIntTy);
1341 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1342 return CompilerType(ast, ast->UnsignedLongTy);
1343 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1344 return CompilerType(ast, ast->UnsignedLongLongTy);
1345 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1346 return CompilerType(ast, ast->UnsignedInt128Ty);
1347 break;
1348
1349 case DW_ATE_unsigned_char:
1350 if (!ast->getLangOpts().CharIsSigned && type_name &&
1351 streq(type_name, "char")) {
1352 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1353 return CompilerType(ast, ast->CharTy);
1354 }
1355 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1356 return CompilerType(ast, ast->UnsignedCharTy);
1357 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1358 return CompilerType(ast, ast->UnsignedShortTy);
1359 break;
1360
1361 case DW_ATE_imaginary_float:
1362 break;
1363
1364 case DW_ATE_UTF:
1365 if (type_name) {
1366 if (streq(type_name, "char16_t")) {
1367 return CompilerType(ast, ast->Char16Ty);
1368 } else if (streq(type_name, "char32_t")) {
1369 return CompilerType(ast, ast->Char32Ty);
1370 }
1371 }
1372 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001373 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001374 }
1375 // This assert should fire for anything that we don't catch above so we know
1376 // to fix any issues we run into.
1377 if (type_name) {
1378 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1379 "DW_TAG_base_type '%s' encoded with "
1380 "DW_ATE = 0x%x, bit_size = %u\n",
1381 type_name, dw_ate, bit_size);
1382 } else {
1383 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1384 "DW_TAG_base_type encoded with "
1385 "DW_ATE = 0x%x, bit_size = %u\n",
1386 dw_ate, bit_size);
1387 }
1388 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001389}
1390
Kate Stoneb9c1b512016-09-06 20:57:50 +00001391CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) {
1392 if (ast)
1393 return CompilerType(ast, ast->UnknownAnyTy);
1394 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001395}
1396
Kate Stoneb9c1b512016-09-06 20:57:50 +00001397CompilerType ClangASTContext::GetCStringType(bool is_const) {
1398 ASTContext *ast = getASTContext();
1399 QualType char_type(ast->CharTy);
1400
1401 if (is_const)
1402 char_type.addConst();
1403
1404 return CompilerType(ast, ast->getPointerType(char_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001405}
1406
Zachary Turner115209e2018-11-05 19:25:39 +00001407clang::DeclContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001408ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1409 return ast->getTranslationUnitDecl();
Sean Callanan09ab4b72011-11-30 22:11:59 +00001410}
1411
Kate Stoneb9c1b512016-09-06 20:57:50 +00001412clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1413 clang::Decl *source_decl) {
1414 FileSystemOptions file_system_options;
1415 FileManager file_manager(file_system_options);
1416 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1417
1418 return importer.Import(source_decl);
Greg Clayton526e5af2010-11-13 03:52:47 +00001419}
1420
Kate Stoneb9c1b512016-09-06 20:57:50 +00001421bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1422 bool ignore_qualifiers) {
1423 ClangASTContext *ast =
1424 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1425 if (!ast || ast != type2.GetTypeSystem())
1426 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001427
Kate Stoneb9c1b512016-09-06 20:57:50 +00001428 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1429 return true;
Greg Clayton55995eb2012-04-06 17:38:55 +00001430
Kate Stoneb9c1b512016-09-06 20:57:50 +00001431 QualType type1_qual = ClangUtil::GetQualType(type1);
1432 QualType type2_qual = ClangUtil::GetQualType(type2);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001433
Kate Stoneb9c1b512016-09-06 20:57:50 +00001434 if (ignore_qualifiers) {
1435 type1_qual = type1_qual.getUnqualifiedType();
1436 type2_qual = type2_qual.getUnqualifiedType();
1437 }
1438
1439 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001440}
1441
Kate Stoneb9c1b512016-09-06 20:57:50 +00001442CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1443 if (clang::ObjCInterfaceDecl *interface_decl =
1444 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1445 return GetTypeForDecl(interface_decl);
1446 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1447 return GetTypeForDecl(tag_decl);
1448 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001449}
1450
Kate Stoneb9c1b512016-09-06 20:57:50 +00001451CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001452 // No need to call the getASTContext() accessor (which can create the AST if
1453 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001454 // AST if our AST didn't already exist...
1455 ASTContext *ast = &decl->getASTContext();
1456 if (ast)
1457 return CompilerType(ast, ast->getTagDeclType(decl));
1458 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001459}
1460
Kate Stoneb9c1b512016-09-06 20:57:50 +00001461CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001462 // No need to call the getASTContext() accessor (which can create the AST if
1463 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001464 // AST if our AST didn't already exist...
1465 ASTContext *ast = &decl->getASTContext();
1466 if (ast)
1467 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1468 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001469}
1470
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001471#pragma mark Structure, Unions, Classes
1472
Kate Stoneb9c1b512016-09-06 20:57:50 +00001473CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx,
1474 AccessType access_type,
1475 const char *name, int kind,
1476 LanguageType language,
1477 ClangASTMetadata *metadata) {
1478 ASTContext *ast = getASTContext();
1479 assert(ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001480
Kate Stoneb9c1b512016-09-06 20:57:50 +00001481 if (decl_ctx == nullptr)
1482 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton9e409562010-07-28 02:04:09 +00001483
Kate Stoneb9c1b512016-09-06 20:57:50 +00001484 if (language == eLanguageTypeObjC ||
1485 language == eLanguageTypeObjC_plus_plus) {
1486 bool isForwardDecl = true;
1487 bool isInternal = false;
1488 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1489 }
Greg Clayton9e409562010-07-28 02:04:09 +00001490
Kate Stoneb9c1b512016-09-06 20:57:50 +00001491 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
Adrian Prantl05097242018-04-30 16:49:04 +00001492 // we will need to update this code. I was told to currently always use the
1493 // CXXRecordDecl class since we often don't know from debug information if
1494 // something is struct or a class, so we default to always use the more
Kate Stoneb9c1b512016-09-06 20:57:50 +00001495 // complete definition just in case.
Greg Claytonc4ffd662013-03-08 01:37:30 +00001496
Kate Stoneb9c1b512016-09-06 20:57:50 +00001497 bool is_anonymous = (!name) || (!name[0]);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001498
Kate Stoneb9c1b512016-09-06 20:57:50 +00001499 CXXRecordDecl *decl = CXXRecordDecl::Create(
1500 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1501 SourceLocation(), is_anonymous ? nullptr : &ast->Idents.get(name));
1502
1503 if (is_anonymous)
1504 decl->setAnonymousStructOrUnion(true);
1505
1506 if (decl) {
1507 if (metadata)
1508 SetMetadata(ast, decl, *metadata);
1509
1510 if (access_type != eAccessNone)
1511 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1512
1513 if (decl_ctx)
1514 decl_ctx->addDecl(decl);
1515
1516 return CompilerType(ast, ast->getTagDeclType(decl));
1517 }
1518 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001519}
1520
Sean Callanan09e91ac2017-05-11 22:08:05 +00001521namespace {
1522 bool IsValueParam(const clang::TemplateArgument &argument) {
1523 return argument.getKind() == TemplateArgument::Integral;
1524 }
1525}
1526
Kate Stoneb9c1b512016-09-06 20:57:50 +00001527static TemplateParameterList *CreateTemplateParameterList(
1528 ASTContext *ast,
1529 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1530 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1531 const bool parameter_pack = false;
1532 const bool is_typename = false;
1533 const unsigned depth = 0;
Sean Callanan09e91ac2017-05-11 22:08:05 +00001534 const size_t num_template_params = template_param_infos.args.size();
1535 DeclContext *const decl_context =
1536 ast->getTranslationUnitDecl(); // Is this the right decl context?,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001537 for (size_t i = 0; i < num_template_params; ++i) {
1538 const char *name = template_param_infos.names[i];
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001539
Kate Stoneb9c1b512016-09-06 20:57:50 +00001540 IdentifierInfo *identifier_info = nullptr;
1541 if (name && name[0])
1542 identifier_info = &ast->Idents.get(name);
Sean Callanan09e91ac2017-05-11 22:08:05 +00001543 if (IsValueParam(template_param_infos.args[i])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001544 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001545 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001546 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1547 template_param_infos.args[i].getIntegralType(), parameter_pack,
1548 nullptr));
1549
1550 } else {
1551 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001552 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001553 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1554 is_typename, parameter_pack));
1555 }
1556 }
Eugene Zemtsova9d928c2017-09-29 03:15:08 +00001557
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001558 if (template_param_infos.packed_args) {
Sean Callanan09e91ac2017-05-11 22:08:05 +00001559 IdentifierInfo *identifier_info = nullptr;
1560 if (template_param_infos.pack_name && template_param_infos.pack_name[0])
1561 identifier_info = &ast->Idents.get(template_param_infos.pack_name);
1562 const bool parameter_pack_true = true;
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001563
1564 if (!template_param_infos.packed_args->args.empty() &&
1565 IsValueParam(template_param_infos.packed_args->args[0])) {
Sean Callanan09e91ac2017-05-11 22:08:05 +00001566 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001567 *ast, decl_context, SourceLocation(), SourceLocation(), depth,
1568 num_template_params, identifier_info,
Sean Callanan09e91ac2017-05-11 22:08:05 +00001569 template_param_infos.packed_args->args[0].getIntegralType(),
1570 parameter_pack_true, nullptr));
1571 } else {
1572 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001573 *ast, decl_context, SourceLocation(), SourceLocation(), depth,
1574 num_template_params, identifier_info, is_typename,
1575 parameter_pack_true));
Sean Callanan09e91ac2017-05-11 22:08:05 +00001576 }
1577 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001578 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1579 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1580 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1581 SourceLocation(), requires_clause);
1582 return template_param_list;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001583}
1584
Kate Stoneb9c1b512016-09-06 20:57:50 +00001585clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1586 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1587 const char *name, const TemplateParameterInfos &template_param_infos) {
Adrian Prantld8f460e2018-05-02 16:55:16 +00001588 // /// Create a function template node.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001589 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001590
Kate Stoneb9c1b512016-09-06 20:57:50 +00001591 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001592
Kate Stoneb9c1b512016-09-06 20:57:50 +00001593 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1594 ast, template_param_infos, template_param_decls);
1595 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1596 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1597 template_param_list, func_decl);
1598
1599 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1600 i < template_param_decl_count; ++i) {
1601 // TODO: verify which decl context we should put template_param_decls into..
1602 template_param_decls[i]->setDeclContext(func_decl);
1603 }
1604
1605 return func_tmpl_decl;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001606}
1607
Kate Stoneb9c1b512016-09-06 20:57:50 +00001608void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1609 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1610 const TemplateParameterInfos &infos) {
1611 TemplateArgumentList template_args(TemplateArgumentList::OnStack, infos.args);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001612
Kate Stoneb9c1b512016-09-06 20:57:50 +00001613 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, &template_args,
1614 nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001615}
1616
Kate Stoneb9c1b512016-09-06 20:57:50 +00001617ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1618 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1619 int kind, const TemplateParameterInfos &template_param_infos) {
1620 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001621
Kate Stoneb9c1b512016-09-06 20:57:50 +00001622 ClassTemplateDecl *class_template_decl = nullptr;
1623 if (decl_ctx == nullptr)
1624 decl_ctx = ast->getTranslationUnitDecl();
Greg Claytonf0705c82011-10-22 03:33:13 +00001625
Kate Stoneb9c1b512016-09-06 20:57:50 +00001626 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1627 DeclarationName decl_name(&identifier_info);
Greg Claytonf0705c82011-10-22 03:33:13 +00001628
Kate Stoneb9c1b512016-09-06 20:57:50 +00001629 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Greg Claytonf0705c82011-10-22 03:33:13 +00001630
Kate Stoneb9c1b512016-09-06 20:57:50 +00001631 for (NamedDecl *decl : result) {
1632 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001633 if (class_template_decl)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001634 return class_template_decl;
1635 }
1636
1637 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1638
1639 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1640 ast, template_param_infos, template_param_decls);
1641
1642 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1643 *ast, (TagDecl::TagKind)kind,
1644 decl_ctx, // What decl context do we use here? TU? The actual decl
1645 // context?
1646 SourceLocation(), SourceLocation(), &identifier_info);
1647
1648 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1649 i < template_param_decl_count; ++i) {
1650 template_param_decls[i]->setDeclContext(template_cxx_decl);
1651 }
1652
1653 // With templated classes, we say that a class is templated with
1654 // specializations, but that the bare class has no functions.
1655 // template_cxx_decl->startDefinition();
1656 // template_cxx_decl->completeDefinition();
1657
1658 class_template_decl = ClassTemplateDecl::Create(
1659 *ast,
1660 decl_ctx, // What decl context do we use here? TU? The actual decl
1661 // context?
Pavel Labath4294de32017-01-12 10:44:16 +00001662 SourceLocation(), decl_name, template_param_list, template_cxx_decl);
Richard Smith35b007e2019-02-15 21:48:09 +00001663 template_cxx_decl->setDescribedClassTemplate(class_template_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001664
1665 if (class_template_decl) {
1666 if (access_type != eAccessNone)
1667 class_template_decl->setAccess(
1668 ConvertAccessTypeToAccessSpecifier(access_type));
1669
1670 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1671 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1672
1673 decl_ctx->addDecl(class_template_decl);
1674
Sean Callanan5e9e1992011-10-26 01:06:27 +00001675#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001676 VerifyDecl(class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001677#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001678 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001679
Kate Stoneb9c1b512016-09-06 20:57:50 +00001680 return class_template_decl;
Greg Claytonf0705c82011-10-22 03:33:13 +00001681}
1682
Frederic Rissf4e7e522018-04-02 16:18:32 +00001683TemplateTemplateParmDecl *
1684ClangASTContext::CreateTemplateTemplateParmDecl(const char *template_name) {
1685 ASTContext *ast = getASTContext();
1686
1687 auto *decl_ctx = ast->getTranslationUnitDecl();
1688
1689 IdentifierInfo &identifier_info = ast->Idents.get(template_name);
1690 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1691
1692 ClangASTContext::TemplateParameterInfos template_param_infos;
1693 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1694 ast, template_param_infos, template_param_decls);
1695
1696 // LLDB needs to create those decls only to be able to display a
Adrian Prantl05097242018-04-30 16:49:04 +00001697 // type that includes a template template argument. Only the name matters for
1698 // this purpose, so we use dummy values for the other characterisitcs of the
1699 // type.
Frederic Rissf4e7e522018-04-02 16:18:32 +00001700 return TemplateTemplateParmDecl::Create(
1701 *ast, decl_ctx, SourceLocation(),
1702 /*Depth*/ 0, /*Position*/ 0,
1703 /*IsParameterPack*/ false, &identifier_info, template_param_list);
1704}
1705
Greg Claytonf0705c82011-10-22 03:33:13 +00001706ClassTemplateSpecializationDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001707ClangASTContext::CreateClassTemplateSpecializationDecl(
1708 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1709 const TemplateParameterInfos &template_param_infos) {
1710 ASTContext *ast = getASTContext();
Sean Callanan09e91ac2017-05-11 22:08:05 +00001711 llvm::SmallVector<clang::TemplateArgument, 2> args(
1712 template_param_infos.args.size() +
1713 (template_param_infos.packed_args ? 1 : 0));
1714 std::copy(template_param_infos.args.begin(), template_param_infos.args.end(),
1715 args.begin());
1716 if (template_param_infos.packed_args) {
1717 args[args.size() - 1] = TemplateArgument::CreatePackCopy(
1718 *ast, template_param_infos.packed_args->args);
1719 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001720 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1721 ClassTemplateSpecializationDecl::Create(
1722 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
Sean Callanan09e91ac2017-05-11 22:08:05 +00001723 SourceLocation(), class_template_decl, args,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001724 nullptr);
1725
1726 class_template_specialization_decl->setSpecializationKind(
1727 TSK_ExplicitSpecialization);
1728
1729 return class_template_specialization_decl;
1730}
1731
1732CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1733 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1734 if (class_template_specialization_decl) {
Greg Claytonf0705c82011-10-22 03:33:13 +00001735 ASTContext *ast = getASTContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001736 if (ast)
1737 return CompilerType(
1738 ast, ast->getTagDeclType(class_template_specialization_decl));
1739 }
1740 return CompilerType();
Greg Claytonf0705c82011-10-22 03:33:13 +00001741}
1742
Kate Stoneb9c1b512016-09-06 20:57:50 +00001743static inline bool check_op_param(bool is_method,
1744 clang::OverloadedOperatorKind op_kind,
1745 bool unary, bool binary,
1746 uint32_t num_params) {
1747 // Special-case call since it can take any number of operands
1748 if (op_kind == OO_Call)
1749 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001750
Kate Stoneb9c1b512016-09-06 20:57:50 +00001751 // The parameter count doesn't include "this"
1752 if (is_method)
1753 ++num_params;
1754 if (num_params == 1)
1755 return unary;
1756 if (num_params == 2)
1757 return binary;
1758 else
Greg Clayton090d0982011-06-19 03:43:27 +00001759 return false;
1760}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001761
Kate Stoneb9c1b512016-09-06 20:57:50 +00001762bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1763 bool is_method, clang::OverloadedOperatorKind op_kind,
1764 uint32_t num_params) {
1765 switch (op_kind) {
1766 default:
1767 break;
1768 // C++ standard allows any number of arguments to new/delete
1769 case OO_New:
1770 case OO_Array_New:
1771 case OO_Delete:
1772 case OO_Array_Delete:
1773 return true;
1774 }
Pavel Labath1ac2b202016-08-15 14:32:32 +00001775
Kate Stoneb9c1b512016-09-06 20:57:50 +00001776#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1777 case OO_##Name: \
1778 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1779 switch (op_kind) {
Greg Clayton090d0982011-06-19 03:43:27 +00001780#include "clang/Basic/OperatorKinds.def"
Kate Stoneb9c1b512016-09-06 20:57:50 +00001781 default:
1782 break;
1783 }
1784 return false;
Greg Clayton090d0982011-06-19 03:43:27 +00001785}
1786
Greg Clayton57ee3062013-07-11 22:46:58 +00001787clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +00001788ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1789 clang::AccessSpecifier rhs) {
1790 // Make the access equal to the stricter of the field and the nested field's
1791 // access
1792 if (lhs == AS_none || rhs == AS_none)
1793 return AS_none;
1794 if (lhs == AS_private || rhs == AS_private)
1795 return AS_private;
1796 if (lhs == AS_protected || rhs == AS_protected)
1797 return AS_protected;
1798 return AS_public;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001799}
1800
Kate Stoneb9c1b512016-09-06 20:57:50 +00001801bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1802 uint32_t &bitfield_bit_size) {
1803 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001804}
1805
Kate Stoneb9c1b512016-09-06 20:57:50 +00001806bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field,
1807 uint32_t &bitfield_bit_size) {
1808 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001809 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001810
Kate Stoneb9c1b512016-09-06 20:57:50 +00001811 if (field->isBitField()) {
1812 Expr *bit_width_expr = field->getBitWidth();
1813 if (bit_width_expr) {
1814 llvm::APSInt bit_width_apsint;
1815 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1816 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001817 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001818 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001819 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001820 }
1821 return false;
1822}
1823
1824bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1825 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001826 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001827
1828 if (!record_decl->field_empty())
1829 return true;
1830
1831 // No fields, lets check this is a CXX record and check the base classes
1832 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1833 if (cxx_record_decl) {
1834 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1835 for (base_class = cxx_record_decl->bases_begin(),
1836 base_class_end = cxx_record_decl->bases_end();
1837 base_class != base_class_end; ++base_class) {
1838 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1839 base_class->getType()->getAs<RecordType>()->getDecl());
1840 if (RecordHasFields(base_class_decl))
1841 return true;
1842 }
1843 }
1844 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001845}
1846
Adrian Prantl4e8be2c2018-06-13 16:21:24 +00001847#pragma mark Objective-C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001848
Kate Stoneb9c1b512016-09-06 20:57:50 +00001849CompilerType ClangASTContext::CreateObjCClass(const char *name,
1850 DeclContext *decl_ctx,
1851 bool isForwardDecl,
1852 bool isInternal,
1853 ClangASTMetadata *metadata) {
1854 ASTContext *ast = getASTContext();
1855 assert(ast != nullptr);
1856 assert(name && name[0]);
1857 if (decl_ctx == nullptr)
1858 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001859
Kate Stoneb9c1b512016-09-06 20:57:50 +00001860 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1861 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1862 nullptr, SourceLocation(),
1863 /*isForwardDecl,*/
1864 isInternal);
1865
1866 if (decl && metadata)
1867 SetMetadata(ast, decl, *metadata);
1868
1869 return CompilerType(ast, ast->getObjCInterfaceType(decl));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001870}
1871
Kate Stoneb9c1b512016-09-06 20:57:50 +00001872static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001873 return !ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001874}
1875
Greg Clayton57ee3062013-07-11 22:46:58 +00001876uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001877ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1878 bool omit_empty_base_classes) {
1879 uint32_t num_bases = 0;
1880 if (cxx_record_decl) {
1881 if (omit_empty_base_classes) {
1882 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1883 for (base_class = cxx_record_decl->bases_begin(),
1884 base_class_end = cxx_record_decl->bases_end();
1885 base_class != base_class_end; ++base_class) {
1886 // Skip empty base classes
1887 if (omit_empty_base_classes) {
1888 if (BaseSpecifierIsEmpty(base_class))
1889 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001890 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001891 ++num_bases;
1892 }
1893 } else
1894 num_bases = cxx_record_decl->getNumBases();
1895 }
1896 return num_bases;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001897}
1898
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001899#pragma mark Namespace Declarations
1900
Raphael Isemanna9469972019-03-12 07:45:04 +00001901NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1902 const char *name, DeclContext *decl_ctx, bool is_inline) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001903 NamespaceDecl *namespace_decl = nullptr;
1904 ASTContext *ast = getASTContext();
1905 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1906 if (decl_ctx == nullptr)
1907 decl_ctx = translation_unit_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00001908
Kate Stoneb9c1b512016-09-06 20:57:50 +00001909 if (name) {
1910 IdentifierInfo &identifier_info = ast->Idents.get(name);
1911 DeclarationName decl_name(&identifier_info);
1912 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1913 for (NamedDecl *decl : result) {
1914 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1915 if (namespace_decl)
1916 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001917 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001918
1919 namespace_decl =
Raphael Isemanna9469972019-03-12 07:45:04 +00001920 NamespaceDecl::Create(*ast, decl_ctx, is_inline, SourceLocation(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001921 SourceLocation(), &identifier_info, nullptr);
1922
1923 decl_ctx->addDecl(namespace_decl);
1924 } else {
1925 if (decl_ctx == translation_unit_decl) {
1926 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1927 if (namespace_decl)
1928 return namespace_decl;
1929
1930 namespace_decl =
1931 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1932 SourceLocation(), nullptr, nullptr);
1933 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1934 translation_unit_decl->addDecl(namespace_decl);
1935 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
1936 } else {
1937 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1938 if (parent_namespace_decl) {
1939 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1940 if (namespace_decl)
1941 return namespace_decl;
1942 namespace_decl =
1943 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1944 SourceLocation(), nullptr, nullptr);
1945 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1946 parent_namespace_decl->addDecl(namespace_decl);
1947 assert(namespace_decl ==
1948 parent_namespace_decl->getAnonymousNamespace());
1949 } else {
1950 // BAD!!!
1951 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001952 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001953 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001954#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001955 VerifyDecl(namespace_decl);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001956#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001957 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001958}
1959
Kate Stoneb9c1b512016-09-06 20:57:50 +00001960NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
Raphael Isemanna9469972019-03-12 07:45:04 +00001961 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx,
1962 bool is_inline) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001963 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1964 if (ast_ctx == nullptr)
1965 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +00001966
Raphael Isemanna9469972019-03-12 07:45:04 +00001967 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx, is_inline);
Siva Chandra03ff5c82016-02-05 19:10:04 +00001968}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001969
Paul Hermand628cbb2015-09-15 23:44:17 +00001970clang::BlockDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001971ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1972 if (ctx != nullptr) {
1973 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1974 clang::SourceLocation());
1975 ctx->addDecl(decl);
1976 return decl;
1977 }
1978 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001979}
1980
Kate Stoneb9c1b512016-09-06 20:57:50 +00001981clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1982 clang::DeclContext *right,
1983 clang::DeclContext *root) {
1984 if (root == nullptr)
Paul Hermanea188fc2015-09-16 18:48:30 +00001985 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001986
1987 std::set<clang::DeclContext *> path_left;
1988 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1989 path_left.insert(d);
1990
1991 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1992 if (path_left.find(d) != path_left.end())
1993 return d;
1994
1995 return nullptr;
Paul Hermanea188fc2015-09-16 18:48:30 +00001996}
1997
Kate Stoneb9c1b512016-09-06 20:57:50 +00001998clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
1999 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
2000 if (decl_ctx != nullptr && ns_decl != nullptr) {
2001 clang::TranslationUnitDecl *translation_unit =
2002 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
2003 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
2004 *getASTContext(), decl_ctx, clang::SourceLocation(),
2005 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
2006 clang::SourceLocation(), ns_decl,
2007 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
2008 decl_ctx->addDecl(using_decl);
2009 return using_decl;
2010 }
2011 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002012}
2013
2014clang::UsingDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00002015ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
2016 clang::NamedDecl *target) {
2017 if (current_decl_ctx != nullptr && target != nullptr) {
2018 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
2019 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
2020 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
2021 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
2022 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
2023 target);
2024 using_decl->addShadowDecl(shadow_decl);
2025 current_decl_ctx->addDecl(using_decl);
2026 return using_decl;
2027 }
2028 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002029}
2030
Kate Stoneb9c1b512016-09-06 20:57:50 +00002031clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
2032 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
2033 if (decl_context != nullptr) {
2034 clang::VarDecl *var_decl = clang::VarDecl::Create(
2035 *getASTContext(), decl_context, clang::SourceLocation(),
2036 clang::SourceLocation(),
2037 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
2038 nullptr, clang::SC_None);
2039 var_decl->setAccess(clang::AS_public);
2040 decl_context->addDecl(var_decl);
2041 return var_decl;
2042 }
2043 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002044}
2045
Zachary Turner9d8a97e2016-04-01 23:20:35 +00002046lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00002047ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
2048 lldb::BasicType basic_type) {
2049 switch (basic_type) {
2050 case eBasicTypeVoid:
2051 return ast->VoidTy.getAsOpaquePtr();
2052 case eBasicTypeChar:
2053 return ast->CharTy.getAsOpaquePtr();
2054 case eBasicTypeSignedChar:
2055 return ast->SignedCharTy.getAsOpaquePtr();
2056 case eBasicTypeUnsignedChar:
2057 return ast->UnsignedCharTy.getAsOpaquePtr();
2058 case eBasicTypeWChar:
2059 return ast->getWCharType().getAsOpaquePtr();
2060 case eBasicTypeSignedWChar:
2061 return ast->getSignedWCharType().getAsOpaquePtr();
2062 case eBasicTypeUnsignedWChar:
2063 return ast->getUnsignedWCharType().getAsOpaquePtr();
2064 case eBasicTypeChar16:
2065 return ast->Char16Ty.getAsOpaquePtr();
2066 case eBasicTypeChar32:
2067 return ast->Char32Ty.getAsOpaquePtr();
2068 case eBasicTypeShort:
2069 return ast->ShortTy.getAsOpaquePtr();
2070 case eBasicTypeUnsignedShort:
2071 return ast->UnsignedShortTy.getAsOpaquePtr();
2072 case eBasicTypeInt:
2073 return ast->IntTy.getAsOpaquePtr();
2074 case eBasicTypeUnsignedInt:
2075 return ast->UnsignedIntTy.getAsOpaquePtr();
2076 case eBasicTypeLong:
2077 return ast->LongTy.getAsOpaquePtr();
2078 case eBasicTypeUnsignedLong:
2079 return ast->UnsignedLongTy.getAsOpaquePtr();
2080 case eBasicTypeLongLong:
2081 return ast->LongLongTy.getAsOpaquePtr();
2082 case eBasicTypeUnsignedLongLong:
2083 return ast->UnsignedLongLongTy.getAsOpaquePtr();
2084 case eBasicTypeInt128:
2085 return ast->Int128Ty.getAsOpaquePtr();
2086 case eBasicTypeUnsignedInt128:
2087 return ast->UnsignedInt128Ty.getAsOpaquePtr();
2088 case eBasicTypeBool:
2089 return ast->BoolTy.getAsOpaquePtr();
2090 case eBasicTypeHalf:
2091 return ast->HalfTy.getAsOpaquePtr();
2092 case eBasicTypeFloat:
2093 return ast->FloatTy.getAsOpaquePtr();
2094 case eBasicTypeDouble:
2095 return ast->DoubleTy.getAsOpaquePtr();
2096 case eBasicTypeLongDouble:
2097 return ast->LongDoubleTy.getAsOpaquePtr();
2098 case eBasicTypeFloatComplex:
2099 return ast->FloatComplexTy.getAsOpaquePtr();
2100 case eBasicTypeDoubleComplex:
2101 return ast->DoubleComplexTy.getAsOpaquePtr();
2102 case eBasicTypeLongDoubleComplex:
2103 return ast->LongDoubleComplexTy.getAsOpaquePtr();
2104 case eBasicTypeObjCID:
2105 return ast->getObjCIdType().getAsOpaquePtr();
2106 case eBasicTypeObjCClass:
2107 return ast->getObjCClassType().getAsOpaquePtr();
2108 case eBasicTypeObjCSel:
2109 return ast->getObjCSelType().getAsOpaquePtr();
2110 case eBasicTypeNullPtr:
2111 return ast->NullPtrTy.getAsOpaquePtr();
2112 default:
2113 return nullptr;
2114 }
Zachary Turner9d8a97e2016-04-01 23:20:35 +00002115}
2116
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002117#pragma mark Function Types
2118
Pavel Labath1ac2b202016-08-15 14:32:32 +00002119clang::DeclarationName
Kate Stoneb9c1b512016-09-06 20:57:50 +00002120ClangASTContext::GetDeclarationName(const char *name,
2121 const CompilerType &function_clang_type) {
2122 if (!name || !name[0])
2123 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002124
Kate Stoneb9c1b512016-09-06 20:57:50 +00002125 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
2126 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
2127 return DeclarationName(&getASTContext()->Idents.get(
2128 name)); // Not operator, but a regular function.
Pavel Labath1ac2b202016-08-15 14:32:32 +00002129
Adrian Prantl05097242018-04-30 16:49:04 +00002130 // Check the number of operator parameters. Sometimes we have seen bad DWARF
2131 // that doesn't correctly describe operators and if we try to create a method
2132 // and add it to the class, clang will assert and crash, so we need to make
2133 // sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002134 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
2135 const clang::FunctionProtoType *function_type =
2136 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
2137 if (function_type == nullptr)
2138 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002139
Kate Stoneb9c1b512016-09-06 20:57:50 +00002140 const bool is_method = false;
2141 const unsigned int num_params = function_type->getNumParams();
2142 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
2143 is_method, op_kind, num_params))
2144 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002145
Kate Stoneb9c1b512016-09-06 20:57:50 +00002146 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
Pavel Labath1ac2b202016-08-15 14:32:32 +00002147}
2148
Kate Stoneb9c1b512016-09-06 20:57:50 +00002149FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
2150 DeclContext *decl_ctx, const char *name,
2151 const CompilerType &function_clang_type, int storage, bool is_inline) {
2152 FunctionDecl *func_decl = nullptr;
2153 ASTContext *ast = getASTContext();
2154 if (decl_ctx == nullptr)
2155 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002156
Kate Stoneb9c1b512016-09-06 20:57:50 +00002157 const bool hasWrittenPrototype = true;
2158 const bool isConstexprSpecified = false;
Greg Clayton0d551042013-06-28 21:08:47 +00002159
Kate Stoneb9c1b512016-09-06 20:57:50 +00002160 clang::DeclarationName declarationName =
2161 GetDeclarationName(name, function_clang_type);
2162 func_decl = FunctionDecl::Create(
2163 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
2164 ClangUtil::GetQualType(function_clang_type), nullptr,
2165 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
2166 isConstexprSpecified);
2167 if (func_decl)
2168 decl_ctx->addDecl(func_decl);
2169
Sean Callanan5e9e1992011-10-26 01:06:27 +00002170#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00002171 VerifyDecl(func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002172#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002173
2174 return func_decl;
2175}
2176
2177CompilerType ClangASTContext::CreateFunctionType(
2178 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002179 unsigned num_args, bool is_variadic, unsigned type_quals,
2180 clang::CallingConv cc) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002181 if (ast == nullptr)
2182 return CompilerType(); // invalid AST
2183
2184 if (!result_type || !ClangUtil::IsClangType(result_type))
2185 return CompilerType(); // invalid return type
2186
2187 std::vector<QualType> qual_type_args;
2188 if (num_args > 0 && args == nullptr)
2189 return CompilerType(); // invalid argument array passed in
2190
2191 // Verify that all arguments are valid and the right type
2192 for (unsigned i = 0; i < num_args; ++i) {
2193 if (args[i]) {
2194 // Make sure we have a clang type in args[i] and not a type from another
2195 // language whose name might match
2196 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2197 lldbassert(is_clang_type);
2198 if (is_clang_type)
2199 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2200 else
2201 return CompilerType(); // invalid argument type (must be a clang type)
2202 } else
2203 return CompilerType(); // invalid argument type (empty)
2204 }
2205
2206 // TODO: Detect calling convention in DWARF?
2207 FunctionProtoType::ExtProtoInfo proto_info;
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002208 proto_info.ExtInfo = cc;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002209 proto_info.Variadic = is_variadic;
2210 proto_info.ExceptionSpec = EST_None;
Mikael Nilsson8b3bf6c2018-12-13 10:17:26 +00002211 proto_info.TypeQuals = clang::Qualifiers::fromFastMask(type_quals);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002212 proto_info.RefQualifier = RQ_None;
2213
2214 return CompilerType(ast,
2215 ast->getFunctionType(ClangUtil::GetQualType(result_type),
2216 qual_type_args, proto_info));
2217}
2218
2219ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
Zachary Turner6753d2d2018-12-12 17:17:53 +00002220 clang::DeclContext *decl_ctx, const char *name,
2221 const CompilerType &param_type, int storage) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002222 ASTContext *ast = getASTContext();
2223 assert(ast != nullptr);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00002224 auto *decl =
2225 ParmVarDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(),
2226 name && name[0] ? &ast->Idents.get(name) : nullptr,
2227 ClangUtil::GetQualType(param_type), nullptr,
2228 (clang::StorageClass)storage, nullptr);
2229 decl_ctx->addDecl(decl);
2230 return decl;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002231}
2232
2233void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2234 ParmVarDecl **params,
2235 unsigned num_params) {
2236 if (function_decl)
2237 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002238}
2239
Greg Claytona1e5dc82015-08-11 22:53:00 +00002240CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002241ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00002242 QualType block_type = m_ast_up->getBlockPointerType(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002243 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
Greg Claytonceeb5212016-05-26 22:33:25 +00002244
Kate Stoneb9c1b512016-09-06 20:57:50 +00002245 return CompilerType(this, block_type.getAsOpaquePtr());
Sean Callananc530ba92016-05-02 21:15:31 +00002246}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002247
2248#pragma mark Array Types
2249
Kate Stoneb9c1b512016-09-06 20:57:50 +00002250CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2251 size_t element_count,
2252 bool is_vector) {
2253 if (element_type.IsValid()) {
2254 ASTContext *ast = getASTContext();
2255 assert(ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002256
Kate Stoneb9c1b512016-09-06 20:57:50 +00002257 if (is_vector) {
2258 return CompilerType(
2259 ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2260 element_count));
2261 } else {
2262
2263 llvm::APInt ap_element_count(64, element_count);
2264 if (element_count == 0) {
2265 return CompilerType(ast, ast->getIncompleteArrayType(
2266 ClangUtil::GetQualType(element_type),
2267 clang::ArrayType::Normal, 0));
2268 } else {
2269 return CompilerType(
2270 ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2271 ap_element_count,
2272 clang::ArrayType::Normal, 0));
2273 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002274 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002275 }
2276 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002277}
2278
Kate Stoneb9c1b512016-09-06 20:57:50 +00002279CompilerType ClangASTContext::CreateStructForIdentifier(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002280 ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002281 const std::initializer_list<std::pair<const char *, CompilerType>>
2282 &type_fields,
2283 bool packed) {
2284 CompilerType type;
2285 if (!type_name.IsEmpty() &&
2286 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2287 .IsValid()) {
Pavel Labathf31c9d22017-01-05 13:18:42 +00002288 lldbassert(0 && "Trying to create a type for an existing name");
Enrico Granata76b08d52014-10-29 23:08:02 +00002289 return type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002290 }
2291
2292 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2293 clang::TTK_Struct, lldb::eLanguageTypeC);
2294 StartTagDeclarationDefinition(type);
2295 for (const auto &field : type_fields)
2296 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2297 0);
2298 if (packed)
2299 SetIsPacked(type);
2300 CompleteTagDeclarationDefinition(type);
2301 return type;
Enrico Granata76b08d52014-10-29 23:08:02 +00002302}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002303
Kate Stoneb9c1b512016-09-06 20:57:50 +00002304CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002305 ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002306 const std::initializer_list<std::pair<const char *, CompilerType>>
2307 &type_fields,
2308 bool packed) {
2309 CompilerType type;
2310 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2311 return type;
Sean Callananc530ba92016-05-02 21:15:31 +00002312
Kate Stoneb9c1b512016-09-06 20:57:50 +00002313 return CreateStructForIdentifier(type_name, type_fields, packed);
Sean Callananc530ba92016-05-02 21:15:31 +00002314}
2315
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002316#pragma mark Enumeration Types
2317
Greg Claytona1e5dc82015-08-11 22:53:00 +00002318CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002319ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2320 const Declaration &decl,
Tamas Berghammer59765832017-11-07 10:39:22 +00002321 const CompilerType &integer_clang_type,
2322 bool is_scoped) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002323 // TODO: Do something intelligent with the Declaration object passed in
2324 // like maybe filling in the SourceLocation with it...
2325 ASTContext *ast = getASTContext();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002326
Kate Stoneb9c1b512016-09-06 20:57:50 +00002327 // TODO: ask about these...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002328 // const bool IsFixed = false;
2329
2330 EnumDecl *enum_decl = EnumDecl::Create(
2331 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2332 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
Tamas Berghammercf6bf4c2017-11-07 13:43:55 +00002333 is_scoped, // IsScoped
2334 is_scoped, // IsScopedUsingClassTag
2335 false); // IsFixed
Kate Stoneb9c1b512016-09-06 20:57:50 +00002336
2337 if (enum_decl) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +00002338 if (decl_ctx)
2339 decl_ctx->addDecl(enum_decl);
2340
Kate Stoneb9c1b512016-09-06 20:57:50 +00002341 // TODO: check if we should be setting the promotion type too?
2342 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2343
2344 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2345
2346 return CompilerType(ast, ast->getTagDeclType(enum_decl));
2347 }
2348 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002349}
2350
Kate Stoneb9c1b512016-09-06 20:57:50 +00002351CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2352 size_t bit_size,
2353 bool is_signed) {
2354 if (ast) {
2355 if (is_signed) {
2356 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
2357 return CompilerType(ast, ast->SignedCharTy);
2358
2359 if (bit_size == ast->getTypeSize(ast->ShortTy))
2360 return CompilerType(ast, ast->ShortTy);
2361
2362 if (bit_size == ast->getTypeSize(ast->IntTy))
2363 return CompilerType(ast, ast->IntTy);
2364
2365 if (bit_size == ast->getTypeSize(ast->LongTy))
2366 return CompilerType(ast, ast->LongTy);
2367
2368 if (bit_size == ast->getTypeSize(ast->LongLongTy))
2369 return CompilerType(ast, ast->LongLongTy);
2370
2371 if (bit_size == ast->getTypeSize(ast->Int128Ty))
2372 return CompilerType(ast, ast->Int128Ty);
2373 } else {
2374 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
2375 return CompilerType(ast, ast->UnsignedCharTy);
2376
2377 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
2378 return CompilerType(ast, ast->UnsignedShortTy);
2379
2380 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
2381 return CompilerType(ast, ast->UnsignedIntTy);
2382
2383 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
2384 return CompilerType(ast, ast->UnsignedLongTy);
2385
2386 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
2387 return CompilerType(ast, ast->UnsignedLongLongTy);
2388
2389 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
2390 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002391 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002392 }
2393 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002394}
2395
Kate Stoneb9c1b512016-09-06 20:57:50 +00002396CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2397 bool is_signed) {
2398 if (ast)
2399 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2400 is_signed);
2401 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002402}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002403
Kate Stoneb9c1b512016-09-06 20:57:50 +00002404void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2405 if (decl_ctx) {
2406 DumpDeclContextHiearchy(decl_ctx->getParent());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002407
Kate Stoneb9c1b512016-09-06 20:57:50 +00002408 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2409 if (named_decl) {
2410 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2411 named_decl->getDeclName().getAsString().c_str());
2412 } else {
2413 printf("%20s\n", decl_ctx->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002414 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002415 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002416}
2417
Kate Stoneb9c1b512016-09-06 20:57:50 +00002418void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2419 if (decl == nullptr)
2420 return;
2421 DumpDeclContextHiearchy(decl->getDeclContext());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002422
Kate Stoneb9c1b512016-09-06 20:57:50 +00002423 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2424 if (record_decl) {
2425 printf("%20s: %s%s\n", decl->getDeclKindName(),
2426 record_decl->getDeclName().getAsString().c_str(),
2427 record_decl->isInjectedClassName() ? " (injected class name)" : "");
Greg Claytone6b36cd2015-12-08 01:02:08 +00002428
Kate Stoneb9c1b512016-09-06 20:57:50 +00002429 } else {
2430 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2431 if (named_decl) {
2432 printf("%20s: %s\n", decl->getDeclKindName(),
2433 named_decl->getDeclName().getAsString().c_str());
2434 } else {
2435 printf("%20s\n", decl->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002436 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002437 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002438}
2439
Kate Stoneb9c1b512016-09-06 20:57:50 +00002440bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2441 clang::Decl *rhs_decl) {
2442 if (lhs_decl && rhs_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002443 // Make sure the decl kinds match first
Kate Stoneb9c1b512016-09-06 20:57:50 +00002444 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2445 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002446
Kate Stoneb9c1b512016-09-06 20:57:50 +00002447 if (lhs_decl_kind == rhs_decl_kind) {
Adrian Prantl05097242018-04-30 16:49:04 +00002448 // Now check that the decl contexts kinds are all equivalent before we
2449 // have to check any names of the decl contexts...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002450 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2451 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2452 if (lhs_decl_ctx && rhs_decl_ctx) {
2453 while (1) {
2454 if (lhs_decl_ctx && rhs_decl_ctx) {
2455 const clang::Decl::Kind lhs_decl_ctx_kind =
2456 lhs_decl_ctx->getDeclKind();
2457 const clang::Decl::Kind rhs_decl_ctx_kind =
2458 rhs_decl_ctx->getDeclKind();
2459 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2460 lhs_decl_ctx = lhs_decl_ctx->getParent();
2461 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002462
Kate Stoneb9c1b512016-09-06 20:57:50 +00002463 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2464 break;
2465 } else
2466 return false;
2467 } else
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00002468 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002469 }
2470
Kate Stoneb9c1b512016-09-06 20:57:50 +00002471 // Now make sure the name of the decls match
Kate Stoneb9c1b512016-09-06 20:57:50 +00002472 clang::NamedDecl *lhs_named_decl =
2473 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2474 clang::NamedDecl *rhs_named_decl =
2475 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2476 if (lhs_named_decl && rhs_named_decl) {
2477 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2478 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2479 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2480 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2481 return false;
2482 } else
Greg Claytona2721472011-06-25 00:44:06 +00002483 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002484 } else
2485 return false;
Greg Claytona2721472011-06-25 00:44:06 +00002486
Adrian Prantl05097242018-04-30 16:49:04 +00002487 // We know that the decl context kinds all match, so now we need to
2488 // make sure the names match as well
Kate Stoneb9c1b512016-09-06 20:57:50 +00002489 lhs_decl_ctx = lhs_decl->getDeclContext();
2490 rhs_decl_ctx = rhs_decl->getDeclContext();
2491 while (1) {
2492 switch (lhs_decl_ctx->getDeclKind()) {
2493 case clang::Decl::TranslationUnit:
2494 // We don't care about the translation unit names
2495 return true;
2496 default: {
2497 clang::NamedDecl *lhs_named_decl =
2498 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2499 clang::NamedDecl *rhs_named_decl =
2500 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2501 if (lhs_named_decl && rhs_named_decl) {
2502 clang::DeclarationName lhs_decl_name =
2503 lhs_named_decl->getDeclName();
2504 clang::DeclarationName rhs_decl_name =
2505 rhs_named_decl->getDeclName();
2506 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2507 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2508 return false;
2509 } else
2510 return false;
2511 } else
2512 return false;
2513 } break;
2514 }
2515 lhs_decl_ctx = lhs_decl_ctx->getParent();
2516 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytond8d4a572015-08-11 21:38:15 +00002517 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002518 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002519 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002520 }
2521 return false;
2522}
2523bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2524 clang::Decl *decl) {
2525 if (!decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002526 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002527
Kate Stoneb9c1b512016-09-06 20:57:50 +00002528 ExternalASTSource *ast_source = ast->getExternalSource();
Greg Claytond8d4a572015-08-11 21:38:15 +00002529
Kate Stoneb9c1b512016-09-06 20:57:50 +00002530 if (!ast_source)
Greg Claytond8d4a572015-08-11 21:38:15 +00002531 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002532
2533 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2534 if (tag_decl->isCompleteDefinition())
2535 return true;
2536
2537 if (!tag_decl->hasExternalLexicalStorage())
2538 return false;
2539
2540 ast_source->CompleteType(tag_decl);
2541
2542 return !tag_decl->getTypeForDecl()->isIncompleteType();
2543 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2544 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2545 if (objc_interface_decl->getDefinition())
2546 return true;
2547
2548 if (!objc_interface_decl->hasExternalLexicalStorage())
2549 return false;
2550
2551 ast_source->CompleteType(objc_interface_decl);
2552
2553 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2554 } else {
2555 return false;
2556 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002557}
2558
Kate Stoneb9c1b512016-09-06 20:57:50 +00002559void ClangASTContext::SetMetadataAsUserID(const void *object,
2560 user_id_t user_id) {
2561 ClangASTMetadata meta_data;
2562 meta_data.SetUserID(user_id);
2563 SetMetadata(object, meta_data);
Greg Clayton99558cc42015-08-24 23:46:31 +00002564}
2565
Kate Stoneb9c1b512016-09-06 20:57:50 +00002566void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2567 ClangASTMetadata &metadata) {
2568 ClangExternalASTSourceCommon *external_source =
2569 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2570
2571 if (external_source)
2572 external_source->SetMetadata(object, metadata);
2573}
2574
2575ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2576 const void *object) {
2577 ClangExternalASTSourceCommon *external_source =
2578 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2579
2580 if (external_source && external_source->HasMetadata(object))
2581 return external_source->GetMetadata(object);
2582 else
Greg Claytond8d4a572015-08-11 21:38:15 +00002583 return nullptr;
2584}
2585
Kate Stoneb9c1b512016-09-06 20:57:50 +00002586clang::DeclContext *
2587ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2588 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2589}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002590
Kate Stoneb9c1b512016-09-06 20:57:50 +00002591clang::DeclContext *
2592ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2593 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2594}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002595
Kate Stoneb9c1b512016-09-06 20:57:50 +00002596bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2597 int kind) const {
2598 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2599 if (clang_type) {
2600 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2601 if (tag_type) {
2602 clang::TagDecl *tag_decl =
2603 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2604 if (tag_decl) {
2605 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2606 return true;
2607 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002608 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002609 }
2610 return false;
2611}
2612
2613bool ClangASTContext::SetDefaultAccessForRecordFields(
2614 clang::RecordDecl *record_decl, int default_accessibility,
2615 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2616 if (record_decl) {
2617 uint32_t field_idx;
2618 clang::RecordDecl::field_iterator field, field_end;
2619 for (field = record_decl->field_begin(),
2620 field_end = record_decl->field_end(), field_idx = 0;
2621 field != field_end; ++field, ++field_idx) {
2622 // If no accessibility was assigned, assign the correct one
2623 if (field_idx < num_assigned_accessibilities &&
2624 assigned_accessibilities[field_idx] == clang::AS_none)
2625 field->setAccess((clang::AccessSpecifier)default_accessibility);
2626 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002627 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002628 }
2629 return false;
2630}
2631
2632clang::DeclContext *
2633ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2634 return GetDeclContextForType(ClangUtil::GetQualType(type));
2635}
2636
2637clang::DeclContext *
2638ClangASTContext::GetDeclContextForType(clang::QualType type) {
2639 if (type.isNull())
2640 return nullptr;
2641
2642 clang::QualType qual_type = type.getCanonicalType();
2643 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2644 switch (type_class) {
2645 case clang::Type::ObjCInterface:
2646 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2647 ->getInterface();
2648 case clang::Type::ObjCObjectPointer:
2649 return GetDeclContextForType(
2650 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2651 ->getPointeeType());
2652 case clang::Type::Record:
2653 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2654 case clang::Type::Enum:
2655 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2656 case clang::Type::Typedef:
2657 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2658 ->getDecl()
2659 ->getUnderlyingType());
2660 case clang::Type::Auto:
2661 return GetDeclContextForType(
2662 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2663 case clang::Type::Elaborated:
2664 return GetDeclContextForType(
2665 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2666 case clang::Type::Paren:
2667 return GetDeclContextForType(
2668 llvm::cast<clang::ParenType>(qual_type)->desugar());
2669 default:
2670 break;
2671 }
2672 // No DeclContext in this type...
2673 return nullptr;
2674}
2675
2676static bool GetCompleteQualType(clang::ASTContext *ast,
2677 clang::QualType qual_type,
2678 bool allow_completion = true) {
2679 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2680 switch (type_class) {
2681 case clang::Type::ConstantArray:
2682 case clang::Type::IncompleteArray:
2683 case clang::Type::VariableArray: {
2684 const clang::ArrayType *array_type =
2685 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2686
2687 if (array_type)
2688 return GetCompleteQualType(ast, array_type->getElementType(),
2689 allow_completion);
2690 } break;
2691 case clang::Type::Record: {
2692 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2693 if (cxx_record_decl) {
2694 if (cxx_record_decl->hasExternalLexicalStorage()) {
2695 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2696 const bool fields_loaded =
2697 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2698 if (is_complete && fields_loaded)
2699 return true;
2700
2701 if (!allow_completion)
2702 return false;
2703
2704 // Call the field_begin() accessor to for it to use the external source
2705 // to load the fields...
2706 clang::ExternalASTSource *external_ast_source =
2707 ast->getExternalSource();
2708 if (external_ast_source) {
2709 external_ast_source->CompleteType(cxx_record_decl);
2710 if (cxx_record_decl->isCompleteDefinition()) {
2711 cxx_record_decl->field_begin();
2712 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2713 }
2714 }
2715 }
2716 }
2717 const clang::TagType *tag_type =
2718 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2719 return !tag_type->isIncompleteType();
2720 } break;
2721
2722 case clang::Type::Enum: {
2723 const clang::TagType *tag_type =
2724 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2725 if (tag_type) {
2726 clang::TagDecl *tag_decl = tag_type->getDecl();
2727 if (tag_decl) {
2728 if (tag_decl->getDefinition())
2729 return true;
2730
2731 if (!allow_completion)
2732 return false;
2733
2734 if (tag_decl->hasExternalLexicalStorage()) {
2735 if (ast) {
2736 clang::ExternalASTSource *external_ast_source =
2737 ast->getExternalSource();
2738 if (external_ast_source) {
2739 external_ast_source->CompleteType(tag_decl);
2740 return !tag_type->isIncompleteType();
2741 }
2742 }
2743 }
2744 return false;
2745 }
2746 }
2747
2748 } break;
2749 case clang::Type::ObjCObject:
2750 case clang::Type::ObjCInterface: {
2751 const clang::ObjCObjectType *objc_class_type =
2752 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2753 if (objc_class_type) {
2754 clang::ObjCInterfaceDecl *class_interface_decl =
2755 objc_class_type->getInterface();
2756 // We currently can't complete objective C types through the newly added
Adrian Prantl05097242018-04-30 16:49:04 +00002757 // ASTContext because it only supports TagDecl objects right now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002758 if (class_interface_decl) {
2759 if (class_interface_decl->getDefinition())
2760 return true;
2761
2762 if (!allow_completion)
2763 return false;
2764
2765 if (class_interface_decl->hasExternalLexicalStorage()) {
2766 if (ast) {
2767 clang::ExternalASTSource *external_ast_source =
2768 ast->getExternalSource();
2769 if (external_ast_source) {
2770 external_ast_source->CompleteType(class_interface_decl);
2771 return !objc_class_type->isIncompleteType();
2772 }
2773 }
2774 }
2775 return false;
2776 }
2777 }
2778 } break;
2779
2780 case clang::Type::Typedef:
2781 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2782 ->getDecl()
2783 ->getUnderlyingType(),
2784 allow_completion);
2785
2786 case clang::Type::Auto:
2787 return GetCompleteQualType(
2788 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2789 allow_completion);
2790
2791 case clang::Type::Elaborated:
2792 return GetCompleteQualType(
2793 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2794 allow_completion);
2795
2796 case clang::Type::Paren:
2797 return GetCompleteQualType(
2798 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2799 allow_completion);
2800
2801 case clang::Type::Attributed:
2802 return GetCompleteQualType(
2803 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2804 allow_completion);
2805
2806 default:
2807 break;
2808 }
2809
2810 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00002811}
2812
2813static clang::ObjCIvarDecl::AccessControl
Kate Stoneb9c1b512016-09-06 20:57:50 +00002814ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2815 switch (access) {
2816 case eAccessNone:
Greg Claytond8d4a572015-08-11 21:38:15 +00002817 return clang::ObjCIvarDecl::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002818 case eAccessPublic:
2819 return clang::ObjCIvarDecl::Public;
2820 case eAccessPrivate:
2821 return clang::ObjCIvarDecl::Private;
2822 case eAccessProtected:
2823 return clang::ObjCIvarDecl::Protected;
2824 case eAccessPackage:
2825 return clang::ObjCIvarDecl::Package;
2826 }
2827 return clang::ObjCIvarDecl::None;
Greg Claytond8d4a572015-08-11 21:38:15 +00002828}
2829
Greg Claytond8d4a572015-08-11 21:38:15 +00002830// Tests
Greg Claytond8d4a572015-08-11 21:38:15 +00002831
Kate Stoneb9c1b512016-09-06 20:57:50 +00002832bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2833 clang::QualType qual_type(GetCanonicalQualType(type));
2834
2835 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2836 switch (type_class) {
2837 case clang::Type::IncompleteArray:
2838 case clang::Type::VariableArray:
2839 case clang::Type::ConstantArray:
2840 case clang::Type::ExtVector:
2841 case clang::Type::Vector:
2842 case clang::Type::Record:
2843 case clang::Type::ObjCObject:
2844 case clang::Type::ObjCInterface:
2845 return true;
2846 case clang::Type::Auto:
2847 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2848 ->getDeducedType()
2849 .getAsOpaquePtr());
2850 case clang::Type::Elaborated:
2851 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2852 ->getNamedType()
2853 .getAsOpaquePtr());
2854 case clang::Type::Typedef:
2855 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2856 ->getDecl()
2857 ->getUnderlyingType()
2858 .getAsOpaquePtr());
2859 case clang::Type::Paren:
2860 return IsAggregateType(
2861 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2862 default:
2863 break;
2864 }
2865 // The clang type does have a value
2866 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002867}
2868
Kate Stoneb9c1b512016-09-06 20:57:50 +00002869bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2870 clang::QualType qual_type(GetCanonicalQualType(type));
2871
2872 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2873 switch (type_class) {
2874 case clang::Type::Record: {
2875 if (const clang::RecordType *record_type =
2876 llvm::dyn_cast_or_null<clang::RecordType>(
2877 qual_type.getTypePtrOrNull())) {
2878 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2879 return record_decl->isAnonymousStructOrUnion();
2880 }
Enrico Granata7123e2b2015-11-07 02:06:57 +00002881 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002882 break;
2883 }
2884 case clang::Type::Auto:
2885 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2886 ->getDeducedType()
2887 .getAsOpaquePtr());
2888 case clang::Type::Elaborated:
2889 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2890 ->getNamedType()
2891 .getAsOpaquePtr());
2892 case clang::Type::Typedef:
2893 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2894 ->getDecl()
2895 ->getUnderlyingType()
2896 .getAsOpaquePtr());
2897 case clang::Type::Paren:
2898 return IsAnonymousType(
2899 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2900 default:
2901 break;
2902 }
2903 // The clang type does have a value
2904 return false;
Enrico Granata7123e2b2015-11-07 02:06:57 +00002905}
2906
Kate Stoneb9c1b512016-09-06 20:57:50 +00002907bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2908 CompilerType *element_type_ptr,
2909 uint64_t *size, bool *is_incomplete) {
2910 clang::QualType qual_type(GetCanonicalQualType(type));
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002911
Kate Stoneb9c1b512016-09-06 20:57:50 +00002912 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2913 switch (type_class) {
2914 default:
2915 break;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002916
Kate Stoneb9c1b512016-09-06 20:57:50 +00002917 case clang::Type::ConstantArray:
Greg Claytond8d4a572015-08-11 21:38:15 +00002918 if (element_type_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002919 element_type_ptr->SetCompilerType(
2920 getASTContext(),
2921 llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002922 if (size)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002923 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2924 ->getSize()
2925 .getLimitedValue(ULLONG_MAX);
Greg Claytond8d4a572015-08-11 21:38:15 +00002926 if (is_incomplete)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002927 *is_incomplete = false;
2928 return true;
2929
2930 case clang::Type::IncompleteArray:
2931 if (element_type_ptr)
2932 element_type_ptr->SetCompilerType(
2933 getASTContext(),
2934 llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2935 if (size)
2936 *size = 0;
2937 if (is_incomplete)
2938 *is_incomplete = true;
2939 return true;
2940
2941 case clang::Type::VariableArray:
2942 if (element_type_ptr)
2943 element_type_ptr->SetCompilerType(
2944 getASTContext(),
2945 llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2946 if (size)
2947 *size = 0;
2948 if (is_incomplete)
2949 *is_incomplete = false;
2950 return true;
2951
2952 case clang::Type::DependentSizedArray:
2953 if (element_type_ptr)
2954 element_type_ptr->SetCompilerType(
2955 getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)
2956 ->getElementType());
2957 if (size)
2958 *size = 0;
2959 if (is_incomplete)
2960 *is_incomplete = false;
2961 return true;
2962
2963 case clang::Type::Typedef:
2964 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2965 ->getDecl()
2966 ->getUnderlyingType()
2967 .getAsOpaquePtr(),
2968 element_type_ptr, size, is_incomplete);
2969 case clang::Type::Auto:
2970 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2971 ->getDeducedType()
2972 .getAsOpaquePtr(),
2973 element_type_ptr, size, is_incomplete);
2974 case clang::Type::Elaborated:
2975 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2976 ->getNamedType()
2977 .getAsOpaquePtr(),
2978 element_type_ptr, size, is_incomplete);
2979 case clang::Type::Paren:
2980 return IsArrayType(
2981 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2982 element_type_ptr, size, is_incomplete);
2983 }
2984 if (element_type_ptr)
2985 element_type_ptr->Clear();
2986 if (size)
2987 *size = 0;
2988 if (is_incomplete)
2989 *is_incomplete = false;
2990 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002991}
2992
Kate Stoneb9c1b512016-09-06 20:57:50 +00002993bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
2994 CompilerType *element_type, uint64_t *size) {
2995 clang::QualType qual_type(GetCanonicalQualType(type));
2996
2997 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2998 switch (type_class) {
2999 case clang::Type::Vector: {
3000 const clang::VectorType *vector_type =
3001 qual_type->getAs<clang::VectorType>();
3002 if (vector_type) {
3003 if (size)
3004 *size = vector_type->getNumElements();
3005 if (element_type)
3006 *element_type =
3007 CompilerType(getASTContext(), vector_type->getElementType());
3008 }
3009 return true;
3010 } break;
3011 case clang::Type::ExtVector: {
3012 const clang::ExtVectorType *ext_vector_type =
3013 qual_type->getAs<clang::ExtVectorType>();
3014 if (ext_vector_type) {
3015 if (size)
3016 *size = ext_vector_type->getNumElements();
3017 if (element_type)
3018 *element_type =
3019 CompilerType(getASTContext(), ext_vector_type->getElementType());
3020 }
3021 return true;
3022 }
3023 default:
3024 break;
3025 }
3026 return false;
3027}
3028
3029bool ClangASTContext::IsRuntimeGeneratedType(
3030 lldb::opaque_compiler_type_t type) {
3031 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
3032 ->GetDeclContextForType(GetQualType(type));
3033 if (!decl_ctx)
3034 return false;
3035
3036 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
3037 return false;
3038
3039 clang::ObjCInterfaceDecl *result_iface_decl =
3040 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
3041
3042 ClangASTMetadata *ast_metadata =
3043 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
3044 if (!ast_metadata)
3045 return false;
3046 return (ast_metadata->GetISAPtr() != 0);
3047}
3048
3049bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
3050 return GetQualType(type).getUnqualifiedType()->isCharType();
3051}
3052
3053bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
3054 const bool allow_completion = false;
3055 return GetCompleteQualType(getASTContext(), GetQualType(type),
3056 allow_completion);
3057}
3058
3059bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
3060 return GetQualType(type).isConstQualified();
3061}
3062
3063bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
3064 uint32_t &length) {
3065 CompilerType pointee_or_element_clang_type;
3066 length = 0;
3067 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
3068
3069 if (!pointee_or_element_clang_type.IsValid())
3070 return false;
3071
3072 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
3073 if (pointee_or_element_clang_type.IsCharType()) {
3074 if (type_flags.Test(eTypeIsArray)) {
Adrian Prantl05097242018-04-30 16:49:04 +00003075 // We know the size of the array and it could be a C string since it is
3076 // an array of characters
Kate Stoneb9c1b512016-09-06 20:57:50 +00003077 length = llvm::cast<clang::ConstantArrayType>(
3078 GetCanonicalQualType(type).getTypePtr())
3079 ->getSize()
3080 .getLimitedValue();
3081 }
3082 return true;
3083 }
3084 }
3085 return false;
3086}
3087
3088bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
3089 bool *is_variadic_ptr) {
3090 if (type) {
3091 clang::QualType qual_type(GetCanonicalQualType(type));
3092
3093 if (qual_type->isFunctionType()) {
3094 if (is_variadic_ptr) {
3095 const clang::FunctionProtoType *function_proto_type =
3096 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3097 if (function_proto_type)
3098 *is_variadic_ptr = function_proto_type->isVariadic();
3099 else
3100 *is_variadic_ptr = false;
3101 }
3102 return true;
3103 }
3104
Greg Claytond8d4a572015-08-11 21:38:15 +00003105 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003106 switch (type_class) {
3107 default:
3108 break;
3109 case clang::Type::Typedef:
3110 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
3111 ->getDecl()
3112 ->getUnderlyingType()
3113 .getAsOpaquePtr(),
3114 nullptr);
3115 case clang::Type::Auto:
3116 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
3117 ->getDeducedType()
3118 .getAsOpaquePtr(),
3119 nullptr);
3120 case clang::Type::Elaborated:
3121 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
3122 ->getNamedType()
3123 .getAsOpaquePtr(),
3124 nullptr);
3125 case clang::Type::Paren:
3126 return IsFunctionType(
3127 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3128 nullptr);
3129 case clang::Type::LValueReference:
3130 case clang::Type::RValueReference: {
3131 const clang::ReferenceType *reference_type =
3132 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3133 if (reference_type)
3134 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
3135 nullptr);
3136 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00003137 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003138 }
3139 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003140}
3141
3142// Used to detect "Homogeneous Floating-point Aggregates"
3143uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003144ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
3145 CompilerType *base_type_ptr) {
3146 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003147 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003148
3149 clang::QualType qual_type(GetCanonicalQualType(type));
3150 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3151 switch (type_class) {
3152 case clang::Type::Record:
3153 if (GetCompleteType(type)) {
3154 const clang::CXXRecordDecl *cxx_record_decl =
3155 qual_type->getAsCXXRecordDecl();
3156 if (cxx_record_decl) {
3157 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3158 return 0;
3159 }
3160 const clang::RecordType *record_type =
3161 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3162 if (record_type) {
3163 const clang::RecordDecl *record_decl = record_type->getDecl();
3164 if (record_decl) {
3165 // We are looking for a structure that contains only floating point
3166 // types
3167 clang::RecordDecl::field_iterator field_pos,
3168 field_end = record_decl->field_end();
3169 uint32_t num_fields = 0;
3170 bool is_hva = false;
3171 bool is_hfa = false;
3172 clang::QualType base_qual_type;
3173 uint64_t base_bitwidth = 0;
3174 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3175 ++field_pos) {
3176 clang::QualType field_qual_type = field_pos->getType();
3177 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
3178 if (field_qual_type->isFloatingType()) {
3179 if (field_qual_type->isComplexType())
3180 return 0;
3181 else {
3182 if (num_fields == 0)
3183 base_qual_type = field_qual_type;
3184 else {
3185 if (is_hva)
3186 return 0;
3187 is_hfa = true;
3188 if (field_qual_type.getTypePtr() !=
3189 base_qual_type.getTypePtr())
3190 return 0;
3191 }
3192 }
3193 } else if (field_qual_type->isVectorType() ||
3194 field_qual_type->isExtVectorType()) {
3195 if (num_fields == 0) {
3196 base_qual_type = field_qual_type;
3197 base_bitwidth = field_bitwidth;
3198 } else {
3199 if (is_hfa)
3200 return 0;
3201 is_hva = true;
3202 if (base_bitwidth != field_bitwidth)
3203 return 0;
3204 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3205 return 0;
3206 }
3207 } else
3208 return 0;
3209 ++num_fields;
3210 }
3211 if (base_type_ptr)
3212 *base_type_ptr = CompilerType(getASTContext(), base_qual_type);
3213 return num_fields;
3214 }
3215 }
3216 }
3217 break;
3218
3219 case clang::Type::Typedef:
3220 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3221 ->getDecl()
3222 ->getUnderlyingType()
3223 .getAsOpaquePtr(),
3224 base_type_ptr);
3225
3226 case clang::Type::Auto:
3227 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3228 ->getDeducedType()
3229 .getAsOpaquePtr(),
3230 base_type_ptr);
3231
3232 case clang::Type::Elaborated:
3233 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3234 ->getNamedType()
3235 .getAsOpaquePtr(),
3236 base_type_ptr);
3237 default:
3238 break;
3239 }
3240 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003241}
3242
Kate Stoneb9c1b512016-09-06 20:57:50 +00003243size_t ClangASTContext::GetNumberOfFunctionArguments(
3244 lldb::opaque_compiler_type_t type) {
3245 if (type) {
3246 clang::QualType qual_type(GetCanonicalQualType(type));
3247 const clang::FunctionProtoType *func =
3248 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3249 if (func)
3250 return func->getNumParams();
3251 }
3252 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003253}
3254
Greg Claytona1e5dc82015-08-11 22:53:00 +00003255CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00003256ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3257 const size_t index) {
3258 if (type) {
Greg Claytond8d4a572015-08-11 21:38:15 +00003259 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003260 const clang::FunctionProtoType *func =
3261 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3262 if (func) {
3263 if (index < func->getNumParams())
3264 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00003265 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003266 }
3267 return CompilerType();
3268}
3269
3270bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3271 if (type) {
3272 clang::QualType qual_type(GetCanonicalQualType(type));
3273
3274 if (qual_type->isFunctionPointerType())
3275 return true;
3276
3277 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3278 switch (type_class) {
3279 default:
3280 break;
3281 case clang::Type::Typedef:
3282 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3283 ->getDecl()
3284 ->getUnderlyingType()
3285 .getAsOpaquePtr());
3286 case clang::Type::Auto:
3287 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3288 ->getDeducedType()
3289 .getAsOpaquePtr());
3290 case clang::Type::Elaborated:
3291 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3292 ->getNamedType()
3293 .getAsOpaquePtr());
3294 case clang::Type::Paren:
3295 return IsFunctionPointerType(
3296 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3297
3298 case clang::Type::LValueReference:
3299 case clang::Type::RValueReference: {
3300 const clang::ReferenceType *reference_type =
3301 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3302 if (reference_type)
3303 return IsFunctionPointerType(
3304 reference_type->getPointeeType().getAsOpaquePtr());
3305 } break;
3306 }
3307 }
3308 return false;
3309}
3310
3311bool ClangASTContext::IsBlockPointerType(
3312 lldb::opaque_compiler_type_t type,
3313 CompilerType *function_pointer_type_ptr) {
3314 if (type) {
3315 clang::QualType qual_type(GetCanonicalQualType(type));
3316
3317 if (qual_type->isBlockPointerType()) {
3318 if (function_pointer_type_ptr) {
3319 const clang::BlockPointerType *block_pointer_type =
3320 qual_type->getAs<clang::BlockPointerType>();
3321 QualType pointee_type = block_pointer_type->getPointeeType();
Jonas Devlieghered5b44032019-02-13 06:25:41 +00003322 QualType function_pointer_type = m_ast_up->getPointerType(pointee_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003323 *function_pointer_type_ptr =
3324 CompilerType(getASTContext(), function_pointer_type);
3325 }
3326 return true;
3327 }
3328
3329 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3330 switch (type_class) {
3331 default:
3332 break;
3333 case clang::Type::Typedef:
3334 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3335 ->getDecl()
3336 ->getUnderlyingType()
3337 .getAsOpaquePtr(),
3338 function_pointer_type_ptr);
3339 case clang::Type::Auto:
3340 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3341 ->getDeducedType()
3342 .getAsOpaquePtr(),
3343 function_pointer_type_ptr);
3344 case clang::Type::Elaborated:
3345 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3346 ->getNamedType()
3347 .getAsOpaquePtr(),
3348 function_pointer_type_ptr);
3349 case clang::Type::Paren:
3350 return IsBlockPointerType(
3351 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3352 function_pointer_type_ptr);
3353
3354 case clang::Type::LValueReference:
3355 case clang::Type::RValueReference: {
3356 const clang::ReferenceType *reference_type =
3357 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3358 if (reference_type)
3359 return IsBlockPointerType(
3360 reference_type->getPointeeType().getAsOpaquePtr(),
3361 function_pointer_type_ptr);
3362 } break;
3363 }
3364 }
3365 return false;
3366}
3367
3368bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3369 bool &is_signed) {
3370 if (!type)
3371 return false;
3372
3373 clang::QualType qual_type(GetCanonicalQualType(type));
3374 const clang::BuiltinType *builtin_type =
3375 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3376
3377 if (builtin_type) {
3378 if (builtin_type->isInteger()) {
3379 is_signed = builtin_type->isSignedInteger();
3380 return true;
3381 }
3382 }
3383
3384 return false;
3385}
3386
3387bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3388 bool &is_signed) {
3389 if (type) {
3390 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3391 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3392
3393 if (enum_type) {
3394 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3395 is_signed);
3396 return true;
3397 }
3398 }
3399
3400 return false;
3401}
3402
3403bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3404 CompilerType *pointee_type) {
3405 if (type) {
3406 clang::QualType qual_type(GetCanonicalQualType(type));
3407 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3408 switch (type_class) {
3409 case clang::Type::Builtin:
3410 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3411 default:
3412 break;
3413 case clang::BuiltinType::ObjCId:
3414 case clang::BuiltinType::ObjCClass:
3415 return true;
3416 }
3417 return false;
3418 case clang::Type::ObjCObjectPointer:
3419 if (pointee_type)
3420 pointee_type->SetCompilerType(
3421 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3422 ->getPointeeType());
3423 return true;
3424 case clang::Type::BlockPointer:
3425 if (pointee_type)
3426 pointee_type->SetCompilerType(
3427 getASTContext(),
3428 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3429 return true;
3430 case clang::Type::Pointer:
3431 if (pointee_type)
3432 pointee_type->SetCompilerType(
3433 getASTContext(),
3434 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3435 return true;
3436 case clang::Type::MemberPointer:
3437 if (pointee_type)
3438 pointee_type->SetCompilerType(
3439 getASTContext(),
3440 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3441 return true;
3442 case clang::Type::Typedef:
3443 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3444 ->getDecl()
3445 ->getUnderlyingType()
3446 .getAsOpaquePtr(),
3447 pointee_type);
3448 case clang::Type::Auto:
3449 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3450 ->getDeducedType()
3451 .getAsOpaquePtr(),
3452 pointee_type);
3453 case clang::Type::Elaborated:
3454 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3455 ->getNamedType()
3456 .getAsOpaquePtr(),
3457 pointee_type);
3458 case clang::Type::Paren:
3459 return IsPointerType(
3460 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3461 pointee_type);
3462 default:
3463 break;
3464 }
3465 }
3466 if (pointee_type)
3467 pointee_type->Clear();
3468 return false;
3469}
3470
3471bool ClangASTContext::IsPointerOrReferenceType(
3472 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3473 if (type) {
3474 clang::QualType qual_type(GetCanonicalQualType(type));
3475 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3476 switch (type_class) {
3477 case clang::Type::Builtin:
3478 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3479 default:
3480 break;
3481 case clang::BuiltinType::ObjCId:
3482 case clang::BuiltinType::ObjCClass:
3483 return true;
3484 }
3485 return false;
3486 case clang::Type::ObjCObjectPointer:
3487 if (pointee_type)
3488 pointee_type->SetCompilerType(
3489 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3490 ->getPointeeType());
3491 return true;
3492 case clang::Type::BlockPointer:
3493 if (pointee_type)
3494 pointee_type->SetCompilerType(
3495 getASTContext(),
3496 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3497 return true;
3498 case clang::Type::Pointer:
3499 if (pointee_type)
3500 pointee_type->SetCompilerType(
3501 getASTContext(),
3502 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3503 return true;
3504 case clang::Type::MemberPointer:
3505 if (pointee_type)
3506 pointee_type->SetCompilerType(
3507 getASTContext(),
3508 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3509 return true;
3510 case clang::Type::LValueReference:
3511 if (pointee_type)
3512 pointee_type->SetCompilerType(
3513 getASTContext(),
3514 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3515 return true;
3516 case clang::Type::RValueReference:
3517 if (pointee_type)
3518 pointee_type->SetCompilerType(
3519 getASTContext(),
3520 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3521 return true;
3522 case clang::Type::Typedef:
3523 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3524 ->getDecl()
3525 ->getUnderlyingType()
3526 .getAsOpaquePtr(),
3527 pointee_type);
3528 case clang::Type::Auto:
3529 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3530 ->getDeducedType()
3531 .getAsOpaquePtr(),
3532 pointee_type);
3533 case clang::Type::Elaborated:
3534 return IsPointerOrReferenceType(
3535 llvm::cast<clang::ElaboratedType>(qual_type)
3536 ->getNamedType()
3537 .getAsOpaquePtr(),
3538 pointee_type);
3539 case clang::Type::Paren:
3540 return IsPointerOrReferenceType(
3541 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3542 pointee_type);
3543 default:
3544 break;
3545 }
3546 }
3547 if (pointee_type)
3548 pointee_type->Clear();
3549 return false;
3550}
3551
3552bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3553 CompilerType *pointee_type,
3554 bool *is_rvalue) {
3555 if (type) {
3556 clang::QualType qual_type(GetCanonicalQualType(type));
3557 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3558
3559 switch (type_class) {
3560 case clang::Type::LValueReference:
3561 if (pointee_type)
3562 pointee_type->SetCompilerType(
3563 getASTContext(),
3564 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3565 if (is_rvalue)
3566 *is_rvalue = false;
3567 return true;
3568 case clang::Type::RValueReference:
3569 if (pointee_type)
3570 pointee_type->SetCompilerType(
3571 getASTContext(),
3572 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3573 if (is_rvalue)
3574 *is_rvalue = true;
3575 return true;
3576 case clang::Type::Typedef:
3577 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3578 ->getDecl()
3579 ->getUnderlyingType()
3580 .getAsOpaquePtr(),
3581 pointee_type, is_rvalue);
3582 case clang::Type::Auto:
3583 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3584 ->getDeducedType()
3585 .getAsOpaquePtr(),
3586 pointee_type, is_rvalue);
3587 case clang::Type::Elaborated:
3588 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3589 ->getNamedType()
3590 .getAsOpaquePtr(),
3591 pointee_type, is_rvalue);
3592 case clang::Type::Paren:
3593 return IsReferenceType(
3594 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3595 pointee_type, is_rvalue);
3596
3597 default:
3598 break;
3599 }
3600 }
3601 if (pointee_type)
3602 pointee_type->Clear();
3603 return false;
3604}
3605
3606bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3607 uint32_t &count, bool &is_complex) {
3608 if (type) {
3609 clang::QualType qual_type(GetCanonicalQualType(type));
3610
3611 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3612 qual_type->getCanonicalTypeInternal())) {
3613 clang::BuiltinType::Kind kind = BT->getKind();
3614 if (kind >= clang::BuiltinType::Float &&
3615 kind <= clang::BuiltinType::LongDouble) {
3616 count = 1;
3617 is_complex = false;
3618 return true;
3619 }
3620 } else if (const clang::ComplexType *CT =
3621 llvm::dyn_cast<clang::ComplexType>(
3622 qual_type->getCanonicalTypeInternal())) {
3623 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3624 is_complex)) {
3625 count = 2;
3626 is_complex = true;
3627 return true;
3628 }
3629 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3630 qual_type->getCanonicalTypeInternal())) {
3631 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3632 is_complex)) {
3633 count = VT->getNumElements();
3634 is_complex = false;
3635 return true;
3636 }
3637 }
3638 }
3639 count = 0;
3640 is_complex = false;
3641 return false;
3642}
3643
3644bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3645 if (!type)
3646 return false;
3647
3648 clang::QualType qual_type(GetQualType(type));
3649 const clang::TagType *tag_type =
3650 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3651 if (tag_type) {
3652 clang::TagDecl *tag_decl = tag_type->getDecl();
3653 if (tag_decl)
3654 return tag_decl->isCompleteDefinition();
3655 return false;
3656 } else {
3657 const clang::ObjCObjectType *objc_class_type =
3658 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3659 if (objc_class_type) {
3660 clang::ObjCInterfaceDecl *class_interface_decl =
3661 objc_class_type->getInterface();
3662 if (class_interface_decl)
3663 return class_interface_decl->getDefinition() != nullptr;
3664 return false;
3665 }
3666 }
3667 return true;
3668}
3669
3670bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
3671 if (type) {
3672 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3673
3674 const clang::ObjCObjectPointerType *obj_pointer_type =
3675 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3676
3677 if (obj_pointer_type)
3678 return obj_pointer_type->isObjCClassType();
3679 }
3680 return false;
3681}
3682
3683bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3684 if (ClangUtil::IsClangType(type))
3685 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3686 return false;
3687}
3688
3689bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3690 if (!type)
3691 return false;
3692 clang::QualType qual_type(GetCanonicalQualType(type));
3693 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3694 return (type_class == clang::Type::Record);
3695}
3696
3697bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3698 if (!type)
3699 return false;
3700 clang::QualType qual_type(GetCanonicalQualType(type));
3701 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3702 return (type_class == clang::Type::Enum);
3703}
3704
3705bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3706 if (type) {
3707 clang::QualType qual_type(GetCanonicalQualType(type));
3708 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3709 switch (type_class) {
3710 case clang::Type::Record:
3711 if (GetCompleteType(type)) {
3712 const clang::RecordType *record_type =
3713 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3714 const clang::RecordDecl *record_decl = record_type->getDecl();
3715 if (record_decl) {
3716 const clang::CXXRecordDecl *cxx_record_decl =
3717 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3718 if (cxx_record_decl)
3719 return cxx_record_decl->isPolymorphic();
Greg Claytond8d4a572015-08-11 21:38:15 +00003720 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003721 }
3722 break;
3723
3724 default:
3725 break;
3726 }
3727 }
3728 return false;
3729}
3730
3731bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3732 CompilerType *dynamic_pointee_type,
3733 bool check_cplusplus,
3734 bool check_objc) {
3735 clang::QualType pointee_qual_type;
3736 if (type) {
3737 clang::QualType qual_type(GetCanonicalQualType(type));
3738 bool success = false;
3739 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3740 switch (type_class) {
3741 case clang::Type::Builtin:
3742 if (check_objc &&
3743 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3744 clang::BuiltinType::ObjCId) {
3745 if (dynamic_pointee_type)
3746 dynamic_pointee_type->SetCompilerType(this, type);
3747 return true;
3748 }
3749 break;
3750
3751 case clang::Type::ObjCObjectPointer:
3752 if (check_objc) {
3753 if (auto objc_pointee_type =
3754 qual_type->getPointeeType().getTypePtrOrNull()) {
3755 if (auto objc_object_type =
3756 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3757 objc_pointee_type)) {
3758 if (objc_object_type->isObjCClass())
3759 return false;
3760 }
3761 }
3762 if (dynamic_pointee_type)
3763 dynamic_pointee_type->SetCompilerType(
3764 getASTContext(),
3765 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3766 ->getPointeeType());
3767 return true;
3768 }
3769 break;
3770
3771 case clang::Type::Pointer:
3772 pointee_qual_type =
3773 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3774 success = true;
3775 break;
3776
3777 case clang::Type::LValueReference:
3778 case clang::Type::RValueReference:
3779 pointee_qual_type =
3780 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3781 success = true;
3782 break;
3783
3784 case clang::Type::Typedef:
3785 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3786 ->getDecl()
3787 ->getUnderlyingType()
3788 .getAsOpaquePtr(),
3789 dynamic_pointee_type, check_cplusplus,
3790 check_objc);
3791
3792 case clang::Type::Auto:
3793 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3794 ->getDeducedType()
3795 .getAsOpaquePtr(),
3796 dynamic_pointee_type, check_cplusplus,
3797 check_objc);
3798
3799 case clang::Type::Elaborated:
3800 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3801 ->getNamedType()
3802 .getAsOpaquePtr(),
3803 dynamic_pointee_type, check_cplusplus,
3804 check_objc);
3805
3806 case clang::Type::Paren:
3807 return IsPossibleDynamicType(
3808 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3809 dynamic_pointee_type, check_cplusplus, check_objc);
3810 default:
3811 break;
3812 }
3813
3814 if (success) {
3815 // Check to make sure what we are pointing too is a possible dynamic C++
Adrian Prantl05097242018-04-30 16:49:04 +00003816 // type We currently accept any "void *" (in case we have a class that
3817 // has been watered down to an opaque pointer) and virtual C++ classes.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003818 const clang::Type::TypeClass pointee_type_class =
3819 pointee_qual_type.getCanonicalType()->getTypeClass();
3820 switch (pointee_type_class) {
3821 case clang::Type::Builtin:
3822 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3823 case clang::BuiltinType::UnknownAny:
3824 case clang::BuiltinType::Void:
3825 if (dynamic_pointee_type)
3826 dynamic_pointee_type->SetCompilerType(getASTContext(),
3827 pointee_qual_type);
3828 return true;
3829 default:
3830 break;
3831 }
3832 break;
3833
3834 case clang::Type::Record:
3835 if (check_cplusplus) {
3836 clang::CXXRecordDecl *cxx_record_decl =
3837 pointee_qual_type->getAsCXXRecordDecl();
3838 if (cxx_record_decl) {
3839 bool is_complete = cxx_record_decl->isCompleteDefinition();
3840
3841 if (is_complete)
3842 success = cxx_record_decl->isDynamicClass();
3843 else {
3844 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3845 getASTContext(), cxx_record_decl);
3846 if (metadata)
3847 success = metadata->GetIsDynamicCXXType();
3848 else {
3849 is_complete = CompilerType(getASTContext(), pointee_qual_type)
3850 .GetCompleteType();
3851 if (is_complete)
3852 success = cxx_record_decl->isDynamicClass();
3853 else
3854 success = false;
3855 }
3856 }
3857
3858 if (success) {
3859 if (dynamic_pointee_type)
3860 dynamic_pointee_type->SetCompilerType(getASTContext(),
3861 pointee_qual_type);
3862 return true;
3863 }
3864 }
3865 }
3866 break;
3867
3868 case clang::Type::ObjCObject:
3869 case clang::Type::ObjCInterface:
3870 if (check_objc) {
3871 if (dynamic_pointee_type)
3872 dynamic_pointee_type->SetCompilerType(getASTContext(),
3873 pointee_qual_type);
3874 return true;
3875 }
3876 break;
3877
3878 default:
3879 break;
3880 }
3881 }
3882 }
3883 if (dynamic_pointee_type)
3884 dynamic_pointee_type->Clear();
3885 return false;
3886}
3887
3888bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3889 if (!type)
3890 return false;
3891
3892 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3893}
3894
3895bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3896 if (!type)
3897 return false;
3898 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3899}
3900
3901bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3902 if (!type)
3903 return false;
3904 return GetCanonicalQualType(type)->isVoidType();
3905}
3906
3907bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3908 return ClangASTContextSupportsLanguage(language);
3909}
3910
3911bool ClangASTContext::GetCXXClassName(const CompilerType &type,
3912 std::string &class_name) {
3913 if (type) {
3914 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3915 if (!qual_type.isNull()) {
3916 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3917 if (cxx_record_decl) {
3918 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3919 return true;
3920 }
3921 }
3922 }
3923 class_name.clear();
3924 return false;
3925}
3926
3927bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3928 if (!type)
3929 return false;
3930
3931 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Jonas Devliegherea6682a42018-12-15 00:15:33 +00003932 return !qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003933}
3934
3935bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3936 if (!type)
3937 return false;
3938 clang::QualType qual_type(GetCanonicalQualType(type));
3939 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3940 if (tag_type)
3941 return tag_type->isBeingDefined();
3942 return false;
3943}
3944
3945bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3946 CompilerType *class_type_ptr) {
3947 if (!type)
3948 return false;
3949
3950 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3951
3952 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3953 if (class_type_ptr) {
3954 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3955 const clang::ObjCObjectPointerType *obj_pointer_type =
3956 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3957 if (obj_pointer_type == nullptr)
3958 class_type_ptr->Clear();
3959 else
3960 class_type_ptr->SetCompilerType(
3961 type.GetTypeSystem(),
3962 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3963 .getAsOpaquePtr());
3964 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003965 }
3966 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003967 }
3968 if (class_type_ptr)
3969 class_type_ptr->Clear();
3970 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003971}
3972
Kate Stoneb9c1b512016-09-06 20:57:50 +00003973bool ClangASTContext::GetObjCClassName(const CompilerType &type,
3974 std::string &class_name) {
3975 if (!type)
3976 return false;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003977
Kate Stoneb9c1b512016-09-06 20:57:50 +00003978 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3979
3980 const clang::ObjCObjectType *object_type =
3981 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3982 if (object_type) {
3983 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3984 if (interface) {
3985 class_name = interface->getNameAsString();
3986 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00003987 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003988 }
3989 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003990}
3991
Greg Claytond8d4a572015-08-11 21:38:15 +00003992// Type Completion
Greg Claytond8d4a572015-08-11 21:38:15 +00003993
Kate Stoneb9c1b512016-09-06 20:57:50 +00003994bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
3995 if (!type)
3996 return false;
3997 const bool allow_completion = true;
3998 return GetCompleteQualType(getASTContext(), GetQualType(type),
3999 allow_completion);
Greg Claytond8d4a572015-08-11 21:38:15 +00004000}
4001
Kate Stoneb9c1b512016-09-06 20:57:50 +00004002ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
4003 std::string type_name;
4004 if (type) {
4005 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
4006 clang::QualType qual_type(GetQualType(type));
4007 printing_policy.SuppressTagKeyword = true;
4008 const clang::TypedefType *typedef_type =
4009 qual_type->getAs<clang::TypedefType>();
4010 if (typedef_type) {
4011 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
4012 type_name = typedef_decl->getQualifiedNameAsString();
4013 } else {
4014 type_name = qual_type.getAsString(printing_policy);
Greg Claytond8d4a572015-08-11 21:38:15 +00004015 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004016 }
4017 return ConstString(type_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00004018}
4019
4020uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004021ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
4022 CompilerType *pointee_or_element_clang_type) {
4023 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004024 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004025
4026 if (pointee_or_element_clang_type)
4027 pointee_or_element_clang_type->Clear();
4028
4029 clang::QualType qual_type(GetQualType(type));
4030
4031 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4032 switch (type_class) {
Sean Callananddf802a2017-06-02 01:24:18 +00004033 case clang::Type::Attributed:
4034 return GetTypeInfo(
4035 qual_type->getAs<clang::AttributedType>()
4036 ->getModifiedType().getAsOpaquePtr(),
4037 pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004038 case clang::Type::Builtin: {
4039 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
4040 qual_type->getCanonicalTypeInternal());
4041
4042 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
4043 switch (builtin_type->getKind()) {
4044 case clang::BuiltinType::ObjCId:
4045 case clang::BuiltinType::ObjCClass:
4046 if (pointee_or_element_clang_type)
4047 pointee_or_element_clang_type->SetCompilerType(
4048 getASTContext(), getASTContext()->ObjCBuiltinClassTy);
4049 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
4050 break;
4051
4052 case clang::BuiltinType::ObjCSel:
4053 if (pointee_or_element_clang_type)
4054 pointee_or_element_clang_type->SetCompilerType(getASTContext(),
4055 getASTContext()->CharTy);
4056 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
4057 break;
4058
4059 case clang::BuiltinType::Bool:
4060 case clang::BuiltinType::Char_U:
4061 case clang::BuiltinType::UChar:
4062 case clang::BuiltinType::WChar_U:
4063 case clang::BuiltinType::Char16:
4064 case clang::BuiltinType::Char32:
4065 case clang::BuiltinType::UShort:
4066 case clang::BuiltinType::UInt:
4067 case clang::BuiltinType::ULong:
4068 case clang::BuiltinType::ULongLong:
4069 case clang::BuiltinType::UInt128:
4070 case clang::BuiltinType::Char_S:
4071 case clang::BuiltinType::SChar:
4072 case clang::BuiltinType::WChar_S:
4073 case clang::BuiltinType::Short:
4074 case clang::BuiltinType::Int:
4075 case clang::BuiltinType::Long:
4076 case clang::BuiltinType::LongLong:
4077 case clang::BuiltinType::Int128:
4078 case clang::BuiltinType::Float:
4079 case clang::BuiltinType::Double:
4080 case clang::BuiltinType::LongDouble:
4081 builtin_type_flags |= eTypeIsScalar;
4082 if (builtin_type->isInteger()) {
4083 builtin_type_flags |= eTypeIsInteger;
4084 if (builtin_type->isSignedInteger())
4085 builtin_type_flags |= eTypeIsSigned;
4086 } else if (builtin_type->isFloatingPoint())
4087 builtin_type_flags |= eTypeIsFloat;
4088 break;
4089 default:
4090 break;
4091 }
4092 return builtin_type_flags;
4093 }
4094
4095 case clang::Type::BlockPointer:
4096 if (pointee_or_element_clang_type)
4097 pointee_or_element_clang_type->SetCompilerType(
4098 getASTContext(), qual_type->getPointeeType());
4099 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
4100
4101 case clang::Type::Complex: {
4102 uint32_t complex_type_flags =
4103 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
4104 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
4105 qual_type->getCanonicalTypeInternal());
4106 if (complex_type) {
4107 clang::QualType complex_element_type(complex_type->getElementType());
4108 if (complex_element_type->isIntegerType())
4109 complex_type_flags |= eTypeIsFloat;
4110 else if (complex_element_type->isFloatingType())
4111 complex_type_flags |= eTypeIsInteger;
4112 }
4113 return complex_type_flags;
4114 } break;
4115
4116 case clang::Type::ConstantArray:
4117 case clang::Type::DependentSizedArray:
4118 case clang::Type::IncompleteArray:
4119 case clang::Type::VariableArray:
4120 if (pointee_or_element_clang_type)
4121 pointee_or_element_clang_type->SetCompilerType(
4122 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
4123 ->getElementType());
4124 return eTypeHasChildren | eTypeIsArray;
4125
4126 case clang::Type::DependentName:
4127 return 0;
4128 case clang::Type::DependentSizedExtVector:
4129 return eTypeHasChildren | eTypeIsVector;
4130 case clang::Type::DependentTemplateSpecialization:
4131 return eTypeIsTemplate;
4132 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004133 return CompilerType(
4134 getASTContext(),
4135 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
4136 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004137
4138 case clang::Type::Enum:
4139 if (pointee_or_element_clang_type)
4140 pointee_or_element_clang_type->SetCompilerType(
4141 getASTContext(),
4142 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
4143 return eTypeIsEnumeration | eTypeHasValue;
4144
4145 case clang::Type::Auto:
4146 return CompilerType(
4147 getASTContext(),
4148 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4149 .GetTypeInfo(pointee_or_element_clang_type);
4150 case clang::Type::Elaborated:
4151 return CompilerType(
4152 getASTContext(),
4153 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4154 .GetTypeInfo(pointee_or_element_clang_type);
4155 case clang::Type::Paren:
4156 return CompilerType(getASTContext(),
4157 llvm::cast<clang::ParenType>(qual_type)->desugar())
4158 .GetTypeInfo(pointee_or_element_clang_type);
4159
4160 case clang::Type::FunctionProto:
4161 return eTypeIsFuncPrototype | eTypeHasValue;
4162 case clang::Type::FunctionNoProto:
4163 return eTypeIsFuncPrototype | eTypeHasValue;
4164 case clang::Type::InjectedClassName:
4165 return 0;
4166
4167 case clang::Type::LValueReference:
4168 case clang::Type::RValueReference:
4169 if (pointee_or_element_clang_type)
4170 pointee_or_element_clang_type->SetCompilerType(
4171 getASTContext(),
4172 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4173 ->getPointeeType());
4174 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4175
4176 case clang::Type::MemberPointer:
4177 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4178
4179 case clang::Type::ObjCObjectPointer:
4180 if (pointee_or_element_clang_type)
4181 pointee_or_element_clang_type->SetCompilerType(
4182 getASTContext(), qual_type->getPointeeType());
4183 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4184 eTypeHasValue;
4185
4186 case clang::Type::ObjCObject:
4187 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4188 case clang::Type::ObjCInterface:
4189 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4190
4191 case clang::Type::Pointer:
4192 if (pointee_or_element_clang_type)
4193 pointee_or_element_clang_type->SetCompilerType(
4194 getASTContext(), qual_type->getPointeeType());
4195 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4196
4197 case clang::Type::Record:
4198 if (qual_type->getAsCXXRecordDecl())
4199 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4200 else
4201 return eTypeHasChildren | eTypeIsStructUnion;
4202 break;
4203 case clang::Type::SubstTemplateTypeParm:
4204 return eTypeIsTemplate;
4205 case clang::Type::TemplateTypeParm:
4206 return eTypeIsTemplate;
4207 case clang::Type::TemplateSpecialization:
4208 return eTypeIsTemplate;
4209
4210 case clang::Type::Typedef:
4211 return eTypeIsTypedef |
4212 CompilerType(getASTContext(),
4213 llvm::cast<clang::TypedefType>(qual_type)
4214 ->getDecl()
4215 ->getUnderlyingType())
4216 .GetTypeInfo(pointee_or_element_clang_type);
4217 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004218 return CompilerType(getASTContext(),
4219 llvm::cast<clang::TypeOfExprType>(qual_type)
4220 ->getUnderlyingExpr()
4221 ->getType())
4222 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004223 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004224 return CompilerType(
4225 getASTContext(),
4226 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4227 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004228 case clang::Type::UnresolvedUsing:
4229 return 0;
4230
4231 case clang::Type::ExtVector:
4232 case clang::Type::Vector: {
4233 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4234 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4235 qual_type->getCanonicalTypeInternal());
4236 if (vector_type) {
4237 if (vector_type->isIntegerType())
4238 vector_type_flags |= eTypeIsFloat;
4239 else if (vector_type->isFloatingType())
4240 vector_type_flags |= eTypeIsInteger;
4241 }
4242 return vector_type_flags;
4243 }
4244 default:
4245 return 0;
4246 }
4247 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004248}
4249
Greg Claytond8d4a572015-08-11 21:38:15 +00004250lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004251ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4252 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004253 return lldb::eLanguageTypeC;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004254
4255 // If the type is a reference, then resolve it to what it refers to first:
4256 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4257 if (qual_type->isAnyPointerType()) {
4258 if (qual_type->isObjCObjectPointerType())
4259 return lldb::eLanguageTypeObjC;
Adrian Prantl1db0f0c2019-05-02 23:07:23 +00004260 if (qual_type->getPointeeCXXRecordDecl())
4261 return lldb::eLanguageTypeC_plus_plus;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004262
4263 clang::QualType pointee_type(qual_type->getPointeeType());
Adrian Prantl1db0f0c2019-05-02 23:07:23 +00004264 if (pointee_type->getPointeeCXXRecordDecl())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004265 return lldb::eLanguageTypeC_plus_plus;
4266 if (pointee_type->isObjCObjectOrInterfaceType())
4267 return lldb::eLanguageTypeObjC;
4268 if (pointee_type->isObjCClassType())
4269 return lldb::eLanguageTypeObjC;
4270 if (pointee_type.getTypePtr() ==
4271 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4272 return lldb::eLanguageTypeObjC;
4273 } else {
4274 if (qual_type->isObjCObjectOrInterfaceType())
4275 return lldb::eLanguageTypeObjC;
4276 if (qual_type->getAsCXXRecordDecl())
4277 return lldb::eLanguageTypeC_plus_plus;
4278 switch (qual_type->getTypeClass()) {
4279 default:
4280 break;
4281 case clang::Type::Builtin:
4282 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4283 default:
4284 case clang::BuiltinType::Void:
4285 case clang::BuiltinType::Bool:
4286 case clang::BuiltinType::Char_U:
4287 case clang::BuiltinType::UChar:
4288 case clang::BuiltinType::WChar_U:
4289 case clang::BuiltinType::Char16:
4290 case clang::BuiltinType::Char32:
4291 case clang::BuiltinType::UShort:
4292 case clang::BuiltinType::UInt:
4293 case clang::BuiltinType::ULong:
4294 case clang::BuiltinType::ULongLong:
4295 case clang::BuiltinType::UInt128:
4296 case clang::BuiltinType::Char_S:
4297 case clang::BuiltinType::SChar:
4298 case clang::BuiltinType::WChar_S:
4299 case clang::BuiltinType::Short:
4300 case clang::BuiltinType::Int:
4301 case clang::BuiltinType::Long:
4302 case clang::BuiltinType::LongLong:
4303 case clang::BuiltinType::Int128:
4304 case clang::BuiltinType::Float:
4305 case clang::BuiltinType::Double:
4306 case clang::BuiltinType::LongDouble:
4307 break;
4308
4309 case clang::BuiltinType::NullPtr:
4310 return eLanguageTypeC_plus_plus;
4311
4312 case clang::BuiltinType::ObjCId:
4313 case clang::BuiltinType::ObjCClass:
4314 case clang::BuiltinType::ObjCSel:
4315 return eLanguageTypeObjC;
4316
4317 case clang::BuiltinType::Dependent:
4318 case clang::BuiltinType::Overload:
4319 case clang::BuiltinType::BoundMember:
4320 case clang::BuiltinType::UnknownAny:
4321 break;
4322 }
4323 break;
4324 case clang::Type::Typedef:
4325 return CompilerType(getASTContext(),
4326 llvm::cast<clang::TypedefType>(qual_type)
4327 ->getDecl()
4328 ->getUnderlyingType())
4329 .GetMinimumLanguage();
4330 }
4331 }
4332 return lldb::eLanguageTypeC;
Greg Claytond8d4a572015-08-11 21:38:15 +00004333}
4334
4335lldb::TypeClass
Kate Stoneb9c1b512016-09-06 20:57:50 +00004336ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4337 if (!type)
4338 return lldb::eTypeClassInvalid;
4339
4340 clang::QualType qual_type(GetQualType(type));
4341
4342 switch (qual_type->getTypeClass()) {
4343 case clang::Type::UnaryTransform:
4344 break;
4345 case clang::Type::FunctionNoProto:
4346 return lldb::eTypeClassFunction;
4347 case clang::Type::FunctionProto:
4348 return lldb::eTypeClassFunction;
4349 case clang::Type::IncompleteArray:
4350 return lldb::eTypeClassArray;
4351 case clang::Type::VariableArray:
4352 return lldb::eTypeClassArray;
4353 case clang::Type::ConstantArray:
4354 return lldb::eTypeClassArray;
4355 case clang::Type::DependentSizedArray:
4356 return lldb::eTypeClassArray;
4357 case clang::Type::DependentSizedExtVector:
4358 return lldb::eTypeClassVector;
Fangrui Song8f284882018-07-13 22:40:40 +00004359 case clang::Type::DependentVector:
4360 return lldb::eTypeClassVector;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004361 case clang::Type::ExtVector:
4362 return lldb::eTypeClassVector;
4363 case clang::Type::Vector:
4364 return lldb::eTypeClassVector;
4365 case clang::Type::Builtin:
4366 return lldb::eTypeClassBuiltin;
4367 case clang::Type::ObjCObjectPointer:
4368 return lldb::eTypeClassObjCObjectPointer;
4369 case clang::Type::BlockPointer:
4370 return lldb::eTypeClassBlockPointer;
4371 case clang::Type::Pointer:
4372 return lldb::eTypeClassPointer;
4373 case clang::Type::LValueReference:
4374 return lldb::eTypeClassReference;
4375 case clang::Type::RValueReference:
4376 return lldb::eTypeClassReference;
4377 case clang::Type::MemberPointer:
4378 return lldb::eTypeClassMemberPointer;
4379 case clang::Type::Complex:
4380 if (qual_type->isComplexType())
4381 return lldb::eTypeClassComplexFloat;
4382 else
4383 return lldb::eTypeClassComplexInteger;
4384 case clang::Type::ObjCObject:
4385 return lldb::eTypeClassObjCObject;
4386 case clang::Type::ObjCInterface:
4387 return lldb::eTypeClassObjCInterface;
4388 case clang::Type::Record: {
4389 const clang::RecordType *record_type =
4390 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4391 const clang::RecordDecl *record_decl = record_type->getDecl();
4392 if (record_decl->isUnion())
4393 return lldb::eTypeClassUnion;
4394 else if (record_decl->isStruct())
4395 return lldb::eTypeClassStruct;
4396 else
4397 return lldb::eTypeClassClass;
4398 } break;
4399 case clang::Type::Enum:
4400 return lldb::eTypeClassEnumeration;
4401 case clang::Type::Typedef:
4402 return lldb::eTypeClassTypedef;
4403 case clang::Type::UnresolvedUsing:
4404 break;
4405 case clang::Type::Paren:
4406 return CompilerType(getASTContext(),
4407 llvm::cast<clang::ParenType>(qual_type)->desugar())
4408 .GetTypeClass();
4409 case clang::Type::Auto:
4410 return CompilerType(
4411 getASTContext(),
4412 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4413 .GetTypeClass();
4414 case clang::Type::Elaborated:
4415 return CompilerType(
4416 getASTContext(),
4417 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4418 .GetTypeClass();
4419
4420 case clang::Type::Attributed:
4421 break;
4422 case clang::Type::TemplateTypeParm:
4423 break;
4424 case clang::Type::SubstTemplateTypeParm:
4425 break;
4426 case clang::Type::SubstTemplateTypeParmPack:
4427 break;
4428 case clang::Type::InjectedClassName:
4429 break;
4430 case clang::Type::DependentName:
4431 break;
4432 case clang::Type::DependentTemplateSpecialization:
4433 break;
4434 case clang::Type::PackExpansion:
4435 break;
4436
4437 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004438 return CompilerType(getASTContext(),
4439 llvm::cast<clang::TypeOfExprType>(qual_type)
4440 ->getUnderlyingExpr()
4441 ->getType())
4442 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004443 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004444 return CompilerType(
4445 getASTContext(),
4446 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4447 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004448 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004449 return CompilerType(
4450 getASTContext(),
4451 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4452 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004453 case clang::Type::TemplateSpecialization:
4454 break;
Pavel Labath4f19fce22017-02-17 13:39:50 +00004455 case clang::Type::DeducedTemplateSpecialization:
4456 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004457 case clang::Type::Atomic:
4458 break;
4459 case clang::Type::Pipe:
4460 break;
4461
4462 // pointer type decayed from an array or function type.
4463 case clang::Type::Decayed:
4464 break;
4465 case clang::Type::Adjusted:
4466 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00004467 case clang::Type::ObjCTypeParam:
4468 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00004469
4470 case clang::Type::DependentAddressSpace:
4471 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004472 }
4473 // We don't know hot to display this type...
4474 return lldb::eTypeClassOther;
Greg Claytond8d4a572015-08-11 21:38:15 +00004475}
4476
Kate Stoneb9c1b512016-09-06 20:57:50 +00004477unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4478 if (type)
4479 return GetQualType(type).getQualifiers().getCVRQualifiers();
4480 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004481}
4482
Greg Claytond8d4a572015-08-11 21:38:15 +00004483// Creating related types
Greg Claytond8d4a572015-08-11 21:38:15 +00004484
Greg Claytona1e5dc82015-08-11 22:53:00 +00004485CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004486ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4487 uint64_t *stride) {
4488 if (type) {
4489 clang::QualType qual_type(GetCanonicalQualType(type));
4490
4491 const clang::Type *array_eletype =
4492 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4493
4494 if (!array_eletype)
4495 return CompilerType();
4496
4497 CompilerType element_type(getASTContext(),
4498 array_eletype->getCanonicalTypeUnqualified());
4499
4500 // TODO: the real stride will be >= this value.. find the real one!
4501 if (stride)
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00004502 if (Optional<uint64_t> size = element_type.GetByteSize(nullptr))
Adrian Prantld963a7c2019-01-15 18:07:52 +00004503 *stride = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004504
4505 return element_type;
4506 }
4507 return CompilerType();
4508}
4509
4510CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4511 uint64_t size) {
4512 if (type) {
4513 clang::QualType qual_type(GetCanonicalQualType(type));
4514 if (clang::ASTContext *ast_ctx = getASTContext()) {
4515 if (size != 0)
4516 return CompilerType(
4517 ast_ctx, ast_ctx->getConstantArrayType(
4518 qual_type, llvm::APInt(64, size),
4519 clang::ArrayType::ArraySizeModifier::Normal, 0));
4520 else
4521 return CompilerType(
4522 ast_ctx,
4523 ast_ctx->getIncompleteArrayType(
4524 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
Greg Claytond8d4a572015-08-11 21:38:15 +00004525 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004526 }
4527
4528 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004529}
4530
Greg Claytona1e5dc82015-08-11 22:53:00 +00004531CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004532ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4533 if (type)
4534 return CompilerType(getASTContext(), GetCanonicalQualType(type));
4535 return CompilerType();
4536}
4537
4538static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4539 clang::QualType qual_type) {
4540 if (qual_type->isPointerType())
4541 qual_type = ast->getPointerType(
4542 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4543 else
4544 qual_type = qual_type.getUnqualifiedType();
4545 qual_type.removeLocalConst();
4546 qual_type.removeLocalRestrict();
4547 qual_type.removeLocalVolatile();
4548 return qual_type;
Enrico Granata639392f2016-08-30 20:39:58 +00004549}
4550
4551CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004552ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4553 if (type)
4554 return CompilerType(
4555 getASTContext(),
4556 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4557 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004558}
4559
Kate Stoneb9c1b512016-09-06 20:57:50 +00004560int ClangASTContext::GetFunctionArgumentCount(
4561 lldb::opaque_compiler_type_t type) {
4562 if (type) {
4563 const clang::FunctionProtoType *func =
4564 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4565 if (func)
4566 return func->getNumParams();
4567 }
4568 return -1;
4569}
4570
4571CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4572 lldb::opaque_compiler_type_t type, size_t idx) {
4573 if (type) {
4574 const clang::FunctionProtoType *func =
4575 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4576 if (func) {
4577 const uint32_t num_args = func->getNumParams();
4578 if (idx < num_args)
4579 return CompilerType(getASTContext(), func->getParamType(idx));
4580 }
4581 }
4582 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004583}
4584
Greg Claytona1e5dc82015-08-11 22:53:00 +00004585CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004586ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4587 if (type) {
4588 clang::QualType qual_type(GetQualType(type));
4589 const clang::FunctionProtoType *func =
4590 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4591 if (func)
4592 return CompilerType(getASTContext(), func->getReturnType());
4593 }
4594 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004595}
4596
4597size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004598ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4599 size_t num_functions = 0;
4600 if (type) {
4601 clang::QualType qual_type(GetCanonicalQualType(type));
4602 switch (qual_type->getTypeClass()) {
4603 case clang::Type::Record:
4604 if (GetCompleteQualType(getASTContext(), qual_type)) {
4605 const clang::RecordType *record_type =
4606 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4607 const clang::RecordDecl *record_decl = record_type->getDecl();
4608 assert(record_decl);
4609 const clang::CXXRecordDecl *cxx_record_decl =
4610 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4611 if (cxx_record_decl)
4612 num_functions = std::distance(cxx_record_decl->method_begin(),
4613 cxx_record_decl->method_end());
4614 }
4615 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004616
Sean Callananf9c622a2016-09-30 18:44:43 +00004617 case clang::Type::ObjCObjectPointer: {
4618 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004619 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004620 const clang::ObjCInterfaceType *objc_interface_type =
4621 objc_class_type->getInterfaceType();
4622 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004623 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4624 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004625 clang::ObjCInterfaceDecl *class_interface_decl =
4626 objc_interface_type->getDecl();
4627 if (class_interface_decl) {
4628 num_functions = std::distance(class_interface_decl->meth_begin(),
4629 class_interface_decl->meth_end());
Greg Claytond8d4a572015-08-11 21:38:15 +00004630 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004631 }
4632 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004633 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004634
4635 case clang::Type::ObjCObject:
4636 case clang::Type::ObjCInterface:
4637 if (GetCompleteType(type)) {
4638 const clang::ObjCObjectType *objc_class_type =
4639 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4640 if (objc_class_type) {
4641 clang::ObjCInterfaceDecl *class_interface_decl =
4642 objc_class_type->getInterface();
4643 if (class_interface_decl)
4644 num_functions = std::distance(class_interface_decl->meth_begin(),
4645 class_interface_decl->meth_end());
4646 }
4647 }
4648 break;
4649
4650 case clang::Type::Typedef:
4651 return CompilerType(getASTContext(),
4652 llvm::cast<clang::TypedefType>(qual_type)
4653 ->getDecl()
4654 ->getUnderlyingType())
4655 .GetNumMemberFunctions();
4656
4657 case clang::Type::Auto:
4658 return CompilerType(
4659 getASTContext(),
4660 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4661 .GetNumMemberFunctions();
4662
4663 case clang::Type::Elaborated:
4664 return CompilerType(
4665 getASTContext(),
4666 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4667 .GetNumMemberFunctions();
4668
4669 case clang::Type::Paren:
4670 return CompilerType(getASTContext(),
4671 llvm::cast<clang::ParenType>(qual_type)->desugar())
4672 .GetNumMemberFunctions();
4673
4674 default:
4675 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004676 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004677 }
4678 return num_functions;
Greg Claytond8d4a572015-08-11 21:38:15 +00004679}
4680
4681TypeMemberFunctionImpl
Kate Stoneb9c1b512016-09-06 20:57:50 +00004682ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4683 size_t idx) {
4684 std::string name;
4685 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4686 CompilerType clang_type;
4687 CompilerDecl clang_decl;
4688 if (type) {
4689 clang::QualType qual_type(GetCanonicalQualType(type));
4690 switch (qual_type->getTypeClass()) {
4691 case clang::Type::Record:
4692 if (GetCompleteQualType(getASTContext(), qual_type)) {
4693 const clang::RecordType *record_type =
4694 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4695 const clang::RecordDecl *record_decl = record_type->getDecl();
4696 assert(record_decl);
4697 const clang::CXXRecordDecl *cxx_record_decl =
4698 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4699 if (cxx_record_decl) {
4700 auto method_iter = cxx_record_decl->method_begin();
4701 auto method_end = cxx_record_decl->method_end();
4702 if (idx <
4703 static_cast<size_t>(std::distance(method_iter, method_end))) {
4704 std::advance(method_iter, idx);
4705 clang::CXXMethodDecl *cxx_method_decl =
4706 method_iter->getCanonicalDecl();
4707 if (cxx_method_decl) {
4708 name = cxx_method_decl->getDeclName().getAsString();
4709 if (cxx_method_decl->isStatic())
4710 kind = lldb::eMemberFunctionKindStaticMethod;
4711 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4712 kind = lldb::eMemberFunctionKindConstructor;
4713 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4714 kind = lldb::eMemberFunctionKindDestructor;
4715 else
4716 kind = lldb::eMemberFunctionKindInstanceMethod;
4717 clang_type = CompilerType(
4718 this, cxx_method_decl->getType().getAsOpaquePtr());
4719 clang_decl = CompilerDecl(this, cxx_method_decl);
4720 }
4721 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004722 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004723 }
4724 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004725
Sean Callananf9c622a2016-09-30 18:44:43 +00004726 case clang::Type::ObjCObjectPointer: {
4727 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004728 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004729 const clang::ObjCInterfaceType *objc_interface_type =
4730 objc_class_type->getInterfaceType();
4731 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004732 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4733 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004734 clang::ObjCInterfaceDecl *class_interface_decl =
4735 objc_interface_type->getDecl();
4736 if (class_interface_decl) {
4737 auto method_iter = class_interface_decl->meth_begin();
4738 auto method_end = class_interface_decl->meth_end();
4739 if (idx <
4740 static_cast<size_t>(std::distance(method_iter, method_end))) {
4741 std::advance(method_iter, idx);
4742 clang::ObjCMethodDecl *objc_method_decl =
4743 method_iter->getCanonicalDecl();
4744 if (objc_method_decl) {
4745 clang_decl = CompilerDecl(this, objc_method_decl);
4746 name = objc_method_decl->getSelector().getAsString();
4747 if (objc_method_decl->isClassMethod())
4748 kind = lldb::eMemberFunctionKindStaticMethod;
4749 else
4750 kind = lldb::eMemberFunctionKindInstanceMethod;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004751 }
4752 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004753 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004754 }
4755 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004756 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004757
Kate Stoneb9c1b512016-09-06 20:57:50 +00004758 case clang::Type::ObjCObject:
4759 case clang::Type::ObjCInterface:
4760 if (GetCompleteType(type)) {
4761 const clang::ObjCObjectType *objc_class_type =
4762 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4763 if (objc_class_type) {
4764 clang::ObjCInterfaceDecl *class_interface_decl =
4765 objc_class_type->getInterface();
4766 if (class_interface_decl) {
4767 auto method_iter = class_interface_decl->meth_begin();
4768 auto method_end = class_interface_decl->meth_end();
4769 if (idx <
4770 static_cast<size_t>(std::distance(method_iter, method_end))) {
4771 std::advance(method_iter, idx);
4772 clang::ObjCMethodDecl *objc_method_decl =
4773 method_iter->getCanonicalDecl();
4774 if (objc_method_decl) {
4775 clang_decl = CompilerDecl(this, objc_method_decl);
4776 name = objc_method_decl->getSelector().getAsString();
4777 if (objc_method_decl->isClassMethod())
4778 kind = lldb::eMemberFunctionKindStaticMethod;
4779 else
4780 kind = lldb::eMemberFunctionKindInstanceMethod;
4781 }
4782 }
4783 }
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004784 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004785 }
4786 break;
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004787
Kate Stoneb9c1b512016-09-06 20:57:50 +00004788 case clang::Type::Typedef:
4789 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4790 ->getDecl()
4791 ->getUnderlyingType()
4792 .getAsOpaquePtr(),
4793 idx);
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004794
Kate Stoneb9c1b512016-09-06 20:57:50 +00004795 case clang::Type::Auto:
4796 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4797 ->getDeducedType()
4798 .getAsOpaquePtr(),
4799 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004800
Kate Stoneb9c1b512016-09-06 20:57:50 +00004801 case clang::Type::Elaborated:
4802 return GetMemberFunctionAtIndex(
4803 llvm::cast<clang::ElaboratedType>(qual_type)
4804 ->getNamedType()
4805 .getAsOpaquePtr(),
4806 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004807
Kate Stoneb9c1b512016-09-06 20:57:50 +00004808 case clang::Type::Paren:
4809 return GetMemberFunctionAtIndex(
4810 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4811 idx);
4812
4813 default:
4814 break;
Greg Clayton56939cb2015-09-17 22:23:34 +00004815 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004816 }
Greg Clayton56939cb2015-09-17 22:23:34 +00004817
Kate Stoneb9c1b512016-09-06 20:57:50 +00004818 if (kind == eMemberFunctionKindUnknown)
4819 return TypeMemberFunctionImpl();
4820 else
4821 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
Greg Clayton56939cb2015-09-17 22:23:34 +00004822}
4823
Greg Claytona1e5dc82015-08-11 22:53:00 +00004824CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004825ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4826 if (type)
4827 return CompilerType(getASTContext(),
4828 GetQualType(type).getNonReferenceType());
4829 return CompilerType();
4830}
4831
4832CompilerType ClangASTContext::CreateTypedefType(
4833 const CompilerType &type, const char *typedef_name,
4834 const CompilerDeclContext &compiler_decl_ctx) {
4835 if (type && typedef_name && typedef_name[0]) {
4836 ClangASTContext *ast =
4837 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4838 if (!ast)
4839 return CompilerType();
4840 clang::ASTContext *clang_ast = ast->getASTContext();
4841 clang::QualType qual_type(ClangUtil::GetQualType(type));
4842
4843 clang::DeclContext *decl_ctx =
4844 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4845 if (decl_ctx == nullptr)
4846 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4847
4848 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4849 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4850 &clang_ast->Idents.get(typedef_name),
4851 clang_ast->getTrivialTypeSourceInfo(qual_type));
4852
4853 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4854
Aleksandr Urakov709426b2018-09-10 08:08:43 +00004855 decl_ctx->addDecl(decl);
4856
Kate Stoneb9c1b512016-09-06 20:57:50 +00004857 // Get a uniqued clang::QualType for the typedef decl type
4858 return CompilerType(clang_ast, clang_ast->getTypedefType(decl));
4859 }
4860 return CompilerType();
4861}
4862
4863CompilerType
4864ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4865 if (type) {
4866 clang::QualType qual_type(GetQualType(type));
4867 return CompilerType(getASTContext(),
4868 qual_type.getTypePtr()->getPointeeType());
4869 }
4870 return CompilerType();
4871}
4872
4873CompilerType
4874ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4875 if (type) {
4876 clang::QualType qual_type(GetQualType(type));
4877
4878 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4879 switch (type_class) {
4880 case clang::Type::ObjCObject:
4881 case clang::Type::ObjCInterface:
4882 return CompilerType(getASTContext(),
4883 getASTContext()->getObjCObjectPointerType(qual_type));
4884
4885 default:
4886 return CompilerType(getASTContext(),
4887 getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004888 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004889 }
4890 return CompilerType();
4891}
4892
4893CompilerType
4894ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4895 if (type)
4896 return CompilerType(this, getASTContext()
4897 ->getLValueReferenceType(GetQualType(type))
4898 .getAsOpaquePtr());
4899 else
Greg Claytona1e5dc82015-08-11 22:53:00 +00004900 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004901}
4902
Kate Stoneb9c1b512016-09-06 20:57:50 +00004903CompilerType
4904ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4905 if (type)
4906 return CompilerType(this, getASTContext()
4907 ->getRValueReferenceType(GetQualType(type))
4908 .getAsOpaquePtr());
4909 else
4910 return CompilerType();
4911}
4912
4913CompilerType
4914ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4915 if (type) {
4916 clang::QualType result(GetQualType(type));
4917 result.addConst();
4918 return CompilerType(this, result.getAsOpaquePtr());
4919 }
4920 return CompilerType();
4921}
4922
4923CompilerType
4924ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4925 if (type) {
4926 clang::QualType result(GetQualType(type));
4927 result.addVolatile();
4928 return CompilerType(this, result.getAsOpaquePtr());
4929 }
4930 return CompilerType();
4931}
4932
4933CompilerType
4934ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4935 if (type) {
4936 clang::QualType result(GetQualType(type));
4937 result.addRestrict();
4938 return CompilerType(this, result.getAsOpaquePtr());
4939 }
4940 return CompilerType();
4941}
4942
4943CompilerType
4944ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4945 const char *typedef_name,
4946 const CompilerDeclContext &compiler_decl_ctx) {
4947 if (type) {
4948 clang::ASTContext *clang_ast = getASTContext();
4949 clang::QualType qual_type(GetQualType(type));
4950
4951 clang::DeclContext *decl_ctx =
4952 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4953 if (decl_ctx == nullptr)
4954 decl_ctx = getASTContext()->getTranslationUnitDecl();
4955
4956 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4957 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4958 &clang_ast->Idents.get(typedef_name),
4959 clang_ast->getTrivialTypeSourceInfo(qual_type));
4960
4961 clang::TagDecl *tdecl = nullptr;
4962 if (!qual_type.isNull()) {
4963 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4964 tdecl = rt->getDecl();
4965 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4966 tdecl = et->getDecl();
4967 }
4968
4969 // Check whether this declaration is an anonymous struct, union, or enum,
Adrian Prantl05097242018-04-30 16:49:04 +00004970 // hidden behind a typedef. If so, we try to check whether we have a
4971 // typedef tag to attach to the original record declaration
Kate Stoneb9c1b512016-09-06 20:57:50 +00004972 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4973 tdecl->setTypedefNameForAnonDecl(decl);
4974
4975 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4976
4977 // Get a uniqued clang::QualType for the typedef decl type
4978 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4979 }
4980 return CompilerType();
4981}
4982
4983CompilerType
4984ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4985 if (type) {
4986 const clang::TypedefType *typedef_type =
4987 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4988 if (typedef_type)
4989 return CompilerType(getASTContext(),
4990 typedef_type->getDecl()->getUnderlyingType());
4991 }
4992 return CompilerType();
4993}
Greg Claytond8d4a572015-08-11 21:38:15 +00004994
Greg Claytond8d4a572015-08-11 21:38:15 +00004995// Create related types using the current type's AST
Greg Claytond8d4a572015-08-11 21:38:15 +00004996
Kate Stoneb9c1b512016-09-06 20:57:50 +00004997CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
4998 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004999}
Greg Claytond8d4a572015-08-11 21:38:15 +00005000// Exploring the type
Greg Claytond8d4a572015-08-11 21:38:15 +00005001
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005002Optional<uint64_t>
5003ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
5004 ExecutionContextScope *exe_scope) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005005 if (GetCompleteType(type)) {
Greg Claytond8d4a572015-08-11 21:38:15 +00005006 clang::QualType qual_type(GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00005007 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005008 switch (type_class) {
5009 case clang::Type::Record:
5010 if (GetCompleteType(type))
5011 return getASTContext()->getTypeSize(qual_type);
5012 else
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005013 return None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005014 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00005015
Kate Stoneb9c1b512016-09-06 20:57:50 +00005016 case clang::Type::ObjCInterface:
5017 case clang::Type::ObjCObject: {
5018 ExecutionContext exe_ctx(exe_scope);
5019 Process *process = exe_ctx.GetProcessPtr();
5020 if (process) {
5021 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
5022 if (objc_runtime) {
5023 uint64_t bit_size = 0;
5024 if (objc_runtime->GetTypeBitSize(
5025 CompilerType(getASTContext(), qual_type), bit_size))
5026 return bit_size;
5027 }
5028 } else {
5029 static bool g_printed = false;
5030 if (!g_printed) {
5031 StreamString s;
5032 DumpTypeDescription(type, &s);
5033
5034 llvm::outs() << "warning: trying to determine the size of type ";
5035 llvm::outs() << s.GetString() << "\n";
5036 llvm::outs() << "without a valid ExecutionContext. this is not "
5037 "reliable. please file a bug against LLDB.\n";
5038 llvm::outs() << "backtrace:\n";
5039 llvm::sys::PrintStackTrace(llvm::outs());
5040 llvm::outs() << "\n";
5041 g_printed = true;
5042 }
5043 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005044 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005045 LLVM_FALLTHROUGH;
5046 default:
5047 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
5048 if (bit_size == 0) {
5049 if (qual_type->isIncompleteArrayType())
5050 return getASTContext()->getTypeSize(
5051 qual_type->getArrayElementTypeNoTypeQual()
5052 ->getCanonicalTypeUnqualified());
5053 }
5054 if (qual_type->isObjCObjectOrInterfaceType())
5055 return bit_size +
5056 getASTContext()->getTypeSize(
5057 getASTContext()->ObjCBuiltinClassTy);
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005058 // Function types actually have a size of 0, that's not an error.
5059 if (qual_type->isFunctionProtoType())
5060 return bit_size;
5061 if (bit_size)
5062 return bit_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005063 }
5064 }
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005065 return None;
Greg Claytond8d4a572015-08-11 21:38:15 +00005066}
5067
Kate Stoneb9c1b512016-09-06 20:57:50 +00005068size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
5069 if (GetCompleteType(type))
5070 return getASTContext()->getTypeAlign(GetQualType(type));
5071 return 0;
5072}
5073
5074lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
5075 uint64_t &count) {
5076 if (!type)
5077 return lldb::eEncodingInvalid;
5078
5079 count = 1;
5080 clang::QualType qual_type(GetCanonicalQualType(type));
5081
5082 switch (qual_type->getTypeClass()) {
5083 case clang::Type::UnaryTransform:
5084 break;
5085
5086 case clang::Type::FunctionNoProto:
5087 case clang::Type::FunctionProto:
5088 break;
5089
5090 case clang::Type::IncompleteArray:
5091 case clang::Type::VariableArray:
5092 break;
5093
5094 case clang::Type::ConstantArray:
5095 break;
5096
Fangrui Song8f284882018-07-13 22:40:40 +00005097 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005098 case clang::Type::ExtVector:
5099 case clang::Type::Vector:
5100 // TODO: Set this to more than one???
5101 break;
5102
5103 case clang::Type::Builtin:
5104 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5105 case clang::BuiltinType::Void:
5106 break;
5107
5108 case clang::BuiltinType::Bool:
5109 case clang::BuiltinType::Char_S:
5110 case clang::BuiltinType::SChar:
5111 case clang::BuiltinType::WChar_S:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005112 case clang::BuiltinType::Short:
5113 case clang::BuiltinType::Int:
5114 case clang::BuiltinType::Long:
5115 case clang::BuiltinType::LongLong:
5116 case clang::BuiltinType::Int128:
5117 return lldb::eEncodingSint;
5118
5119 case clang::BuiltinType::Char_U:
5120 case clang::BuiltinType::UChar:
5121 case clang::BuiltinType::WChar_U:
Richard Smith51d12d82018-05-02 02:43:22 +00005122 case clang::BuiltinType::Char8:
5123 case clang::BuiltinType::Char16:
5124 case clang::BuiltinType::Char32:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005125 case clang::BuiltinType::UShort:
5126 case clang::BuiltinType::UInt:
5127 case clang::BuiltinType::ULong:
5128 case clang::BuiltinType::ULongLong:
5129 case clang::BuiltinType::UInt128:
5130 return lldb::eEncodingUint;
5131
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005132 // Fixed point types. Note that they are currently ignored.
5133 case clang::BuiltinType::ShortAccum:
5134 case clang::BuiltinType::Accum:
5135 case clang::BuiltinType::LongAccum:
5136 case clang::BuiltinType::UShortAccum:
5137 case clang::BuiltinType::UAccum:
5138 case clang::BuiltinType::ULongAccum:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005139 case clang::BuiltinType::ShortFract:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005140 case clang::BuiltinType::Fract:
5141 case clang::BuiltinType::LongFract:
5142 case clang::BuiltinType::UShortFract:
5143 case clang::BuiltinType::UFract:
5144 case clang::BuiltinType::ULongFract:
5145 case clang::BuiltinType::SatShortAccum:
5146 case clang::BuiltinType::SatAccum:
5147 case clang::BuiltinType::SatLongAccum:
5148 case clang::BuiltinType::SatUShortAccum:
5149 case clang::BuiltinType::SatUAccum:
5150 case clang::BuiltinType::SatULongAccum:
5151 case clang::BuiltinType::SatShortFract:
5152 case clang::BuiltinType::SatFract:
5153 case clang::BuiltinType::SatLongFract:
5154 case clang::BuiltinType::SatUShortFract:
5155 case clang::BuiltinType::SatUFract:
5156 case clang::BuiltinType::SatULongFract:
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005157 break;
5158
Kate Stoneb9c1b512016-09-06 20:57:50 +00005159 case clang::BuiltinType::Half:
5160 case clang::BuiltinType::Float:
Ted Woodward4355c7c2017-09-20 19:16:53 +00005161 case clang::BuiltinType::Float16:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005162 case clang::BuiltinType::Float128:
5163 case clang::BuiltinType::Double:
5164 case clang::BuiltinType::LongDouble:
5165 return lldb::eEncodingIEEE754;
5166
5167 case clang::BuiltinType::ObjCClass:
5168 case clang::BuiltinType::ObjCId:
5169 case clang::BuiltinType::ObjCSel:
5170 return lldb::eEncodingUint;
5171
5172 case clang::BuiltinType::NullPtr:
5173 return lldb::eEncodingUint;
5174
5175 case clang::BuiltinType::Kind::ARCUnbridgedCast:
5176 case clang::BuiltinType::Kind::BoundMember:
5177 case clang::BuiltinType::Kind::BuiltinFn:
5178 case clang::BuiltinType::Kind::Dependent:
5179 case clang::BuiltinType::Kind::OCLClkEvent:
5180 case clang::BuiltinType::Kind::OCLEvent:
5181 case clang::BuiltinType::Kind::OCLImage1dRO:
5182 case clang::BuiltinType::Kind::OCLImage1dWO:
5183 case clang::BuiltinType::Kind::OCLImage1dRW:
5184 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
5185 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
5186 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
5187 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
5188 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
5189 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
5190 case clang::BuiltinType::Kind::OCLImage2dRO:
5191 case clang::BuiltinType::Kind::OCLImage2dWO:
5192 case clang::BuiltinType::Kind::OCLImage2dRW:
5193 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
5194 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
5195 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
5196 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
5197 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
5198 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
5199 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5200 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5201 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5202 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5203 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5204 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5205 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5206 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5207 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5208 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5209 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5210 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5211 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5212 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5213 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5214 case clang::BuiltinType::Kind::OCLImage3dRO:
5215 case clang::BuiltinType::Kind::OCLImage3dWO:
5216 case clang::BuiltinType::Kind::OCLImage3dRW:
5217 case clang::BuiltinType::Kind::OCLQueue:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005218 case clang::BuiltinType::Kind::OCLReserveID:
5219 case clang::BuiltinType::Kind::OCLSampler:
5220 case clang::BuiltinType::Kind::OMPArraySection:
5221 case clang::BuiltinType::Kind::Overload:
5222 case clang::BuiltinType::Kind::PseudoObject:
5223 case clang::BuiltinType::Kind::UnknownAny:
5224 break;
Jorge Gorbe Moyaa6e6c182018-11-08 22:04:58 +00005225
5226 case clang::BuiltinType::OCLIntelSubgroupAVCMcePayload:
5227 case clang::BuiltinType::OCLIntelSubgroupAVCImePayload:
5228 case clang::BuiltinType::OCLIntelSubgroupAVCRefPayload:
5229 case clang::BuiltinType::OCLIntelSubgroupAVCSicPayload:
5230 case clang::BuiltinType::OCLIntelSubgroupAVCMceResult:
5231 case clang::BuiltinType::OCLIntelSubgroupAVCImeResult:
5232 case clang::BuiltinType::OCLIntelSubgroupAVCRefResult:
5233 case clang::BuiltinType::OCLIntelSubgroupAVCSicResult:
5234 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultSingleRefStreamout:
5235 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultDualRefStreamout:
5236 case clang::BuiltinType::OCLIntelSubgroupAVCImeSingleRefStreamin:
5237 case clang::BuiltinType::OCLIntelSubgroupAVCImeDualRefStreamin:
5238 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005239 }
5240 break;
Adrian Prantl05097242018-04-30 16:49:04 +00005241 // All pointer types are represented as unsigned integer encodings. We may
5242 // nee to add a eEncodingPointer if we ever need to know the difference
Kate Stoneb9c1b512016-09-06 20:57:50 +00005243 case clang::Type::ObjCObjectPointer:
5244 case clang::Type::BlockPointer:
5245 case clang::Type::Pointer:
5246 case clang::Type::LValueReference:
5247 case clang::Type::RValueReference:
5248 case clang::Type::MemberPointer:
5249 return lldb::eEncodingUint;
5250 case clang::Type::Complex: {
5251 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5252 if (qual_type->isComplexType())
5253 encoding = lldb::eEncodingIEEE754;
5254 else {
5255 const clang::ComplexType *complex_type =
5256 qual_type->getAsComplexIntegerType();
5257 if (complex_type)
5258 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5259 .GetEncoding(count);
5260 else
5261 encoding = lldb::eEncodingSint;
5262 }
5263 count = 2;
5264 return encoding;
5265 }
5266
5267 case clang::Type::ObjCInterface:
5268 break;
5269 case clang::Type::Record:
5270 break;
5271 case clang::Type::Enum:
5272 return lldb::eEncodingSint;
5273 case clang::Type::Typedef:
5274 return CompilerType(getASTContext(),
5275 llvm::cast<clang::TypedefType>(qual_type)
5276 ->getDecl()
5277 ->getUnderlyingType())
5278 .GetEncoding(count);
5279
5280 case clang::Type::Auto:
5281 return CompilerType(
5282 getASTContext(),
5283 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5284 .GetEncoding(count);
5285
5286 case clang::Type::Elaborated:
5287 return CompilerType(
5288 getASTContext(),
5289 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5290 .GetEncoding(count);
5291
5292 case clang::Type::Paren:
5293 return CompilerType(getASTContext(),
5294 llvm::cast<clang::ParenType>(qual_type)->desugar())
5295 .GetEncoding(count);
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005296 case clang::Type::TypeOfExpr:
5297 return CompilerType(getASTContext(),
5298 llvm::cast<clang::TypeOfExprType>(qual_type)
5299 ->getUnderlyingExpr()
5300 ->getType())
5301 .GetEncoding(count);
5302 case clang::Type::TypeOf:
5303 return CompilerType(
5304 getASTContext(),
5305 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5306 .GetEncoding(count);
5307 case clang::Type::Decltype:
5308 return CompilerType(
5309 getASTContext(),
5310 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5311 .GetEncoding(count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005312 case clang::Type::DependentSizedArray:
5313 case clang::Type::DependentSizedExtVector:
5314 case clang::Type::UnresolvedUsing:
5315 case clang::Type::Attributed:
5316 case clang::Type::TemplateTypeParm:
5317 case clang::Type::SubstTemplateTypeParm:
5318 case clang::Type::SubstTemplateTypeParmPack:
5319 case clang::Type::InjectedClassName:
5320 case clang::Type::DependentName:
5321 case clang::Type::DependentTemplateSpecialization:
5322 case clang::Type::PackExpansion:
5323 case clang::Type::ObjCObject:
5324
Kate Stoneb9c1b512016-09-06 20:57:50 +00005325 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005326 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005327 case clang::Type::Atomic:
5328 case clang::Type::Adjusted:
5329 case clang::Type::Pipe:
5330 break;
5331
5332 // pointer type decayed from an array or function type.
5333 case clang::Type::Decayed:
5334 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005335 case clang::Type::ObjCTypeParam:
5336 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005337
5338 case clang::Type::DependentAddressSpace:
5339 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005340 }
5341 count = 0;
5342 return lldb::eEncodingInvalid;
5343}
5344
5345lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5346 if (!type)
5347 return lldb::eFormatDefault;
5348
5349 clang::QualType qual_type(GetCanonicalQualType(type));
5350
5351 switch (qual_type->getTypeClass()) {
5352 case clang::Type::UnaryTransform:
5353 break;
5354
5355 case clang::Type::FunctionNoProto:
5356 case clang::Type::FunctionProto:
5357 break;
5358
5359 case clang::Type::IncompleteArray:
5360 case clang::Type::VariableArray:
5361 break;
5362
5363 case clang::Type::ConstantArray:
5364 return lldb::eFormatVoid; // no value
5365
Fangrui Song8f284882018-07-13 22:40:40 +00005366 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005367 case clang::Type::ExtVector:
5368 case clang::Type::Vector:
5369 break;
5370
5371 case clang::Type::Builtin:
5372 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5373 // default: assert(0 && "Unknown builtin type!");
5374 case clang::BuiltinType::UnknownAny:
5375 case clang::BuiltinType::Void:
5376 case clang::BuiltinType::BoundMember:
5377 break;
5378
5379 case clang::BuiltinType::Bool:
5380 return lldb::eFormatBoolean;
5381 case clang::BuiltinType::Char_S:
5382 case clang::BuiltinType::SChar:
5383 case clang::BuiltinType::WChar_S:
5384 case clang::BuiltinType::Char_U:
5385 case clang::BuiltinType::UChar:
5386 case clang::BuiltinType::WChar_U:
5387 return lldb::eFormatChar;
5388 case clang::BuiltinType::Char16:
5389 return lldb::eFormatUnicode16;
5390 case clang::BuiltinType::Char32:
5391 return lldb::eFormatUnicode32;
5392 case clang::BuiltinType::UShort:
5393 return lldb::eFormatUnsigned;
5394 case clang::BuiltinType::Short:
5395 return lldb::eFormatDecimal;
5396 case clang::BuiltinType::UInt:
5397 return lldb::eFormatUnsigned;
5398 case clang::BuiltinType::Int:
5399 return lldb::eFormatDecimal;
5400 case clang::BuiltinType::ULong:
5401 return lldb::eFormatUnsigned;
5402 case clang::BuiltinType::Long:
5403 return lldb::eFormatDecimal;
5404 case clang::BuiltinType::ULongLong:
5405 return lldb::eFormatUnsigned;
5406 case clang::BuiltinType::LongLong:
5407 return lldb::eFormatDecimal;
5408 case clang::BuiltinType::UInt128:
5409 return lldb::eFormatUnsigned;
5410 case clang::BuiltinType::Int128:
5411 return lldb::eFormatDecimal;
5412 case clang::BuiltinType::Half:
5413 case clang::BuiltinType::Float:
5414 case clang::BuiltinType::Double:
5415 case clang::BuiltinType::LongDouble:
5416 return lldb::eFormatFloat;
5417 default:
5418 return lldb::eFormatHex;
5419 }
5420 break;
5421 case clang::Type::ObjCObjectPointer:
5422 return lldb::eFormatHex;
5423 case clang::Type::BlockPointer:
5424 return lldb::eFormatHex;
5425 case clang::Type::Pointer:
5426 return lldb::eFormatHex;
5427 case clang::Type::LValueReference:
5428 case clang::Type::RValueReference:
5429 return lldb::eFormatHex;
5430 case clang::Type::MemberPointer:
5431 break;
5432 case clang::Type::Complex: {
5433 if (qual_type->isComplexType())
5434 return lldb::eFormatComplex;
5435 else
5436 return lldb::eFormatComplexInteger;
5437 }
5438 case clang::Type::ObjCInterface:
5439 break;
5440 case clang::Type::Record:
5441 break;
5442 case clang::Type::Enum:
5443 return lldb::eFormatEnum;
5444 case clang::Type::Typedef:
5445 return CompilerType(getASTContext(),
5446 llvm::cast<clang::TypedefType>(qual_type)
5447 ->getDecl()
5448 ->getUnderlyingType())
5449 .GetFormat();
5450 case clang::Type::Auto:
5451 return CompilerType(getASTContext(),
5452 llvm::cast<clang::AutoType>(qual_type)->desugar())
5453 .GetFormat();
5454 case clang::Type::Paren:
5455 return CompilerType(getASTContext(),
5456 llvm::cast<clang::ParenType>(qual_type)->desugar())
5457 .GetFormat();
5458 case clang::Type::Elaborated:
5459 return CompilerType(
5460 getASTContext(),
5461 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5462 .GetFormat();
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005463 case clang::Type::TypeOfExpr:
5464 return CompilerType(getASTContext(),
5465 llvm::cast<clang::TypeOfExprType>(qual_type)
5466 ->getUnderlyingExpr()
5467 ->getType())
5468 .GetFormat();
5469 case clang::Type::TypeOf:
5470 return CompilerType(
5471 getASTContext(),
5472 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5473 .GetFormat();
5474 case clang::Type::Decltype:
5475 return CompilerType(
5476 getASTContext(),
5477 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5478 .GetFormat();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005479 case clang::Type::DependentSizedArray:
5480 case clang::Type::DependentSizedExtVector:
5481 case clang::Type::UnresolvedUsing:
5482 case clang::Type::Attributed:
5483 case clang::Type::TemplateTypeParm:
5484 case clang::Type::SubstTemplateTypeParm:
5485 case clang::Type::SubstTemplateTypeParmPack:
5486 case clang::Type::InjectedClassName:
5487 case clang::Type::DependentName:
5488 case clang::Type::DependentTemplateSpecialization:
5489 case clang::Type::PackExpansion:
5490 case clang::Type::ObjCObject:
5491
Kate Stoneb9c1b512016-09-06 20:57:50 +00005492 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005493 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005494 case clang::Type::Atomic:
5495 case clang::Type::Adjusted:
5496 case clang::Type::Pipe:
5497 break;
5498
5499 // pointer type decayed from an array or function type.
5500 case clang::Type::Decayed:
5501 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005502 case clang::Type::ObjCTypeParam:
5503 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005504
5505 case clang::Type::DependentAddressSpace:
5506 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005507 }
5508 // We don't know hot to display this type...
5509 return lldb::eFormatBytes;
5510}
5511
5512static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5513 bool check_superclass) {
5514 while (class_interface_decl) {
5515 if (class_interface_decl->ivar_size() > 0)
5516 return true;
5517
5518 if (check_superclass)
5519 class_interface_decl = class_interface_decl->getSuperClass();
5520 else
5521 break;
5522 }
5523 return false;
5524}
5525
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00005526static Optional<SymbolFile::ArrayInfo>
Adrian Prantleca07c52018-11-05 20:49:07 +00005527GetDynamicArrayInfo(ClangASTContext &ast, SymbolFile *sym_file,
5528 clang::QualType qual_type,
5529 const ExecutionContext *exe_ctx) {
5530 if (qual_type->isIncompleteArrayType())
5531 if (auto *metadata = ast.GetMetadata(qual_type.getAsOpaquePtr()))
Pavel Labathffec31e2018-12-28 13:34:44 +00005532 return sym_file->GetDynamicArrayInfoForUID(metadata->GetUserID(),
5533 exe_ctx);
Adrian Prantleca07c52018-11-05 20:49:07 +00005534 return llvm::None;
5535}
5536
Kate Stoneb9c1b512016-09-06 20:57:50 +00005537uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
Adrian Prantleca07c52018-11-05 20:49:07 +00005538 bool omit_empty_base_classes,
5539 const ExecutionContext *exe_ctx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005540 if (!type)
5541 return 0;
5542
5543 uint32_t num_children = 0;
5544 clang::QualType qual_type(GetQualType(type));
5545 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5546 switch (type_class) {
5547 case clang::Type::Builtin:
5548 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5549 case clang::BuiltinType::ObjCId: // child is Class
5550 case clang::BuiltinType::ObjCClass: // child is Class
5551 num_children = 1;
5552 break;
5553
5554 default:
5555 break;
5556 }
5557 break;
5558
5559 case clang::Type::Complex:
5560 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005561 case clang::Type::Record:
5562 if (GetCompleteQualType(getASTContext(), qual_type)) {
5563 const clang::RecordType *record_type =
5564 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5565 const clang::RecordDecl *record_decl = record_type->getDecl();
5566 assert(record_decl);
5567 const clang::CXXRecordDecl *cxx_record_decl =
5568 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5569 if (cxx_record_decl) {
5570 if (omit_empty_base_classes) {
Adrian Prantl05097242018-04-30 16:49:04 +00005571 // Check each base classes to see if it or any of its base classes
5572 // contain any fields. This can help limit the noise in variable
5573 // views by not having to show base classes that contain no members.
Kate Stoneb9c1b512016-09-06 20:57:50 +00005574 clang::CXXRecordDecl::base_class_const_iterator base_class,
5575 base_class_end;
5576 for (base_class = cxx_record_decl->bases_begin(),
5577 base_class_end = cxx_record_decl->bases_end();
5578 base_class != base_class_end; ++base_class) {
5579 const clang::CXXRecordDecl *base_class_decl =
5580 llvm::cast<clang::CXXRecordDecl>(
5581 base_class->getType()
5582 ->getAs<clang::RecordType>()
5583 ->getDecl());
5584
5585 // Skip empty base classes
Jonas Devliegherea6682a42018-12-15 00:15:33 +00005586 if (!ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00005587 continue;
5588
5589 num_children++;
5590 }
5591 } else {
5592 // Include all base classes
5593 num_children += cxx_record_decl->getNumBases();
5594 }
5595 }
5596 clang::RecordDecl::field_iterator field, field_end;
5597 for (field = record_decl->field_begin(),
5598 field_end = record_decl->field_end();
5599 field != field_end; ++field)
5600 ++num_children;
5601 }
5602 break;
5603
5604 case clang::Type::ObjCObject:
5605 case clang::Type::ObjCInterface:
5606 if (GetCompleteQualType(getASTContext(), qual_type)) {
5607 const clang::ObjCObjectType *objc_class_type =
5608 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5609 assert(objc_class_type);
5610 if (objc_class_type) {
5611 clang::ObjCInterfaceDecl *class_interface_decl =
5612 objc_class_type->getInterface();
5613
5614 if (class_interface_decl) {
5615
5616 clang::ObjCInterfaceDecl *superclass_interface_decl =
5617 class_interface_decl->getSuperClass();
5618 if (superclass_interface_decl) {
5619 if (omit_empty_base_classes) {
5620 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5621 ++num_children;
5622 } else
5623 ++num_children;
5624 }
5625
5626 num_children += class_interface_decl->ivar_size();
5627 }
5628 }
5629 }
5630 break;
5631
5632 case clang::Type::ObjCObjectPointer: {
5633 const clang::ObjCObjectPointerType *pointer_type =
5634 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5635 clang::QualType pointee_type = pointer_type->getPointeeType();
5636 uint32_t num_pointee_children =
5637 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005638 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005639 // If this type points to a simple type, then it has 1 child
5640 if (num_pointee_children == 0)
5641 num_children = 1;
5642 else
5643 num_children = num_pointee_children;
5644 } break;
5645
5646 case clang::Type::Vector:
5647 case clang::Type::ExtVector:
5648 num_children =
5649 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5650 break;
5651
5652 case clang::Type::ConstantArray:
5653 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5654 ->getSize()
5655 .getLimitedValue();
5656 break;
Adrian Prantleca07c52018-11-05 20:49:07 +00005657 case clang::Type::IncompleteArray:
5658 if (auto array_info =
5659 GetDynamicArrayInfo(*this, GetSymbolFile(), qual_type, exe_ctx))
5660 // Only 1-dimensional arrays are supported.
5661 num_children = array_info->element_orders.size()
5662 ? array_info->element_orders.back()
5663 : 0;
5664 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005665
5666 case clang::Type::Pointer: {
5667 const clang::PointerType *pointer_type =
5668 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5669 clang::QualType pointee_type(pointer_type->getPointeeType());
5670 uint32_t num_pointee_children =
5671 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005672 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005673 if (num_pointee_children == 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00005674 // We have a pointer to a pointee type that claims it has no children. We
5675 // will want to look at
Kate Stoneb9c1b512016-09-06 20:57:50 +00005676 num_children = GetNumPointeeChildren(pointee_type);
5677 } else
5678 num_children = num_pointee_children;
5679 } break;
5680
5681 case clang::Type::LValueReference:
5682 case clang::Type::RValueReference: {
5683 const clang::ReferenceType *reference_type =
5684 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5685 clang::QualType pointee_type = reference_type->getPointeeType();
5686 uint32_t num_pointee_children =
5687 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005688 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005689 // If this type points to a simple type, then it has 1 child
5690 if (num_pointee_children == 0)
5691 num_children = 1;
5692 else
5693 num_children = num_pointee_children;
5694 } break;
5695
5696 case clang::Type::Typedef:
5697 num_children =
5698 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5699 ->getDecl()
5700 ->getUnderlyingType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005701 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005702 break;
5703
5704 case clang::Type::Auto:
5705 num_children =
5706 CompilerType(getASTContext(),
5707 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005708 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005709 break;
5710
5711 case clang::Type::Elaborated:
5712 num_children =
5713 CompilerType(
5714 getASTContext(),
5715 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005716 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005717 break;
5718
5719 case clang::Type::Paren:
5720 num_children =
5721 CompilerType(getASTContext(),
5722 llvm::cast<clang::ParenType>(qual_type)->desugar())
Adrian Prantleca07c52018-11-05 20:49:07 +00005723 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005724 break;
5725 default:
5726 break;
5727 }
5728 return num_children;
5729}
5730
Adrian Prantl0e4c4822019-03-06 21:22:25 +00005731CompilerType ClangASTContext::GetBuiltinTypeByName(ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005732 return GetBasicType(GetBasicTypeEnumeration(name));
Greg Clayton56939cb2015-09-17 22:23:34 +00005733}
5734
Greg Claytond8d4a572015-08-11 21:38:15 +00005735lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00005736ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5737 if (type) {
5738 clang::QualType qual_type(GetQualType(type));
5739 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5740 if (type_class == clang::Type::Builtin) {
5741 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5742 case clang::BuiltinType::Void:
5743 return eBasicTypeVoid;
5744 case clang::BuiltinType::Bool:
5745 return eBasicTypeBool;
5746 case clang::BuiltinType::Char_S:
5747 return eBasicTypeSignedChar;
5748 case clang::BuiltinType::Char_U:
5749 return eBasicTypeUnsignedChar;
5750 case clang::BuiltinType::Char16:
5751 return eBasicTypeChar16;
5752 case clang::BuiltinType::Char32:
5753 return eBasicTypeChar32;
5754 case clang::BuiltinType::UChar:
5755 return eBasicTypeUnsignedChar;
5756 case clang::BuiltinType::SChar:
5757 return eBasicTypeSignedChar;
5758 case clang::BuiltinType::WChar_S:
5759 return eBasicTypeSignedWChar;
5760 case clang::BuiltinType::WChar_U:
5761 return eBasicTypeUnsignedWChar;
5762 case clang::BuiltinType::Short:
5763 return eBasicTypeShort;
5764 case clang::BuiltinType::UShort:
5765 return eBasicTypeUnsignedShort;
5766 case clang::BuiltinType::Int:
5767 return eBasicTypeInt;
5768 case clang::BuiltinType::UInt:
5769 return eBasicTypeUnsignedInt;
5770 case clang::BuiltinType::Long:
5771 return eBasicTypeLong;
5772 case clang::BuiltinType::ULong:
5773 return eBasicTypeUnsignedLong;
5774 case clang::BuiltinType::LongLong:
5775 return eBasicTypeLongLong;
5776 case clang::BuiltinType::ULongLong:
5777 return eBasicTypeUnsignedLongLong;
5778 case clang::BuiltinType::Int128:
5779 return eBasicTypeInt128;
5780 case clang::BuiltinType::UInt128:
5781 return eBasicTypeUnsignedInt128;
5782
5783 case clang::BuiltinType::Half:
5784 return eBasicTypeHalf;
5785 case clang::BuiltinType::Float:
5786 return eBasicTypeFloat;
5787 case clang::BuiltinType::Double:
5788 return eBasicTypeDouble;
5789 case clang::BuiltinType::LongDouble:
5790 return eBasicTypeLongDouble;
5791
5792 case clang::BuiltinType::NullPtr:
5793 return eBasicTypeNullPtr;
5794 case clang::BuiltinType::ObjCId:
5795 return eBasicTypeObjCID;
5796 case clang::BuiltinType::ObjCClass:
5797 return eBasicTypeObjCClass;
5798 case clang::BuiltinType::ObjCSel:
5799 return eBasicTypeObjCSel;
5800 default:
5801 return eBasicTypeOther;
5802 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005803 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005804 }
5805 return eBasicTypeInvalid;
Greg Claytond8d4a572015-08-11 21:38:15 +00005806}
5807
Kate Stoneb9c1b512016-09-06 20:57:50 +00005808void ClangASTContext::ForEachEnumerator(
5809 lldb::opaque_compiler_type_t type,
5810 std::function<bool(const CompilerType &integer_type,
Adrian Prantl0e4c4822019-03-06 21:22:25 +00005811 ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00005812 const llvm::APSInt &value)> const &callback) {
5813 const clang::EnumType *enum_type =
5814 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5815 if (enum_type) {
5816 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5817 if (enum_decl) {
5818 CompilerType integer_type(this,
5819 enum_decl->getIntegerType().getAsOpaquePtr());
Greg Clayton99558cc42015-08-24 23:46:31 +00005820
Kate Stoneb9c1b512016-09-06 20:57:50 +00005821 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5822 for (enum_pos = enum_decl->enumerator_begin(),
5823 enum_end_pos = enum_decl->enumerator_end();
5824 enum_pos != enum_end_pos; ++enum_pos) {
5825 ConstString name(enum_pos->getNameAsString().c_str());
5826 if (!callback(integer_type, name, enum_pos->getInitVal()))
5827 break;
5828 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005829 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005830 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005831}
5832
Greg Claytond8d4a572015-08-11 21:38:15 +00005833#pragma mark Aggregate Types
5834
Kate Stoneb9c1b512016-09-06 20:57:50 +00005835uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5836 if (!type)
5837 return 0;
Enrico Granata36f51e42015-12-18 22:41:25 +00005838
Kate Stoneb9c1b512016-09-06 20:57:50 +00005839 uint32_t count = 0;
5840 clang::QualType qual_type(GetCanonicalQualType(type));
5841 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5842 switch (type_class) {
5843 case clang::Type::Record:
5844 if (GetCompleteType(type)) {
5845 const clang::RecordType *record_type =
5846 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5847 if (record_type) {
5848 clang::RecordDecl *record_decl = record_type->getDecl();
5849 if (record_decl) {
5850 uint32_t field_idx = 0;
5851 clang::RecordDecl::field_iterator field, field_end;
5852 for (field = record_decl->field_begin(),
5853 field_end = record_decl->field_end();
5854 field != field_end; ++field)
5855 ++field_idx;
5856 count = field_idx;
5857 }
5858 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005859 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005860 break;
5861
5862 case clang::Type::Typedef:
5863 count =
5864 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5865 ->getDecl()
5866 ->getUnderlyingType())
5867 .GetNumFields();
5868 break;
5869
5870 case clang::Type::Auto:
5871 count =
5872 CompilerType(getASTContext(),
5873 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5874 .GetNumFields();
5875 break;
5876
5877 case clang::Type::Elaborated:
5878 count = CompilerType(
5879 getASTContext(),
5880 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5881 .GetNumFields();
5882 break;
5883
5884 case clang::Type::Paren:
5885 count = CompilerType(getASTContext(),
5886 llvm::cast<clang::ParenType>(qual_type)->desugar())
5887 .GetNumFields();
5888 break;
5889
Sean Callananf9c622a2016-09-30 18:44:43 +00005890 case clang::Type::ObjCObjectPointer: {
5891 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005892 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005893 const clang::ObjCInterfaceType *objc_interface_type =
5894 objc_class_type->getInterfaceType();
5895 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005896 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5897 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005898 clang::ObjCInterfaceDecl *class_interface_decl =
5899 objc_interface_type->getDecl();
5900 if (class_interface_decl) {
5901 count = class_interface_decl->ivar_size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005902 }
5903 }
5904 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005905 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005906
5907 case clang::Type::ObjCObject:
5908 case clang::Type::ObjCInterface:
5909 if (GetCompleteType(type)) {
5910 const clang::ObjCObjectType *objc_class_type =
5911 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5912 if (objc_class_type) {
5913 clang::ObjCInterfaceDecl *class_interface_decl =
5914 objc_class_type->getInterface();
5915
5916 if (class_interface_decl)
5917 count = class_interface_decl->ivar_size();
5918 }
5919 }
5920 break;
5921
5922 default:
5923 break;
5924 }
5925 return count;
Greg Claytond8d4a572015-08-11 21:38:15 +00005926}
5927
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005928static lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005929GetObjCFieldAtIndex(clang::ASTContext *ast,
5930 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5931 std::string &name, uint64_t *bit_offset_ptr,
5932 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5933 if (class_interface_decl) {
5934 if (idx < (class_interface_decl->ivar_size())) {
5935 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5936 ivar_end = class_interface_decl->ivar_end();
5937 uint32_t ivar_idx = 0;
5938
5939 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5940 ++ivar_pos, ++ivar_idx) {
5941 if (ivar_idx == idx) {
5942 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5943
5944 clang::QualType ivar_qual_type(ivar_decl->getType());
5945
5946 name.assign(ivar_decl->getNameAsString());
5947
5948 if (bit_offset_ptr) {
5949 const clang::ASTRecordLayout &interface_layout =
5950 ast->getASTObjCInterfaceLayout(class_interface_decl);
5951 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5952 }
5953
5954 const bool is_bitfield = ivar_pos->isBitField();
5955
5956 if (bitfield_bit_size_ptr) {
5957 *bitfield_bit_size_ptr = 0;
5958
5959 if (is_bitfield && ast) {
5960 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
Hans Wennborg30ce9622018-11-28 14:30:18 +00005961 clang::Expr::EvalResult result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005962 if (bitfield_bit_size_expr &&
Hans Wennborg30ce9622018-11-28 14:30:18 +00005963 bitfield_bit_size_expr->EvaluateAsInt(result, *ast)) {
5964 llvm::APSInt bitfield_apsint = result.Val.getInt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005965 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5966 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005967 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005968 }
5969 if (is_bitfield_ptr)
5970 *is_bitfield_ptr = is_bitfield;
5971
5972 return ivar_qual_type.getAsOpaquePtr();
Greg Claytond8d4a572015-08-11 21:38:15 +00005973 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005974 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005975 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005976 }
5977 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00005978}
5979
Kate Stoneb9c1b512016-09-06 20:57:50 +00005980CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5981 size_t idx, std::string &name,
5982 uint64_t *bit_offset_ptr,
5983 uint32_t *bitfield_bit_size_ptr,
5984 bool *is_bitfield_ptr) {
5985 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005986 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005987
5988 clang::QualType qual_type(GetCanonicalQualType(type));
5989 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5990 switch (type_class) {
5991 case clang::Type::Record:
5992 if (GetCompleteType(type)) {
5993 const clang::RecordType *record_type =
5994 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5995 const clang::RecordDecl *record_decl = record_type->getDecl();
5996 uint32_t field_idx = 0;
5997 clang::RecordDecl::field_iterator field, field_end;
5998 for (field = record_decl->field_begin(),
5999 field_end = record_decl->field_end();
6000 field != field_end; ++field, ++field_idx) {
6001 if (idx == field_idx) {
6002 // Print the member type if requested
6003 // Print the member name and equal sign
6004 name.assign(field->getNameAsString());
6005
6006 // Figure out the type byte size (field_type_info.first) and
6007 // alignment (field_type_info.second) from the AST context.
6008 if (bit_offset_ptr) {
6009 const clang::ASTRecordLayout &record_layout =
6010 getASTContext()->getASTRecordLayout(record_decl);
6011 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
6012 }
6013
6014 const bool is_bitfield = field->isBitField();
6015
6016 if (bitfield_bit_size_ptr) {
6017 *bitfield_bit_size_ptr = 0;
6018
6019 if (is_bitfield) {
6020 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
Hans Wennborg30ce9622018-11-28 14:30:18 +00006021 clang::Expr::EvalResult result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006022 if (bitfield_bit_size_expr &&
Hans Wennborg30ce9622018-11-28 14:30:18 +00006023 bitfield_bit_size_expr->EvaluateAsInt(result,
Kate Stoneb9c1b512016-09-06 20:57:50 +00006024 *getASTContext())) {
Hans Wennborg30ce9622018-11-28 14:30:18 +00006025 llvm::APSInt bitfield_apsint = result.Val.getInt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006026 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
6027 }
6028 }
6029 }
6030 if (is_bitfield_ptr)
6031 *is_bitfield_ptr = is_bitfield;
6032
6033 return CompilerType(getASTContext(), field->getType());
6034 }
6035 }
6036 }
6037 break;
6038
Sean Callananf9c622a2016-09-30 18:44:43 +00006039 case clang::Type::ObjCObjectPointer: {
6040 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00006041 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00006042 const clang::ObjCInterfaceType *objc_interface_type =
6043 objc_class_type->getInterfaceType();
6044 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00006045 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
6046 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00006047 clang::ObjCInterfaceDecl *class_interface_decl =
6048 objc_interface_type->getDecl();
6049 if (class_interface_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006050 return CompilerType(
6051 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
6052 idx, name, bit_offset_ptr,
6053 bitfield_bit_size_ptr, is_bitfield_ptr));
6054 }
6055 }
6056 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00006057 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006058
6059 case clang::Type::ObjCObject:
6060 case clang::Type::ObjCInterface:
6061 if (GetCompleteType(type)) {
6062 const clang::ObjCObjectType *objc_class_type =
6063 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6064 assert(objc_class_type);
6065 if (objc_class_type) {
6066 clang::ObjCInterfaceDecl *class_interface_decl =
6067 objc_class_type->getInterface();
6068 return CompilerType(
6069 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
6070 idx, name, bit_offset_ptr,
6071 bitfield_bit_size_ptr, is_bitfield_ptr));
6072 }
6073 }
6074 break;
6075
6076 case clang::Type::Typedef:
6077 return CompilerType(getASTContext(),
6078 llvm::cast<clang::TypedefType>(qual_type)
6079 ->getDecl()
6080 ->getUnderlyingType())
6081 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6082 is_bitfield_ptr);
6083
6084 case clang::Type::Auto:
6085 return CompilerType(
6086 getASTContext(),
6087 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
6088 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6089 is_bitfield_ptr);
6090
6091 case clang::Type::Elaborated:
6092 return CompilerType(
6093 getASTContext(),
6094 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
6095 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6096 is_bitfield_ptr);
6097
6098 case clang::Type::Paren:
6099 return CompilerType(getASTContext(),
6100 llvm::cast<clang::ParenType>(qual_type)->desugar())
6101 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6102 is_bitfield_ptr);
6103
6104 default:
6105 break;
6106 }
6107 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006108}
6109
Greg Clayton99558cc42015-08-24 23:46:31 +00006110uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006111ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
6112 uint32_t count = 0;
6113 clang::QualType qual_type(GetCanonicalQualType(type));
6114 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6115 switch (type_class) {
6116 case clang::Type::Record:
6117 if (GetCompleteType(type)) {
6118 const clang::CXXRecordDecl *cxx_record_decl =
6119 qual_type->getAsCXXRecordDecl();
6120 if (cxx_record_decl)
6121 count = cxx_record_decl->getNumBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006122 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006123 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006124
Kate Stoneb9c1b512016-09-06 20:57:50 +00006125 case clang::Type::ObjCObjectPointer:
6126 count = GetPointeeType(type).GetNumDirectBaseClasses();
6127 break;
6128
6129 case clang::Type::ObjCObject:
6130 if (GetCompleteType(type)) {
6131 const clang::ObjCObjectType *objc_class_type =
6132 qual_type->getAsObjCQualifiedInterfaceType();
6133 if (objc_class_type) {
6134 clang::ObjCInterfaceDecl *class_interface_decl =
6135 objc_class_type->getInterface();
6136
6137 if (class_interface_decl && class_interface_decl->getSuperClass())
6138 count = 1;
6139 }
6140 }
6141 break;
6142 case clang::Type::ObjCInterface:
6143 if (GetCompleteType(type)) {
6144 const clang::ObjCInterfaceType *objc_interface_type =
6145 qual_type->getAs<clang::ObjCInterfaceType>();
6146 if (objc_interface_type) {
6147 clang::ObjCInterfaceDecl *class_interface_decl =
6148 objc_interface_type->getInterface();
6149
6150 if (class_interface_decl && class_interface_decl->getSuperClass())
6151 count = 1;
6152 }
6153 }
6154 break;
6155
6156 case clang::Type::Typedef:
6157 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6158 ->getDecl()
6159 ->getUnderlyingType()
6160 .getAsOpaquePtr());
6161 break;
6162
6163 case clang::Type::Auto:
6164 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6165 ->getDeducedType()
6166 .getAsOpaquePtr());
6167 break;
6168
6169 case clang::Type::Elaborated:
6170 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6171 ->getNamedType()
6172 .getAsOpaquePtr());
6173 break;
6174
6175 case clang::Type::Paren:
6176 return GetNumDirectBaseClasses(
6177 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6178
6179 default:
6180 break;
6181 }
6182 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006183}
6184
6185uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006186ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
6187 uint32_t count = 0;
6188 clang::QualType qual_type(GetCanonicalQualType(type));
6189 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6190 switch (type_class) {
6191 case clang::Type::Record:
6192 if (GetCompleteType(type)) {
6193 const clang::CXXRecordDecl *cxx_record_decl =
6194 qual_type->getAsCXXRecordDecl();
6195 if (cxx_record_decl)
6196 count = cxx_record_decl->getNumVBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006197 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006198 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006199
Kate Stoneb9c1b512016-09-06 20:57:50 +00006200 case clang::Type::Typedef:
6201 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6202 ->getDecl()
6203 ->getUnderlyingType()
6204 .getAsOpaquePtr());
6205 break;
6206
6207 case clang::Type::Auto:
6208 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6209 ->getDeducedType()
6210 .getAsOpaquePtr());
6211 break;
6212
6213 case clang::Type::Elaborated:
6214 count =
6215 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6216 ->getNamedType()
6217 .getAsOpaquePtr());
6218 break;
6219
6220 case clang::Type::Paren:
6221 count = GetNumVirtualBaseClasses(
6222 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6223 break;
6224
6225 default:
6226 break;
6227 }
6228 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006229}
6230
Kate Stoneb9c1b512016-09-06 20:57:50 +00006231CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
6232 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6233 clang::QualType qual_type(GetCanonicalQualType(type));
6234 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6235 switch (type_class) {
6236 case clang::Type::Record:
6237 if (GetCompleteType(type)) {
6238 const clang::CXXRecordDecl *cxx_record_decl =
6239 qual_type->getAsCXXRecordDecl();
6240 if (cxx_record_decl) {
6241 uint32_t curr_idx = 0;
6242 clang::CXXRecordDecl::base_class_const_iterator base_class,
6243 base_class_end;
6244 for (base_class = cxx_record_decl->bases_begin(),
6245 base_class_end = cxx_record_decl->bases_end();
6246 base_class != base_class_end; ++base_class, ++curr_idx) {
6247 if (curr_idx == idx) {
6248 if (bit_offset_ptr) {
6249 const clang::ASTRecordLayout &record_layout =
6250 getASTContext()->getASTRecordLayout(cxx_record_decl);
6251 const clang::CXXRecordDecl *base_class_decl =
6252 llvm::cast<clang::CXXRecordDecl>(
6253 base_class->getType()
6254 ->getAs<clang::RecordType>()
6255 ->getDecl());
6256 if (base_class->isVirtual())
6257 *bit_offset_ptr =
6258 record_layout.getVBaseClassOffset(base_class_decl)
6259 .getQuantity() *
6260 8;
6261 else
6262 *bit_offset_ptr =
6263 record_layout.getBaseClassOffset(base_class_decl)
6264 .getQuantity() *
6265 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006266 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006267 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6268 }
6269 }
6270 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006271 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006272 break;
6273
6274 case clang::Type::ObjCObjectPointer:
6275 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6276
6277 case clang::Type::ObjCObject:
6278 if (idx == 0 && GetCompleteType(type)) {
6279 const clang::ObjCObjectType *objc_class_type =
6280 qual_type->getAsObjCQualifiedInterfaceType();
6281 if (objc_class_type) {
6282 clang::ObjCInterfaceDecl *class_interface_decl =
6283 objc_class_type->getInterface();
6284
6285 if (class_interface_decl) {
6286 clang::ObjCInterfaceDecl *superclass_interface_decl =
6287 class_interface_decl->getSuperClass();
6288 if (superclass_interface_decl) {
6289 if (bit_offset_ptr)
6290 *bit_offset_ptr = 0;
6291 return CompilerType(getASTContext(),
6292 getASTContext()->getObjCInterfaceType(
6293 superclass_interface_decl));
6294 }
6295 }
6296 }
6297 }
6298 break;
6299 case clang::Type::ObjCInterface:
6300 if (idx == 0 && GetCompleteType(type)) {
6301 const clang::ObjCObjectType *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) {
6308 clang::ObjCInterfaceDecl *superclass_interface_decl =
6309 class_interface_decl->getSuperClass();
6310 if (superclass_interface_decl) {
6311 if (bit_offset_ptr)
6312 *bit_offset_ptr = 0;
6313 return CompilerType(getASTContext(),
6314 getASTContext()->getObjCInterfaceType(
6315 superclass_interface_decl));
6316 }
6317 }
6318 }
6319 }
6320 break;
6321
6322 case clang::Type::Typedef:
6323 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6324 ->getDecl()
6325 ->getUnderlyingType()
6326 .getAsOpaquePtr(),
6327 idx, bit_offset_ptr);
6328
6329 case clang::Type::Auto:
6330 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6331 ->getDeducedType()
6332 .getAsOpaquePtr(),
6333 idx, bit_offset_ptr);
6334
6335 case clang::Type::Elaborated:
6336 return GetDirectBaseClassAtIndex(
6337 llvm::cast<clang::ElaboratedType>(qual_type)
6338 ->getNamedType()
6339 .getAsOpaquePtr(),
6340 idx, bit_offset_ptr);
6341
6342 case clang::Type::Paren:
6343 return GetDirectBaseClassAtIndex(
6344 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6345 idx, bit_offset_ptr);
6346
6347 default:
6348 break;
6349 }
6350 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006351}
6352
Kate Stoneb9c1b512016-09-06 20:57:50 +00006353CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6354 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6355 clang::QualType qual_type(GetCanonicalQualType(type));
6356 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6357 switch (type_class) {
6358 case clang::Type::Record:
6359 if (GetCompleteType(type)) {
6360 const clang::CXXRecordDecl *cxx_record_decl =
6361 qual_type->getAsCXXRecordDecl();
6362 if (cxx_record_decl) {
6363 uint32_t curr_idx = 0;
6364 clang::CXXRecordDecl::base_class_const_iterator base_class,
6365 base_class_end;
6366 for (base_class = cxx_record_decl->vbases_begin(),
6367 base_class_end = cxx_record_decl->vbases_end();
6368 base_class != base_class_end; ++base_class, ++curr_idx) {
6369 if (curr_idx == idx) {
6370 if (bit_offset_ptr) {
6371 const clang::ASTRecordLayout &record_layout =
6372 getASTContext()->getASTRecordLayout(cxx_record_decl);
6373 const clang::CXXRecordDecl *base_class_decl =
6374 llvm::cast<clang::CXXRecordDecl>(
6375 base_class->getType()
6376 ->getAs<clang::RecordType>()
6377 ->getDecl());
6378 *bit_offset_ptr =
6379 record_layout.getVBaseClassOffset(base_class_decl)
6380 .getQuantity() *
6381 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006382 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006383 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6384 }
6385 }
6386 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006387 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006388 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006389
Kate Stoneb9c1b512016-09-06 20:57:50 +00006390 case clang::Type::Typedef:
6391 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6392 ->getDecl()
6393 ->getUnderlyingType()
6394 .getAsOpaquePtr(),
6395 idx, bit_offset_ptr);
6396
6397 case clang::Type::Auto:
6398 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6399 ->getDeducedType()
6400 .getAsOpaquePtr(),
6401 idx, bit_offset_ptr);
6402
6403 case clang::Type::Elaborated:
6404 return GetVirtualBaseClassAtIndex(
6405 llvm::cast<clang::ElaboratedType>(qual_type)
6406 ->getNamedType()
6407 .getAsOpaquePtr(),
6408 idx, bit_offset_ptr);
6409
6410 case clang::Type::Paren:
6411 return GetVirtualBaseClassAtIndex(
6412 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6413 idx, bit_offset_ptr);
6414
6415 default:
6416 break;
6417 }
6418 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006419}
6420
Greg Claytond8d4a572015-08-11 21:38:15 +00006421// If a pointer to a pointee type (the clang_type arg) says that it has no
6422// children, then we either need to trust it, or override it and return a
6423// different result. For example, an "int *" has one child that is an integer,
6424// but a function pointer doesn't have any children. Likewise if a Record type
6425// claims it has no children, then there really is nothing to show.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006426uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6427 if (type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00006428 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006429
6430 clang::QualType qual_type(type.getCanonicalType());
6431 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6432 switch (type_class) {
6433 case clang::Type::Builtin:
6434 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6435 case clang::BuiltinType::UnknownAny:
6436 case clang::BuiltinType::Void:
6437 case clang::BuiltinType::NullPtr:
6438 case clang::BuiltinType::OCLEvent:
6439 case clang::BuiltinType::OCLImage1dRO:
6440 case clang::BuiltinType::OCLImage1dWO:
6441 case clang::BuiltinType::OCLImage1dRW:
6442 case clang::BuiltinType::OCLImage1dArrayRO:
6443 case clang::BuiltinType::OCLImage1dArrayWO:
6444 case clang::BuiltinType::OCLImage1dArrayRW:
6445 case clang::BuiltinType::OCLImage1dBufferRO:
6446 case clang::BuiltinType::OCLImage1dBufferWO:
6447 case clang::BuiltinType::OCLImage1dBufferRW:
6448 case clang::BuiltinType::OCLImage2dRO:
6449 case clang::BuiltinType::OCLImage2dWO:
6450 case clang::BuiltinType::OCLImage2dRW:
6451 case clang::BuiltinType::OCLImage2dArrayRO:
6452 case clang::BuiltinType::OCLImage2dArrayWO:
6453 case clang::BuiltinType::OCLImage2dArrayRW:
6454 case clang::BuiltinType::OCLImage3dRO:
6455 case clang::BuiltinType::OCLImage3dWO:
6456 case clang::BuiltinType::OCLImage3dRW:
6457 case clang::BuiltinType::OCLSampler:
6458 return 0;
6459 case clang::BuiltinType::Bool:
6460 case clang::BuiltinType::Char_U:
6461 case clang::BuiltinType::UChar:
6462 case clang::BuiltinType::WChar_U:
6463 case clang::BuiltinType::Char16:
6464 case clang::BuiltinType::Char32:
6465 case clang::BuiltinType::UShort:
6466 case clang::BuiltinType::UInt:
6467 case clang::BuiltinType::ULong:
6468 case clang::BuiltinType::ULongLong:
6469 case clang::BuiltinType::UInt128:
6470 case clang::BuiltinType::Char_S:
6471 case clang::BuiltinType::SChar:
6472 case clang::BuiltinType::WChar_S:
6473 case clang::BuiltinType::Short:
6474 case clang::BuiltinType::Int:
6475 case clang::BuiltinType::Long:
6476 case clang::BuiltinType::LongLong:
6477 case clang::BuiltinType::Int128:
6478 case clang::BuiltinType::Float:
6479 case clang::BuiltinType::Double:
6480 case clang::BuiltinType::LongDouble:
6481 case clang::BuiltinType::Dependent:
6482 case clang::BuiltinType::Overload:
6483 case clang::BuiltinType::ObjCId:
6484 case clang::BuiltinType::ObjCClass:
6485 case clang::BuiltinType::ObjCSel:
6486 case clang::BuiltinType::BoundMember:
6487 case clang::BuiltinType::Half:
6488 case clang::BuiltinType::ARCUnbridgedCast:
6489 case clang::BuiltinType::PseudoObject:
6490 case clang::BuiltinType::BuiltinFn:
6491 case clang::BuiltinType::OMPArraySection:
6492 return 1;
6493 default:
6494 return 0;
6495 }
6496 break;
6497
6498 case clang::Type::Complex:
6499 return 1;
6500 case clang::Type::Pointer:
6501 return 1;
6502 case clang::Type::BlockPointer:
6503 return 0; // If block pointers don't have debug info, then no children for
6504 // them
6505 case clang::Type::LValueReference:
6506 return 1;
6507 case clang::Type::RValueReference:
6508 return 1;
6509 case clang::Type::MemberPointer:
6510 return 0;
6511 case clang::Type::ConstantArray:
6512 return 0;
6513 case clang::Type::IncompleteArray:
6514 return 0;
6515 case clang::Type::VariableArray:
6516 return 0;
6517 case clang::Type::DependentSizedArray:
6518 return 0;
6519 case clang::Type::DependentSizedExtVector:
6520 return 0;
6521 case clang::Type::Vector:
6522 return 0;
6523 case clang::Type::ExtVector:
6524 return 0;
6525 case clang::Type::FunctionProto:
6526 return 0; // When we function pointers, they have no children...
6527 case clang::Type::FunctionNoProto:
6528 return 0; // When we function pointers, they have no children...
6529 case clang::Type::UnresolvedUsing:
6530 return 0;
6531 case clang::Type::Paren:
6532 return GetNumPointeeChildren(
6533 llvm::cast<clang::ParenType>(qual_type)->desugar());
6534 case clang::Type::Typedef:
6535 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6536 ->getDecl()
6537 ->getUnderlyingType());
6538 case clang::Type::Auto:
6539 return GetNumPointeeChildren(
6540 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6541 case clang::Type::Elaborated:
6542 return GetNumPointeeChildren(
6543 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6544 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006545 return GetNumPointeeChildren(llvm::cast<clang::TypeOfExprType>(qual_type)
6546 ->getUnderlyingExpr()
6547 ->getType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006548 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006549 return GetNumPointeeChildren(
6550 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006551 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006552 return GetNumPointeeChildren(
6553 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006554 case clang::Type::Record:
6555 return 0;
6556 case clang::Type::Enum:
6557 return 1;
6558 case clang::Type::TemplateTypeParm:
6559 return 1;
6560 case clang::Type::SubstTemplateTypeParm:
6561 return 1;
6562 case clang::Type::TemplateSpecialization:
6563 return 1;
6564 case clang::Type::InjectedClassName:
6565 return 0;
6566 case clang::Type::DependentName:
6567 return 1;
6568 case clang::Type::DependentTemplateSpecialization:
6569 return 1;
6570 case clang::Type::ObjCObject:
6571 return 0;
6572 case clang::Type::ObjCInterface:
6573 return 0;
6574 case clang::Type::ObjCObjectPointer:
6575 return 1;
6576 default:
6577 break;
6578 }
6579 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006580}
6581
Kate Stoneb9c1b512016-09-06 20:57:50 +00006582CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6583 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6584 bool transparent_pointers, bool omit_empty_base_classes,
6585 bool ignore_array_bounds, std::string &child_name,
6586 uint32_t &child_byte_size, int32_t &child_byte_offset,
6587 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6588 bool &child_is_base_class, bool &child_is_deref_of_parent,
6589 ValueObject *valobj, uint64_t &language_flags) {
6590 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006591 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006592
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006593 auto get_exe_scope = [&exe_ctx]() {
6594 return exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr;
6595 };
6596
Kate Stoneb9c1b512016-09-06 20:57:50 +00006597 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6598 const clang::Type::TypeClass parent_type_class =
6599 parent_qual_type->getTypeClass();
6600 child_bitfield_bit_size = 0;
6601 child_bitfield_bit_offset = 0;
6602 child_is_base_class = false;
6603 language_flags = 0;
6604
Adrian Prantleca07c52018-11-05 20:49:07 +00006605 const bool idx_is_valid =
6606 idx < GetNumChildren(type, omit_empty_base_classes, exe_ctx);
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00006607 int32_t bit_offset;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006608 switch (parent_type_class) {
6609 case clang::Type::Builtin:
6610 if (idx_is_valid) {
6611 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6612 case clang::BuiltinType::ObjCId:
6613 case clang::BuiltinType::ObjCClass:
6614 child_name = "isa";
6615 child_byte_size =
6616 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6617 CHAR_BIT;
6618 return CompilerType(getASTContext(),
6619 getASTContext()->ObjCBuiltinClassTy);
6620
6621 default:
6622 break;
6623 }
6624 }
6625 break;
6626
6627 case clang::Type::Record:
6628 if (idx_is_valid && GetCompleteType(type)) {
6629 const clang::RecordType *record_type =
6630 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6631 const clang::RecordDecl *record_decl = record_type->getDecl();
6632 assert(record_decl);
6633 const clang::ASTRecordLayout &record_layout =
6634 getASTContext()->getASTRecordLayout(record_decl);
6635 uint32_t child_idx = 0;
6636
6637 const clang::CXXRecordDecl *cxx_record_decl =
6638 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6639 if (cxx_record_decl) {
6640 // We might have base classes to print out first
6641 clang::CXXRecordDecl::base_class_const_iterator base_class,
6642 base_class_end;
6643 for (base_class = cxx_record_decl->bases_begin(),
6644 base_class_end = cxx_record_decl->bases_end();
6645 base_class != base_class_end; ++base_class) {
6646 const clang::CXXRecordDecl *base_class_decl = nullptr;
6647
6648 // Skip empty base classes
6649 if (omit_empty_base_classes) {
6650 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6651 base_class->getType()->getAs<clang::RecordType>()->getDecl());
Jonas Devliegherea6682a42018-12-15 00:15:33 +00006652 if (!ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00006653 continue;
6654 }
6655
6656 if (idx == child_idx) {
6657 if (base_class_decl == nullptr)
6658 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6659 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6660
6661 if (base_class->isVirtual()) {
6662 bool handled = false;
6663 if (valobj) {
Aleksandr Urakov1dc51db2018-11-12 16:23:50 +00006664 clang::VTableContextBase *vtable_ctx =
6665 getASTContext()->getVTableContext();
6666 if (vtable_ctx)
6667 handled = GetVBaseBitOffset(*vtable_ctx, *valobj,
6668 record_layout, cxx_record_decl,
6669 base_class_decl, bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006670 }
6671 if (!handled)
6672 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6673 .getQuantity() *
6674 8;
6675 } else
6676 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6677 .getQuantity() *
6678 8;
6679
6680 // Base classes should be a multiple of 8 bits in size
6681 child_byte_offset = bit_offset / 8;
6682 CompilerType base_class_clang_type(getASTContext(),
6683 base_class->getType());
6684 child_name = base_class_clang_type.GetTypeName().AsCString("");
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006685 Optional<uint64_t> size =
6686 base_class_clang_type.GetBitSize(get_exe_scope());
Adrian Prantld963a7c2019-01-15 18:07:52 +00006687 if (!size)
6688 return {};
6689 uint64_t base_class_clang_type_bit_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006690
6691 // Base classes bit sizes should be a multiple of 8 bits in size
6692 assert(base_class_clang_type_bit_size % 8 == 0);
6693 child_byte_size = base_class_clang_type_bit_size / 8;
6694 child_is_base_class = true;
6695 return base_class_clang_type;
6696 }
6697 // We don't increment the child index in the for loop since we might
6698 // be skipping empty base classes
6699 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006700 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006701 }
6702 // Make sure index is in range...
6703 uint32_t field_idx = 0;
6704 clang::RecordDecl::field_iterator field, field_end;
6705 for (field = record_decl->field_begin(),
6706 field_end = record_decl->field_end();
6707 field != field_end; ++field, ++field_idx, ++child_idx) {
6708 if (idx == child_idx) {
6709 // Print the member type if requested
6710 // Print the member name and equal sign
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006711 child_name.assign(field->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006712
6713 // Figure out the type byte size (field_type_info.first) and
6714 // alignment (field_type_info.second) from the AST context.
6715 CompilerType field_clang_type(getASTContext(), field->getType());
6716 assert(field_idx < record_layout.getFieldCount());
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006717 Optional<uint64_t> size =
6718 field_clang_type.GetByteSize(get_exe_scope());
Adrian Prantld963a7c2019-01-15 18:07:52 +00006719 if (!size)
6720 return {};
6721 child_byte_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006722 const uint32_t child_bit_size = child_byte_size * 8;
6723
6724 // Figure out the field offset within the current struct/union/class
6725 // type
6726 bit_offset = record_layout.getFieldOffset(field_idx);
6727 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6728 child_bitfield_bit_size)) {
6729 child_bitfield_bit_offset = bit_offset % child_bit_size;
6730 const uint32_t child_bit_offset =
6731 bit_offset - child_bitfield_bit_offset;
6732 child_byte_offset = child_bit_offset / 8;
6733 } else {
6734 child_byte_offset = bit_offset / 8;
6735 }
6736
6737 return field_clang_type;
6738 }
6739 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006740 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006741 break;
6742
6743 case clang::Type::ObjCObject:
6744 case clang::Type::ObjCInterface:
6745 if (idx_is_valid && GetCompleteType(type)) {
6746 const clang::ObjCObjectType *objc_class_type =
6747 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6748 assert(objc_class_type);
6749 if (objc_class_type) {
6750 uint32_t child_idx = 0;
6751 clang::ObjCInterfaceDecl *class_interface_decl =
6752 objc_class_type->getInterface();
6753
6754 if (class_interface_decl) {
6755
6756 const clang::ASTRecordLayout &interface_layout =
6757 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6758 clang::ObjCInterfaceDecl *superclass_interface_decl =
6759 class_interface_decl->getSuperClass();
6760 if (superclass_interface_decl) {
6761 if (omit_empty_base_classes) {
6762 CompilerType base_class_clang_type(
6763 getASTContext(), getASTContext()->getObjCInterfaceType(
6764 superclass_interface_decl));
Adrian Prantleca07c52018-11-05 20:49:07 +00006765 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes,
6766 exe_ctx) > 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006767 if (idx == 0) {
6768 clang::QualType ivar_qual_type(
6769 getASTContext()->getObjCInterfaceType(
6770 superclass_interface_decl));
6771
6772 child_name.assign(
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006773 superclass_interface_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006774
6775 clang::TypeInfo ivar_type_info =
6776 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6777
6778 child_byte_size = ivar_type_info.Width / 8;
6779 child_byte_offset = 0;
6780 child_is_base_class = true;
6781
6782 return CompilerType(getASTContext(), ivar_qual_type);
6783 }
6784
6785 ++child_idx;
6786 }
6787 } else
6788 ++child_idx;
6789 }
6790
6791 const uint32_t superclass_idx = child_idx;
6792
6793 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6794 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6795 ivar_end = class_interface_decl->ivar_end();
6796
6797 for (ivar_pos = class_interface_decl->ivar_begin();
6798 ivar_pos != ivar_end; ++ivar_pos) {
6799 if (child_idx == idx) {
6800 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6801
6802 clang::QualType ivar_qual_type(ivar_decl->getType());
6803
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006804 child_name.assign(ivar_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006805
6806 clang::TypeInfo ivar_type_info =
6807 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6808
6809 child_byte_size = ivar_type_info.Width / 8;
6810
6811 // Figure out the field offset within the current
Adrian Prantl05097242018-04-30 16:49:04 +00006812 // struct/union/class type For ObjC objects, we can't trust the
6813 // bit offset we get from the Clang AST, since that doesn't
6814 // account for the space taken up by unbacked properties, or
6815 // from the changing size of base classes that are newer than
6816 // this class. So if we have a process around that we can ask
6817 // about this object, do so.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006818 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6819 Process *process = nullptr;
6820 if (exe_ctx)
6821 process = exe_ctx->GetProcessPtr();
6822 if (process) {
6823 ObjCLanguageRuntime *objc_runtime =
6824 process->GetObjCLanguageRuntime();
6825 if (objc_runtime != nullptr) {
6826 CompilerType parent_ast_type(getASTContext(),
6827 parent_qual_type);
6828 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6829 parent_ast_type, ivar_decl->getNameAsString().c_str());
6830 }
6831 }
6832
Aleksandr Urakovff701722018-08-20 05:59:27 +00006833 // Setting this to INT32_MAX to make sure we don't compute it
Kate Stoneb9c1b512016-09-06 20:57:50 +00006834 // twice...
Aleksandr Urakov53459482018-08-17 07:28:24 +00006835 bit_offset = INT32_MAX;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006836
6837 if (child_byte_offset ==
6838 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6839 bit_offset = interface_layout.getFieldOffset(child_idx -
6840 superclass_idx);
6841 child_byte_offset = bit_offset / 8;
6842 }
6843
6844 // Note, the ObjC Ivar Byte offset is just that, it doesn't
Adrian Prantl05097242018-04-30 16:49:04 +00006845 // account for the bit offset of a bitfield within its
6846 // containing object. So regardless of where we get the byte
Kate Stoneb9c1b512016-09-06 20:57:50 +00006847 // offset from, we still need to get the bit offset for
6848 // bitfields from the layout.
6849
6850 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6851 child_bitfield_bit_size)) {
Aleksandr Urakov53459482018-08-17 07:28:24 +00006852 if (bit_offset == INT32_MAX)
Kate Stoneb9c1b512016-09-06 20:57:50 +00006853 bit_offset = interface_layout.getFieldOffset(
6854 child_idx - superclass_idx);
6855
6856 child_bitfield_bit_offset = bit_offset % 8;
6857 }
6858 return CompilerType(getASTContext(), ivar_qual_type);
6859 }
6860 ++child_idx;
6861 }
6862 }
6863 }
6864 }
6865 }
6866 break;
6867
6868 case clang::Type::ObjCObjectPointer:
6869 if (idx_is_valid) {
6870 CompilerType pointee_clang_type(GetPointeeType(type));
6871
6872 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6873 child_is_deref_of_parent = false;
6874 bool tmp_child_is_deref_of_parent = false;
6875 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6876 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6877 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6878 child_bitfield_bit_size, child_bitfield_bit_offset,
6879 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6880 language_flags);
6881 } else {
6882 child_is_deref_of_parent = true;
6883 const char *parent_name =
6884 valobj ? valobj->GetName().GetCString() : NULL;
6885 if (parent_name) {
6886 child_name.assign(1, '*');
6887 child_name += parent_name;
6888 }
6889
6890 // We have a pointer to an simple type
6891 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006892 if (Optional<uint64_t> size =
6893 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006894 child_byte_size = *size;
6895 child_byte_offset = 0;
6896 return pointee_clang_type;
6897 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006898 }
6899 }
6900 }
6901 break;
6902
6903 case clang::Type::Vector:
6904 case clang::Type::ExtVector:
6905 if (idx_is_valid) {
6906 const clang::VectorType *array =
6907 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6908 if (array) {
6909 CompilerType element_type(getASTContext(), array->getElementType());
6910 if (element_type.GetCompleteType()) {
6911 char element_name[64];
6912 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6913 static_cast<uint64_t>(idx));
6914 child_name.assign(element_name);
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006915 if (Optional<uint64_t> size =
6916 element_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006917 child_byte_size = *size;
6918 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6919 return element_type;
6920 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006921 }
6922 }
6923 }
6924 break;
6925
6926 case clang::Type::ConstantArray:
6927 case clang::Type::IncompleteArray:
6928 if (ignore_array_bounds || idx_is_valid) {
6929 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6930 if (array) {
6931 CompilerType element_type(getASTContext(), array->getElementType());
6932 if (element_type.GetCompleteType()) {
Zachary Turner827d5d72016-12-16 04:27:00 +00006933 child_name = llvm::formatv("[{0}]", idx);
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006934 if (Optional<uint64_t> size =
6935 element_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006936 child_byte_size = *size;
6937 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6938 return element_type;
6939 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006940 }
6941 }
6942 }
6943 break;
6944
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006945 case clang::Type::Pointer: {
6946 CompilerType pointee_clang_type(GetPointeeType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00006947
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006948 // Don't dereference "void *" pointers
6949 if (pointee_clang_type.IsVoidType())
6950 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006951
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006952 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6953 child_is_deref_of_parent = false;
6954 bool tmp_child_is_deref_of_parent = false;
6955 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6956 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6957 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6958 child_bitfield_bit_size, child_bitfield_bit_offset,
6959 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6960 language_flags);
6961 } else {
6962 child_is_deref_of_parent = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006963
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006964 const char *parent_name =
6965 valobj ? valobj->GetName().GetCString() : NULL;
6966 if (parent_name) {
6967 child_name.assign(1, '*');
6968 child_name += parent_name;
6969 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006970
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006971 // We have a pointer to an simple type
6972 if (idx == 0) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006973 if (Optional<uint64_t> size =
6974 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006975 child_byte_size = *size;
6976 child_byte_offset = 0;
6977 return pointee_clang_type;
6978 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006979 }
6980 }
6981 break;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006982 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006983
6984 case clang::Type::LValueReference:
6985 case clang::Type::RValueReference:
6986 if (idx_is_valid) {
6987 const clang::ReferenceType *reference_type =
6988 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
6989 CompilerType pointee_clang_type(getASTContext(),
6990 reference_type->getPointeeType());
6991 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6992 child_is_deref_of_parent = false;
6993 bool tmp_child_is_deref_of_parent = false;
6994 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6995 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6996 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6997 child_bitfield_bit_size, child_bitfield_bit_offset,
6998 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6999 language_flags);
7000 } else {
7001 const char *parent_name =
7002 valobj ? valobj->GetName().GetCString() : NULL;
7003 if (parent_name) {
7004 child_name.assign(1, '&');
7005 child_name += parent_name;
7006 }
7007
7008 // We have a pointer to an simple type
7009 if (idx == 0) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00007010 if (Optional<uint64_t> size =
7011 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00007012 child_byte_size = *size;
7013 child_byte_offset = 0;
7014 return pointee_clang_type;
7015 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007016 }
7017 }
7018 }
7019 break;
7020
7021 case clang::Type::Typedef: {
7022 CompilerType typedefed_clang_type(
7023 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)
7024 ->getDecl()
7025 ->getUnderlyingType());
7026 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
7027 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7028 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7029 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7030 child_is_deref_of_parent, valobj, language_flags);
7031 } break;
7032
7033 case clang::Type::Auto: {
7034 CompilerType elaborated_clang_type(
7035 getASTContext(),
7036 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
7037 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
7038 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7039 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7040 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7041 child_is_deref_of_parent, valobj, language_flags);
7042 }
7043
7044 case clang::Type::Elaborated: {
7045 CompilerType elaborated_clang_type(
7046 getASTContext(),
7047 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
7048 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
7049 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7050 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7051 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7052 child_is_deref_of_parent, valobj, language_flags);
7053 }
7054
7055 case clang::Type::Paren: {
7056 CompilerType paren_clang_type(
7057 getASTContext(),
7058 llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
7059 return paren_clang_type.GetChildCompilerTypeAtIndex(
7060 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7061 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7062 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7063 child_is_deref_of_parent, valobj, language_flags);
7064 }
7065
7066 default:
7067 break;
7068 }
7069 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007070}
7071
Kate Stoneb9c1b512016-09-06 20:57:50 +00007072static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
7073 const clang::CXXBaseSpecifier *base_spec,
7074 bool omit_empty_base_classes) {
7075 uint32_t child_idx = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007076
Kate Stoneb9c1b512016-09-06 20:57:50 +00007077 const clang::CXXRecordDecl *cxx_record_decl =
7078 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7079
7080 // const char *super_name = record_decl->getNameAsCString();
7081 // const char *base_name =
7082 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
7083 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
7084 //
7085 if (cxx_record_decl) {
7086 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
7087 for (base_class = cxx_record_decl->bases_begin(),
7088 base_class_end = cxx_record_decl->bases_end();
7089 base_class != base_class_end; ++base_class) {
7090 if (omit_empty_base_classes) {
7091 if (BaseSpecifierIsEmpty(base_class))
7092 continue;
7093 }
7094
7095 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
7096 // super_name, base_name,
7097 // child_idx,
7098 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
7099 //
7100 //
7101 if (base_class == base_spec)
7102 return child_idx;
7103 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00007104 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007105 }
7106
7107 return UINT32_MAX;
7108}
7109
7110static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
7111 clang::NamedDecl *canonical_decl,
7112 bool omit_empty_base_classes) {
7113 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
7114 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
7115 omit_empty_base_classes);
7116
7117 clang::RecordDecl::field_iterator field, field_end;
7118 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
7119 field != field_end; ++field, ++child_idx) {
7120 if (field->getCanonicalDecl() == canonical_decl)
7121 return child_idx;
7122 }
7123
7124 return UINT32_MAX;
Greg Claytond8d4a572015-08-11 21:38:15 +00007125}
7126
7127// Look for a child member (doesn't include base classes, but it does include
Adrian Prantl05097242018-04-30 16:49:04 +00007128// their members) in the type hierarchy. Returns an index path into
7129// "clang_type" on how to reach the appropriate member.
Greg Claytond8d4a572015-08-11 21:38:15 +00007130//
7131// class A
7132// {
7133// public:
7134// int m_a;
7135// int m_b;
7136// };
7137//
7138// class B
7139// {
7140// };
7141//
7142// class C :
7143// public B,
7144// public A
7145// {
7146// };
7147//
7148// If we have a clang type that describes "class C", and we wanted to looked
7149// "m_b" in it:
7150//
Kate Stoneb9c1b512016-09-06 20:57:50 +00007151// With omit_empty_base_classes == false we would get an integer array back
Adrian Prantl05097242018-04-30 16:49:04 +00007152// with: { 1, 1 } The first index 1 is the child index for "class A" within
7153// class C The second index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007154//
Adrian Prantl05097242018-04-30 16:49:04 +00007155// With omit_empty_base_classes == true we would get an integer array back
7156// with: { 0, 1 } The first index 0 is the child index for "class A" within
7157// class C (since class B doesn't have any members it doesn't count) The second
7158// index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007159
Kate Stoneb9c1b512016-09-06 20:57:50 +00007160size_t ClangASTContext::GetIndexOfChildMemberWithName(
7161 lldb::opaque_compiler_type_t type, const char *name,
7162 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
7163 if (type && name && name[0]) {
7164 clang::QualType qual_type(GetCanonicalQualType(type));
7165 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7166 switch (type_class) {
7167 case clang::Type::Record:
7168 if (GetCompleteType(type)) {
7169 const clang::RecordType *record_type =
7170 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7171 const clang::RecordDecl *record_decl = record_type->getDecl();
Enrico Granata36f51e42015-12-18 22:41:25 +00007172
Kate Stoneb9c1b512016-09-06 20:57:50 +00007173 assert(record_decl);
7174 uint32_t child_idx = 0;
7175
7176 const clang::CXXRecordDecl *cxx_record_decl =
7177 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7178
7179 // Try and find a field that matches NAME
7180 clang::RecordDecl::field_iterator field, field_end;
7181 llvm::StringRef name_sref(name);
7182 for (field = record_decl->field_begin(),
7183 field_end = record_decl->field_end();
7184 field != field_end; ++field, ++child_idx) {
7185 llvm::StringRef field_name = field->getName();
7186 if (field_name.empty()) {
7187 CompilerType field_type(getASTContext(), field->getType());
7188 child_indexes.push_back(child_idx);
7189 if (field_type.GetIndexOfChildMemberWithName(
7190 name, omit_empty_base_classes, child_indexes))
7191 return child_indexes.size();
7192 child_indexes.pop_back();
7193
7194 } else if (field_name.equals(name_sref)) {
7195 // We have to add on the number of base classes to this index!
7196 child_indexes.push_back(
7197 child_idx + ClangASTContext::GetNumBaseClasses(
7198 cxx_record_decl, omit_empty_base_classes));
7199 return child_indexes.size();
7200 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007201 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007202
Kate Stoneb9c1b512016-09-06 20:57:50 +00007203 if (cxx_record_decl) {
7204 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7205
7206 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7207
7208 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7209 // Didn't find things easily, lets let clang do its thang...
7210 clang::IdentifierInfo &ident_ref =
7211 getASTContext()->Idents.get(name_sref);
7212 clang::DeclarationName decl_name(&ident_ref);
7213
7214 clang::CXXBasePaths paths;
7215 if (cxx_record_decl->lookupInBases(
7216 [decl_name](const clang::CXXBaseSpecifier *specifier,
7217 clang::CXXBasePath &path) {
7218 return clang::CXXRecordDecl::FindOrdinaryMember(
7219 specifier, path, decl_name);
7220 },
7221 paths)) {
7222 clang::CXXBasePaths::const_paths_iterator path,
7223 path_end = paths.end();
7224 for (path = paths.begin(); path != path_end; ++path) {
7225 const size_t num_path_elements = path->size();
7226 for (size_t e = 0; e < num_path_elements; ++e) {
7227 clang::CXXBasePathElement elem = (*path)[e];
7228
7229 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7230 omit_empty_base_classes);
7231 if (child_idx == UINT32_MAX) {
7232 child_indexes.clear();
7233 return 0;
7234 } else {
7235 child_indexes.push_back(child_idx);
7236 parent_record_decl = llvm::cast<clang::RecordDecl>(
7237 elem.Base->getType()
7238 ->getAs<clang::RecordType>()
7239 ->getDecl());
7240 }
7241 }
7242 for (clang::NamedDecl *path_decl : path->Decls) {
7243 child_idx = GetIndexForRecordChild(
7244 parent_record_decl, path_decl, omit_empty_base_classes);
7245 if (child_idx == UINT32_MAX) {
7246 child_indexes.clear();
7247 return 0;
7248 } else {
7249 child_indexes.push_back(child_idx);
7250 }
7251 }
7252 }
7253 return child_indexes.size();
7254 }
7255 }
7256 }
7257 break;
7258
7259 case clang::Type::ObjCObject:
7260 case clang::Type::ObjCInterface:
7261 if (GetCompleteType(type)) {
7262 llvm::StringRef name_sref(name);
7263 const clang::ObjCObjectType *objc_class_type =
7264 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7265 assert(objc_class_type);
7266 if (objc_class_type) {
7267 uint32_t child_idx = 0;
7268 clang::ObjCInterfaceDecl *class_interface_decl =
7269 objc_class_type->getInterface();
7270
7271 if (class_interface_decl) {
7272 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7273 ivar_end = class_interface_decl->ivar_end();
7274 clang::ObjCInterfaceDecl *superclass_interface_decl =
7275 class_interface_decl->getSuperClass();
7276
7277 for (ivar_pos = class_interface_decl->ivar_begin();
7278 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7279 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7280
7281 if (ivar_decl->getName().equals(name_sref)) {
7282 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7283 (omit_empty_base_classes &&
7284 ObjCDeclHasIVars(superclass_interface_decl, true)))
7285 ++child_idx;
7286
7287 child_indexes.push_back(child_idx);
7288 return child_indexes.size();
7289 }
7290 }
7291
7292 if (superclass_interface_decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00007293 // The super class index is always zero for ObjC classes, so we
7294 // push it onto the child indexes in case we find an ivar in our
7295 // superclass...
Kate Stoneb9c1b512016-09-06 20:57:50 +00007296 child_indexes.push_back(0);
7297
7298 CompilerType superclass_clang_type(
7299 getASTContext(), getASTContext()->getObjCInterfaceType(
7300 superclass_interface_decl));
7301 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7302 name, omit_empty_base_classes, child_indexes)) {
Adrian Prantl05097242018-04-30 16:49:04 +00007303 // We did find an ivar in a superclass so just return the
7304 // results!
Kate Stoneb9c1b512016-09-06 20:57:50 +00007305 return child_indexes.size();
7306 }
7307
Adrian Prantl05097242018-04-30 16:49:04 +00007308 // We didn't find an ivar matching "name" in our superclass, pop
7309 // the superclass zero index that we pushed on above.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007310 child_indexes.pop_back();
7311 }
7312 }
7313 }
7314 }
7315 break;
7316
7317 case clang::Type::ObjCObjectPointer: {
7318 CompilerType objc_object_clang_type(
7319 getASTContext(),
7320 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7321 ->getPointeeType());
7322 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7323 name, omit_empty_base_classes, child_indexes);
7324 } break;
7325
7326 case clang::Type::ConstantArray: {
7327 // const clang::ConstantArrayType *array =
7328 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7329 // const uint64_t element_count =
7330 // array->getSize().getLimitedValue();
7331 //
7332 // if (idx < element_count)
7333 // {
7334 // std::pair<uint64_t, unsigned> field_type_info =
7335 // ast->getTypeInfo(array->getElementType());
7336 //
7337 // char element_name[32];
7338 // ::snprintf (element_name, sizeof (element_name),
7339 // "%s[%u]", parent_name ? parent_name : "", idx);
7340 //
7341 // child_name.assign(element_name);
7342 // assert(field_type_info.first % 8 == 0);
7343 // child_byte_size = field_type_info.first / 8;
7344 // child_byte_offset = idx * child_byte_size;
7345 // return array->getElementType().getAsOpaquePtr();
7346 // }
7347 } break;
7348
7349 // case clang::Type::MemberPointerType:
7350 // {
7351 // MemberPointerType *mem_ptr_type =
7352 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7353 // clang::QualType pointee_type =
7354 // mem_ptr_type->getPointeeType();
7355 //
7356 // if (ClangASTContext::IsAggregateType
7357 // (pointee_type.getAsOpaquePtr()))
7358 // {
7359 // return GetIndexOfChildWithName (ast,
7360 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7361 // name);
7362 // }
7363 // }
7364 // break;
7365 //
7366 case clang::Type::LValueReference:
7367 case clang::Type::RValueReference: {
7368 const clang::ReferenceType *reference_type =
7369 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7370 clang::QualType pointee_type(reference_type->getPointeeType());
7371 CompilerType pointee_clang_type(getASTContext(), pointee_type);
7372
7373 if (pointee_clang_type.IsAggregateType()) {
7374 return pointee_clang_type.GetIndexOfChildMemberWithName(
7375 name, omit_empty_base_classes, child_indexes);
7376 }
7377 } break;
7378
7379 case clang::Type::Pointer: {
7380 CompilerType pointee_clang_type(GetPointeeType(type));
7381
7382 if (pointee_clang_type.IsAggregateType()) {
7383 return pointee_clang_type.GetIndexOfChildMemberWithName(
7384 name, omit_empty_base_classes, child_indexes);
7385 }
7386 } break;
7387
7388 case clang::Type::Typedef:
7389 return CompilerType(getASTContext(),
7390 llvm::cast<clang::TypedefType>(qual_type)
7391 ->getDecl()
7392 ->getUnderlyingType())
7393 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7394 child_indexes);
7395
7396 case clang::Type::Auto:
7397 return CompilerType(
7398 getASTContext(),
7399 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7400 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7401 child_indexes);
7402
7403 case clang::Type::Elaborated:
7404 return CompilerType(
7405 getASTContext(),
7406 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7407 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7408 child_indexes);
7409
7410 case clang::Type::Paren:
7411 return CompilerType(getASTContext(),
7412 llvm::cast<clang::ParenType>(qual_type)->desugar())
7413 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7414 child_indexes);
7415
7416 default:
7417 break;
7418 }
7419 }
7420 return 0;
7421}
Greg Claytond8d4a572015-08-11 21:38:15 +00007422
7423// Get the index of the child of "clang_type" whose name matches. This function
7424// doesn't descend into the children, but only looks one level deep and name
7425// matches can include base class names.
7426
7427uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007428ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7429 const char *name,
7430 bool omit_empty_base_classes) {
7431 if (type && name && name[0]) {
7432 clang::QualType qual_type(GetCanonicalQualType(type));
Enrico Granata36f51e42015-12-18 22:41:25 +00007433
Kate Stoneb9c1b512016-09-06 20:57:50 +00007434 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7435
7436 switch (type_class) {
7437 case clang::Type::Record:
7438 if (GetCompleteType(type)) {
7439 const clang::RecordType *record_type =
7440 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7441 const clang::RecordDecl *record_decl = record_type->getDecl();
7442
7443 assert(record_decl);
7444 uint32_t child_idx = 0;
7445
7446 const clang::CXXRecordDecl *cxx_record_decl =
7447 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7448
7449 if (cxx_record_decl) {
7450 clang::CXXRecordDecl::base_class_const_iterator base_class,
7451 base_class_end;
7452 for (base_class = cxx_record_decl->bases_begin(),
7453 base_class_end = cxx_record_decl->bases_end();
7454 base_class != base_class_end; ++base_class) {
7455 // Skip empty base classes
7456 clang::CXXRecordDecl *base_class_decl =
7457 llvm::cast<clang::CXXRecordDecl>(
7458 base_class->getType()
7459 ->getAs<clang::RecordType>()
7460 ->getDecl());
7461 if (omit_empty_base_classes &&
Jonas Devliegherea6682a42018-12-15 00:15:33 +00007462 !ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00007463 continue;
7464
7465 CompilerType base_class_clang_type(getASTContext(),
7466 base_class->getType());
7467 std::string base_class_type_name(
7468 base_class_clang_type.GetTypeName().AsCString(""));
Jonas Devlieghere8d20cfd2018-12-21 22:46:10 +00007469 if (base_class_type_name == name)
Kate Stoneb9c1b512016-09-06 20:57:50 +00007470 return child_idx;
7471 ++child_idx;
7472 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007473 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007474
Kate Stoneb9c1b512016-09-06 20:57:50 +00007475 // Try and find a field that matches NAME
7476 clang::RecordDecl::field_iterator field, field_end;
7477 llvm::StringRef name_sref(name);
7478 for (field = record_decl->field_begin(),
7479 field_end = record_decl->field_end();
7480 field != field_end; ++field, ++child_idx) {
7481 if (field->getName().equals(name_sref))
7482 return child_idx;
7483 }
7484 }
7485 break;
7486
7487 case clang::Type::ObjCObject:
7488 case clang::Type::ObjCInterface:
7489 if (GetCompleteType(type)) {
7490 llvm::StringRef name_sref(name);
7491 const clang::ObjCObjectType *objc_class_type =
7492 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7493 assert(objc_class_type);
7494 if (objc_class_type) {
7495 uint32_t child_idx = 0;
7496 clang::ObjCInterfaceDecl *class_interface_decl =
7497 objc_class_type->getInterface();
7498
7499 if (class_interface_decl) {
7500 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7501 ivar_end = class_interface_decl->ivar_end();
7502 clang::ObjCInterfaceDecl *superclass_interface_decl =
7503 class_interface_decl->getSuperClass();
7504
7505 for (ivar_pos = class_interface_decl->ivar_begin();
7506 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7507 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7508
7509 if (ivar_decl->getName().equals(name_sref)) {
7510 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7511 (omit_empty_base_classes &&
7512 ObjCDeclHasIVars(superclass_interface_decl, true)))
7513 ++child_idx;
7514
7515 return child_idx;
7516 }
7517 }
7518
7519 if (superclass_interface_decl) {
7520 if (superclass_interface_decl->getName().equals(name_sref))
7521 return 0;
7522 }
7523 }
7524 }
7525 }
7526 break;
7527
7528 case clang::Type::ObjCObjectPointer: {
7529 CompilerType pointee_clang_type(
7530 getASTContext(),
7531 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7532 ->getPointeeType());
7533 return pointee_clang_type.GetIndexOfChildWithName(
7534 name, omit_empty_base_classes);
7535 } break;
7536
7537 case clang::Type::ConstantArray: {
7538 // const clang::ConstantArrayType *array =
7539 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7540 // const uint64_t element_count =
7541 // array->getSize().getLimitedValue();
7542 //
7543 // if (idx < element_count)
7544 // {
7545 // std::pair<uint64_t, unsigned> field_type_info =
7546 // ast->getTypeInfo(array->getElementType());
7547 //
7548 // char element_name[32];
7549 // ::snprintf (element_name, sizeof (element_name),
7550 // "%s[%u]", parent_name ? parent_name : "", idx);
7551 //
7552 // child_name.assign(element_name);
7553 // assert(field_type_info.first % 8 == 0);
7554 // child_byte_size = field_type_info.first / 8;
7555 // child_byte_offset = idx * child_byte_size;
7556 // return array->getElementType().getAsOpaquePtr();
7557 // }
7558 } break;
7559
7560 // case clang::Type::MemberPointerType:
7561 // {
7562 // MemberPointerType *mem_ptr_type =
7563 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7564 // clang::QualType pointee_type =
7565 // mem_ptr_type->getPointeeType();
7566 //
7567 // if (ClangASTContext::IsAggregateType
7568 // (pointee_type.getAsOpaquePtr()))
7569 // {
7570 // return GetIndexOfChildWithName (ast,
7571 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7572 // name);
7573 // }
7574 // }
7575 // break;
7576 //
7577 case clang::Type::LValueReference:
7578 case clang::Type::RValueReference: {
7579 const clang::ReferenceType *reference_type =
7580 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7581 CompilerType pointee_type(getASTContext(),
7582 reference_type->getPointeeType());
7583
7584 if (pointee_type.IsAggregateType()) {
7585 return pointee_type.GetIndexOfChildWithName(name,
7586 omit_empty_base_classes);
7587 }
7588 } break;
7589
7590 case clang::Type::Pointer: {
7591 const clang::PointerType *pointer_type =
7592 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7593 CompilerType pointee_type(getASTContext(),
7594 pointer_type->getPointeeType());
7595
7596 if (pointee_type.IsAggregateType()) {
7597 return pointee_type.GetIndexOfChildWithName(name,
7598 omit_empty_base_classes);
7599 } else {
7600 // if (parent_name)
7601 // {
7602 // child_name.assign(1, '*');
7603 // child_name += parent_name;
7604 // }
7605 //
7606 // // We have a pointer to an simple type
7607 // if (idx == 0)
7608 // {
7609 // std::pair<uint64_t, unsigned> clang_type_info
7610 // = ast->getTypeInfo(pointee_type);
7611 // assert(clang_type_info.first % 8 == 0);
7612 // child_byte_size = clang_type_info.first / 8;
7613 // child_byte_offset = 0;
7614 // return pointee_type.getAsOpaquePtr();
7615 // }
7616 }
7617 } break;
7618
7619 case clang::Type::Auto:
7620 return CompilerType(
7621 getASTContext(),
7622 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7623 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7624
7625 case clang::Type::Elaborated:
7626 return CompilerType(
7627 getASTContext(),
7628 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7629 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7630
7631 case clang::Type::Paren:
7632 return CompilerType(getASTContext(),
7633 llvm::cast<clang::ParenType>(qual_type)->desugar())
7634 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7635
7636 case clang::Type::Typedef:
7637 return CompilerType(getASTContext(),
7638 llvm::cast<clang::TypedefType>(qual_type)
7639 ->getDecl()
7640 ->getUnderlyingType())
7641 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7642
7643 default:
7644 break;
7645 }
7646 }
7647 return UINT32_MAX;
7648}
Greg Claytond8d4a572015-08-11 21:38:15 +00007649
7650size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007651ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7652 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007653 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007654
Kate Stoneb9c1b512016-09-06 20:57:50 +00007655 clang::QualType qual_type(GetCanonicalQualType(type));
7656 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7657 switch (type_class) {
7658 case clang::Type::Record:
7659 if (GetCompleteType(type)) {
7660 const clang::CXXRecordDecl *cxx_record_decl =
7661 qual_type->getAsCXXRecordDecl();
7662 if (cxx_record_decl) {
7663 const clang::ClassTemplateSpecializationDecl *template_decl =
7664 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7665 cxx_record_decl);
7666 if (template_decl)
7667 return template_decl->getTemplateArgs().size();
7668 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007669 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007670 break;
7671
7672 case clang::Type::Typedef:
7673 return (CompilerType(getASTContext(),
7674 llvm::cast<clang::TypedefType>(qual_type)
7675 ->getDecl()
7676 ->getUnderlyingType()))
7677 .GetNumTemplateArguments();
7678
7679 case clang::Type::Auto:
7680 return (CompilerType(
7681 getASTContext(),
7682 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7683 .GetNumTemplateArguments();
7684
7685 case clang::Type::Elaborated:
7686 return (CompilerType(
7687 getASTContext(),
7688 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7689 .GetNumTemplateArguments();
7690
7691 case clang::Type::Paren:
7692 return (CompilerType(getASTContext(),
7693 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7694 .GetNumTemplateArguments();
7695
7696 default:
7697 break;
7698 }
7699
7700 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007701}
7702
Pavel Labath769b21e2017-11-13 14:26:21 +00007703const clang::ClassTemplateSpecializationDecl *
7704ClangASTContext::GetAsTemplateSpecialization(
7705 lldb::opaque_compiler_type_t type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00007706 if (!type)
Pavel Labath769b21e2017-11-13 14:26:21 +00007707 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007708
7709 clang::QualType qual_type(GetCanonicalQualType(type));
7710 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7711 switch (type_class) {
Pavel Labath769b21e2017-11-13 14:26:21 +00007712 case clang::Type::Record: {
7713 if (! GetCompleteType(type))
7714 return nullptr;
7715 const clang::CXXRecordDecl *cxx_record_decl =
7716 qual_type->getAsCXXRecordDecl();
7717 if (!cxx_record_decl)
7718 return nullptr;
7719 return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7720 cxx_record_decl);
7721 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007722
7723 case clang::Type::Typedef:
Pavel Labath769b21e2017-11-13 14:26:21 +00007724 return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type)
7725 ->getDecl()
7726 ->getUnderlyingType()
7727 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007728
7729 case clang::Type::Auto:
Pavel Labath769b21e2017-11-13 14:26:21 +00007730 return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type)
7731 ->getDeducedType()
7732 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007733
7734 case clang::Type::Elaborated:
Pavel Labath769b21e2017-11-13 14:26:21 +00007735 return GetAsTemplateSpecialization(
7736 llvm::cast<clang::ElaboratedType>(qual_type)
7737 ->getNamedType()
7738 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007739
7740 case clang::Type::Paren:
Pavel Labath769b21e2017-11-13 14:26:21 +00007741 return GetAsTemplateSpecialization(
7742 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007743
7744 default:
Pavel Labath769b21e2017-11-13 14:26:21 +00007745 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007746 }
Pavel Labath769b21e2017-11-13 14:26:21 +00007747}
7748
7749lldb::TemplateArgumentKind
7750ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
7751 size_t arg_idx) {
7752 const clang::ClassTemplateSpecializationDecl *template_decl =
7753 GetAsTemplateSpecialization(type);
7754 if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size())
7755 return eTemplateArgumentKindNull;
7756
7757 switch (template_decl->getTemplateArgs()[arg_idx].getKind()) {
7758 case clang::TemplateArgument::Null:
7759 return eTemplateArgumentKindNull;
7760
7761 case clang::TemplateArgument::NullPtr:
7762 return eTemplateArgumentKindNullPtr;
7763
7764 case clang::TemplateArgument::Type:
7765 return eTemplateArgumentKindType;
7766
7767 case clang::TemplateArgument::Declaration:
7768 return eTemplateArgumentKindDeclaration;
7769
7770 case clang::TemplateArgument::Integral:
7771 return eTemplateArgumentKindIntegral;
7772
7773 case clang::TemplateArgument::Template:
7774 return eTemplateArgumentKindTemplate;
7775
7776 case clang::TemplateArgument::TemplateExpansion:
7777 return eTemplateArgumentKindTemplateExpansion;
7778
7779 case clang::TemplateArgument::Expression:
7780 return eTemplateArgumentKindExpression;
7781
7782 case clang::TemplateArgument::Pack:
7783 return eTemplateArgumentKindPack;
7784 }
7785 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
7786}
7787
7788CompilerType
7789ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
7790 size_t idx) {
7791 const clang::ClassTemplateSpecializationDecl *template_decl =
7792 GetAsTemplateSpecialization(type);
7793 if (!template_decl || idx >= template_decl->getTemplateArgs().size())
7794 return CompilerType();
7795
7796 const clang::TemplateArgument &template_arg =
7797 template_decl->getTemplateArgs()[idx];
7798 if (template_arg.getKind() != clang::TemplateArgument::Type)
7799 return CompilerType();
7800
7801 return CompilerType(getASTContext(), template_arg.getAsType());
7802}
7803
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00007804Optional<CompilerType::IntegralTemplateArgument>
Pavel Labath769b21e2017-11-13 14:26:21 +00007805ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
7806 size_t idx) {
7807 const clang::ClassTemplateSpecializationDecl *template_decl =
7808 GetAsTemplateSpecialization(type);
7809 if (! template_decl || idx >= template_decl->getTemplateArgs().size())
Pavel Labathf59056f2017-11-30 10:16:54 +00007810 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007811
7812 const clang::TemplateArgument &template_arg =
7813 template_decl->getTemplateArgs()[idx];
7814 if (template_arg.getKind() != clang::TemplateArgument::Integral)
Pavel Labathf59056f2017-11-30 10:16:54 +00007815 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007816
Pavel Labathf59056f2017-11-30 10:16:54 +00007817 return {{template_arg.getAsIntegral(),
7818 CompilerType(getASTContext(), template_arg.getIntegralType())}};
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007819}
7820
Kate Stoneb9c1b512016-09-06 20:57:50 +00007821CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7822 if (type)
7823 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7824 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007825}
7826
Kate Stoneb9c1b512016-09-06 20:57:50 +00007827clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7828 const clang::EnumType *enutype =
7829 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7830 if (enutype)
7831 return enutype->getDecl();
7832 return NULL;
7833}
7834
7835clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7836 const clang::RecordType *record_type =
7837 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7838 if (record_type)
7839 return record_type->getDecl();
7840 return nullptr;
7841}
7842
7843clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
Zachary Turner1639c6b2018-12-17 16:15:13 +00007844 return ClangUtil::GetAsTagDecl(type);
Greg Claytone6b36cd2015-12-08 01:02:08 +00007845}
7846
Aleksandr Urakov709426b2018-09-10 08:08:43 +00007847clang::TypedefNameDecl *
7848ClangASTContext::GetAsTypedefDecl(const CompilerType &type) {
7849 const clang::TypedefType *typedef_type =
7850 llvm::dyn_cast<clang::TypedefType>(ClangUtil::GetQualType(type));
7851 if (typedef_type)
7852 return typedef_type->getDecl();
7853 return nullptr;
7854}
7855
Greg Claytond8d4a572015-08-11 21:38:15 +00007856clang::CXXRecordDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007857ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7858 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
Greg Claytond8d4a572015-08-11 21:38:15 +00007859}
7860
7861clang::ObjCInterfaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007862ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7863 const clang::ObjCObjectType *objc_class_type =
7864 llvm::dyn_cast<clang::ObjCObjectType>(
7865 ClangUtil::GetCanonicalQualType(type));
7866 if (objc_class_type)
7867 return objc_class_type->getInterface();
7868 return nullptr;
7869}
7870
7871clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007872 const CompilerType &type, llvm::StringRef name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00007873 const CompilerType &field_clang_type, AccessType access,
7874 uint32_t bitfield_bit_size) {
7875 if (!type.IsValid() || !field_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00007876 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007877 ClangASTContext *ast =
7878 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7879 if (!ast)
7880 return nullptr;
7881 clang::ASTContext *clang_ast = ast->getASTContext();
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007882 clang::IdentifierInfo *ident = nullptr;
7883 if (!name.empty())
7884 ident = &clang_ast->Idents.get(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00007885
7886 clang::FieldDecl *field = nullptr;
7887
7888 clang::Expr *bit_width = nullptr;
7889 if (bitfield_bit_size != 0) {
7890 llvm::APInt bitfield_bit_size_apint(
7891 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7892 bit_width = new (*clang_ast)
7893 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7894 clang_ast->IntTy, clang::SourceLocation());
7895 }
7896
7897 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7898 if (record_decl) {
7899 field = clang::FieldDecl::Create(
7900 *clang_ast, record_decl, clang::SourceLocation(),
7901 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007902 ident, // Identifier
7903 ClangUtil::GetQualType(field_clang_type), // Field type
7904 nullptr, // TInfo *
7905 bit_width, // BitWidth
7906 false, // Mutable
7907 clang::ICIS_NoInit); // HasInit
Kate Stoneb9c1b512016-09-06 20:57:50 +00007908
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007909 if (name.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +00007910 // Determine whether this field corresponds to an anonymous struct or
7911 // union.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007912 if (const clang::TagType *TagT =
7913 field->getType()->getAs<clang::TagType>()) {
7914 if (clang::RecordDecl *Rec =
7915 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7916 if (!Rec->getDeclName()) {
7917 Rec->setAnonymousStructOrUnion(true);
7918 field->setImplicit();
7919 }
7920 }
7921 }
7922
7923 if (field) {
7924 field->setAccess(
7925 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7926
7927 record_decl->addDecl(field);
7928
7929#ifdef LLDB_CONFIGURATION_DEBUG
7930 VerifyDecl(field);
7931#endif
7932 }
7933 } else {
7934 clang::ObjCInterfaceDecl *class_interface_decl =
7935 ast->GetAsObjCInterfaceDecl(type);
7936
7937 if (class_interface_decl) {
7938 const bool is_synthesized = false;
7939
7940 field_clang_type.GetCompleteType();
7941
7942 field = clang::ObjCIvarDecl::Create(
7943 *clang_ast, class_interface_decl, clang::SourceLocation(),
7944 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007945 ident, // Identifier
7946 ClangUtil::GetQualType(field_clang_type), // Field type
7947 nullptr, // TypeSourceInfo *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007948 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7949 is_synthesized);
7950
7951 if (field) {
7952 class_interface_decl->addDecl(field);
7953
7954#ifdef LLDB_CONFIGURATION_DEBUG
7955 VerifyDecl(field);
7956#endif
7957 }
7958 }
7959 }
7960 return field;
Greg Claytond8d4a572015-08-11 21:38:15 +00007961}
7962
Kate Stoneb9c1b512016-09-06 20:57:50 +00007963void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7964 if (!type)
7965 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007966
Kate Stoneb9c1b512016-09-06 20:57:50 +00007967 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7968 if (!ast)
7969 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007970
Kate Stoneb9c1b512016-09-06 20:57:50 +00007971 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007972
Kate Stoneb9c1b512016-09-06 20:57:50 +00007973 if (!record_decl)
7974 return;
7975
7976 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7977
7978 IndirectFieldVector indirect_fields;
7979 clang::RecordDecl::field_iterator field_pos;
7980 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7981 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7982 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7983 last_field_pos = field_pos++) {
7984 if (field_pos->isAnonymousStructOrUnion()) {
7985 clang::QualType field_qual_type = field_pos->getType();
7986
7987 const clang::RecordType *field_record_type =
7988 field_qual_type->getAs<clang::RecordType>();
7989
7990 if (!field_record_type)
7991 continue;
7992
7993 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7994
7995 if (!field_record_decl)
7996 continue;
7997
7998 for (clang::RecordDecl::decl_iterator
7999 di = field_record_decl->decls_begin(),
8000 de = field_record_decl->decls_end();
8001 di != de; ++di) {
8002 if (clang::FieldDecl *nested_field_decl =
8003 llvm::dyn_cast<clang::FieldDecl>(*di)) {
8004 clang::NamedDecl **chain =
8005 new (*ast->getASTContext()) clang::NamedDecl *[2];
8006 chain[0] = *field_pos;
8007 chain[1] = nested_field_decl;
8008 clang::IndirectFieldDecl *indirect_field =
8009 clang::IndirectFieldDecl::Create(
8010 *ast->getASTContext(), record_decl, clang::SourceLocation(),
8011 nested_field_decl->getIdentifier(),
8012 nested_field_decl->getType(), {chain, 2});
8013
8014 indirect_field->setImplicit();
8015
8016 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
8017 field_pos->getAccess(), nested_field_decl->getAccess()));
8018
8019 indirect_fields.push_back(indirect_field);
8020 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
8021 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
8022 size_t nested_chain_size =
8023 nested_indirect_field_decl->getChainingSize();
8024 clang::NamedDecl **chain = new (*ast->getASTContext())
8025 clang::NamedDecl *[nested_chain_size + 1];
8026 chain[0] = *field_pos;
8027
8028 int chain_index = 1;
8029 for (clang::IndirectFieldDecl::chain_iterator
8030 nci = nested_indirect_field_decl->chain_begin(),
8031 nce = nested_indirect_field_decl->chain_end();
8032 nci < nce; ++nci) {
8033 chain[chain_index] = *nci;
8034 chain_index++;
8035 }
8036
8037 clang::IndirectFieldDecl *indirect_field =
8038 clang::IndirectFieldDecl::Create(
8039 *ast->getASTContext(), record_decl, clang::SourceLocation(),
8040 nested_indirect_field_decl->getIdentifier(),
8041 nested_indirect_field_decl->getType(),
8042 {chain, nested_chain_size + 1});
8043
8044 indirect_field->setImplicit();
8045
8046 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
8047 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
8048
8049 indirect_fields.push_back(indirect_field);
Greg Claytond8d4a572015-08-11 21:38:15 +00008050 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008051 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008052 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008053 }
8054
Adrian Prantl05097242018-04-30 16:49:04 +00008055 // Check the last field to see if it has an incomplete array type as its last
8056 // member and if it does, the tell the record decl about it
Kate Stoneb9c1b512016-09-06 20:57:50 +00008057 if (last_field_pos != field_end_pos) {
8058 if (last_field_pos->getType()->isIncompleteArrayType())
8059 record_decl->hasFlexibleArrayMember();
8060 }
8061
8062 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
8063 ife = indirect_fields.end();
8064 ifi < ife; ++ifi) {
8065 record_decl->addDecl(*ifi);
8066 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008067}
8068
Kate Stoneb9c1b512016-09-06 20:57:50 +00008069void ClangASTContext::SetIsPacked(const CompilerType &type) {
8070 if (type) {
8071 ClangASTContext *ast =
8072 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8073 if (ast) {
8074 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
8075
8076 if (!record_decl)
Greg Claytonf73034f2015-09-08 18:15:05 +00008077 return;
8078
Kate Stoneb9c1b512016-09-06 20:57:50 +00008079 record_decl->addAttr(
8080 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
Greg Claytond8d4a572015-08-11 21:38:15 +00008081 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008082 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008083}
8084
Kate Stoneb9c1b512016-09-06 20:57:50 +00008085clang::VarDecl *ClangASTContext::AddVariableToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008086 const CompilerType &type, llvm::StringRef name,
8087 const CompilerType &var_type, AccessType access) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00008088 if (!type.IsValid() || !var_type.IsValid())
8089 return nullptr;
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008090
Kate Stoneb9c1b512016-09-06 20:57:50 +00008091 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8092 if (!ast)
8093 return nullptr;
8094
8095 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008096 if (!record_decl)
8097 return nullptr;
8098
8099 clang::VarDecl *var_decl = nullptr;
8100 clang::IdentifierInfo *ident = nullptr;
8101 if (!name.empty())
8102 ident = &ast->getASTContext()->Idents.get(name);
8103
8104 var_decl = clang::VarDecl::Create(
8105 *ast->getASTContext(), // ASTContext &
8106 record_decl, // DeclContext *
8107 clang::SourceLocation(), // clang::SourceLocation StartLoc
8108 clang::SourceLocation(), // clang::SourceLocation IdLoc
8109 ident, // clang::IdentifierInfo *
8110 ClangUtil::GetQualType(var_type), // Variable clang::QualType
8111 nullptr, // TypeSourceInfo *
8112 clang::SC_Static); // StorageClass
8113 if (!var_decl)
8114 return nullptr;
8115
8116 var_decl->setAccess(
8117 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
8118 record_decl->addDecl(var_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008119
Greg Claytond8d4a572015-08-11 21:38:15 +00008120#ifdef LLDB_CONFIGURATION_DEBUG
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008121 VerifyDecl(var_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008122#endif
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008123
Kate Stoneb9c1b512016-09-06 20:57:50 +00008124 return var_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008125}
8126
Kate Stoneb9c1b512016-09-06 20:57:50 +00008127clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
Davide Italiano675767a2018-03-27 19:40:50 +00008128 lldb::opaque_compiler_type_t type, const char *name, const char *mangled_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008129 const CompilerType &method_clang_type, lldb::AccessType access,
8130 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
8131 bool is_attr_used, bool is_artificial) {
8132 if (!type || !method_clang_type.IsValid() || name == nullptr ||
8133 name[0] == '\0')
8134 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008135
Kate Stoneb9c1b512016-09-06 20:57:50 +00008136 clang::QualType record_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008137
Kate Stoneb9c1b512016-09-06 20:57:50 +00008138 clang::CXXRecordDecl *cxx_record_decl =
8139 record_qual_type->getAsCXXRecordDecl();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008140
Kate Stoneb9c1b512016-09-06 20:57:50 +00008141 if (cxx_record_decl == nullptr)
8142 return nullptr;
8143
8144 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8145
8146 clang::CXXMethodDecl *cxx_method_decl = nullptr;
8147
8148 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
8149
8150 const clang::FunctionType *function_type =
8151 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
8152
8153 if (function_type == nullptr)
8154 return nullptr;
8155
8156 const clang::FunctionProtoType *method_function_prototype(
8157 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
8158
8159 if (!method_function_prototype)
8160 return nullptr;
8161
8162 unsigned int num_params = method_function_prototype->getNumParams();
8163
8164 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
8165 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
8166
8167 if (is_artificial)
8168 return nullptr; // skip everything artificial
8169
8170 if (name[0] == '~') {
8171 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
8172 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8173 clang::DeclarationNameInfo(
8174 getASTContext()->DeclarationNames.getCXXDestructorName(
8175 getASTContext()->getCanonicalType(record_qual_type)),
8176 clang::SourceLocation()),
8177 method_qual_type, nullptr, is_inline, is_artificial);
8178 cxx_method_decl = cxx_dtor_decl;
8179 } else if (decl_name == cxx_record_decl->getDeclName()) {
8180 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
8181 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8182 clang::DeclarationNameInfo(
8183 getASTContext()->DeclarationNames.getCXXConstructorName(
8184 getASTContext()->getCanonicalType(record_qual_type)),
8185 clang::SourceLocation()),
8186 method_qual_type,
8187 nullptr, // TypeSourceInfo *
8188 is_explicit, is_inline, is_artificial, false /*is_constexpr*/);
8189 cxx_method_decl = cxx_ctor_decl;
8190 } else {
8191 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
8192 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
8193
8194 if (IsOperator(name, op_kind)) {
8195 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
Adrian Prantl05097242018-04-30 16:49:04 +00008196 // Check the number of operator parameters. Sometimes we have seen bad
8197 // DWARF that doesn't correctly describe operators and if we try to
8198 // create a method and add it to the class, clang will assert and
8199 // crash, so we need to make sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00008200 const bool is_method = true;
8201 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
8202 is_method, op_kind, num_params))
8203 return nullptr;
8204 cxx_method_decl = clang::CXXMethodDecl::Create(
8205 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8206 clang::DeclarationNameInfo(
8207 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
8208 clang::SourceLocation()),
8209 method_qual_type,
8210 nullptr, // TypeSourceInfo *
8211 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
8212 } else if (num_params == 0) {
8213 // Conversion operators don't take params...
8214 cxx_method_decl = clang::CXXConversionDecl::Create(
8215 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8216 clang::DeclarationNameInfo(
8217 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
8218 getASTContext()->getCanonicalType(
8219 function_type->getReturnType())),
8220 clang::SourceLocation()),
8221 method_qual_type,
8222 nullptr, // TypeSourceInfo *
8223 is_inline, is_explicit, false /*is_constexpr*/,
8224 clang::SourceLocation());
8225 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008226 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008227
8228 if (cxx_method_decl == nullptr) {
8229 cxx_method_decl = clang::CXXMethodDecl::Create(
8230 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8231 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8232 method_qual_type,
8233 nullptr, // TypeSourceInfo *
8234 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
Greg Claytond8d4a572015-08-11 21:38:15 +00008235 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008236 }
8237
8238 clang::AccessSpecifier access_specifier =
8239 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8240
8241 cxx_method_decl->setAccess(access_specifier);
8242 cxx_method_decl->setVirtualAsWritten(is_virtual);
8243
8244 if (is_attr_used)
8245 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8246
Davide Italiano675767a2018-03-27 19:40:50 +00008247 if (mangled_name != NULL) {
8248 cxx_method_decl->addAttr(
8249 clang::AsmLabelAttr::CreateImplicit(*getASTContext(), mangled_name));
8250 }
8251
Kate Stoneb9c1b512016-09-06 20:57:50 +00008252 // Populate the method decl with parameter decls
8253
8254 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8255
8256 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8257 params.push_back(clang::ParmVarDecl::Create(
8258 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8259 clang::SourceLocation(),
8260 nullptr, // anonymous
8261 method_function_prototype->getParamType(param_index), nullptr,
8262 clang::SC_None, nullptr));
8263 }
8264
8265 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8266
8267 cxx_record_decl->addDecl(cxx_method_decl);
8268
8269 // Sometimes the debug info will mention a constructor (default/copy/move),
8270 // destructor, or assignment operator (copy/move) but there won't be any
8271 // version of this in the code. So we check if the function was artificially
8272 // generated and if it is trivial and this lets the compiler/backend know
8273 // that it can inline the IR for these when it needs to and we can avoid a
8274 // "missing function" error when running expressions.
8275
8276 if (is_artificial) {
8277 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8278 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8279 (cxx_ctor_decl->isCopyConstructor() &&
8280 cxx_record_decl->hasTrivialCopyConstructor()) ||
8281 (cxx_ctor_decl->isMoveConstructor() &&
8282 cxx_record_decl->hasTrivialMoveConstructor()))) {
8283 cxx_ctor_decl->setDefaulted();
8284 cxx_ctor_decl->setTrivial(true);
8285 } else if (cxx_dtor_decl) {
8286 if (cxx_record_decl->hasTrivialDestructor()) {
8287 cxx_dtor_decl->setDefaulted();
8288 cxx_dtor_decl->setTrivial(true);
8289 }
8290 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8291 cxx_record_decl->hasTrivialCopyAssignment()) ||
8292 (cxx_method_decl->isMoveAssignmentOperator() &&
8293 cxx_record_decl->hasTrivialMoveAssignment())) {
8294 cxx_method_decl->setDefaulted();
8295 cxx_method_decl->setTrivial(true);
Greg Claytond8d4a572015-08-11 21:38:15 +00008296 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008297 }
8298
Greg Claytond8d4a572015-08-11 21:38:15 +00008299#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008300 VerifyDecl(cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008301#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008302
Kate Stoneb9c1b512016-09-06 20:57:50 +00008303 // printf ("decl->isPolymorphic() = %i\n",
8304 // cxx_record_decl->isPolymorphic());
8305 // printf ("decl->isAggregate() = %i\n",
8306 // cxx_record_decl->isAggregate());
8307 // printf ("decl->isPOD() = %i\n",
8308 // cxx_record_decl->isPOD());
8309 // printf ("decl->isEmpty() = %i\n",
8310 // cxx_record_decl->isEmpty());
8311 // printf ("decl->isAbstract() = %i\n",
8312 // cxx_record_decl->isAbstract());
8313 // printf ("decl->hasTrivialConstructor() = %i\n",
8314 // cxx_record_decl->hasTrivialConstructor());
8315 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8316 // cxx_record_decl->hasTrivialCopyConstructor());
8317 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8318 // cxx_record_decl->hasTrivialCopyAssignment());
8319 // printf ("decl->hasTrivialDestructor() = %i\n",
8320 // cxx_record_decl->hasTrivialDestructor());
8321 return cxx_method_decl;
8322}
Greg Claytond8d4a572015-08-11 21:38:15 +00008323
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00008324void ClangASTContext::AddMethodOverridesForCXXRecordType(
8325 lldb::opaque_compiler_type_t type) {
8326 if (auto *record = GetAsCXXRecordDecl(type))
8327 for (auto *method : record->methods())
8328 addOverridesForMethod(method);
8329}
8330
Greg Claytond8d4a572015-08-11 21:38:15 +00008331#pragma mark C++ Base Classes
8332
Zachary Turner970f38e2018-10-25 20:44:56 +00008333std::unique_ptr<clang::CXXBaseSpecifier>
Kate Stoneb9c1b512016-09-06 20:57:50 +00008334ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8335 AccessType access, bool is_virtual,
8336 bool base_of_class) {
Zachary Turner970f38e2018-10-25 20:44:56 +00008337 if (!type)
8338 return nullptr;
8339
8340 return llvm::make_unique<clang::CXXBaseSpecifier>(
8341 clang::SourceRange(), is_virtual, base_of_class,
8342 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8343 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8344 clang::SourceLocation());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008345}
8346
Zachary Turner970f38e2018-10-25 20:44:56 +00008347bool ClangASTContext::TransferBaseClasses(
Kate Stoneb9c1b512016-09-06 20:57:50 +00008348 lldb::opaque_compiler_type_t type,
Zachary Turner970f38e2018-10-25 20:44:56 +00008349 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases) {
8350 if (!type)
8351 return false;
8352 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8353 if (!cxx_record_decl)
8354 return false;
8355 std::vector<clang::CXXBaseSpecifier *> raw_bases;
8356 raw_bases.reserve(bases.size());
8357
8358 // Clang will make a copy of them, so it's ok that we pass pointers that we're
8359 // about to destroy.
8360 for (auto &b : bases)
8361 raw_bases.push_back(b.get());
8362 cxx_record_decl->setBases(raw_bases.data(), raw_bases.size());
8363 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008364}
8365
8366bool ClangASTContext::SetObjCSuperClass(
8367 const CompilerType &type, const CompilerType &superclass_clang_type) {
8368 ClangASTContext *ast =
8369 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8370 if (!ast)
8371 return false;
8372 clang::ASTContext *clang_ast = ast->getASTContext();
8373
8374 if (type && superclass_clang_type.IsValid() &&
8375 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8376 clang::ObjCInterfaceDecl *class_interface_decl =
8377 GetAsObjCInterfaceDecl(type);
8378 clang::ObjCInterfaceDecl *super_interface_decl =
8379 GetAsObjCInterfaceDecl(superclass_clang_type);
8380 if (class_interface_decl && super_interface_decl) {
8381 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8382 clang_ast->getObjCInterfaceType(super_interface_decl)));
8383 return true;
8384 }
8385 }
8386 return false;
8387}
8388
8389bool ClangASTContext::AddObjCClassProperty(
8390 const CompilerType &type, const char *property_name,
8391 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8392 const char *property_setter_name, const char *property_getter_name,
8393 uint32_t property_attributes, ClangASTMetadata *metadata) {
8394 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8395 property_name[0] == '\0')
8396 return false;
8397 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8398 if (!ast)
8399 return false;
8400 clang::ASTContext *clang_ast = ast->getASTContext();
8401
8402 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8403
8404 if (class_interface_decl) {
8405 CompilerType property_clang_type_to_access;
8406
8407 if (property_clang_type.IsValid())
8408 property_clang_type_to_access = property_clang_type;
8409 else if (ivar_decl)
8410 property_clang_type_to_access =
8411 CompilerType(clang_ast, ivar_decl->getType());
8412
8413 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8414 clang::TypeSourceInfo *prop_type_source;
8415 if (ivar_decl)
8416 prop_type_source =
8417 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8418 else
8419 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8420 ClangUtil::GetQualType(property_clang_type));
8421
8422 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8423 *clang_ast, class_interface_decl,
8424 clang::SourceLocation(), // Source Location
8425 &clang_ast->Idents.get(property_name),
8426 clang::SourceLocation(), // Source Location for AT
8427 clang::SourceLocation(), // Source location for (
8428 ivar_decl ? ivar_decl->getType()
8429 : ClangUtil::GetQualType(property_clang_type),
8430 prop_type_source);
8431
8432 if (property_decl) {
8433 if (metadata)
8434 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8435
8436 class_interface_decl->addDecl(property_decl);
8437
8438 clang::Selector setter_sel, getter_sel;
8439
8440 if (property_setter_name != nullptr) {
8441 std::string property_setter_no_colon(
8442 property_setter_name, strlen(property_setter_name) - 1);
8443 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008444 &clang_ast->Idents.get(property_setter_no_colon);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008445 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8446 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8447 std::string setter_sel_string("set");
8448 setter_sel_string.push_back(::toupper(property_name[0]));
8449 setter_sel_string.append(&property_name[1]);
8450 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008451 &clang_ast->Idents.get(setter_sel_string);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008452 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8453 }
8454 property_decl->setSetterName(setter_sel);
8455 property_decl->setPropertyAttributes(
8456 clang::ObjCPropertyDecl::OBJC_PR_setter);
8457
8458 if (property_getter_name != nullptr) {
8459 clang::IdentifierInfo *getter_ident =
8460 &clang_ast->Idents.get(property_getter_name);
8461 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8462 } else {
8463 clang::IdentifierInfo *getter_ident =
8464 &clang_ast->Idents.get(property_name);
8465 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8466 }
8467 property_decl->setGetterName(getter_sel);
8468 property_decl->setPropertyAttributes(
8469 clang::ObjCPropertyDecl::OBJC_PR_getter);
8470
8471 if (ivar_decl)
8472 property_decl->setPropertyIvarDecl(ivar_decl);
8473
8474 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8475 property_decl->setPropertyAttributes(
8476 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8477 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8478 property_decl->setPropertyAttributes(
8479 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8480 if (property_attributes & DW_APPLE_PROPERTY_assign)
8481 property_decl->setPropertyAttributes(
8482 clang::ObjCPropertyDecl::OBJC_PR_assign);
8483 if (property_attributes & DW_APPLE_PROPERTY_retain)
8484 property_decl->setPropertyAttributes(
8485 clang::ObjCPropertyDecl::OBJC_PR_retain);
8486 if (property_attributes & DW_APPLE_PROPERTY_copy)
8487 property_decl->setPropertyAttributes(
8488 clang::ObjCPropertyDecl::OBJC_PR_copy);
8489 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8490 property_decl->setPropertyAttributes(
8491 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8492 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8493 property_decl->setPropertyAttributes(
8494 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8495 if (property_attributes &
8496 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8497 property_decl->setPropertyAttributes(
8498 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8499 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8500 property_decl->setPropertyAttributes(
8501 clang::ObjCPropertyDecl::OBJC_PR_class);
8502
8503 const bool isInstance =
8504 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8505
8506 if (!getter_sel.isNull() &&
8507 !(isInstance
8508 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8509 : class_interface_decl->lookupClassMethod(getter_sel))) {
8510 const bool isVariadic = false;
8511 const bool isSynthesized = false;
8512 const bool isImplicitlyDeclared = true;
8513 const bool isDefined = false;
8514 const clang::ObjCMethodDecl::ImplementationControl impControl =
8515 clang::ObjCMethodDecl::None;
8516 const bool HasRelatedResultType = false;
8517
8518 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8519 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8520 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8521 nullptr, class_interface_decl, isInstance, isVariadic,
8522 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8523 HasRelatedResultType);
8524
8525 if (getter && metadata)
8526 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8527
8528 if (getter) {
8529 getter->setMethodParams(*clang_ast,
8530 llvm::ArrayRef<clang::ParmVarDecl *>(),
8531 llvm::ArrayRef<clang::SourceLocation>());
8532
8533 class_interface_decl->addDecl(getter);
8534 }
8535 }
8536
8537 if (!setter_sel.isNull() &&
8538 !(isInstance
8539 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8540 : class_interface_decl->lookupClassMethod(setter_sel))) {
8541 clang::QualType result_type = clang_ast->VoidTy;
8542 const bool isVariadic = false;
8543 const bool isSynthesized = false;
8544 const bool isImplicitlyDeclared = true;
8545 const bool isDefined = false;
8546 const clang::ObjCMethodDecl::ImplementationControl impControl =
8547 clang::ObjCMethodDecl::None;
8548 const bool HasRelatedResultType = false;
8549
8550 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8551 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8552 setter_sel, result_type, nullptr, class_interface_decl,
8553 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8554 isDefined, impControl, HasRelatedResultType);
8555
8556 if (setter && metadata)
8557 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8558
8559 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8560
8561 params.push_back(clang::ParmVarDecl::Create(
8562 *clang_ast, setter, clang::SourceLocation(),
8563 clang::SourceLocation(),
8564 nullptr, // anonymous
8565 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8566 clang::SC_Auto, nullptr));
8567
8568 if (setter) {
8569 setter->setMethodParams(
8570 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8571 llvm::ArrayRef<clang::SourceLocation>());
8572
8573 class_interface_decl->addDecl(setter);
8574 }
8575 }
8576
8577 return true;
8578 }
8579 }
8580 }
8581 return false;
8582}
8583
8584bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8585 bool check_superclass) {
8586 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8587 if (class_interface_decl)
8588 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8589 return false;
8590}
8591
8592clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8593 const CompilerType &type,
8594 const char *name, // the full symbol name as seen in the symbol table
8595 // (lldb::opaque_compiler_type_t type, "-[NString
8596 // stringWithCString:]")
8597 const CompilerType &method_clang_type, lldb::AccessType access,
8598 bool is_artificial, bool is_variadic) {
8599 if (!type || !method_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008600 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008601
Kate Stoneb9c1b512016-09-06 20:57:50 +00008602 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8603
8604 if (class_interface_decl == nullptr)
8605 return nullptr;
8606 ClangASTContext *lldb_ast =
8607 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8608 if (lldb_ast == nullptr)
8609 return nullptr;
8610 clang::ASTContext *ast = lldb_ast->getASTContext();
8611
8612 const char *selector_start = ::strchr(name, ' ');
8613 if (selector_start == nullptr)
8614 return nullptr;
8615
8616 selector_start++;
8617 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8618
8619 size_t len = 0;
8620 const char *start;
8621 // printf ("name = '%s'\n", name);
8622
8623 unsigned num_selectors_with_args = 0;
8624 for (start = selector_start; start && *start != '\0' && *start != ']';
8625 start += len) {
8626 len = ::strcspn(start, ":]");
8627 bool has_arg = (start[len] == ':');
8628 if (has_arg)
8629 ++num_selectors_with_args;
8630 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8631 if (has_arg)
8632 len += 1;
8633 }
8634
8635 if (selector_idents.size() == 0)
8636 return nullptr;
8637
8638 clang::Selector method_selector = ast->Selectors.getSelector(
8639 num_selectors_with_args ? selector_idents.size() : 0,
8640 selector_idents.data());
8641
8642 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8643
8644 // Populate the method decl with parameter decls
8645 const clang::Type *method_type(method_qual_type.getTypePtr());
8646
8647 if (method_type == nullptr)
8648 return nullptr;
8649
8650 const clang::FunctionProtoType *method_function_prototype(
8651 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8652
8653 if (!method_function_prototype)
8654 return nullptr;
8655
8656 bool is_synthesized = false;
8657 bool is_defined = false;
8658 clang::ObjCMethodDecl::ImplementationControl imp_control =
8659 clang::ObjCMethodDecl::None;
8660
8661 const unsigned num_args = method_function_prototype->getNumParams();
8662
8663 if (num_args != num_selectors_with_args)
8664 return nullptr; // some debug information is corrupt. We are not going to
8665 // deal with it.
8666
8667 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8668 *ast,
8669 clang::SourceLocation(), // beginLoc,
8670 clang::SourceLocation(), // endLoc,
8671 method_selector, method_function_prototype->getReturnType(),
8672 nullptr, // TypeSourceInfo *ResultTInfo,
8673 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8674 ClangUtil::GetQualType(type)),
8675 name[0] == '-', is_variadic, is_synthesized,
8676 true, // is_implicitly_declared; we force this to true because we don't
8677 // have source locations
8678 is_defined, imp_control, false /*has_related_result_type*/);
8679
8680 if (objc_method_decl == nullptr)
8681 return nullptr;
8682
8683 if (num_args > 0) {
8684 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8685
8686 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8687 params.push_back(clang::ParmVarDecl::Create(
8688 *ast, objc_method_decl, clang::SourceLocation(),
8689 clang::SourceLocation(),
8690 nullptr, // anonymous
8691 method_function_prototype->getParamType(param_index), nullptr,
8692 clang::SC_Auto, nullptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00008693 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008694
Kate Stoneb9c1b512016-09-06 20:57:50 +00008695 objc_method_decl->setMethodParams(
8696 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8697 llvm::ArrayRef<clang::SourceLocation>());
8698 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008699
Kate Stoneb9c1b512016-09-06 20:57:50 +00008700 class_interface_decl->addDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008701
Greg Claytond8d4a572015-08-11 21:38:15 +00008702#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008703 VerifyDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008704#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00008705
8706 return objc_method_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008707}
8708
Kate Stoneb9c1b512016-09-06 20:57:50 +00008709bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8710 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00008711 return false;
Greg Claytone6b36cd2015-12-08 01:02:08 +00008712
Kate Stoneb9c1b512016-09-06 20:57:50 +00008713 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008714
Kate Stoneb9c1b512016-09-06 20:57:50 +00008715 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8716 switch (type_class) {
8717 case clang::Type::Record: {
8718 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8719 if (cxx_record_decl)
8720 return cxx_record_decl->hasExternalLexicalStorage() ||
8721 cxx_record_decl->hasExternalVisibleStorage();
8722 } break;
Enrico Granata36f51e42015-12-18 22:41:25 +00008723
Kate Stoneb9c1b512016-09-06 20:57:50 +00008724 case clang::Type::Enum: {
8725 clang::EnumDecl *enum_decl =
8726 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8727 if (enum_decl)
8728 return enum_decl->hasExternalLexicalStorage() ||
8729 enum_decl->hasExternalVisibleStorage();
8730 } break;
8731
8732 case clang::Type::ObjCObject:
8733 case clang::Type::ObjCInterface: {
8734 const clang::ObjCObjectType *objc_class_type =
8735 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8736 assert(objc_class_type);
8737 if (objc_class_type) {
8738 clang::ObjCInterfaceDecl *class_interface_decl =
8739 objc_class_type->getInterface();
8740
8741 if (class_interface_decl)
8742 return class_interface_decl->hasExternalLexicalStorage() ||
8743 class_interface_decl->hasExternalVisibleStorage();
Greg Claytond8d4a572015-08-11 21:38:15 +00008744 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008745 } break;
8746
8747 case clang::Type::Typedef:
8748 return GetHasExternalStorage(CompilerType(
8749 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8750 ->getDecl()
8751 ->getUnderlyingType()
8752 .getAsOpaquePtr()));
8753
8754 case clang::Type::Auto:
8755 return GetHasExternalStorage(CompilerType(
8756 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8757 ->getDeducedType()
8758 .getAsOpaquePtr()));
8759
8760 case clang::Type::Elaborated:
8761 return GetHasExternalStorage(CompilerType(
8762 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8763 ->getNamedType()
8764 .getAsOpaquePtr()));
8765
8766 case clang::Type::Paren:
8767 return GetHasExternalStorage(CompilerType(
8768 type.GetTypeSystem(),
8769 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8770
8771 default:
8772 break;
8773 }
8774 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008775}
8776
Kate Stoneb9c1b512016-09-06 20:57:50 +00008777bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8778 bool has_extern) {
8779 if (!type)
8780 return false;
8781
8782 clang::QualType qual_type(GetCanonicalQualType(type));
8783
8784 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8785 switch (type_class) {
8786 case clang::Type::Record: {
8787 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8788 if (cxx_record_decl) {
8789 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8790 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8791 return true;
8792 }
8793 } break;
8794
8795 case clang::Type::Enum: {
8796 clang::EnumDecl *enum_decl =
8797 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8798 if (enum_decl) {
8799 enum_decl->setHasExternalLexicalStorage(has_extern);
8800 enum_decl->setHasExternalVisibleStorage(has_extern);
8801 return true;
8802 }
8803 } break;
8804
8805 case clang::Type::ObjCObject:
8806 case clang::Type::ObjCInterface: {
8807 const clang::ObjCObjectType *objc_class_type =
8808 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8809 assert(objc_class_type);
8810 if (objc_class_type) {
8811 clang::ObjCInterfaceDecl *class_interface_decl =
8812 objc_class_type->getInterface();
8813
8814 if (class_interface_decl) {
8815 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8816 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8817 return true;
8818 }
8819 }
8820 } break;
8821
8822 case clang::Type::Typedef:
8823 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8824 ->getDecl()
8825 ->getUnderlyingType()
8826 .getAsOpaquePtr(),
8827 has_extern);
8828
8829 case clang::Type::Auto:
8830 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8831 ->getDeducedType()
8832 .getAsOpaquePtr(),
8833 has_extern);
8834
8835 case clang::Type::Elaborated:
8836 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8837 ->getNamedType()
8838 .getAsOpaquePtr(),
8839 has_extern);
8840
8841 case clang::Type::Paren:
8842 return SetHasExternalStorage(
8843 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8844 has_extern);
8845
8846 default:
8847 break;
8848 }
8849 return false;
8850}
Greg Claytond8d4a572015-08-11 21:38:15 +00008851
8852#pragma mark TagDecl
8853
Kate Stoneb9c1b512016-09-06 20:57:50 +00008854bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8855 clang::QualType qual_type(ClangUtil::GetQualType(type));
8856 if (!qual_type.isNull()) {
8857 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8858 if (tag_type) {
8859 clang::TagDecl *tag_decl = tag_type->getDecl();
8860 if (tag_decl) {
8861 tag_decl->startDefinition();
8862 return true;
8863 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008864 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008865
8866 const clang::ObjCObjectType *object_type =
8867 qual_type->getAs<clang::ObjCObjectType>();
8868 if (object_type) {
8869 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8870 if (interface_decl) {
8871 interface_decl->startDefinition();
8872 return true;
8873 }
8874 }
8875 }
8876 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008877}
8878
Kate Stoneb9c1b512016-09-06 20:57:50 +00008879bool ClangASTContext::CompleteTagDeclarationDefinition(
8880 const CompilerType &type) {
8881 clang::QualType qual_type(ClangUtil::GetQualType(type));
8882 if (!qual_type.isNull()) {
8883 // Make sure we use the same methodology as
Adrian Prantl05097242018-04-30 16:49:04 +00008884 // ClangASTContext::StartTagDeclarationDefinition() as to how we start/end
8885 // the definition. Previously we were calling
Kate Stoneb9c1b512016-09-06 20:57:50 +00008886 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8887 if (tag_type) {
8888 clang::TagDecl *tag_decl = tag_type->getDecl();
8889 if (tag_decl) {
8890 clang::CXXRecordDecl *cxx_record_decl =
8891 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8892
8893 if (cxx_record_decl) {
8894 if (!cxx_record_decl->isCompleteDefinition())
8895 cxx_record_decl->completeDefinition();
8896 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8897 cxx_record_decl->setHasExternalLexicalStorage(false);
8898 cxx_record_decl->setHasExternalVisibleStorage(false);
8899 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008900 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008901 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008902 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008903
8904 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8905
8906 if (enutype) {
8907 clang::EnumDecl *enum_decl = enutype->getDecl();
8908
8909 if (enum_decl) {
8910 if (!enum_decl->isCompleteDefinition()) {
8911 ClangASTContext *lldb_ast =
8912 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8913 if (lldb_ast == nullptr)
8914 return false;
8915 clang::ASTContext *ast = lldb_ast->getASTContext();
8916
8917 /// TODO This really needs to be fixed.
8918
8919 QualType integer_type(enum_decl->getIntegerType());
8920 if (!integer_type.isNull()) {
8921 unsigned NumPositiveBits = 1;
8922 unsigned NumNegativeBits = 0;
8923
8924 clang::QualType promotion_qual_type;
8925 // If the enum integer type is less than an integer in bit width,
8926 // then we must promote it to an integer size.
8927 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8928 ast->getTypeSize(ast->IntTy)) {
8929 if (enum_decl->getIntegerType()->isSignedIntegerType())
8930 promotion_qual_type = ast->IntTy;
8931 else
8932 promotion_qual_type = ast->UnsignedIntTy;
8933 } else
8934 promotion_qual_type = enum_decl->getIntegerType();
8935
8936 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8937 promotion_qual_type, NumPositiveBits,
8938 NumNegativeBits);
8939 }
8940 }
8941 return true;
8942 }
8943 }
8944 }
8945 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008946}
8947
Aleksandr Urakov709426b2018-09-10 08:08:43 +00008948clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008949 const CompilerType &enum_type, const Declaration &decl, const char *name,
Zachary Turner1639c6b2018-12-17 16:15:13 +00008950 const llvm::APSInt &value) {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008951
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008952 if (!enum_type || ConstString(name).IsEmpty())
8953 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008954
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008955 lldbassert(enum_type.GetTypeSystem() == static_cast<TypeSystem *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +00008956
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008957 lldb::opaque_compiler_type_t enum_opaque_compiler_type =
8958 enum_type.GetOpaqueQualType();
8959
8960 if (!enum_opaque_compiler_type)
8961 return nullptr;
8962
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008963 clang::QualType enum_qual_type(
8964 GetCanonicalQualType(enum_opaque_compiler_type));
8965
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008966 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8967
8968 if (!clang_type)
8969 return nullptr;
8970
8971 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8972
8973 if (!enutype)
8974 return nullptr;
8975
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008976 clang::EnumConstantDecl *enumerator_decl = clang::EnumConstantDecl::Create(
8977 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8978 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier
Zachary Turner1639c6b2018-12-17 16:15:13 +00008979 clang::QualType(enutype, 0), nullptr, value);
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008980
8981 if (!enumerator_decl)
8982 return nullptr;
8983
8984 enutype->getDecl()->addDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008985
8986#ifdef LLDB_CONFIGURATION_DEBUG
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008987 VerifyDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008988#endif
8989
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008990 return enumerator_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008991}
8992
Zachary Turner1639c6b2018-12-17 16:15:13 +00008993clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
8994 const CompilerType &enum_type, const Declaration &decl, const char *name,
8995 int64_t enum_value, uint32_t enum_value_bit_size) {
8996 CompilerType underlying_type =
8997 GetEnumerationIntegerType(enum_type.GetOpaqueQualType());
8998 bool is_signed = false;
8999 underlying_type.IsIntegerType(is_signed);
9000
9001 llvm::APSInt value(enum_value_bit_size, is_signed);
9002 value = enum_value;
9003
9004 return AddEnumerationValueToEnumerationType(enum_type, decl, name, value);
9005}
9006
Greg Claytona1e5dc82015-08-11 22:53:00 +00009007CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00009008ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
9009 clang::QualType enum_qual_type(GetCanonicalQualType(type));
9010 const clang::Type *clang_type = enum_qual_type.getTypePtr();
9011 if (clang_type) {
9012 const clang::EnumType *enutype =
9013 llvm::dyn_cast<clang::EnumType>(clang_type);
9014 if (enutype) {
9015 clang::EnumDecl *enum_decl = enutype->getDecl();
9016 if (enum_decl)
9017 return CompilerType(getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00009018 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009019 }
9020 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00009021}
9022
Kate Stoneb9c1b512016-09-06 20:57:50 +00009023CompilerType
9024ClangASTContext::CreateMemberPointerType(const CompilerType &type,
9025 const CompilerType &pointee_type) {
9026 if (type && pointee_type.IsValid() &&
9027 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
9028 ClangASTContext *ast =
9029 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
9030 if (!ast)
9031 return CompilerType();
9032 return CompilerType(ast->getASTContext(),
9033 ast->getASTContext()->getMemberPointerType(
9034 ClangUtil::GetQualType(pointee_type),
9035 ClangUtil::GetQualType(type).getTypePtr()));
9036 }
9037 return CompilerType();
9038}
Greg Claytond8d4a572015-08-11 21:38:15 +00009039
9040size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00009041ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
9042 const char *s, uint8_t *dst,
9043 size_t dst_size) {
9044 if (type) {
9045 clang::QualType qual_type(GetCanonicalQualType(type));
9046 uint32_t count = 0;
9047 bool is_complex = false;
9048 if (IsFloatingPointType(type, count, is_complex)) {
9049 // TODO: handle complex and vector types
9050 if (count != 1)
9051 return false;
9052
9053 llvm::StringRef s_sref(s);
9054 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type),
9055 s_sref);
9056
9057 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type);
9058 const uint64_t byte_size = bit_size / 8;
9059 if (dst_size >= byte_size) {
9060 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(
9061 llvm::NextPowerOf2(byte_size) * 8);
Zachary Turner97206d52017-05-12 04:51:55 +00009062 lldb_private::Status get_data_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009063 if (scalar.GetAsMemoryData(dst, byte_size,
9064 lldb_private::endian::InlHostByteOrder(),
9065 get_data_error))
9066 return byte_size;
9067 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009068 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009069 }
9070 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00009071}
9072
Greg Claytond8d4a572015-08-11 21:38:15 +00009073// Dumping types
Greg Claytond8d4a572015-08-11 21:38:15 +00009074#define DEPTH_INCREMENT 2
9075
Adrian Prantl0c72a422019-03-07 20:20:02 +00009076#ifndef NDEBUG
9077LLVM_DUMP_METHOD void
9078ClangASTContext::dump(lldb::opaque_compiler_type_t type) const {
9079 if (!type)
9080 return;
9081 clang::QualType qual_type(GetQualType(type));
9082 qual_type.dump();
9083}
9084#endif
9085
Zachary Turner49110232018-11-05 17:40:28 +00009086void ClangASTContext::Dump(Stream &s) {
Zachary Turner115209e2018-11-05 19:25:39 +00009087 Decl *tu = Decl::castFromDeclContext(GetTranslationUnitDecl());
Zachary Turner49110232018-11-05 17:40:28 +00009088 tu->dump(s.AsRawOstream());
9089}
9090
Kate Stoneb9c1b512016-09-06 20:57:50 +00009091void ClangASTContext::DumpValue(
9092 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
Zachary Turner29cb8682017-03-03 20:57:05 +00009093 lldb::Format format, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009094 lldb::offset_t data_byte_offset, size_t data_byte_size,
9095 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
9096 bool show_summary, bool verbose, uint32_t depth) {
9097 if (!type)
9098 return;
9099
9100 clang::QualType qual_type(GetQualType(type));
9101 switch (qual_type->getTypeClass()) {
9102 case clang::Type::Record:
9103 if (GetCompleteType(type)) {
9104 const clang::RecordType *record_type =
9105 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9106 const clang::RecordDecl *record_decl = record_type->getDecl();
9107 assert(record_decl);
9108 uint32_t field_bit_offset = 0;
9109 uint32_t field_byte_offset = 0;
9110 const clang::ASTRecordLayout &record_layout =
9111 getASTContext()->getASTRecordLayout(record_decl);
9112 uint32_t child_idx = 0;
9113
9114 const clang::CXXRecordDecl *cxx_record_decl =
9115 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9116 if (cxx_record_decl) {
9117 // We might have base classes to print out first
9118 clang::CXXRecordDecl::base_class_const_iterator base_class,
9119 base_class_end;
9120 for (base_class = cxx_record_decl->bases_begin(),
9121 base_class_end = cxx_record_decl->bases_end();
9122 base_class != base_class_end; ++base_class) {
9123 const clang::CXXRecordDecl *base_class_decl =
9124 llvm::cast<clang::CXXRecordDecl>(
9125 base_class->getType()->getAs<clang::RecordType>()->getDecl());
9126
9127 // Skip empty base classes
Jonas Devliegherea6682a42018-12-15 00:15:33 +00009128 if (!verbose && !ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00009129 continue;
9130
9131 if (base_class->isVirtual())
9132 field_bit_offset =
9133 record_layout.getVBaseClassOffset(base_class_decl)
9134 .getQuantity() *
9135 8;
9136 else
9137 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
9138 .getQuantity() *
9139 8;
9140 field_byte_offset = field_bit_offset / 8;
9141 assert(field_bit_offset % 8 == 0);
9142 if (child_idx == 0)
9143 s->PutChar('{');
9144 else
9145 s->PutChar(',');
9146
9147 clang::QualType base_class_qual_type = base_class->getType();
9148 std::string base_class_type_name(base_class_qual_type.getAsString());
9149
9150 // Indent and print the base class type name
Zachary Turner827d5d72016-12-16 04:27:00 +00009151 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
9152 base_class_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009153
9154 clang::TypeInfo base_class_type_info =
9155 getASTContext()->getTypeInfo(base_class_qual_type);
9156
9157 // Dump the value of the member
9158 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
9159 base_clang_type.DumpValue(
9160 exe_ctx,
9161 s, // Stream to dump to
9162 base_clang_type
9163 .GetFormat(), // The format with which to display the member
9164 data, // Data buffer containing all bytes for this type
9165 data_byte_offset + field_byte_offset, // Offset into "data" where
9166 // to grab value from
9167 base_class_type_info.Width / 8, // Size of this type in bytes
9168 0, // Bitfield bit size
9169 0, // Bitfield bit offset
9170 show_types, // Boolean indicating if we should show the variable
9171 // types
9172 show_summary, // Boolean indicating if we should show a summary
9173 // for the current type
9174 verbose, // Verbose output?
9175 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9176 // children
9177
9178 ++child_idx;
9179 }
9180 }
9181 uint32_t field_idx = 0;
9182 clang::RecordDecl::field_iterator field, field_end;
9183 for (field = record_decl->field_begin(),
9184 field_end = record_decl->field_end();
9185 field != field_end; ++field, ++field_idx, ++child_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009186 // Print the starting squiggly bracket (if this is the first member) or
9187 // comma (for member 2 and beyond) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009188 if (child_idx == 0)
9189 s->PutChar('{');
9190 else
9191 s->PutChar(',');
9192
9193 // Indent
9194 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
9195
9196 clang::QualType field_type = field->getType();
9197 // Print the member type if requested
Adrian Prantl05097242018-04-30 16:49:04 +00009198 // Figure out the type byte size (field_type_info.first) and alignment
9199 // (field_type_info.second) from the AST context.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009200 clang::TypeInfo field_type_info =
9201 getASTContext()->getTypeInfo(field_type);
9202 assert(field_idx < record_layout.getFieldCount());
9203 // Figure out the field offset within the current struct/union/class
9204 // type
9205 field_bit_offset = record_layout.getFieldOffset(field_idx);
9206 field_byte_offset = field_bit_offset / 8;
9207 uint32_t field_bitfield_bit_size = 0;
9208 uint32_t field_bitfield_bit_offset = 0;
9209 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
9210 field_bitfield_bit_size))
9211 field_bitfield_bit_offset = field_bit_offset % 8;
9212
9213 if (show_types) {
9214 std::string field_type_name(field_type.getAsString());
9215 if (field_bitfield_bit_size > 0)
9216 s->Printf("(%s:%u) ", field_type_name.c_str(),
9217 field_bitfield_bit_size);
9218 else
9219 s->Printf("(%s) ", field_type_name.c_str());
9220 }
9221 // Print the member name and equal sign
9222 s->Printf("%s = ", field->getNameAsString().c_str());
9223
9224 // Dump the value of the member
9225 CompilerType field_clang_type(getASTContext(), field_type);
9226 field_clang_type.DumpValue(
9227 exe_ctx,
9228 s, // Stream to dump to
9229 field_clang_type
9230 .GetFormat(), // The format with which to display the member
9231 data, // Data buffer containing all bytes for this type
9232 data_byte_offset + field_byte_offset, // Offset into "data" where to
9233 // grab value from
9234 field_type_info.Width / 8, // Size of this type in bytes
9235 field_bitfield_bit_size, // Bitfield bit size
9236 field_bitfield_bit_offset, // Bitfield bit offset
9237 show_types, // Boolean indicating if we should show the variable
9238 // types
9239 show_summary, // Boolean indicating if we should show a summary for
9240 // the current type
9241 verbose, // Verbose output?
9242 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9243 // children
9244 }
9245
9246 // Indent the trailing squiggly bracket
9247 if (child_idx > 0)
9248 s->Printf("\n%*s}", depth, "");
9249 }
9250 return;
9251
9252 case clang::Type::Enum:
9253 if (GetCompleteType(type)) {
9254 const clang::EnumType *enutype =
9255 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9256 const clang::EnumDecl *enum_decl = enutype->getDecl();
9257 assert(enum_decl);
9258 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9259 lldb::offset_t offset = data_byte_offset;
9260 const int64_t enum_value = data.GetMaxU64Bitfield(
9261 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
9262 for (enum_pos = enum_decl->enumerator_begin(),
9263 enum_end_pos = enum_decl->enumerator_end();
9264 enum_pos != enum_end_pos; ++enum_pos) {
9265 if (enum_pos->getInitVal() == enum_value) {
9266 s->Printf("%s", enum_pos->getNameAsString().c_str());
9267 return;
9268 }
9269 }
Adrian Prantl05097242018-04-30 16:49:04 +00009270 // If we have gotten here we didn't get find the enumerator in the enum
9271 // decl, so just print the integer.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009272 s->Printf("%" PRIi64, enum_value);
9273 }
9274 return;
9275
9276 case clang::Type::ConstantArray: {
9277 const clang::ConstantArrayType *array =
9278 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9279 bool is_array_of_characters = false;
9280 clang::QualType element_qual_type = array->getElementType();
9281
9282 const clang::Type *canonical_type =
9283 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9284 if (canonical_type)
9285 is_array_of_characters = canonical_type->isCharType();
9286
9287 const uint64_t element_count = array->getSize().getLimitedValue();
9288
9289 clang::TypeInfo field_type_info =
9290 getASTContext()->getTypeInfo(element_qual_type);
9291
9292 uint32_t element_idx = 0;
9293 uint32_t element_offset = 0;
9294 uint64_t element_byte_size = field_type_info.Width / 8;
9295 uint32_t element_stride = element_byte_size;
9296
9297 if (is_array_of_characters) {
9298 s->PutChar('"');
Zachary Turner29cb8682017-03-03 20:57:05 +00009299 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9300 element_byte_size, element_count, UINT32_MAX,
9301 LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009302 s->PutChar('"');
9303 return;
9304 } else {
9305 CompilerType element_clang_type(getASTContext(), element_qual_type);
9306 lldb::Format element_format = element_clang_type.GetFormat();
9307
9308 for (element_idx = 0; element_idx < element_count; ++element_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009309 // Print the starting squiggly bracket (if this is the first member) or
9310 // comman (for member 2 and beyong) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009311 if (element_idx == 0)
9312 s->PutChar('{');
9313 else
9314 s->PutChar(',');
9315
9316 // Indent and print the index
9317 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9318
9319 // Figure out the field offset within the current struct/union/class
9320 // type
9321 element_offset = element_idx * element_stride;
9322
9323 // Dump the value of the member
9324 element_clang_type.DumpValue(
9325 exe_ctx,
9326 s, // Stream to dump to
9327 element_format, // The format with which to display the element
9328 data, // Data buffer containing all bytes for this type
9329 data_byte_offset +
9330 element_offset, // Offset into "data" where to grab value from
9331 element_byte_size, // Size of this type in bytes
9332 0, // Bitfield bit size
9333 0, // Bitfield bit offset
9334 show_types, // Boolean indicating if we should show the variable
9335 // types
9336 show_summary, // Boolean indicating if we should show a summary for
9337 // the current type
9338 verbose, // Verbose output?
9339 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9340 // children
9341 }
9342
9343 // Indent the trailing squiggly bracket
9344 if (element_idx > 0)
9345 s->Printf("\n%*s}", depth, "");
9346 }
9347 }
9348 return;
9349
9350 case clang::Type::Typedef: {
9351 clang::QualType typedef_qual_type =
9352 llvm::cast<clang::TypedefType>(qual_type)
9353 ->getDecl()
9354 ->getUnderlyingType();
9355
9356 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9357 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9358 clang::TypeInfo typedef_type_info =
9359 getASTContext()->getTypeInfo(typedef_qual_type);
9360 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9361
9362 return typedef_clang_type.DumpValue(
9363 exe_ctx,
9364 s, // Stream to dump to
9365 typedef_format, // The format with which to display the element
9366 data, // Data buffer containing all bytes for this type
9367 data_byte_offset, // Offset into "data" where to grab value from
9368 typedef_byte_size, // Size of this type in bytes
9369 bitfield_bit_size, // Bitfield bit size
9370 bitfield_bit_offset, // Bitfield bit offset
9371 show_types, // Boolean indicating if we should show the variable types
9372 show_summary, // Boolean indicating if we should show a summary for the
9373 // current type
9374 verbose, // Verbose output?
9375 depth); // Scope depth for any types that have children
9376 } break;
9377
9378 case clang::Type::Auto: {
9379 clang::QualType elaborated_qual_type =
9380 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9381 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9382 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9383 clang::TypeInfo elaborated_type_info =
9384 getASTContext()->getTypeInfo(elaborated_qual_type);
9385 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9386
9387 return elaborated_clang_type.DumpValue(
9388 exe_ctx,
9389 s, // Stream to dump to
9390 elaborated_format, // The format with which to display the element
9391 data, // Data buffer containing all bytes for this type
9392 data_byte_offset, // Offset into "data" where to grab value from
9393 elaborated_byte_size, // Size of this type in bytes
9394 bitfield_bit_size, // Bitfield bit size
9395 bitfield_bit_offset, // Bitfield bit offset
9396 show_types, // Boolean indicating if we should show the variable types
9397 show_summary, // Boolean indicating if we should show a summary for the
9398 // current type
9399 verbose, // Verbose output?
9400 depth); // Scope depth for any types that have children
9401 } break;
9402
9403 case clang::Type::Elaborated: {
9404 clang::QualType elaborated_qual_type =
9405 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9406 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9407 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9408 clang::TypeInfo elaborated_type_info =
9409 getASTContext()->getTypeInfo(elaborated_qual_type);
9410 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9411
9412 return elaborated_clang_type.DumpValue(
9413 exe_ctx,
9414 s, // Stream to dump to
9415 elaborated_format, // The format with which to display the element
9416 data, // Data buffer containing all bytes for this type
9417 data_byte_offset, // Offset into "data" where to grab value from
9418 elaborated_byte_size, // Size of this type in bytes
9419 bitfield_bit_size, // Bitfield bit size
9420 bitfield_bit_offset, // Bitfield bit offset
9421 show_types, // Boolean indicating if we should show the variable types
9422 show_summary, // Boolean indicating if we should show a summary for the
9423 // current type
9424 verbose, // Verbose output?
9425 depth); // Scope depth for any types that have children
9426 } break;
9427
9428 case clang::Type::Paren: {
9429 clang::QualType desugar_qual_type =
9430 llvm::cast<clang::ParenType>(qual_type)->desugar();
9431 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type);
9432
9433 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9434 clang::TypeInfo desugar_type_info =
9435 getASTContext()->getTypeInfo(desugar_qual_type);
9436 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9437
9438 return desugar_clang_type.DumpValue(
9439 exe_ctx,
9440 s, // Stream to dump to
9441 desugar_format, // The format with which to display the element
9442 data, // Data buffer containing all bytes for this type
9443 data_byte_offset, // Offset into "data" where to grab value from
9444 desugar_byte_size, // Size of this type in bytes
9445 bitfield_bit_size, // Bitfield bit size
9446 bitfield_bit_offset, // Bitfield bit offset
9447 show_types, // Boolean indicating if we should show the variable types
9448 show_summary, // Boolean indicating if we should show a summary for the
9449 // current type
9450 verbose, // Verbose output?
9451 depth); // Scope depth for any types that have children
9452 } break;
9453
9454 default:
9455 // We are down to a scalar type that we just need to display.
Zachary Turner29cb8682017-03-03 20:57:05 +00009456 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9457 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9458 bitfield_bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009459
9460 if (show_summary)
9461 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9462 break;
9463 }
9464}
9465
9466bool ClangASTContext::DumpTypeValue(
9467 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
Zachary Turner29cb8682017-03-03 20:57:05 +00009468 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9469 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009470 ExecutionContextScope *exe_scope) {
9471 if (!type)
9472 return false;
9473 if (IsAggregateType(type)) {
9474 return false;
9475 } else {
Greg Claytond8d4a572015-08-11 21:38:15 +00009476 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009477
9478 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9479 switch (type_class) {
9480 case clang::Type::Typedef: {
9481 clang::QualType typedef_qual_type =
9482 llvm::cast<clang::TypedefType>(qual_type)
9483 ->getDecl()
9484 ->getUnderlyingType();
9485 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9486 if (format == eFormatDefault)
9487 format = typedef_clang_type.GetFormat();
9488 clang::TypeInfo typedef_type_info =
9489 getASTContext()->getTypeInfo(typedef_qual_type);
9490 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9491
9492 return typedef_clang_type.DumpTypeValue(
9493 s,
9494 format, // The format with which to display the element
9495 data, // Data buffer containing all bytes for this type
9496 byte_offset, // Offset into "data" where to grab value from
9497 typedef_byte_size, // Size of this type in bytes
9498 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9499 // treat as a bitfield
9500 bitfield_bit_offset, // Offset in bits of a bitfield value if
9501 // bitfield_bit_size != 0
9502 exe_scope);
9503 } break;
9504
9505 case clang::Type::Enum:
Adrian Prantl05097242018-04-30 16:49:04 +00009506 // If our format is enum or default, show the enumeration value as its
9507 // enumeration string value, else just display it as requested.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009508 if ((format == eFormatEnum || format == eFormatDefault) &&
9509 GetCompleteType(type)) {
9510 const clang::EnumType *enutype =
9511 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9512 const clang::EnumDecl *enum_decl = enutype->getDecl();
9513 assert(enum_decl);
9514 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9515 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9516 lldb::offset_t offset = byte_offset;
9517 if (is_signed) {
9518 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9519 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9520 for (enum_pos = enum_decl->enumerator_begin(),
9521 enum_end_pos = enum_decl->enumerator_end();
9522 enum_pos != enum_end_pos; ++enum_pos) {
9523 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009524 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009525 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009526 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009527 }
9528 // If we have gotten here we didn't get find the enumerator in the
9529 // enum decl, so just print the integer.
9530 s->Printf("%" PRIi64, enum_svalue);
9531 } else {
9532 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9533 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9534 for (enum_pos = enum_decl->enumerator_begin(),
9535 enum_end_pos = enum_decl->enumerator_end();
9536 enum_pos != enum_end_pos; ++enum_pos) {
9537 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009538 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009539 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009540 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009541 }
9542 // If we have gotten here we didn't get find the enumerator in the
9543 // enum decl, so just print the integer.
9544 s->Printf("%" PRIu64, enum_uvalue);
Greg Claytond8d4a572015-08-11 21:38:15 +00009545 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009546 return true;
9547 }
9548 // format was not enum, just fall through and dump the value as
9549 // requested....
9550 LLVM_FALLTHROUGH;
9551
9552 default:
9553 // We are down to a scalar type that we just need to display.
9554 {
9555 uint32_t item_count = 1;
9556 // A few formats, we might need to modify our size and count for
9557 // depending
9558 // on how we are trying to display the value...
9559 switch (format) {
Greg Claytond8d4a572015-08-11 21:38:15 +00009560 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00009561 case eFormatBoolean:
9562 case eFormatBinary:
9563 case eFormatComplex:
9564 case eFormatCString: // NULL terminated C strings
9565 case eFormatDecimal:
9566 case eFormatEnum:
9567 case eFormatHex:
9568 case eFormatHexUppercase:
9569 case eFormatFloat:
9570 case eFormatOctal:
9571 case eFormatOSType:
9572 case eFormatUnsigned:
9573 case eFormatPointer:
9574 case eFormatVectorOfChar:
9575 case eFormatVectorOfSInt8:
9576 case eFormatVectorOfUInt8:
9577 case eFormatVectorOfSInt16:
9578 case eFormatVectorOfUInt16:
9579 case eFormatVectorOfSInt32:
9580 case eFormatVectorOfUInt32:
9581 case eFormatVectorOfSInt64:
9582 case eFormatVectorOfUInt64:
9583 case eFormatVectorOfFloat32:
9584 case eFormatVectorOfFloat64:
9585 case eFormatVectorOfUInt128:
9586 break;
9587
9588 case eFormatChar:
9589 case eFormatCharPrintable:
9590 case eFormatCharArray:
9591 case eFormatBytes:
9592 case eFormatBytesWithASCII:
9593 item_count = byte_size;
9594 byte_size = 1;
9595 break;
9596
9597 case eFormatUnicode16:
9598 item_count = byte_size / 2;
9599 byte_size = 2;
9600 break;
9601
9602 case eFormatUnicode32:
9603 item_count = byte_size / 4;
9604 byte_size = 4;
9605 break;
9606 }
Zachary Turner29cb8682017-03-03 20:57:05 +00009607 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9608 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
9609 bitfield_bit_size, bitfield_bit_offset,
9610 exe_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009611 }
9612 break;
9613 }
9614 }
9615 return 0;
9616}
9617
9618void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9619 ExecutionContext *exe_ctx, Stream *s,
9620 const lldb_private::DataExtractor &data,
9621 lldb::offset_t data_byte_offset,
9622 size_t data_byte_size) {
9623 uint32_t length = 0;
9624 if (IsCStringType(type, length)) {
9625 if (exe_ctx) {
9626 Process *process = exe_ctx->GetProcessPtr();
9627 if (process) {
9628 lldb::offset_t offset = data_byte_offset;
9629 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9630 std::vector<uint8_t> buf;
9631 if (length > 0)
9632 buf.resize(length);
9633 else
9634 buf.resize(256);
9635
Zachary Turner29cb8682017-03-03 20:57:05 +00009636 DataExtractor cstr_data(&buf.front(), buf.size(),
9637 process->GetByteOrder(), 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009638 buf.back() = '\0';
9639 size_t bytes_read;
9640 size_t total_cstr_len = 0;
Zachary Turner97206d52017-05-12 04:51:55 +00009641 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009642 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9643 buf.size(), error)) > 0) {
9644 const size_t len = strlen((const char *)&buf.front());
9645 if (len == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00009646 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009647 if (total_cstr_len == 0)
9648 s->PutCString(" \"");
Zachary Turner29cb8682017-03-03 20:57:05 +00009649 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9650 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009651 total_cstr_len += len;
9652 if (len < buf.size())
9653 break;
9654 pointer_address += total_cstr_len;
Greg Claytond8d4a572015-08-11 21:38:15 +00009655 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009656 if (total_cstr_len > 0)
9657 s->PutChar('"');
9658 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009659 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009660 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009661}
9662
Kate Stoneb9c1b512016-09-06 20:57:50 +00009663void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9664 StreamFile s(stdout, false);
9665 DumpTypeDescription(type, &s);
9666 ClangASTMetadata *metadata =
9667 ClangASTContext::GetMetadata(getASTContext(), type);
9668 if (metadata) {
9669 metadata->Dump(&s);
9670 }
9671}
Greg Claytond8d4a572015-08-11 21:38:15 +00009672
Kate Stoneb9c1b512016-09-06 20:57:50 +00009673void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9674 Stream *s) {
9675 if (type) {
9676 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00009677
Kate Stoneb9c1b512016-09-06 20:57:50 +00009678 llvm::SmallVector<char, 1024> buf;
9679 llvm::raw_svector_ostream llvm_ostrm(buf);
9680
9681 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9682 switch (type_class) {
9683 case clang::Type::ObjCObject:
9684 case clang::Type::ObjCInterface: {
9685 GetCompleteType(type);
9686
9687 const clang::ObjCObjectType *objc_class_type =
9688 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9689 assert(objc_class_type);
9690 if (objc_class_type) {
9691 clang::ObjCInterfaceDecl *class_interface_decl =
9692 objc_class_type->getInterface();
9693 if (class_interface_decl) {
9694 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9695 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
Greg Claytond8d4a572015-08-11 21:38:15 +00009696 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009697 }
9698 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00009699
Kate Stoneb9c1b512016-09-06 20:57:50 +00009700 case clang::Type::Typedef: {
9701 const clang::TypedefType *typedef_type =
9702 qual_type->getAs<clang::TypedefType>();
9703 if (typedef_type) {
9704 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9705 std::string clang_typedef_name(
9706 typedef_decl->getQualifiedNameAsString());
9707 if (!clang_typedef_name.empty()) {
9708 s->PutCString("typedef ");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009709 s->PutCString(clang_typedef_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00009710 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009711 }
9712 } break;
9713
9714 case clang::Type::Auto:
9715 CompilerType(getASTContext(),
9716 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
9717 .DumpTypeDescription(s);
9718 return;
9719
9720 case clang::Type::Elaborated:
9721 CompilerType(getASTContext(),
9722 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
9723 .DumpTypeDescription(s);
9724 return;
9725
9726 case clang::Type::Paren:
9727 CompilerType(getASTContext(),
9728 llvm::cast<clang::ParenType>(qual_type)->desugar())
9729 .DumpTypeDescription(s);
9730 return;
9731
9732 case clang::Type::Record: {
9733 GetCompleteType(type);
9734
9735 const clang::RecordType *record_type =
9736 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9737 const clang::RecordDecl *record_decl = record_type->getDecl();
9738 const clang::CXXRecordDecl *cxx_record_decl =
9739 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9740
9741 if (cxx_record_decl)
9742 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9743 s->GetIndentLevel());
9744 else
9745 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9746 s->GetIndentLevel());
9747 } break;
9748
9749 default: {
9750 const clang::TagType *tag_type =
9751 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9752 if (tag_type) {
9753 clang::TagDecl *tag_decl = tag_type->getDecl();
9754 if (tag_decl)
9755 tag_decl->print(llvm_ostrm, 0);
9756 } else {
9757 std::string clang_type_name(qual_type.getAsString());
9758 if (!clang_type_name.empty())
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009759 s->PutCString(clang_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009760 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009761 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00009762 }
9763
Kate Stoneb9c1b512016-09-06 20:57:50 +00009764 if (buf.size() > 0) {
9765 s->Write(buf.data(), buf.size());
Greg Clayton8b4edba2015-08-14 20:02:05 +00009766 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009767 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00009768}
9769
Kate Stoneb9c1b512016-09-06 20:57:50 +00009770void ClangASTContext::DumpTypeName(const CompilerType &type) {
9771 if (ClangUtil::IsClangType(type)) {
9772 clang::QualType qual_type(
9773 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9774
9775 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9776 switch (type_class) {
9777 case clang::Type::Record: {
9778 const clang::CXXRecordDecl *cxx_record_decl =
9779 qual_type->getAsCXXRecordDecl();
9780 if (cxx_record_decl)
9781 printf("class %s", cxx_record_decl->getName().str().c_str());
9782 } break;
9783
9784 case clang::Type::Enum: {
9785 clang::EnumDecl *enum_decl =
9786 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9787 if (enum_decl) {
9788 printf("enum %s", enum_decl->getName().str().c_str());
9789 }
9790 } break;
9791
9792 case clang::Type::ObjCObject:
9793 case clang::Type::ObjCInterface: {
9794 const clang::ObjCObjectType *objc_class_type =
9795 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9796 if (objc_class_type) {
9797 clang::ObjCInterfaceDecl *class_interface_decl =
9798 objc_class_type->getInterface();
Adrian Prantl05097242018-04-30 16:49:04 +00009799 // We currently can't complete objective C types through the newly
9800 // added ASTContext because it only supports TagDecl objects right
9801 // now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00009802 if (class_interface_decl)
9803 printf("@class %s", class_interface_decl->getName().str().c_str());
9804 }
9805 } break;
9806
9807 case clang::Type::Typedef:
9808 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9809 ->getDecl()
9810 ->getName()
9811 .str()
9812 .c_str());
9813 break;
9814
9815 case clang::Type::Auto:
9816 printf("auto ");
9817 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9818 llvm::cast<clang::AutoType>(qual_type)
9819 ->getDeducedType()
9820 .getAsOpaquePtr()));
9821
9822 case clang::Type::Elaborated:
9823 printf("elaborated ");
9824 return DumpTypeName(CompilerType(
9825 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9826 ->getNamedType()
9827 .getAsOpaquePtr()));
9828
9829 case clang::Type::Paren:
9830 printf("paren ");
9831 return DumpTypeName(CompilerType(
9832 type.GetTypeSystem(),
9833 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9834
9835 default:
9836 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9837 break;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009838 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009839 }
Greg Clayton6dc8d582015-08-18 22:32:36 +00009840}
9841
Kate Stoneb9c1b512016-09-06 20:57:50 +00009842clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9843 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9844 const char *parent_name, int tag_decl_kind,
9845 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9846 if (template_param_infos.IsValid()) {
9847 std::string template_basename(parent_name);
9848 template_basename.erase(template_basename.find('<'));
9849
9850 return CreateClassTemplateDecl(decl_ctx, access_type,
9851 template_basename.c_str(), tag_decl_kind,
9852 template_param_infos);
9853 }
9854 return NULL;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009855}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009856
Kate Stoneb9c1b512016-09-06 20:57:50 +00009857void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9858 ClangASTContext *ast = (ClangASTContext *)baton;
9859 SymbolFile *sym_file = ast->GetSymbolFile();
9860 if (sym_file) {
9861 CompilerType clang_type = GetTypeForDecl(decl);
9862 if (clang_type)
9863 sym_file->CompleteType(clang_type);
9864 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00009865}
9866
Kate Stoneb9c1b512016-09-06 20:57:50 +00009867void ClangASTContext::CompleteObjCInterfaceDecl(
9868 void *baton, clang::ObjCInterfaceDecl *decl) {
9869 ClangASTContext *ast = (ClangASTContext *)baton;
9870 SymbolFile *sym_file = ast->GetSymbolFile();
9871 if (sym_file) {
9872 CompilerType clang_type = GetTypeForDecl(decl);
9873 if (clang_type)
9874 sym_file->CompleteType(clang_type);
9875 }
Zachary Turner42dff792016-04-15 00:21:26 +00009876}
Greg Clayton261ac3f2015-08-28 01:01:03 +00009877
Kate Stoneb9c1b512016-09-06 20:57:50 +00009878DWARFASTParser *ClangASTContext::GetDWARFParser() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009879 if (!m_dwarf_ast_parser_up)
9880 m_dwarf_ast_parser_up.reset(new DWARFASTParserClang(*this));
9881 return m_dwarf_ast_parser_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00009882}
9883
9884PDBASTParser *ClangASTContext::GetPDBParser() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009885 if (!m_pdb_ast_parser_up)
9886 m_pdb_ast_parser_up.reset(new PDBASTParser(*this));
9887 return m_pdb_ast_parser_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00009888}
9889
9890bool ClangASTContext::LayoutRecordType(
9891 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9892 uint64_t &alignment,
9893 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9894 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9895 &base_offsets,
9896 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9897 &vbase_offsets) {
9898 ClangASTContext *ast = (ClangASTContext *)baton;
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00009899 lldb_private::ClangASTImporter *importer = nullptr;
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009900 if (ast->m_dwarf_ast_parser_up)
9901 importer = &ast->m_dwarf_ast_parser_up->GetClangASTImporter();
9902 if (!importer && ast->m_pdb_ast_parser_up)
9903 importer = &ast->m_pdb_ast_parser_up->GetClangASTImporter();
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00009904 if (!importer)
9905 return false;
9906
9907 return importer->LayoutRecordType(record_decl, bit_size, alignment,
9908 field_offsets, base_offsets, vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009909}
9910
Paul Hermand628cbb2015-09-15 23:44:17 +00009911// CompilerDecl override functions
Paul Hermand628cbb2015-09-15 23:44:17 +00009912
Kate Stoneb9c1b512016-09-06 20:57:50 +00009913ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9914 if (opaque_decl) {
9915 clang::NamedDecl *nd =
9916 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9917 if (nd != nullptr)
9918 return ConstString(nd->getDeclName().getAsString());
9919 }
9920 return ConstString();
Paul Hermand628cbb2015-09-15 23:44:17 +00009921}
9922
Kate Stoneb9c1b512016-09-06 20:57:50 +00009923ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9924 if (opaque_decl) {
9925 clang::NamedDecl *nd =
9926 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9927 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9928 clang::MangleContext *mc = getMangleContext();
9929 if (mc && mc->shouldMangleCXXName(nd)) {
9930 llvm::SmallVector<char, 1024> buf;
9931 llvm::raw_svector_ostream llvm_ostrm(buf);
9932 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9933 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9934 Ctor_Complete, llvm_ostrm);
9935 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9936 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9937 Dtor_Complete, llvm_ostrm);
9938 } else {
9939 mc->mangleName(nd, llvm_ostrm);
Greg Claytonfe689042015-11-10 17:47:04 +00009940 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009941 if (buf.size() > 0)
9942 return ConstString(buf.data(), buf.size());
9943 }
Greg Claytonfe689042015-11-10 17:47:04 +00009944 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009945 }
9946 return ConstString();
Greg Claytonfe689042015-11-10 17:47:04 +00009947}
9948
Kate Stoneb9c1b512016-09-06 20:57:50 +00009949CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9950 if (opaque_decl)
9951 return CompilerDeclContext(this,
9952 ((clang::Decl *)opaque_decl)->getDeclContext());
9953 else
9954 return CompilerDeclContext();
Greg Claytonfe689042015-11-10 17:47:04 +00009955}
9956
Kate Stoneb9c1b512016-09-06 20:57:50 +00009957CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9958 if (clang::FunctionDecl *func_decl =
9959 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9960 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9961 if (clang::ObjCMethodDecl *objc_method =
9962 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9963 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9964 else
Greg Claytonfe689042015-11-10 17:47:04 +00009965 return CompilerType();
9966}
9967
Kate Stoneb9c1b512016-09-06 20:57:50 +00009968size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9969 if (clang::FunctionDecl *func_decl =
9970 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9971 return func_decl->param_size();
9972 if (clang::ObjCMethodDecl *objc_method =
9973 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9974 return objc_method->param_size();
9975 else
9976 return 0;
9977}
9978
9979CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
9980 size_t idx) {
9981 if (clang::FunctionDecl *func_decl =
9982 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
9983 if (idx < func_decl->param_size()) {
9984 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9985 if (var_decl)
9986 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
9987 }
9988 } else if (clang::ObjCMethodDecl *objc_method =
9989 llvm::dyn_cast<clang::ObjCMethodDecl>(
9990 (clang::Decl *)opaque_decl)) {
9991 if (idx < objc_method->param_size())
9992 return CompilerType(
9993 this,
9994 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9995 }
9996 return CompilerType();
9997}
9998
Greg Clayton99558cc42015-08-24 23:46:31 +00009999// CompilerDeclContext functions
Greg Clayton99558cc42015-08-24 23:46:31 +000010000
Kate Stoneb9c1b512016-09-06 20:57:50 +000010001std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
10002 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
10003 std::vector<CompilerDecl> found_decls;
10004 if (opaque_decl_ctx) {
10005 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
10006 std::set<DeclContext *> searched;
10007 std::multimap<DeclContext *, DeclContext *> search_queue;
10008 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +000010009
Kate Stoneb9c1b512016-09-06 20:57:50 +000010010 for (clang::DeclContext *decl_context = root_decl_ctx;
10011 decl_context != nullptr && found_decls.empty();
10012 decl_context = decl_context->getParent()) {
10013 search_queue.insert(std::make_pair(decl_context, decl_context));
Paul Hermand628cbb2015-09-15 23:44:17 +000010014
Kate Stoneb9c1b512016-09-06 20:57:50 +000010015 for (auto it = search_queue.find(decl_context); it != search_queue.end();
10016 it++) {
10017 if (!searched.insert(it->second).second)
10018 continue;
10019 symbol_file->ParseDeclsForContext(
10020 CompilerDeclContext(this, it->second));
Paul Hermanea188fc2015-09-16 18:48:30 +000010021
Kate Stoneb9c1b512016-09-06 20:57:50 +000010022 for (clang::Decl *child : it->second->decls()) {
10023 if (clang::UsingDirectiveDecl *ud =
10024 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10025 if (ignore_using_decls)
10026 continue;
10027 clang::DeclContext *from = ud->getCommonAncestor();
10028 if (searched.find(ud->getNominatedNamespace()) == searched.end())
10029 search_queue.insert(
10030 std::make_pair(from, ud->getNominatedNamespace()));
10031 } else if (clang::UsingDecl *ud =
10032 llvm::dyn_cast<clang::UsingDecl>(child)) {
10033 if (ignore_using_decls)
10034 continue;
10035 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10036 clang::Decl *target = usd->getTargetDecl();
10037 if (clang::NamedDecl *nd =
10038 llvm::dyn_cast<clang::NamedDecl>(target)) {
10039 IdentifierInfo *ii = nd->getIdentifier();
10040 if (ii != nullptr &&
10041 ii->getName().equals(name.AsCString(nullptr)))
10042 found_decls.push_back(CompilerDecl(this, nd));
10043 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010044 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010045 } else if (clang::NamedDecl *nd =
10046 llvm::dyn_cast<clang::NamedDecl>(child)) {
10047 IdentifierInfo *ii = nd->getIdentifier();
10048 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
10049 found_decls.push_back(CompilerDecl(this, nd));
10050 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010051 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010052 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010053 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010054 }
10055 return found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +000010056}
10057
Dawn Perchikb5925782015-12-12 19:31:41 +000010058// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010059// and return the number of levels it took to find it, or
Adrian Prantl05097242018-04-30 16:49:04 +000010060// LLDB_INVALID_DECL_LEVEL if not found. If the decl was imported via a using
10061// declaration, its name and/or type, if set, will be used to check that the
10062// decl found in the scope is a match.
Dawn Perchikb5925782015-12-12 19:31:41 +000010063//
Kate Stoneb9c1b512016-09-06 20:57:50 +000010064// The optional name is required by languages (like C++) to handle using
Adrian Prantl05097242018-04-30 16:49:04 +000010065// declarations like:
Dawn Perchikb5925782015-12-12 19:31:41 +000010066//
10067// void poo();
10068// namespace ns {
10069// void foo();
10070// void goo();
10071// }
10072// void bar() {
10073// using ns::foo;
10074// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
10075// // LLDB_INVALID_DECL_LEVEL for 'goo'.
10076// }
10077//
10078// The optional type is useful in the case that there's a specific overload
10079// that we're looking for that might otherwise be shadowed, like:
10080//
10081// void foo(int);
10082// namespace ns {
10083// void foo();
10084// }
10085// void bar() {
10086// using ns::foo;
10087// // CountDeclLevels returns 0 for { 'foo', void() },
10088// // 1 for { 'foo', void(int) }, and
10089// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
10090// }
10091//
10092// NOTE: Because file statics are at the TranslationUnit along with globals, a
Kate Stoneb9c1b512016-09-06 20:57:50 +000010093// function at file scope will return the same level as a function at global
Adrian Prantl05097242018-04-30 16:49:04 +000010094// scope. Ideally we'd like to treat the file scope as an additional scope just
10095// below the global scope. More work needs to be done to recognise that, if
10096// the decl we're trying to look up is static, we should compare its source
10097// file with that of the current scope and return a lower number for it.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010098uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
10099 clang::DeclContext *child_decl_ctx,
10100 ConstString *child_name,
10101 CompilerType *child_type) {
10102 if (frame_decl_ctx) {
10103 std::set<DeclContext *> searched;
10104 std::multimap<DeclContext *, DeclContext *> search_queue;
10105 SymbolFile *symbol_file = GetSymbolFile();
Dawn Perchikb5925782015-12-12 19:31:41 +000010106
Kate Stoneb9c1b512016-09-06 20:57:50 +000010107 // Get the lookup scope for the decl we're trying to find.
10108 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
Dawn Perchikb5925782015-12-12 19:31:41 +000010109
Kate Stoneb9c1b512016-09-06 20:57:50 +000010110 // Look for it in our scope's decl context and its parents.
10111 uint32_t level = 0;
10112 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
10113 decl_ctx = decl_ctx->getParent()) {
10114 if (!decl_ctx->isLookupContext())
10115 continue;
10116 if (decl_ctx == parent_decl_ctx)
10117 // Found it!
10118 return level;
10119 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
10120 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
10121 it++) {
10122 if (searched.find(it->second) != searched.end())
10123 continue;
10124
10125 // Currently DWARF has one shared translation unit for all Decls at top
Adrian Prantl05097242018-04-30 16:49:04 +000010126 // level, so this would erroneously find using statements anywhere. So
10127 // don't look at the top-level translation unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010128 // TODO fix this and add a testcase that depends on it.
10129
10130 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
10131 continue;
10132
10133 searched.insert(it->second);
10134 symbol_file->ParseDeclsForContext(
10135 CompilerDeclContext(this, it->second));
10136
10137 for (clang::Decl *child : it->second->decls()) {
10138 if (clang::UsingDirectiveDecl *ud =
10139 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10140 clang::DeclContext *ns = ud->getNominatedNamespace();
10141 if (ns == parent_decl_ctx)
10142 // Found it!
10143 return level;
10144 clang::DeclContext *from = ud->getCommonAncestor();
10145 if (searched.find(ns) == searched.end())
10146 search_queue.insert(std::make_pair(from, ns));
10147 } else if (child_name) {
10148 if (clang::UsingDecl *ud =
10149 llvm::dyn_cast<clang::UsingDecl>(child)) {
10150 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10151 clang::Decl *target = usd->getTargetDecl();
10152 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
10153 if (!nd)
10154 continue;
10155 // Check names.
10156 IdentifierInfo *ii = nd->getIdentifier();
10157 if (ii == nullptr ||
10158 !ii->getName().equals(child_name->AsCString(nullptr)))
10159 continue;
10160 // Check types, if one was provided.
10161 if (child_type) {
10162 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
10163 if (!AreTypesSame(clang_type, *child_type,
10164 /*ignore_qualifiers=*/true))
10165 continue;
10166 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010167 // Found it!
10168 return level;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010169 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010170 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010171 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010172 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010173 }
10174 ++level;
Dawn Perchikb5925782015-12-12 19:31:41 +000010175 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010176 }
10177 return LLDB_INVALID_DECL_LEVEL;
Dawn Perchikb5925782015-12-12 19:31:41 +000010178}
10179
Kate Stoneb9c1b512016-09-06 20:57:50 +000010180bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
10181 if (opaque_decl_ctx)
10182 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
10183 else
Greg Clayton99558cc42015-08-24 23:46:31 +000010184 return false;
10185}
10186
Kate Stoneb9c1b512016-09-06 20:57:50 +000010187ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
10188 if (opaque_decl_ctx) {
10189 clang::NamedDecl *named_decl =
10190 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10191 if (named_decl)
10192 return ConstString(named_decl->getName());
10193 }
10194 return ConstString();
Greg Clayton99558cc42015-08-24 23:46:31 +000010195}
10196
Kate Stoneb9c1b512016-09-06 20:57:50 +000010197ConstString
10198ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
10199 if (opaque_decl_ctx) {
10200 clang::NamedDecl *named_decl =
10201 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10202 if (named_decl)
10203 return ConstString(
10204 llvm::StringRef(named_decl->getQualifiedNameAsString()));
10205 }
10206 return ConstString();
10207}
10208
10209bool ClangASTContext::DeclContextIsClassMethod(
10210 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
10211 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
10212 if (opaque_decl_ctx) {
10213 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10214 if (ObjCMethodDecl *objc_method =
10215 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
10216 if (is_instance_method_ptr)
10217 *is_instance_method_ptr = objc_method->isInstanceMethod();
10218 if (language_ptr)
10219 *language_ptr = eLanguageTypeObjC;
10220 if (language_object_name_ptr)
10221 language_object_name_ptr->SetCString("self");
10222 return true;
10223 } else if (CXXMethodDecl *cxx_method =
10224 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
10225 if (is_instance_method_ptr)
10226 *is_instance_method_ptr = cxx_method->isInstance();
10227 if (language_ptr)
10228 *language_ptr = eLanguageTypeC_plus_plus;
10229 if (language_object_name_ptr)
10230 language_object_name_ptr->SetCString("this");
10231 return true;
10232 } else if (clang::FunctionDecl *function_decl =
10233 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
10234 ClangASTMetadata *metadata =
10235 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
10236 if (metadata && metadata->HasObjectPtr()) {
10237 if (is_instance_method_ptr)
10238 *is_instance_method_ptr = true;
10239 if (language_ptr)
10240 *language_ptr = eLanguageTypeObjC;
10241 if (language_object_name_ptr)
10242 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
10243 return true;
10244 }
10245 }
10246 }
10247 return false;
10248}
10249
Raphael Isemanna9469972019-03-12 07:45:04 +000010250bool ClangASTContext::DeclContextIsContainedInLookup(
10251 void *opaque_decl_ctx, void *other_opaque_decl_ctx) {
10252 auto *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10253 auto *other = (clang::DeclContext *)other_opaque_decl_ctx;
10254
10255 do {
10256 // A decl context always includes its own contents in its lookup.
10257 if (decl_ctx == other)
10258 return true;
10259
10260 // If we have an inline namespace, then the lookup of the parent context
10261 // also includes the inline namespace contents.
10262 } while (other->isInlineNamespace() && (other = other->getParent()));
10263
10264 return false;
10265}
10266
Kate Stoneb9c1b512016-09-06 20:57:50 +000010267clang::DeclContext *
10268ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
10269 if (dc.IsClang())
10270 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
10271 return nullptr;
10272}
Greg Clayton99558cc42015-08-24 23:46:31 +000010273
10274ObjCMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010275ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
10276 if (dc.IsClang())
10277 return llvm::dyn_cast<clang::ObjCMethodDecl>(
10278 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10279 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010280}
10281
10282CXXMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010283ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10284 if (dc.IsClang())
10285 return llvm::dyn_cast<clang::CXXMethodDecl>(
10286 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10287 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010288}
10289
10290clang::FunctionDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010291ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10292 if (dc.IsClang())
10293 return llvm::dyn_cast<clang::FunctionDecl>(
10294 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10295 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010296}
10297
10298clang::NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010299ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10300 if (dc.IsClang())
10301 return llvm::dyn_cast<clang::NamespaceDecl>(
10302 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10303 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010304}
10305
10306ClangASTMetadata *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010307ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10308 const void *object) {
10309 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10310 if (ast)
10311 return ClangASTContext::GetMetadata(ast, object);
10312 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010313}
10314
10315clang::ASTContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010316ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10317 ClangASTContext *ast =
10318 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10319 if (ast)
10320 return ast->getASTContext();
10321 return nullptr;
10322}
10323
10324ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10325 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10326 m_target_wp(target.shared_from_this()),
10327 m_persistent_variables(new ClangPersistentVariables) {}
10328
10329UserExpression *ClangASTContextForExpressions::GetUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010330 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010331 Expression::ResultType desired_type,
Aleksandr Urakov40624a02019-02-05 09:14:36 +000010332 const EvaluateExpressionOptions &options,
10333 ValueObject *ctx_obj) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000010334 TargetSP target_sp = m_target_wp.lock();
10335 if (!target_sp)
Greg Clayton99558cc42015-08-24 23:46:31 +000010336 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010337
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010338 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
Aleksandr Urakov40624a02019-02-05 09:14:36 +000010339 desired_type, options, ctx_obj);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010340}
10341
Kate Stoneb9c1b512016-09-06 20:57:50 +000010342FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10343 const CompilerType &return_type, const Address &function_address,
10344 const ValueList &arg_value_list, const char *name) {
10345 TargetSP target_sp = m_target_wp.lock();
10346 if (!target_sp)
10347 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010348
Kate Stoneb9c1b512016-09-06 20:57:50 +000010349 Process *process = target_sp->GetProcessSP().get();
10350 if (!process)
10351 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010352
Kate Stoneb9c1b512016-09-06 20:57:50 +000010353 return new ClangFunctionCaller(*process, return_type, function_address,
10354 arg_value_list, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010355}
10356
10357UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010358ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10359 const char *name) {
10360 TargetSP target_sp = m_target_wp.lock();
10361 if (!target_sp)
10362 return nullptr;
10363
10364 return new ClangUtilityFunction(*target_sp.get(), text, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010365}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010366
10367PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010368ClangASTContextForExpressions::GetPersistentExpressionState() {
10369 return m_persistent_variables.get();
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010370}
Sean Callanan68e44232017-09-28 20:20:25 +000010371
10372clang::ExternalASTMerger &
10373ClangASTContextForExpressions::GetMergerUnchecked() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +000010374 lldbassert(m_scratch_ast_source_up != nullptr);
10375 return m_scratch_ast_source_up->GetMergerUnchecked();
Sean Callanan68e44232017-09-28 20:20:25 +000010376}