blob: dc5d1ebe93f3e343a9edd575c59f58da1edea4a2 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ClangASTContext.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eli Friedman932197d2010-06-13 19:06:42 +000010#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011
Zachary Turner827d5d72016-12-16 04:27:00 +000012#include "llvm/Support/FormatAdapters.h"
13#include "llvm/Support/FormatVariadic.h"
14
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000015#include <mutex>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include <string>
Sean Callananfe38c852015-10-08 23:07:53 +000017#include <vector>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018
Greg Clayton6beaaa62011-01-17 03:46:26 +000019
Kate Stoneb9c1b512016-09-06 20:57:50 +000020// Clang headers like to use NDEBUG inside of them to enable/disable debug
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +000021// related features using "#ifndef NDEBUG" preprocessor blocks to do one thing
Greg Clayton6beaaa62011-01-17 03:46:26 +000022// or another. This is bad because it means that if clang was built in release
23// mode, it assumes that you are building in release mode which is not always
24// the case. You can end up with functions that are defined as empty in header
25// files when NDEBUG is not defined, and this can cause link errors with the
26// clang .a files that you have since you might be missing functions in the .a
27// file. So we have to define NDEBUG when including clang headers to avoid any
28// mismatches. This is covered by rdar://problem/8691220
29
Sean Callanan3b1d4f62011-10-26 17:46:51 +000030#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
Greg Clayton6beaaa62011-01-17 03:46:26 +000031#define LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000032#define NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000033// Need to include assert.h so it is as clang would expect it to be (disabled)
34#include <assert.h>
35#endif
36
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037#include "clang/AST/ASTContext.h"
38#include "clang/AST/ASTImporter.h"
Greg Claytonf74c4032012-12-03 18:29:55 +000039#include "clang/AST/Attr.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "clang/AST/CXXInheritance.h"
Greg Clayton8cf05932010-07-22 18:30:50 +000041#include "clang/AST/DeclObjC.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000042#include "clang/AST/DeclTemplate.h"
Greg Claytonfe689042015-11-10 17:47:04 +000043#include "clang/AST/Mangle.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044#include "clang/AST/RecordLayout.h"
45#include "clang/AST/Type.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000046#include "clang/AST/VTableBuilder.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "clang/Basic/Builtins.h"
Sean Callanan7e2863b2012-02-06 21:28:03 +000048#include "clang/Basic/Diagnostic.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049#include "clang/Basic/FileManager.h"
Sean Callanan79439e82010-11-18 02:56:27 +000050#include "clang/Basic/FileSystemOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051#include "clang/Basic/SourceManager.h"
52#include "clang/Basic/TargetInfo.h"
53#include "clang/Basic/TargetOptions.h"
54#include "clang/Frontend/FrontendOptions.h"
55#include "clang/Frontend/LangStandard.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)
657 : TypeSystem(TypeSystem::eKindClang), m_target_triple(), m_ast_ap(),
658 m_language_options_ap(), m_source_manager_ap(), m_diagnostics_engine_ap(),
659 m_target_options_rp(), m_target_info_ap(), m_identifier_table_ap(),
660 m_selector_table_ap(), m_builtins_ap(), m_callback_tag_decl(nullptr),
661 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
667//----------------------------------------------------------------------
668// Destructor
669//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000670ClangASTContext::~ClangASTContext() { Finalize(); }
671
672ConstString ClangASTContext::GetPluginNameStatic() {
673 return ConstString("clang");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000674}
675
Kate Stoneb9c1b512016-09-06 20:57:50 +0000676ConstString ClangASTContext::GetPluginName() {
677 return ClangASTContext::GetPluginNameStatic();
Greg Clayton56939cb2015-09-17 22:23:34 +0000678}
679
Kate Stoneb9c1b512016-09-06 20:57:50 +0000680uint32_t ClangASTContext::GetPluginVersion() { return 1; }
Greg Clayton56939cb2015-09-17 22:23:34 +0000681
Kate Stoneb9c1b512016-09-06 20:57:50 +0000682lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
683 lldb_private::Module *module,
684 Target *target) {
685 if (ClangASTContextSupportsLanguage(language)) {
686 ArchSpec arch;
687 if (module)
688 arch = module->GetArchitecture();
689 else if (target)
690 arch = target->GetArchitecture();
Greg Clayton56939cb2015-09-17 22:23:34 +0000691
Kate Stoneb9c1b512016-09-06 20:57:50 +0000692 if (arch.IsValid()) {
693 ArchSpec fixed_arch = arch;
694 // LLVM wants this to be set to iOS or MacOSX; if we're working on
695 // a bare-boards type image, change the triple for llvm's benefit.
696 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
697 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) {
698 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
699 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
700 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) {
701 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
702 } else {
703 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
Greg Clayton56939cb2015-09-17 22:23:34 +0000704 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000705 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000706
Kate Stoneb9c1b512016-09-06 20:57:50 +0000707 if (module) {
708 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
709 if (ast_sp) {
710 ast_sp->SetArchitecture(fixed_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000711 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000712 return ast_sp;
713 } else if (target && target->IsValid()) {
714 std::shared_ptr<ClangASTContextForExpressions> ast_sp(
715 new ClangASTContextForExpressions(*target));
716 if (ast_sp) {
717 ast_sp->SetArchitecture(fixed_arch);
718 ast_sp->m_scratch_ast_source_ap.reset(
719 new ClangASTSource(target->shared_from_this()));
Sean Callanan68e44232017-09-28 20:20:25 +0000720 lldbassert(ast_sp->getFileManager());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000721 ast_sp->m_scratch_ast_source_ap->InstallASTContext(
Sean Callanan68e44232017-09-28 20:20:25 +0000722 *ast_sp->getASTContext(), *ast_sp->getFileManager(), true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000723 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
724 ast_sp->m_scratch_ast_source_ap->CreateProxy());
725 ast_sp->SetExternalSource(proxy_ast_source);
726 return ast_sp;
727 }
728 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000729 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000730 }
731 return lldb::TypeSystemSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000732}
733
Kate Stoneb9c1b512016-09-06 20:57:50 +0000734void ClangASTContext::EnumerateSupportedLanguages(
735 std::set<lldb::LanguageType> &languages_for_types,
736 std::set<lldb::LanguageType> &languages_for_expressions) {
737 static std::vector<lldb::LanguageType> s_supported_languages_for_types(
738 {lldb::eLanguageTypeC89, lldb::eLanguageTypeC, lldb::eLanguageTypeC11,
739 lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeC99,
740 lldb::eLanguageTypeObjC, lldb::eLanguageTypeObjC_plus_plus,
741 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
742 lldb::eLanguageTypeC11, lldb::eLanguageTypeC_plus_plus_14});
743
744 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions(
745 {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC_plus_plus,
746 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
747 lldb::eLanguageTypeC_plus_plus_14});
748
749 languages_for_types.insert(s_supported_languages_for_types.begin(),
750 s_supported_languages_for_types.end());
751 languages_for_expressions.insert(
752 s_supported_languages_for_expressions.begin(),
753 s_supported_languages_for_expressions.end());
Enrico Granata5d84a692014-08-19 21:46:37 +0000754}
755
Kate Stoneb9c1b512016-09-06 20:57:50 +0000756void ClangASTContext::Initialize() {
757 PluginManager::RegisterPlugin(GetPluginNameStatic(),
758 "clang base AST context plug-in",
759 CreateInstance, EnumerateSupportedLanguages);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000760}
761
Kate Stoneb9c1b512016-09-06 20:57:50 +0000762void ClangASTContext::Terminate() {
763 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000764}
765
Kate Stoneb9c1b512016-09-06 20:57:50 +0000766void ClangASTContext::Finalize() {
767 if (m_ast_ap.get()) {
768 GetASTMap().Erase(m_ast_ap.get());
769 if (!m_ast_owned)
770 m_ast_ap.release();
771 }
772
773 m_builtins_ap.reset();
774 m_selector_table_ap.reset();
775 m_identifier_table_ap.reset();
776 m_target_info_ap.reset();
777 m_target_options_rp.reset();
778 m_diagnostics_engine_ap.reset();
779 m_source_manager_ap.reset();
780 m_language_options_ap.reset();
781 m_ast_ap.reset();
782 m_scratch_ast_source_ap.reset();
783}
784
785void ClangASTContext::Clear() {
786 m_ast_ap.reset();
787 m_language_options_ap.reset();
788 m_source_manager_ap.reset();
789 m_diagnostics_engine_ap.reset();
790 m_target_options_rp.reset();
791 m_target_info_ap.reset();
792 m_identifier_table_ap.reset();
793 m_selector_table_ap.reset();
794 m_builtins_ap.reset();
795 m_pointer_byte_size = 0;
796}
797
798const char *ClangASTContext::GetTargetTriple() {
799 return m_target_triple.c_str();
800}
801
802void ClangASTContext::SetTargetTriple(const char *target_triple) {
803 Clear();
804 m_target_triple.assign(target_triple);
805}
806
807void ClangASTContext::SetArchitecture(const ArchSpec &arch) {
808 SetTargetTriple(arch.GetTriple().str().c_str());
809}
810
811bool ClangASTContext::HasExternalSource() {
812 ASTContext *ast = getASTContext();
813 if (ast)
814 return ast->getExternalSource() != nullptr;
815 return false;
816}
817
818void ClangASTContext::SetExternalSource(
819 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap) {
820 ASTContext *ast = getASTContext();
821 if (ast) {
822 ast->setExternalSource(ast_source_ap);
823 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000824 }
825}
826
827void ClangASTContext::RemoveExternalSource() {
828 ASTContext *ast = getASTContext();
829
830 if (ast) {
831 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
832 ast->setExternalSource(empty_ast_source_ap);
833 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000834 }
835}
836
837void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) {
838 if (!m_ast_owned) {
839 m_ast_ap.release();
840 }
841 m_ast_owned = false;
842 m_ast_ap.reset(ast_ctx);
843 GetASTMap().Insert(ast_ctx, this);
844}
845
846ASTContext *ClangASTContext::getASTContext() {
847 if (m_ast_ap.get() == nullptr) {
848 m_ast_owned = true;
849 m_ast_ap.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
850 *getIdentifierTable(), *getSelectorTable(),
851 *getBuiltinContext()));
852
853 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
854
855 // This can be NULL if we don't know anything about the architecture or if
Adrian Prantl05097242018-04-30 16:49:04 +0000856 // the target for an architecture isn't enabled in the llvm/clang that we
857 // built
Kate Stoneb9c1b512016-09-06 20:57:50 +0000858 TargetInfo *target_info = getTargetInfo();
859 if (target_info)
860 m_ast_ap->InitBuiltinTypes(*target_info);
861
862 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) {
863 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
864 // m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000865 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000866
867 GetASTMap().Insert(m_ast_ap.get(), this);
868
869 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap(
870 new ClangExternalASTSourceCallbacks(
871 ClangASTContext::CompleteTagDecl,
872 ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
873 ClangASTContext::LayoutRecordType, this));
874 SetExternalSource(ast_source_ap);
875 }
876 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000877}
878
Kate Stoneb9c1b512016-09-06 20:57:50 +0000879ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
880 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
881 return clang_ast;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000882}
883
Kate Stoneb9c1b512016-09-06 20:57:50 +0000884Builtin::Context *ClangASTContext::getBuiltinContext() {
885 if (m_builtins_ap.get() == nullptr)
886 m_builtins_ap.reset(new Builtin::Context());
887 return m_builtins_ap.get();
Sean Callanan79439e82010-11-18 02:56:27 +0000888}
889
Kate Stoneb9c1b512016-09-06 20:57:50 +0000890IdentifierTable *ClangASTContext::getIdentifierTable() {
891 if (m_identifier_table_ap.get() == nullptr)
892 m_identifier_table_ap.reset(
893 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
894 return m_identifier_table_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000895}
896
Kate Stoneb9c1b512016-09-06 20:57:50 +0000897LangOptions *ClangASTContext::getLanguageOptions() {
898 if (m_language_options_ap.get() == nullptr) {
899 m_language_options_ap.reset(new LangOptions());
Richard Smith8186cd42017-04-26 22:10:53 +0000900 ParseLangArgs(*m_language_options_ap, InputKind::ObjCXX, GetTargetTriple());
901 // InitializeLangOptions(*m_language_options_ap, InputKind::ObjCXX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000902 }
903 return m_language_options_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000904}
905
Kate Stoneb9c1b512016-09-06 20:57:50 +0000906SelectorTable *ClangASTContext::getSelectorTable() {
907 if (m_selector_table_ap.get() == nullptr)
908 m_selector_table_ap.reset(new SelectorTable());
909 return m_selector_table_ap.get();
Greg Claytonfe689042015-11-10 17:47:04 +0000910}
911
Kate Stoneb9c1b512016-09-06 20:57:50 +0000912clang::FileManager *ClangASTContext::getFileManager() {
913 if (m_file_manager_ap.get() == nullptr) {
914 clang::FileSystemOptions file_system_options;
915 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
916 }
917 return m_file_manager_ap.get();
918}
919
920clang::SourceManager *ClangASTContext::getSourceManager() {
921 if (m_source_manager_ap.get() == nullptr)
922 m_source_manager_ap.reset(
923 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
924 return m_source_manager_ap.get();
925}
926
927clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() {
928 if (m_diagnostics_engine_ap.get() == nullptr) {
929 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
930 m_diagnostics_engine_ap.reset(
931 new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
932 }
933 return m_diagnostics_engine_ap.get();
934}
935
936clang::MangleContext *ClangASTContext::getMangleContext() {
937 if (m_mangle_ctx_ap.get() == nullptr)
938 m_mangle_ctx_ap.reset(getASTContext()->createMangleContext());
939 return m_mangle_ctx_ap.get();
940}
941
942class NullDiagnosticConsumer : public DiagnosticConsumer {
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000943public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000944 NullDiagnosticConsumer() {
945 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
946 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000947
Kate Stoneb9c1b512016-09-06 20:57:50 +0000948 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
949 const clang::Diagnostic &info) {
950 if (m_log) {
951 llvm::SmallVector<char, 32> diag_str(10);
952 info.FormatDiagnostic(diag_str);
953 diag_str.push_back('\0');
954 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000955 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000956 }
957
958 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
959 return new NullDiagnosticConsumer();
960 }
961
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000962private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000963 Log *m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000964};
965
Kate Stoneb9c1b512016-09-06 20:57:50 +0000966DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() {
967 if (m_diagnostic_consumer_ap.get() == nullptr)
968 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
969
970 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000971}
972
Kate Stoneb9c1b512016-09-06 20:57:50 +0000973std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
974 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty()) {
975 m_target_options_rp = std::make_shared<clang::TargetOptions>();
976 if (m_target_options_rp.get() != nullptr)
977 m_target_options_rp->Triple = m_target_triple;
978 }
979 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000980}
981
Kate Stoneb9c1b512016-09-06 20:57:50 +0000982TargetInfo *ClangASTContext::getTargetInfo() {
983 // target_triple should be something like "x86_64-apple-macosx"
984 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty())
985 m_target_info_ap.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
986 getTargetOptions()));
987 return m_target_info_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000988}
989
990#pragma mark Basic Types
991
Kate Stoneb9c1b512016-09-06 20:57:50 +0000992static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
993 ASTContext *ast, QualType qual_type) {
994 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000995 return qual_type_bit_size == bit_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000996}
Greg Clayton56939cb2015-09-17 22:23:34 +0000997
Greg Claytona1e5dc82015-08-11 22:53:00 +0000998CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000999ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
1000 size_t bit_size) {
1001 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
1002 getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001003}
1004
Kate Stoneb9c1b512016-09-06 20:57:50 +00001005CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
1006 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
1007 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001008 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001009 switch (encoding) {
1010 case eEncodingInvalid:
1011 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1012 return CompilerType(ast, ast->VoidPtrTy);
1013 break;
1014
1015 case eEncodingUint:
1016 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1017 return CompilerType(ast, ast->UnsignedCharTy);
1018 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1019 return CompilerType(ast, ast->UnsignedShortTy);
1020 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1021 return CompilerType(ast, ast->UnsignedIntTy);
1022 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1023 return CompilerType(ast, ast->UnsignedLongTy);
1024 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1025 return CompilerType(ast, ast->UnsignedLongLongTy);
1026 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1027 return CompilerType(ast, ast->UnsignedInt128Ty);
1028 break;
1029
1030 case eEncodingSint:
1031 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1032 return CompilerType(ast, ast->SignedCharTy);
1033 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1034 return CompilerType(ast, ast->ShortTy);
1035 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1036 return CompilerType(ast, ast->IntTy);
1037 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1038 return CompilerType(ast, ast->LongTy);
1039 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1040 return CompilerType(ast, ast->LongLongTy);
1041 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1042 return CompilerType(ast, ast->Int128Ty);
1043 break;
1044
1045 case eEncodingIEEE754:
1046 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1047 return CompilerType(ast, ast->FloatTy);
1048 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1049 return CompilerType(ast, ast->DoubleTy);
1050 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1051 return CompilerType(ast, ast->LongDoubleTy);
1052 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1053 return CompilerType(ast, ast->HalfTy);
1054 break;
1055
1056 case eEncodingVector:
1057 // Sanity check that bit_size is a multiple of 8's.
1058 if (bit_size && !(bit_size & 0x7u))
1059 return CompilerType(
1060 ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8));
1061 break;
1062 }
1063
1064 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001065}
1066
Greg Clayton57ee3062013-07-11 22:46:58 +00001067lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00001068ClangASTContext::GetBasicTypeEnumeration(const ConstString &name) {
1069 if (name) {
1070 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
1071 static TypeNameToBasicTypeMap g_type_map;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +00001072 static llvm::once_flag g_once_flag;
1073 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001074 // "void"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001075 g_type_map.Append(ConstString("void"), eBasicTypeVoid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001076
1077 // "char"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001078 g_type_map.Append(ConstString("char"), eBasicTypeChar);
1079 g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar);
1080 g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar);
1081 g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar);
1082 g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar);
1083 g_type_map.Append(ConstString("unsigned wchar_t"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001084 eBasicTypeUnsignedWChar);
1085 // "short"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001086 g_type_map.Append(ConstString("short"), eBasicTypeShort);
1087 g_type_map.Append(ConstString("short int"), eBasicTypeShort);
1088 g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort);
1089 g_type_map.Append(ConstString("unsigned short int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001090 eBasicTypeUnsignedShort);
1091
1092 // "int"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001093 g_type_map.Append(ConstString("int"), eBasicTypeInt);
1094 g_type_map.Append(ConstString("signed int"), eBasicTypeInt);
1095 g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt);
1096 g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001097
1098 // "long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001099 g_type_map.Append(ConstString("long"), eBasicTypeLong);
1100 g_type_map.Append(ConstString("long int"), eBasicTypeLong);
1101 g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong);
1102 g_type_map.Append(ConstString("unsigned long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001103 eBasicTypeUnsignedLong);
1104
1105 // "long long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001106 g_type_map.Append(ConstString("long long"), eBasicTypeLongLong);
1107 g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong);
1108 g_type_map.Append(ConstString("unsigned long long"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001109 eBasicTypeUnsignedLongLong);
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001110 g_type_map.Append(ConstString("unsigned long long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001111 eBasicTypeUnsignedLongLong);
1112
1113 // "int128"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001114 g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128);
1115 g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001116
1117 // Miscellaneous
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001118 g_type_map.Append(ConstString("bool"), eBasicTypeBool);
1119 g_type_map.Append(ConstString("float"), eBasicTypeFloat);
1120 g_type_map.Append(ConstString("double"), eBasicTypeDouble);
1121 g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble);
1122 g_type_map.Append(ConstString("id"), eBasicTypeObjCID);
1123 g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel);
1124 g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001125 g_type_map.Sort();
1126 });
1127
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001128 return g_type_map.Find(name, eBasicTypeInvalid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001129 }
1130 return eBasicTypeInvalid;
Greg Clayton57ee3062013-07-11 22:46:58 +00001131}
1132
Kate Stoneb9c1b512016-09-06 20:57:50 +00001133CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1134 const ConstString &name) {
1135 if (ast) {
1136 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
1137 return ClangASTContext::GetBasicType(ast, basic_type);
1138 }
1139 return CompilerType();
1140}
1141
1142uint32_t ClangASTContext::GetPointerByteSize() {
1143 if (m_pointer_byte_size == 0)
Adrian Prantld963a7c2019-01-15 18:07:52 +00001144 if (auto size = GetBasicType(lldb::eBasicTypeVoid)
1145 .GetPointerType()
1146 .GetByteSize(nullptr))
1147 m_pointer_byte_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001148 return m_pointer_byte_size;
1149}
1150
1151CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
1152 return GetBasicType(getASTContext(), basic_type);
1153}
1154
1155CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1156 lldb::BasicType basic_type) {
1157 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001158 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001159 lldb::opaque_compiler_type_t clang_type =
1160 GetOpaqueCompilerType(ast, basic_type);
1161
1162 if (clang_type)
1163 return CompilerType(GetASTContext(ast), clang_type);
1164 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +00001165}
1166
Kate Stoneb9c1b512016-09-06 20:57:50 +00001167CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
1168 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
1169 ASTContext *ast = getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001170
Kate Stoneb9c1b512016-09-06 20:57:50 +00001171#define streq(a, b) strcmp(a, b) == 0
1172 assert(ast != nullptr);
1173 if (ast) {
1174 switch (dw_ate) {
1175 default:
1176 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001177
Kate Stoneb9c1b512016-09-06 20:57:50 +00001178 case DW_ATE_address:
1179 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1180 return CompilerType(ast, ast->VoidPtrTy);
1181 break;
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001182
Kate Stoneb9c1b512016-09-06 20:57:50 +00001183 case DW_ATE_boolean:
1184 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
1185 return CompilerType(ast, ast->BoolTy);
1186 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1187 return CompilerType(ast, ast->UnsignedCharTy);
1188 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1189 return CompilerType(ast, ast->UnsignedShortTy);
1190 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1191 return CompilerType(ast, ast->UnsignedIntTy);
1192 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001193
Kate Stoneb9c1b512016-09-06 20:57:50 +00001194 case DW_ATE_lo_user:
1195 // This has been seen to mean DW_AT_complex_integer
1196 if (type_name) {
1197 if (::strstr(type_name, "complex")) {
1198 CompilerType complex_int_clang_type =
1199 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1200 bit_size / 2);
1201 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1202 complex_int_clang_type)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001203 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001204 }
1205 break;
1206
1207 case DW_ATE_complex_float:
1208 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
1209 return CompilerType(ast, ast->FloatComplexTy);
1210 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
1211 return CompilerType(ast, ast->DoubleComplexTy);
1212 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
1213 return CompilerType(ast, ast->LongDoubleComplexTy);
1214 else {
1215 CompilerType complex_float_clang_type =
1216 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1217 bit_size / 2);
1218 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1219 complex_float_clang_type)));
1220 }
1221 break;
1222
1223 case DW_ATE_float:
1224 if (streq(type_name, "float") &&
1225 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1226 return CompilerType(ast, ast->FloatTy);
1227 if (streq(type_name, "double") &&
1228 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1229 return CompilerType(ast, ast->DoubleTy);
1230 if (streq(type_name, "long double") &&
1231 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1232 return CompilerType(ast, ast->LongDoubleTy);
1233 // Fall back to not requiring a name match
1234 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1235 return CompilerType(ast, ast->FloatTy);
1236 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1237 return CompilerType(ast, ast->DoubleTy);
1238 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1239 return CompilerType(ast, ast->LongDoubleTy);
1240 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1241 return CompilerType(ast, ast->HalfTy);
1242 break;
1243
1244 case DW_ATE_signed:
1245 if (type_name) {
1246 if (streq(type_name, "wchar_t") &&
1247 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1248 (getTargetInfo() &&
1249 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1250 return CompilerType(ast, ast->WCharTy);
1251 if (streq(type_name, "void") &&
1252 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
1253 return CompilerType(ast, ast->VoidTy);
1254 if (strstr(type_name, "long long") &&
1255 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1256 return CompilerType(ast, ast->LongLongTy);
1257 if (strstr(type_name, "long") &&
1258 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1259 return CompilerType(ast, ast->LongTy);
1260 if (strstr(type_name, "short") &&
1261 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1262 return CompilerType(ast, ast->ShortTy);
1263 if (strstr(type_name, "char")) {
1264 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1265 return CompilerType(ast, ast->CharTy);
1266 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1267 return CompilerType(ast, ast->SignedCharTy);
1268 }
1269 if (strstr(type_name, "int")) {
1270 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1271 return CompilerType(ast, ast->IntTy);
1272 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1273 return CompilerType(ast, ast->Int128Ty);
1274 }
1275 }
1276 // We weren't able to match up a type name, just search by size
1277 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1278 return CompilerType(ast, ast->CharTy);
1279 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1280 return CompilerType(ast, ast->ShortTy);
1281 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1282 return CompilerType(ast, ast->IntTy);
1283 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1284 return CompilerType(ast, ast->LongTy);
1285 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1286 return CompilerType(ast, ast->LongLongTy);
1287 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1288 return CompilerType(ast, ast->Int128Ty);
1289 break;
1290
1291 case DW_ATE_signed_char:
1292 if (ast->getLangOpts().CharIsSigned && type_name &&
1293 streq(type_name, "char")) {
1294 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1295 return CompilerType(ast, ast->CharTy);
1296 }
1297 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1298 return CompilerType(ast, ast->SignedCharTy);
1299 break;
1300
1301 case DW_ATE_unsigned:
1302 if (type_name) {
1303 if (streq(type_name, "wchar_t")) {
1304 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1305 if (!(getTargetInfo() &&
1306 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1307 return CompilerType(ast, ast->WCharTy);
1308 }
1309 }
1310 if (strstr(type_name, "long long")) {
1311 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1312 return CompilerType(ast, ast->UnsignedLongLongTy);
1313 } else if (strstr(type_name, "long")) {
1314 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1315 return CompilerType(ast, ast->UnsignedLongTy);
1316 } else if (strstr(type_name, "short")) {
1317 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1318 return CompilerType(ast, ast->UnsignedShortTy);
1319 } else if (strstr(type_name, "char")) {
1320 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1321 return CompilerType(ast, ast->UnsignedCharTy);
1322 } else if (strstr(type_name, "int")) {
1323 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1324 return CompilerType(ast, ast->UnsignedIntTy);
1325 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1326 return CompilerType(ast, ast->UnsignedInt128Ty);
1327 }
1328 }
1329 // We weren't able to match up a type name, just search by size
1330 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1331 return CompilerType(ast, ast->UnsignedCharTy);
1332 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1333 return CompilerType(ast, ast->UnsignedShortTy);
1334 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1335 return CompilerType(ast, ast->UnsignedIntTy);
1336 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1337 return CompilerType(ast, ast->UnsignedLongTy);
1338 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1339 return CompilerType(ast, ast->UnsignedLongLongTy);
1340 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1341 return CompilerType(ast, ast->UnsignedInt128Ty);
1342 break;
1343
1344 case DW_ATE_unsigned_char:
1345 if (!ast->getLangOpts().CharIsSigned && type_name &&
1346 streq(type_name, "char")) {
1347 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1348 return CompilerType(ast, ast->CharTy);
1349 }
1350 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1351 return CompilerType(ast, ast->UnsignedCharTy);
1352 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1353 return CompilerType(ast, ast->UnsignedShortTy);
1354 break;
1355
1356 case DW_ATE_imaginary_float:
1357 break;
1358
1359 case DW_ATE_UTF:
1360 if (type_name) {
1361 if (streq(type_name, "char16_t")) {
1362 return CompilerType(ast, ast->Char16Ty);
1363 } else if (streq(type_name, "char32_t")) {
1364 return CompilerType(ast, ast->Char32Ty);
1365 }
1366 }
1367 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001368 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001369 }
1370 // This assert should fire for anything that we don't catch above so we know
1371 // to fix any issues we run into.
1372 if (type_name) {
1373 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1374 "DW_TAG_base_type '%s' encoded with "
1375 "DW_ATE = 0x%x, bit_size = %u\n",
1376 type_name, dw_ate, bit_size);
1377 } else {
1378 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1379 "DW_TAG_base_type encoded with "
1380 "DW_ATE = 0x%x, bit_size = %u\n",
1381 dw_ate, bit_size);
1382 }
1383 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001384}
1385
Kate Stoneb9c1b512016-09-06 20:57:50 +00001386CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) {
1387 if (ast)
1388 return CompilerType(ast, ast->UnknownAnyTy);
1389 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001390}
1391
Kate Stoneb9c1b512016-09-06 20:57:50 +00001392CompilerType ClangASTContext::GetCStringType(bool is_const) {
1393 ASTContext *ast = getASTContext();
1394 QualType char_type(ast->CharTy);
1395
1396 if (is_const)
1397 char_type.addConst();
1398
1399 return CompilerType(ast, ast->getPointerType(char_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001400}
1401
Zachary Turner115209e2018-11-05 19:25:39 +00001402clang::DeclContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001403ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1404 return ast->getTranslationUnitDecl();
Sean Callanan09ab4b72011-11-30 22:11:59 +00001405}
1406
Kate Stoneb9c1b512016-09-06 20:57:50 +00001407clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1408 clang::Decl *source_decl) {
1409 FileSystemOptions file_system_options;
1410 FileManager file_manager(file_system_options);
1411 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1412
1413 return importer.Import(source_decl);
Greg Clayton526e5af2010-11-13 03:52:47 +00001414}
1415
Kate Stoneb9c1b512016-09-06 20:57:50 +00001416bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1417 bool ignore_qualifiers) {
1418 ClangASTContext *ast =
1419 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1420 if (!ast || ast != type2.GetTypeSystem())
1421 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001422
Kate Stoneb9c1b512016-09-06 20:57:50 +00001423 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1424 return true;
Greg Clayton55995eb2012-04-06 17:38:55 +00001425
Kate Stoneb9c1b512016-09-06 20:57:50 +00001426 QualType type1_qual = ClangUtil::GetQualType(type1);
1427 QualType type2_qual = ClangUtil::GetQualType(type2);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001428
Kate Stoneb9c1b512016-09-06 20:57:50 +00001429 if (ignore_qualifiers) {
1430 type1_qual = type1_qual.getUnqualifiedType();
1431 type2_qual = type2_qual.getUnqualifiedType();
1432 }
1433
1434 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001435}
1436
Kate Stoneb9c1b512016-09-06 20:57:50 +00001437CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1438 if (clang::ObjCInterfaceDecl *interface_decl =
1439 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1440 return GetTypeForDecl(interface_decl);
1441 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1442 return GetTypeForDecl(tag_decl);
1443 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001444}
1445
Kate Stoneb9c1b512016-09-06 20:57:50 +00001446CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001447 // No need to call the getASTContext() accessor (which can create the AST if
1448 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001449 // AST if our AST didn't already exist...
1450 ASTContext *ast = &decl->getASTContext();
1451 if (ast)
1452 return CompilerType(ast, ast->getTagDeclType(decl));
1453 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001454}
1455
Kate Stoneb9c1b512016-09-06 20:57:50 +00001456CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001457 // No need to call the getASTContext() accessor (which can create the AST if
1458 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001459 // AST if our AST didn't already exist...
1460 ASTContext *ast = &decl->getASTContext();
1461 if (ast)
1462 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1463 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001464}
1465
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001466#pragma mark Structure, Unions, Classes
1467
Kate Stoneb9c1b512016-09-06 20:57:50 +00001468CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx,
1469 AccessType access_type,
1470 const char *name, int kind,
1471 LanguageType language,
1472 ClangASTMetadata *metadata) {
1473 ASTContext *ast = getASTContext();
1474 assert(ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001475
Kate Stoneb9c1b512016-09-06 20:57:50 +00001476 if (decl_ctx == nullptr)
1477 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton9e409562010-07-28 02:04:09 +00001478
Kate Stoneb9c1b512016-09-06 20:57:50 +00001479 if (language == eLanguageTypeObjC ||
1480 language == eLanguageTypeObjC_plus_plus) {
1481 bool isForwardDecl = true;
1482 bool isInternal = false;
1483 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1484 }
Greg Clayton9e409562010-07-28 02:04:09 +00001485
Kate Stoneb9c1b512016-09-06 20:57:50 +00001486 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
Adrian Prantl05097242018-04-30 16:49:04 +00001487 // we will need to update this code. I was told to currently always use the
1488 // CXXRecordDecl class since we often don't know from debug information if
1489 // something is struct or a class, so we default to always use the more
Kate Stoneb9c1b512016-09-06 20:57:50 +00001490 // complete definition just in case.
Greg Claytonc4ffd662013-03-08 01:37:30 +00001491
Kate Stoneb9c1b512016-09-06 20:57:50 +00001492 bool is_anonymous = (!name) || (!name[0]);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001493
Kate Stoneb9c1b512016-09-06 20:57:50 +00001494 CXXRecordDecl *decl = CXXRecordDecl::Create(
1495 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1496 SourceLocation(), is_anonymous ? nullptr : &ast->Idents.get(name));
1497
1498 if (is_anonymous)
1499 decl->setAnonymousStructOrUnion(true);
1500
1501 if (decl) {
1502 if (metadata)
1503 SetMetadata(ast, decl, *metadata);
1504
1505 if (access_type != eAccessNone)
1506 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1507
1508 if (decl_ctx)
1509 decl_ctx->addDecl(decl);
1510
1511 return CompilerType(ast, ast->getTagDeclType(decl));
1512 }
1513 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001514}
1515
Sean Callanan09e91ac2017-05-11 22:08:05 +00001516namespace {
1517 bool IsValueParam(const clang::TemplateArgument &argument) {
1518 return argument.getKind() == TemplateArgument::Integral;
1519 }
1520}
1521
Kate Stoneb9c1b512016-09-06 20:57:50 +00001522static TemplateParameterList *CreateTemplateParameterList(
1523 ASTContext *ast,
1524 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1525 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1526 const bool parameter_pack = false;
1527 const bool is_typename = false;
1528 const unsigned depth = 0;
Sean Callanan09e91ac2017-05-11 22:08:05 +00001529 const size_t num_template_params = template_param_infos.args.size();
1530 DeclContext *const decl_context =
1531 ast->getTranslationUnitDecl(); // Is this the right decl context?,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001532 for (size_t i = 0; i < num_template_params; ++i) {
1533 const char *name = template_param_infos.names[i];
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001534
Kate Stoneb9c1b512016-09-06 20:57:50 +00001535 IdentifierInfo *identifier_info = nullptr;
1536 if (name && name[0])
1537 identifier_info = &ast->Idents.get(name);
Sean Callanan09e91ac2017-05-11 22:08:05 +00001538 if (IsValueParam(template_param_infos.args[i])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001539 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001540 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001541 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1542 template_param_infos.args[i].getIntegralType(), parameter_pack,
1543 nullptr));
1544
1545 } else {
1546 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001547 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001548 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1549 is_typename, parameter_pack));
1550 }
1551 }
Eugene Zemtsova9d928c2017-09-29 03:15:08 +00001552
Sean Callanan09e91ac2017-05-11 22:08:05 +00001553 if (template_param_infos.packed_args &&
1554 template_param_infos.packed_args->args.size()) {
1555 IdentifierInfo *identifier_info = nullptr;
1556 if (template_param_infos.pack_name && template_param_infos.pack_name[0])
1557 identifier_info = &ast->Idents.get(template_param_infos.pack_name);
1558 const bool parameter_pack_true = true;
1559 if (IsValueParam(template_param_infos.packed_args->args[0])) {
1560 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1561 *ast, decl_context,
1562 SourceLocation(), SourceLocation(), depth, num_template_params,
1563 identifier_info,
1564 template_param_infos.packed_args->args[0].getIntegralType(),
1565 parameter_pack_true, nullptr));
1566 } else {
1567 template_param_decls.push_back(TemplateTypeParmDecl::Create(
1568 *ast, decl_context,
1569 SourceLocation(), SourceLocation(), depth, num_template_params,
1570 identifier_info,
1571 is_typename, parameter_pack_true));
1572 }
1573 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001574 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1575 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1576 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1577 SourceLocation(), requires_clause);
1578 return template_param_list;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001579}
1580
Kate Stoneb9c1b512016-09-06 20:57:50 +00001581clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1582 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1583 const char *name, const TemplateParameterInfos &template_param_infos) {
Adrian Prantld8f460e2018-05-02 16:55:16 +00001584 // /// Create a function template node.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001585 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001586
Kate Stoneb9c1b512016-09-06 20:57:50 +00001587 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001588
Kate Stoneb9c1b512016-09-06 20:57:50 +00001589 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1590 ast, template_param_infos, template_param_decls);
1591 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1592 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1593 template_param_list, func_decl);
1594
1595 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1596 i < template_param_decl_count; ++i) {
1597 // TODO: verify which decl context we should put template_param_decls into..
1598 template_param_decls[i]->setDeclContext(func_decl);
1599 }
1600
1601 return func_tmpl_decl;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001602}
1603
Kate Stoneb9c1b512016-09-06 20:57:50 +00001604void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1605 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1606 const TemplateParameterInfos &infos) {
1607 TemplateArgumentList template_args(TemplateArgumentList::OnStack, infos.args);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001608
Kate Stoneb9c1b512016-09-06 20:57:50 +00001609 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, &template_args,
1610 nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001611}
1612
Kate Stoneb9c1b512016-09-06 20:57:50 +00001613ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1614 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1615 int kind, const TemplateParameterInfos &template_param_infos) {
1616 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001617
Kate Stoneb9c1b512016-09-06 20:57:50 +00001618 ClassTemplateDecl *class_template_decl = nullptr;
1619 if (decl_ctx == nullptr)
1620 decl_ctx = ast->getTranslationUnitDecl();
Greg Claytonf0705c82011-10-22 03:33:13 +00001621
Kate Stoneb9c1b512016-09-06 20:57:50 +00001622 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1623 DeclarationName decl_name(&identifier_info);
Greg Claytonf0705c82011-10-22 03:33:13 +00001624
Kate Stoneb9c1b512016-09-06 20:57:50 +00001625 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Greg Claytonf0705c82011-10-22 03:33:13 +00001626
Kate Stoneb9c1b512016-09-06 20:57:50 +00001627 for (NamedDecl *decl : result) {
1628 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001629 if (class_template_decl)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001630 return class_template_decl;
1631 }
1632
1633 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1634
1635 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1636 ast, template_param_infos, template_param_decls);
1637
1638 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1639 *ast, (TagDecl::TagKind)kind,
1640 decl_ctx, // What decl context do we use here? TU? The actual decl
1641 // context?
1642 SourceLocation(), SourceLocation(), &identifier_info);
1643
1644 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1645 i < template_param_decl_count; ++i) {
1646 template_param_decls[i]->setDeclContext(template_cxx_decl);
1647 }
1648
1649 // With templated classes, we say that a class is templated with
1650 // specializations, but that the bare class has no functions.
1651 // template_cxx_decl->startDefinition();
1652 // template_cxx_decl->completeDefinition();
1653
1654 class_template_decl = ClassTemplateDecl::Create(
1655 *ast,
1656 decl_ctx, // What decl context do we use here? TU? The actual decl
1657 // context?
Pavel Labath4294de32017-01-12 10:44:16 +00001658 SourceLocation(), decl_name, template_param_list, template_cxx_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001659
1660 if (class_template_decl) {
1661 if (access_type != eAccessNone)
1662 class_template_decl->setAccess(
1663 ConvertAccessTypeToAccessSpecifier(access_type));
1664
1665 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1666 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1667
1668 decl_ctx->addDecl(class_template_decl);
1669
Sean Callanan5e9e1992011-10-26 01:06:27 +00001670#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001671 VerifyDecl(class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001672#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001673 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001674
Kate Stoneb9c1b512016-09-06 20:57:50 +00001675 return class_template_decl;
Greg Claytonf0705c82011-10-22 03:33:13 +00001676}
1677
Frederic Rissf4e7e522018-04-02 16:18:32 +00001678TemplateTemplateParmDecl *
1679ClangASTContext::CreateTemplateTemplateParmDecl(const char *template_name) {
1680 ASTContext *ast = getASTContext();
1681
1682 auto *decl_ctx = ast->getTranslationUnitDecl();
1683
1684 IdentifierInfo &identifier_info = ast->Idents.get(template_name);
1685 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1686
1687 ClangASTContext::TemplateParameterInfos template_param_infos;
1688 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1689 ast, template_param_infos, template_param_decls);
1690
1691 // LLDB needs to create those decls only to be able to display a
Adrian Prantl05097242018-04-30 16:49:04 +00001692 // type that includes a template template argument. Only the name matters for
1693 // this purpose, so we use dummy values for the other characterisitcs of the
1694 // type.
Frederic Rissf4e7e522018-04-02 16:18:32 +00001695 return TemplateTemplateParmDecl::Create(
1696 *ast, decl_ctx, SourceLocation(),
1697 /*Depth*/ 0, /*Position*/ 0,
1698 /*IsParameterPack*/ false, &identifier_info, template_param_list);
1699}
1700
Greg Claytonf0705c82011-10-22 03:33:13 +00001701ClassTemplateSpecializationDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001702ClangASTContext::CreateClassTemplateSpecializationDecl(
1703 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1704 const TemplateParameterInfos &template_param_infos) {
1705 ASTContext *ast = getASTContext();
Sean Callanan09e91ac2017-05-11 22:08:05 +00001706 llvm::SmallVector<clang::TemplateArgument, 2> args(
1707 template_param_infos.args.size() +
1708 (template_param_infos.packed_args ? 1 : 0));
1709 std::copy(template_param_infos.args.begin(), template_param_infos.args.end(),
1710 args.begin());
1711 if (template_param_infos.packed_args) {
1712 args[args.size() - 1] = TemplateArgument::CreatePackCopy(
1713 *ast, template_param_infos.packed_args->args);
1714 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001715 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1716 ClassTemplateSpecializationDecl::Create(
1717 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
Sean Callanan09e91ac2017-05-11 22:08:05 +00001718 SourceLocation(), class_template_decl, args,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001719 nullptr);
1720
1721 class_template_specialization_decl->setSpecializationKind(
1722 TSK_ExplicitSpecialization);
1723
1724 return class_template_specialization_decl;
1725}
1726
1727CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1728 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1729 if (class_template_specialization_decl) {
Greg Claytonf0705c82011-10-22 03:33:13 +00001730 ASTContext *ast = getASTContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001731 if (ast)
1732 return CompilerType(
1733 ast, ast->getTagDeclType(class_template_specialization_decl));
1734 }
1735 return CompilerType();
Greg Claytonf0705c82011-10-22 03:33:13 +00001736}
1737
Kate Stoneb9c1b512016-09-06 20:57:50 +00001738static inline bool check_op_param(bool is_method,
1739 clang::OverloadedOperatorKind op_kind,
1740 bool unary, bool binary,
1741 uint32_t num_params) {
1742 // Special-case call since it can take any number of operands
1743 if (op_kind == OO_Call)
1744 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001745
Kate Stoneb9c1b512016-09-06 20:57:50 +00001746 // The parameter count doesn't include "this"
1747 if (is_method)
1748 ++num_params;
1749 if (num_params == 1)
1750 return unary;
1751 if (num_params == 2)
1752 return binary;
1753 else
Greg Clayton090d0982011-06-19 03:43:27 +00001754 return false;
1755}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001756
Kate Stoneb9c1b512016-09-06 20:57:50 +00001757bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1758 bool is_method, clang::OverloadedOperatorKind op_kind,
1759 uint32_t num_params) {
1760 switch (op_kind) {
1761 default:
1762 break;
1763 // C++ standard allows any number of arguments to new/delete
1764 case OO_New:
1765 case OO_Array_New:
1766 case OO_Delete:
1767 case OO_Array_Delete:
1768 return true;
1769 }
Pavel Labath1ac2b202016-08-15 14:32:32 +00001770
Kate Stoneb9c1b512016-09-06 20:57:50 +00001771#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1772 case OO_##Name: \
1773 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1774 switch (op_kind) {
Greg Clayton090d0982011-06-19 03:43:27 +00001775#include "clang/Basic/OperatorKinds.def"
Kate Stoneb9c1b512016-09-06 20:57:50 +00001776 default:
1777 break;
1778 }
1779 return false;
Greg Clayton090d0982011-06-19 03:43:27 +00001780}
1781
Greg Clayton57ee3062013-07-11 22:46:58 +00001782clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +00001783ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1784 clang::AccessSpecifier rhs) {
1785 // Make the access equal to the stricter of the field and the nested field's
1786 // access
1787 if (lhs == AS_none || rhs == AS_none)
1788 return AS_none;
1789 if (lhs == AS_private || rhs == AS_private)
1790 return AS_private;
1791 if (lhs == AS_protected || rhs == AS_protected)
1792 return AS_protected;
1793 return AS_public;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001794}
1795
Kate Stoneb9c1b512016-09-06 20:57:50 +00001796bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1797 uint32_t &bitfield_bit_size) {
1798 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001799}
1800
Kate Stoneb9c1b512016-09-06 20:57:50 +00001801bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field,
1802 uint32_t &bitfield_bit_size) {
1803 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001804 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001805
Kate Stoneb9c1b512016-09-06 20:57:50 +00001806 if (field->isBitField()) {
1807 Expr *bit_width_expr = field->getBitWidth();
1808 if (bit_width_expr) {
1809 llvm::APSInt bit_width_apsint;
1810 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1811 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001812 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001813 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001814 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001815 }
1816 return false;
1817}
1818
1819bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1820 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001821 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001822
1823 if (!record_decl->field_empty())
1824 return true;
1825
1826 // No fields, lets check this is a CXX record and check the base classes
1827 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1828 if (cxx_record_decl) {
1829 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1830 for (base_class = cxx_record_decl->bases_begin(),
1831 base_class_end = cxx_record_decl->bases_end();
1832 base_class != base_class_end; ++base_class) {
1833 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1834 base_class->getType()->getAs<RecordType>()->getDecl());
1835 if (RecordHasFields(base_class_decl))
1836 return true;
1837 }
1838 }
1839 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001840}
1841
Adrian Prantl4e8be2c2018-06-13 16:21:24 +00001842#pragma mark Objective-C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001843
Kate Stoneb9c1b512016-09-06 20:57:50 +00001844CompilerType ClangASTContext::CreateObjCClass(const char *name,
1845 DeclContext *decl_ctx,
1846 bool isForwardDecl,
1847 bool isInternal,
1848 ClangASTMetadata *metadata) {
1849 ASTContext *ast = getASTContext();
1850 assert(ast != nullptr);
1851 assert(name && name[0]);
1852 if (decl_ctx == nullptr)
1853 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001854
Kate Stoneb9c1b512016-09-06 20:57:50 +00001855 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1856 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1857 nullptr, SourceLocation(),
1858 /*isForwardDecl,*/
1859 isInternal);
1860
1861 if (decl && metadata)
1862 SetMetadata(ast, decl, *metadata);
1863
1864 return CompilerType(ast, ast->getObjCInterfaceType(decl));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001865}
1866
Kate Stoneb9c1b512016-09-06 20:57:50 +00001867static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001868 return !ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001869}
1870
Greg Clayton57ee3062013-07-11 22:46:58 +00001871uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001872ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1873 bool omit_empty_base_classes) {
1874 uint32_t num_bases = 0;
1875 if (cxx_record_decl) {
1876 if (omit_empty_base_classes) {
1877 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1878 for (base_class = cxx_record_decl->bases_begin(),
1879 base_class_end = cxx_record_decl->bases_end();
1880 base_class != base_class_end; ++base_class) {
1881 // Skip empty base classes
1882 if (omit_empty_base_classes) {
1883 if (BaseSpecifierIsEmpty(base_class))
1884 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001885 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001886 ++num_bases;
1887 }
1888 } else
1889 num_bases = cxx_record_decl->getNumBases();
1890 }
1891 return num_bases;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001892}
1893
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001894#pragma mark Namespace Declarations
1895
1896NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001897ClangASTContext::GetUniqueNamespaceDeclaration(const char *name,
1898 DeclContext *decl_ctx) {
1899 NamespaceDecl *namespace_decl = nullptr;
1900 ASTContext *ast = getASTContext();
1901 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1902 if (decl_ctx == nullptr)
1903 decl_ctx = translation_unit_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00001904
Kate Stoneb9c1b512016-09-06 20:57:50 +00001905 if (name) {
1906 IdentifierInfo &identifier_info = ast->Idents.get(name);
1907 DeclarationName decl_name(&identifier_info);
1908 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1909 for (NamedDecl *decl : result) {
1910 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1911 if (namespace_decl)
1912 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001913 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001914
1915 namespace_decl =
1916 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1917 SourceLocation(), &identifier_info, nullptr);
1918
1919 decl_ctx->addDecl(namespace_decl);
1920 } else {
1921 if (decl_ctx == translation_unit_decl) {
1922 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1923 if (namespace_decl)
1924 return namespace_decl;
1925
1926 namespace_decl =
1927 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1928 SourceLocation(), nullptr, nullptr);
1929 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1930 translation_unit_decl->addDecl(namespace_decl);
1931 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
1932 } else {
1933 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1934 if (parent_namespace_decl) {
1935 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1936 if (namespace_decl)
1937 return namespace_decl;
1938 namespace_decl =
1939 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1940 SourceLocation(), nullptr, nullptr);
1941 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1942 parent_namespace_decl->addDecl(namespace_decl);
1943 assert(namespace_decl ==
1944 parent_namespace_decl->getAnonymousNamespace());
1945 } else {
1946 // BAD!!!
1947 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001948 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001949 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001950#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001951 VerifyDecl(namespace_decl);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001952#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001953 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001954}
1955
Kate Stoneb9c1b512016-09-06 20:57:50 +00001956NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1957 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx) {
1958 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1959 if (ast_ctx == nullptr)
1960 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +00001961
Kate Stoneb9c1b512016-09-06 20:57:50 +00001962 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx);
Siva Chandra03ff5c82016-02-05 19:10:04 +00001963}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001964
Paul Hermand628cbb2015-09-15 23:44:17 +00001965clang::BlockDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001966ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1967 if (ctx != nullptr) {
1968 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1969 clang::SourceLocation());
1970 ctx->addDecl(decl);
1971 return decl;
1972 }
1973 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001974}
1975
Kate Stoneb9c1b512016-09-06 20:57:50 +00001976clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1977 clang::DeclContext *right,
1978 clang::DeclContext *root) {
1979 if (root == nullptr)
Paul Hermanea188fc2015-09-16 18:48:30 +00001980 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001981
1982 std::set<clang::DeclContext *> path_left;
1983 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1984 path_left.insert(d);
1985
1986 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1987 if (path_left.find(d) != path_left.end())
1988 return d;
1989
1990 return nullptr;
Paul Hermanea188fc2015-09-16 18:48:30 +00001991}
1992
Kate Stoneb9c1b512016-09-06 20:57:50 +00001993clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
1994 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
1995 if (decl_ctx != nullptr && ns_decl != nullptr) {
1996 clang::TranslationUnitDecl *translation_unit =
1997 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
1998 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
1999 *getASTContext(), decl_ctx, clang::SourceLocation(),
2000 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
2001 clang::SourceLocation(), ns_decl,
2002 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
2003 decl_ctx->addDecl(using_decl);
2004 return using_decl;
2005 }
2006 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002007}
2008
2009clang::UsingDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00002010ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
2011 clang::NamedDecl *target) {
2012 if (current_decl_ctx != nullptr && target != nullptr) {
2013 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
2014 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
2015 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
2016 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
2017 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
2018 target);
2019 using_decl->addShadowDecl(shadow_decl);
2020 current_decl_ctx->addDecl(using_decl);
2021 return using_decl;
2022 }
2023 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002024}
2025
Kate Stoneb9c1b512016-09-06 20:57:50 +00002026clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
2027 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
2028 if (decl_context != nullptr) {
2029 clang::VarDecl *var_decl = clang::VarDecl::Create(
2030 *getASTContext(), decl_context, clang::SourceLocation(),
2031 clang::SourceLocation(),
2032 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
2033 nullptr, clang::SC_None);
2034 var_decl->setAccess(clang::AS_public);
2035 decl_context->addDecl(var_decl);
2036 return var_decl;
2037 }
2038 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002039}
2040
Zachary Turner9d8a97e2016-04-01 23:20:35 +00002041lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00002042ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
2043 lldb::BasicType basic_type) {
2044 switch (basic_type) {
2045 case eBasicTypeVoid:
2046 return ast->VoidTy.getAsOpaquePtr();
2047 case eBasicTypeChar:
2048 return ast->CharTy.getAsOpaquePtr();
2049 case eBasicTypeSignedChar:
2050 return ast->SignedCharTy.getAsOpaquePtr();
2051 case eBasicTypeUnsignedChar:
2052 return ast->UnsignedCharTy.getAsOpaquePtr();
2053 case eBasicTypeWChar:
2054 return ast->getWCharType().getAsOpaquePtr();
2055 case eBasicTypeSignedWChar:
2056 return ast->getSignedWCharType().getAsOpaquePtr();
2057 case eBasicTypeUnsignedWChar:
2058 return ast->getUnsignedWCharType().getAsOpaquePtr();
2059 case eBasicTypeChar16:
2060 return ast->Char16Ty.getAsOpaquePtr();
2061 case eBasicTypeChar32:
2062 return ast->Char32Ty.getAsOpaquePtr();
2063 case eBasicTypeShort:
2064 return ast->ShortTy.getAsOpaquePtr();
2065 case eBasicTypeUnsignedShort:
2066 return ast->UnsignedShortTy.getAsOpaquePtr();
2067 case eBasicTypeInt:
2068 return ast->IntTy.getAsOpaquePtr();
2069 case eBasicTypeUnsignedInt:
2070 return ast->UnsignedIntTy.getAsOpaquePtr();
2071 case eBasicTypeLong:
2072 return ast->LongTy.getAsOpaquePtr();
2073 case eBasicTypeUnsignedLong:
2074 return ast->UnsignedLongTy.getAsOpaquePtr();
2075 case eBasicTypeLongLong:
2076 return ast->LongLongTy.getAsOpaquePtr();
2077 case eBasicTypeUnsignedLongLong:
2078 return ast->UnsignedLongLongTy.getAsOpaquePtr();
2079 case eBasicTypeInt128:
2080 return ast->Int128Ty.getAsOpaquePtr();
2081 case eBasicTypeUnsignedInt128:
2082 return ast->UnsignedInt128Ty.getAsOpaquePtr();
2083 case eBasicTypeBool:
2084 return ast->BoolTy.getAsOpaquePtr();
2085 case eBasicTypeHalf:
2086 return ast->HalfTy.getAsOpaquePtr();
2087 case eBasicTypeFloat:
2088 return ast->FloatTy.getAsOpaquePtr();
2089 case eBasicTypeDouble:
2090 return ast->DoubleTy.getAsOpaquePtr();
2091 case eBasicTypeLongDouble:
2092 return ast->LongDoubleTy.getAsOpaquePtr();
2093 case eBasicTypeFloatComplex:
2094 return ast->FloatComplexTy.getAsOpaquePtr();
2095 case eBasicTypeDoubleComplex:
2096 return ast->DoubleComplexTy.getAsOpaquePtr();
2097 case eBasicTypeLongDoubleComplex:
2098 return ast->LongDoubleComplexTy.getAsOpaquePtr();
2099 case eBasicTypeObjCID:
2100 return ast->getObjCIdType().getAsOpaquePtr();
2101 case eBasicTypeObjCClass:
2102 return ast->getObjCClassType().getAsOpaquePtr();
2103 case eBasicTypeObjCSel:
2104 return ast->getObjCSelType().getAsOpaquePtr();
2105 case eBasicTypeNullPtr:
2106 return ast->NullPtrTy.getAsOpaquePtr();
2107 default:
2108 return nullptr;
2109 }
Zachary Turner9d8a97e2016-04-01 23:20:35 +00002110}
2111
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002112#pragma mark Function Types
2113
Pavel Labath1ac2b202016-08-15 14:32:32 +00002114clang::DeclarationName
Kate Stoneb9c1b512016-09-06 20:57:50 +00002115ClangASTContext::GetDeclarationName(const char *name,
2116 const CompilerType &function_clang_type) {
2117 if (!name || !name[0])
2118 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002119
Kate Stoneb9c1b512016-09-06 20:57:50 +00002120 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
2121 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
2122 return DeclarationName(&getASTContext()->Idents.get(
2123 name)); // Not operator, but a regular function.
Pavel Labath1ac2b202016-08-15 14:32:32 +00002124
Adrian Prantl05097242018-04-30 16:49:04 +00002125 // Check the number of operator parameters. Sometimes we have seen bad DWARF
2126 // that doesn't correctly describe operators and if we try to create a method
2127 // and add it to the class, clang will assert and crash, so we need to make
2128 // sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002129 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
2130 const clang::FunctionProtoType *function_type =
2131 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
2132 if (function_type == nullptr)
2133 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002134
Kate Stoneb9c1b512016-09-06 20:57:50 +00002135 const bool is_method = false;
2136 const unsigned int num_params = function_type->getNumParams();
2137 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
2138 is_method, op_kind, num_params))
2139 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002140
Kate Stoneb9c1b512016-09-06 20:57:50 +00002141 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
Pavel Labath1ac2b202016-08-15 14:32:32 +00002142}
2143
Kate Stoneb9c1b512016-09-06 20:57:50 +00002144FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
2145 DeclContext *decl_ctx, const char *name,
2146 const CompilerType &function_clang_type, int storage, bool is_inline) {
2147 FunctionDecl *func_decl = nullptr;
2148 ASTContext *ast = getASTContext();
2149 if (decl_ctx == nullptr)
2150 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002151
Kate Stoneb9c1b512016-09-06 20:57:50 +00002152 const bool hasWrittenPrototype = true;
2153 const bool isConstexprSpecified = false;
Greg Clayton0d551042013-06-28 21:08:47 +00002154
Kate Stoneb9c1b512016-09-06 20:57:50 +00002155 clang::DeclarationName declarationName =
2156 GetDeclarationName(name, function_clang_type);
2157 func_decl = FunctionDecl::Create(
2158 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
2159 ClangUtil::GetQualType(function_clang_type), nullptr,
2160 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
2161 isConstexprSpecified);
2162 if (func_decl)
2163 decl_ctx->addDecl(func_decl);
2164
Sean Callanan5e9e1992011-10-26 01:06:27 +00002165#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00002166 VerifyDecl(func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002167#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002168
2169 return func_decl;
2170}
2171
2172CompilerType ClangASTContext::CreateFunctionType(
2173 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002174 unsigned num_args, bool is_variadic, unsigned type_quals,
2175 clang::CallingConv cc) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002176 if (ast == nullptr)
2177 return CompilerType(); // invalid AST
2178
2179 if (!result_type || !ClangUtil::IsClangType(result_type))
2180 return CompilerType(); // invalid return type
2181
2182 std::vector<QualType> qual_type_args;
2183 if (num_args > 0 && args == nullptr)
2184 return CompilerType(); // invalid argument array passed in
2185
2186 // Verify that all arguments are valid and the right type
2187 for (unsigned i = 0; i < num_args; ++i) {
2188 if (args[i]) {
2189 // Make sure we have a clang type in args[i] and not a type from another
2190 // language whose name might match
2191 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2192 lldbassert(is_clang_type);
2193 if (is_clang_type)
2194 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2195 else
2196 return CompilerType(); // invalid argument type (must be a clang type)
2197 } else
2198 return CompilerType(); // invalid argument type (empty)
2199 }
2200
2201 // TODO: Detect calling convention in DWARF?
2202 FunctionProtoType::ExtProtoInfo proto_info;
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002203 proto_info.ExtInfo = cc;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002204 proto_info.Variadic = is_variadic;
2205 proto_info.ExceptionSpec = EST_None;
Mikael Nilsson8b3bf6c2018-12-13 10:17:26 +00002206 proto_info.TypeQuals = clang::Qualifiers::fromFastMask(type_quals);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002207 proto_info.RefQualifier = RQ_None;
2208
2209 return CompilerType(ast,
2210 ast->getFunctionType(ClangUtil::GetQualType(result_type),
2211 qual_type_args, proto_info));
2212}
2213
2214ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
Zachary Turner6753d2d2018-12-12 17:17:53 +00002215 clang::DeclContext *decl_ctx, const char *name,
2216 const CompilerType &param_type, int storage) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002217 ASTContext *ast = getASTContext();
2218 assert(ast != nullptr);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00002219 auto *decl =
2220 ParmVarDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(),
2221 name && name[0] ? &ast->Idents.get(name) : nullptr,
2222 ClangUtil::GetQualType(param_type), nullptr,
2223 (clang::StorageClass)storage, nullptr);
2224 decl_ctx->addDecl(decl);
2225 return decl;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002226}
2227
2228void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2229 ParmVarDecl **params,
2230 unsigned num_params) {
2231 if (function_decl)
2232 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002233}
2234
Greg Claytona1e5dc82015-08-11 22:53:00 +00002235CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002236ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
2237 QualType block_type = m_ast_ap->getBlockPointerType(
2238 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
Greg Claytonceeb5212016-05-26 22:33:25 +00002239
Kate Stoneb9c1b512016-09-06 20:57:50 +00002240 return CompilerType(this, block_type.getAsOpaquePtr());
Sean Callananc530ba92016-05-02 21:15:31 +00002241}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002242
2243#pragma mark Array Types
2244
Kate Stoneb9c1b512016-09-06 20:57:50 +00002245CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2246 size_t element_count,
2247 bool is_vector) {
2248 if (element_type.IsValid()) {
2249 ASTContext *ast = getASTContext();
2250 assert(ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002251
Kate Stoneb9c1b512016-09-06 20:57:50 +00002252 if (is_vector) {
2253 return CompilerType(
2254 ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2255 element_count));
2256 } else {
2257
2258 llvm::APInt ap_element_count(64, element_count);
2259 if (element_count == 0) {
2260 return CompilerType(ast, ast->getIncompleteArrayType(
2261 ClangUtil::GetQualType(element_type),
2262 clang::ArrayType::Normal, 0));
2263 } else {
2264 return CompilerType(
2265 ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2266 ap_element_count,
2267 clang::ArrayType::Normal, 0));
2268 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002269 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002270 }
2271 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002272}
2273
Kate Stoneb9c1b512016-09-06 20:57:50 +00002274CompilerType ClangASTContext::CreateStructForIdentifier(
2275 const ConstString &type_name,
2276 const std::initializer_list<std::pair<const char *, CompilerType>>
2277 &type_fields,
2278 bool packed) {
2279 CompilerType type;
2280 if (!type_name.IsEmpty() &&
2281 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2282 .IsValid()) {
Pavel Labathf31c9d22017-01-05 13:18:42 +00002283 lldbassert(0 && "Trying to create a type for an existing name");
Enrico Granata76b08d52014-10-29 23:08:02 +00002284 return type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002285 }
2286
2287 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2288 clang::TTK_Struct, lldb::eLanguageTypeC);
2289 StartTagDeclarationDefinition(type);
2290 for (const auto &field : type_fields)
2291 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2292 0);
2293 if (packed)
2294 SetIsPacked(type);
2295 CompleteTagDeclarationDefinition(type);
2296 return type;
Enrico Granata76b08d52014-10-29 23:08:02 +00002297}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002298
Kate Stoneb9c1b512016-09-06 20:57:50 +00002299CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
2300 const ConstString &type_name,
2301 const std::initializer_list<std::pair<const char *, CompilerType>>
2302 &type_fields,
2303 bool packed) {
2304 CompilerType type;
2305 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2306 return type;
Sean Callananc530ba92016-05-02 21:15:31 +00002307
Kate Stoneb9c1b512016-09-06 20:57:50 +00002308 return CreateStructForIdentifier(type_name, type_fields, packed);
Sean Callananc530ba92016-05-02 21:15:31 +00002309}
2310
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002311#pragma mark Enumeration Types
2312
Greg Claytona1e5dc82015-08-11 22:53:00 +00002313CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002314ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2315 const Declaration &decl,
Tamas Berghammer59765832017-11-07 10:39:22 +00002316 const CompilerType &integer_clang_type,
2317 bool is_scoped) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002318 // TODO: Do something intelligent with the Declaration object passed in
2319 // like maybe filling in the SourceLocation with it...
2320 ASTContext *ast = getASTContext();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002321
Kate Stoneb9c1b512016-09-06 20:57:50 +00002322 // TODO: ask about these...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002323 // const bool IsFixed = false;
2324
2325 EnumDecl *enum_decl = EnumDecl::Create(
2326 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2327 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
Tamas Berghammercf6bf4c2017-11-07 13:43:55 +00002328 is_scoped, // IsScoped
2329 is_scoped, // IsScopedUsingClassTag
2330 false); // IsFixed
Kate Stoneb9c1b512016-09-06 20:57:50 +00002331
2332 if (enum_decl) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +00002333 if (decl_ctx)
2334 decl_ctx->addDecl(enum_decl);
2335
Kate Stoneb9c1b512016-09-06 20:57:50 +00002336 // TODO: check if we should be setting the promotion type too?
2337 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2338
2339 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2340
2341 return CompilerType(ast, ast->getTagDeclType(enum_decl));
2342 }
2343 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002344}
2345
Kate Stoneb9c1b512016-09-06 20:57:50 +00002346CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2347 size_t bit_size,
2348 bool is_signed) {
2349 if (ast) {
2350 if (is_signed) {
2351 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
2352 return CompilerType(ast, ast->SignedCharTy);
2353
2354 if (bit_size == ast->getTypeSize(ast->ShortTy))
2355 return CompilerType(ast, ast->ShortTy);
2356
2357 if (bit_size == ast->getTypeSize(ast->IntTy))
2358 return CompilerType(ast, ast->IntTy);
2359
2360 if (bit_size == ast->getTypeSize(ast->LongTy))
2361 return CompilerType(ast, ast->LongTy);
2362
2363 if (bit_size == ast->getTypeSize(ast->LongLongTy))
2364 return CompilerType(ast, ast->LongLongTy);
2365
2366 if (bit_size == ast->getTypeSize(ast->Int128Ty))
2367 return CompilerType(ast, ast->Int128Ty);
2368 } else {
2369 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
2370 return CompilerType(ast, ast->UnsignedCharTy);
2371
2372 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
2373 return CompilerType(ast, ast->UnsignedShortTy);
2374
2375 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
2376 return CompilerType(ast, ast->UnsignedIntTy);
2377
2378 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
2379 return CompilerType(ast, ast->UnsignedLongTy);
2380
2381 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
2382 return CompilerType(ast, ast->UnsignedLongLongTy);
2383
2384 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
2385 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002386 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002387 }
2388 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002389}
2390
Kate Stoneb9c1b512016-09-06 20:57:50 +00002391CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2392 bool is_signed) {
2393 if (ast)
2394 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2395 is_signed);
2396 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002397}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002398
Kate Stoneb9c1b512016-09-06 20:57:50 +00002399void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2400 if (decl_ctx) {
2401 DumpDeclContextHiearchy(decl_ctx->getParent());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002402
Kate Stoneb9c1b512016-09-06 20:57:50 +00002403 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2404 if (named_decl) {
2405 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2406 named_decl->getDeclName().getAsString().c_str());
2407 } else {
2408 printf("%20s\n", decl_ctx->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002409 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002410 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002411}
2412
Kate Stoneb9c1b512016-09-06 20:57:50 +00002413void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2414 if (decl == nullptr)
2415 return;
2416 DumpDeclContextHiearchy(decl->getDeclContext());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002417
Kate Stoneb9c1b512016-09-06 20:57:50 +00002418 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2419 if (record_decl) {
2420 printf("%20s: %s%s\n", decl->getDeclKindName(),
2421 record_decl->getDeclName().getAsString().c_str(),
2422 record_decl->isInjectedClassName() ? " (injected class name)" : "");
Greg Claytone6b36cd2015-12-08 01:02:08 +00002423
Kate Stoneb9c1b512016-09-06 20:57:50 +00002424 } else {
2425 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2426 if (named_decl) {
2427 printf("%20s: %s\n", decl->getDeclKindName(),
2428 named_decl->getDeclName().getAsString().c_str());
2429 } else {
2430 printf("%20s\n", decl->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002431 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002432 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002433}
2434
Kate Stoneb9c1b512016-09-06 20:57:50 +00002435bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2436 clang::Decl *rhs_decl) {
2437 if (lhs_decl && rhs_decl) {
2438 //----------------------------------------------------------------------
2439 // Make sure the decl kinds match first
2440 //----------------------------------------------------------------------
2441 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2442 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002443
Kate Stoneb9c1b512016-09-06 20:57:50 +00002444 if (lhs_decl_kind == rhs_decl_kind) {
2445 //------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +00002446 // Now check that the decl contexts kinds are all equivalent before we
2447 // have to check any names of the decl contexts...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002448 //------------------------------------------------------------------
2449 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2450 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2451 if (lhs_decl_ctx && rhs_decl_ctx) {
2452 while (1) {
2453 if (lhs_decl_ctx && rhs_decl_ctx) {
2454 const clang::Decl::Kind lhs_decl_ctx_kind =
2455 lhs_decl_ctx->getDeclKind();
2456 const clang::Decl::Kind rhs_decl_ctx_kind =
2457 rhs_decl_ctx->getDeclKind();
2458 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2459 lhs_decl_ctx = lhs_decl_ctx->getParent();
2460 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002461
Kate Stoneb9c1b512016-09-06 20:57:50 +00002462 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2463 break;
2464 } else
2465 return false;
2466 } else
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00002467 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002468 }
2469
2470 //--------------------------------------------------------------
2471 // Now make sure the name of the decls match
2472 //--------------------------------------------------------------
2473 clang::NamedDecl *lhs_named_decl =
2474 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2475 clang::NamedDecl *rhs_named_decl =
2476 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2477 if (lhs_named_decl && rhs_named_decl) {
2478 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2479 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2480 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2481 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2482 return false;
2483 } else
Greg Claytona2721472011-06-25 00:44:06 +00002484 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002485 } else
2486 return false;
Greg Claytona2721472011-06-25 00:44:06 +00002487
Kate Stoneb9c1b512016-09-06 20:57:50 +00002488 //--------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +00002489 // We know that the decl context kinds all match, so now we need to
2490 // make sure the names match as well
Kate Stoneb9c1b512016-09-06 20:57:50 +00002491 //--------------------------------------------------------------
2492 lhs_decl_ctx = lhs_decl->getDeclContext();
2493 rhs_decl_ctx = rhs_decl->getDeclContext();
2494 while (1) {
2495 switch (lhs_decl_ctx->getDeclKind()) {
2496 case clang::Decl::TranslationUnit:
2497 // We don't care about the translation unit names
2498 return true;
2499 default: {
2500 clang::NamedDecl *lhs_named_decl =
2501 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2502 clang::NamedDecl *rhs_named_decl =
2503 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2504 if (lhs_named_decl && rhs_named_decl) {
2505 clang::DeclarationName lhs_decl_name =
2506 lhs_named_decl->getDeclName();
2507 clang::DeclarationName rhs_decl_name =
2508 rhs_named_decl->getDeclName();
2509 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2510 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2511 return false;
2512 } else
2513 return false;
2514 } else
2515 return false;
2516 } break;
2517 }
2518 lhs_decl_ctx = lhs_decl_ctx->getParent();
2519 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytond8d4a572015-08-11 21:38:15 +00002520 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002521 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002522 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002523 }
2524 return false;
2525}
2526bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2527 clang::Decl *decl) {
2528 if (!decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002529 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002530
Kate Stoneb9c1b512016-09-06 20:57:50 +00002531 ExternalASTSource *ast_source = ast->getExternalSource();
Greg Claytond8d4a572015-08-11 21:38:15 +00002532
Kate Stoneb9c1b512016-09-06 20:57:50 +00002533 if (!ast_source)
Greg Claytond8d4a572015-08-11 21:38:15 +00002534 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002535
2536 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2537 if (tag_decl->isCompleteDefinition())
2538 return true;
2539
2540 if (!tag_decl->hasExternalLexicalStorage())
2541 return false;
2542
2543 ast_source->CompleteType(tag_decl);
2544
2545 return !tag_decl->getTypeForDecl()->isIncompleteType();
2546 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2547 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2548 if (objc_interface_decl->getDefinition())
2549 return true;
2550
2551 if (!objc_interface_decl->hasExternalLexicalStorage())
2552 return false;
2553
2554 ast_source->CompleteType(objc_interface_decl);
2555
2556 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2557 } else {
2558 return false;
2559 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002560}
2561
Kate Stoneb9c1b512016-09-06 20:57:50 +00002562void ClangASTContext::SetMetadataAsUserID(const void *object,
2563 user_id_t user_id) {
2564 ClangASTMetadata meta_data;
2565 meta_data.SetUserID(user_id);
2566 SetMetadata(object, meta_data);
Greg Clayton99558cc42015-08-24 23:46:31 +00002567}
2568
Kate Stoneb9c1b512016-09-06 20:57:50 +00002569void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2570 ClangASTMetadata &metadata) {
2571 ClangExternalASTSourceCommon *external_source =
2572 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2573
2574 if (external_source)
2575 external_source->SetMetadata(object, metadata);
2576}
2577
2578ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2579 const void *object) {
2580 ClangExternalASTSourceCommon *external_source =
2581 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2582
2583 if (external_source && external_source->HasMetadata(object))
2584 return external_source->GetMetadata(object);
2585 else
Greg Claytond8d4a572015-08-11 21:38:15 +00002586 return nullptr;
2587}
2588
Kate Stoneb9c1b512016-09-06 20:57:50 +00002589clang::DeclContext *
2590ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2591 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2592}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002593
Kate Stoneb9c1b512016-09-06 20:57:50 +00002594clang::DeclContext *
2595ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2596 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2597}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002598
Kate Stoneb9c1b512016-09-06 20:57:50 +00002599bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2600 int kind) const {
2601 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2602 if (clang_type) {
2603 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2604 if (tag_type) {
2605 clang::TagDecl *tag_decl =
2606 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2607 if (tag_decl) {
2608 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2609 return true;
2610 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002611 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002612 }
2613 return false;
2614}
2615
2616bool ClangASTContext::SetDefaultAccessForRecordFields(
2617 clang::RecordDecl *record_decl, int default_accessibility,
2618 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2619 if (record_decl) {
2620 uint32_t field_idx;
2621 clang::RecordDecl::field_iterator field, field_end;
2622 for (field = record_decl->field_begin(),
2623 field_end = record_decl->field_end(), field_idx = 0;
2624 field != field_end; ++field, ++field_idx) {
2625 // If no accessibility was assigned, assign the correct one
2626 if (field_idx < num_assigned_accessibilities &&
2627 assigned_accessibilities[field_idx] == clang::AS_none)
2628 field->setAccess((clang::AccessSpecifier)default_accessibility);
2629 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002630 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002631 }
2632 return false;
2633}
2634
2635clang::DeclContext *
2636ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2637 return GetDeclContextForType(ClangUtil::GetQualType(type));
2638}
2639
2640clang::DeclContext *
2641ClangASTContext::GetDeclContextForType(clang::QualType type) {
2642 if (type.isNull())
2643 return nullptr;
2644
2645 clang::QualType qual_type = type.getCanonicalType();
2646 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2647 switch (type_class) {
2648 case clang::Type::ObjCInterface:
2649 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2650 ->getInterface();
2651 case clang::Type::ObjCObjectPointer:
2652 return GetDeclContextForType(
2653 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2654 ->getPointeeType());
2655 case clang::Type::Record:
2656 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2657 case clang::Type::Enum:
2658 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2659 case clang::Type::Typedef:
2660 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2661 ->getDecl()
2662 ->getUnderlyingType());
2663 case clang::Type::Auto:
2664 return GetDeclContextForType(
2665 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2666 case clang::Type::Elaborated:
2667 return GetDeclContextForType(
2668 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2669 case clang::Type::Paren:
2670 return GetDeclContextForType(
2671 llvm::cast<clang::ParenType>(qual_type)->desugar());
2672 default:
2673 break;
2674 }
2675 // No DeclContext in this type...
2676 return nullptr;
2677}
2678
2679static bool GetCompleteQualType(clang::ASTContext *ast,
2680 clang::QualType qual_type,
2681 bool allow_completion = true) {
2682 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2683 switch (type_class) {
2684 case clang::Type::ConstantArray:
2685 case clang::Type::IncompleteArray:
2686 case clang::Type::VariableArray: {
2687 const clang::ArrayType *array_type =
2688 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2689
2690 if (array_type)
2691 return GetCompleteQualType(ast, array_type->getElementType(),
2692 allow_completion);
2693 } break;
2694 case clang::Type::Record: {
2695 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2696 if (cxx_record_decl) {
2697 if (cxx_record_decl->hasExternalLexicalStorage()) {
2698 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2699 const bool fields_loaded =
2700 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2701 if (is_complete && fields_loaded)
2702 return true;
2703
2704 if (!allow_completion)
2705 return false;
2706
2707 // Call the field_begin() accessor to for it to use the external source
2708 // to load the fields...
2709 clang::ExternalASTSource *external_ast_source =
2710 ast->getExternalSource();
2711 if (external_ast_source) {
2712 external_ast_source->CompleteType(cxx_record_decl);
2713 if (cxx_record_decl->isCompleteDefinition()) {
2714 cxx_record_decl->field_begin();
2715 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2716 }
2717 }
2718 }
2719 }
2720 const clang::TagType *tag_type =
2721 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2722 return !tag_type->isIncompleteType();
2723 } break;
2724
2725 case clang::Type::Enum: {
2726 const clang::TagType *tag_type =
2727 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2728 if (tag_type) {
2729 clang::TagDecl *tag_decl = tag_type->getDecl();
2730 if (tag_decl) {
2731 if (tag_decl->getDefinition())
2732 return true;
2733
2734 if (!allow_completion)
2735 return false;
2736
2737 if (tag_decl->hasExternalLexicalStorage()) {
2738 if (ast) {
2739 clang::ExternalASTSource *external_ast_source =
2740 ast->getExternalSource();
2741 if (external_ast_source) {
2742 external_ast_source->CompleteType(tag_decl);
2743 return !tag_type->isIncompleteType();
2744 }
2745 }
2746 }
2747 return false;
2748 }
2749 }
2750
2751 } break;
2752 case clang::Type::ObjCObject:
2753 case clang::Type::ObjCInterface: {
2754 const clang::ObjCObjectType *objc_class_type =
2755 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2756 if (objc_class_type) {
2757 clang::ObjCInterfaceDecl *class_interface_decl =
2758 objc_class_type->getInterface();
2759 // We currently can't complete objective C types through the newly added
Adrian Prantl05097242018-04-30 16:49:04 +00002760 // ASTContext because it only supports TagDecl objects right now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002761 if (class_interface_decl) {
2762 if (class_interface_decl->getDefinition())
2763 return true;
2764
2765 if (!allow_completion)
2766 return false;
2767
2768 if (class_interface_decl->hasExternalLexicalStorage()) {
2769 if (ast) {
2770 clang::ExternalASTSource *external_ast_source =
2771 ast->getExternalSource();
2772 if (external_ast_source) {
2773 external_ast_source->CompleteType(class_interface_decl);
2774 return !objc_class_type->isIncompleteType();
2775 }
2776 }
2777 }
2778 return false;
2779 }
2780 }
2781 } break;
2782
2783 case clang::Type::Typedef:
2784 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2785 ->getDecl()
2786 ->getUnderlyingType(),
2787 allow_completion);
2788
2789 case clang::Type::Auto:
2790 return GetCompleteQualType(
2791 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2792 allow_completion);
2793
2794 case clang::Type::Elaborated:
2795 return GetCompleteQualType(
2796 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2797 allow_completion);
2798
2799 case clang::Type::Paren:
2800 return GetCompleteQualType(
2801 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2802 allow_completion);
2803
2804 case clang::Type::Attributed:
2805 return GetCompleteQualType(
2806 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2807 allow_completion);
2808
2809 default:
2810 break;
2811 }
2812
2813 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00002814}
2815
2816static clang::ObjCIvarDecl::AccessControl
Kate Stoneb9c1b512016-09-06 20:57:50 +00002817ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2818 switch (access) {
2819 case eAccessNone:
Greg Claytond8d4a572015-08-11 21:38:15 +00002820 return clang::ObjCIvarDecl::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002821 case eAccessPublic:
2822 return clang::ObjCIvarDecl::Public;
2823 case eAccessPrivate:
2824 return clang::ObjCIvarDecl::Private;
2825 case eAccessProtected:
2826 return clang::ObjCIvarDecl::Protected;
2827 case eAccessPackage:
2828 return clang::ObjCIvarDecl::Package;
2829 }
2830 return clang::ObjCIvarDecl::None;
Greg Claytond8d4a572015-08-11 21:38:15 +00002831}
2832
Greg Claytond8d4a572015-08-11 21:38:15 +00002833//----------------------------------------------------------------------
2834// Tests
2835//----------------------------------------------------------------------
2836
Kate Stoneb9c1b512016-09-06 20:57:50 +00002837bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2838 clang::QualType qual_type(GetCanonicalQualType(type));
2839
2840 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2841 switch (type_class) {
2842 case clang::Type::IncompleteArray:
2843 case clang::Type::VariableArray:
2844 case clang::Type::ConstantArray:
2845 case clang::Type::ExtVector:
2846 case clang::Type::Vector:
2847 case clang::Type::Record:
2848 case clang::Type::ObjCObject:
2849 case clang::Type::ObjCInterface:
2850 return true;
2851 case clang::Type::Auto:
2852 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2853 ->getDeducedType()
2854 .getAsOpaquePtr());
2855 case clang::Type::Elaborated:
2856 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2857 ->getNamedType()
2858 .getAsOpaquePtr());
2859 case clang::Type::Typedef:
2860 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2861 ->getDecl()
2862 ->getUnderlyingType()
2863 .getAsOpaquePtr());
2864 case clang::Type::Paren:
2865 return IsAggregateType(
2866 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2867 default:
2868 break;
2869 }
2870 // The clang type does have a value
2871 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002872}
2873
Kate Stoneb9c1b512016-09-06 20:57:50 +00002874bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2875 clang::QualType qual_type(GetCanonicalQualType(type));
2876
2877 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2878 switch (type_class) {
2879 case clang::Type::Record: {
2880 if (const clang::RecordType *record_type =
2881 llvm::dyn_cast_or_null<clang::RecordType>(
2882 qual_type.getTypePtrOrNull())) {
2883 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2884 return record_decl->isAnonymousStructOrUnion();
2885 }
Enrico Granata7123e2b2015-11-07 02:06:57 +00002886 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002887 break;
2888 }
2889 case clang::Type::Auto:
2890 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2891 ->getDeducedType()
2892 .getAsOpaquePtr());
2893 case clang::Type::Elaborated:
2894 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2895 ->getNamedType()
2896 .getAsOpaquePtr());
2897 case clang::Type::Typedef:
2898 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2899 ->getDecl()
2900 ->getUnderlyingType()
2901 .getAsOpaquePtr());
2902 case clang::Type::Paren:
2903 return IsAnonymousType(
2904 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2905 default:
2906 break;
2907 }
2908 // The clang type does have a value
2909 return false;
Enrico Granata7123e2b2015-11-07 02:06:57 +00002910}
2911
Kate Stoneb9c1b512016-09-06 20:57:50 +00002912bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2913 CompilerType *element_type_ptr,
2914 uint64_t *size, bool *is_incomplete) {
2915 clang::QualType qual_type(GetCanonicalQualType(type));
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002916
Kate Stoneb9c1b512016-09-06 20:57:50 +00002917 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2918 switch (type_class) {
2919 default:
2920 break;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002921
Kate Stoneb9c1b512016-09-06 20:57:50 +00002922 case clang::Type::ConstantArray:
Greg Claytond8d4a572015-08-11 21:38:15 +00002923 if (element_type_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002924 element_type_ptr->SetCompilerType(
2925 getASTContext(),
2926 llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002927 if (size)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002928 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2929 ->getSize()
2930 .getLimitedValue(ULLONG_MAX);
Greg Claytond8d4a572015-08-11 21:38:15 +00002931 if (is_incomplete)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002932 *is_incomplete = false;
2933 return true;
2934
2935 case clang::Type::IncompleteArray:
2936 if (element_type_ptr)
2937 element_type_ptr->SetCompilerType(
2938 getASTContext(),
2939 llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2940 if (size)
2941 *size = 0;
2942 if (is_incomplete)
2943 *is_incomplete = true;
2944 return true;
2945
2946 case clang::Type::VariableArray:
2947 if (element_type_ptr)
2948 element_type_ptr->SetCompilerType(
2949 getASTContext(),
2950 llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2951 if (size)
2952 *size = 0;
2953 if (is_incomplete)
2954 *is_incomplete = false;
2955 return true;
2956
2957 case clang::Type::DependentSizedArray:
2958 if (element_type_ptr)
2959 element_type_ptr->SetCompilerType(
2960 getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)
2961 ->getElementType());
2962 if (size)
2963 *size = 0;
2964 if (is_incomplete)
2965 *is_incomplete = false;
2966 return true;
2967
2968 case clang::Type::Typedef:
2969 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2970 ->getDecl()
2971 ->getUnderlyingType()
2972 .getAsOpaquePtr(),
2973 element_type_ptr, size, is_incomplete);
2974 case clang::Type::Auto:
2975 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2976 ->getDeducedType()
2977 .getAsOpaquePtr(),
2978 element_type_ptr, size, is_incomplete);
2979 case clang::Type::Elaborated:
2980 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2981 ->getNamedType()
2982 .getAsOpaquePtr(),
2983 element_type_ptr, size, is_incomplete);
2984 case clang::Type::Paren:
2985 return IsArrayType(
2986 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2987 element_type_ptr, size, is_incomplete);
2988 }
2989 if (element_type_ptr)
2990 element_type_ptr->Clear();
2991 if (size)
2992 *size = 0;
2993 if (is_incomplete)
2994 *is_incomplete = false;
2995 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002996}
2997
Kate Stoneb9c1b512016-09-06 20:57:50 +00002998bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
2999 CompilerType *element_type, uint64_t *size) {
3000 clang::QualType qual_type(GetCanonicalQualType(type));
3001
3002 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3003 switch (type_class) {
3004 case clang::Type::Vector: {
3005 const clang::VectorType *vector_type =
3006 qual_type->getAs<clang::VectorType>();
3007 if (vector_type) {
3008 if (size)
3009 *size = vector_type->getNumElements();
3010 if (element_type)
3011 *element_type =
3012 CompilerType(getASTContext(), vector_type->getElementType());
3013 }
3014 return true;
3015 } break;
3016 case clang::Type::ExtVector: {
3017 const clang::ExtVectorType *ext_vector_type =
3018 qual_type->getAs<clang::ExtVectorType>();
3019 if (ext_vector_type) {
3020 if (size)
3021 *size = ext_vector_type->getNumElements();
3022 if (element_type)
3023 *element_type =
3024 CompilerType(getASTContext(), ext_vector_type->getElementType());
3025 }
3026 return true;
3027 }
3028 default:
3029 break;
3030 }
3031 return false;
3032}
3033
3034bool ClangASTContext::IsRuntimeGeneratedType(
3035 lldb::opaque_compiler_type_t type) {
3036 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
3037 ->GetDeclContextForType(GetQualType(type));
3038 if (!decl_ctx)
3039 return false;
3040
3041 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
3042 return false;
3043
3044 clang::ObjCInterfaceDecl *result_iface_decl =
3045 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
3046
3047 ClangASTMetadata *ast_metadata =
3048 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
3049 if (!ast_metadata)
3050 return false;
3051 return (ast_metadata->GetISAPtr() != 0);
3052}
3053
3054bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
3055 return GetQualType(type).getUnqualifiedType()->isCharType();
3056}
3057
3058bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
3059 const bool allow_completion = false;
3060 return GetCompleteQualType(getASTContext(), GetQualType(type),
3061 allow_completion);
3062}
3063
3064bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
3065 return GetQualType(type).isConstQualified();
3066}
3067
3068bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
3069 uint32_t &length) {
3070 CompilerType pointee_or_element_clang_type;
3071 length = 0;
3072 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
3073
3074 if (!pointee_or_element_clang_type.IsValid())
3075 return false;
3076
3077 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
3078 if (pointee_or_element_clang_type.IsCharType()) {
3079 if (type_flags.Test(eTypeIsArray)) {
Adrian Prantl05097242018-04-30 16:49:04 +00003080 // We know the size of the array and it could be a C string since it is
3081 // an array of characters
Kate Stoneb9c1b512016-09-06 20:57:50 +00003082 length = llvm::cast<clang::ConstantArrayType>(
3083 GetCanonicalQualType(type).getTypePtr())
3084 ->getSize()
3085 .getLimitedValue();
3086 }
3087 return true;
3088 }
3089 }
3090 return false;
3091}
3092
3093bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
3094 bool *is_variadic_ptr) {
3095 if (type) {
3096 clang::QualType qual_type(GetCanonicalQualType(type));
3097
3098 if (qual_type->isFunctionType()) {
3099 if (is_variadic_ptr) {
3100 const clang::FunctionProtoType *function_proto_type =
3101 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3102 if (function_proto_type)
3103 *is_variadic_ptr = function_proto_type->isVariadic();
3104 else
3105 *is_variadic_ptr = false;
3106 }
3107 return true;
3108 }
3109
Greg Claytond8d4a572015-08-11 21:38:15 +00003110 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003111 switch (type_class) {
3112 default:
3113 break;
3114 case clang::Type::Typedef:
3115 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
3116 ->getDecl()
3117 ->getUnderlyingType()
3118 .getAsOpaquePtr(),
3119 nullptr);
3120 case clang::Type::Auto:
3121 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
3122 ->getDeducedType()
3123 .getAsOpaquePtr(),
3124 nullptr);
3125 case clang::Type::Elaborated:
3126 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
3127 ->getNamedType()
3128 .getAsOpaquePtr(),
3129 nullptr);
3130 case clang::Type::Paren:
3131 return IsFunctionType(
3132 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3133 nullptr);
3134 case clang::Type::LValueReference:
3135 case clang::Type::RValueReference: {
3136 const clang::ReferenceType *reference_type =
3137 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3138 if (reference_type)
3139 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
3140 nullptr);
3141 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00003142 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003143 }
3144 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003145}
3146
3147// Used to detect "Homogeneous Floating-point Aggregates"
3148uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003149ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
3150 CompilerType *base_type_ptr) {
3151 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003152 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003153
3154 clang::QualType qual_type(GetCanonicalQualType(type));
3155 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3156 switch (type_class) {
3157 case clang::Type::Record:
3158 if (GetCompleteType(type)) {
3159 const clang::CXXRecordDecl *cxx_record_decl =
3160 qual_type->getAsCXXRecordDecl();
3161 if (cxx_record_decl) {
3162 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3163 return 0;
3164 }
3165 const clang::RecordType *record_type =
3166 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3167 if (record_type) {
3168 const clang::RecordDecl *record_decl = record_type->getDecl();
3169 if (record_decl) {
3170 // We are looking for a structure that contains only floating point
3171 // types
3172 clang::RecordDecl::field_iterator field_pos,
3173 field_end = record_decl->field_end();
3174 uint32_t num_fields = 0;
3175 bool is_hva = false;
3176 bool is_hfa = false;
3177 clang::QualType base_qual_type;
3178 uint64_t base_bitwidth = 0;
3179 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3180 ++field_pos) {
3181 clang::QualType field_qual_type = field_pos->getType();
3182 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
3183 if (field_qual_type->isFloatingType()) {
3184 if (field_qual_type->isComplexType())
3185 return 0;
3186 else {
3187 if (num_fields == 0)
3188 base_qual_type = field_qual_type;
3189 else {
3190 if (is_hva)
3191 return 0;
3192 is_hfa = true;
3193 if (field_qual_type.getTypePtr() !=
3194 base_qual_type.getTypePtr())
3195 return 0;
3196 }
3197 }
3198 } else if (field_qual_type->isVectorType() ||
3199 field_qual_type->isExtVectorType()) {
3200 if (num_fields == 0) {
3201 base_qual_type = field_qual_type;
3202 base_bitwidth = field_bitwidth;
3203 } else {
3204 if (is_hfa)
3205 return 0;
3206 is_hva = true;
3207 if (base_bitwidth != field_bitwidth)
3208 return 0;
3209 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3210 return 0;
3211 }
3212 } else
3213 return 0;
3214 ++num_fields;
3215 }
3216 if (base_type_ptr)
3217 *base_type_ptr = CompilerType(getASTContext(), base_qual_type);
3218 return num_fields;
3219 }
3220 }
3221 }
3222 break;
3223
3224 case clang::Type::Typedef:
3225 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3226 ->getDecl()
3227 ->getUnderlyingType()
3228 .getAsOpaquePtr(),
3229 base_type_ptr);
3230
3231 case clang::Type::Auto:
3232 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3233 ->getDeducedType()
3234 .getAsOpaquePtr(),
3235 base_type_ptr);
3236
3237 case clang::Type::Elaborated:
3238 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3239 ->getNamedType()
3240 .getAsOpaquePtr(),
3241 base_type_ptr);
3242 default:
3243 break;
3244 }
3245 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003246}
3247
Kate Stoneb9c1b512016-09-06 20:57:50 +00003248size_t ClangASTContext::GetNumberOfFunctionArguments(
3249 lldb::opaque_compiler_type_t type) {
3250 if (type) {
3251 clang::QualType qual_type(GetCanonicalQualType(type));
3252 const clang::FunctionProtoType *func =
3253 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3254 if (func)
3255 return func->getNumParams();
3256 }
3257 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003258}
3259
Greg Claytona1e5dc82015-08-11 22:53:00 +00003260CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00003261ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3262 const size_t index) {
3263 if (type) {
Greg Claytond8d4a572015-08-11 21:38:15 +00003264 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003265 const clang::FunctionProtoType *func =
3266 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3267 if (func) {
3268 if (index < func->getNumParams())
3269 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00003270 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003271 }
3272 return CompilerType();
3273}
3274
3275bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3276 if (type) {
3277 clang::QualType qual_type(GetCanonicalQualType(type));
3278
3279 if (qual_type->isFunctionPointerType())
3280 return true;
3281
3282 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3283 switch (type_class) {
3284 default:
3285 break;
3286 case clang::Type::Typedef:
3287 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3288 ->getDecl()
3289 ->getUnderlyingType()
3290 .getAsOpaquePtr());
3291 case clang::Type::Auto:
3292 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3293 ->getDeducedType()
3294 .getAsOpaquePtr());
3295 case clang::Type::Elaborated:
3296 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3297 ->getNamedType()
3298 .getAsOpaquePtr());
3299 case clang::Type::Paren:
3300 return IsFunctionPointerType(
3301 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3302
3303 case clang::Type::LValueReference:
3304 case clang::Type::RValueReference: {
3305 const clang::ReferenceType *reference_type =
3306 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3307 if (reference_type)
3308 return IsFunctionPointerType(
3309 reference_type->getPointeeType().getAsOpaquePtr());
3310 } break;
3311 }
3312 }
3313 return false;
3314}
3315
3316bool ClangASTContext::IsBlockPointerType(
3317 lldb::opaque_compiler_type_t type,
3318 CompilerType *function_pointer_type_ptr) {
3319 if (type) {
3320 clang::QualType qual_type(GetCanonicalQualType(type));
3321
3322 if (qual_type->isBlockPointerType()) {
3323 if (function_pointer_type_ptr) {
3324 const clang::BlockPointerType *block_pointer_type =
3325 qual_type->getAs<clang::BlockPointerType>();
3326 QualType pointee_type = block_pointer_type->getPointeeType();
3327 QualType function_pointer_type = m_ast_ap->getPointerType(pointee_type);
3328 *function_pointer_type_ptr =
3329 CompilerType(getASTContext(), function_pointer_type);
3330 }
3331 return true;
3332 }
3333
3334 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3335 switch (type_class) {
3336 default:
3337 break;
3338 case clang::Type::Typedef:
3339 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3340 ->getDecl()
3341 ->getUnderlyingType()
3342 .getAsOpaquePtr(),
3343 function_pointer_type_ptr);
3344 case clang::Type::Auto:
3345 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3346 ->getDeducedType()
3347 .getAsOpaquePtr(),
3348 function_pointer_type_ptr);
3349 case clang::Type::Elaborated:
3350 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3351 ->getNamedType()
3352 .getAsOpaquePtr(),
3353 function_pointer_type_ptr);
3354 case clang::Type::Paren:
3355 return IsBlockPointerType(
3356 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3357 function_pointer_type_ptr);
3358
3359 case clang::Type::LValueReference:
3360 case clang::Type::RValueReference: {
3361 const clang::ReferenceType *reference_type =
3362 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3363 if (reference_type)
3364 return IsBlockPointerType(
3365 reference_type->getPointeeType().getAsOpaquePtr(),
3366 function_pointer_type_ptr);
3367 } break;
3368 }
3369 }
3370 return false;
3371}
3372
3373bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3374 bool &is_signed) {
3375 if (!type)
3376 return false;
3377
3378 clang::QualType qual_type(GetCanonicalQualType(type));
3379 const clang::BuiltinType *builtin_type =
3380 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3381
3382 if (builtin_type) {
3383 if (builtin_type->isInteger()) {
3384 is_signed = builtin_type->isSignedInteger();
3385 return true;
3386 }
3387 }
3388
3389 return false;
3390}
3391
3392bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3393 bool &is_signed) {
3394 if (type) {
3395 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3396 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3397
3398 if (enum_type) {
3399 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3400 is_signed);
3401 return true;
3402 }
3403 }
3404
3405 return false;
3406}
3407
3408bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3409 CompilerType *pointee_type) {
3410 if (type) {
3411 clang::QualType qual_type(GetCanonicalQualType(type));
3412 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3413 switch (type_class) {
3414 case clang::Type::Builtin:
3415 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3416 default:
3417 break;
3418 case clang::BuiltinType::ObjCId:
3419 case clang::BuiltinType::ObjCClass:
3420 return true;
3421 }
3422 return false;
3423 case clang::Type::ObjCObjectPointer:
3424 if (pointee_type)
3425 pointee_type->SetCompilerType(
3426 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3427 ->getPointeeType());
3428 return true;
3429 case clang::Type::BlockPointer:
3430 if (pointee_type)
3431 pointee_type->SetCompilerType(
3432 getASTContext(),
3433 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3434 return true;
3435 case clang::Type::Pointer:
3436 if (pointee_type)
3437 pointee_type->SetCompilerType(
3438 getASTContext(),
3439 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3440 return true;
3441 case clang::Type::MemberPointer:
3442 if (pointee_type)
3443 pointee_type->SetCompilerType(
3444 getASTContext(),
3445 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3446 return true;
3447 case clang::Type::Typedef:
3448 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3449 ->getDecl()
3450 ->getUnderlyingType()
3451 .getAsOpaquePtr(),
3452 pointee_type);
3453 case clang::Type::Auto:
3454 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3455 ->getDeducedType()
3456 .getAsOpaquePtr(),
3457 pointee_type);
3458 case clang::Type::Elaborated:
3459 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3460 ->getNamedType()
3461 .getAsOpaquePtr(),
3462 pointee_type);
3463 case clang::Type::Paren:
3464 return IsPointerType(
3465 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3466 pointee_type);
3467 default:
3468 break;
3469 }
3470 }
3471 if (pointee_type)
3472 pointee_type->Clear();
3473 return false;
3474}
3475
3476bool ClangASTContext::IsPointerOrReferenceType(
3477 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3478 if (type) {
3479 clang::QualType qual_type(GetCanonicalQualType(type));
3480 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3481 switch (type_class) {
3482 case clang::Type::Builtin:
3483 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3484 default:
3485 break;
3486 case clang::BuiltinType::ObjCId:
3487 case clang::BuiltinType::ObjCClass:
3488 return true;
3489 }
3490 return false;
3491 case clang::Type::ObjCObjectPointer:
3492 if (pointee_type)
3493 pointee_type->SetCompilerType(
3494 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3495 ->getPointeeType());
3496 return true;
3497 case clang::Type::BlockPointer:
3498 if (pointee_type)
3499 pointee_type->SetCompilerType(
3500 getASTContext(),
3501 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3502 return true;
3503 case clang::Type::Pointer:
3504 if (pointee_type)
3505 pointee_type->SetCompilerType(
3506 getASTContext(),
3507 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3508 return true;
3509 case clang::Type::MemberPointer:
3510 if (pointee_type)
3511 pointee_type->SetCompilerType(
3512 getASTContext(),
3513 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3514 return true;
3515 case clang::Type::LValueReference:
3516 if (pointee_type)
3517 pointee_type->SetCompilerType(
3518 getASTContext(),
3519 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3520 return true;
3521 case clang::Type::RValueReference:
3522 if (pointee_type)
3523 pointee_type->SetCompilerType(
3524 getASTContext(),
3525 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3526 return true;
3527 case clang::Type::Typedef:
3528 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3529 ->getDecl()
3530 ->getUnderlyingType()
3531 .getAsOpaquePtr(),
3532 pointee_type);
3533 case clang::Type::Auto:
3534 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3535 ->getDeducedType()
3536 .getAsOpaquePtr(),
3537 pointee_type);
3538 case clang::Type::Elaborated:
3539 return IsPointerOrReferenceType(
3540 llvm::cast<clang::ElaboratedType>(qual_type)
3541 ->getNamedType()
3542 .getAsOpaquePtr(),
3543 pointee_type);
3544 case clang::Type::Paren:
3545 return IsPointerOrReferenceType(
3546 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3547 pointee_type);
3548 default:
3549 break;
3550 }
3551 }
3552 if (pointee_type)
3553 pointee_type->Clear();
3554 return false;
3555}
3556
3557bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3558 CompilerType *pointee_type,
3559 bool *is_rvalue) {
3560 if (type) {
3561 clang::QualType qual_type(GetCanonicalQualType(type));
3562 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3563
3564 switch (type_class) {
3565 case clang::Type::LValueReference:
3566 if (pointee_type)
3567 pointee_type->SetCompilerType(
3568 getASTContext(),
3569 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3570 if (is_rvalue)
3571 *is_rvalue = false;
3572 return true;
3573 case clang::Type::RValueReference:
3574 if (pointee_type)
3575 pointee_type->SetCompilerType(
3576 getASTContext(),
3577 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3578 if (is_rvalue)
3579 *is_rvalue = true;
3580 return true;
3581 case clang::Type::Typedef:
3582 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3583 ->getDecl()
3584 ->getUnderlyingType()
3585 .getAsOpaquePtr(),
3586 pointee_type, is_rvalue);
3587 case clang::Type::Auto:
3588 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3589 ->getDeducedType()
3590 .getAsOpaquePtr(),
3591 pointee_type, is_rvalue);
3592 case clang::Type::Elaborated:
3593 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3594 ->getNamedType()
3595 .getAsOpaquePtr(),
3596 pointee_type, is_rvalue);
3597 case clang::Type::Paren:
3598 return IsReferenceType(
3599 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3600 pointee_type, is_rvalue);
3601
3602 default:
3603 break;
3604 }
3605 }
3606 if (pointee_type)
3607 pointee_type->Clear();
3608 return false;
3609}
3610
3611bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3612 uint32_t &count, bool &is_complex) {
3613 if (type) {
3614 clang::QualType qual_type(GetCanonicalQualType(type));
3615
3616 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3617 qual_type->getCanonicalTypeInternal())) {
3618 clang::BuiltinType::Kind kind = BT->getKind();
3619 if (kind >= clang::BuiltinType::Float &&
3620 kind <= clang::BuiltinType::LongDouble) {
3621 count = 1;
3622 is_complex = false;
3623 return true;
3624 }
3625 } else if (const clang::ComplexType *CT =
3626 llvm::dyn_cast<clang::ComplexType>(
3627 qual_type->getCanonicalTypeInternal())) {
3628 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3629 is_complex)) {
3630 count = 2;
3631 is_complex = true;
3632 return true;
3633 }
3634 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3635 qual_type->getCanonicalTypeInternal())) {
3636 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3637 is_complex)) {
3638 count = VT->getNumElements();
3639 is_complex = false;
3640 return true;
3641 }
3642 }
3643 }
3644 count = 0;
3645 is_complex = false;
3646 return false;
3647}
3648
3649bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3650 if (!type)
3651 return false;
3652
3653 clang::QualType qual_type(GetQualType(type));
3654 const clang::TagType *tag_type =
3655 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3656 if (tag_type) {
3657 clang::TagDecl *tag_decl = tag_type->getDecl();
3658 if (tag_decl)
3659 return tag_decl->isCompleteDefinition();
3660 return false;
3661 } else {
3662 const clang::ObjCObjectType *objc_class_type =
3663 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3664 if (objc_class_type) {
3665 clang::ObjCInterfaceDecl *class_interface_decl =
3666 objc_class_type->getInterface();
3667 if (class_interface_decl)
3668 return class_interface_decl->getDefinition() != nullptr;
3669 return false;
3670 }
3671 }
3672 return true;
3673}
3674
3675bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
3676 if (type) {
3677 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3678
3679 const clang::ObjCObjectPointerType *obj_pointer_type =
3680 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3681
3682 if (obj_pointer_type)
3683 return obj_pointer_type->isObjCClassType();
3684 }
3685 return false;
3686}
3687
3688bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3689 if (ClangUtil::IsClangType(type))
3690 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3691 return false;
3692}
3693
3694bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3695 if (!type)
3696 return false;
3697 clang::QualType qual_type(GetCanonicalQualType(type));
3698 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3699 return (type_class == clang::Type::Record);
3700}
3701
3702bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3703 if (!type)
3704 return false;
3705 clang::QualType qual_type(GetCanonicalQualType(type));
3706 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3707 return (type_class == clang::Type::Enum);
3708}
3709
3710bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3711 if (type) {
3712 clang::QualType qual_type(GetCanonicalQualType(type));
3713 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3714 switch (type_class) {
3715 case clang::Type::Record:
3716 if (GetCompleteType(type)) {
3717 const clang::RecordType *record_type =
3718 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3719 const clang::RecordDecl *record_decl = record_type->getDecl();
3720 if (record_decl) {
3721 const clang::CXXRecordDecl *cxx_record_decl =
3722 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3723 if (cxx_record_decl)
3724 return cxx_record_decl->isPolymorphic();
Greg Claytond8d4a572015-08-11 21:38:15 +00003725 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003726 }
3727 break;
3728
3729 default:
3730 break;
3731 }
3732 }
3733 return false;
3734}
3735
3736bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3737 CompilerType *dynamic_pointee_type,
3738 bool check_cplusplus,
3739 bool check_objc) {
3740 clang::QualType pointee_qual_type;
3741 if (type) {
3742 clang::QualType qual_type(GetCanonicalQualType(type));
3743 bool success = false;
3744 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3745 switch (type_class) {
3746 case clang::Type::Builtin:
3747 if (check_objc &&
3748 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3749 clang::BuiltinType::ObjCId) {
3750 if (dynamic_pointee_type)
3751 dynamic_pointee_type->SetCompilerType(this, type);
3752 return true;
3753 }
3754 break;
3755
3756 case clang::Type::ObjCObjectPointer:
3757 if (check_objc) {
3758 if (auto objc_pointee_type =
3759 qual_type->getPointeeType().getTypePtrOrNull()) {
3760 if (auto objc_object_type =
3761 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3762 objc_pointee_type)) {
3763 if (objc_object_type->isObjCClass())
3764 return false;
3765 }
3766 }
3767 if (dynamic_pointee_type)
3768 dynamic_pointee_type->SetCompilerType(
3769 getASTContext(),
3770 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3771 ->getPointeeType());
3772 return true;
3773 }
3774 break;
3775
3776 case clang::Type::Pointer:
3777 pointee_qual_type =
3778 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3779 success = true;
3780 break;
3781
3782 case clang::Type::LValueReference:
3783 case clang::Type::RValueReference:
3784 pointee_qual_type =
3785 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3786 success = true;
3787 break;
3788
3789 case clang::Type::Typedef:
3790 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3791 ->getDecl()
3792 ->getUnderlyingType()
3793 .getAsOpaquePtr(),
3794 dynamic_pointee_type, check_cplusplus,
3795 check_objc);
3796
3797 case clang::Type::Auto:
3798 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3799 ->getDeducedType()
3800 .getAsOpaquePtr(),
3801 dynamic_pointee_type, check_cplusplus,
3802 check_objc);
3803
3804 case clang::Type::Elaborated:
3805 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3806 ->getNamedType()
3807 .getAsOpaquePtr(),
3808 dynamic_pointee_type, check_cplusplus,
3809 check_objc);
3810
3811 case clang::Type::Paren:
3812 return IsPossibleDynamicType(
3813 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3814 dynamic_pointee_type, check_cplusplus, check_objc);
3815 default:
3816 break;
3817 }
3818
3819 if (success) {
3820 // Check to make sure what we are pointing too is a possible dynamic C++
Adrian Prantl05097242018-04-30 16:49:04 +00003821 // type We currently accept any "void *" (in case we have a class that
3822 // has been watered down to an opaque pointer) and virtual C++ classes.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003823 const clang::Type::TypeClass pointee_type_class =
3824 pointee_qual_type.getCanonicalType()->getTypeClass();
3825 switch (pointee_type_class) {
3826 case clang::Type::Builtin:
3827 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3828 case clang::BuiltinType::UnknownAny:
3829 case clang::BuiltinType::Void:
3830 if (dynamic_pointee_type)
3831 dynamic_pointee_type->SetCompilerType(getASTContext(),
3832 pointee_qual_type);
3833 return true;
3834 default:
3835 break;
3836 }
3837 break;
3838
3839 case clang::Type::Record:
3840 if (check_cplusplus) {
3841 clang::CXXRecordDecl *cxx_record_decl =
3842 pointee_qual_type->getAsCXXRecordDecl();
3843 if (cxx_record_decl) {
3844 bool is_complete = cxx_record_decl->isCompleteDefinition();
3845
3846 if (is_complete)
3847 success = cxx_record_decl->isDynamicClass();
3848 else {
3849 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3850 getASTContext(), cxx_record_decl);
3851 if (metadata)
3852 success = metadata->GetIsDynamicCXXType();
3853 else {
3854 is_complete = CompilerType(getASTContext(), pointee_qual_type)
3855 .GetCompleteType();
3856 if (is_complete)
3857 success = cxx_record_decl->isDynamicClass();
3858 else
3859 success = false;
3860 }
3861 }
3862
3863 if (success) {
3864 if (dynamic_pointee_type)
3865 dynamic_pointee_type->SetCompilerType(getASTContext(),
3866 pointee_qual_type);
3867 return true;
3868 }
3869 }
3870 }
3871 break;
3872
3873 case clang::Type::ObjCObject:
3874 case clang::Type::ObjCInterface:
3875 if (check_objc) {
3876 if (dynamic_pointee_type)
3877 dynamic_pointee_type->SetCompilerType(getASTContext(),
3878 pointee_qual_type);
3879 return true;
3880 }
3881 break;
3882
3883 default:
3884 break;
3885 }
3886 }
3887 }
3888 if (dynamic_pointee_type)
3889 dynamic_pointee_type->Clear();
3890 return false;
3891}
3892
3893bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3894 if (!type)
3895 return false;
3896
3897 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3898}
3899
3900bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3901 if (!type)
3902 return false;
3903 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3904}
3905
3906bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3907 if (!type)
3908 return false;
3909 return GetCanonicalQualType(type)->isVoidType();
3910}
3911
3912bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3913 return ClangASTContextSupportsLanguage(language);
3914}
3915
3916bool ClangASTContext::GetCXXClassName(const CompilerType &type,
3917 std::string &class_name) {
3918 if (type) {
3919 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3920 if (!qual_type.isNull()) {
3921 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3922 if (cxx_record_decl) {
3923 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3924 return true;
3925 }
3926 }
3927 }
3928 class_name.clear();
3929 return false;
3930}
3931
3932bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3933 if (!type)
3934 return false;
3935
3936 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Jonas Devliegherea6682a42018-12-15 00:15:33 +00003937 return !qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003938}
3939
3940bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3941 if (!type)
3942 return false;
3943 clang::QualType qual_type(GetCanonicalQualType(type));
3944 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3945 if (tag_type)
3946 return tag_type->isBeingDefined();
3947 return false;
3948}
3949
3950bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3951 CompilerType *class_type_ptr) {
3952 if (!type)
3953 return false;
3954
3955 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3956
3957 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3958 if (class_type_ptr) {
3959 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3960 const clang::ObjCObjectPointerType *obj_pointer_type =
3961 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3962 if (obj_pointer_type == nullptr)
3963 class_type_ptr->Clear();
3964 else
3965 class_type_ptr->SetCompilerType(
3966 type.GetTypeSystem(),
3967 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3968 .getAsOpaquePtr());
3969 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003970 }
3971 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003972 }
3973 if (class_type_ptr)
3974 class_type_ptr->Clear();
3975 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003976}
3977
Kate Stoneb9c1b512016-09-06 20:57:50 +00003978bool ClangASTContext::GetObjCClassName(const CompilerType &type,
3979 std::string &class_name) {
3980 if (!type)
3981 return false;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003982
Kate Stoneb9c1b512016-09-06 20:57:50 +00003983 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3984
3985 const clang::ObjCObjectType *object_type =
3986 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3987 if (object_type) {
3988 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3989 if (interface) {
3990 class_name = interface->getNameAsString();
3991 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00003992 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003993 }
3994 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003995}
3996
Greg Claytond8d4a572015-08-11 21:38:15 +00003997//----------------------------------------------------------------------
3998// Type Completion
3999//----------------------------------------------------------------------
4000
Kate Stoneb9c1b512016-09-06 20:57:50 +00004001bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
4002 if (!type)
4003 return false;
4004 const bool allow_completion = true;
4005 return GetCompleteQualType(getASTContext(), GetQualType(type),
4006 allow_completion);
Greg Claytond8d4a572015-08-11 21:38:15 +00004007}
4008
Kate Stoneb9c1b512016-09-06 20:57:50 +00004009ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
4010 std::string type_name;
4011 if (type) {
4012 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
4013 clang::QualType qual_type(GetQualType(type));
4014 printing_policy.SuppressTagKeyword = true;
4015 const clang::TypedefType *typedef_type =
4016 qual_type->getAs<clang::TypedefType>();
4017 if (typedef_type) {
4018 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
4019 type_name = typedef_decl->getQualifiedNameAsString();
4020 } else {
4021 type_name = qual_type.getAsString(printing_policy);
Greg Claytond8d4a572015-08-11 21:38:15 +00004022 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004023 }
4024 return ConstString(type_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00004025}
4026
4027uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004028ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
4029 CompilerType *pointee_or_element_clang_type) {
4030 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004031 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004032
4033 if (pointee_or_element_clang_type)
4034 pointee_or_element_clang_type->Clear();
4035
4036 clang::QualType qual_type(GetQualType(type));
4037
4038 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4039 switch (type_class) {
Sean Callananddf802a2017-06-02 01:24:18 +00004040 case clang::Type::Attributed:
4041 return GetTypeInfo(
4042 qual_type->getAs<clang::AttributedType>()
4043 ->getModifiedType().getAsOpaquePtr(),
4044 pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004045 case clang::Type::Builtin: {
4046 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
4047 qual_type->getCanonicalTypeInternal());
4048
4049 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
4050 switch (builtin_type->getKind()) {
4051 case clang::BuiltinType::ObjCId:
4052 case clang::BuiltinType::ObjCClass:
4053 if (pointee_or_element_clang_type)
4054 pointee_or_element_clang_type->SetCompilerType(
4055 getASTContext(), getASTContext()->ObjCBuiltinClassTy);
4056 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
4057 break;
4058
4059 case clang::BuiltinType::ObjCSel:
4060 if (pointee_or_element_clang_type)
4061 pointee_or_element_clang_type->SetCompilerType(getASTContext(),
4062 getASTContext()->CharTy);
4063 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
4064 break;
4065
4066 case clang::BuiltinType::Bool:
4067 case clang::BuiltinType::Char_U:
4068 case clang::BuiltinType::UChar:
4069 case clang::BuiltinType::WChar_U:
4070 case clang::BuiltinType::Char16:
4071 case clang::BuiltinType::Char32:
4072 case clang::BuiltinType::UShort:
4073 case clang::BuiltinType::UInt:
4074 case clang::BuiltinType::ULong:
4075 case clang::BuiltinType::ULongLong:
4076 case clang::BuiltinType::UInt128:
4077 case clang::BuiltinType::Char_S:
4078 case clang::BuiltinType::SChar:
4079 case clang::BuiltinType::WChar_S:
4080 case clang::BuiltinType::Short:
4081 case clang::BuiltinType::Int:
4082 case clang::BuiltinType::Long:
4083 case clang::BuiltinType::LongLong:
4084 case clang::BuiltinType::Int128:
4085 case clang::BuiltinType::Float:
4086 case clang::BuiltinType::Double:
4087 case clang::BuiltinType::LongDouble:
4088 builtin_type_flags |= eTypeIsScalar;
4089 if (builtin_type->isInteger()) {
4090 builtin_type_flags |= eTypeIsInteger;
4091 if (builtin_type->isSignedInteger())
4092 builtin_type_flags |= eTypeIsSigned;
4093 } else if (builtin_type->isFloatingPoint())
4094 builtin_type_flags |= eTypeIsFloat;
4095 break;
4096 default:
4097 break;
4098 }
4099 return builtin_type_flags;
4100 }
4101
4102 case clang::Type::BlockPointer:
4103 if (pointee_or_element_clang_type)
4104 pointee_or_element_clang_type->SetCompilerType(
4105 getASTContext(), qual_type->getPointeeType());
4106 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
4107
4108 case clang::Type::Complex: {
4109 uint32_t complex_type_flags =
4110 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
4111 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
4112 qual_type->getCanonicalTypeInternal());
4113 if (complex_type) {
4114 clang::QualType complex_element_type(complex_type->getElementType());
4115 if (complex_element_type->isIntegerType())
4116 complex_type_flags |= eTypeIsFloat;
4117 else if (complex_element_type->isFloatingType())
4118 complex_type_flags |= eTypeIsInteger;
4119 }
4120 return complex_type_flags;
4121 } break;
4122
4123 case clang::Type::ConstantArray:
4124 case clang::Type::DependentSizedArray:
4125 case clang::Type::IncompleteArray:
4126 case clang::Type::VariableArray:
4127 if (pointee_or_element_clang_type)
4128 pointee_or_element_clang_type->SetCompilerType(
4129 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
4130 ->getElementType());
4131 return eTypeHasChildren | eTypeIsArray;
4132
4133 case clang::Type::DependentName:
4134 return 0;
4135 case clang::Type::DependentSizedExtVector:
4136 return eTypeHasChildren | eTypeIsVector;
4137 case clang::Type::DependentTemplateSpecialization:
4138 return eTypeIsTemplate;
4139 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004140 return CompilerType(
4141 getASTContext(),
4142 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
4143 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004144
4145 case clang::Type::Enum:
4146 if (pointee_or_element_clang_type)
4147 pointee_or_element_clang_type->SetCompilerType(
4148 getASTContext(),
4149 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
4150 return eTypeIsEnumeration | eTypeHasValue;
4151
4152 case clang::Type::Auto:
4153 return CompilerType(
4154 getASTContext(),
4155 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4156 .GetTypeInfo(pointee_or_element_clang_type);
4157 case clang::Type::Elaborated:
4158 return CompilerType(
4159 getASTContext(),
4160 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4161 .GetTypeInfo(pointee_or_element_clang_type);
4162 case clang::Type::Paren:
4163 return CompilerType(getASTContext(),
4164 llvm::cast<clang::ParenType>(qual_type)->desugar())
4165 .GetTypeInfo(pointee_or_element_clang_type);
4166
4167 case clang::Type::FunctionProto:
4168 return eTypeIsFuncPrototype | eTypeHasValue;
4169 case clang::Type::FunctionNoProto:
4170 return eTypeIsFuncPrototype | eTypeHasValue;
4171 case clang::Type::InjectedClassName:
4172 return 0;
4173
4174 case clang::Type::LValueReference:
4175 case clang::Type::RValueReference:
4176 if (pointee_or_element_clang_type)
4177 pointee_or_element_clang_type->SetCompilerType(
4178 getASTContext(),
4179 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4180 ->getPointeeType());
4181 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4182
4183 case clang::Type::MemberPointer:
4184 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4185
4186 case clang::Type::ObjCObjectPointer:
4187 if (pointee_or_element_clang_type)
4188 pointee_or_element_clang_type->SetCompilerType(
4189 getASTContext(), qual_type->getPointeeType());
4190 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4191 eTypeHasValue;
4192
4193 case clang::Type::ObjCObject:
4194 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4195 case clang::Type::ObjCInterface:
4196 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4197
4198 case clang::Type::Pointer:
4199 if (pointee_or_element_clang_type)
4200 pointee_or_element_clang_type->SetCompilerType(
4201 getASTContext(), qual_type->getPointeeType());
4202 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4203
4204 case clang::Type::Record:
4205 if (qual_type->getAsCXXRecordDecl())
4206 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4207 else
4208 return eTypeHasChildren | eTypeIsStructUnion;
4209 break;
4210 case clang::Type::SubstTemplateTypeParm:
4211 return eTypeIsTemplate;
4212 case clang::Type::TemplateTypeParm:
4213 return eTypeIsTemplate;
4214 case clang::Type::TemplateSpecialization:
4215 return eTypeIsTemplate;
4216
4217 case clang::Type::Typedef:
4218 return eTypeIsTypedef |
4219 CompilerType(getASTContext(),
4220 llvm::cast<clang::TypedefType>(qual_type)
4221 ->getDecl()
4222 ->getUnderlyingType())
4223 .GetTypeInfo(pointee_or_element_clang_type);
4224 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004225 return CompilerType(getASTContext(),
4226 llvm::cast<clang::TypeOfExprType>(qual_type)
4227 ->getUnderlyingExpr()
4228 ->getType())
4229 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004230 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004231 return CompilerType(
4232 getASTContext(),
4233 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4234 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004235 case clang::Type::UnresolvedUsing:
4236 return 0;
4237
4238 case clang::Type::ExtVector:
4239 case clang::Type::Vector: {
4240 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4241 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4242 qual_type->getCanonicalTypeInternal());
4243 if (vector_type) {
4244 if (vector_type->isIntegerType())
4245 vector_type_flags |= eTypeIsFloat;
4246 else if (vector_type->isFloatingType())
4247 vector_type_flags |= eTypeIsInteger;
4248 }
4249 return vector_type_flags;
4250 }
4251 default:
4252 return 0;
4253 }
4254 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004255}
4256
Greg Claytond8d4a572015-08-11 21:38:15 +00004257lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004258ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4259 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004260 return lldb::eLanguageTypeC;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004261
4262 // If the type is a reference, then resolve it to what it refers to first:
4263 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4264 if (qual_type->isAnyPointerType()) {
4265 if (qual_type->isObjCObjectPointerType())
4266 return lldb::eLanguageTypeObjC;
4267
4268 clang::QualType pointee_type(qual_type->getPointeeType());
4269 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
4270 return lldb::eLanguageTypeC_plus_plus;
4271 if (pointee_type->isObjCObjectOrInterfaceType())
4272 return lldb::eLanguageTypeObjC;
4273 if (pointee_type->isObjCClassType())
4274 return lldb::eLanguageTypeObjC;
4275 if (pointee_type.getTypePtr() ==
4276 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4277 return lldb::eLanguageTypeObjC;
4278 } else {
4279 if (qual_type->isObjCObjectOrInterfaceType())
4280 return lldb::eLanguageTypeObjC;
4281 if (qual_type->getAsCXXRecordDecl())
4282 return lldb::eLanguageTypeC_plus_plus;
4283 switch (qual_type->getTypeClass()) {
4284 default:
4285 break;
4286 case clang::Type::Builtin:
4287 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4288 default:
4289 case clang::BuiltinType::Void:
4290 case clang::BuiltinType::Bool:
4291 case clang::BuiltinType::Char_U:
4292 case clang::BuiltinType::UChar:
4293 case clang::BuiltinType::WChar_U:
4294 case clang::BuiltinType::Char16:
4295 case clang::BuiltinType::Char32:
4296 case clang::BuiltinType::UShort:
4297 case clang::BuiltinType::UInt:
4298 case clang::BuiltinType::ULong:
4299 case clang::BuiltinType::ULongLong:
4300 case clang::BuiltinType::UInt128:
4301 case clang::BuiltinType::Char_S:
4302 case clang::BuiltinType::SChar:
4303 case clang::BuiltinType::WChar_S:
4304 case clang::BuiltinType::Short:
4305 case clang::BuiltinType::Int:
4306 case clang::BuiltinType::Long:
4307 case clang::BuiltinType::LongLong:
4308 case clang::BuiltinType::Int128:
4309 case clang::BuiltinType::Float:
4310 case clang::BuiltinType::Double:
4311 case clang::BuiltinType::LongDouble:
4312 break;
4313
4314 case clang::BuiltinType::NullPtr:
4315 return eLanguageTypeC_plus_plus;
4316
4317 case clang::BuiltinType::ObjCId:
4318 case clang::BuiltinType::ObjCClass:
4319 case clang::BuiltinType::ObjCSel:
4320 return eLanguageTypeObjC;
4321
4322 case clang::BuiltinType::Dependent:
4323 case clang::BuiltinType::Overload:
4324 case clang::BuiltinType::BoundMember:
4325 case clang::BuiltinType::UnknownAny:
4326 break;
4327 }
4328 break;
4329 case clang::Type::Typedef:
4330 return CompilerType(getASTContext(),
4331 llvm::cast<clang::TypedefType>(qual_type)
4332 ->getDecl()
4333 ->getUnderlyingType())
4334 .GetMinimumLanguage();
4335 }
4336 }
4337 return lldb::eLanguageTypeC;
Greg Claytond8d4a572015-08-11 21:38:15 +00004338}
4339
4340lldb::TypeClass
Kate Stoneb9c1b512016-09-06 20:57:50 +00004341ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4342 if (!type)
4343 return lldb::eTypeClassInvalid;
4344
4345 clang::QualType qual_type(GetQualType(type));
4346
4347 switch (qual_type->getTypeClass()) {
4348 case clang::Type::UnaryTransform:
4349 break;
4350 case clang::Type::FunctionNoProto:
4351 return lldb::eTypeClassFunction;
4352 case clang::Type::FunctionProto:
4353 return lldb::eTypeClassFunction;
4354 case clang::Type::IncompleteArray:
4355 return lldb::eTypeClassArray;
4356 case clang::Type::VariableArray:
4357 return lldb::eTypeClassArray;
4358 case clang::Type::ConstantArray:
4359 return lldb::eTypeClassArray;
4360 case clang::Type::DependentSizedArray:
4361 return lldb::eTypeClassArray;
4362 case clang::Type::DependentSizedExtVector:
4363 return lldb::eTypeClassVector;
Fangrui Song8f284882018-07-13 22:40:40 +00004364 case clang::Type::DependentVector:
4365 return lldb::eTypeClassVector;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004366 case clang::Type::ExtVector:
4367 return lldb::eTypeClassVector;
4368 case clang::Type::Vector:
4369 return lldb::eTypeClassVector;
4370 case clang::Type::Builtin:
4371 return lldb::eTypeClassBuiltin;
4372 case clang::Type::ObjCObjectPointer:
4373 return lldb::eTypeClassObjCObjectPointer;
4374 case clang::Type::BlockPointer:
4375 return lldb::eTypeClassBlockPointer;
4376 case clang::Type::Pointer:
4377 return lldb::eTypeClassPointer;
4378 case clang::Type::LValueReference:
4379 return lldb::eTypeClassReference;
4380 case clang::Type::RValueReference:
4381 return lldb::eTypeClassReference;
4382 case clang::Type::MemberPointer:
4383 return lldb::eTypeClassMemberPointer;
4384 case clang::Type::Complex:
4385 if (qual_type->isComplexType())
4386 return lldb::eTypeClassComplexFloat;
4387 else
4388 return lldb::eTypeClassComplexInteger;
4389 case clang::Type::ObjCObject:
4390 return lldb::eTypeClassObjCObject;
4391 case clang::Type::ObjCInterface:
4392 return lldb::eTypeClassObjCInterface;
4393 case clang::Type::Record: {
4394 const clang::RecordType *record_type =
4395 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4396 const clang::RecordDecl *record_decl = record_type->getDecl();
4397 if (record_decl->isUnion())
4398 return lldb::eTypeClassUnion;
4399 else if (record_decl->isStruct())
4400 return lldb::eTypeClassStruct;
4401 else
4402 return lldb::eTypeClassClass;
4403 } break;
4404 case clang::Type::Enum:
4405 return lldb::eTypeClassEnumeration;
4406 case clang::Type::Typedef:
4407 return lldb::eTypeClassTypedef;
4408 case clang::Type::UnresolvedUsing:
4409 break;
4410 case clang::Type::Paren:
4411 return CompilerType(getASTContext(),
4412 llvm::cast<clang::ParenType>(qual_type)->desugar())
4413 .GetTypeClass();
4414 case clang::Type::Auto:
4415 return CompilerType(
4416 getASTContext(),
4417 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4418 .GetTypeClass();
4419 case clang::Type::Elaborated:
4420 return CompilerType(
4421 getASTContext(),
4422 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4423 .GetTypeClass();
4424
4425 case clang::Type::Attributed:
4426 break;
4427 case clang::Type::TemplateTypeParm:
4428 break;
4429 case clang::Type::SubstTemplateTypeParm:
4430 break;
4431 case clang::Type::SubstTemplateTypeParmPack:
4432 break;
4433 case clang::Type::InjectedClassName:
4434 break;
4435 case clang::Type::DependentName:
4436 break;
4437 case clang::Type::DependentTemplateSpecialization:
4438 break;
4439 case clang::Type::PackExpansion:
4440 break;
4441
4442 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004443 return CompilerType(getASTContext(),
4444 llvm::cast<clang::TypeOfExprType>(qual_type)
4445 ->getUnderlyingExpr()
4446 ->getType())
4447 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004448 case clang::Type::TypeOf:
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::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004454 return CompilerType(
4455 getASTContext(),
4456 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4457 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004458 case clang::Type::TemplateSpecialization:
4459 break;
Pavel Labath4f19fce22017-02-17 13:39:50 +00004460 case clang::Type::DeducedTemplateSpecialization:
4461 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004462 case clang::Type::Atomic:
4463 break;
4464 case clang::Type::Pipe:
4465 break;
4466
4467 // pointer type decayed from an array or function type.
4468 case clang::Type::Decayed:
4469 break;
4470 case clang::Type::Adjusted:
4471 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00004472 case clang::Type::ObjCTypeParam:
4473 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00004474
4475 case clang::Type::DependentAddressSpace:
4476 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004477 }
4478 // We don't know hot to display this type...
4479 return lldb::eTypeClassOther;
Greg Claytond8d4a572015-08-11 21:38:15 +00004480}
4481
Kate Stoneb9c1b512016-09-06 20:57:50 +00004482unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4483 if (type)
4484 return GetQualType(type).getQualifiers().getCVRQualifiers();
4485 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004486}
4487
4488//----------------------------------------------------------------------
4489// Creating related types
4490//----------------------------------------------------------------------
4491
Greg Claytona1e5dc82015-08-11 22:53:00 +00004492CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004493ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4494 uint64_t *stride) {
4495 if (type) {
4496 clang::QualType qual_type(GetCanonicalQualType(type));
4497
4498 const clang::Type *array_eletype =
4499 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4500
4501 if (!array_eletype)
4502 return CompilerType();
4503
4504 CompilerType element_type(getASTContext(),
4505 array_eletype->getCanonicalTypeUnqualified());
4506
4507 // TODO: the real stride will be >= this value.. find the real one!
4508 if (stride)
Adrian Prantld963a7c2019-01-15 18:07:52 +00004509 if (auto size = element_type.GetByteSize(nullptr))
4510 *stride = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004511
4512 return element_type;
4513 }
4514 return CompilerType();
4515}
4516
4517CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4518 uint64_t size) {
4519 if (type) {
4520 clang::QualType qual_type(GetCanonicalQualType(type));
4521 if (clang::ASTContext *ast_ctx = getASTContext()) {
4522 if (size != 0)
4523 return CompilerType(
4524 ast_ctx, ast_ctx->getConstantArrayType(
4525 qual_type, llvm::APInt(64, size),
4526 clang::ArrayType::ArraySizeModifier::Normal, 0));
4527 else
4528 return CompilerType(
4529 ast_ctx,
4530 ast_ctx->getIncompleteArrayType(
4531 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
Greg Claytond8d4a572015-08-11 21:38:15 +00004532 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004533 }
4534
4535 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004536}
4537
Greg Claytona1e5dc82015-08-11 22:53:00 +00004538CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004539ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4540 if (type)
4541 return CompilerType(getASTContext(), GetCanonicalQualType(type));
4542 return CompilerType();
4543}
4544
4545static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4546 clang::QualType qual_type) {
4547 if (qual_type->isPointerType())
4548 qual_type = ast->getPointerType(
4549 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4550 else
4551 qual_type = qual_type.getUnqualifiedType();
4552 qual_type.removeLocalConst();
4553 qual_type.removeLocalRestrict();
4554 qual_type.removeLocalVolatile();
4555 return qual_type;
Enrico Granata639392f2016-08-30 20:39:58 +00004556}
4557
4558CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004559ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4560 if (type)
4561 return CompilerType(
4562 getASTContext(),
4563 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4564 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004565}
4566
Kate Stoneb9c1b512016-09-06 20:57:50 +00004567int ClangASTContext::GetFunctionArgumentCount(
4568 lldb::opaque_compiler_type_t type) {
4569 if (type) {
4570 const clang::FunctionProtoType *func =
4571 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4572 if (func)
4573 return func->getNumParams();
4574 }
4575 return -1;
4576}
4577
4578CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4579 lldb::opaque_compiler_type_t type, size_t idx) {
4580 if (type) {
4581 const clang::FunctionProtoType *func =
4582 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4583 if (func) {
4584 const uint32_t num_args = func->getNumParams();
4585 if (idx < num_args)
4586 return CompilerType(getASTContext(), func->getParamType(idx));
4587 }
4588 }
4589 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004590}
4591
Greg Claytona1e5dc82015-08-11 22:53:00 +00004592CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004593ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4594 if (type) {
4595 clang::QualType qual_type(GetQualType(type));
4596 const clang::FunctionProtoType *func =
4597 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4598 if (func)
4599 return CompilerType(getASTContext(), func->getReturnType());
4600 }
4601 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004602}
4603
4604size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004605ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4606 size_t num_functions = 0;
4607 if (type) {
4608 clang::QualType qual_type(GetCanonicalQualType(type));
4609 switch (qual_type->getTypeClass()) {
4610 case clang::Type::Record:
4611 if (GetCompleteQualType(getASTContext(), qual_type)) {
4612 const clang::RecordType *record_type =
4613 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4614 const clang::RecordDecl *record_decl = record_type->getDecl();
4615 assert(record_decl);
4616 const clang::CXXRecordDecl *cxx_record_decl =
4617 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4618 if (cxx_record_decl)
4619 num_functions = std::distance(cxx_record_decl->method_begin(),
4620 cxx_record_decl->method_end());
4621 }
4622 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004623
Sean Callananf9c622a2016-09-30 18:44:43 +00004624 case clang::Type::ObjCObjectPointer: {
4625 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004626 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004627 const clang::ObjCInterfaceType *objc_interface_type =
4628 objc_class_type->getInterfaceType();
4629 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004630 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4631 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004632 clang::ObjCInterfaceDecl *class_interface_decl =
4633 objc_interface_type->getDecl();
4634 if (class_interface_decl) {
4635 num_functions = std::distance(class_interface_decl->meth_begin(),
4636 class_interface_decl->meth_end());
Greg Claytond8d4a572015-08-11 21:38:15 +00004637 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004638 }
4639 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004640 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004641
4642 case clang::Type::ObjCObject:
4643 case clang::Type::ObjCInterface:
4644 if (GetCompleteType(type)) {
4645 const clang::ObjCObjectType *objc_class_type =
4646 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4647 if (objc_class_type) {
4648 clang::ObjCInterfaceDecl *class_interface_decl =
4649 objc_class_type->getInterface();
4650 if (class_interface_decl)
4651 num_functions = std::distance(class_interface_decl->meth_begin(),
4652 class_interface_decl->meth_end());
4653 }
4654 }
4655 break;
4656
4657 case clang::Type::Typedef:
4658 return CompilerType(getASTContext(),
4659 llvm::cast<clang::TypedefType>(qual_type)
4660 ->getDecl()
4661 ->getUnderlyingType())
4662 .GetNumMemberFunctions();
4663
4664 case clang::Type::Auto:
4665 return CompilerType(
4666 getASTContext(),
4667 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4668 .GetNumMemberFunctions();
4669
4670 case clang::Type::Elaborated:
4671 return CompilerType(
4672 getASTContext(),
4673 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4674 .GetNumMemberFunctions();
4675
4676 case clang::Type::Paren:
4677 return CompilerType(getASTContext(),
4678 llvm::cast<clang::ParenType>(qual_type)->desugar())
4679 .GetNumMemberFunctions();
4680
4681 default:
4682 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004683 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004684 }
4685 return num_functions;
Greg Claytond8d4a572015-08-11 21:38:15 +00004686}
4687
4688TypeMemberFunctionImpl
Kate Stoneb9c1b512016-09-06 20:57:50 +00004689ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4690 size_t idx) {
4691 std::string name;
4692 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4693 CompilerType clang_type;
4694 CompilerDecl clang_decl;
4695 if (type) {
4696 clang::QualType qual_type(GetCanonicalQualType(type));
4697 switch (qual_type->getTypeClass()) {
4698 case clang::Type::Record:
4699 if (GetCompleteQualType(getASTContext(), qual_type)) {
4700 const clang::RecordType *record_type =
4701 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4702 const clang::RecordDecl *record_decl = record_type->getDecl();
4703 assert(record_decl);
4704 const clang::CXXRecordDecl *cxx_record_decl =
4705 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4706 if (cxx_record_decl) {
4707 auto method_iter = cxx_record_decl->method_begin();
4708 auto method_end = cxx_record_decl->method_end();
4709 if (idx <
4710 static_cast<size_t>(std::distance(method_iter, method_end))) {
4711 std::advance(method_iter, idx);
4712 clang::CXXMethodDecl *cxx_method_decl =
4713 method_iter->getCanonicalDecl();
4714 if (cxx_method_decl) {
4715 name = cxx_method_decl->getDeclName().getAsString();
4716 if (cxx_method_decl->isStatic())
4717 kind = lldb::eMemberFunctionKindStaticMethod;
4718 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4719 kind = lldb::eMemberFunctionKindConstructor;
4720 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4721 kind = lldb::eMemberFunctionKindDestructor;
4722 else
4723 kind = lldb::eMemberFunctionKindInstanceMethod;
4724 clang_type = CompilerType(
4725 this, cxx_method_decl->getType().getAsOpaquePtr());
4726 clang_decl = CompilerDecl(this, cxx_method_decl);
4727 }
4728 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004729 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004730 }
4731 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004732
Sean Callananf9c622a2016-09-30 18:44:43 +00004733 case clang::Type::ObjCObjectPointer: {
4734 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004735 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004736 const clang::ObjCInterfaceType *objc_interface_type =
4737 objc_class_type->getInterfaceType();
4738 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004739 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4740 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004741 clang::ObjCInterfaceDecl *class_interface_decl =
4742 objc_interface_type->getDecl();
4743 if (class_interface_decl) {
4744 auto method_iter = class_interface_decl->meth_begin();
4745 auto method_end = class_interface_decl->meth_end();
4746 if (idx <
4747 static_cast<size_t>(std::distance(method_iter, method_end))) {
4748 std::advance(method_iter, idx);
4749 clang::ObjCMethodDecl *objc_method_decl =
4750 method_iter->getCanonicalDecl();
4751 if (objc_method_decl) {
4752 clang_decl = CompilerDecl(this, objc_method_decl);
4753 name = objc_method_decl->getSelector().getAsString();
4754 if (objc_method_decl->isClassMethod())
4755 kind = lldb::eMemberFunctionKindStaticMethod;
4756 else
4757 kind = lldb::eMemberFunctionKindInstanceMethod;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004758 }
4759 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004760 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004761 }
4762 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004763 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004764
Kate Stoneb9c1b512016-09-06 20:57:50 +00004765 case clang::Type::ObjCObject:
4766 case clang::Type::ObjCInterface:
4767 if (GetCompleteType(type)) {
4768 const clang::ObjCObjectType *objc_class_type =
4769 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4770 if (objc_class_type) {
4771 clang::ObjCInterfaceDecl *class_interface_decl =
4772 objc_class_type->getInterface();
4773 if (class_interface_decl) {
4774 auto method_iter = class_interface_decl->meth_begin();
4775 auto method_end = class_interface_decl->meth_end();
4776 if (idx <
4777 static_cast<size_t>(std::distance(method_iter, method_end))) {
4778 std::advance(method_iter, idx);
4779 clang::ObjCMethodDecl *objc_method_decl =
4780 method_iter->getCanonicalDecl();
4781 if (objc_method_decl) {
4782 clang_decl = CompilerDecl(this, objc_method_decl);
4783 name = objc_method_decl->getSelector().getAsString();
4784 if (objc_method_decl->isClassMethod())
4785 kind = lldb::eMemberFunctionKindStaticMethod;
4786 else
4787 kind = lldb::eMemberFunctionKindInstanceMethod;
4788 }
4789 }
4790 }
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004791 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004792 }
4793 break;
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004794
Kate Stoneb9c1b512016-09-06 20:57:50 +00004795 case clang::Type::Typedef:
4796 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4797 ->getDecl()
4798 ->getUnderlyingType()
4799 .getAsOpaquePtr(),
4800 idx);
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004801
Kate Stoneb9c1b512016-09-06 20:57:50 +00004802 case clang::Type::Auto:
4803 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4804 ->getDeducedType()
4805 .getAsOpaquePtr(),
4806 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004807
Kate Stoneb9c1b512016-09-06 20:57:50 +00004808 case clang::Type::Elaborated:
4809 return GetMemberFunctionAtIndex(
4810 llvm::cast<clang::ElaboratedType>(qual_type)
4811 ->getNamedType()
4812 .getAsOpaquePtr(),
4813 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004814
Kate Stoneb9c1b512016-09-06 20:57:50 +00004815 case clang::Type::Paren:
4816 return GetMemberFunctionAtIndex(
4817 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4818 idx);
4819
4820 default:
4821 break;
Greg Clayton56939cb2015-09-17 22:23:34 +00004822 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004823 }
Greg Clayton56939cb2015-09-17 22:23:34 +00004824
Kate Stoneb9c1b512016-09-06 20:57:50 +00004825 if (kind == eMemberFunctionKindUnknown)
4826 return TypeMemberFunctionImpl();
4827 else
4828 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
Greg Clayton56939cb2015-09-17 22:23:34 +00004829}
4830
Greg Claytona1e5dc82015-08-11 22:53:00 +00004831CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004832ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4833 if (type)
4834 return CompilerType(getASTContext(),
4835 GetQualType(type).getNonReferenceType());
4836 return CompilerType();
4837}
4838
4839CompilerType ClangASTContext::CreateTypedefType(
4840 const CompilerType &type, const char *typedef_name,
4841 const CompilerDeclContext &compiler_decl_ctx) {
4842 if (type && typedef_name && typedef_name[0]) {
4843 ClangASTContext *ast =
4844 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4845 if (!ast)
4846 return CompilerType();
4847 clang::ASTContext *clang_ast = ast->getASTContext();
4848 clang::QualType qual_type(ClangUtil::GetQualType(type));
4849
4850 clang::DeclContext *decl_ctx =
4851 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4852 if (decl_ctx == nullptr)
4853 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4854
4855 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4856 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4857 &clang_ast->Idents.get(typedef_name),
4858 clang_ast->getTrivialTypeSourceInfo(qual_type));
4859
4860 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4861
Aleksandr Urakov709426b2018-09-10 08:08:43 +00004862 decl_ctx->addDecl(decl);
4863
Kate Stoneb9c1b512016-09-06 20:57:50 +00004864 // Get a uniqued clang::QualType for the typedef decl type
4865 return CompilerType(clang_ast, clang_ast->getTypedefType(decl));
4866 }
4867 return CompilerType();
4868}
4869
4870CompilerType
4871ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4872 if (type) {
4873 clang::QualType qual_type(GetQualType(type));
4874 return CompilerType(getASTContext(),
4875 qual_type.getTypePtr()->getPointeeType());
4876 }
4877 return CompilerType();
4878}
4879
4880CompilerType
4881ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4882 if (type) {
4883 clang::QualType qual_type(GetQualType(type));
4884
4885 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4886 switch (type_class) {
4887 case clang::Type::ObjCObject:
4888 case clang::Type::ObjCInterface:
4889 return CompilerType(getASTContext(),
4890 getASTContext()->getObjCObjectPointerType(qual_type));
4891
4892 default:
4893 return CompilerType(getASTContext(),
4894 getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004895 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004896 }
4897 return CompilerType();
4898}
4899
4900CompilerType
4901ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4902 if (type)
4903 return CompilerType(this, getASTContext()
4904 ->getLValueReferenceType(GetQualType(type))
4905 .getAsOpaquePtr());
4906 else
Greg Claytona1e5dc82015-08-11 22:53:00 +00004907 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004908}
4909
Kate Stoneb9c1b512016-09-06 20:57:50 +00004910CompilerType
4911ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4912 if (type)
4913 return CompilerType(this, getASTContext()
4914 ->getRValueReferenceType(GetQualType(type))
4915 .getAsOpaquePtr());
4916 else
4917 return CompilerType();
4918}
4919
4920CompilerType
4921ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4922 if (type) {
4923 clang::QualType result(GetQualType(type));
4924 result.addConst();
4925 return CompilerType(this, result.getAsOpaquePtr());
4926 }
4927 return CompilerType();
4928}
4929
4930CompilerType
4931ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4932 if (type) {
4933 clang::QualType result(GetQualType(type));
4934 result.addVolatile();
4935 return CompilerType(this, result.getAsOpaquePtr());
4936 }
4937 return CompilerType();
4938}
4939
4940CompilerType
4941ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4942 if (type) {
4943 clang::QualType result(GetQualType(type));
4944 result.addRestrict();
4945 return CompilerType(this, result.getAsOpaquePtr());
4946 }
4947 return CompilerType();
4948}
4949
4950CompilerType
4951ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4952 const char *typedef_name,
4953 const CompilerDeclContext &compiler_decl_ctx) {
4954 if (type) {
4955 clang::ASTContext *clang_ast = getASTContext();
4956 clang::QualType qual_type(GetQualType(type));
4957
4958 clang::DeclContext *decl_ctx =
4959 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4960 if (decl_ctx == nullptr)
4961 decl_ctx = getASTContext()->getTranslationUnitDecl();
4962
4963 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4964 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4965 &clang_ast->Idents.get(typedef_name),
4966 clang_ast->getTrivialTypeSourceInfo(qual_type));
4967
4968 clang::TagDecl *tdecl = nullptr;
4969 if (!qual_type.isNull()) {
4970 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4971 tdecl = rt->getDecl();
4972 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4973 tdecl = et->getDecl();
4974 }
4975
4976 // Check whether this declaration is an anonymous struct, union, or enum,
Adrian Prantl05097242018-04-30 16:49:04 +00004977 // hidden behind a typedef. If so, we try to check whether we have a
4978 // typedef tag to attach to the original record declaration
Kate Stoneb9c1b512016-09-06 20:57:50 +00004979 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4980 tdecl->setTypedefNameForAnonDecl(decl);
4981
4982 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4983
4984 // Get a uniqued clang::QualType for the typedef decl type
4985 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4986 }
4987 return CompilerType();
4988}
4989
4990CompilerType
4991ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4992 if (type) {
4993 const clang::TypedefType *typedef_type =
4994 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4995 if (typedef_type)
4996 return CompilerType(getASTContext(),
4997 typedef_type->getDecl()->getUnderlyingType());
4998 }
4999 return CompilerType();
5000}
Greg Claytond8d4a572015-08-11 21:38:15 +00005001
5002//----------------------------------------------------------------------
5003// Create related types using the current type's AST
5004//----------------------------------------------------------------------
5005
Kate Stoneb9c1b512016-09-06 20:57:50 +00005006CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
5007 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005008}
5009//----------------------------------------------------------------------
5010// Exploring the type
5011//----------------------------------------------------------------------
5012
Kate Stoneb9c1b512016-09-06 20:57:50 +00005013uint64_t ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
5014 ExecutionContextScope *exe_scope) {
5015 if (GetCompleteType(type)) {
Greg Claytond8d4a572015-08-11 21:38:15 +00005016 clang::QualType qual_type(GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00005017 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005018 switch (type_class) {
5019 case clang::Type::Record:
5020 if (GetCompleteType(type))
5021 return getASTContext()->getTypeSize(qual_type);
5022 else
5023 return 0;
5024 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00005025
Kate Stoneb9c1b512016-09-06 20:57:50 +00005026 case clang::Type::ObjCInterface:
5027 case clang::Type::ObjCObject: {
5028 ExecutionContext exe_ctx(exe_scope);
5029 Process *process = exe_ctx.GetProcessPtr();
5030 if (process) {
5031 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
5032 if (objc_runtime) {
5033 uint64_t bit_size = 0;
5034 if (objc_runtime->GetTypeBitSize(
5035 CompilerType(getASTContext(), qual_type), bit_size))
5036 return bit_size;
5037 }
5038 } else {
5039 static bool g_printed = false;
5040 if (!g_printed) {
5041 StreamString s;
5042 DumpTypeDescription(type, &s);
5043
5044 llvm::outs() << "warning: trying to determine the size of type ";
5045 llvm::outs() << s.GetString() << "\n";
5046 llvm::outs() << "without a valid ExecutionContext. this is not "
5047 "reliable. please file a bug against LLDB.\n";
5048 llvm::outs() << "backtrace:\n";
5049 llvm::sys::PrintStackTrace(llvm::outs());
5050 llvm::outs() << "\n";
5051 g_printed = true;
5052 }
5053 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005054 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005055 LLVM_FALLTHROUGH;
5056 default:
5057 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
5058 if (bit_size == 0) {
5059 if (qual_type->isIncompleteArrayType())
5060 return getASTContext()->getTypeSize(
5061 qual_type->getArrayElementTypeNoTypeQual()
5062 ->getCanonicalTypeUnqualified());
5063 }
5064 if (qual_type->isObjCObjectOrInterfaceType())
5065 return bit_size +
5066 getASTContext()->getTypeSize(
5067 getASTContext()->ObjCBuiltinClassTy);
5068 return bit_size;
5069 }
5070 }
5071 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00005072}
5073
Kate Stoneb9c1b512016-09-06 20:57:50 +00005074size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
5075 if (GetCompleteType(type))
5076 return getASTContext()->getTypeAlign(GetQualType(type));
5077 return 0;
5078}
5079
5080lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
5081 uint64_t &count) {
5082 if (!type)
5083 return lldb::eEncodingInvalid;
5084
5085 count = 1;
5086 clang::QualType qual_type(GetCanonicalQualType(type));
5087
5088 switch (qual_type->getTypeClass()) {
5089 case clang::Type::UnaryTransform:
5090 break;
5091
5092 case clang::Type::FunctionNoProto:
5093 case clang::Type::FunctionProto:
5094 break;
5095
5096 case clang::Type::IncompleteArray:
5097 case clang::Type::VariableArray:
5098 break;
5099
5100 case clang::Type::ConstantArray:
5101 break;
5102
Fangrui Song8f284882018-07-13 22:40:40 +00005103 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005104 case clang::Type::ExtVector:
5105 case clang::Type::Vector:
5106 // TODO: Set this to more than one???
5107 break;
5108
5109 case clang::Type::Builtin:
5110 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5111 case clang::BuiltinType::Void:
5112 break;
5113
5114 case clang::BuiltinType::Bool:
5115 case clang::BuiltinType::Char_S:
5116 case clang::BuiltinType::SChar:
5117 case clang::BuiltinType::WChar_S:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005118 case clang::BuiltinType::Short:
5119 case clang::BuiltinType::Int:
5120 case clang::BuiltinType::Long:
5121 case clang::BuiltinType::LongLong:
5122 case clang::BuiltinType::Int128:
5123 return lldb::eEncodingSint;
5124
5125 case clang::BuiltinType::Char_U:
5126 case clang::BuiltinType::UChar:
5127 case clang::BuiltinType::WChar_U:
Richard Smith51d12d82018-05-02 02:43:22 +00005128 case clang::BuiltinType::Char8:
5129 case clang::BuiltinType::Char16:
5130 case clang::BuiltinType::Char32:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005131 case clang::BuiltinType::UShort:
5132 case clang::BuiltinType::UInt:
5133 case clang::BuiltinType::ULong:
5134 case clang::BuiltinType::ULongLong:
5135 case clang::BuiltinType::UInt128:
5136 return lldb::eEncodingUint;
5137
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005138 // Fixed point types. Note that they are currently ignored.
5139 case clang::BuiltinType::ShortAccum:
5140 case clang::BuiltinType::Accum:
5141 case clang::BuiltinType::LongAccum:
5142 case clang::BuiltinType::UShortAccum:
5143 case clang::BuiltinType::UAccum:
5144 case clang::BuiltinType::ULongAccum:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005145 case clang::BuiltinType::ShortFract:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005146 case clang::BuiltinType::Fract:
5147 case clang::BuiltinType::LongFract:
5148 case clang::BuiltinType::UShortFract:
5149 case clang::BuiltinType::UFract:
5150 case clang::BuiltinType::ULongFract:
5151 case clang::BuiltinType::SatShortAccum:
5152 case clang::BuiltinType::SatAccum:
5153 case clang::BuiltinType::SatLongAccum:
5154 case clang::BuiltinType::SatUShortAccum:
5155 case clang::BuiltinType::SatUAccum:
5156 case clang::BuiltinType::SatULongAccum:
5157 case clang::BuiltinType::SatShortFract:
5158 case clang::BuiltinType::SatFract:
5159 case clang::BuiltinType::SatLongFract:
5160 case clang::BuiltinType::SatUShortFract:
5161 case clang::BuiltinType::SatUFract:
5162 case clang::BuiltinType::SatULongFract:
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005163 break;
5164
Kate Stoneb9c1b512016-09-06 20:57:50 +00005165 case clang::BuiltinType::Half:
5166 case clang::BuiltinType::Float:
Ted Woodward4355c7c2017-09-20 19:16:53 +00005167 case clang::BuiltinType::Float16:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005168 case clang::BuiltinType::Float128:
5169 case clang::BuiltinType::Double:
5170 case clang::BuiltinType::LongDouble:
5171 return lldb::eEncodingIEEE754;
5172
5173 case clang::BuiltinType::ObjCClass:
5174 case clang::BuiltinType::ObjCId:
5175 case clang::BuiltinType::ObjCSel:
5176 return lldb::eEncodingUint;
5177
5178 case clang::BuiltinType::NullPtr:
5179 return lldb::eEncodingUint;
5180
5181 case clang::BuiltinType::Kind::ARCUnbridgedCast:
5182 case clang::BuiltinType::Kind::BoundMember:
5183 case clang::BuiltinType::Kind::BuiltinFn:
5184 case clang::BuiltinType::Kind::Dependent:
5185 case clang::BuiltinType::Kind::OCLClkEvent:
5186 case clang::BuiltinType::Kind::OCLEvent:
5187 case clang::BuiltinType::Kind::OCLImage1dRO:
5188 case clang::BuiltinType::Kind::OCLImage1dWO:
5189 case clang::BuiltinType::Kind::OCLImage1dRW:
5190 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
5191 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
5192 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
5193 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
5194 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
5195 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
5196 case clang::BuiltinType::Kind::OCLImage2dRO:
5197 case clang::BuiltinType::Kind::OCLImage2dWO:
5198 case clang::BuiltinType::Kind::OCLImage2dRW:
5199 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
5200 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
5201 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
5202 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
5203 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
5204 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
5205 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5206 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5207 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5208 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5209 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5210 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5211 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5212 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5213 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5214 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5215 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5216 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5217 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5218 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5219 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5220 case clang::BuiltinType::Kind::OCLImage3dRO:
5221 case clang::BuiltinType::Kind::OCLImage3dWO:
5222 case clang::BuiltinType::Kind::OCLImage3dRW:
5223 case clang::BuiltinType::Kind::OCLQueue:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005224 case clang::BuiltinType::Kind::OCLReserveID:
5225 case clang::BuiltinType::Kind::OCLSampler:
5226 case clang::BuiltinType::Kind::OMPArraySection:
5227 case clang::BuiltinType::Kind::Overload:
5228 case clang::BuiltinType::Kind::PseudoObject:
5229 case clang::BuiltinType::Kind::UnknownAny:
5230 break;
Jorge Gorbe Moyaa6e6c182018-11-08 22:04:58 +00005231
5232 case clang::BuiltinType::OCLIntelSubgroupAVCMcePayload:
5233 case clang::BuiltinType::OCLIntelSubgroupAVCImePayload:
5234 case clang::BuiltinType::OCLIntelSubgroupAVCRefPayload:
5235 case clang::BuiltinType::OCLIntelSubgroupAVCSicPayload:
5236 case clang::BuiltinType::OCLIntelSubgroupAVCMceResult:
5237 case clang::BuiltinType::OCLIntelSubgroupAVCImeResult:
5238 case clang::BuiltinType::OCLIntelSubgroupAVCRefResult:
5239 case clang::BuiltinType::OCLIntelSubgroupAVCSicResult:
5240 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultSingleRefStreamout:
5241 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultDualRefStreamout:
5242 case clang::BuiltinType::OCLIntelSubgroupAVCImeSingleRefStreamin:
5243 case clang::BuiltinType::OCLIntelSubgroupAVCImeDualRefStreamin:
5244 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005245 }
5246 break;
Adrian Prantl05097242018-04-30 16:49:04 +00005247 // All pointer types are represented as unsigned integer encodings. We may
5248 // nee to add a eEncodingPointer if we ever need to know the difference
Kate Stoneb9c1b512016-09-06 20:57:50 +00005249 case clang::Type::ObjCObjectPointer:
5250 case clang::Type::BlockPointer:
5251 case clang::Type::Pointer:
5252 case clang::Type::LValueReference:
5253 case clang::Type::RValueReference:
5254 case clang::Type::MemberPointer:
5255 return lldb::eEncodingUint;
5256 case clang::Type::Complex: {
5257 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5258 if (qual_type->isComplexType())
5259 encoding = lldb::eEncodingIEEE754;
5260 else {
5261 const clang::ComplexType *complex_type =
5262 qual_type->getAsComplexIntegerType();
5263 if (complex_type)
5264 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5265 .GetEncoding(count);
5266 else
5267 encoding = lldb::eEncodingSint;
5268 }
5269 count = 2;
5270 return encoding;
5271 }
5272
5273 case clang::Type::ObjCInterface:
5274 break;
5275 case clang::Type::Record:
5276 break;
5277 case clang::Type::Enum:
5278 return lldb::eEncodingSint;
5279 case clang::Type::Typedef:
5280 return CompilerType(getASTContext(),
5281 llvm::cast<clang::TypedefType>(qual_type)
5282 ->getDecl()
5283 ->getUnderlyingType())
5284 .GetEncoding(count);
5285
5286 case clang::Type::Auto:
5287 return CompilerType(
5288 getASTContext(),
5289 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5290 .GetEncoding(count);
5291
5292 case clang::Type::Elaborated:
5293 return CompilerType(
5294 getASTContext(),
5295 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5296 .GetEncoding(count);
5297
5298 case clang::Type::Paren:
5299 return CompilerType(getASTContext(),
5300 llvm::cast<clang::ParenType>(qual_type)->desugar())
5301 .GetEncoding(count);
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005302 case clang::Type::TypeOfExpr:
5303 return CompilerType(getASTContext(),
5304 llvm::cast<clang::TypeOfExprType>(qual_type)
5305 ->getUnderlyingExpr()
5306 ->getType())
5307 .GetEncoding(count);
5308 case clang::Type::TypeOf:
5309 return CompilerType(
5310 getASTContext(),
5311 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5312 .GetEncoding(count);
5313 case clang::Type::Decltype:
5314 return CompilerType(
5315 getASTContext(),
5316 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5317 .GetEncoding(count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005318 case clang::Type::DependentSizedArray:
5319 case clang::Type::DependentSizedExtVector:
5320 case clang::Type::UnresolvedUsing:
5321 case clang::Type::Attributed:
5322 case clang::Type::TemplateTypeParm:
5323 case clang::Type::SubstTemplateTypeParm:
5324 case clang::Type::SubstTemplateTypeParmPack:
5325 case clang::Type::InjectedClassName:
5326 case clang::Type::DependentName:
5327 case clang::Type::DependentTemplateSpecialization:
5328 case clang::Type::PackExpansion:
5329 case clang::Type::ObjCObject:
5330
Kate Stoneb9c1b512016-09-06 20:57:50 +00005331 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005332 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005333 case clang::Type::Atomic:
5334 case clang::Type::Adjusted:
5335 case clang::Type::Pipe:
5336 break;
5337
5338 // pointer type decayed from an array or function type.
5339 case clang::Type::Decayed:
5340 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005341 case clang::Type::ObjCTypeParam:
5342 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005343
5344 case clang::Type::DependentAddressSpace:
5345 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005346 }
5347 count = 0;
5348 return lldb::eEncodingInvalid;
5349}
5350
5351lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5352 if (!type)
5353 return lldb::eFormatDefault;
5354
5355 clang::QualType qual_type(GetCanonicalQualType(type));
5356
5357 switch (qual_type->getTypeClass()) {
5358 case clang::Type::UnaryTransform:
5359 break;
5360
5361 case clang::Type::FunctionNoProto:
5362 case clang::Type::FunctionProto:
5363 break;
5364
5365 case clang::Type::IncompleteArray:
5366 case clang::Type::VariableArray:
5367 break;
5368
5369 case clang::Type::ConstantArray:
5370 return lldb::eFormatVoid; // no value
5371
Fangrui Song8f284882018-07-13 22:40:40 +00005372 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005373 case clang::Type::ExtVector:
5374 case clang::Type::Vector:
5375 break;
5376
5377 case clang::Type::Builtin:
5378 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5379 // default: assert(0 && "Unknown builtin type!");
5380 case clang::BuiltinType::UnknownAny:
5381 case clang::BuiltinType::Void:
5382 case clang::BuiltinType::BoundMember:
5383 break;
5384
5385 case clang::BuiltinType::Bool:
5386 return lldb::eFormatBoolean;
5387 case clang::BuiltinType::Char_S:
5388 case clang::BuiltinType::SChar:
5389 case clang::BuiltinType::WChar_S:
5390 case clang::BuiltinType::Char_U:
5391 case clang::BuiltinType::UChar:
5392 case clang::BuiltinType::WChar_U:
5393 return lldb::eFormatChar;
5394 case clang::BuiltinType::Char16:
5395 return lldb::eFormatUnicode16;
5396 case clang::BuiltinType::Char32:
5397 return lldb::eFormatUnicode32;
5398 case clang::BuiltinType::UShort:
5399 return lldb::eFormatUnsigned;
5400 case clang::BuiltinType::Short:
5401 return lldb::eFormatDecimal;
5402 case clang::BuiltinType::UInt:
5403 return lldb::eFormatUnsigned;
5404 case clang::BuiltinType::Int:
5405 return lldb::eFormatDecimal;
5406 case clang::BuiltinType::ULong:
5407 return lldb::eFormatUnsigned;
5408 case clang::BuiltinType::Long:
5409 return lldb::eFormatDecimal;
5410 case clang::BuiltinType::ULongLong:
5411 return lldb::eFormatUnsigned;
5412 case clang::BuiltinType::LongLong:
5413 return lldb::eFormatDecimal;
5414 case clang::BuiltinType::UInt128:
5415 return lldb::eFormatUnsigned;
5416 case clang::BuiltinType::Int128:
5417 return lldb::eFormatDecimal;
5418 case clang::BuiltinType::Half:
5419 case clang::BuiltinType::Float:
5420 case clang::BuiltinType::Double:
5421 case clang::BuiltinType::LongDouble:
5422 return lldb::eFormatFloat;
5423 default:
5424 return lldb::eFormatHex;
5425 }
5426 break;
5427 case clang::Type::ObjCObjectPointer:
5428 return lldb::eFormatHex;
5429 case clang::Type::BlockPointer:
5430 return lldb::eFormatHex;
5431 case clang::Type::Pointer:
5432 return lldb::eFormatHex;
5433 case clang::Type::LValueReference:
5434 case clang::Type::RValueReference:
5435 return lldb::eFormatHex;
5436 case clang::Type::MemberPointer:
5437 break;
5438 case clang::Type::Complex: {
5439 if (qual_type->isComplexType())
5440 return lldb::eFormatComplex;
5441 else
5442 return lldb::eFormatComplexInteger;
5443 }
5444 case clang::Type::ObjCInterface:
5445 break;
5446 case clang::Type::Record:
5447 break;
5448 case clang::Type::Enum:
5449 return lldb::eFormatEnum;
5450 case clang::Type::Typedef:
5451 return CompilerType(getASTContext(),
5452 llvm::cast<clang::TypedefType>(qual_type)
5453 ->getDecl()
5454 ->getUnderlyingType())
5455 .GetFormat();
5456 case clang::Type::Auto:
5457 return CompilerType(getASTContext(),
5458 llvm::cast<clang::AutoType>(qual_type)->desugar())
5459 .GetFormat();
5460 case clang::Type::Paren:
5461 return CompilerType(getASTContext(),
5462 llvm::cast<clang::ParenType>(qual_type)->desugar())
5463 .GetFormat();
5464 case clang::Type::Elaborated:
5465 return CompilerType(
5466 getASTContext(),
5467 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5468 .GetFormat();
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005469 case clang::Type::TypeOfExpr:
5470 return CompilerType(getASTContext(),
5471 llvm::cast<clang::TypeOfExprType>(qual_type)
5472 ->getUnderlyingExpr()
5473 ->getType())
5474 .GetFormat();
5475 case clang::Type::TypeOf:
5476 return CompilerType(
5477 getASTContext(),
5478 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5479 .GetFormat();
5480 case clang::Type::Decltype:
5481 return CompilerType(
5482 getASTContext(),
5483 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5484 .GetFormat();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005485 case clang::Type::DependentSizedArray:
5486 case clang::Type::DependentSizedExtVector:
5487 case clang::Type::UnresolvedUsing:
5488 case clang::Type::Attributed:
5489 case clang::Type::TemplateTypeParm:
5490 case clang::Type::SubstTemplateTypeParm:
5491 case clang::Type::SubstTemplateTypeParmPack:
5492 case clang::Type::InjectedClassName:
5493 case clang::Type::DependentName:
5494 case clang::Type::DependentTemplateSpecialization:
5495 case clang::Type::PackExpansion:
5496 case clang::Type::ObjCObject:
5497
Kate Stoneb9c1b512016-09-06 20:57:50 +00005498 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005499 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005500 case clang::Type::Atomic:
5501 case clang::Type::Adjusted:
5502 case clang::Type::Pipe:
5503 break;
5504
5505 // pointer type decayed from an array or function type.
5506 case clang::Type::Decayed:
5507 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005508 case clang::Type::ObjCTypeParam:
5509 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005510
5511 case clang::Type::DependentAddressSpace:
5512 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005513 }
5514 // We don't know hot to display this type...
5515 return lldb::eFormatBytes;
5516}
5517
5518static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5519 bool check_superclass) {
5520 while (class_interface_decl) {
5521 if (class_interface_decl->ivar_size() > 0)
5522 return true;
5523
5524 if (check_superclass)
5525 class_interface_decl = class_interface_decl->getSuperClass();
5526 else
5527 break;
5528 }
5529 return false;
5530}
5531
Adrian Prantleca07c52018-11-05 20:49:07 +00005532static llvm::Optional<SymbolFile::ArrayInfo>
5533GetDynamicArrayInfo(ClangASTContext &ast, SymbolFile *sym_file,
5534 clang::QualType qual_type,
5535 const ExecutionContext *exe_ctx) {
5536 if (qual_type->isIncompleteArrayType())
5537 if (auto *metadata = ast.GetMetadata(qual_type.getAsOpaquePtr()))
Pavel Labathffec31e2018-12-28 13:34:44 +00005538 return sym_file->GetDynamicArrayInfoForUID(metadata->GetUserID(),
5539 exe_ctx);
Adrian Prantleca07c52018-11-05 20:49:07 +00005540 return llvm::None;
5541}
5542
Kate Stoneb9c1b512016-09-06 20:57:50 +00005543uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
Adrian Prantleca07c52018-11-05 20:49:07 +00005544 bool omit_empty_base_classes,
5545 const ExecutionContext *exe_ctx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005546 if (!type)
5547 return 0;
5548
5549 uint32_t num_children = 0;
5550 clang::QualType qual_type(GetQualType(type));
5551 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5552 switch (type_class) {
5553 case clang::Type::Builtin:
5554 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5555 case clang::BuiltinType::ObjCId: // child is Class
5556 case clang::BuiltinType::ObjCClass: // child is Class
5557 num_children = 1;
5558 break;
5559
5560 default:
5561 break;
5562 }
5563 break;
5564
5565 case clang::Type::Complex:
5566 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005567 case clang::Type::Record:
5568 if (GetCompleteQualType(getASTContext(), qual_type)) {
5569 const clang::RecordType *record_type =
5570 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5571 const clang::RecordDecl *record_decl = record_type->getDecl();
5572 assert(record_decl);
5573 const clang::CXXRecordDecl *cxx_record_decl =
5574 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5575 if (cxx_record_decl) {
5576 if (omit_empty_base_classes) {
Adrian Prantl05097242018-04-30 16:49:04 +00005577 // Check each base classes to see if it or any of its base classes
5578 // contain any fields. This can help limit the noise in variable
5579 // views by not having to show base classes that contain no members.
Kate Stoneb9c1b512016-09-06 20:57:50 +00005580 clang::CXXRecordDecl::base_class_const_iterator base_class,
5581 base_class_end;
5582 for (base_class = cxx_record_decl->bases_begin(),
5583 base_class_end = cxx_record_decl->bases_end();
5584 base_class != base_class_end; ++base_class) {
5585 const clang::CXXRecordDecl *base_class_decl =
5586 llvm::cast<clang::CXXRecordDecl>(
5587 base_class->getType()
5588 ->getAs<clang::RecordType>()
5589 ->getDecl());
5590
5591 // Skip empty base classes
Jonas Devliegherea6682a42018-12-15 00:15:33 +00005592 if (!ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00005593 continue;
5594
5595 num_children++;
5596 }
5597 } else {
5598 // Include all base classes
5599 num_children += cxx_record_decl->getNumBases();
5600 }
5601 }
5602 clang::RecordDecl::field_iterator field, field_end;
5603 for (field = record_decl->field_begin(),
5604 field_end = record_decl->field_end();
5605 field != field_end; ++field)
5606 ++num_children;
5607 }
5608 break;
5609
5610 case clang::Type::ObjCObject:
5611 case clang::Type::ObjCInterface:
5612 if (GetCompleteQualType(getASTContext(), qual_type)) {
5613 const clang::ObjCObjectType *objc_class_type =
5614 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5615 assert(objc_class_type);
5616 if (objc_class_type) {
5617 clang::ObjCInterfaceDecl *class_interface_decl =
5618 objc_class_type->getInterface();
5619
5620 if (class_interface_decl) {
5621
5622 clang::ObjCInterfaceDecl *superclass_interface_decl =
5623 class_interface_decl->getSuperClass();
5624 if (superclass_interface_decl) {
5625 if (omit_empty_base_classes) {
5626 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5627 ++num_children;
5628 } else
5629 ++num_children;
5630 }
5631
5632 num_children += class_interface_decl->ivar_size();
5633 }
5634 }
5635 }
5636 break;
5637
5638 case clang::Type::ObjCObjectPointer: {
5639 const clang::ObjCObjectPointerType *pointer_type =
5640 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5641 clang::QualType pointee_type = pointer_type->getPointeeType();
5642 uint32_t num_pointee_children =
5643 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005644 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005645 // If this type points to a simple type, then it has 1 child
5646 if (num_pointee_children == 0)
5647 num_children = 1;
5648 else
5649 num_children = num_pointee_children;
5650 } break;
5651
5652 case clang::Type::Vector:
5653 case clang::Type::ExtVector:
5654 num_children =
5655 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5656 break;
5657
5658 case clang::Type::ConstantArray:
5659 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5660 ->getSize()
5661 .getLimitedValue();
5662 break;
Adrian Prantleca07c52018-11-05 20:49:07 +00005663 case clang::Type::IncompleteArray:
5664 if (auto array_info =
5665 GetDynamicArrayInfo(*this, GetSymbolFile(), qual_type, exe_ctx))
5666 // Only 1-dimensional arrays are supported.
5667 num_children = array_info->element_orders.size()
5668 ? array_info->element_orders.back()
5669 : 0;
5670 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005671
5672 case clang::Type::Pointer: {
5673 const clang::PointerType *pointer_type =
5674 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5675 clang::QualType pointee_type(pointer_type->getPointeeType());
5676 uint32_t num_pointee_children =
5677 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005678 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005679 if (num_pointee_children == 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00005680 // We have a pointer to a pointee type that claims it has no children. We
5681 // will want to look at
Kate Stoneb9c1b512016-09-06 20:57:50 +00005682 num_children = GetNumPointeeChildren(pointee_type);
5683 } else
5684 num_children = num_pointee_children;
5685 } break;
5686
5687 case clang::Type::LValueReference:
5688 case clang::Type::RValueReference: {
5689 const clang::ReferenceType *reference_type =
5690 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5691 clang::QualType pointee_type = reference_type->getPointeeType();
5692 uint32_t num_pointee_children =
5693 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005694 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005695 // If this type points to a simple type, then it has 1 child
5696 if (num_pointee_children == 0)
5697 num_children = 1;
5698 else
5699 num_children = num_pointee_children;
5700 } break;
5701
5702 case clang::Type::Typedef:
5703 num_children =
5704 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5705 ->getDecl()
5706 ->getUnderlyingType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005707 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005708 break;
5709
5710 case clang::Type::Auto:
5711 num_children =
5712 CompilerType(getASTContext(),
5713 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005714 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005715 break;
5716
5717 case clang::Type::Elaborated:
5718 num_children =
5719 CompilerType(
5720 getASTContext(),
5721 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005722 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005723 break;
5724
5725 case clang::Type::Paren:
5726 num_children =
5727 CompilerType(getASTContext(),
5728 llvm::cast<clang::ParenType>(qual_type)->desugar())
Adrian Prantleca07c52018-11-05 20:49:07 +00005729 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005730 break;
5731 default:
5732 break;
5733 }
5734 return num_children;
5735}
5736
5737CompilerType ClangASTContext::GetBuiltinTypeByName(const ConstString &name) {
5738 return GetBasicType(GetBasicTypeEnumeration(name));
Greg Clayton56939cb2015-09-17 22:23:34 +00005739}
5740
Greg Claytond8d4a572015-08-11 21:38:15 +00005741lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00005742ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5743 if (type) {
5744 clang::QualType qual_type(GetQualType(type));
5745 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5746 if (type_class == clang::Type::Builtin) {
5747 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5748 case clang::BuiltinType::Void:
5749 return eBasicTypeVoid;
5750 case clang::BuiltinType::Bool:
5751 return eBasicTypeBool;
5752 case clang::BuiltinType::Char_S:
5753 return eBasicTypeSignedChar;
5754 case clang::BuiltinType::Char_U:
5755 return eBasicTypeUnsignedChar;
5756 case clang::BuiltinType::Char16:
5757 return eBasicTypeChar16;
5758 case clang::BuiltinType::Char32:
5759 return eBasicTypeChar32;
5760 case clang::BuiltinType::UChar:
5761 return eBasicTypeUnsignedChar;
5762 case clang::BuiltinType::SChar:
5763 return eBasicTypeSignedChar;
5764 case clang::BuiltinType::WChar_S:
5765 return eBasicTypeSignedWChar;
5766 case clang::BuiltinType::WChar_U:
5767 return eBasicTypeUnsignedWChar;
5768 case clang::BuiltinType::Short:
5769 return eBasicTypeShort;
5770 case clang::BuiltinType::UShort:
5771 return eBasicTypeUnsignedShort;
5772 case clang::BuiltinType::Int:
5773 return eBasicTypeInt;
5774 case clang::BuiltinType::UInt:
5775 return eBasicTypeUnsignedInt;
5776 case clang::BuiltinType::Long:
5777 return eBasicTypeLong;
5778 case clang::BuiltinType::ULong:
5779 return eBasicTypeUnsignedLong;
5780 case clang::BuiltinType::LongLong:
5781 return eBasicTypeLongLong;
5782 case clang::BuiltinType::ULongLong:
5783 return eBasicTypeUnsignedLongLong;
5784 case clang::BuiltinType::Int128:
5785 return eBasicTypeInt128;
5786 case clang::BuiltinType::UInt128:
5787 return eBasicTypeUnsignedInt128;
5788
5789 case clang::BuiltinType::Half:
5790 return eBasicTypeHalf;
5791 case clang::BuiltinType::Float:
5792 return eBasicTypeFloat;
5793 case clang::BuiltinType::Double:
5794 return eBasicTypeDouble;
5795 case clang::BuiltinType::LongDouble:
5796 return eBasicTypeLongDouble;
5797
5798 case clang::BuiltinType::NullPtr:
5799 return eBasicTypeNullPtr;
5800 case clang::BuiltinType::ObjCId:
5801 return eBasicTypeObjCID;
5802 case clang::BuiltinType::ObjCClass:
5803 return eBasicTypeObjCClass;
5804 case clang::BuiltinType::ObjCSel:
5805 return eBasicTypeObjCSel;
5806 default:
5807 return eBasicTypeOther;
5808 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005809 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005810 }
5811 return eBasicTypeInvalid;
Greg Claytond8d4a572015-08-11 21:38:15 +00005812}
5813
Kate Stoneb9c1b512016-09-06 20:57:50 +00005814void ClangASTContext::ForEachEnumerator(
5815 lldb::opaque_compiler_type_t type,
5816 std::function<bool(const CompilerType &integer_type,
5817 const ConstString &name,
5818 const llvm::APSInt &value)> const &callback) {
5819 const clang::EnumType *enum_type =
5820 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5821 if (enum_type) {
5822 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5823 if (enum_decl) {
5824 CompilerType integer_type(this,
5825 enum_decl->getIntegerType().getAsOpaquePtr());
Greg Clayton99558cc42015-08-24 23:46:31 +00005826
Kate Stoneb9c1b512016-09-06 20:57:50 +00005827 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5828 for (enum_pos = enum_decl->enumerator_begin(),
5829 enum_end_pos = enum_decl->enumerator_end();
5830 enum_pos != enum_end_pos; ++enum_pos) {
5831 ConstString name(enum_pos->getNameAsString().c_str());
5832 if (!callback(integer_type, name, enum_pos->getInitVal()))
5833 break;
5834 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005835 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005836 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005837}
5838
Greg Claytond8d4a572015-08-11 21:38:15 +00005839#pragma mark Aggregate Types
5840
Kate Stoneb9c1b512016-09-06 20:57:50 +00005841uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5842 if (!type)
5843 return 0;
Enrico Granata36f51e42015-12-18 22:41:25 +00005844
Kate Stoneb9c1b512016-09-06 20:57:50 +00005845 uint32_t count = 0;
5846 clang::QualType qual_type(GetCanonicalQualType(type));
5847 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5848 switch (type_class) {
5849 case clang::Type::Record:
5850 if (GetCompleteType(type)) {
5851 const clang::RecordType *record_type =
5852 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5853 if (record_type) {
5854 clang::RecordDecl *record_decl = record_type->getDecl();
5855 if (record_decl) {
5856 uint32_t field_idx = 0;
5857 clang::RecordDecl::field_iterator field, field_end;
5858 for (field = record_decl->field_begin(),
5859 field_end = record_decl->field_end();
5860 field != field_end; ++field)
5861 ++field_idx;
5862 count = field_idx;
5863 }
5864 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005865 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005866 break;
5867
5868 case clang::Type::Typedef:
5869 count =
5870 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5871 ->getDecl()
5872 ->getUnderlyingType())
5873 .GetNumFields();
5874 break;
5875
5876 case clang::Type::Auto:
5877 count =
5878 CompilerType(getASTContext(),
5879 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5880 .GetNumFields();
5881 break;
5882
5883 case clang::Type::Elaborated:
5884 count = CompilerType(
5885 getASTContext(),
5886 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5887 .GetNumFields();
5888 break;
5889
5890 case clang::Type::Paren:
5891 count = CompilerType(getASTContext(),
5892 llvm::cast<clang::ParenType>(qual_type)->desugar())
5893 .GetNumFields();
5894 break;
5895
Sean Callananf9c622a2016-09-30 18:44:43 +00005896 case clang::Type::ObjCObjectPointer: {
5897 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005898 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005899 const clang::ObjCInterfaceType *objc_interface_type =
5900 objc_class_type->getInterfaceType();
5901 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005902 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5903 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005904 clang::ObjCInterfaceDecl *class_interface_decl =
5905 objc_interface_type->getDecl();
5906 if (class_interface_decl) {
5907 count = class_interface_decl->ivar_size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005908 }
5909 }
5910 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005911 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005912
5913 case clang::Type::ObjCObject:
5914 case clang::Type::ObjCInterface:
5915 if (GetCompleteType(type)) {
5916 const clang::ObjCObjectType *objc_class_type =
5917 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5918 if (objc_class_type) {
5919 clang::ObjCInterfaceDecl *class_interface_decl =
5920 objc_class_type->getInterface();
5921
5922 if (class_interface_decl)
5923 count = class_interface_decl->ivar_size();
5924 }
5925 }
5926 break;
5927
5928 default:
5929 break;
5930 }
5931 return count;
Greg Claytond8d4a572015-08-11 21:38:15 +00005932}
5933
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005934static lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005935GetObjCFieldAtIndex(clang::ASTContext *ast,
5936 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5937 std::string &name, uint64_t *bit_offset_ptr,
5938 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5939 if (class_interface_decl) {
5940 if (idx < (class_interface_decl->ivar_size())) {
5941 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5942 ivar_end = class_interface_decl->ivar_end();
5943 uint32_t ivar_idx = 0;
5944
5945 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5946 ++ivar_pos, ++ivar_idx) {
5947 if (ivar_idx == idx) {
5948 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5949
5950 clang::QualType ivar_qual_type(ivar_decl->getType());
5951
5952 name.assign(ivar_decl->getNameAsString());
5953
5954 if (bit_offset_ptr) {
5955 const clang::ASTRecordLayout &interface_layout =
5956 ast->getASTObjCInterfaceLayout(class_interface_decl);
5957 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5958 }
5959
5960 const bool is_bitfield = ivar_pos->isBitField();
5961
5962 if (bitfield_bit_size_ptr) {
5963 *bitfield_bit_size_ptr = 0;
5964
5965 if (is_bitfield && ast) {
5966 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
Hans Wennborg30ce9622018-11-28 14:30:18 +00005967 clang::Expr::EvalResult result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005968 if (bitfield_bit_size_expr &&
Hans Wennborg30ce9622018-11-28 14:30:18 +00005969 bitfield_bit_size_expr->EvaluateAsInt(result, *ast)) {
5970 llvm::APSInt bitfield_apsint = result.Val.getInt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005971 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5972 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005973 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005974 }
5975 if (is_bitfield_ptr)
5976 *is_bitfield_ptr = is_bitfield;
5977
5978 return ivar_qual_type.getAsOpaquePtr();
Greg Claytond8d4a572015-08-11 21:38:15 +00005979 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005980 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005981 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005982 }
5983 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00005984}
5985
Kate Stoneb9c1b512016-09-06 20:57:50 +00005986CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5987 size_t idx, std::string &name,
5988 uint64_t *bit_offset_ptr,
5989 uint32_t *bitfield_bit_size_ptr,
5990 bool *is_bitfield_ptr) {
5991 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005992 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005993
5994 clang::QualType qual_type(GetCanonicalQualType(type));
5995 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5996 switch (type_class) {
5997 case clang::Type::Record:
5998 if (GetCompleteType(type)) {
5999 const clang::RecordType *record_type =
6000 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6001 const clang::RecordDecl *record_decl = record_type->getDecl();
6002 uint32_t field_idx = 0;
6003 clang::RecordDecl::field_iterator field, field_end;
6004 for (field = record_decl->field_begin(),
6005 field_end = record_decl->field_end();
6006 field != field_end; ++field, ++field_idx) {
6007 if (idx == field_idx) {
6008 // Print the member type if requested
6009 // Print the member name and equal sign
6010 name.assign(field->getNameAsString());
6011
6012 // Figure out the type byte size (field_type_info.first) and
6013 // alignment (field_type_info.second) from the AST context.
6014 if (bit_offset_ptr) {
6015 const clang::ASTRecordLayout &record_layout =
6016 getASTContext()->getASTRecordLayout(record_decl);
6017 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
6018 }
6019
6020 const bool is_bitfield = field->isBitField();
6021
6022 if (bitfield_bit_size_ptr) {
6023 *bitfield_bit_size_ptr = 0;
6024
6025 if (is_bitfield) {
6026 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
Hans Wennborg30ce9622018-11-28 14:30:18 +00006027 clang::Expr::EvalResult result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006028 if (bitfield_bit_size_expr &&
Hans Wennborg30ce9622018-11-28 14:30:18 +00006029 bitfield_bit_size_expr->EvaluateAsInt(result,
Kate Stoneb9c1b512016-09-06 20:57:50 +00006030 *getASTContext())) {
Hans Wennborg30ce9622018-11-28 14:30:18 +00006031 llvm::APSInt bitfield_apsint = result.Val.getInt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006032 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
6033 }
6034 }
6035 }
6036 if (is_bitfield_ptr)
6037 *is_bitfield_ptr = is_bitfield;
6038
6039 return CompilerType(getASTContext(), field->getType());
6040 }
6041 }
6042 }
6043 break;
6044
Sean Callananf9c622a2016-09-30 18:44:43 +00006045 case clang::Type::ObjCObjectPointer: {
6046 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00006047 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00006048 const clang::ObjCInterfaceType *objc_interface_type =
6049 objc_class_type->getInterfaceType();
6050 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00006051 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
6052 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00006053 clang::ObjCInterfaceDecl *class_interface_decl =
6054 objc_interface_type->getDecl();
6055 if (class_interface_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006056 return CompilerType(
6057 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
6058 idx, name, bit_offset_ptr,
6059 bitfield_bit_size_ptr, is_bitfield_ptr));
6060 }
6061 }
6062 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00006063 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006064
6065 case clang::Type::ObjCObject:
6066 case clang::Type::ObjCInterface:
6067 if (GetCompleteType(type)) {
6068 const clang::ObjCObjectType *objc_class_type =
6069 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6070 assert(objc_class_type);
6071 if (objc_class_type) {
6072 clang::ObjCInterfaceDecl *class_interface_decl =
6073 objc_class_type->getInterface();
6074 return CompilerType(
6075 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
6076 idx, name, bit_offset_ptr,
6077 bitfield_bit_size_ptr, is_bitfield_ptr));
6078 }
6079 }
6080 break;
6081
6082 case clang::Type::Typedef:
6083 return CompilerType(getASTContext(),
6084 llvm::cast<clang::TypedefType>(qual_type)
6085 ->getDecl()
6086 ->getUnderlyingType())
6087 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6088 is_bitfield_ptr);
6089
6090 case clang::Type::Auto:
6091 return CompilerType(
6092 getASTContext(),
6093 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
6094 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6095 is_bitfield_ptr);
6096
6097 case clang::Type::Elaborated:
6098 return CompilerType(
6099 getASTContext(),
6100 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
6101 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6102 is_bitfield_ptr);
6103
6104 case clang::Type::Paren:
6105 return CompilerType(getASTContext(),
6106 llvm::cast<clang::ParenType>(qual_type)->desugar())
6107 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6108 is_bitfield_ptr);
6109
6110 default:
6111 break;
6112 }
6113 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006114}
6115
Greg Clayton99558cc42015-08-24 23:46:31 +00006116uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006117ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
6118 uint32_t count = 0;
6119 clang::QualType qual_type(GetCanonicalQualType(type));
6120 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6121 switch (type_class) {
6122 case clang::Type::Record:
6123 if (GetCompleteType(type)) {
6124 const clang::CXXRecordDecl *cxx_record_decl =
6125 qual_type->getAsCXXRecordDecl();
6126 if (cxx_record_decl)
6127 count = cxx_record_decl->getNumBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006128 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006129 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006130
Kate Stoneb9c1b512016-09-06 20:57:50 +00006131 case clang::Type::ObjCObjectPointer:
6132 count = GetPointeeType(type).GetNumDirectBaseClasses();
6133 break;
6134
6135 case clang::Type::ObjCObject:
6136 if (GetCompleteType(type)) {
6137 const clang::ObjCObjectType *objc_class_type =
6138 qual_type->getAsObjCQualifiedInterfaceType();
6139 if (objc_class_type) {
6140 clang::ObjCInterfaceDecl *class_interface_decl =
6141 objc_class_type->getInterface();
6142
6143 if (class_interface_decl && class_interface_decl->getSuperClass())
6144 count = 1;
6145 }
6146 }
6147 break;
6148 case clang::Type::ObjCInterface:
6149 if (GetCompleteType(type)) {
6150 const clang::ObjCInterfaceType *objc_interface_type =
6151 qual_type->getAs<clang::ObjCInterfaceType>();
6152 if (objc_interface_type) {
6153 clang::ObjCInterfaceDecl *class_interface_decl =
6154 objc_interface_type->getInterface();
6155
6156 if (class_interface_decl && class_interface_decl->getSuperClass())
6157 count = 1;
6158 }
6159 }
6160 break;
6161
6162 case clang::Type::Typedef:
6163 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6164 ->getDecl()
6165 ->getUnderlyingType()
6166 .getAsOpaquePtr());
6167 break;
6168
6169 case clang::Type::Auto:
6170 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6171 ->getDeducedType()
6172 .getAsOpaquePtr());
6173 break;
6174
6175 case clang::Type::Elaborated:
6176 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6177 ->getNamedType()
6178 .getAsOpaquePtr());
6179 break;
6180
6181 case clang::Type::Paren:
6182 return GetNumDirectBaseClasses(
6183 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6184
6185 default:
6186 break;
6187 }
6188 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006189}
6190
6191uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006192ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
6193 uint32_t count = 0;
6194 clang::QualType qual_type(GetCanonicalQualType(type));
6195 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6196 switch (type_class) {
6197 case clang::Type::Record:
6198 if (GetCompleteType(type)) {
6199 const clang::CXXRecordDecl *cxx_record_decl =
6200 qual_type->getAsCXXRecordDecl();
6201 if (cxx_record_decl)
6202 count = cxx_record_decl->getNumVBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006203 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006204 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006205
Kate Stoneb9c1b512016-09-06 20:57:50 +00006206 case clang::Type::Typedef:
6207 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6208 ->getDecl()
6209 ->getUnderlyingType()
6210 .getAsOpaquePtr());
6211 break;
6212
6213 case clang::Type::Auto:
6214 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6215 ->getDeducedType()
6216 .getAsOpaquePtr());
6217 break;
6218
6219 case clang::Type::Elaborated:
6220 count =
6221 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6222 ->getNamedType()
6223 .getAsOpaquePtr());
6224 break;
6225
6226 case clang::Type::Paren:
6227 count = GetNumVirtualBaseClasses(
6228 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6229 break;
6230
6231 default:
6232 break;
6233 }
6234 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006235}
6236
Kate Stoneb9c1b512016-09-06 20:57:50 +00006237CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
6238 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6239 clang::QualType qual_type(GetCanonicalQualType(type));
6240 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6241 switch (type_class) {
6242 case clang::Type::Record:
6243 if (GetCompleteType(type)) {
6244 const clang::CXXRecordDecl *cxx_record_decl =
6245 qual_type->getAsCXXRecordDecl();
6246 if (cxx_record_decl) {
6247 uint32_t curr_idx = 0;
6248 clang::CXXRecordDecl::base_class_const_iterator base_class,
6249 base_class_end;
6250 for (base_class = cxx_record_decl->bases_begin(),
6251 base_class_end = cxx_record_decl->bases_end();
6252 base_class != base_class_end; ++base_class, ++curr_idx) {
6253 if (curr_idx == idx) {
6254 if (bit_offset_ptr) {
6255 const clang::ASTRecordLayout &record_layout =
6256 getASTContext()->getASTRecordLayout(cxx_record_decl);
6257 const clang::CXXRecordDecl *base_class_decl =
6258 llvm::cast<clang::CXXRecordDecl>(
6259 base_class->getType()
6260 ->getAs<clang::RecordType>()
6261 ->getDecl());
6262 if (base_class->isVirtual())
6263 *bit_offset_ptr =
6264 record_layout.getVBaseClassOffset(base_class_decl)
6265 .getQuantity() *
6266 8;
6267 else
6268 *bit_offset_ptr =
6269 record_layout.getBaseClassOffset(base_class_decl)
6270 .getQuantity() *
6271 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006272 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006273 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6274 }
6275 }
6276 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006277 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006278 break;
6279
6280 case clang::Type::ObjCObjectPointer:
6281 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6282
6283 case clang::Type::ObjCObject:
6284 if (idx == 0 && GetCompleteType(type)) {
6285 const clang::ObjCObjectType *objc_class_type =
6286 qual_type->getAsObjCQualifiedInterfaceType();
6287 if (objc_class_type) {
6288 clang::ObjCInterfaceDecl *class_interface_decl =
6289 objc_class_type->getInterface();
6290
6291 if (class_interface_decl) {
6292 clang::ObjCInterfaceDecl *superclass_interface_decl =
6293 class_interface_decl->getSuperClass();
6294 if (superclass_interface_decl) {
6295 if (bit_offset_ptr)
6296 *bit_offset_ptr = 0;
6297 return CompilerType(getASTContext(),
6298 getASTContext()->getObjCInterfaceType(
6299 superclass_interface_decl));
6300 }
6301 }
6302 }
6303 }
6304 break;
6305 case clang::Type::ObjCInterface:
6306 if (idx == 0 && GetCompleteType(type)) {
6307 const clang::ObjCObjectType *objc_interface_type =
6308 qual_type->getAs<clang::ObjCInterfaceType>();
6309 if (objc_interface_type) {
6310 clang::ObjCInterfaceDecl *class_interface_decl =
6311 objc_interface_type->getInterface();
6312
6313 if (class_interface_decl) {
6314 clang::ObjCInterfaceDecl *superclass_interface_decl =
6315 class_interface_decl->getSuperClass();
6316 if (superclass_interface_decl) {
6317 if (bit_offset_ptr)
6318 *bit_offset_ptr = 0;
6319 return CompilerType(getASTContext(),
6320 getASTContext()->getObjCInterfaceType(
6321 superclass_interface_decl));
6322 }
6323 }
6324 }
6325 }
6326 break;
6327
6328 case clang::Type::Typedef:
6329 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6330 ->getDecl()
6331 ->getUnderlyingType()
6332 .getAsOpaquePtr(),
6333 idx, bit_offset_ptr);
6334
6335 case clang::Type::Auto:
6336 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6337 ->getDeducedType()
6338 .getAsOpaquePtr(),
6339 idx, bit_offset_ptr);
6340
6341 case clang::Type::Elaborated:
6342 return GetDirectBaseClassAtIndex(
6343 llvm::cast<clang::ElaboratedType>(qual_type)
6344 ->getNamedType()
6345 .getAsOpaquePtr(),
6346 idx, bit_offset_ptr);
6347
6348 case clang::Type::Paren:
6349 return GetDirectBaseClassAtIndex(
6350 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6351 idx, bit_offset_ptr);
6352
6353 default:
6354 break;
6355 }
6356 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006357}
6358
Kate Stoneb9c1b512016-09-06 20:57:50 +00006359CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6360 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6361 clang::QualType qual_type(GetCanonicalQualType(type));
6362 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6363 switch (type_class) {
6364 case clang::Type::Record:
6365 if (GetCompleteType(type)) {
6366 const clang::CXXRecordDecl *cxx_record_decl =
6367 qual_type->getAsCXXRecordDecl();
6368 if (cxx_record_decl) {
6369 uint32_t curr_idx = 0;
6370 clang::CXXRecordDecl::base_class_const_iterator base_class,
6371 base_class_end;
6372 for (base_class = cxx_record_decl->vbases_begin(),
6373 base_class_end = cxx_record_decl->vbases_end();
6374 base_class != base_class_end; ++base_class, ++curr_idx) {
6375 if (curr_idx == idx) {
6376 if (bit_offset_ptr) {
6377 const clang::ASTRecordLayout &record_layout =
6378 getASTContext()->getASTRecordLayout(cxx_record_decl);
6379 const clang::CXXRecordDecl *base_class_decl =
6380 llvm::cast<clang::CXXRecordDecl>(
6381 base_class->getType()
6382 ->getAs<clang::RecordType>()
6383 ->getDecl());
6384 *bit_offset_ptr =
6385 record_layout.getVBaseClassOffset(base_class_decl)
6386 .getQuantity() *
6387 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006388 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006389 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6390 }
6391 }
6392 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006393 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006394 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006395
Kate Stoneb9c1b512016-09-06 20:57:50 +00006396 case clang::Type::Typedef:
6397 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6398 ->getDecl()
6399 ->getUnderlyingType()
6400 .getAsOpaquePtr(),
6401 idx, bit_offset_ptr);
6402
6403 case clang::Type::Auto:
6404 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6405 ->getDeducedType()
6406 .getAsOpaquePtr(),
6407 idx, bit_offset_ptr);
6408
6409 case clang::Type::Elaborated:
6410 return GetVirtualBaseClassAtIndex(
6411 llvm::cast<clang::ElaboratedType>(qual_type)
6412 ->getNamedType()
6413 .getAsOpaquePtr(),
6414 idx, bit_offset_ptr);
6415
6416 case clang::Type::Paren:
6417 return GetVirtualBaseClassAtIndex(
6418 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6419 idx, bit_offset_ptr);
6420
6421 default:
6422 break;
6423 }
6424 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006425}
6426
Greg Claytond8d4a572015-08-11 21:38:15 +00006427// If a pointer to a pointee type (the clang_type arg) says that it has no
6428// children, then we either need to trust it, or override it and return a
6429// different result. For example, an "int *" has one child that is an integer,
6430// but a function pointer doesn't have any children. Likewise if a Record type
6431// claims it has no children, then there really is nothing to show.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006432uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6433 if (type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00006434 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006435
6436 clang::QualType qual_type(type.getCanonicalType());
6437 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6438 switch (type_class) {
6439 case clang::Type::Builtin:
6440 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6441 case clang::BuiltinType::UnknownAny:
6442 case clang::BuiltinType::Void:
6443 case clang::BuiltinType::NullPtr:
6444 case clang::BuiltinType::OCLEvent:
6445 case clang::BuiltinType::OCLImage1dRO:
6446 case clang::BuiltinType::OCLImage1dWO:
6447 case clang::BuiltinType::OCLImage1dRW:
6448 case clang::BuiltinType::OCLImage1dArrayRO:
6449 case clang::BuiltinType::OCLImage1dArrayWO:
6450 case clang::BuiltinType::OCLImage1dArrayRW:
6451 case clang::BuiltinType::OCLImage1dBufferRO:
6452 case clang::BuiltinType::OCLImage1dBufferWO:
6453 case clang::BuiltinType::OCLImage1dBufferRW:
6454 case clang::BuiltinType::OCLImage2dRO:
6455 case clang::BuiltinType::OCLImage2dWO:
6456 case clang::BuiltinType::OCLImage2dRW:
6457 case clang::BuiltinType::OCLImage2dArrayRO:
6458 case clang::BuiltinType::OCLImage2dArrayWO:
6459 case clang::BuiltinType::OCLImage2dArrayRW:
6460 case clang::BuiltinType::OCLImage3dRO:
6461 case clang::BuiltinType::OCLImage3dWO:
6462 case clang::BuiltinType::OCLImage3dRW:
6463 case clang::BuiltinType::OCLSampler:
6464 return 0;
6465 case clang::BuiltinType::Bool:
6466 case clang::BuiltinType::Char_U:
6467 case clang::BuiltinType::UChar:
6468 case clang::BuiltinType::WChar_U:
6469 case clang::BuiltinType::Char16:
6470 case clang::BuiltinType::Char32:
6471 case clang::BuiltinType::UShort:
6472 case clang::BuiltinType::UInt:
6473 case clang::BuiltinType::ULong:
6474 case clang::BuiltinType::ULongLong:
6475 case clang::BuiltinType::UInt128:
6476 case clang::BuiltinType::Char_S:
6477 case clang::BuiltinType::SChar:
6478 case clang::BuiltinType::WChar_S:
6479 case clang::BuiltinType::Short:
6480 case clang::BuiltinType::Int:
6481 case clang::BuiltinType::Long:
6482 case clang::BuiltinType::LongLong:
6483 case clang::BuiltinType::Int128:
6484 case clang::BuiltinType::Float:
6485 case clang::BuiltinType::Double:
6486 case clang::BuiltinType::LongDouble:
6487 case clang::BuiltinType::Dependent:
6488 case clang::BuiltinType::Overload:
6489 case clang::BuiltinType::ObjCId:
6490 case clang::BuiltinType::ObjCClass:
6491 case clang::BuiltinType::ObjCSel:
6492 case clang::BuiltinType::BoundMember:
6493 case clang::BuiltinType::Half:
6494 case clang::BuiltinType::ARCUnbridgedCast:
6495 case clang::BuiltinType::PseudoObject:
6496 case clang::BuiltinType::BuiltinFn:
6497 case clang::BuiltinType::OMPArraySection:
6498 return 1;
6499 default:
6500 return 0;
6501 }
6502 break;
6503
6504 case clang::Type::Complex:
6505 return 1;
6506 case clang::Type::Pointer:
6507 return 1;
6508 case clang::Type::BlockPointer:
6509 return 0; // If block pointers don't have debug info, then no children for
6510 // them
6511 case clang::Type::LValueReference:
6512 return 1;
6513 case clang::Type::RValueReference:
6514 return 1;
6515 case clang::Type::MemberPointer:
6516 return 0;
6517 case clang::Type::ConstantArray:
6518 return 0;
6519 case clang::Type::IncompleteArray:
6520 return 0;
6521 case clang::Type::VariableArray:
6522 return 0;
6523 case clang::Type::DependentSizedArray:
6524 return 0;
6525 case clang::Type::DependentSizedExtVector:
6526 return 0;
6527 case clang::Type::Vector:
6528 return 0;
6529 case clang::Type::ExtVector:
6530 return 0;
6531 case clang::Type::FunctionProto:
6532 return 0; // When we function pointers, they have no children...
6533 case clang::Type::FunctionNoProto:
6534 return 0; // When we function pointers, they have no children...
6535 case clang::Type::UnresolvedUsing:
6536 return 0;
6537 case clang::Type::Paren:
6538 return GetNumPointeeChildren(
6539 llvm::cast<clang::ParenType>(qual_type)->desugar());
6540 case clang::Type::Typedef:
6541 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6542 ->getDecl()
6543 ->getUnderlyingType());
6544 case clang::Type::Auto:
6545 return GetNumPointeeChildren(
6546 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6547 case clang::Type::Elaborated:
6548 return GetNumPointeeChildren(
6549 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6550 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006551 return GetNumPointeeChildren(llvm::cast<clang::TypeOfExprType>(qual_type)
6552 ->getUnderlyingExpr()
6553 ->getType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006554 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006555 return GetNumPointeeChildren(
6556 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006557 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006558 return GetNumPointeeChildren(
6559 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006560 case clang::Type::Record:
6561 return 0;
6562 case clang::Type::Enum:
6563 return 1;
6564 case clang::Type::TemplateTypeParm:
6565 return 1;
6566 case clang::Type::SubstTemplateTypeParm:
6567 return 1;
6568 case clang::Type::TemplateSpecialization:
6569 return 1;
6570 case clang::Type::InjectedClassName:
6571 return 0;
6572 case clang::Type::DependentName:
6573 return 1;
6574 case clang::Type::DependentTemplateSpecialization:
6575 return 1;
6576 case clang::Type::ObjCObject:
6577 return 0;
6578 case clang::Type::ObjCInterface:
6579 return 0;
6580 case clang::Type::ObjCObjectPointer:
6581 return 1;
6582 default:
6583 break;
6584 }
6585 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006586}
6587
Kate Stoneb9c1b512016-09-06 20:57:50 +00006588CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6589 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6590 bool transparent_pointers, bool omit_empty_base_classes,
6591 bool ignore_array_bounds, std::string &child_name,
6592 uint32_t &child_byte_size, int32_t &child_byte_offset,
6593 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6594 bool &child_is_base_class, bool &child_is_deref_of_parent,
6595 ValueObject *valobj, uint64_t &language_flags) {
6596 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006597 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006598
Kate Stoneb9c1b512016-09-06 20:57:50 +00006599 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6600 const clang::Type::TypeClass parent_type_class =
6601 parent_qual_type->getTypeClass();
6602 child_bitfield_bit_size = 0;
6603 child_bitfield_bit_offset = 0;
6604 child_is_base_class = false;
6605 language_flags = 0;
6606
Adrian Prantleca07c52018-11-05 20:49:07 +00006607 const bool idx_is_valid =
6608 idx < GetNumChildren(type, omit_empty_base_classes, exe_ctx);
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00006609 int32_t bit_offset;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006610 switch (parent_type_class) {
6611 case clang::Type::Builtin:
6612 if (idx_is_valid) {
6613 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6614 case clang::BuiltinType::ObjCId:
6615 case clang::BuiltinType::ObjCClass:
6616 child_name = "isa";
6617 child_byte_size =
6618 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6619 CHAR_BIT;
6620 return CompilerType(getASTContext(),
6621 getASTContext()->ObjCBuiltinClassTy);
6622
6623 default:
6624 break;
6625 }
6626 }
6627 break;
6628
6629 case clang::Type::Record:
6630 if (idx_is_valid && GetCompleteType(type)) {
6631 const clang::RecordType *record_type =
6632 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6633 const clang::RecordDecl *record_decl = record_type->getDecl();
6634 assert(record_decl);
6635 const clang::ASTRecordLayout &record_layout =
6636 getASTContext()->getASTRecordLayout(record_decl);
6637 uint32_t child_idx = 0;
6638
6639 const clang::CXXRecordDecl *cxx_record_decl =
6640 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6641 if (cxx_record_decl) {
6642 // We might have base classes to print out first
6643 clang::CXXRecordDecl::base_class_const_iterator base_class,
6644 base_class_end;
6645 for (base_class = cxx_record_decl->bases_begin(),
6646 base_class_end = cxx_record_decl->bases_end();
6647 base_class != base_class_end; ++base_class) {
6648 const clang::CXXRecordDecl *base_class_decl = nullptr;
6649
6650 // Skip empty base classes
6651 if (omit_empty_base_classes) {
6652 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6653 base_class->getType()->getAs<clang::RecordType>()->getDecl());
Jonas Devliegherea6682a42018-12-15 00:15:33 +00006654 if (!ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00006655 continue;
6656 }
6657
6658 if (idx == child_idx) {
6659 if (base_class_decl == nullptr)
6660 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6661 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6662
6663 if (base_class->isVirtual()) {
6664 bool handled = false;
6665 if (valobj) {
Aleksandr Urakov1dc51db2018-11-12 16:23:50 +00006666 clang::VTableContextBase *vtable_ctx =
6667 getASTContext()->getVTableContext();
6668 if (vtable_ctx)
6669 handled = GetVBaseBitOffset(*vtable_ctx, *valobj,
6670 record_layout, cxx_record_decl,
6671 base_class_decl, bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006672 }
6673 if (!handled)
6674 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6675 .getQuantity() *
6676 8;
6677 } else
6678 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6679 .getQuantity() *
6680 8;
6681
6682 // Base classes should be a multiple of 8 bits in size
6683 child_byte_offset = bit_offset / 8;
6684 CompilerType base_class_clang_type(getASTContext(),
6685 base_class->getType());
6686 child_name = base_class_clang_type.GetTypeName().AsCString("");
Adrian Prantld963a7c2019-01-15 18:07:52 +00006687 auto size = base_class_clang_type.GetBitSize(
6688 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6689 if (!size)
6690 return {};
6691 uint64_t base_class_clang_type_bit_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006692
6693 // Base classes bit sizes should be a multiple of 8 bits in size
6694 assert(base_class_clang_type_bit_size % 8 == 0);
6695 child_byte_size = base_class_clang_type_bit_size / 8;
6696 child_is_base_class = true;
6697 return base_class_clang_type;
6698 }
6699 // We don't increment the child index in the for loop since we might
6700 // be skipping empty base classes
6701 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006702 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006703 }
6704 // Make sure index is in range...
6705 uint32_t field_idx = 0;
6706 clang::RecordDecl::field_iterator field, field_end;
6707 for (field = record_decl->field_begin(),
6708 field_end = record_decl->field_end();
6709 field != field_end; ++field, ++field_idx, ++child_idx) {
6710 if (idx == child_idx) {
6711 // Print the member type if requested
6712 // Print the member name and equal sign
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006713 child_name.assign(field->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006714
6715 // Figure out the type byte size (field_type_info.first) and
6716 // alignment (field_type_info.second) from the AST context.
6717 CompilerType field_clang_type(getASTContext(), field->getType());
6718 assert(field_idx < record_layout.getFieldCount());
Adrian Prantld963a7c2019-01-15 18:07:52 +00006719 auto size = field_clang_type.GetByteSize(
Kate Stoneb9c1b512016-09-06 20:57:50 +00006720 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
Adrian Prantld963a7c2019-01-15 18:07:52 +00006721 if (!size)
6722 return {};
6723 child_byte_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006724 const uint32_t child_bit_size = child_byte_size * 8;
6725
6726 // Figure out the field offset within the current struct/union/class
6727 // type
6728 bit_offset = record_layout.getFieldOffset(field_idx);
6729 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6730 child_bitfield_bit_size)) {
6731 child_bitfield_bit_offset = bit_offset % child_bit_size;
6732 const uint32_t child_bit_offset =
6733 bit_offset - child_bitfield_bit_offset;
6734 child_byte_offset = child_bit_offset / 8;
6735 } else {
6736 child_byte_offset = bit_offset / 8;
6737 }
6738
6739 return field_clang_type;
6740 }
6741 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006742 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006743 break;
6744
6745 case clang::Type::ObjCObject:
6746 case clang::Type::ObjCInterface:
6747 if (idx_is_valid && GetCompleteType(type)) {
6748 const clang::ObjCObjectType *objc_class_type =
6749 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6750 assert(objc_class_type);
6751 if (objc_class_type) {
6752 uint32_t child_idx = 0;
6753 clang::ObjCInterfaceDecl *class_interface_decl =
6754 objc_class_type->getInterface();
6755
6756 if (class_interface_decl) {
6757
6758 const clang::ASTRecordLayout &interface_layout =
6759 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6760 clang::ObjCInterfaceDecl *superclass_interface_decl =
6761 class_interface_decl->getSuperClass();
6762 if (superclass_interface_decl) {
6763 if (omit_empty_base_classes) {
6764 CompilerType base_class_clang_type(
6765 getASTContext(), getASTContext()->getObjCInterfaceType(
6766 superclass_interface_decl));
Adrian Prantleca07c52018-11-05 20:49:07 +00006767 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes,
6768 exe_ctx) > 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006769 if (idx == 0) {
6770 clang::QualType ivar_qual_type(
6771 getASTContext()->getObjCInterfaceType(
6772 superclass_interface_decl));
6773
6774 child_name.assign(
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006775 superclass_interface_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006776
6777 clang::TypeInfo ivar_type_info =
6778 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6779
6780 child_byte_size = ivar_type_info.Width / 8;
6781 child_byte_offset = 0;
6782 child_is_base_class = true;
6783
6784 return CompilerType(getASTContext(), ivar_qual_type);
6785 }
6786
6787 ++child_idx;
6788 }
6789 } else
6790 ++child_idx;
6791 }
6792
6793 const uint32_t superclass_idx = child_idx;
6794
6795 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6796 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6797 ivar_end = class_interface_decl->ivar_end();
6798
6799 for (ivar_pos = class_interface_decl->ivar_begin();
6800 ivar_pos != ivar_end; ++ivar_pos) {
6801 if (child_idx == idx) {
6802 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6803
6804 clang::QualType ivar_qual_type(ivar_decl->getType());
6805
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006806 child_name.assign(ivar_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006807
6808 clang::TypeInfo ivar_type_info =
6809 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6810
6811 child_byte_size = ivar_type_info.Width / 8;
6812
6813 // Figure out the field offset within the current
Adrian Prantl05097242018-04-30 16:49:04 +00006814 // struct/union/class type For ObjC objects, we can't trust the
6815 // bit offset we get from the Clang AST, since that doesn't
6816 // account for the space taken up by unbacked properties, or
6817 // from the changing size of base classes that are newer than
6818 // this class. So if we have a process around that we can ask
6819 // about this object, do so.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006820 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6821 Process *process = nullptr;
6822 if (exe_ctx)
6823 process = exe_ctx->GetProcessPtr();
6824 if (process) {
6825 ObjCLanguageRuntime *objc_runtime =
6826 process->GetObjCLanguageRuntime();
6827 if (objc_runtime != nullptr) {
6828 CompilerType parent_ast_type(getASTContext(),
6829 parent_qual_type);
6830 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6831 parent_ast_type, ivar_decl->getNameAsString().c_str());
6832 }
6833 }
6834
Aleksandr Urakovff701722018-08-20 05:59:27 +00006835 // Setting this to INT32_MAX to make sure we don't compute it
Kate Stoneb9c1b512016-09-06 20:57:50 +00006836 // twice...
Aleksandr Urakov53459482018-08-17 07:28:24 +00006837 bit_offset = INT32_MAX;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006838
6839 if (child_byte_offset ==
6840 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6841 bit_offset = interface_layout.getFieldOffset(child_idx -
6842 superclass_idx);
6843 child_byte_offset = bit_offset / 8;
6844 }
6845
6846 // Note, the ObjC Ivar Byte offset is just that, it doesn't
Adrian Prantl05097242018-04-30 16:49:04 +00006847 // account for the bit offset of a bitfield within its
6848 // containing object. So regardless of where we get the byte
Kate Stoneb9c1b512016-09-06 20:57:50 +00006849 // offset from, we still need to get the bit offset for
6850 // bitfields from the layout.
6851
6852 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6853 child_bitfield_bit_size)) {
Aleksandr Urakov53459482018-08-17 07:28:24 +00006854 if (bit_offset == INT32_MAX)
Kate Stoneb9c1b512016-09-06 20:57:50 +00006855 bit_offset = interface_layout.getFieldOffset(
6856 child_idx - superclass_idx);
6857
6858 child_bitfield_bit_offset = bit_offset % 8;
6859 }
6860 return CompilerType(getASTContext(), ivar_qual_type);
6861 }
6862 ++child_idx;
6863 }
6864 }
6865 }
6866 }
6867 }
6868 break;
6869
6870 case clang::Type::ObjCObjectPointer:
6871 if (idx_is_valid) {
6872 CompilerType pointee_clang_type(GetPointeeType(type));
6873
6874 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6875 child_is_deref_of_parent = false;
6876 bool tmp_child_is_deref_of_parent = false;
6877 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6878 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6879 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6880 child_bitfield_bit_size, child_bitfield_bit_offset,
6881 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6882 language_flags);
6883 } else {
6884 child_is_deref_of_parent = true;
6885 const char *parent_name =
6886 valobj ? valobj->GetName().GetCString() : NULL;
6887 if (parent_name) {
6888 child_name.assign(1, '*');
6889 child_name += parent_name;
6890 }
6891
6892 // We have a pointer to an simple type
6893 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006894 if (auto size = pointee_clang_type.GetByteSize(
6895 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL)) {
6896 child_byte_size = *size;
6897 child_byte_offset = 0;
6898 return pointee_clang_type;
6899 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006900 }
6901 }
6902 }
6903 break;
6904
6905 case clang::Type::Vector:
6906 case clang::Type::ExtVector:
6907 if (idx_is_valid) {
6908 const clang::VectorType *array =
6909 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6910 if (array) {
6911 CompilerType element_type(getASTContext(), array->getElementType());
6912 if (element_type.GetCompleteType()) {
6913 char element_name[64];
6914 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6915 static_cast<uint64_t>(idx));
6916 child_name.assign(element_name);
Adrian Prantld963a7c2019-01-15 18:07:52 +00006917 if (auto size = element_type.GetByteSize(
6918 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL)) {
6919 child_byte_size = *size;
6920 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6921 return element_type;
6922 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006923 }
6924 }
6925 }
6926 break;
6927
6928 case clang::Type::ConstantArray:
6929 case clang::Type::IncompleteArray:
6930 if (ignore_array_bounds || idx_is_valid) {
6931 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6932 if (array) {
6933 CompilerType element_type(getASTContext(), array->getElementType());
6934 if (element_type.GetCompleteType()) {
Zachary Turner827d5d72016-12-16 04:27:00 +00006935 child_name = llvm::formatv("[{0}]", idx);
Adrian Prantld963a7c2019-01-15 18:07:52 +00006936 if (auto size = element_type.GetByteSize(
6937 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL)) {
6938 child_byte_size = *size;
6939 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6940 return element_type;
6941 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006942 }
6943 }
6944 }
6945 break;
6946
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006947 case clang::Type::Pointer: {
6948 CompilerType pointee_clang_type(GetPointeeType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00006949
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006950 // Don't dereference "void *" pointers
6951 if (pointee_clang_type.IsVoidType())
6952 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006953
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006954 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6955 child_is_deref_of_parent = false;
6956 bool tmp_child_is_deref_of_parent = false;
6957 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6958 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6959 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6960 child_bitfield_bit_size, child_bitfield_bit_offset,
6961 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6962 language_flags);
6963 } else {
6964 child_is_deref_of_parent = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006965
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006966 const char *parent_name =
6967 valobj ? valobj->GetName().GetCString() : NULL;
6968 if (parent_name) {
6969 child_name.assign(1, '*');
6970 child_name += parent_name;
6971 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006972
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006973 // We have a pointer to an simple type
6974 if (idx == 0) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006975 if (auto size = pointee_clang_type.GetByteSize(
6976 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL)) {
6977 child_byte_size = *size;
6978 child_byte_offset = 0;
6979 return pointee_clang_type;
6980 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006981 }
6982 }
6983 break;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006984 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006985
6986 case clang::Type::LValueReference:
6987 case clang::Type::RValueReference:
6988 if (idx_is_valid) {
6989 const clang::ReferenceType *reference_type =
6990 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
6991 CompilerType pointee_clang_type(getASTContext(),
6992 reference_type->getPointeeType());
6993 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6994 child_is_deref_of_parent = false;
6995 bool tmp_child_is_deref_of_parent = false;
6996 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6997 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6998 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6999 child_bitfield_bit_size, child_bitfield_bit_offset,
7000 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
7001 language_flags);
7002 } else {
7003 const char *parent_name =
7004 valobj ? valobj->GetName().GetCString() : NULL;
7005 if (parent_name) {
7006 child_name.assign(1, '&');
7007 child_name += parent_name;
7008 }
7009
7010 // We have a pointer to an simple type
7011 if (idx == 0) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00007012 if (auto size = pointee_clang_type.GetByteSize(
7013 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL)) {
7014 child_byte_size = *size;
7015 child_byte_offset = 0;
7016 return pointee_clang_type;
7017 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007018 }
7019 }
7020 }
7021 break;
7022
7023 case clang::Type::Typedef: {
7024 CompilerType typedefed_clang_type(
7025 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)
7026 ->getDecl()
7027 ->getUnderlyingType());
7028 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
7029 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7030 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7031 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7032 child_is_deref_of_parent, valobj, language_flags);
7033 } break;
7034
7035 case clang::Type::Auto: {
7036 CompilerType elaborated_clang_type(
7037 getASTContext(),
7038 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
7039 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
7040 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7041 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7042 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7043 child_is_deref_of_parent, valobj, language_flags);
7044 }
7045
7046 case clang::Type::Elaborated: {
7047 CompilerType elaborated_clang_type(
7048 getASTContext(),
7049 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
7050 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
7051 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7052 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7053 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7054 child_is_deref_of_parent, valobj, language_flags);
7055 }
7056
7057 case clang::Type::Paren: {
7058 CompilerType paren_clang_type(
7059 getASTContext(),
7060 llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
7061 return paren_clang_type.GetChildCompilerTypeAtIndex(
7062 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7063 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7064 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7065 child_is_deref_of_parent, valobj, language_flags);
7066 }
7067
7068 default:
7069 break;
7070 }
7071 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007072}
7073
Kate Stoneb9c1b512016-09-06 20:57:50 +00007074static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
7075 const clang::CXXBaseSpecifier *base_spec,
7076 bool omit_empty_base_classes) {
7077 uint32_t child_idx = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007078
Kate Stoneb9c1b512016-09-06 20:57:50 +00007079 const clang::CXXRecordDecl *cxx_record_decl =
7080 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7081
7082 // const char *super_name = record_decl->getNameAsCString();
7083 // const char *base_name =
7084 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
7085 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
7086 //
7087 if (cxx_record_decl) {
7088 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
7089 for (base_class = cxx_record_decl->bases_begin(),
7090 base_class_end = cxx_record_decl->bases_end();
7091 base_class != base_class_end; ++base_class) {
7092 if (omit_empty_base_classes) {
7093 if (BaseSpecifierIsEmpty(base_class))
7094 continue;
7095 }
7096
7097 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
7098 // super_name, base_name,
7099 // child_idx,
7100 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
7101 //
7102 //
7103 if (base_class == base_spec)
7104 return child_idx;
7105 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00007106 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007107 }
7108
7109 return UINT32_MAX;
7110}
7111
7112static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
7113 clang::NamedDecl *canonical_decl,
7114 bool omit_empty_base_classes) {
7115 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
7116 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
7117 omit_empty_base_classes);
7118
7119 clang::RecordDecl::field_iterator field, field_end;
7120 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
7121 field != field_end; ++field, ++child_idx) {
7122 if (field->getCanonicalDecl() == canonical_decl)
7123 return child_idx;
7124 }
7125
7126 return UINT32_MAX;
Greg Claytond8d4a572015-08-11 21:38:15 +00007127}
7128
7129// Look for a child member (doesn't include base classes, but it does include
Adrian Prantl05097242018-04-30 16:49:04 +00007130// their members) in the type hierarchy. Returns an index path into
7131// "clang_type" on how to reach the appropriate member.
Greg Claytond8d4a572015-08-11 21:38:15 +00007132//
7133// class A
7134// {
7135// public:
7136// int m_a;
7137// int m_b;
7138// };
7139//
7140// class B
7141// {
7142// };
7143//
7144// class C :
7145// public B,
7146// public A
7147// {
7148// };
7149//
7150// If we have a clang type that describes "class C", and we wanted to looked
7151// "m_b" in it:
7152//
Kate Stoneb9c1b512016-09-06 20:57:50 +00007153// With omit_empty_base_classes == false we would get an integer array back
Adrian Prantl05097242018-04-30 16:49:04 +00007154// with: { 1, 1 } The first index 1 is the child index for "class A" within
7155// class C The second index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007156//
Adrian Prantl05097242018-04-30 16:49:04 +00007157// With omit_empty_base_classes == true we would get an integer array back
7158// with: { 0, 1 } The first index 0 is the child index for "class A" within
7159// class C (since class B doesn't have any members it doesn't count) The second
7160// index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007161
Kate Stoneb9c1b512016-09-06 20:57:50 +00007162size_t ClangASTContext::GetIndexOfChildMemberWithName(
7163 lldb::opaque_compiler_type_t type, const char *name,
7164 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
7165 if (type && name && name[0]) {
7166 clang::QualType qual_type(GetCanonicalQualType(type));
7167 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7168 switch (type_class) {
7169 case clang::Type::Record:
7170 if (GetCompleteType(type)) {
7171 const clang::RecordType *record_type =
7172 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7173 const clang::RecordDecl *record_decl = record_type->getDecl();
Enrico Granata36f51e42015-12-18 22:41:25 +00007174
Kate Stoneb9c1b512016-09-06 20:57:50 +00007175 assert(record_decl);
7176 uint32_t child_idx = 0;
7177
7178 const clang::CXXRecordDecl *cxx_record_decl =
7179 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7180
7181 // Try and find a field that matches NAME
7182 clang::RecordDecl::field_iterator field, field_end;
7183 llvm::StringRef name_sref(name);
7184 for (field = record_decl->field_begin(),
7185 field_end = record_decl->field_end();
7186 field != field_end; ++field, ++child_idx) {
7187 llvm::StringRef field_name = field->getName();
7188 if (field_name.empty()) {
7189 CompilerType field_type(getASTContext(), field->getType());
7190 child_indexes.push_back(child_idx);
7191 if (field_type.GetIndexOfChildMemberWithName(
7192 name, omit_empty_base_classes, child_indexes))
7193 return child_indexes.size();
7194 child_indexes.pop_back();
7195
7196 } else if (field_name.equals(name_sref)) {
7197 // We have to add on the number of base classes to this index!
7198 child_indexes.push_back(
7199 child_idx + ClangASTContext::GetNumBaseClasses(
7200 cxx_record_decl, omit_empty_base_classes));
7201 return child_indexes.size();
7202 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007203 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007204
Kate Stoneb9c1b512016-09-06 20:57:50 +00007205 if (cxx_record_decl) {
7206 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7207
7208 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7209
7210 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7211 // Didn't find things easily, lets let clang do its thang...
7212 clang::IdentifierInfo &ident_ref =
7213 getASTContext()->Idents.get(name_sref);
7214 clang::DeclarationName decl_name(&ident_ref);
7215
7216 clang::CXXBasePaths paths;
7217 if (cxx_record_decl->lookupInBases(
7218 [decl_name](const clang::CXXBaseSpecifier *specifier,
7219 clang::CXXBasePath &path) {
7220 return clang::CXXRecordDecl::FindOrdinaryMember(
7221 specifier, path, decl_name);
7222 },
7223 paths)) {
7224 clang::CXXBasePaths::const_paths_iterator path,
7225 path_end = paths.end();
7226 for (path = paths.begin(); path != path_end; ++path) {
7227 const size_t num_path_elements = path->size();
7228 for (size_t e = 0; e < num_path_elements; ++e) {
7229 clang::CXXBasePathElement elem = (*path)[e];
7230
7231 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7232 omit_empty_base_classes);
7233 if (child_idx == UINT32_MAX) {
7234 child_indexes.clear();
7235 return 0;
7236 } else {
7237 child_indexes.push_back(child_idx);
7238 parent_record_decl = llvm::cast<clang::RecordDecl>(
7239 elem.Base->getType()
7240 ->getAs<clang::RecordType>()
7241 ->getDecl());
7242 }
7243 }
7244 for (clang::NamedDecl *path_decl : path->Decls) {
7245 child_idx = GetIndexForRecordChild(
7246 parent_record_decl, path_decl, omit_empty_base_classes);
7247 if (child_idx == UINT32_MAX) {
7248 child_indexes.clear();
7249 return 0;
7250 } else {
7251 child_indexes.push_back(child_idx);
7252 }
7253 }
7254 }
7255 return child_indexes.size();
7256 }
7257 }
7258 }
7259 break;
7260
7261 case clang::Type::ObjCObject:
7262 case clang::Type::ObjCInterface:
7263 if (GetCompleteType(type)) {
7264 llvm::StringRef name_sref(name);
7265 const clang::ObjCObjectType *objc_class_type =
7266 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7267 assert(objc_class_type);
7268 if (objc_class_type) {
7269 uint32_t child_idx = 0;
7270 clang::ObjCInterfaceDecl *class_interface_decl =
7271 objc_class_type->getInterface();
7272
7273 if (class_interface_decl) {
7274 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7275 ivar_end = class_interface_decl->ivar_end();
7276 clang::ObjCInterfaceDecl *superclass_interface_decl =
7277 class_interface_decl->getSuperClass();
7278
7279 for (ivar_pos = class_interface_decl->ivar_begin();
7280 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7281 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7282
7283 if (ivar_decl->getName().equals(name_sref)) {
7284 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7285 (omit_empty_base_classes &&
7286 ObjCDeclHasIVars(superclass_interface_decl, true)))
7287 ++child_idx;
7288
7289 child_indexes.push_back(child_idx);
7290 return child_indexes.size();
7291 }
7292 }
7293
7294 if (superclass_interface_decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00007295 // The super class index is always zero for ObjC classes, so we
7296 // push it onto the child indexes in case we find an ivar in our
7297 // superclass...
Kate Stoneb9c1b512016-09-06 20:57:50 +00007298 child_indexes.push_back(0);
7299
7300 CompilerType superclass_clang_type(
7301 getASTContext(), getASTContext()->getObjCInterfaceType(
7302 superclass_interface_decl));
7303 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7304 name, omit_empty_base_classes, child_indexes)) {
Adrian Prantl05097242018-04-30 16:49:04 +00007305 // We did find an ivar in a superclass so just return the
7306 // results!
Kate Stoneb9c1b512016-09-06 20:57:50 +00007307 return child_indexes.size();
7308 }
7309
Adrian Prantl05097242018-04-30 16:49:04 +00007310 // We didn't find an ivar matching "name" in our superclass, pop
7311 // the superclass zero index that we pushed on above.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007312 child_indexes.pop_back();
7313 }
7314 }
7315 }
7316 }
7317 break;
7318
7319 case clang::Type::ObjCObjectPointer: {
7320 CompilerType objc_object_clang_type(
7321 getASTContext(),
7322 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7323 ->getPointeeType());
7324 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7325 name, omit_empty_base_classes, child_indexes);
7326 } break;
7327
7328 case clang::Type::ConstantArray: {
7329 // const clang::ConstantArrayType *array =
7330 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7331 // const uint64_t element_count =
7332 // array->getSize().getLimitedValue();
7333 //
7334 // if (idx < element_count)
7335 // {
7336 // std::pair<uint64_t, unsigned> field_type_info =
7337 // ast->getTypeInfo(array->getElementType());
7338 //
7339 // char element_name[32];
7340 // ::snprintf (element_name, sizeof (element_name),
7341 // "%s[%u]", parent_name ? parent_name : "", idx);
7342 //
7343 // child_name.assign(element_name);
7344 // assert(field_type_info.first % 8 == 0);
7345 // child_byte_size = field_type_info.first / 8;
7346 // child_byte_offset = idx * child_byte_size;
7347 // return array->getElementType().getAsOpaquePtr();
7348 // }
7349 } break;
7350
7351 // case clang::Type::MemberPointerType:
7352 // {
7353 // MemberPointerType *mem_ptr_type =
7354 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7355 // clang::QualType pointee_type =
7356 // mem_ptr_type->getPointeeType();
7357 //
7358 // if (ClangASTContext::IsAggregateType
7359 // (pointee_type.getAsOpaquePtr()))
7360 // {
7361 // return GetIndexOfChildWithName (ast,
7362 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7363 // name);
7364 // }
7365 // }
7366 // break;
7367 //
7368 case clang::Type::LValueReference:
7369 case clang::Type::RValueReference: {
7370 const clang::ReferenceType *reference_type =
7371 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7372 clang::QualType pointee_type(reference_type->getPointeeType());
7373 CompilerType pointee_clang_type(getASTContext(), pointee_type);
7374
7375 if (pointee_clang_type.IsAggregateType()) {
7376 return pointee_clang_type.GetIndexOfChildMemberWithName(
7377 name, omit_empty_base_classes, child_indexes);
7378 }
7379 } break;
7380
7381 case clang::Type::Pointer: {
7382 CompilerType pointee_clang_type(GetPointeeType(type));
7383
7384 if (pointee_clang_type.IsAggregateType()) {
7385 return pointee_clang_type.GetIndexOfChildMemberWithName(
7386 name, omit_empty_base_classes, child_indexes);
7387 }
7388 } break;
7389
7390 case clang::Type::Typedef:
7391 return CompilerType(getASTContext(),
7392 llvm::cast<clang::TypedefType>(qual_type)
7393 ->getDecl()
7394 ->getUnderlyingType())
7395 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7396 child_indexes);
7397
7398 case clang::Type::Auto:
7399 return CompilerType(
7400 getASTContext(),
7401 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7402 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7403 child_indexes);
7404
7405 case clang::Type::Elaborated:
7406 return CompilerType(
7407 getASTContext(),
7408 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7409 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7410 child_indexes);
7411
7412 case clang::Type::Paren:
7413 return CompilerType(getASTContext(),
7414 llvm::cast<clang::ParenType>(qual_type)->desugar())
7415 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7416 child_indexes);
7417
7418 default:
7419 break;
7420 }
7421 }
7422 return 0;
7423}
Greg Claytond8d4a572015-08-11 21:38:15 +00007424
7425// Get the index of the child of "clang_type" whose name matches. This function
7426// doesn't descend into the children, but only looks one level deep and name
7427// matches can include base class names.
7428
7429uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007430ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7431 const char *name,
7432 bool omit_empty_base_classes) {
7433 if (type && name && name[0]) {
7434 clang::QualType qual_type(GetCanonicalQualType(type));
Enrico Granata36f51e42015-12-18 22:41:25 +00007435
Kate Stoneb9c1b512016-09-06 20:57:50 +00007436 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7437
7438 switch (type_class) {
7439 case clang::Type::Record:
7440 if (GetCompleteType(type)) {
7441 const clang::RecordType *record_type =
7442 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7443 const clang::RecordDecl *record_decl = record_type->getDecl();
7444
7445 assert(record_decl);
7446 uint32_t child_idx = 0;
7447
7448 const clang::CXXRecordDecl *cxx_record_decl =
7449 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7450
7451 if (cxx_record_decl) {
7452 clang::CXXRecordDecl::base_class_const_iterator base_class,
7453 base_class_end;
7454 for (base_class = cxx_record_decl->bases_begin(),
7455 base_class_end = cxx_record_decl->bases_end();
7456 base_class != base_class_end; ++base_class) {
7457 // Skip empty base classes
7458 clang::CXXRecordDecl *base_class_decl =
7459 llvm::cast<clang::CXXRecordDecl>(
7460 base_class->getType()
7461 ->getAs<clang::RecordType>()
7462 ->getDecl());
7463 if (omit_empty_base_classes &&
Jonas Devliegherea6682a42018-12-15 00:15:33 +00007464 !ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00007465 continue;
7466
7467 CompilerType base_class_clang_type(getASTContext(),
7468 base_class->getType());
7469 std::string base_class_type_name(
7470 base_class_clang_type.GetTypeName().AsCString(""));
Jonas Devlieghere8d20cfd2018-12-21 22:46:10 +00007471 if (base_class_type_name == name)
Kate Stoneb9c1b512016-09-06 20:57:50 +00007472 return child_idx;
7473 ++child_idx;
7474 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007475 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007476
Kate Stoneb9c1b512016-09-06 20:57:50 +00007477 // Try and find a field that matches NAME
7478 clang::RecordDecl::field_iterator field, field_end;
7479 llvm::StringRef name_sref(name);
7480 for (field = record_decl->field_begin(),
7481 field_end = record_decl->field_end();
7482 field != field_end; ++field, ++child_idx) {
7483 if (field->getName().equals(name_sref))
7484 return child_idx;
7485 }
7486 }
7487 break;
7488
7489 case clang::Type::ObjCObject:
7490 case clang::Type::ObjCInterface:
7491 if (GetCompleteType(type)) {
7492 llvm::StringRef name_sref(name);
7493 const clang::ObjCObjectType *objc_class_type =
7494 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7495 assert(objc_class_type);
7496 if (objc_class_type) {
7497 uint32_t child_idx = 0;
7498 clang::ObjCInterfaceDecl *class_interface_decl =
7499 objc_class_type->getInterface();
7500
7501 if (class_interface_decl) {
7502 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7503 ivar_end = class_interface_decl->ivar_end();
7504 clang::ObjCInterfaceDecl *superclass_interface_decl =
7505 class_interface_decl->getSuperClass();
7506
7507 for (ivar_pos = class_interface_decl->ivar_begin();
7508 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7509 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7510
7511 if (ivar_decl->getName().equals(name_sref)) {
7512 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7513 (omit_empty_base_classes &&
7514 ObjCDeclHasIVars(superclass_interface_decl, true)))
7515 ++child_idx;
7516
7517 return child_idx;
7518 }
7519 }
7520
7521 if (superclass_interface_decl) {
7522 if (superclass_interface_decl->getName().equals(name_sref))
7523 return 0;
7524 }
7525 }
7526 }
7527 }
7528 break;
7529
7530 case clang::Type::ObjCObjectPointer: {
7531 CompilerType pointee_clang_type(
7532 getASTContext(),
7533 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7534 ->getPointeeType());
7535 return pointee_clang_type.GetIndexOfChildWithName(
7536 name, omit_empty_base_classes);
7537 } break;
7538
7539 case clang::Type::ConstantArray: {
7540 // const clang::ConstantArrayType *array =
7541 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7542 // const uint64_t element_count =
7543 // array->getSize().getLimitedValue();
7544 //
7545 // if (idx < element_count)
7546 // {
7547 // std::pair<uint64_t, unsigned> field_type_info =
7548 // ast->getTypeInfo(array->getElementType());
7549 //
7550 // char element_name[32];
7551 // ::snprintf (element_name, sizeof (element_name),
7552 // "%s[%u]", parent_name ? parent_name : "", idx);
7553 //
7554 // child_name.assign(element_name);
7555 // assert(field_type_info.first % 8 == 0);
7556 // child_byte_size = field_type_info.first / 8;
7557 // child_byte_offset = idx * child_byte_size;
7558 // return array->getElementType().getAsOpaquePtr();
7559 // }
7560 } break;
7561
7562 // case clang::Type::MemberPointerType:
7563 // {
7564 // MemberPointerType *mem_ptr_type =
7565 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7566 // clang::QualType pointee_type =
7567 // mem_ptr_type->getPointeeType();
7568 //
7569 // if (ClangASTContext::IsAggregateType
7570 // (pointee_type.getAsOpaquePtr()))
7571 // {
7572 // return GetIndexOfChildWithName (ast,
7573 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7574 // name);
7575 // }
7576 // }
7577 // break;
7578 //
7579 case clang::Type::LValueReference:
7580 case clang::Type::RValueReference: {
7581 const clang::ReferenceType *reference_type =
7582 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7583 CompilerType pointee_type(getASTContext(),
7584 reference_type->getPointeeType());
7585
7586 if (pointee_type.IsAggregateType()) {
7587 return pointee_type.GetIndexOfChildWithName(name,
7588 omit_empty_base_classes);
7589 }
7590 } break;
7591
7592 case clang::Type::Pointer: {
7593 const clang::PointerType *pointer_type =
7594 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7595 CompilerType pointee_type(getASTContext(),
7596 pointer_type->getPointeeType());
7597
7598 if (pointee_type.IsAggregateType()) {
7599 return pointee_type.GetIndexOfChildWithName(name,
7600 omit_empty_base_classes);
7601 } else {
7602 // if (parent_name)
7603 // {
7604 // child_name.assign(1, '*');
7605 // child_name += parent_name;
7606 // }
7607 //
7608 // // We have a pointer to an simple type
7609 // if (idx == 0)
7610 // {
7611 // std::pair<uint64_t, unsigned> clang_type_info
7612 // = ast->getTypeInfo(pointee_type);
7613 // assert(clang_type_info.first % 8 == 0);
7614 // child_byte_size = clang_type_info.first / 8;
7615 // child_byte_offset = 0;
7616 // return pointee_type.getAsOpaquePtr();
7617 // }
7618 }
7619 } break;
7620
7621 case clang::Type::Auto:
7622 return CompilerType(
7623 getASTContext(),
7624 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7625 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7626
7627 case clang::Type::Elaborated:
7628 return CompilerType(
7629 getASTContext(),
7630 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7631 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7632
7633 case clang::Type::Paren:
7634 return CompilerType(getASTContext(),
7635 llvm::cast<clang::ParenType>(qual_type)->desugar())
7636 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7637
7638 case clang::Type::Typedef:
7639 return CompilerType(getASTContext(),
7640 llvm::cast<clang::TypedefType>(qual_type)
7641 ->getDecl()
7642 ->getUnderlyingType())
7643 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7644
7645 default:
7646 break;
7647 }
7648 }
7649 return UINT32_MAX;
7650}
Greg Claytond8d4a572015-08-11 21:38:15 +00007651
7652size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007653ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7654 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007655 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007656
Kate Stoneb9c1b512016-09-06 20:57:50 +00007657 clang::QualType qual_type(GetCanonicalQualType(type));
7658 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7659 switch (type_class) {
7660 case clang::Type::Record:
7661 if (GetCompleteType(type)) {
7662 const clang::CXXRecordDecl *cxx_record_decl =
7663 qual_type->getAsCXXRecordDecl();
7664 if (cxx_record_decl) {
7665 const clang::ClassTemplateSpecializationDecl *template_decl =
7666 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7667 cxx_record_decl);
7668 if (template_decl)
7669 return template_decl->getTemplateArgs().size();
7670 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007671 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007672 break;
7673
7674 case clang::Type::Typedef:
7675 return (CompilerType(getASTContext(),
7676 llvm::cast<clang::TypedefType>(qual_type)
7677 ->getDecl()
7678 ->getUnderlyingType()))
7679 .GetNumTemplateArguments();
7680
7681 case clang::Type::Auto:
7682 return (CompilerType(
7683 getASTContext(),
7684 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7685 .GetNumTemplateArguments();
7686
7687 case clang::Type::Elaborated:
7688 return (CompilerType(
7689 getASTContext(),
7690 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7691 .GetNumTemplateArguments();
7692
7693 case clang::Type::Paren:
7694 return (CompilerType(getASTContext(),
7695 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7696 .GetNumTemplateArguments();
7697
7698 default:
7699 break;
7700 }
7701
7702 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007703}
7704
Pavel Labath769b21e2017-11-13 14:26:21 +00007705const clang::ClassTemplateSpecializationDecl *
7706ClangASTContext::GetAsTemplateSpecialization(
7707 lldb::opaque_compiler_type_t type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00007708 if (!type)
Pavel Labath769b21e2017-11-13 14:26:21 +00007709 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007710
7711 clang::QualType qual_type(GetCanonicalQualType(type));
7712 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7713 switch (type_class) {
Pavel Labath769b21e2017-11-13 14:26:21 +00007714 case clang::Type::Record: {
7715 if (! GetCompleteType(type))
7716 return nullptr;
7717 const clang::CXXRecordDecl *cxx_record_decl =
7718 qual_type->getAsCXXRecordDecl();
7719 if (!cxx_record_decl)
7720 return nullptr;
7721 return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7722 cxx_record_decl);
7723 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007724
7725 case clang::Type::Typedef:
Pavel Labath769b21e2017-11-13 14:26:21 +00007726 return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type)
7727 ->getDecl()
7728 ->getUnderlyingType()
7729 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007730
7731 case clang::Type::Auto:
Pavel Labath769b21e2017-11-13 14:26:21 +00007732 return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type)
7733 ->getDeducedType()
7734 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007735
7736 case clang::Type::Elaborated:
Pavel Labath769b21e2017-11-13 14:26:21 +00007737 return GetAsTemplateSpecialization(
7738 llvm::cast<clang::ElaboratedType>(qual_type)
7739 ->getNamedType()
7740 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007741
7742 case clang::Type::Paren:
Pavel Labath769b21e2017-11-13 14:26:21 +00007743 return GetAsTemplateSpecialization(
7744 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007745
7746 default:
Pavel Labath769b21e2017-11-13 14:26:21 +00007747 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007748 }
Pavel Labath769b21e2017-11-13 14:26:21 +00007749}
7750
7751lldb::TemplateArgumentKind
7752ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
7753 size_t arg_idx) {
7754 const clang::ClassTemplateSpecializationDecl *template_decl =
7755 GetAsTemplateSpecialization(type);
7756 if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size())
7757 return eTemplateArgumentKindNull;
7758
7759 switch (template_decl->getTemplateArgs()[arg_idx].getKind()) {
7760 case clang::TemplateArgument::Null:
7761 return eTemplateArgumentKindNull;
7762
7763 case clang::TemplateArgument::NullPtr:
7764 return eTemplateArgumentKindNullPtr;
7765
7766 case clang::TemplateArgument::Type:
7767 return eTemplateArgumentKindType;
7768
7769 case clang::TemplateArgument::Declaration:
7770 return eTemplateArgumentKindDeclaration;
7771
7772 case clang::TemplateArgument::Integral:
7773 return eTemplateArgumentKindIntegral;
7774
7775 case clang::TemplateArgument::Template:
7776 return eTemplateArgumentKindTemplate;
7777
7778 case clang::TemplateArgument::TemplateExpansion:
7779 return eTemplateArgumentKindTemplateExpansion;
7780
7781 case clang::TemplateArgument::Expression:
7782 return eTemplateArgumentKindExpression;
7783
7784 case clang::TemplateArgument::Pack:
7785 return eTemplateArgumentKindPack;
7786 }
7787 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
7788}
7789
7790CompilerType
7791ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
7792 size_t idx) {
7793 const clang::ClassTemplateSpecializationDecl *template_decl =
7794 GetAsTemplateSpecialization(type);
7795 if (!template_decl || idx >= template_decl->getTemplateArgs().size())
7796 return CompilerType();
7797
7798 const clang::TemplateArgument &template_arg =
7799 template_decl->getTemplateArgs()[idx];
7800 if (template_arg.getKind() != clang::TemplateArgument::Type)
7801 return CompilerType();
7802
7803 return CompilerType(getASTContext(), template_arg.getAsType());
7804}
7805
Pavel Labathf59056f2017-11-30 10:16:54 +00007806llvm::Optional<CompilerType::IntegralTemplateArgument>
Pavel Labath769b21e2017-11-13 14:26:21 +00007807ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
7808 size_t idx) {
7809 const clang::ClassTemplateSpecializationDecl *template_decl =
7810 GetAsTemplateSpecialization(type);
7811 if (! template_decl || idx >= template_decl->getTemplateArgs().size())
Pavel Labathf59056f2017-11-30 10:16:54 +00007812 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007813
7814 const clang::TemplateArgument &template_arg =
7815 template_decl->getTemplateArgs()[idx];
7816 if (template_arg.getKind() != clang::TemplateArgument::Integral)
Pavel Labathf59056f2017-11-30 10:16:54 +00007817 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007818
Pavel Labathf59056f2017-11-30 10:16:54 +00007819 return {{template_arg.getAsIntegral(),
7820 CompilerType(getASTContext(), template_arg.getIntegralType())}};
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007821}
7822
Kate Stoneb9c1b512016-09-06 20:57:50 +00007823CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7824 if (type)
7825 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7826 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007827}
7828
Kate Stoneb9c1b512016-09-06 20:57:50 +00007829clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7830 const clang::EnumType *enutype =
7831 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7832 if (enutype)
7833 return enutype->getDecl();
7834 return NULL;
7835}
7836
7837clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7838 const clang::RecordType *record_type =
7839 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7840 if (record_type)
7841 return record_type->getDecl();
7842 return nullptr;
7843}
7844
7845clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
Zachary Turner1639c6b2018-12-17 16:15:13 +00007846 return ClangUtil::GetAsTagDecl(type);
Greg Claytone6b36cd2015-12-08 01:02:08 +00007847}
7848
Aleksandr Urakov709426b2018-09-10 08:08:43 +00007849clang::TypedefNameDecl *
7850ClangASTContext::GetAsTypedefDecl(const CompilerType &type) {
7851 const clang::TypedefType *typedef_type =
7852 llvm::dyn_cast<clang::TypedefType>(ClangUtil::GetQualType(type));
7853 if (typedef_type)
7854 return typedef_type->getDecl();
7855 return nullptr;
7856}
7857
Greg Claytond8d4a572015-08-11 21:38:15 +00007858clang::CXXRecordDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007859ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7860 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
Greg Claytond8d4a572015-08-11 21:38:15 +00007861}
7862
7863clang::ObjCInterfaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007864ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7865 const clang::ObjCObjectType *objc_class_type =
7866 llvm::dyn_cast<clang::ObjCObjectType>(
7867 ClangUtil::GetCanonicalQualType(type));
7868 if (objc_class_type)
7869 return objc_class_type->getInterface();
7870 return nullptr;
7871}
7872
7873clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007874 const CompilerType &type, llvm::StringRef name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00007875 const CompilerType &field_clang_type, AccessType access,
7876 uint32_t bitfield_bit_size) {
7877 if (!type.IsValid() || !field_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00007878 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007879 ClangASTContext *ast =
7880 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7881 if (!ast)
7882 return nullptr;
7883 clang::ASTContext *clang_ast = ast->getASTContext();
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007884 clang::IdentifierInfo *ident = nullptr;
7885 if (!name.empty())
7886 ident = &clang_ast->Idents.get(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00007887
7888 clang::FieldDecl *field = nullptr;
7889
7890 clang::Expr *bit_width = nullptr;
7891 if (bitfield_bit_size != 0) {
7892 llvm::APInt bitfield_bit_size_apint(
7893 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7894 bit_width = new (*clang_ast)
7895 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7896 clang_ast->IntTy, clang::SourceLocation());
7897 }
7898
7899 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7900 if (record_decl) {
7901 field = clang::FieldDecl::Create(
7902 *clang_ast, record_decl, clang::SourceLocation(),
7903 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007904 ident, // Identifier
7905 ClangUtil::GetQualType(field_clang_type), // Field type
7906 nullptr, // TInfo *
7907 bit_width, // BitWidth
7908 false, // Mutable
7909 clang::ICIS_NoInit); // HasInit
Kate Stoneb9c1b512016-09-06 20:57:50 +00007910
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007911 if (name.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +00007912 // Determine whether this field corresponds to an anonymous struct or
7913 // union.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007914 if (const clang::TagType *TagT =
7915 field->getType()->getAs<clang::TagType>()) {
7916 if (clang::RecordDecl *Rec =
7917 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7918 if (!Rec->getDeclName()) {
7919 Rec->setAnonymousStructOrUnion(true);
7920 field->setImplicit();
7921 }
7922 }
7923 }
7924
7925 if (field) {
7926 field->setAccess(
7927 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7928
7929 record_decl->addDecl(field);
7930
7931#ifdef LLDB_CONFIGURATION_DEBUG
7932 VerifyDecl(field);
7933#endif
7934 }
7935 } else {
7936 clang::ObjCInterfaceDecl *class_interface_decl =
7937 ast->GetAsObjCInterfaceDecl(type);
7938
7939 if (class_interface_decl) {
7940 const bool is_synthesized = false;
7941
7942 field_clang_type.GetCompleteType();
7943
7944 field = clang::ObjCIvarDecl::Create(
7945 *clang_ast, class_interface_decl, clang::SourceLocation(),
7946 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007947 ident, // Identifier
7948 ClangUtil::GetQualType(field_clang_type), // Field type
7949 nullptr, // TypeSourceInfo *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007950 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7951 is_synthesized);
7952
7953 if (field) {
7954 class_interface_decl->addDecl(field);
7955
7956#ifdef LLDB_CONFIGURATION_DEBUG
7957 VerifyDecl(field);
7958#endif
7959 }
7960 }
7961 }
7962 return field;
Greg Claytond8d4a572015-08-11 21:38:15 +00007963}
7964
Kate Stoneb9c1b512016-09-06 20:57:50 +00007965void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7966 if (!type)
7967 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007968
Kate Stoneb9c1b512016-09-06 20:57:50 +00007969 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7970 if (!ast)
7971 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007972
Kate Stoneb9c1b512016-09-06 20:57:50 +00007973 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007974
Kate Stoneb9c1b512016-09-06 20:57:50 +00007975 if (!record_decl)
7976 return;
7977
7978 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7979
7980 IndirectFieldVector indirect_fields;
7981 clang::RecordDecl::field_iterator field_pos;
7982 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7983 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7984 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7985 last_field_pos = field_pos++) {
7986 if (field_pos->isAnonymousStructOrUnion()) {
7987 clang::QualType field_qual_type = field_pos->getType();
7988
7989 const clang::RecordType *field_record_type =
7990 field_qual_type->getAs<clang::RecordType>();
7991
7992 if (!field_record_type)
7993 continue;
7994
7995 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7996
7997 if (!field_record_decl)
7998 continue;
7999
8000 for (clang::RecordDecl::decl_iterator
8001 di = field_record_decl->decls_begin(),
8002 de = field_record_decl->decls_end();
8003 di != de; ++di) {
8004 if (clang::FieldDecl *nested_field_decl =
8005 llvm::dyn_cast<clang::FieldDecl>(*di)) {
8006 clang::NamedDecl **chain =
8007 new (*ast->getASTContext()) clang::NamedDecl *[2];
8008 chain[0] = *field_pos;
8009 chain[1] = nested_field_decl;
8010 clang::IndirectFieldDecl *indirect_field =
8011 clang::IndirectFieldDecl::Create(
8012 *ast->getASTContext(), record_decl, clang::SourceLocation(),
8013 nested_field_decl->getIdentifier(),
8014 nested_field_decl->getType(), {chain, 2});
8015
8016 indirect_field->setImplicit();
8017
8018 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
8019 field_pos->getAccess(), nested_field_decl->getAccess()));
8020
8021 indirect_fields.push_back(indirect_field);
8022 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
8023 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
8024 size_t nested_chain_size =
8025 nested_indirect_field_decl->getChainingSize();
8026 clang::NamedDecl **chain = new (*ast->getASTContext())
8027 clang::NamedDecl *[nested_chain_size + 1];
8028 chain[0] = *field_pos;
8029
8030 int chain_index = 1;
8031 for (clang::IndirectFieldDecl::chain_iterator
8032 nci = nested_indirect_field_decl->chain_begin(),
8033 nce = nested_indirect_field_decl->chain_end();
8034 nci < nce; ++nci) {
8035 chain[chain_index] = *nci;
8036 chain_index++;
8037 }
8038
8039 clang::IndirectFieldDecl *indirect_field =
8040 clang::IndirectFieldDecl::Create(
8041 *ast->getASTContext(), record_decl, clang::SourceLocation(),
8042 nested_indirect_field_decl->getIdentifier(),
8043 nested_indirect_field_decl->getType(),
8044 {chain, nested_chain_size + 1});
8045
8046 indirect_field->setImplicit();
8047
8048 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
8049 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
8050
8051 indirect_fields.push_back(indirect_field);
Greg Claytond8d4a572015-08-11 21:38:15 +00008052 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008053 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008054 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008055 }
8056
Adrian Prantl05097242018-04-30 16:49:04 +00008057 // Check the last field to see if it has an incomplete array type as its last
8058 // member and if it does, the tell the record decl about it
Kate Stoneb9c1b512016-09-06 20:57:50 +00008059 if (last_field_pos != field_end_pos) {
8060 if (last_field_pos->getType()->isIncompleteArrayType())
8061 record_decl->hasFlexibleArrayMember();
8062 }
8063
8064 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
8065 ife = indirect_fields.end();
8066 ifi < ife; ++ifi) {
8067 record_decl->addDecl(*ifi);
8068 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008069}
8070
Kate Stoneb9c1b512016-09-06 20:57:50 +00008071void ClangASTContext::SetIsPacked(const CompilerType &type) {
8072 if (type) {
8073 ClangASTContext *ast =
8074 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8075 if (ast) {
8076 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
8077
8078 if (!record_decl)
Greg Claytonf73034f2015-09-08 18:15:05 +00008079 return;
8080
Kate Stoneb9c1b512016-09-06 20:57:50 +00008081 record_decl->addAttr(
8082 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
Greg Claytond8d4a572015-08-11 21:38:15 +00008083 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008084 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008085}
8086
Kate Stoneb9c1b512016-09-06 20:57:50 +00008087clang::VarDecl *ClangASTContext::AddVariableToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008088 const CompilerType &type, llvm::StringRef name,
8089 const CompilerType &var_type, AccessType access) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00008090 if (!type.IsValid() || !var_type.IsValid())
8091 return nullptr;
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008092
Kate Stoneb9c1b512016-09-06 20:57:50 +00008093 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8094 if (!ast)
8095 return nullptr;
8096
8097 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008098 if (!record_decl)
8099 return nullptr;
8100
8101 clang::VarDecl *var_decl = nullptr;
8102 clang::IdentifierInfo *ident = nullptr;
8103 if (!name.empty())
8104 ident = &ast->getASTContext()->Idents.get(name);
8105
8106 var_decl = clang::VarDecl::Create(
8107 *ast->getASTContext(), // ASTContext &
8108 record_decl, // DeclContext *
8109 clang::SourceLocation(), // clang::SourceLocation StartLoc
8110 clang::SourceLocation(), // clang::SourceLocation IdLoc
8111 ident, // clang::IdentifierInfo *
8112 ClangUtil::GetQualType(var_type), // Variable clang::QualType
8113 nullptr, // TypeSourceInfo *
8114 clang::SC_Static); // StorageClass
8115 if (!var_decl)
8116 return nullptr;
8117
8118 var_decl->setAccess(
8119 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
8120 record_decl->addDecl(var_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008121
Greg Claytond8d4a572015-08-11 21:38:15 +00008122#ifdef LLDB_CONFIGURATION_DEBUG
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008123 VerifyDecl(var_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008124#endif
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008125
Kate Stoneb9c1b512016-09-06 20:57:50 +00008126 return var_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008127}
8128
Kate Stoneb9c1b512016-09-06 20:57:50 +00008129clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
Davide Italiano675767a2018-03-27 19:40:50 +00008130 lldb::opaque_compiler_type_t type, const char *name, const char *mangled_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008131 const CompilerType &method_clang_type, lldb::AccessType access,
8132 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
8133 bool is_attr_used, bool is_artificial) {
8134 if (!type || !method_clang_type.IsValid() || name == nullptr ||
8135 name[0] == '\0')
8136 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008137
Kate Stoneb9c1b512016-09-06 20:57:50 +00008138 clang::QualType record_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008139
Kate Stoneb9c1b512016-09-06 20:57:50 +00008140 clang::CXXRecordDecl *cxx_record_decl =
8141 record_qual_type->getAsCXXRecordDecl();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008142
Kate Stoneb9c1b512016-09-06 20:57:50 +00008143 if (cxx_record_decl == nullptr)
8144 return nullptr;
8145
8146 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8147
8148 clang::CXXMethodDecl *cxx_method_decl = nullptr;
8149
8150 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
8151
8152 const clang::FunctionType *function_type =
8153 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
8154
8155 if (function_type == nullptr)
8156 return nullptr;
8157
8158 const clang::FunctionProtoType *method_function_prototype(
8159 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
8160
8161 if (!method_function_prototype)
8162 return nullptr;
8163
8164 unsigned int num_params = method_function_prototype->getNumParams();
8165
8166 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
8167 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
8168
8169 if (is_artificial)
8170 return nullptr; // skip everything artificial
8171
8172 if (name[0] == '~') {
8173 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
8174 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8175 clang::DeclarationNameInfo(
8176 getASTContext()->DeclarationNames.getCXXDestructorName(
8177 getASTContext()->getCanonicalType(record_qual_type)),
8178 clang::SourceLocation()),
8179 method_qual_type, nullptr, is_inline, is_artificial);
8180 cxx_method_decl = cxx_dtor_decl;
8181 } else if (decl_name == cxx_record_decl->getDeclName()) {
8182 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
8183 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8184 clang::DeclarationNameInfo(
8185 getASTContext()->DeclarationNames.getCXXConstructorName(
8186 getASTContext()->getCanonicalType(record_qual_type)),
8187 clang::SourceLocation()),
8188 method_qual_type,
8189 nullptr, // TypeSourceInfo *
8190 is_explicit, is_inline, is_artificial, false /*is_constexpr*/);
8191 cxx_method_decl = cxx_ctor_decl;
8192 } else {
8193 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
8194 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
8195
8196 if (IsOperator(name, op_kind)) {
8197 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
Adrian Prantl05097242018-04-30 16:49:04 +00008198 // Check the number of operator parameters. Sometimes we have seen bad
8199 // DWARF that doesn't correctly describe operators and if we try to
8200 // create a method and add it to the class, clang will assert and
8201 // crash, so we need to make sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00008202 const bool is_method = true;
8203 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
8204 is_method, op_kind, num_params))
8205 return nullptr;
8206 cxx_method_decl = clang::CXXMethodDecl::Create(
8207 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8208 clang::DeclarationNameInfo(
8209 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
8210 clang::SourceLocation()),
8211 method_qual_type,
8212 nullptr, // TypeSourceInfo *
8213 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
8214 } else if (num_params == 0) {
8215 // Conversion operators don't take params...
8216 cxx_method_decl = clang::CXXConversionDecl::Create(
8217 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8218 clang::DeclarationNameInfo(
8219 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
8220 getASTContext()->getCanonicalType(
8221 function_type->getReturnType())),
8222 clang::SourceLocation()),
8223 method_qual_type,
8224 nullptr, // TypeSourceInfo *
8225 is_inline, is_explicit, false /*is_constexpr*/,
8226 clang::SourceLocation());
8227 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008228 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008229
8230 if (cxx_method_decl == nullptr) {
8231 cxx_method_decl = clang::CXXMethodDecl::Create(
8232 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8233 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8234 method_qual_type,
8235 nullptr, // TypeSourceInfo *
8236 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
Greg Claytond8d4a572015-08-11 21:38:15 +00008237 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008238 }
8239
8240 clang::AccessSpecifier access_specifier =
8241 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8242
8243 cxx_method_decl->setAccess(access_specifier);
8244 cxx_method_decl->setVirtualAsWritten(is_virtual);
8245
8246 if (is_attr_used)
8247 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8248
Davide Italiano675767a2018-03-27 19:40:50 +00008249 if (mangled_name != NULL) {
8250 cxx_method_decl->addAttr(
8251 clang::AsmLabelAttr::CreateImplicit(*getASTContext(), mangled_name));
8252 }
8253
Kate Stoneb9c1b512016-09-06 20:57:50 +00008254 // Populate the method decl with parameter decls
8255
8256 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8257
8258 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8259 params.push_back(clang::ParmVarDecl::Create(
8260 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8261 clang::SourceLocation(),
8262 nullptr, // anonymous
8263 method_function_prototype->getParamType(param_index), nullptr,
8264 clang::SC_None, nullptr));
8265 }
8266
8267 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8268
8269 cxx_record_decl->addDecl(cxx_method_decl);
8270
8271 // Sometimes the debug info will mention a constructor (default/copy/move),
8272 // destructor, or assignment operator (copy/move) but there won't be any
8273 // version of this in the code. So we check if the function was artificially
8274 // generated and if it is trivial and this lets the compiler/backend know
8275 // that it can inline the IR for these when it needs to and we can avoid a
8276 // "missing function" error when running expressions.
8277
8278 if (is_artificial) {
8279 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8280 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8281 (cxx_ctor_decl->isCopyConstructor() &&
8282 cxx_record_decl->hasTrivialCopyConstructor()) ||
8283 (cxx_ctor_decl->isMoveConstructor() &&
8284 cxx_record_decl->hasTrivialMoveConstructor()))) {
8285 cxx_ctor_decl->setDefaulted();
8286 cxx_ctor_decl->setTrivial(true);
8287 } else if (cxx_dtor_decl) {
8288 if (cxx_record_decl->hasTrivialDestructor()) {
8289 cxx_dtor_decl->setDefaulted();
8290 cxx_dtor_decl->setTrivial(true);
8291 }
8292 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8293 cxx_record_decl->hasTrivialCopyAssignment()) ||
8294 (cxx_method_decl->isMoveAssignmentOperator() &&
8295 cxx_record_decl->hasTrivialMoveAssignment())) {
8296 cxx_method_decl->setDefaulted();
8297 cxx_method_decl->setTrivial(true);
Greg Claytond8d4a572015-08-11 21:38:15 +00008298 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008299 }
8300
Greg Claytond8d4a572015-08-11 21:38:15 +00008301#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008302 VerifyDecl(cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008303#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008304
Kate Stoneb9c1b512016-09-06 20:57:50 +00008305 // printf ("decl->isPolymorphic() = %i\n",
8306 // cxx_record_decl->isPolymorphic());
8307 // printf ("decl->isAggregate() = %i\n",
8308 // cxx_record_decl->isAggregate());
8309 // printf ("decl->isPOD() = %i\n",
8310 // cxx_record_decl->isPOD());
8311 // printf ("decl->isEmpty() = %i\n",
8312 // cxx_record_decl->isEmpty());
8313 // printf ("decl->isAbstract() = %i\n",
8314 // cxx_record_decl->isAbstract());
8315 // printf ("decl->hasTrivialConstructor() = %i\n",
8316 // cxx_record_decl->hasTrivialConstructor());
8317 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8318 // cxx_record_decl->hasTrivialCopyConstructor());
8319 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8320 // cxx_record_decl->hasTrivialCopyAssignment());
8321 // printf ("decl->hasTrivialDestructor() = %i\n",
8322 // cxx_record_decl->hasTrivialDestructor());
8323 return cxx_method_decl;
8324}
Greg Claytond8d4a572015-08-11 21:38:15 +00008325
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00008326void ClangASTContext::AddMethodOverridesForCXXRecordType(
8327 lldb::opaque_compiler_type_t type) {
8328 if (auto *record = GetAsCXXRecordDecl(type))
8329 for (auto *method : record->methods())
8330 addOverridesForMethod(method);
8331}
8332
Greg Claytond8d4a572015-08-11 21:38:15 +00008333#pragma mark C++ Base Classes
8334
Zachary Turner970f38e2018-10-25 20:44:56 +00008335std::unique_ptr<clang::CXXBaseSpecifier>
Kate Stoneb9c1b512016-09-06 20:57:50 +00008336ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8337 AccessType access, bool is_virtual,
8338 bool base_of_class) {
Zachary Turner970f38e2018-10-25 20:44:56 +00008339 if (!type)
8340 return nullptr;
8341
8342 return llvm::make_unique<clang::CXXBaseSpecifier>(
8343 clang::SourceRange(), is_virtual, base_of_class,
8344 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8345 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8346 clang::SourceLocation());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008347}
8348
Zachary Turner970f38e2018-10-25 20:44:56 +00008349bool ClangASTContext::TransferBaseClasses(
Kate Stoneb9c1b512016-09-06 20:57:50 +00008350 lldb::opaque_compiler_type_t type,
Zachary Turner970f38e2018-10-25 20:44:56 +00008351 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases) {
8352 if (!type)
8353 return false;
8354 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8355 if (!cxx_record_decl)
8356 return false;
8357 std::vector<clang::CXXBaseSpecifier *> raw_bases;
8358 raw_bases.reserve(bases.size());
8359
8360 // Clang will make a copy of them, so it's ok that we pass pointers that we're
8361 // about to destroy.
8362 for (auto &b : bases)
8363 raw_bases.push_back(b.get());
8364 cxx_record_decl->setBases(raw_bases.data(), raw_bases.size());
8365 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008366}
8367
8368bool ClangASTContext::SetObjCSuperClass(
8369 const CompilerType &type, const CompilerType &superclass_clang_type) {
8370 ClangASTContext *ast =
8371 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8372 if (!ast)
8373 return false;
8374 clang::ASTContext *clang_ast = ast->getASTContext();
8375
8376 if (type && superclass_clang_type.IsValid() &&
8377 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8378 clang::ObjCInterfaceDecl *class_interface_decl =
8379 GetAsObjCInterfaceDecl(type);
8380 clang::ObjCInterfaceDecl *super_interface_decl =
8381 GetAsObjCInterfaceDecl(superclass_clang_type);
8382 if (class_interface_decl && super_interface_decl) {
8383 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8384 clang_ast->getObjCInterfaceType(super_interface_decl)));
8385 return true;
8386 }
8387 }
8388 return false;
8389}
8390
8391bool ClangASTContext::AddObjCClassProperty(
8392 const CompilerType &type, const char *property_name,
8393 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8394 const char *property_setter_name, const char *property_getter_name,
8395 uint32_t property_attributes, ClangASTMetadata *metadata) {
8396 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8397 property_name[0] == '\0')
8398 return false;
8399 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8400 if (!ast)
8401 return false;
8402 clang::ASTContext *clang_ast = ast->getASTContext();
8403
8404 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8405
8406 if (class_interface_decl) {
8407 CompilerType property_clang_type_to_access;
8408
8409 if (property_clang_type.IsValid())
8410 property_clang_type_to_access = property_clang_type;
8411 else if (ivar_decl)
8412 property_clang_type_to_access =
8413 CompilerType(clang_ast, ivar_decl->getType());
8414
8415 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8416 clang::TypeSourceInfo *prop_type_source;
8417 if (ivar_decl)
8418 prop_type_source =
8419 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8420 else
8421 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8422 ClangUtil::GetQualType(property_clang_type));
8423
8424 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8425 *clang_ast, class_interface_decl,
8426 clang::SourceLocation(), // Source Location
8427 &clang_ast->Idents.get(property_name),
8428 clang::SourceLocation(), // Source Location for AT
8429 clang::SourceLocation(), // Source location for (
8430 ivar_decl ? ivar_decl->getType()
8431 : ClangUtil::GetQualType(property_clang_type),
8432 prop_type_source);
8433
8434 if (property_decl) {
8435 if (metadata)
8436 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8437
8438 class_interface_decl->addDecl(property_decl);
8439
8440 clang::Selector setter_sel, getter_sel;
8441
8442 if (property_setter_name != nullptr) {
8443 std::string property_setter_no_colon(
8444 property_setter_name, strlen(property_setter_name) - 1);
8445 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008446 &clang_ast->Idents.get(property_setter_no_colon);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008447 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8448 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8449 std::string setter_sel_string("set");
8450 setter_sel_string.push_back(::toupper(property_name[0]));
8451 setter_sel_string.append(&property_name[1]);
8452 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008453 &clang_ast->Idents.get(setter_sel_string);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008454 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8455 }
8456 property_decl->setSetterName(setter_sel);
8457 property_decl->setPropertyAttributes(
8458 clang::ObjCPropertyDecl::OBJC_PR_setter);
8459
8460 if (property_getter_name != nullptr) {
8461 clang::IdentifierInfo *getter_ident =
8462 &clang_ast->Idents.get(property_getter_name);
8463 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8464 } else {
8465 clang::IdentifierInfo *getter_ident =
8466 &clang_ast->Idents.get(property_name);
8467 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8468 }
8469 property_decl->setGetterName(getter_sel);
8470 property_decl->setPropertyAttributes(
8471 clang::ObjCPropertyDecl::OBJC_PR_getter);
8472
8473 if (ivar_decl)
8474 property_decl->setPropertyIvarDecl(ivar_decl);
8475
8476 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8477 property_decl->setPropertyAttributes(
8478 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8479 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8480 property_decl->setPropertyAttributes(
8481 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8482 if (property_attributes & DW_APPLE_PROPERTY_assign)
8483 property_decl->setPropertyAttributes(
8484 clang::ObjCPropertyDecl::OBJC_PR_assign);
8485 if (property_attributes & DW_APPLE_PROPERTY_retain)
8486 property_decl->setPropertyAttributes(
8487 clang::ObjCPropertyDecl::OBJC_PR_retain);
8488 if (property_attributes & DW_APPLE_PROPERTY_copy)
8489 property_decl->setPropertyAttributes(
8490 clang::ObjCPropertyDecl::OBJC_PR_copy);
8491 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8492 property_decl->setPropertyAttributes(
8493 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8494 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8495 property_decl->setPropertyAttributes(
8496 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8497 if (property_attributes &
8498 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8499 property_decl->setPropertyAttributes(
8500 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8501 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8502 property_decl->setPropertyAttributes(
8503 clang::ObjCPropertyDecl::OBJC_PR_class);
8504
8505 const bool isInstance =
8506 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8507
8508 if (!getter_sel.isNull() &&
8509 !(isInstance
8510 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8511 : class_interface_decl->lookupClassMethod(getter_sel))) {
8512 const bool isVariadic = false;
8513 const bool isSynthesized = false;
8514 const bool isImplicitlyDeclared = true;
8515 const bool isDefined = false;
8516 const clang::ObjCMethodDecl::ImplementationControl impControl =
8517 clang::ObjCMethodDecl::None;
8518 const bool HasRelatedResultType = false;
8519
8520 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8521 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8522 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8523 nullptr, class_interface_decl, isInstance, isVariadic,
8524 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8525 HasRelatedResultType);
8526
8527 if (getter && metadata)
8528 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8529
8530 if (getter) {
8531 getter->setMethodParams(*clang_ast,
8532 llvm::ArrayRef<clang::ParmVarDecl *>(),
8533 llvm::ArrayRef<clang::SourceLocation>());
8534
8535 class_interface_decl->addDecl(getter);
8536 }
8537 }
8538
8539 if (!setter_sel.isNull() &&
8540 !(isInstance
8541 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8542 : class_interface_decl->lookupClassMethod(setter_sel))) {
8543 clang::QualType result_type = clang_ast->VoidTy;
8544 const bool isVariadic = false;
8545 const bool isSynthesized = false;
8546 const bool isImplicitlyDeclared = true;
8547 const bool isDefined = false;
8548 const clang::ObjCMethodDecl::ImplementationControl impControl =
8549 clang::ObjCMethodDecl::None;
8550 const bool HasRelatedResultType = false;
8551
8552 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8553 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8554 setter_sel, result_type, nullptr, class_interface_decl,
8555 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8556 isDefined, impControl, HasRelatedResultType);
8557
8558 if (setter && metadata)
8559 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8560
8561 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8562
8563 params.push_back(clang::ParmVarDecl::Create(
8564 *clang_ast, setter, clang::SourceLocation(),
8565 clang::SourceLocation(),
8566 nullptr, // anonymous
8567 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8568 clang::SC_Auto, nullptr));
8569
8570 if (setter) {
8571 setter->setMethodParams(
8572 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8573 llvm::ArrayRef<clang::SourceLocation>());
8574
8575 class_interface_decl->addDecl(setter);
8576 }
8577 }
8578
8579 return true;
8580 }
8581 }
8582 }
8583 return false;
8584}
8585
8586bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8587 bool check_superclass) {
8588 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8589 if (class_interface_decl)
8590 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8591 return false;
8592}
8593
8594clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8595 const CompilerType &type,
8596 const char *name, // the full symbol name as seen in the symbol table
8597 // (lldb::opaque_compiler_type_t type, "-[NString
8598 // stringWithCString:]")
8599 const CompilerType &method_clang_type, lldb::AccessType access,
8600 bool is_artificial, bool is_variadic) {
8601 if (!type || !method_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008602 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008603
Kate Stoneb9c1b512016-09-06 20:57:50 +00008604 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8605
8606 if (class_interface_decl == nullptr)
8607 return nullptr;
8608 ClangASTContext *lldb_ast =
8609 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8610 if (lldb_ast == nullptr)
8611 return nullptr;
8612 clang::ASTContext *ast = lldb_ast->getASTContext();
8613
8614 const char *selector_start = ::strchr(name, ' ');
8615 if (selector_start == nullptr)
8616 return nullptr;
8617
8618 selector_start++;
8619 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8620
8621 size_t len = 0;
8622 const char *start;
8623 // printf ("name = '%s'\n", name);
8624
8625 unsigned num_selectors_with_args = 0;
8626 for (start = selector_start; start && *start != '\0' && *start != ']';
8627 start += len) {
8628 len = ::strcspn(start, ":]");
8629 bool has_arg = (start[len] == ':');
8630 if (has_arg)
8631 ++num_selectors_with_args;
8632 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8633 if (has_arg)
8634 len += 1;
8635 }
8636
8637 if (selector_idents.size() == 0)
8638 return nullptr;
8639
8640 clang::Selector method_selector = ast->Selectors.getSelector(
8641 num_selectors_with_args ? selector_idents.size() : 0,
8642 selector_idents.data());
8643
8644 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8645
8646 // Populate the method decl with parameter decls
8647 const clang::Type *method_type(method_qual_type.getTypePtr());
8648
8649 if (method_type == nullptr)
8650 return nullptr;
8651
8652 const clang::FunctionProtoType *method_function_prototype(
8653 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8654
8655 if (!method_function_prototype)
8656 return nullptr;
8657
8658 bool is_synthesized = false;
8659 bool is_defined = false;
8660 clang::ObjCMethodDecl::ImplementationControl imp_control =
8661 clang::ObjCMethodDecl::None;
8662
8663 const unsigned num_args = method_function_prototype->getNumParams();
8664
8665 if (num_args != num_selectors_with_args)
8666 return nullptr; // some debug information is corrupt. We are not going to
8667 // deal with it.
8668
8669 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8670 *ast,
8671 clang::SourceLocation(), // beginLoc,
8672 clang::SourceLocation(), // endLoc,
8673 method_selector, method_function_prototype->getReturnType(),
8674 nullptr, // TypeSourceInfo *ResultTInfo,
8675 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8676 ClangUtil::GetQualType(type)),
8677 name[0] == '-', is_variadic, is_synthesized,
8678 true, // is_implicitly_declared; we force this to true because we don't
8679 // have source locations
8680 is_defined, imp_control, false /*has_related_result_type*/);
8681
8682 if (objc_method_decl == nullptr)
8683 return nullptr;
8684
8685 if (num_args > 0) {
8686 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8687
8688 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8689 params.push_back(clang::ParmVarDecl::Create(
8690 *ast, objc_method_decl, clang::SourceLocation(),
8691 clang::SourceLocation(),
8692 nullptr, // anonymous
8693 method_function_prototype->getParamType(param_index), nullptr,
8694 clang::SC_Auto, nullptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00008695 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008696
Kate Stoneb9c1b512016-09-06 20:57:50 +00008697 objc_method_decl->setMethodParams(
8698 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8699 llvm::ArrayRef<clang::SourceLocation>());
8700 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008701
Kate Stoneb9c1b512016-09-06 20:57:50 +00008702 class_interface_decl->addDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008703
Greg Claytond8d4a572015-08-11 21:38:15 +00008704#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008705 VerifyDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008706#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00008707
8708 return objc_method_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008709}
8710
Kate Stoneb9c1b512016-09-06 20:57:50 +00008711bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8712 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00008713 return false;
Greg Claytone6b36cd2015-12-08 01:02:08 +00008714
Kate Stoneb9c1b512016-09-06 20:57:50 +00008715 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008716
Kate Stoneb9c1b512016-09-06 20:57:50 +00008717 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8718 switch (type_class) {
8719 case clang::Type::Record: {
8720 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8721 if (cxx_record_decl)
8722 return cxx_record_decl->hasExternalLexicalStorage() ||
8723 cxx_record_decl->hasExternalVisibleStorage();
8724 } break;
Enrico Granata36f51e42015-12-18 22:41:25 +00008725
Kate Stoneb9c1b512016-09-06 20:57:50 +00008726 case clang::Type::Enum: {
8727 clang::EnumDecl *enum_decl =
8728 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8729 if (enum_decl)
8730 return enum_decl->hasExternalLexicalStorage() ||
8731 enum_decl->hasExternalVisibleStorage();
8732 } break;
8733
8734 case clang::Type::ObjCObject:
8735 case clang::Type::ObjCInterface: {
8736 const clang::ObjCObjectType *objc_class_type =
8737 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8738 assert(objc_class_type);
8739 if (objc_class_type) {
8740 clang::ObjCInterfaceDecl *class_interface_decl =
8741 objc_class_type->getInterface();
8742
8743 if (class_interface_decl)
8744 return class_interface_decl->hasExternalLexicalStorage() ||
8745 class_interface_decl->hasExternalVisibleStorage();
Greg Claytond8d4a572015-08-11 21:38:15 +00008746 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008747 } break;
8748
8749 case clang::Type::Typedef:
8750 return GetHasExternalStorage(CompilerType(
8751 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8752 ->getDecl()
8753 ->getUnderlyingType()
8754 .getAsOpaquePtr()));
8755
8756 case clang::Type::Auto:
8757 return GetHasExternalStorage(CompilerType(
8758 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8759 ->getDeducedType()
8760 .getAsOpaquePtr()));
8761
8762 case clang::Type::Elaborated:
8763 return GetHasExternalStorage(CompilerType(
8764 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8765 ->getNamedType()
8766 .getAsOpaquePtr()));
8767
8768 case clang::Type::Paren:
8769 return GetHasExternalStorage(CompilerType(
8770 type.GetTypeSystem(),
8771 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8772
8773 default:
8774 break;
8775 }
8776 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008777}
8778
Kate Stoneb9c1b512016-09-06 20:57:50 +00008779bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8780 bool has_extern) {
8781 if (!type)
8782 return false;
8783
8784 clang::QualType qual_type(GetCanonicalQualType(type));
8785
8786 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8787 switch (type_class) {
8788 case clang::Type::Record: {
8789 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8790 if (cxx_record_decl) {
8791 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8792 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8793 return true;
8794 }
8795 } break;
8796
8797 case clang::Type::Enum: {
8798 clang::EnumDecl *enum_decl =
8799 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8800 if (enum_decl) {
8801 enum_decl->setHasExternalLexicalStorage(has_extern);
8802 enum_decl->setHasExternalVisibleStorage(has_extern);
8803 return true;
8804 }
8805 } break;
8806
8807 case clang::Type::ObjCObject:
8808 case clang::Type::ObjCInterface: {
8809 const clang::ObjCObjectType *objc_class_type =
8810 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8811 assert(objc_class_type);
8812 if (objc_class_type) {
8813 clang::ObjCInterfaceDecl *class_interface_decl =
8814 objc_class_type->getInterface();
8815
8816 if (class_interface_decl) {
8817 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8818 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8819 return true;
8820 }
8821 }
8822 } break;
8823
8824 case clang::Type::Typedef:
8825 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8826 ->getDecl()
8827 ->getUnderlyingType()
8828 .getAsOpaquePtr(),
8829 has_extern);
8830
8831 case clang::Type::Auto:
8832 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8833 ->getDeducedType()
8834 .getAsOpaquePtr(),
8835 has_extern);
8836
8837 case clang::Type::Elaborated:
8838 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8839 ->getNamedType()
8840 .getAsOpaquePtr(),
8841 has_extern);
8842
8843 case clang::Type::Paren:
8844 return SetHasExternalStorage(
8845 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8846 has_extern);
8847
8848 default:
8849 break;
8850 }
8851 return false;
8852}
Greg Claytond8d4a572015-08-11 21:38:15 +00008853
8854#pragma mark TagDecl
8855
Kate Stoneb9c1b512016-09-06 20:57:50 +00008856bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8857 clang::QualType qual_type(ClangUtil::GetQualType(type));
8858 if (!qual_type.isNull()) {
8859 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8860 if (tag_type) {
8861 clang::TagDecl *tag_decl = tag_type->getDecl();
8862 if (tag_decl) {
8863 tag_decl->startDefinition();
8864 return true;
8865 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008866 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008867
8868 const clang::ObjCObjectType *object_type =
8869 qual_type->getAs<clang::ObjCObjectType>();
8870 if (object_type) {
8871 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8872 if (interface_decl) {
8873 interface_decl->startDefinition();
8874 return true;
8875 }
8876 }
8877 }
8878 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008879}
8880
Kate Stoneb9c1b512016-09-06 20:57:50 +00008881bool ClangASTContext::CompleteTagDeclarationDefinition(
8882 const CompilerType &type) {
8883 clang::QualType qual_type(ClangUtil::GetQualType(type));
8884 if (!qual_type.isNull()) {
8885 // Make sure we use the same methodology as
Adrian Prantl05097242018-04-30 16:49:04 +00008886 // ClangASTContext::StartTagDeclarationDefinition() as to how we start/end
8887 // the definition. Previously we were calling
Kate Stoneb9c1b512016-09-06 20:57:50 +00008888 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8889 if (tag_type) {
8890 clang::TagDecl *tag_decl = tag_type->getDecl();
8891 if (tag_decl) {
8892 clang::CXXRecordDecl *cxx_record_decl =
8893 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8894
8895 if (cxx_record_decl) {
8896 if (!cxx_record_decl->isCompleteDefinition())
8897 cxx_record_decl->completeDefinition();
8898 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8899 cxx_record_decl->setHasExternalLexicalStorage(false);
8900 cxx_record_decl->setHasExternalVisibleStorage(false);
8901 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008902 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008903 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008904 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008905
8906 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8907
8908 if (enutype) {
8909 clang::EnumDecl *enum_decl = enutype->getDecl();
8910
8911 if (enum_decl) {
8912 if (!enum_decl->isCompleteDefinition()) {
8913 ClangASTContext *lldb_ast =
8914 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8915 if (lldb_ast == nullptr)
8916 return false;
8917 clang::ASTContext *ast = lldb_ast->getASTContext();
8918
8919 /// TODO This really needs to be fixed.
8920
8921 QualType integer_type(enum_decl->getIntegerType());
8922 if (!integer_type.isNull()) {
8923 unsigned NumPositiveBits = 1;
8924 unsigned NumNegativeBits = 0;
8925
8926 clang::QualType promotion_qual_type;
8927 // If the enum integer type is less than an integer in bit width,
8928 // then we must promote it to an integer size.
8929 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8930 ast->getTypeSize(ast->IntTy)) {
8931 if (enum_decl->getIntegerType()->isSignedIntegerType())
8932 promotion_qual_type = ast->IntTy;
8933 else
8934 promotion_qual_type = ast->UnsignedIntTy;
8935 } else
8936 promotion_qual_type = enum_decl->getIntegerType();
8937
8938 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8939 promotion_qual_type, NumPositiveBits,
8940 NumNegativeBits);
8941 }
8942 }
8943 return true;
8944 }
8945 }
8946 }
8947 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008948}
8949
Aleksandr Urakov709426b2018-09-10 08:08:43 +00008950clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008951 const CompilerType &enum_type, const Declaration &decl, const char *name,
Zachary Turner1639c6b2018-12-17 16:15:13 +00008952 const llvm::APSInt &value) {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008953
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008954 if (!enum_type || ConstString(name).IsEmpty())
8955 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008956
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008957 lldbassert(enum_type.GetTypeSystem() == static_cast<TypeSystem *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +00008958
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008959 lldb::opaque_compiler_type_t enum_opaque_compiler_type =
8960 enum_type.GetOpaqueQualType();
8961
8962 if (!enum_opaque_compiler_type)
8963 return nullptr;
8964
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008965 clang::QualType enum_qual_type(
8966 GetCanonicalQualType(enum_opaque_compiler_type));
8967
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008968 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8969
8970 if (!clang_type)
8971 return nullptr;
8972
8973 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8974
8975 if (!enutype)
8976 return nullptr;
8977
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008978 clang::EnumConstantDecl *enumerator_decl = clang::EnumConstantDecl::Create(
8979 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8980 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier
Zachary Turner1639c6b2018-12-17 16:15:13 +00008981 clang::QualType(enutype, 0), nullptr, value);
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008982
8983 if (!enumerator_decl)
8984 return nullptr;
8985
8986 enutype->getDecl()->addDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008987
8988#ifdef LLDB_CONFIGURATION_DEBUG
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008989 VerifyDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008990#endif
8991
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008992 return enumerator_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008993}
8994
Zachary Turner1639c6b2018-12-17 16:15:13 +00008995clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
8996 const CompilerType &enum_type, const Declaration &decl, const char *name,
8997 int64_t enum_value, uint32_t enum_value_bit_size) {
8998 CompilerType underlying_type =
8999 GetEnumerationIntegerType(enum_type.GetOpaqueQualType());
9000 bool is_signed = false;
9001 underlying_type.IsIntegerType(is_signed);
9002
9003 llvm::APSInt value(enum_value_bit_size, is_signed);
9004 value = enum_value;
9005
9006 return AddEnumerationValueToEnumerationType(enum_type, decl, name, value);
9007}
9008
Greg Claytona1e5dc82015-08-11 22:53:00 +00009009CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00009010ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
9011 clang::QualType enum_qual_type(GetCanonicalQualType(type));
9012 const clang::Type *clang_type = enum_qual_type.getTypePtr();
9013 if (clang_type) {
9014 const clang::EnumType *enutype =
9015 llvm::dyn_cast<clang::EnumType>(clang_type);
9016 if (enutype) {
9017 clang::EnumDecl *enum_decl = enutype->getDecl();
9018 if (enum_decl)
9019 return CompilerType(getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00009020 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009021 }
9022 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00009023}
9024
Kate Stoneb9c1b512016-09-06 20:57:50 +00009025CompilerType
9026ClangASTContext::CreateMemberPointerType(const CompilerType &type,
9027 const CompilerType &pointee_type) {
9028 if (type && pointee_type.IsValid() &&
9029 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
9030 ClangASTContext *ast =
9031 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
9032 if (!ast)
9033 return CompilerType();
9034 return CompilerType(ast->getASTContext(),
9035 ast->getASTContext()->getMemberPointerType(
9036 ClangUtil::GetQualType(pointee_type),
9037 ClangUtil::GetQualType(type).getTypePtr()));
9038 }
9039 return CompilerType();
9040}
Greg Claytond8d4a572015-08-11 21:38:15 +00009041
9042size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00009043ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
9044 const char *s, uint8_t *dst,
9045 size_t dst_size) {
9046 if (type) {
9047 clang::QualType qual_type(GetCanonicalQualType(type));
9048 uint32_t count = 0;
9049 bool is_complex = false;
9050 if (IsFloatingPointType(type, count, is_complex)) {
9051 // TODO: handle complex and vector types
9052 if (count != 1)
9053 return false;
9054
9055 llvm::StringRef s_sref(s);
9056 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type),
9057 s_sref);
9058
9059 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type);
9060 const uint64_t byte_size = bit_size / 8;
9061 if (dst_size >= byte_size) {
9062 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(
9063 llvm::NextPowerOf2(byte_size) * 8);
Zachary Turner97206d52017-05-12 04:51:55 +00009064 lldb_private::Status get_data_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009065 if (scalar.GetAsMemoryData(dst, byte_size,
9066 lldb_private::endian::InlHostByteOrder(),
9067 get_data_error))
9068 return byte_size;
9069 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009070 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009071 }
9072 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00009073}
9074
Greg Claytond8d4a572015-08-11 21:38:15 +00009075//----------------------------------------------------------------------
9076// Dumping types
9077//----------------------------------------------------------------------
9078#define DEPTH_INCREMENT 2
9079
Zachary Turner49110232018-11-05 17:40:28 +00009080void ClangASTContext::Dump(Stream &s) {
Zachary Turner115209e2018-11-05 19:25:39 +00009081 Decl *tu = Decl::castFromDeclContext(GetTranslationUnitDecl());
Zachary Turner49110232018-11-05 17:40:28 +00009082 tu->dump(s.AsRawOstream());
9083}
9084
Kate Stoneb9c1b512016-09-06 20:57:50 +00009085void ClangASTContext::DumpValue(
9086 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
Zachary Turner29cb8682017-03-03 20:57:05 +00009087 lldb::Format format, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009088 lldb::offset_t data_byte_offset, size_t data_byte_size,
9089 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
9090 bool show_summary, bool verbose, uint32_t depth) {
9091 if (!type)
9092 return;
9093
9094 clang::QualType qual_type(GetQualType(type));
9095 switch (qual_type->getTypeClass()) {
9096 case clang::Type::Record:
9097 if (GetCompleteType(type)) {
9098 const clang::RecordType *record_type =
9099 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9100 const clang::RecordDecl *record_decl = record_type->getDecl();
9101 assert(record_decl);
9102 uint32_t field_bit_offset = 0;
9103 uint32_t field_byte_offset = 0;
9104 const clang::ASTRecordLayout &record_layout =
9105 getASTContext()->getASTRecordLayout(record_decl);
9106 uint32_t child_idx = 0;
9107
9108 const clang::CXXRecordDecl *cxx_record_decl =
9109 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9110 if (cxx_record_decl) {
9111 // We might have base classes to print out first
9112 clang::CXXRecordDecl::base_class_const_iterator base_class,
9113 base_class_end;
9114 for (base_class = cxx_record_decl->bases_begin(),
9115 base_class_end = cxx_record_decl->bases_end();
9116 base_class != base_class_end; ++base_class) {
9117 const clang::CXXRecordDecl *base_class_decl =
9118 llvm::cast<clang::CXXRecordDecl>(
9119 base_class->getType()->getAs<clang::RecordType>()->getDecl());
9120
9121 // Skip empty base classes
Jonas Devliegherea6682a42018-12-15 00:15:33 +00009122 if (!verbose && !ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00009123 continue;
9124
9125 if (base_class->isVirtual())
9126 field_bit_offset =
9127 record_layout.getVBaseClassOffset(base_class_decl)
9128 .getQuantity() *
9129 8;
9130 else
9131 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
9132 .getQuantity() *
9133 8;
9134 field_byte_offset = field_bit_offset / 8;
9135 assert(field_bit_offset % 8 == 0);
9136 if (child_idx == 0)
9137 s->PutChar('{');
9138 else
9139 s->PutChar(',');
9140
9141 clang::QualType base_class_qual_type = base_class->getType();
9142 std::string base_class_type_name(base_class_qual_type.getAsString());
9143
9144 // Indent and print the base class type name
Zachary Turner827d5d72016-12-16 04:27:00 +00009145 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
9146 base_class_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009147
9148 clang::TypeInfo base_class_type_info =
9149 getASTContext()->getTypeInfo(base_class_qual_type);
9150
9151 // Dump the value of the member
9152 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
9153 base_clang_type.DumpValue(
9154 exe_ctx,
9155 s, // Stream to dump to
9156 base_clang_type
9157 .GetFormat(), // The format with which to display the member
9158 data, // Data buffer containing all bytes for this type
9159 data_byte_offset + field_byte_offset, // Offset into "data" where
9160 // to grab value from
9161 base_class_type_info.Width / 8, // Size of this type in bytes
9162 0, // Bitfield bit size
9163 0, // Bitfield bit offset
9164 show_types, // Boolean indicating if we should show the variable
9165 // types
9166 show_summary, // Boolean indicating if we should show a summary
9167 // for the current type
9168 verbose, // Verbose output?
9169 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9170 // children
9171
9172 ++child_idx;
9173 }
9174 }
9175 uint32_t field_idx = 0;
9176 clang::RecordDecl::field_iterator field, field_end;
9177 for (field = record_decl->field_begin(),
9178 field_end = record_decl->field_end();
9179 field != field_end; ++field, ++field_idx, ++child_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009180 // Print the starting squiggly bracket (if this is the first member) or
9181 // comma (for member 2 and beyond) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009182 if (child_idx == 0)
9183 s->PutChar('{');
9184 else
9185 s->PutChar(',');
9186
9187 // Indent
9188 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
9189
9190 clang::QualType field_type = field->getType();
9191 // Print the member type if requested
Adrian Prantl05097242018-04-30 16:49:04 +00009192 // Figure out the type byte size (field_type_info.first) and alignment
9193 // (field_type_info.second) from the AST context.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009194 clang::TypeInfo field_type_info =
9195 getASTContext()->getTypeInfo(field_type);
9196 assert(field_idx < record_layout.getFieldCount());
9197 // Figure out the field offset within the current struct/union/class
9198 // type
9199 field_bit_offset = record_layout.getFieldOffset(field_idx);
9200 field_byte_offset = field_bit_offset / 8;
9201 uint32_t field_bitfield_bit_size = 0;
9202 uint32_t field_bitfield_bit_offset = 0;
9203 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
9204 field_bitfield_bit_size))
9205 field_bitfield_bit_offset = field_bit_offset % 8;
9206
9207 if (show_types) {
9208 std::string field_type_name(field_type.getAsString());
9209 if (field_bitfield_bit_size > 0)
9210 s->Printf("(%s:%u) ", field_type_name.c_str(),
9211 field_bitfield_bit_size);
9212 else
9213 s->Printf("(%s) ", field_type_name.c_str());
9214 }
9215 // Print the member name and equal sign
9216 s->Printf("%s = ", field->getNameAsString().c_str());
9217
9218 // Dump the value of the member
9219 CompilerType field_clang_type(getASTContext(), field_type);
9220 field_clang_type.DumpValue(
9221 exe_ctx,
9222 s, // Stream to dump to
9223 field_clang_type
9224 .GetFormat(), // The format with which to display the member
9225 data, // Data buffer containing all bytes for this type
9226 data_byte_offset + field_byte_offset, // Offset into "data" where to
9227 // grab value from
9228 field_type_info.Width / 8, // Size of this type in bytes
9229 field_bitfield_bit_size, // Bitfield bit size
9230 field_bitfield_bit_offset, // Bitfield bit offset
9231 show_types, // Boolean indicating if we should show the variable
9232 // types
9233 show_summary, // Boolean indicating if we should show a summary for
9234 // the current type
9235 verbose, // Verbose output?
9236 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9237 // children
9238 }
9239
9240 // Indent the trailing squiggly bracket
9241 if (child_idx > 0)
9242 s->Printf("\n%*s}", depth, "");
9243 }
9244 return;
9245
9246 case clang::Type::Enum:
9247 if (GetCompleteType(type)) {
9248 const clang::EnumType *enutype =
9249 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9250 const clang::EnumDecl *enum_decl = enutype->getDecl();
9251 assert(enum_decl);
9252 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9253 lldb::offset_t offset = data_byte_offset;
9254 const int64_t enum_value = data.GetMaxU64Bitfield(
9255 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
9256 for (enum_pos = enum_decl->enumerator_begin(),
9257 enum_end_pos = enum_decl->enumerator_end();
9258 enum_pos != enum_end_pos; ++enum_pos) {
9259 if (enum_pos->getInitVal() == enum_value) {
9260 s->Printf("%s", enum_pos->getNameAsString().c_str());
9261 return;
9262 }
9263 }
Adrian Prantl05097242018-04-30 16:49:04 +00009264 // If we have gotten here we didn't get find the enumerator in the enum
9265 // decl, so just print the integer.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009266 s->Printf("%" PRIi64, enum_value);
9267 }
9268 return;
9269
9270 case clang::Type::ConstantArray: {
9271 const clang::ConstantArrayType *array =
9272 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9273 bool is_array_of_characters = false;
9274 clang::QualType element_qual_type = array->getElementType();
9275
9276 const clang::Type *canonical_type =
9277 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9278 if (canonical_type)
9279 is_array_of_characters = canonical_type->isCharType();
9280
9281 const uint64_t element_count = array->getSize().getLimitedValue();
9282
9283 clang::TypeInfo field_type_info =
9284 getASTContext()->getTypeInfo(element_qual_type);
9285
9286 uint32_t element_idx = 0;
9287 uint32_t element_offset = 0;
9288 uint64_t element_byte_size = field_type_info.Width / 8;
9289 uint32_t element_stride = element_byte_size;
9290
9291 if (is_array_of_characters) {
9292 s->PutChar('"');
Zachary Turner29cb8682017-03-03 20:57:05 +00009293 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9294 element_byte_size, element_count, UINT32_MAX,
9295 LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009296 s->PutChar('"');
9297 return;
9298 } else {
9299 CompilerType element_clang_type(getASTContext(), element_qual_type);
9300 lldb::Format element_format = element_clang_type.GetFormat();
9301
9302 for (element_idx = 0; element_idx < element_count; ++element_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009303 // Print the starting squiggly bracket (if this is the first member) or
9304 // comman (for member 2 and beyong) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009305 if (element_idx == 0)
9306 s->PutChar('{');
9307 else
9308 s->PutChar(',');
9309
9310 // Indent and print the index
9311 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9312
9313 // Figure out the field offset within the current struct/union/class
9314 // type
9315 element_offset = element_idx * element_stride;
9316
9317 // Dump the value of the member
9318 element_clang_type.DumpValue(
9319 exe_ctx,
9320 s, // Stream to dump to
9321 element_format, // The format with which to display the element
9322 data, // Data buffer containing all bytes for this type
9323 data_byte_offset +
9324 element_offset, // Offset into "data" where to grab value from
9325 element_byte_size, // Size of this type in bytes
9326 0, // Bitfield bit size
9327 0, // Bitfield bit offset
9328 show_types, // Boolean indicating if we should show the variable
9329 // types
9330 show_summary, // Boolean indicating if we should show a summary for
9331 // the current type
9332 verbose, // Verbose output?
9333 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9334 // children
9335 }
9336
9337 // Indent the trailing squiggly bracket
9338 if (element_idx > 0)
9339 s->Printf("\n%*s}", depth, "");
9340 }
9341 }
9342 return;
9343
9344 case clang::Type::Typedef: {
9345 clang::QualType typedef_qual_type =
9346 llvm::cast<clang::TypedefType>(qual_type)
9347 ->getDecl()
9348 ->getUnderlyingType();
9349
9350 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9351 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9352 clang::TypeInfo typedef_type_info =
9353 getASTContext()->getTypeInfo(typedef_qual_type);
9354 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9355
9356 return typedef_clang_type.DumpValue(
9357 exe_ctx,
9358 s, // Stream to dump to
9359 typedef_format, // The format with which to display the element
9360 data, // Data buffer containing all bytes for this type
9361 data_byte_offset, // Offset into "data" where to grab value from
9362 typedef_byte_size, // Size of this type in bytes
9363 bitfield_bit_size, // Bitfield bit size
9364 bitfield_bit_offset, // Bitfield bit offset
9365 show_types, // Boolean indicating if we should show the variable types
9366 show_summary, // Boolean indicating if we should show a summary for the
9367 // current type
9368 verbose, // Verbose output?
9369 depth); // Scope depth for any types that have children
9370 } break;
9371
9372 case clang::Type::Auto: {
9373 clang::QualType elaborated_qual_type =
9374 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9375 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9376 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9377 clang::TypeInfo elaborated_type_info =
9378 getASTContext()->getTypeInfo(elaborated_qual_type);
9379 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9380
9381 return elaborated_clang_type.DumpValue(
9382 exe_ctx,
9383 s, // Stream to dump to
9384 elaborated_format, // The format with which to display the element
9385 data, // Data buffer containing all bytes for this type
9386 data_byte_offset, // Offset into "data" where to grab value from
9387 elaborated_byte_size, // Size of this type in bytes
9388 bitfield_bit_size, // Bitfield bit size
9389 bitfield_bit_offset, // Bitfield bit offset
9390 show_types, // Boolean indicating if we should show the variable types
9391 show_summary, // Boolean indicating if we should show a summary for the
9392 // current type
9393 verbose, // Verbose output?
9394 depth); // Scope depth for any types that have children
9395 } break;
9396
9397 case clang::Type::Elaborated: {
9398 clang::QualType elaborated_qual_type =
9399 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9400 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9401 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9402 clang::TypeInfo elaborated_type_info =
9403 getASTContext()->getTypeInfo(elaborated_qual_type);
9404 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9405
9406 return elaborated_clang_type.DumpValue(
9407 exe_ctx,
9408 s, // Stream to dump to
9409 elaborated_format, // The format with which to display the element
9410 data, // Data buffer containing all bytes for this type
9411 data_byte_offset, // Offset into "data" where to grab value from
9412 elaborated_byte_size, // Size of this type in bytes
9413 bitfield_bit_size, // Bitfield bit size
9414 bitfield_bit_offset, // Bitfield bit offset
9415 show_types, // Boolean indicating if we should show the variable types
9416 show_summary, // Boolean indicating if we should show a summary for the
9417 // current type
9418 verbose, // Verbose output?
9419 depth); // Scope depth for any types that have children
9420 } break;
9421
9422 case clang::Type::Paren: {
9423 clang::QualType desugar_qual_type =
9424 llvm::cast<clang::ParenType>(qual_type)->desugar();
9425 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type);
9426
9427 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9428 clang::TypeInfo desugar_type_info =
9429 getASTContext()->getTypeInfo(desugar_qual_type);
9430 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9431
9432 return desugar_clang_type.DumpValue(
9433 exe_ctx,
9434 s, // Stream to dump to
9435 desugar_format, // The format with which to display the element
9436 data, // Data buffer containing all bytes for this type
9437 data_byte_offset, // Offset into "data" where to grab value from
9438 desugar_byte_size, // Size of this type in bytes
9439 bitfield_bit_size, // Bitfield bit size
9440 bitfield_bit_offset, // Bitfield bit offset
9441 show_types, // Boolean indicating if we should show the variable types
9442 show_summary, // Boolean indicating if we should show a summary for the
9443 // current type
9444 verbose, // Verbose output?
9445 depth); // Scope depth for any types that have children
9446 } break;
9447
9448 default:
9449 // We are down to a scalar type that we just need to display.
Zachary Turner29cb8682017-03-03 20:57:05 +00009450 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9451 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9452 bitfield_bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009453
9454 if (show_summary)
9455 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9456 break;
9457 }
9458}
9459
9460bool ClangASTContext::DumpTypeValue(
9461 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
Zachary Turner29cb8682017-03-03 20:57:05 +00009462 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9463 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009464 ExecutionContextScope *exe_scope) {
9465 if (!type)
9466 return false;
9467 if (IsAggregateType(type)) {
9468 return false;
9469 } else {
Greg Claytond8d4a572015-08-11 21:38:15 +00009470 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009471
9472 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9473 switch (type_class) {
9474 case clang::Type::Typedef: {
9475 clang::QualType typedef_qual_type =
9476 llvm::cast<clang::TypedefType>(qual_type)
9477 ->getDecl()
9478 ->getUnderlyingType();
9479 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9480 if (format == eFormatDefault)
9481 format = typedef_clang_type.GetFormat();
9482 clang::TypeInfo typedef_type_info =
9483 getASTContext()->getTypeInfo(typedef_qual_type);
9484 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9485
9486 return typedef_clang_type.DumpTypeValue(
9487 s,
9488 format, // The format with which to display the element
9489 data, // Data buffer containing all bytes for this type
9490 byte_offset, // Offset into "data" where to grab value from
9491 typedef_byte_size, // Size of this type in bytes
9492 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9493 // treat as a bitfield
9494 bitfield_bit_offset, // Offset in bits of a bitfield value if
9495 // bitfield_bit_size != 0
9496 exe_scope);
9497 } break;
9498
9499 case clang::Type::Enum:
Adrian Prantl05097242018-04-30 16:49:04 +00009500 // If our format is enum or default, show the enumeration value as its
9501 // enumeration string value, else just display it as requested.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009502 if ((format == eFormatEnum || format == eFormatDefault) &&
9503 GetCompleteType(type)) {
9504 const clang::EnumType *enutype =
9505 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9506 const clang::EnumDecl *enum_decl = enutype->getDecl();
9507 assert(enum_decl);
9508 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9509 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9510 lldb::offset_t offset = byte_offset;
9511 if (is_signed) {
9512 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9513 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9514 for (enum_pos = enum_decl->enumerator_begin(),
9515 enum_end_pos = enum_decl->enumerator_end();
9516 enum_pos != enum_end_pos; ++enum_pos) {
9517 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009518 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009519 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009520 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009521 }
9522 // If we have gotten here we didn't get find the enumerator in the
9523 // enum decl, so just print the integer.
9524 s->Printf("%" PRIi64, enum_svalue);
9525 } else {
9526 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9527 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9528 for (enum_pos = enum_decl->enumerator_begin(),
9529 enum_end_pos = enum_decl->enumerator_end();
9530 enum_pos != enum_end_pos; ++enum_pos) {
9531 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009532 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009533 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009534 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009535 }
9536 // If we have gotten here we didn't get find the enumerator in the
9537 // enum decl, so just print the integer.
9538 s->Printf("%" PRIu64, enum_uvalue);
Greg Claytond8d4a572015-08-11 21:38:15 +00009539 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009540 return true;
9541 }
9542 // format was not enum, just fall through and dump the value as
9543 // requested....
9544 LLVM_FALLTHROUGH;
9545
9546 default:
9547 // We are down to a scalar type that we just need to display.
9548 {
9549 uint32_t item_count = 1;
9550 // A few formats, we might need to modify our size and count for
9551 // depending
9552 // on how we are trying to display the value...
9553 switch (format) {
Greg Claytond8d4a572015-08-11 21:38:15 +00009554 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00009555 case eFormatBoolean:
9556 case eFormatBinary:
9557 case eFormatComplex:
9558 case eFormatCString: // NULL terminated C strings
9559 case eFormatDecimal:
9560 case eFormatEnum:
9561 case eFormatHex:
9562 case eFormatHexUppercase:
9563 case eFormatFloat:
9564 case eFormatOctal:
9565 case eFormatOSType:
9566 case eFormatUnsigned:
9567 case eFormatPointer:
9568 case eFormatVectorOfChar:
9569 case eFormatVectorOfSInt8:
9570 case eFormatVectorOfUInt8:
9571 case eFormatVectorOfSInt16:
9572 case eFormatVectorOfUInt16:
9573 case eFormatVectorOfSInt32:
9574 case eFormatVectorOfUInt32:
9575 case eFormatVectorOfSInt64:
9576 case eFormatVectorOfUInt64:
9577 case eFormatVectorOfFloat32:
9578 case eFormatVectorOfFloat64:
9579 case eFormatVectorOfUInt128:
9580 break;
9581
9582 case eFormatChar:
9583 case eFormatCharPrintable:
9584 case eFormatCharArray:
9585 case eFormatBytes:
9586 case eFormatBytesWithASCII:
9587 item_count = byte_size;
9588 byte_size = 1;
9589 break;
9590
9591 case eFormatUnicode16:
9592 item_count = byte_size / 2;
9593 byte_size = 2;
9594 break;
9595
9596 case eFormatUnicode32:
9597 item_count = byte_size / 4;
9598 byte_size = 4;
9599 break;
9600 }
Zachary Turner29cb8682017-03-03 20:57:05 +00009601 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9602 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
9603 bitfield_bit_size, bitfield_bit_offset,
9604 exe_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009605 }
9606 break;
9607 }
9608 }
9609 return 0;
9610}
9611
9612void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9613 ExecutionContext *exe_ctx, Stream *s,
9614 const lldb_private::DataExtractor &data,
9615 lldb::offset_t data_byte_offset,
9616 size_t data_byte_size) {
9617 uint32_t length = 0;
9618 if (IsCStringType(type, length)) {
9619 if (exe_ctx) {
9620 Process *process = exe_ctx->GetProcessPtr();
9621 if (process) {
9622 lldb::offset_t offset = data_byte_offset;
9623 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9624 std::vector<uint8_t> buf;
9625 if (length > 0)
9626 buf.resize(length);
9627 else
9628 buf.resize(256);
9629
Zachary Turner29cb8682017-03-03 20:57:05 +00009630 DataExtractor cstr_data(&buf.front(), buf.size(),
9631 process->GetByteOrder(), 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009632 buf.back() = '\0';
9633 size_t bytes_read;
9634 size_t total_cstr_len = 0;
Zachary Turner97206d52017-05-12 04:51:55 +00009635 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009636 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9637 buf.size(), error)) > 0) {
9638 const size_t len = strlen((const char *)&buf.front());
9639 if (len == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00009640 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009641 if (total_cstr_len == 0)
9642 s->PutCString(" \"");
Zachary Turner29cb8682017-03-03 20:57:05 +00009643 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9644 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009645 total_cstr_len += len;
9646 if (len < buf.size())
9647 break;
9648 pointer_address += total_cstr_len;
Greg Claytond8d4a572015-08-11 21:38:15 +00009649 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009650 if (total_cstr_len > 0)
9651 s->PutChar('"');
9652 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009653 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009654 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009655}
9656
Kate Stoneb9c1b512016-09-06 20:57:50 +00009657void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9658 StreamFile s(stdout, false);
9659 DumpTypeDescription(type, &s);
9660 ClangASTMetadata *metadata =
9661 ClangASTContext::GetMetadata(getASTContext(), type);
9662 if (metadata) {
9663 metadata->Dump(&s);
9664 }
9665}
Greg Claytond8d4a572015-08-11 21:38:15 +00009666
Kate Stoneb9c1b512016-09-06 20:57:50 +00009667void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9668 Stream *s) {
9669 if (type) {
9670 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00009671
Kate Stoneb9c1b512016-09-06 20:57:50 +00009672 llvm::SmallVector<char, 1024> buf;
9673 llvm::raw_svector_ostream llvm_ostrm(buf);
9674
9675 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9676 switch (type_class) {
9677 case clang::Type::ObjCObject:
9678 case clang::Type::ObjCInterface: {
9679 GetCompleteType(type);
9680
9681 const clang::ObjCObjectType *objc_class_type =
9682 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9683 assert(objc_class_type);
9684 if (objc_class_type) {
9685 clang::ObjCInterfaceDecl *class_interface_decl =
9686 objc_class_type->getInterface();
9687 if (class_interface_decl) {
9688 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9689 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
Greg Claytond8d4a572015-08-11 21:38:15 +00009690 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009691 }
9692 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00009693
Kate Stoneb9c1b512016-09-06 20:57:50 +00009694 case clang::Type::Typedef: {
9695 const clang::TypedefType *typedef_type =
9696 qual_type->getAs<clang::TypedefType>();
9697 if (typedef_type) {
9698 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9699 std::string clang_typedef_name(
9700 typedef_decl->getQualifiedNameAsString());
9701 if (!clang_typedef_name.empty()) {
9702 s->PutCString("typedef ");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009703 s->PutCString(clang_typedef_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00009704 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009705 }
9706 } break;
9707
9708 case clang::Type::Auto:
9709 CompilerType(getASTContext(),
9710 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
9711 .DumpTypeDescription(s);
9712 return;
9713
9714 case clang::Type::Elaborated:
9715 CompilerType(getASTContext(),
9716 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
9717 .DumpTypeDescription(s);
9718 return;
9719
9720 case clang::Type::Paren:
9721 CompilerType(getASTContext(),
9722 llvm::cast<clang::ParenType>(qual_type)->desugar())
9723 .DumpTypeDescription(s);
9724 return;
9725
9726 case clang::Type::Record: {
9727 GetCompleteType(type);
9728
9729 const clang::RecordType *record_type =
9730 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9731 const clang::RecordDecl *record_decl = record_type->getDecl();
9732 const clang::CXXRecordDecl *cxx_record_decl =
9733 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9734
9735 if (cxx_record_decl)
9736 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9737 s->GetIndentLevel());
9738 else
9739 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9740 s->GetIndentLevel());
9741 } break;
9742
9743 default: {
9744 const clang::TagType *tag_type =
9745 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9746 if (tag_type) {
9747 clang::TagDecl *tag_decl = tag_type->getDecl();
9748 if (tag_decl)
9749 tag_decl->print(llvm_ostrm, 0);
9750 } else {
9751 std::string clang_type_name(qual_type.getAsString());
9752 if (!clang_type_name.empty())
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009753 s->PutCString(clang_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009754 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009755 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00009756 }
9757
Kate Stoneb9c1b512016-09-06 20:57:50 +00009758 if (buf.size() > 0) {
9759 s->Write(buf.data(), buf.size());
Greg Clayton8b4edba2015-08-14 20:02:05 +00009760 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009761 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00009762}
9763
Kate Stoneb9c1b512016-09-06 20:57:50 +00009764void ClangASTContext::DumpTypeName(const CompilerType &type) {
9765 if (ClangUtil::IsClangType(type)) {
9766 clang::QualType qual_type(
9767 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9768
9769 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9770 switch (type_class) {
9771 case clang::Type::Record: {
9772 const clang::CXXRecordDecl *cxx_record_decl =
9773 qual_type->getAsCXXRecordDecl();
9774 if (cxx_record_decl)
9775 printf("class %s", cxx_record_decl->getName().str().c_str());
9776 } break;
9777
9778 case clang::Type::Enum: {
9779 clang::EnumDecl *enum_decl =
9780 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9781 if (enum_decl) {
9782 printf("enum %s", enum_decl->getName().str().c_str());
9783 }
9784 } break;
9785
9786 case clang::Type::ObjCObject:
9787 case clang::Type::ObjCInterface: {
9788 const clang::ObjCObjectType *objc_class_type =
9789 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9790 if (objc_class_type) {
9791 clang::ObjCInterfaceDecl *class_interface_decl =
9792 objc_class_type->getInterface();
Adrian Prantl05097242018-04-30 16:49:04 +00009793 // We currently can't complete objective C types through the newly
9794 // added ASTContext because it only supports TagDecl objects right
9795 // now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00009796 if (class_interface_decl)
9797 printf("@class %s", class_interface_decl->getName().str().c_str());
9798 }
9799 } break;
9800
9801 case clang::Type::Typedef:
9802 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9803 ->getDecl()
9804 ->getName()
9805 .str()
9806 .c_str());
9807 break;
9808
9809 case clang::Type::Auto:
9810 printf("auto ");
9811 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9812 llvm::cast<clang::AutoType>(qual_type)
9813 ->getDeducedType()
9814 .getAsOpaquePtr()));
9815
9816 case clang::Type::Elaborated:
9817 printf("elaborated ");
9818 return DumpTypeName(CompilerType(
9819 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9820 ->getNamedType()
9821 .getAsOpaquePtr()));
9822
9823 case clang::Type::Paren:
9824 printf("paren ");
9825 return DumpTypeName(CompilerType(
9826 type.GetTypeSystem(),
9827 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9828
9829 default:
9830 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9831 break;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009832 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009833 }
Greg Clayton6dc8d582015-08-18 22:32:36 +00009834}
9835
Kate Stoneb9c1b512016-09-06 20:57:50 +00009836clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9837 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9838 const char *parent_name, int tag_decl_kind,
9839 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9840 if (template_param_infos.IsValid()) {
9841 std::string template_basename(parent_name);
9842 template_basename.erase(template_basename.find('<'));
9843
9844 return CreateClassTemplateDecl(decl_ctx, access_type,
9845 template_basename.c_str(), tag_decl_kind,
9846 template_param_infos);
9847 }
9848 return NULL;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009849}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009850
Kate Stoneb9c1b512016-09-06 20:57:50 +00009851void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9852 ClangASTContext *ast = (ClangASTContext *)baton;
9853 SymbolFile *sym_file = ast->GetSymbolFile();
9854 if (sym_file) {
9855 CompilerType clang_type = GetTypeForDecl(decl);
9856 if (clang_type)
9857 sym_file->CompleteType(clang_type);
9858 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00009859}
9860
Kate Stoneb9c1b512016-09-06 20:57:50 +00009861void ClangASTContext::CompleteObjCInterfaceDecl(
9862 void *baton, clang::ObjCInterfaceDecl *decl) {
9863 ClangASTContext *ast = (ClangASTContext *)baton;
9864 SymbolFile *sym_file = ast->GetSymbolFile();
9865 if (sym_file) {
9866 CompilerType clang_type = GetTypeForDecl(decl);
9867 if (clang_type)
9868 sym_file->CompleteType(clang_type);
9869 }
Zachary Turner42dff792016-04-15 00:21:26 +00009870}
Greg Clayton261ac3f2015-08-28 01:01:03 +00009871
Kate Stoneb9c1b512016-09-06 20:57:50 +00009872DWARFASTParser *ClangASTContext::GetDWARFParser() {
9873 if (!m_dwarf_ast_parser_ap)
9874 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
9875 return m_dwarf_ast_parser_ap.get();
9876}
9877
9878PDBASTParser *ClangASTContext::GetPDBParser() {
9879 if (!m_pdb_ast_parser_ap)
9880 m_pdb_ast_parser_ap.reset(new PDBASTParser(*this));
9881 return m_pdb_ast_parser_ap.get();
9882}
9883
9884bool ClangASTContext::LayoutRecordType(
9885 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9886 uint64_t &alignment,
9887 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9888 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9889 &base_offsets,
9890 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9891 &vbase_offsets) {
9892 ClangASTContext *ast = (ClangASTContext *)baton;
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00009893 lldb_private::ClangASTImporter *importer = nullptr;
9894 if (ast->m_dwarf_ast_parser_ap)
9895 importer = &ast->m_dwarf_ast_parser_ap->GetClangASTImporter();
9896 if (!importer && ast->m_pdb_ast_parser_ap)
9897 importer = &ast->m_pdb_ast_parser_ap->GetClangASTImporter();
9898 if (!importer)
9899 return false;
9900
9901 return importer->LayoutRecordType(record_decl, bit_size, alignment,
9902 field_offsets, base_offsets, vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009903}
9904
Greg Clayton99558cc42015-08-24 23:46:31 +00009905//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009906// CompilerDecl override functions
9907//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009908
Kate Stoneb9c1b512016-09-06 20:57:50 +00009909ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9910 if (opaque_decl) {
9911 clang::NamedDecl *nd =
9912 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9913 if (nd != nullptr)
9914 return ConstString(nd->getDeclName().getAsString());
9915 }
9916 return ConstString();
Paul Hermand628cbb2015-09-15 23:44:17 +00009917}
9918
Kate Stoneb9c1b512016-09-06 20:57:50 +00009919ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9920 if (opaque_decl) {
9921 clang::NamedDecl *nd =
9922 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9923 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9924 clang::MangleContext *mc = getMangleContext();
9925 if (mc && mc->shouldMangleCXXName(nd)) {
9926 llvm::SmallVector<char, 1024> buf;
9927 llvm::raw_svector_ostream llvm_ostrm(buf);
9928 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9929 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9930 Ctor_Complete, llvm_ostrm);
9931 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9932 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9933 Dtor_Complete, llvm_ostrm);
9934 } else {
9935 mc->mangleName(nd, llvm_ostrm);
Greg Claytonfe689042015-11-10 17:47:04 +00009936 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009937 if (buf.size() > 0)
9938 return ConstString(buf.data(), buf.size());
9939 }
Greg Claytonfe689042015-11-10 17:47:04 +00009940 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009941 }
9942 return ConstString();
Greg Claytonfe689042015-11-10 17:47:04 +00009943}
9944
Kate Stoneb9c1b512016-09-06 20:57:50 +00009945CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9946 if (opaque_decl)
9947 return CompilerDeclContext(this,
9948 ((clang::Decl *)opaque_decl)->getDeclContext());
9949 else
9950 return CompilerDeclContext();
Greg Claytonfe689042015-11-10 17:47:04 +00009951}
9952
Kate Stoneb9c1b512016-09-06 20:57:50 +00009953CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9954 if (clang::FunctionDecl *func_decl =
9955 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9956 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9957 if (clang::ObjCMethodDecl *objc_method =
9958 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9959 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9960 else
Greg Claytonfe689042015-11-10 17:47:04 +00009961 return CompilerType();
9962}
9963
Kate Stoneb9c1b512016-09-06 20:57:50 +00009964size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9965 if (clang::FunctionDecl *func_decl =
9966 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9967 return func_decl->param_size();
9968 if (clang::ObjCMethodDecl *objc_method =
9969 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9970 return objc_method->param_size();
9971 else
9972 return 0;
9973}
9974
9975CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
9976 size_t idx) {
9977 if (clang::FunctionDecl *func_decl =
9978 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
9979 if (idx < func_decl->param_size()) {
9980 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9981 if (var_decl)
9982 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
9983 }
9984 } else if (clang::ObjCMethodDecl *objc_method =
9985 llvm::dyn_cast<clang::ObjCMethodDecl>(
9986 (clang::Decl *)opaque_decl)) {
9987 if (idx < objc_method->param_size())
9988 return CompilerType(
9989 this,
9990 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9991 }
9992 return CompilerType();
9993}
9994
Paul Hermand628cbb2015-09-15 23:44:17 +00009995//----------------------------------------------------------------------
Greg Clayton99558cc42015-08-24 23:46:31 +00009996// CompilerDeclContext functions
9997//----------------------------------------------------------------------
9998
Kate Stoneb9c1b512016-09-06 20:57:50 +00009999std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
10000 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
10001 std::vector<CompilerDecl> found_decls;
10002 if (opaque_decl_ctx) {
10003 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
10004 std::set<DeclContext *> searched;
10005 std::multimap<DeclContext *, DeclContext *> search_queue;
10006 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +000010007
Kate Stoneb9c1b512016-09-06 20:57:50 +000010008 for (clang::DeclContext *decl_context = root_decl_ctx;
10009 decl_context != nullptr && found_decls.empty();
10010 decl_context = decl_context->getParent()) {
10011 search_queue.insert(std::make_pair(decl_context, decl_context));
Paul Hermand628cbb2015-09-15 23:44:17 +000010012
Kate Stoneb9c1b512016-09-06 20:57:50 +000010013 for (auto it = search_queue.find(decl_context); it != search_queue.end();
10014 it++) {
10015 if (!searched.insert(it->second).second)
10016 continue;
10017 symbol_file->ParseDeclsForContext(
10018 CompilerDeclContext(this, it->second));
Paul Hermanea188fc2015-09-16 18:48:30 +000010019
Kate Stoneb9c1b512016-09-06 20:57:50 +000010020 for (clang::Decl *child : it->second->decls()) {
10021 if (clang::UsingDirectiveDecl *ud =
10022 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10023 if (ignore_using_decls)
10024 continue;
10025 clang::DeclContext *from = ud->getCommonAncestor();
10026 if (searched.find(ud->getNominatedNamespace()) == searched.end())
10027 search_queue.insert(
10028 std::make_pair(from, ud->getNominatedNamespace()));
10029 } else if (clang::UsingDecl *ud =
10030 llvm::dyn_cast<clang::UsingDecl>(child)) {
10031 if (ignore_using_decls)
10032 continue;
10033 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10034 clang::Decl *target = usd->getTargetDecl();
10035 if (clang::NamedDecl *nd =
10036 llvm::dyn_cast<clang::NamedDecl>(target)) {
10037 IdentifierInfo *ii = nd->getIdentifier();
10038 if (ii != nullptr &&
10039 ii->getName().equals(name.AsCString(nullptr)))
10040 found_decls.push_back(CompilerDecl(this, nd));
10041 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010042 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010043 } else if (clang::NamedDecl *nd =
10044 llvm::dyn_cast<clang::NamedDecl>(child)) {
10045 IdentifierInfo *ii = nd->getIdentifier();
10046 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
10047 found_decls.push_back(CompilerDecl(this, nd));
10048 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010049 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010050 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010051 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010052 }
10053 return found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +000010054}
10055
Dawn Perchikb5925782015-12-12 19:31:41 +000010056// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010057// and return the number of levels it took to find it, or
Adrian Prantl05097242018-04-30 16:49:04 +000010058// LLDB_INVALID_DECL_LEVEL if not found. If the decl was imported via a using
10059// declaration, its name and/or type, if set, will be used to check that the
10060// decl found in the scope is a match.
Dawn Perchikb5925782015-12-12 19:31:41 +000010061//
Kate Stoneb9c1b512016-09-06 20:57:50 +000010062// The optional name is required by languages (like C++) to handle using
Adrian Prantl05097242018-04-30 16:49:04 +000010063// declarations like:
Dawn Perchikb5925782015-12-12 19:31:41 +000010064//
10065// void poo();
10066// namespace ns {
10067// void foo();
10068// void goo();
10069// }
10070// void bar() {
10071// using ns::foo;
10072// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
10073// // LLDB_INVALID_DECL_LEVEL for 'goo'.
10074// }
10075//
10076// The optional type is useful in the case that there's a specific overload
10077// that we're looking for that might otherwise be shadowed, like:
10078//
10079// void foo(int);
10080// namespace ns {
10081// void foo();
10082// }
10083// void bar() {
10084// using ns::foo;
10085// // CountDeclLevels returns 0 for { 'foo', void() },
10086// // 1 for { 'foo', void(int) }, and
10087// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
10088// }
10089//
10090// NOTE: Because file statics are at the TranslationUnit along with globals, a
Kate Stoneb9c1b512016-09-06 20:57:50 +000010091// function at file scope will return the same level as a function at global
Adrian Prantl05097242018-04-30 16:49:04 +000010092// scope. Ideally we'd like to treat the file scope as an additional scope just
10093// below the global scope. More work needs to be done to recognise that, if
10094// the decl we're trying to look up is static, we should compare its source
10095// file with that of the current scope and return a lower number for it.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010096uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
10097 clang::DeclContext *child_decl_ctx,
10098 ConstString *child_name,
10099 CompilerType *child_type) {
10100 if (frame_decl_ctx) {
10101 std::set<DeclContext *> searched;
10102 std::multimap<DeclContext *, DeclContext *> search_queue;
10103 SymbolFile *symbol_file = GetSymbolFile();
Dawn Perchikb5925782015-12-12 19:31:41 +000010104
Kate Stoneb9c1b512016-09-06 20:57:50 +000010105 // Get the lookup scope for the decl we're trying to find.
10106 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
Dawn Perchikb5925782015-12-12 19:31:41 +000010107
Kate Stoneb9c1b512016-09-06 20:57:50 +000010108 // Look for it in our scope's decl context and its parents.
10109 uint32_t level = 0;
10110 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
10111 decl_ctx = decl_ctx->getParent()) {
10112 if (!decl_ctx->isLookupContext())
10113 continue;
10114 if (decl_ctx == parent_decl_ctx)
10115 // Found it!
10116 return level;
10117 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
10118 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
10119 it++) {
10120 if (searched.find(it->second) != searched.end())
10121 continue;
10122
10123 // Currently DWARF has one shared translation unit for all Decls at top
Adrian Prantl05097242018-04-30 16:49:04 +000010124 // level, so this would erroneously find using statements anywhere. So
10125 // don't look at the top-level translation unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010126 // TODO fix this and add a testcase that depends on it.
10127
10128 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
10129 continue;
10130
10131 searched.insert(it->second);
10132 symbol_file->ParseDeclsForContext(
10133 CompilerDeclContext(this, it->second));
10134
10135 for (clang::Decl *child : it->second->decls()) {
10136 if (clang::UsingDirectiveDecl *ud =
10137 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10138 clang::DeclContext *ns = ud->getNominatedNamespace();
10139 if (ns == parent_decl_ctx)
10140 // Found it!
10141 return level;
10142 clang::DeclContext *from = ud->getCommonAncestor();
10143 if (searched.find(ns) == searched.end())
10144 search_queue.insert(std::make_pair(from, ns));
10145 } else if (child_name) {
10146 if (clang::UsingDecl *ud =
10147 llvm::dyn_cast<clang::UsingDecl>(child)) {
10148 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10149 clang::Decl *target = usd->getTargetDecl();
10150 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
10151 if (!nd)
10152 continue;
10153 // Check names.
10154 IdentifierInfo *ii = nd->getIdentifier();
10155 if (ii == nullptr ||
10156 !ii->getName().equals(child_name->AsCString(nullptr)))
10157 continue;
10158 // Check types, if one was provided.
10159 if (child_type) {
10160 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
10161 if (!AreTypesSame(clang_type, *child_type,
10162 /*ignore_qualifiers=*/true))
10163 continue;
10164 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010165 // Found it!
10166 return level;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010167 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010168 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010169 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010170 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010171 }
10172 ++level;
Dawn Perchikb5925782015-12-12 19:31:41 +000010173 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010174 }
10175 return LLDB_INVALID_DECL_LEVEL;
Dawn Perchikb5925782015-12-12 19:31:41 +000010176}
10177
Kate Stoneb9c1b512016-09-06 20:57:50 +000010178bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
10179 if (opaque_decl_ctx)
10180 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
10181 else
Greg Clayton99558cc42015-08-24 23:46:31 +000010182 return false;
10183}
10184
Kate Stoneb9c1b512016-09-06 20:57:50 +000010185ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
10186 if (opaque_decl_ctx) {
10187 clang::NamedDecl *named_decl =
10188 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10189 if (named_decl)
10190 return ConstString(named_decl->getName());
10191 }
10192 return ConstString();
Greg Clayton99558cc42015-08-24 23:46:31 +000010193}
10194
Kate Stoneb9c1b512016-09-06 20:57:50 +000010195ConstString
10196ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
10197 if (opaque_decl_ctx) {
10198 clang::NamedDecl *named_decl =
10199 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10200 if (named_decl)
10201 return ConstString(
10202 llvm::StringRef(named_decl->getQualifiedNameAsString()));
10203 }
10204 return ConstString();
10205}
10206
10207bool ClangASTContext::DeclContextIsClassMethod(
10208 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
10209 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
10210 if (opaque_decl_ctx) {
10211 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10212 if (ObjCMethodDecl *objc_method =
10213 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
10214 if (is_instance_method_ptr)
10215 *is_instance_method_ptr = objc_method->isInstanceMethod();
10216 if (language_ptr)
10217 *language_ptr = eLanguageTypeObjC;
10218 if (language_object_name_ptr)
10219 language_object_name_ptr->SetCString("self");
10220 return true;
10221 } else if (CXXMethodDecl *cxx_method =
10222 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
10223 if (is_instance_method_ptr)
10224 *is_instance_method_ptr = cxx_method->isInstance();
10225 if (language_ptr)
10226 *language_ptr = eLanguageTypeC_plus_plus;
10227 if (language_object_name_ptr)
10228 language_object_name_ptr->SetCString("this");
10229 return true;
10230 } else if (clang::FunctionDecl *function_decl =
10231 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
10232 ClangASTMetadata *metadata =
10233 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
10234 if (metadata && metadata->HasObjectPtr()) {
10235 if (is_instance_method_ptr)
10236 *is_instance_method_ptr = true;
10237 if (language_ptr)
10238 *language_ptr = eLanguageTypeObjC;
10239 if (language_object_name_ptr)
10240 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
10241 return true;
10242 }
10243 }
10244 }
10245 return false;
10246}
10247
10248clang::DeclContext *
10249ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
10250 if (dc.IsClang())
10251 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
10252 return nullptr;
10253}
Greg Clayton99558cc42015-08-24 23:46:31 +000010254
10255ObjCMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010256ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
10257 if (dc.IsClang())
10258 return llvm::dyn_cast<clang::ObjCMethodDecl>(
10259 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10260 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010261}
10262
10263CXXMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010264ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10265 if (dc.IsClang())
10266 return llvm::dyn_cast<clang::CXXMethodDecl>(
10267 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10268 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010269}
10270
10271clang::FunctionDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010272ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10273 if (dc.IsClang())
10274 return llvm::dyn_cast<clang::FunctionDecl>(
10275 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10276 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010277}
10278
10279clang::NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010280ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10281 if (dc.IsClang())
10282 return llvm::dyn_cast<clang::NamespaceDecl>(
10283 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10284 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010285}
10286
10287ClangASTMetadata *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010288ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10289 const void *object) {
10290 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10291 if (ast)
10292 return ClangASTContext::GetMetadata(ast, object);
10293 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010294}
10295
10296clang::ASTContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010297ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10298 ClangASTContext *ast =
10299 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10300 if (ast)
10301 return ast->getASTContext();
10302 return nullptr;
10303}
10304
10305ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10306 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10307 m_target_wp(target.shared_from_this()),
10308 m_persistent_variables(new ClangPersistentVariables) {}
10309
10310UserExpression *ClangASTContextForExpressions::GetUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010311 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010312 Expression::ResultType desired_type,
10313 const EvaluateExpressionOptions &options) {
10314 TargetSP target_sp = m_target_wp.lock();
10315 if (!target_sp)
Greg Clayton99558cc42015-08-24 23:46:31 +000010316 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010317
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010318 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010319 desired_type, options);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010320}
10321
Kate Stoneb9c1b512016-09-06 20:57:50 +000010322FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10323 const CompilerType &return_type, const Address &function_address,
10324 const ValueList &arg_value_list, const char *name) {
10325 TargetSP target_sp = m_target_wp.lock();
10326 if (!target_sp)
10327 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010328
Kate Stoneb9c1b512016-09-06 20:57:50 +000010329 Process *process = target_sp->GetProcessSP().get();
10330 if (!process)
10331 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010332
Kate Stoneb9c1b512016-09-06 20:57:50 +000010333 return new ClangFunctionCaller(*process, return_type, function_address,
10334 arg_value_list, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010335}
10336
10337UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010338ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10339 const char *name) {
10340 TargetSP target_sp = m_target_wp.lock();
10341 if (!target_sp)
10342 return nullptr;
10343
10344 return new ClangUtilityFunction(*target_sp.get(), text, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010345}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010346
10347PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010348ClangASTContextForExpressions::GetPersistentExpressionState() {
10349 return m_persistent_variables.get();
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010350}
Sean Callanan68e44232017-09-28 20:20:25 +000010351
10352clang::ExternalASTMerger &
10353ClangASTContextForExpressions::GetMergerUnchecked() {
Eugene Zemtsova9d928c2017-09-29 03:15:08 +000010354 lldbassert(m_scratch_ast_source_ap != nullptr);
Sean Callanan68e44232017-09-28 20:20:25 +000010355 return m_scratch_ast_source_ap->GetMergerUnchecked();
10356}