blob: f13631076b41c6fafe9322a5defbf8fe5114a456 [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);
995 if (qual_type_bit_size == bit_size)
996 return true;
997 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000998}
Greg Clayton56939cb2015-09-17 22:23:34 +0000999
Greg Claytona1e5dc82015-08-11 22:53:00 +00001000CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00001001ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
1002 size_t bit_size) {
1003 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
1004 getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001005}
1006
Kate Stoneb9c1b512016-09-06 20:57:50 +00001007CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
1008 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
1009 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001010 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001011 switch (encoding) {
1012 case eEncodingInvalid:
1013 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1014 return CompilerType(ast, ast->VoidPtrTy);
1015 break;
1016
1017 case eEncodingUint:
1018 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1019 return CompilerType(ast, ast->UnsignedCharTy);
1020 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1021 return CompilerType(ast, ast->UnsignedShortTy);
1022 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1023 return CompilerType(ast, ast->UnsignedIntTy);
1024 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1025 return CompilerType(ast, ast->UnsignedLongTy);
1026 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1027 return CompilerType(ast, ast->UnsignedLongLongTy);
1028 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1029 return CompilerType(ast, ast->UnsignedInt128Ty);
1030 break;
1031
1032 case eEncodingSint:
1033 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1034 return CompilerType(ast, ast->SignedCharTy);
1035 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1036 return CompilerType(ast, ast->ShortTy);
1037 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1038 return CompilerType(ast, ast->IntTy);
1039 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1040 return CompilerType(ast, ast->LongTy);
1041 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1042 return CompilerType(ast, ast->LongLongTy);
1043 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1044 return CompilerType(ast, ast->Int128Ty);
1045 break;
1046
1047 case eEncodingIEEE754:
1048 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1049 return CompilerType(ast, ast->FloatTy);
1050 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1051 return CompilerType(ast, ast->DoubleTy);
1052 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1053 return CompilerType(ast, ast->LongDoubleTy);
1054 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1055 return CompilerType(ast, ast->HalfTy);
1056 break;
1057
1058 case eEncodingVector:
1059 // Sanity check that bit_size is a multiple of 8's.
1060 if (bit_size && !(bit_size & 0x7u))
1061 return CompilerType(
1062 ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8));
1063 break;
1064 }
1065
1066 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001067}
1068
Greg Clayton57ee3062013-07-11 22:46:58 +00001069lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00001070ClangASTContext::GetBasicTypeEnumeration(const ConstString &name) {
1071 if (name) {
1072 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
1073 static TypeNameToBasicTypeMap g_type_map;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +00001074 static llvm::once_flag g_once_flag;
1075 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001076 // "void"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001077 g_type_map.Append(ConstString("void"), eBasicTypeVoid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001078
1079 // "char"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001080 g_type_map.Append(ConstString("char"), eBasicTypeChar);
1081 g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar);
1082 g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar);
1083 g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar);
1084 g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar);
1085 g_type_map.Append(ConstString("unsigned wchar_t"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001086 eBasicTypeUnsignedWChar);
1087 // "short"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001088 g_type_map.Append(ConstString("short"), eBasicTypeShort);
1089 g_type_map.Append(ConstString("short int"), eBasicTypeShort);
1090 g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort);
1091 g_type_map.Append(ConstString("unsigned short int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001092 eBasicTypeUnsignedShort);
1093
1094 // "int"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001095 g_type_map.Append(ConstString("int"), eBasicTypeInt);
1096 g_type_map.Append(ConstString("signed int"), eBasicTypeInt);
1097 g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt);
1098 g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001099
1100 // "long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001101 g_type_map.Append(ConstString("long"), eBasicTypeLong);
1102 g_type_map.Append(ConstString("long int"), eBasicTypeLong);
1103 g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong);
1104 g_type_map.Append(ConstString("unsigned long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001105 eBasicTypeUnsignedLong);
1106
1107 // "long long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001108 g_type_map.Append(ConstString("long long"), eBasicTypeLongLong);
1109 g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong);
1110 g_type_map.Append(ConstString("unsigned long long"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001111 eBasicTypeUnsignedLongLong);
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001112 g_type_map.Append(ConstString("unsigned long long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001113 eBasicTypeUnsignedLongLong);
1114
1115 // "int128"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001116 g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128);
1117 g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001118
1119 // Miscellaneous
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001120 g_type_map.Append(ConstString("bool"), eBasicTypeBool);
1121 g_type_map.Append(ConstString("float"), eBasicTypeFloat);
1122 g_type_map.Append(ConstString("double"), eBasicTypeDouble);
1123 g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble);
1124 g_type_map.Append(ConstString("id"), eBasicTypeObjCID);
1125 g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel);
1126 g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001127 g_type_map.Sort();
1128 });
1129
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001130 return g_type_map.Find(name, eBasicTypeInvalid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001131 }
1132 return eBasicTypeInvalid;
Greg Clayton57ee3062013-07-11 22:46:58 +00001133}
1134
Kate Stoneb9c1b512016-09-06 20:57:50 +00001135CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1136 const ConstString &name) {
1137 if (ast) {
1138 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
1139 return ClangASTContext::GetBasicType(ast, basic_type);
1140 }
1141 return CompilerType();
1142}
1143
1144uint32_t ClangASTContext::GetPointerByteSize() {
1145 if (m_pointer_byte_size == 0)
1146 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid)
1147 .GetPointerType()
1148 .GetByteSize(nullptr);
1149 return m_pointer_byte_size;
1150}
1151
1152CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
1153 return GetBasicType(getASTContext(), basic_type);
1154}
1155
1156CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1157 lldb::BasicType basic_type) {
1158 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001159 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001160 lldb::opaque_compiler_type_t clang_type =
1161 GetOpaqueCompilerType(ast, basic_type);
1162
1163 if (clang_type)
1164 return CompilerType(GetASTContext(ast), clang_type);
1165 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +00001166}
1167
Kate Stoneb9c1b512016-09-06 20:57:50 +00001168CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
1169 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
1170 ASTContext *ast = getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001171
Kate Stoneb9c1b512016-09-06 20:57:50 +00001172#define streq(a, b) strcmp(a, b) == 0
1173 assert(ast != nullptr);
1174 if (ast) {
1175 switch (dw_ate) {
1176 default:
1177 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001178
Kate Stoneb9c1b512016-09-06 20:57:50 +00001179 case DW_ATE_address:
1180 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1181 return CompilerType(ast, ast->VoidPtrTy);
1182 break;
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001183
Kate Stoneb9c1b512016-09-06 20:57:50 +00001184 case DW_ATE_boolean:
1185 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
1186 return CompilerType(ast, ast->BoolTy);
1187 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1188 return CompilerType(ast, ast->UnsignedCharTy);
1189 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1190 return CompilerType(ast, ast->UnsignedShortTy);
1191 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1192 return CompilerType(ast, ast->UnsignedIntTy);
1193 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001194
Kate Stoneb9c1b512016-09-06 20:57:50 +00001195 case DW_ATE_lo_user:
1196 // This has been seen to mean DW_AT_complex_integer
1197 if (type_name) {
1198 if (::strstr(type_name, "complex")) {
1199 CompilerType complex_int_clang_type =
1200 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1201 bit_size / 2);
1202 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1203 complex_int_clang_type)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001204 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001205 }
1206 break;
1207
1208 case DW_ATE_complex_float:
1209 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
1210 return CompilerType(ast, ast->FloatComplexTy);
1211 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
1212 return CompilerType(ast, ast->DoubleComplexTy);
1213 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
1214 return CompilerType(ast, ast->LongDoubleComplexTy);
1215 else {
1216 CompilerType complex_float_clang_type =
1217 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1218 bit_size / 2);
1219 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1220 complex_float_clang_type)));
1221 }
1222 break;
1223
1224 case DW_ATE_float:
1225 if (streq(type_name, "float") &&
1226 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1227 return CompilerType(ast, ast->FloatTy);
1228 if (streq(type_name, "double") &&
1229 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1230 return CompilerType(ast, ast->DoubleTy);
1231 if (streq(type_name, "long double") &&
1232 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1233 return CompilerType(ast, ast->LongDoubleTy);
1234 // Fall back to not requiring a name match
1235 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1236 return CompilerType(ast, ast->FloatTy);
1237 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1238 return CompilerType(ast, ast->DoubleTy);
1239 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1240 return CompilerType(ast, ast->LongDoubleTy);
1241 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1242 return CompilerType(ast, ast->HalfTy);
1243 break;
1244
1245 case DW_ATE_signed:
1246 if (type_name) {
1247 if (streq(type_name, "wchar_t") &&
1248 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1249 (getTargetInfo() &&
1250 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1251 return CompilerType(ast, ast->WCharTy);
1252 if (streq(type_name, "void") &&
1253 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
1254 return CompilerType(ast, ast->VoidTy);
1255 if (strstr(type_name, "long long") &&
1256 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1257 return CompilerType(ast, ast->LongLongTy);
1258 if (strstr(type_name, "long") &&
1259 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1260 return CompilerType(ast, ast->LongTy);
1261 if (strstr(type_name, "short") &&
1262 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1263 return CompilerType(ast, ast->ShortTy);
1264 if (strstr(type_name, "char")) {
1265 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1266 return CompilerType(ast, ast->CharTy);
1267 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1268 return CompilerType(ast, ast->SignedCharTy);
1269 }
1270 if (strstr(type_name, "int")) {
1271 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1272 return CompilerType(ast, ast->IntTy);
1273 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1274 return CompilerType(ast, ast->Int128Ty);
1275 }
1276 }
1277 // We weren't able to match up a type name, just search by size
1278 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1279 return CompilerType(ast, ast->CharTy);
1280 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1281 return CompilerType(ast, ast->ShortTy);
1282 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1283 return CompilerType(ast, ast->IntTy);
1284 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1285 return CompilerType(ast, ast->LongTy);
1286 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1287 return CompilerType(ast, ast->LongLongTy);
1288 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1289 return CompilerType(ast, ast->Int128Ty);
1290 break;
1291
1292 case DW_ATE_signed_char:
1293 if (ast->getLangOpts().CharIsSigned && type_name &&
1294 streq(type_name, "char")) {
1295 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1296 return CompilerType(ast, ast->CharTy);
1297 }
1298 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1299 return CompilerType(ast, ast->SignedCharTy);
1300 break;
1301
1302 case DW_ATE_unsigned:
1303 if (type_name) {
1304 if (streq(type_name, "wchar_t")) {
1305 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1306 if (!(getTargetInfo() &&
1307 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1308 return CompilerType(ast, ast->WCharTy);
1309 }
1310 }
1311 if (strstr(type_name, "long long")) {
1312 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1313 return CompilerType(ast, ast->UnsignedLongLongTy);
1314 } else if (strstr(type_name, "long")) {
1315 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1316 return CompilerType(ast, ast->UnsignedLongTy);
1317 } else if (strstr(type_name, "short")) {
1318 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1319 return CompilerType(ast, ast->UnsignedShortTy);
1320 } else if (strstr(type_name, "char")) {
1321 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1322 return CompilerType(ast, ast->UnsignedCharTy);
1323 } else if (strstr(type_name, "int")) {
1324 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1325 return CompilerType(ast, ast->UnsignedIntTy);
1326 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1327 return CompilerType(ast, ast->UnsignedInt128Ty);
1328 }
1329 }
1330 // We weren't able to match up a type name, just search by size
1331 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1332 return CompilerType(ast, ast->UnsignedCharTy);
1333 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1334 return CompilerType(ast, ast->UnsignedShortTy);
1335 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1336 return CompilerType(ast, ast->UnsignedIntTy);
1337 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1338 return CompilerType(ast, ast->UnsignedLongTy);
1339 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1340 return CompilerType(ast, ast->UnsignedLongLongTy);
1341 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1342 return CompilerType(ast, ast->UnsignedInt128Ty);
1343 break;
1344
1345 case DW_ATE_unsigned_char:
1346 if (!ast->getLangOpts().CharIsSigned && type_name &&
1347 streq(type_name, "char")) {
1348 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1349 return CompilerType(ast, ast->CharTy);
1350 }
1351 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1352 return CompilerType(ast, ast->UnsignedCharTy);
1353 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1354 return CompilerType(ast, ast->UnsignedShortTy);
1355 break;
1356
1357 case DW_ATE_imaginary_float:
1358 break;
1359
1360 case DW_ATE_UTF:
1361 if (type_name) {
1362 if (streq(type_name, "char16_t")) {
1363 return CompilerType(ast, ast->Char16Ty);
1364 } else if (streq(type_name, "char32_t")) {
1365 return CompilerType(ast, ast->Char32Ty);
1366 }
1367 }
1368 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001369 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001370 }
1371 // This assert should fire for anything that we don't catch above so we know
1372 // to fix any issues we run into.
1373 if (type_name) {
1374 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1375 "DW_TAG_base_type '%s' encoded with "
1376 "DW_ATE = 0x%x, bit_size = %u\n",
1377 type_name, dw_ate, bit_size);
1378 } else {
1379 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1380 "DW_TAG_base_type encoded with "
1381 "DW_ATE = 0x%x, bit_size = %u\n",
1382 dw_ate, bit_size);
1383 }
1384 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001385}
1386
Kate Stoneb9c1b512016-09-06 20:57:50 +00001387CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) {
1388 if (ast)
1389 return CompilerType(ast, ast->UnknownAnyTy);
1390 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001391}
1392
Kate Stoneb9c1b512016-09-06 20:57:50 +00001393CompilerType ClangASTContext::GetCStringType(bool is_const) {
1394 ASTContext *ast = getASTContext();
1395 QualType char_type(ast->CharTy);
1396
1397 if (is_const)
1398 char_type.addConst();
1399
1400 return CompilerType(ast, ast->getPointerType(char_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001401}
1402
Zachary Turner115209e2018-11-05 19:25:39 +00001403clang::DeclContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001404ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1405 return ast->getTranslationUnitDecl();
Sean Callanan09ab4b72011-11-30 22:11:59 +00001406}
1407
Kate Stoneb9c1b512016-09-06 20:57:50 +00001408clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1409 clang::Decl *source_decl) {
1410 FileSystemOptions file_system_options;
1411 FileManager file_manager(file_system_options);
1412 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1413
1414 return importer.Import(source_decl);
Greg Clayton526e5af2010-11-13 03:52:47 +00001415}
1416
Kate Stoneb9c1b512016-09-06 20:57:50 +00001417bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1418 bool ignore_qualifiers) {
1419 ClangASTContext *ast =
1420 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1421 if (!ast || ast != type2.GetTypeSystem())
1422 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001423
Kate Stoneb9c1b512016-09-06 20:57:50 +00001424 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1425 return true;
Greg Clayton55995eb2012-04-06 17:38:55 +00001426
Kate Stoneb9c1b512016-09-06 20:57:50 +00001427 QualType type1_qual = ClangUtil::GetQualType(type1);
1428 QualType type2_qual = ClangUtil::GetQualType(type2);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001429
Kate Stoneb9c1b512016-09-06 20:57:50 +00001430 if (ignore_qualifiers) {
1431 type1_qual = type1_qual.getUnqualifiedType();
1432 type2_qual = type2_qual.getUnqualifiedType();
1433 }
1434
1435 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001436}
1437
Kate Stoneb9c1b512016-09-06 20:57:50 +00001438CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1439 if (clang::ObjCInterfaceDecl *interface_decl =
1440 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1441 return GetTypeForDecl(interface_decl);
1442 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1443 return GetTypeForDecl(tag_decl);
1444 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001445}
1446
Kate Stoneb9c1b512016-09-06 20:57:50 +00001447CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001448 // No need to call the getASTContext() accessor (which can create the AST if
1449 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001450 // AST if our AST didn't already exist...
1451 ASTContext *ast = &decl->getASTContext();
1452 if (ast)
1453 return CompilerType(ast, ast->getTagDeclType(decl));
1454 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001455}
1456
Kate Stoneb9c1b512016-09-06 20:57:50 +00001457CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001458 // No need to call the getASTContext() accessor (which can create the AST if
1459 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001460 // AST if our AST didn't already exist...
1461 ASTContext *ast = &decl->getASTContext();
1462 if (ast)
1463 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1464 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001465}
1466
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001467#pragma mark Structure, Unions, Classes
1468
Kate Stoneb9c1b512016-09-06 20:57:50 +00001469CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx,
1470 AccessType access_type,
1471 const char *name, int kind,
1472 LanguageType language,
1473 ClangASTMetadata *metadata) {
1474 ASTContext *ast = getASTContext();
1475 assert(ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001476
Kate Stoneb9c1b512016-09-06 20:57:50 +00001477 if (decl_ctx == nullptr)
1478 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton9e409562010-07-28 02:04:09 +00001479
Kate Stoneb9c1b512016-09-06 20:57:50 +00001480 if (language == eLanguageTypeObjC ||
1481 language == eLanguageTypeObjC_plus_plus) {
1482 bool isForwardDecl = true;
1483 bool isInternal = false;
1484 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1485 }
Greg Clayton9e409562010-07-28 02:04:09 +00001486
Kate Stoneb9c1b512016-09-06 20:57:50 +00001487 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
Adrian Prantl05097242018-04-30 16:49:04 +00001488 // we will need to update this code. I was told to currently always use the
1489 // CXXRecordDecl class since we often don't know from debug information if
1490 // something is struct or a class, so we default to always use the more
Kate Stoneb9c1b512016-09-06 20:57:50 +00001491 // complete definition just in case.
Greg Claytonc4ffd662013-03-08 01:37:30 +00001492
Kate Stoneb9c1b512016-09-06 20:57:50 +00001493 bool is_anonymous = (!name) || (!name[0]);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001494
Kate Stoneb9c1b512016-09-06 20:57:50 +00001495 CXXRecordDecl *decl = CXXRecordDecl::Create(
1496 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1497 SourceLocation(), is_anonymous ? nullptr : &ast->Idents.get(name));
1498
1499 if (is_anonymous)
1500 decl->setAnonymousStructOrUnion(true);
1501
1502 if (decl) {
1503 if (metadata)
1504 SetMetadata(ast, decl, *metadata);
1505
1506 if (access_type != eAccessNone)
1507 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1508
1509 if (decl_ctx)
1510 decl_ctx->addDecl(decl);
1511
1512 return CompilerType(ast, ast->getTagDeclType(decl));
1513 }
1514 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001515}
1516
Sean Callanan09e91ac2017-05-11 22:08:05 +00001517namespace {
1518 bool IsValueParam(const clang::TemplateArgument &argument) {
1519 return argument.getKind() == TemplateArgument::Integral;
1520 }
1521}
1522
Kate Stoneb9c1b512016-09-06 20:57:50 +00001523static TemplateParameterList *CreateTemplateParameterList(
1524 ASTContext *ast,
1525 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1526 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1527 const bool parameter_pack = false;
1528 const bool is_typename = false;
1529 const unsigned depth = 0;
Sean Callanan09e91ac2017-05-11 22:08:05 +00001530 const size_t num_template_params = template_param_infos.args.size();
1531 DeclContext *const decl_context =
1532 ast->getTranslationUnitDecl(); // Is this the right decl context?,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001533 for (size_t i = 0; i < num_template_params; ++i) {
1534 const char *name = template_param_infos.names[i];
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001535
Kate Stoneb9c1b512016-09-06 20:57:50 +00001536 IdentifierInfo *identifier_info = nullptr;
1537 if (name && name[0])
1538 identifier_info = &ast->Idents.get(name);
Sean Callanan09e91ac2017-05-11 22:08:05 +00001539 if (IsValueParam(template_param_infos.args[i])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001540 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001541 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001542 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1543 template_param_infos.args[i].getIntegralType(), parameter_pack,
1544 nullptr));
1545
1546 } else {
1547 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001548 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001549 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1550 is_typename, parameter_pack));
1551 }
1552 }
Eugene Zemtsova9d928c2017-09-29 03:15:08 +00001553
Sean Callanan09e91ac2017-05-11 22:08:05 +00001554 if (template_param_infos.packed_args &&
1555 template_param_infos.packed_args->args.size()) {
1556 IdentifierInfo *identifier_info = nullptr;
1557 if (template_param_infos.pack_name && template_param_infos.pack_name[0])
1558 identifier_info = &ast->Idents.get(template_param_infos.pack_name);
1559 const bool parameter_pack_true = true;
1560 if (IsValueParam(template_param_infos.packed_args->args[0])) {
1561 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1562 *ast, decl_context,
1563 SourceLocation(), SourceLocation(), depth, num_template_params,
1564 identifier_info,
1565 template_param_infos.packed_args->args[0].getIntegralType(),
1566 parameter_pack_true, nullptr));
1567 } else {
1568 template_param_decls.push_back(TemplateTypeParmDecl::Create(
1569 *ast, decl_context,
1570 SourceLocation(), SourceLocation(), depth, num_template_params,
1571 identifier_info,
1572 is_typename, parameter_pack_true));
1573 }
1574 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001575 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1576 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1577 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1578 SourceLocation(), requires_clause);
1579 return template_param_list;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001580}
1581
Kate Stoneb9c1b512016-09-06 20:57:50 +00001582clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1583 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1584 const char *name, const TemplateParameterInfos &template_param_infos) {
Adrian Prantld8f460e2018-05-02 16:55:16 +00001585 // /// Create a function template node.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001586 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001587
Kate Stoneb9c1b512016-09-06 20:57:50 +00001588 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001589
Kate Stoneb9c1b512016-09-06 20:57:50 +00001590 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1591 ast, template_param_infos, template_param_decls);
1592 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1593 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1594 template_param_list, func_decl);
1595
1596 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1597 i < template_param_decl_count; ++i) {
1598 // TODO: verify which decl context we should put template_param_decls into..
1599 template_param_decls[i]->setDeclContext(func_decl);
1600 }
1601
1602 return func_tmpl_decl;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001603}
1604
Kate Stoneb9c1b512016-09-06 20:57:50 +00001605void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1606 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1607 const TemplateParameterInfos &infos) {
1608 TemplateArgumentList template_args(TemplateArgumentList::OnStack, infos.args);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001609
Kate Stoneb9c1b512016-09-06 20:57:50 +00001610 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, &template_args,
1611 nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001612}
1613
Kate Stoneb9c1b512016-09-06 20:57:50 +00001614ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1615 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1616 int kind, const TemplateParameterInfos &template_param_infos) {
1617 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001618
Kate Stoneb9c1b512016-09-06 20:57:50 +00001619 ClassTemplateDecl *class_template_decl = nullptr;
1620 if (decl_ctx == nullptr)
1621 decl_ctx = ast->getTranslationUnitDecl();
Greg Claytonf0705c82011-10-22 03:33:13 +00001622
Kate Stoneb9c1b512016-09-06 20:57:50 +00001623 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1624 DeclarationName decl_name(&identifier_info);
Greg Claytonf0705c82011-10-22 03:33:13 +00001625
Kate Stoneb9c1b512016-09-06 20:57:50 +00001626 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Greg Claytonf0705c82011-10-22 03:33:13 +00001627
Kate Stoneb9c1b512016-09-06 20:57:50 +00001628 for (NamedDecl *decl : result) {
1629 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001630 if (class_template_decl)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001631 return class_template_decl;
1632 }
1633
1634 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1635
1636 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1637 ast, template_param_infos, template_param_decls);
1638
1639 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1640 *ast, (TagDecl::TagKind)kind,
1641 decl_ctx, // What decl context do we use here? TU? The actual decl
1642 // context?
1643 SourceLocation(), SourceLocation(), &identifier_info);
1644
1645 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1646 i < template_param_decl_count; ++i) {
1647 template_param_decls[i]->setDeclContext(template_cxx_decl);
1648 }
1649
1650 // With templated classes, we say that a class is templated with
1651 // specializations, but that the bare class has no functions.
1652 // template_cxx_decl->startDefinition();
1653 // template_cxx_decl->completeDefinition();
1654
1655 class_template_decl = ClassTemplateDecl::Create(
1656 *ast,
1657 decl_ctx, // What decl context do we use here? TU? The actual decl
1658 // context?
Pavel Labath4294de32017-01-12 10:44:16 +00001659 SourceLocation(), decl_name, template_param_list, template_cxx_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001660
1661 if (class_template_decl) {
1662 if (access_type != eAccessNone)
1663 class_template_decl->setAccess(
1664 ConvertAccessTypeToAccessSpecifier(access_type));
1665
1666 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1667 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1668
1669 decl_ctx->addDecl(class_template_decl);
1670
Sean Callanan5e9e1992011-10-26 01:06:27 +00001671#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001672 VerifyDecl(class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001673#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001674 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001675
Kate Stoneb9c1b512016-09-06 20:57:50 +00001676 return class_template_decl;
Greg Claytonf0705c82011-10-22 03:33:13 +00001677}
1678
Frederic Rissf4e7e522018-04-02 16:18:32 +00001679TemplateTemplateParmDecl *
1680ClangASTContext::CreateTemplateTemplateParmDecl(const char *template_name) {
1681 ASTContext *ast = getASTContext();
1682
1683 auto *decl_ctx = ast->getTranslationUnitDecl();
1684
1685 IdentifierInfo &identifier_info = ast->Idents.get(template_name);
1686 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1687
1688 ClangASTContext::TemplateParameterInfos template_param_infos;
1689 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1690 ast, template_param_infos, template_param_decls);
1691
1692 // LLDB needs to create those decls only to be able to display a
Adrian Prantl05097242018-04-30 16:49:04 +00001693 // type that includes a template template argument. Only the name matters for
1694 // this purpose, so we use dummy values for the other characterisitcs of the
1695 // type.
Frederic Rissf4e7e522018-04-02 16:18:32 +00001696 return TemplateTemplateParmDecl::Create(
1697 *ast, decl_ctx, SourceLocation(),
1698 /*Depth*/ 0, /*Position*/ 0,
1699 /*IsParameterPack*/ false, &identifier_info, template_param_list);
1700}
1701
Greg Claytonf0705c82011-10-22 03:33:13 +00001702ClassTemplateSpecializationDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001703ClangASTContext::CreateClassTemplateSpecializationDecl(
1704 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1705 const TemplateParameterInfos &template_param_infos) {
1706 ASTContext *ast = getASTContext();
Sean Callanan09e91ac2017-05-11 22:08:05 +00001707 llvm::SmallVector<clang::TemplateArgument, 2> args(
1708 template_param_infos.args.size() +
1709 (template_param_infos.packed_args ? 1 : 0));
1710 std::copy(template_param_infos.args.begin(), template_param_infos.args.end(),
1711 args.begin());
1712 if (template_param_infos.packed_args) {
1713 args[args.size() - 1] = TemplateArgument::CreatePackCopy(
1714 *ast, template_param_infos.packed_args->args);
1715 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001716 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1717 ClassTemplateSpecializationDecl::Create(
1718 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
Sean Callanan09e91ac2017-05-11 22:08:05 +00001719 SourceLocation(), class_template_decl, args,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001720 nullptr);
1721
1722 class_template_specialization_decl->setSpecializationKind(
1723 TSK_ExplicitSpecialization);
1724
1725 return class_template_specialization_decl;
1726}
1727
1728CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1729 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1730 if (class_template_specialization_decl) {
Greg Claytonf0705c82011-10-22 03:33:13 +00001731 ASTContext *ast = getASTContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001732 if (ast)
1733 return CompilerType(
1734 ast, ast->getTagDeclType(class_template_specialization_decl));
1735 }
1736 return CompilerType();
Greg Claytonf0705c82011-10-22 03:33:13 +00001737}
1738
Kate Stoneb9c1b512016-09-06 20:57:50 +00001739static inline bool check_op_param(bool is_method,
1740 clang::OverloadedOperatorKind op_kind,
1741 bool unary, bool binary,
1742 uint32_t num_params) {
1743 // Special-case call since it can take any number of operands
1744 if (op_kind == OO_Call)
1745 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001746
Kate Stoneb9c1b512016-09-06 20:57:50 +00001747 // The parameter count doesn't include "this"
1748 if (is_method)
1749 ++num_params;
1750 if (num_params == 1)
1751 return unary;
1752 if (num_params == 2)
1753 return binary;
1754 else
Greg Clayton090d0982011-06-19 03:43:27 +00001755 return false;
1756}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001757
Kate Stoneb9c1b512016-09-06 20:57:50 +00001758bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1759 bool is_method, clang::OverloadedOperatorKind op_kind,
1760 uint32_t num_params) {
1761 switch (op_kind) {
1762 default:
1763 break;
1764 // C++ standard allows any number of arguments to new/delete
1765 case OO_New:
1766 case OO_Array_New:
1767 case OO_Delete:
1768 case OO_Array_Delete:
1769 return true;
1770 }
Pavel Labath1ac2b202016-08-15 14:32:32 +00001771
Kate Stoneb9c1b512016-09-06 20:57:50 +00001772#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1773 case OO_##Name: \
1774 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1775 switch (op_kind) {
Greg Clayton090d0982011-06-19 03:43:27 +00001776#include "clang/Basic/OperatorKinds.def"
Kate Stoneb9c1b512016-09-06 20:57:50 +00001777 default:
1778 break;
1779 }
1780 return false;
Greg Clayton090d0982011-06-19 03:43:27 +00001781}
1782
Greg Clayton57ee3062013-07-11 22:46:58 +00001783clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +00001784ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1785 clang::AccessSpecifier rhs) {
1786 // Make the access equal to the stricter of the field and the nested field's
1787 // access
1788 if (lhs == AS_none || rhs == AS_none)
1789 return AS_none;
1790 if (lhs == AS_private || rhs == AS_private)
1791 return AS_private;
1792 if (lhs == AS_protected || rhs == AS_protected)
1793 return AS_protected;
1794 return AS_public;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001795}
1796
Kate Stoneb9c1b512016-09-06 20:57:50 +00001797bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1798 uint32_t &bitfield_bit_size) {
1799 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001800}
1801
Kate Stoneb9c1b512016-09-06 20:57:50 +00001802bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field,
1803 uint32_t &bitfield_bit_size) {
1804 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001805 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001806
Kate Stoneb9c1b512016-09-06 20:57:50 +00001807 if (field->isBitField()) {
1808 Expr *bit_width_expr = field->getBitWidth();
1809 if (bit_width_expr) {
1810 llvm::APSInt bit_width_apsint;
1811 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1812 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001813 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001814 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001815 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001816 }
1817 return false;
1818}
1819
1820bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1821 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001822 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001823
1824 if (!record_decl->field_empty())
1825 return true;
1826
1827 // No fields, lets check this is a CXX record and check the base classes
1828 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1829 if (cxx_record_decl) {
1830 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1831 for (base_class = cxx_record_decl->bases_begin(),
1832 base_class_end = cxx_record_decl->bases_end();
1833 base_class != base_class_end; ++base_class) {
1834 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1835 base_class->getType()->getAs<RecordType>()->getDecl());
1836 if (RecordHasFields(base_class_decl))
1837 return true;
1838 }
1839 }
1840 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001841}
1842
Adrian Prantl4e8be2c2018-06-13 16:21:24 +00001843#pragma mark Objective-C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001844
Kate Stoneb9c1b512016-09-06 20:57:50 +00001845CompilerType ClangASTContext::CreateObjCClass(const char *name,
1846 DeclContext *decl_ctx,
1847 bool isForwardDecl,
1848 bool isInternal,
1849 ClangASTMetadata *metadata) {
1850 ASTContext *ast = getASTContext();
1851 assert(ast != nullptr);
1852 assert(name && name[0]);
1853 if (decl_ctx == nullptr)
1854 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001855
Kate Stoneb9c1b512016-09-06 20:57:50 +00001856 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1857 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1858 nullptr, SourceLocation(),
1859 /*isForwardDecl,*/
1860 isInternal);
1861
1862 if (decl && metadata)
1863 SetMetadata(ast, decl, *metadata);
1864
1865 return CompilerType(ast, ast->getObjCInterfaceType(decl));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001866}
1867
Kate Stoneb9c1b512016-09-06 20:57:50 +00001868static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
1869 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) ==
1870 false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001871}
1872
Greg Clayton57ee3062013-07-11 22:46:58 +00001873uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001874ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1875 bool omit_empty_base_classes) {
1876 uint32_t num_bases = 0;
1877 if (cxx_record_decl) {
1878 if (omit_empty_base_classes) {
1879 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1880 for (base_class = cxx_record_decl->bases_begin(),
1881 base_class_end = cxx_record_decl->bases_end();
1882 base_class != base_class_end; ++base_class) {
1883 // Skip empty base classes
1884 if (omit_empty_base_classes) {
1885 if (BaseSpecifierIsEmpty(base_class))
1886 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001887 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001888 ++num_bases;
1889 }
1890 } else
1891 num_bases = cxx_record_decl->getNumBases();
1892 }
1893 return num_bases;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001894}
1895
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001896#pragma mark Namespace Declarations
1897
1898NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001899ClangASTContext::GetUniqueNamespaceDeclaration(const char *name,
1900 DeclContext *decl_ctx) {
1901 NamespaceDecl *namespace_decl = nullptr;
1902 ASTContext *ast = getASTContext();
1903 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1904 if (decl_ctx == nullptr)
1905 decl_ctx = translation_unit_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00001906
Kate Stoneb9c1b512016-09-06 20:57:50 +00001907 if (name) {
1908 IdentifierInfo &identifier_info = ast->Idents.get(name);
1909 DeclarationName decl_name(&identifier_info);
1910 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1911 for (NamedDecl *decl : result) {
1912 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1913 if (namespace_decl)
1914 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001915 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001916
1917 namespace_decl =
1918 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1919 SourceLocation(), &identifier_info, nullptr);
1920
1921 decl_ctx->addDecl(namespace_decl);
1922 } else {
1923 if (decl_ctx == translation_unit_decl) {
1924 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1925 if (namespace_decl)
1926 return namespace_decl;
1927
1928 namespace_decl =
1929 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1930 SourceLocation(), nullptr, nullptr);
1931 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1932 translation_unit_decl->addDecl(namespace_decl);
1933 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
1934 } else {
1935 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1936 if (parent_namespace_decl) {
1937 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1938 if (namespace_decl)
1939 return namespace_decl;
1940 namespace_decl =
1941 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1942 SourceLocation(), nullptr, nullptr);
1943 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1944 parent_namespace_decl->addDecl(namespace_decl);
1945 assert(namespace_decl ==
1946 parent_namespace_decl->getAnonymousNamespace());
1947 } else {
1948 // BAD!!!
1949 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001950 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001951 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001952#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001953 VerifyDecl(namespace_decl);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001954#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001955 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001956}
1957
Kate Stoneb9c1b512016-09-06 20:57:50 +00001958NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1959 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx) {
1960 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1961 if (ast_ctx == nullptr)
1962 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +00001963
Kate Stoneb9c1b512016-09-06 20:57:50 +00001964 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx);
Siva Chandra03ff5c82016-02-05 19:10:04 +00001965}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001966
Paul Hermand628cbb2015-09-15 23:44:17 +00001967clang::BlockDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001968ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1969 if (ctx != nullptr) {
1970 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1971 clang::SourceLocation());
1972 ctx->addDecl(decl);
1973 return decl;
1974 }
1975 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001976}
1977
Kate Stoneb9c1b512016-09-06 20:57:50 +00001978clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1979 clang::DeclContext *right,
1980 clang::DeclContext *root) {
1981 if (root == nullptr)
Paul Hermanea188fc2015-09-16 18:48:30 +00001982 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001983
1984 std::set<clang::DeclContext *> path_left;
1985 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1986 path_left.insert(d);
1987
1988 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1989 if (path_left.find(d) != path_left.end())
1990 return d;
1991
1992 return nullptr;
Paul Hermanea188fc2015-09-16 18:48:30 +00001993}
1994
Kate Stoneb9c1b512016-09-06 20:57:50 +00001995clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
1996 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
1997 if (decl_ctx != nullptr && ns_decl != nullptr) {
1998 clang::TranslationUnitDecl *translation_unit =
1999 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
2000 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
2001 *getASTContext(), decl_ctx, clang::SourceLocation(),
2002 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
2003 clang::SourceLocation(), ns_decl,
2004 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
2005 decl_ctx->addDecl(using_decl);
2006 return using_decl;
2007 }
2008 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002009}
2010
2011clang::UsingDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00002012ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
2013 clang::NamedDecl *target) {
2014 if (current_decl_ctx != nullptr && target != nullptr) {
2015 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
2016 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
2017 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
2018 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
2019 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
2020 target);
2021 using_decl->addShadowDecl(shadow_decl);
2022 current_decl_ctx->addDecl(using_decl);
2023 return using_decl;
2024 }
2025 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002026}
2027
Kate Stoneb9c1b512016-09-06 20:57:50 +00002028clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
2029 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
2030 if (decl_context != nullptr) {
2031 clang::VarDecl *var_decl = clang::VarDecl::Create(
2032 *getASTContext(), decl_context, clang::SourceLocation(),
2033 clang::SourceLocation(),
2034 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
2035 nullptr, clang::SC_None);
2036 var_decl->setAccess(clang::AS_public);
2037 decl_context->addDecl(var_decl);
2038 return var_decl;
2039 }
2040 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002041}
2042
Zachary Turner9d8a97e2016-04-01 23:20:35 +00002043lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00002044ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
2045 lldb::BasicType basic_type) {
2046 switch (basic_type) {
2047 case eBasicTypeVoid:
2048 return ast->VoidTy.getAsOpaquePtr();
2049 case eBasicTypeChar:
2050 return ast->CharTy.getAsOpaquePtr();
2051 case eBasicTypeSignedChar:
2052 return ast->SignedCharTy.getAsOpaquePtr();
2053 case eBasicTypeUnsignedChar:
2054 return ast->UnsignedCharTy.getAsOpaquePtr();
2055 case eBasicTypeWChar:
2056 return ast->getWCharType().getAsOpaquePtr();
2057 case eBasicTypeSignedWChar:
2058 return ast->getSignedWCharType().getAsOpaquePtr();
2059 case eBasicTypeUnsignedWChar:
2060 return ast->getUnsignedWCharType().getAsOpaquePtr();
2061 case eBasicTypeChar16:
2062 return ast->Char16Ty.getAsOpaquePtr();
2063 case eBasicTypeChar32:
2064 return ast->Char32Ty.getAsOpaquePtr();
2065 case eBasicTypeShort:
2066 return ast->ShortTy.getAsOpaquePtr();
2067 case eBasicTypeUnsignedShort:
2068 return ast->UnsignedShortTy.getAsOpaquePtr();
2069 case eBasicTypeInt:
2070 return ast->IntTy.getAsOpaquePtr();
2071 case eBasicTypeUnsignedInt:
2072 return ast->UnsignedIntTy.getAsOpaquePtr();
2073 case eBasicTypeLong:
2074 return ast->LongTy.getAsOpaquePtr();
2075 case eBasicTypeUnsignedLong:
2076 return ast->UnsignedLongTy.getAsOpaquePtr();
2077 case eBasicTypeLongLong:
2078 return ast->LongLongTy.getAsOpaquePtr();
2079 case eBasicTypeUnsignedLongLong:
2080 return ast->UnsignedLongLongTy.getAsOpaquePtr();
2081 case eBasicTypeInt128:
2082 return ast->Int128Ty.getAsOpaquePtr();
2083 case eBasicTypeUnsignedInt128:
2084 return ast->UnsignedInt128Ty.getAsOpaquePtr();
2085 case eBasicTypeBool:
2086 return ast->BoolTy.getAsOpaquePtr();
2087 case eBasicTypeHalf:
2088 return ast->HalfTy.getAsOpaquePtr();
2089 case eBasicTypeFloat:
2090 return ast->FloatTy.getAsOpaquePtr();
2091 case eBasicTypeDouble:
2092 return ast->DoubleTy.getAsOpaquePtr();
2093 case eBasicTypeLongDouble:
2094 return ast->LongDoubleTy.getAsOpaquePtr();
2095 case eBasicTypeFloatComplex:
2096 return ast->FloatComplexTy.getAsOpaquePtr();
2097 case eBasicTypeDoubleComplex:
2098 return ast->DoubleComplexTy.getAsOpaquePtr();
2099 case eBasicTypeLongDoubleComplex:
2100 return ast->LongDoubleComplexTy.getAsOpaquePtr();
2101 case eBasicTypeObjCID:
2102 return ast->getObjCIdType().getAsOpaquePtr();
2103 case eBasicTypeObjCClass:
2104 return ast->getObjCClassType().getAsOpaquePtr();
2105 case eBasicTypeObjCSel:
2106 return ast->getObjCSelType().getAsOpaquePtr();
2107 case eBasicTypeNullPtr:
2108 return ast->NullPtrTy.getAsOpaquePtr();
2109 default:
2110 return nullptr;
2111 }
Zachary Turner9d8a97e2016-04-01 23:20:35 +00002112}
2113
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002114#pragma mark Function Types
2115
Pavel Labath1ac2b202016-08-15 14:32:32 +00002116clang::DeclarationName
Kate Stoneb9c1b512016-09-06 20:57:50 +00002117ClangASTContext::GetDeclarationName(const char *name,
2118 const CompilerType &function_clang_type) {
2119 if (!name || !name[0])
2120 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002121
Kate Stoneb9c1b512016-09-06 20:57:50 +00002122 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
2123 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
2124 return DeclarationName(&getASTContext()->Idents.get(
2125 name)); // Not operator, but a regular function.
Pavel Labath1ac2b202016-08-15 14:32:32 +00002126
Adrian Prantl05097242018-04-30 16:49:04 +00002127 // Check the number of operator parameters. Sometimes we have seen bad DWARF
2128 // that doesn't correctly describe operators and if we try to create a method
2129 // and add it to the class, clang will assert and crash, so we need to make
2130 // sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002131 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
2132 const clang::FunctionProtoType *function_type =
2133 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
2134 if (function_type == nullptr)
2135 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002136
Kate Stoneb9c1b512016-09-06 20:57:50 +00002137 const bool is_method = false;
2138 const unsigned int num_params = function_type->getNumParams();
2139 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
2140 is_method, op_kind, num_params))
2141 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002142
Kate Stoneb9c1b512016-09-06 20:57:50 +00002143 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
Pavel Labath1ac2b202016-08-15 14:32:32 +00002144}
2145
Kate Stoneb9c1b512016-09-06 20:57:50 +00002146FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
2147 DeclContext *decl_ctx, const char *name,
2148 const CompilerType &function_clang_type, int storage, bool is_inline) {
2149 FunctionDecl *func_decl = nullptr;
2150 ASTContext *ast = getASTContext();
2151 if (decl_ctx == nullptr)
2152 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002153
Kate Stoneb9c1b512016-09-06 20:57:50 +00002154 const bool hasWrittenPrototype = true;
2155 const bool isConstexprSpecified = false;
Greg Clayton0d551042013-06-28 21:08:47 +00002156
Kate Stoneb9c1b512016-09-06 20:57:50 +00002157 clang::DeclarationName declarationName =
2158 GetDeclarationName(name, function_clang_type);
2159 func_decl = FunctionDecl::Create(
2160 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
2161 ClangUtil::GetQualType(function_clang_type), nullptr,
2162 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
2163 isConstexprSpecified);
2164 if (func_decl)
2165 decl_ctx->addDecl(func_decl);
2166
Sean Callanan5e9e1992011-10-26 01:06:27 +00002167#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00002168 VerifyDecl(func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002169#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002170
2171 return func_decl;
2172}
2173
2174CompilerType ClangASTContext::CreateFunctionType(
2175 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002176 unsigned num_args, bool is_variadic, unsigned type_quals,
2177 clang::CallingConv cc) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002178 if (ast == nullptr)
2179 return CompilerType(); // invalid AST
2180
2181 if (!result_type || !ClangUtil::IsClangType(result_type))
2182 return CompilerType(); // invalid return type
2183
2184 std::vector<QualType> qual_type_args;
2185 if (num_args > 0 && args == nullptr)
2186 return CompilerType(); // invalid argument array passed in
2187
2188 // Verify that all arguments are valid and the right type
2189 for (unsigned i = 0; i < num_args; ++i) {
2190 if (args[i]) {
2191 // Make sure we have a clang type in args[i] and not a type from another
2192 // language whose name might match
2193 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2194 lldbassert(is_clang_type);
2195 if (is_clang_type)
2196 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2197 else
2198 return CompilerType(); // invalid argument type (must be a clang type)
2199 } else
2200 return CompilerType(); // invalid argument type (empty)
2201 }
2202
2203 // TODO: Detect calling convention in DWARF?
2204 FunctionProtoType::ExtProtoInfo proto_info;
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002205 proto_info.ExtInfo = cc;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002206 proto_info.Variadic = is_variadic;
2207 proto_info.ExceptionSpec = EST_None;
2208 proto_info.TypeQuals = type_quals;
2209 proto_info.RefQualifier = RQ_None;
2210
2211 return CompilerType(ast,
2212 ast->getFunctionType(ClangUtil::GetQualType(result_type),
2213 qual_type_args, proto_info));
2214}
2215
2216ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
2217 const char *name, const CompilerType &param_type, int storage) {
2218 ASTContext *ast = getASTContext();
2219 assert(ast != nullptr);
2220 return ParmVarDecl::Create(*ast, ast->getTranslationUnitDecl(),
2221 SourceLocation(), SourceLocation(),
2222 name && name[0] ? &ast->Idents.get(name) : nullptr,
2223 ClangUtil::GetQualType(param_type), nullptr,
2224 (clang::StorageClass)storage, nullptr);
2225}
2226
2227void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2228 ParmVarDecl **params,
2229 unsigned num_params) {
2230 if (function_decl)
2231 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002232}
2233
Greg Claytona1e5dc82015-08-11 22:53:00 +00002234CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002235ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
2236 QualType block_type = m_ast_ap->getBlockPointerType(
2237 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
Greg Claytonceeb5212016-05-26 22:33:25 +00002238
Kate Stoneb9c1b512016-09-06 20:57:50 +00002239 return CompilerType(this, block_type.getAsOpaquePtr());
Sean Callananc530ba92016-05-02 21:15:31 +00002240}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002241
2242#pragma mark Array Types
2243
Kate Stoneb9c1b512016-09-06 20:57:50 +00002244CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2245 size_t element_count,
2246 bool is_vector) {
2247 if (element_type.IsValid()) {
2248 ASTContext *ast = getASTContext();
2249 assert(ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002250
Kate Stoneb9c1b512016-09-06 20:57:50 +00002251 if (is_vector) {
2252 return CompilerType(
2253 ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2254 element_count));
2255 } else {
2256
2257 llvm::APInt ap_element_count(64, element_count);
2258 if (element_count == 0) {
2259 return CompilerType(ast, ast->getIncompleteArrayType(
2260 ClangUtil::GetQualType(element_type),
2261 clang::ArrayType::Normal, 0));
2262 } else {
2263 return CompilerType(
2264 ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2265 ap_element_count,
2266 clang::ArrayType::Normal, 0));
2267 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002268 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002269 }
2270 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002271}
2272
Kate Stoneb9c1b512016-09-06 20:57:50 +00002273CompilerType ClangASTContext::CreateStructForIdentifier(
2274 const ConstString &type_name,
2275 const std::initializer_list<std::pair<const char *, CompilerType>>
2276 &type_fields,
2277 bool packed) {
2278 CompilerType type;
2279 if (!type_name.IsEmpty() &&
2280 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2281 .IsValid()) {
Pavel Labathf31c9d22017-01-05 13:18:42 +00002282 lldbassert(0 && "Trying to create a type for an existing name");
Enrico Granata76b08d52014-10-29 23:08:02 +00002283 return type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002284 }
2285
2286 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2287 clang::TTK_Struct, lldb::eLanguageTypeC);
2288 StartTagDeclarationDefinition(type);
2289 for (const auto &field : type_fields)
2290 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2291 0);
2292 if (packed)
2293 SetIsPacked(type);
2294 CompleteTagDeclarationDefinition(type);
2295 return type;
Enrico Granata76b08d52014-10-29 23:08:02 +00002296}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002297
Kate Stoneb9c1b512016-09-06 20:57:50 +00002298CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
2299 const ConstString &type_name,
2300 const std::initializer_list<std::pair<const char *, CompilerType>>
2301 &type_fields,
2302 bool packed) {
2303 CompilerType type;
2304 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2305 return type;
Sean Callananc530ba92016-05-02 21:15:31 +00002306
Kate Stoneb9c1b512016-09-06 20:57:50 +00002307 return CreateStructForIdentifier(type_name, type_fields, packed);
Sean Callananc530ba92016-05-02 21:15:31 +00002308}
2309
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002310#pragma mark Enumeration Types
2311
Greg Claytona1e5dc82015-08-11 22:53:00 +00002312CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002313ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2314 const Declaration &decl,
Tamas Berghammer59765832017-11-07 10:39:22 +00002315 const CompilerType &integer_clang_type,
2316 bool is_scoped) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002317 // TODO: Do something intelligent with the Declaration object passed in
2318 // like maybe filling in the SourceLocation with it...
2319 ASTContext *ast = getASTContext();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002320
Kate Stoneb9c1b512016-09-06 20:57:50 +00002321 // TODO: ask about these...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002322 // const bool IsFixed = false;
2323
2324 EnumDecl *enum_decl = EnumDecl::Create(
2325 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2326 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
Tamas Berghammercf6bf4c2017-11-07 13:43:55 +00002327 is_scoped, // IsScoped
2328 is_scoped, // IsScopedUsingClassTag
2329 false); // IsFixed
Kate Stoneb9c1b512016-09-06 20:57:50 +00002330
2331 if (enum_decl) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +00002332 if (decl_ctx)
2333 decl_ctx->addDecl(enum_decl);
2334
Kate Stoneb9c1b512016-09-06 20:57:50 +00002335 // TODO: check if we should be setting the promotion type too?
2336 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2337
2338 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2339
2340 return CompilerType(ast, ast->getTagDeclType(enum_decl));
2341 }
2342 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002343}
2344
Kate Stoneb9c1b512016-09-06 20:57:50 +00002345CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2346 size_t bit_size,
2347 bool is_signed) {
2348 if (ast) {
2349 if (is_signed) {
2350 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
2351 return CompilerType(ast, ast->SignedCharTy);
2352
2353 if (bit_size == ast->getTypeSize(ast->ShortTy))
2354 return CompilerType(ast, ast->ShortTy);
2355
2356 if (bit_size == ast->getTypeSize(ast->IntTy))
2357 return CompilerType(ast, ast->IntTy);
2358
2359 if (bit_size == ast->getTypeSize(ast->LongTy))
2360 return CompilerType(ast, ast->LongTy);
2361
2362 if (bit_size == ast->getTypeSize(ast->LongLongTy))
2363 return CompilerType(ast, ast->LongLongTy);
2364
2365 if (bit_size == ast->getTypeSize(ast->Int128Ty))
2366 return CompilerType(ast, ast->Int128Ty);
2367 } else {
2368 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
2369 return CompilerType(ast, ast->UnsignedCharTy);
2370
2371 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
2372 return CompilerType(ast, ast->UnsignedShortTy);
2373
2374 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
2375 return CompilerType(ast, ast->UnsignedIntTy);
2376
2377 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
2378 return CompilerType(ast, ast->UnsignedLongTy);
2379
2380 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
2381 return CompilerType(ast, ast->UnsignedLongLongTy);
2382
2383 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
2384 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002385 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002386 }
2387 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002388}
2389
Kate Stoneb9c1b512016-09-06 20:57:50 +00002390CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2391 bool is_signed) {
2392 if (ast)
2393 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2394 is_signed);
2395 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002396}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002397
Kate Stoneb9c1b512016-09-06 20:57:50 +00002398void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2399 if (decl_ctx) {
2400 DumpDeclContextHiearchy(decl_ctx->getParent());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002401
Kate Stoneb9c1b512016-09-06 20:57:50 +00002402 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2403 if (named_decl) {
2404 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2405 named_decl->getDeclName().getAsString().c_str());
2406 } else {
2407 printf("%20s\n", decl_ctx->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002408 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002409 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002410}
2411
Kate Stoneb9c1b512016-09-06 20:57:50 +00002412void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2413 if (decl == nullptr)
2414 return;
2415 DumpDeclContextHiearchy(decl->getDeclContext());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002416
Kate Stoneb9c1b512016-09-06 20:57:50 +00002417 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2418 if (record_decl) {
2419 printf("%20s: %s%s\n", decl->getDeclKindName(),
2420 record_decl->getDeclName().getAsString().c_str(),
2421 record_decl->isInjectedClassName() ? " (injected class name)" : "");
Greg Claytone6b36cd2015-12-08 01:02:08 +00002422
Kate Stoneb9c1b512016-09-06 20:57:50 +00002423 } else {
2424 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2425 if (named_decl) {
2426 printf("%20s: %s\n", decl->getDeclKindName(),
2427 named_decl->getDeclName().getAsString().c_str());
2428 } else {
2429 printf("%20s\n", decl->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002430 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002431 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002432}
2433
Kate Stoneb9c1b512016-09-06 20:57:50 +00002434bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2435 clang::Decl *rhs_decl) {
2436 if (lhs_decl && rhs_decl) {
2437 //----------------------------------------------------------------------
2438 // Make sure the decl kinds match first
2439 //----------------------------------------------------------------------
2440 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2441 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002442
Kate Stoneb9c1b512016-09-06 20:57:50 +00002443 if (lhs_decl_kind == rhs_decl_kind) {
2444 //------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +00002445 // Now check that the decl contexts kinds are all equivalent before we
2446 // have to check any names of the decl contexts...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002447 //------------------------------------------------------------------
2448 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2449 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2450 if (lhs_decl_ctx && rhs_decl_ctx) {
2451 while (1) {
2452 if (lhs_decl_ctx && rhs_decl_ctx) {
2453 const clang::Decl::Kind lhs_decl_ctx_kind =
2454 lhs_decl_ctx->getDeclKind();
2455 const clang::Decl::Kind rhs_decl_ctx_kind =
2456 rhs_decl_ctx->getDeclKind();
2457 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2458 lhs_decl_ctx = lhs_decl_ctx->getParent();
2459 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002460
Kate Stoneb9c1b512016-09-06 20:57:50 +00002461 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2462 break;
2463 } else
2464 return false;
2465 } else
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00002466 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002467 }
2468
2469 //--------------------------------------------------------------
2470 // Now make sure the name of the decls match
2471 //--------------------------------------------------------------
2472 clang::NamedDecl *lhs_named_decl =
2473 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2474 clang::NamedDecl *rhs_named_decl =
2475 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2476 if (lhs_named_decl && rhs_named_decl) {
2477 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2478 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2479 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2480 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2481 return false;
2482 } else
Greg Claytona2721472011-06-25 00:44:06 +00002483 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002484 } else
2485 return false;
Greg Claytona2721472011-06-25 00:44:06 +00002486
Kate Stoneb9c1b512016-09-06 20:57:50 +00002487 //--------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +00002488 // We know that the decl context kinds all match, so now we need to
2489 // make sure the names match as well
Kate Stoneb9c1b512016-09-06 20:57:50 +00002490 //--------------------------------------------------------------
2491 lhs_decl_ctx = lhs_decl->getDeclContext();
2492 rhs_decl_ctx = rhs_decl->getDeclContext();
2493 while (1) {
2494 switch (lhs_decl_ctx->getDeclKind()) {
2495 case clang::Decl::TranslationUnit:
2496 // We don't care about the translation unit names
2497 return true;
2498 default: {
2499 clang::NamedDecl *lhs_named_decl =
2500 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2501 clang::NamedDecl *rhs_named_decl =
2502 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2503 if (lhs_named_decl && rhs_named_decl) {
2504 clang::DeclarationName lhs_decl_name =
2505 lhs_named_decl->getDeclName();
2506 clang::DeclarationName rhs_decl_name =
2507 rhs_named_decl->getDeclName();
2508 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2509 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2510 return false;
2511 } else
2512 return false;
2513 } else
2514 return false;
2515 } break;
2516 }
2517 lhs_decl_ctx = lhs_decl_ctx->getParent();
2518 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytond8d4a572015-08-11 21:38:15 +00002519 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002520 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002521 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002522 }
2523 return false;
2524}
2525bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2526 clang::Decl *decl) {
2527 if (!decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002528 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002529
Kate Stoneb9c1b512016-09-06 20:57:50 +00002530 ExternalASTSource *ast_source = ast->getExternalSource();
Greg Claytond8d4a572015-08-11 21:38:15 +00002531
Kate Stoneb9c1b512016-09-06 20:57:50 +00002532 if (!ast_source)
Greg Claytond8d4a572015-08-11 21:38:15 +00002533 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002534
2535 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2536 if (tag_decl->isCompleteDefinition())
2537 return true;
2538
2539 if (!tag_decl->hasExternalLexicalStorage())
2540 return false;
2541
2542 ast_source->CompleteType(tag_decl);
2543
2544 return !tag_decl->getTypeForDecl()->isIncompleteType();
2545 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2546 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2547 if (objc_interface_decl->getDefinition())
2548 return true;
2549
2550 if (!objc_interface_decl->hasExternalLexicalStorage())
2551 return false;
2552
2553 ast_source->CompleteType(objc_interface_decl);
2554
2555 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2556 } else {
2557 return false;
2558 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002559}
2560
Kate Stoneb9c1b512016-09-06 20:57:50 +00002561void ClangASTContext::SetMetadataAsUserID(const void *object,
2562 user_id_t user_id) {
2563 ClangASTMetadata meta_data;
2564 meta_data.SetUserID(user_id);
2565 SetMetadata(object, meta_data);
Greg Clayton99558cc42015-08-24 23:46:31 +00002566}
2567
Kate Stoneb9c1b512016-09-06 20:57:50 +00002568void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2569 ClangASTMetadata &metadata) {
2570 ClangExternalASTSourceCommon *external_source =
2571 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2572
2573 if (external_source)
2574 external_source->SetMetadata(object, metadata);
2575}
2576
2577ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2578 const void *object) {
2579 ClangExternalASTSourceCommon *external_source =
2580 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2581
2582 if (external_source && external_source->HasMetadata(object))
2583 return external_source->GetMetadata(object);
2584 else
Greg Claytond8d4a572015-08-11 21:38:15 +00002585 return nullptr;
2586}
2587
Kate Stoneb9c1b512016-09-06 20:57:50 +00002588clang::DeclContext *
2589ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2590 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2591}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002592
Kate Stoneb9c1b512016-09-06 20:57:50 +00002593clang::DeclContext *
2594ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2595 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2596}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002597
Kate Stoneb9c1b512016-09-06 20:57:50 +00002598bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2599 int kind) const {
2600 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2601 if (clang_type) {
2602 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2603 if (tag_type) {
2604 clang::TagDecl *tag_decl =
2605 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2606 if (tag_decl) {
2607 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2608 return true;
2609 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002610 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002611 }
2612 return false;
2613}
2614
2615bool ClangASTContext::SetDefaultAccessForRecordFields(
2616 clang::RecordDecl *record_decl, int default_accessibility,
2617 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2618 if (record_decl) {
2619 uint32_t field_idx;
2620 clang::RecordDecl::field_iterator field, field_end;
2621 for (field = record_decl->field_begin(),
2622 field_end = record_decl->field_end(), field_idx = 0;
2623 field != field_end; ++field, ++field_idx) {
2624 // If no accessibility was assigned, assign the correct one
2625 if (field_idx < num_assigned_accessibilities &&
2626 assigned_accessibilities[field_idx] == clang::AS_none)
2627 field->setAccess((clang::AccessSpecifier)default_accessibility);
2628 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002629 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002630 }
2631 return false;
2632}
2633
2634clang::DeclContext *
2635ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2636 return GetDeclContextForType(ClangUtil::GetQualType(type));
2637}
2638
2639clang::DeclContext *
2640ClangASTContext::GetDeclContextForType(clang::QualType type) {
2641 if (type.isNull())
2642 return nullptr;
2643
2644 clang::QualType qual_type = type.getCanonicalType();
2645 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2646 switch (type_class) {
2647 case clang::Type::ObjCInterface:
2648 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2649 ->getInterface();
2650 case clang::Type::ObjCObjectPointer:
2651 return GetDeclContextForType(
2652 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2653 ->getPointeeType());
2654 case clang::Type::Record:
2655 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2656 case clang::Type::Enum:
2657 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2658 case clang::Type::Typedef:
2659 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2660 ->getDecl()
2661 ->getUnderlyingType());
2662 case clang::Type::Auto:
2663 return GetDeclContextForType(
2664 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2665 case clang::Type::Elaborated:
2666 return GetDeclContextForType(
2667 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2668 case clang::Type::Paren:
2669 return GetDeclContextForType(
2670 llvm::cast<clang::ParenType>(qual_type)->desugar());
2671 default:
2672 break;
2673 }
2674 // No DeclContext in this type...
2675 return nullptr;
2676}
2677
2678static bool GetCompleteQualType(clang::ASTContext *ast,
2679 clang::QualType qual_type,
2680 bool allow_completion = true) {
2681 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2682 switch (type_class) {
2683 case clang::Type::ConstantArray:
2684 case clang::Type::IncompleteArray:
2685 case clang::Type::VariableArray: {
2686 const clang::ArrayType *array_type =
2687 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2688
2689 if (array_type)
2690 return GetCompleteQualType(ast, array_type->getElementType(),
2691 allow_completion);
2692 } break;
2693 case clang::Type::Record: {
2694 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2695 if (cxx_record_decl) {
2696 if (cxx_record_decl->hasExternalLexicalStorage()) {
2697 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2698 const bool fields_loaded =
2699 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2700 if (is_complete && fields_loaded)
2701 return true;
2702
2703 if (!allow_completion)
2704 return false;
2705
2706 // Call the field_begin() accessor to for it to use the external source
2707 // to load the fields...
2708 clang::ExternalASTSource *external_ast_source =
2709 ast->getExternalSource();
2710 if (external_ast_source) {
2711 external_ast_source->CompleteType(cxx_record_decl);
2712 if (cxx_record_decl->isCompleteDefinition()) {
2713 cxx_record_decl->field_begin();
2714 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2715 }
2716 }
2717 }
2718 }
2719 const clang::TagType *tag_type =
2720 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2721 return !tag_type->isIncompleteType();
2722 } break;
2723
2724 case clang::Type::Enum: {
2725 const clang::TagType *tag_type =
2726 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2727 if (tag_type) {
2728 clang::TagDecl *tag_decl = tag_type->getDecl();
2729 if (tag_decl) {
2730 if (tag_decl->getDefinition())
2731 return true;
2732
2733 if (!allow_completion)
2734 return false;
2735
2736 if (tag_decl->hasExternalLexicalStorage()) {
2737 if (ast) {
2738 clang::ExternalASTSource *external_ast_source =
2739 ast->getExternalSource();
2740 if (external_ast_source) {
2741 external_ast_source->CompleteType(tag_decl);
2742 return !tag_type->isIncompleteType();
2743 }
2744 }
2745 }
2746 return false;
2747 }
2748 }
2749
2750 } break;
2751 case clang::Type::ObjCObject:
2752 case clang::Type::ObjCInterface: {
2753 const clang::ObjCObjectType *objc_class_type =
2754 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2755 if (objc_class_type) {
2756 clang::ObjCInterfaceDecl *class_interface_decl =
2757 objc_class_type->getInterface();
2758 // We currently can't complete objective C types through the newly added
Adrian Prantl05097242018-04-30 16:49:04 +00002759 // ASTContext because it only supports TagDecl objects right now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002760 if (class_interface_decl) {
2761 if (class_interface_decl->getDefinition())
2762 return true;
2763
2764 if (!allow_completion)
2765 return false;
2766
2767 if (class_interface_decl->hasExternalLexicalStorage()) {
2768 if (ast) {
2769 clang::ExternalASTSource *external_ast_source =
2770 ast->getExternalSource();
2771 if (external_ast_source) {
2772 external_ast_source->CompleteType(class_interface_decl);
2773 return !objc_class_type->isIncompleteType();
2774 }
2775 }
2776 }
2777 return false;
2778 }
2779 }
2780 } break;
2781
2782 case clang::Type::Typedef:
2783 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2784 ->getDecl()
2785 ->getUnderlyingType(),
2786 allow_completion);
2787
2788 case clang::Type::Auto:
2789 return GetCompleteQualType(
2790 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2791 allow_completion);
2792
2793 case clang::Type::Elaborated:
2794 return GetCompleteQualType(
2795 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2796 allow_completion);
2797
2798 case clang::Type::Paren:
2799 return GetCompleteQualType(
2800 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2801 allow_completion);
2802
2803 case clang::Type::Attributed:
2804 return GetCompleteQualType(
2805 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2806 allow_completion);
2807
2808 default:
2809 break;
2810 }
2811
2812 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00002813}
2814
2815static clang::ObjCIvarDecl::AccessControl
Kate Stoneb9c1b512016-09-06 20:57:50 +00002816ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2817 switch (access) {
2818 case eAccessNone:
Greg Claytond8d4a572015-08-11 21:38:15 +00002819 return clang::ObjCIvarDecl::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002820 case eAccessPublic:
2821 return clang::ObjCIvarDecl::Public;
2822 case eAccessPrivate:
2823 return clang::ObjCIvarDecl::Private;
2824 case eAccessProtected:
2825 return clang::ObjCIvarDecl::Protected;
2826 case eAccessPackage:
2827 return clang::ObjCIvarDecl::Package;
2828 }
2829 return clang::ObjCIvarDecl::None;
Greg Claytond8d4a572015-08-11 21:38:15 +00002830}
2831
Greg Claytond8d4a572015-08-11 21:38:15 +00002832//----------------------------------------------------------------------
2833// Tests
2834//----------------------------------------------------------------------
2835
Kate Stoneb9c1b512016-09-06 20:57:50 +00002836bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2837 clang::QualType qual_type(GetCanonicalQualType(type));
2838
2839 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2840 switch (type_class) {
2841 case clang::Type::IncompleteArray:
2842 case clang::Type::VariableArray:
2843 case clang::Type::ConstantArray:
2844 case clang::Type::ExtVector:
2845 case clang::Type::Vector:
2846 case clang::Type::Record:
2847 case clang::Type::ObjCObject:
2848 case clang::Type::ObjCInterface:
2849 return true;
2850 case clang::Type::Auto:
2851 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2852 ->getDeducedType()
2853 .getAsOpaquePtr());
2854 case clang::Type::Elaborated:
2855 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2856 ->getNamedType()
2857 .getAsOpaquePtr());
2858 case clang::Type::Typedef:
2859 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2860 ->getDecl()
2861 ->getUnderlyingType()
2862 .getAsOpaquePtr());
2863 case clang::Type::Paren:
2864 return IsAggregateType(
2865 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2866 default:
2867 break;
2868 }
2869 // The clang type does have a value
2870 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002871}
2872
Kate Stoneb9c1b512016-09-06 20:57:50 +00002873bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2874 clang::QualType qual_type(GetCanonicalQualType(type));
2875
2876 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2877 switch (type_class) {
2878 case clang::Type::Record: {
2879 if (const clang::RecordType *record_type =
2880 llvm::dyn_cast_or_null<clang::RecordType>(
2881 qual_type.getTypePtrOrNull())) {
2882 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2883 return record_decl->isAnonymousStructOrUnion();
2884 }
Enrico Granata7123e2b2015-11-07 02:06:57 +00002885 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002886 break;
2887 }
2888 case clang::Type::Auto:
2889 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2890 ->getDeducedType()
2891 .getAsOpaquePtr());
2892 case clang::Type::Elaborated:
2893 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2894 ->getNamedType()
2895 .getAsOpaquePtr());
2896 case clang::Type::Typedef:
2897 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2898 ->getDecl()
2899 ->getUnderlyingType()
2900 .getAsOpaquePtr());
2901 case clang::Type::Paren:
2902 return IsAnonymousType(
2903 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2904 default:
2905 break;
2906 }
2907 // The clang type does have a value
2908 return false;
Enrico Granata7123e2b2015-11-07 02:06:57 +00002909}
2910
Kate Stoneb9c1b512016-09-06 20:57:50 +00002911bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2912 CompilerType *element_type_ptr,
2913 uint64_t *size, bool *is_incomplete) {
2914 clang::QualType qual_type(GetCanonicalQualType(type));
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002915
Kate Stoneb9c1b512016-09-06 20:57:50 +00002916 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2917 switch (type_class) {
2918 default:
2919 break;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002920
Kate Stoneb9c1b512016-09-06 20:57:50 +00002921 case clang::Type::ConstantArray:
Greg Claytond8d4a572015-08-11 21:38:15 +00002922 if (element_type_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002923 element_type_ptr->SetCompilerType(
2924 getASTContext(),
2925 llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002926 if (size)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002927 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2928 ->getSize()
2929 .getLimitedValue(ULLONG_MAX);
Greg Claytond8d4a572015-08-11 21:38:15 +00002930 if (is_incomplete)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002931 *is_incomplete = false;
2932 return true;
2933
2934 case clang::Type::IncompleteArray:
2935 if (element_type_ptr)
2936 element_type_ptr->SetCompilerType(
2937 getASTContext(),
2938 llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2939 if (size)
2940 *size = 0;
2941 if (is_incomplete)
2942 *is_incomplete = true;
2943 return true;
2944
2945 case clang::Type::VariableArray:
2946 if (element_type_ptr)
2947 element_type_ptr->SetCompilerType(
2948 getASTContext(),
2949 llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2950 if (size)
2951 *size = 0;
2952 if (is_incomplete)
2953 *is_incomplete = false;
2954 return true;
2955
2956 case clang::Type::DependentSizedArray:
2957 if (element_type_ptr)
2958 element_type_ptr->SetCompilerType(
2959 getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)
2960 ->getElementType());
2961 if (size)
2962 *size = 0;
2963 if (is_incomplete)
2964 *is_incomplete = false;
2965 return true;
2966
2967 case clang::Type::Typedef:
2968 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2969 ->getDecl()
2970 ->getUnderlyingType()
2971 .getAsOpaquePtr(),
2972 element_type_ptr, size, is_incomplete);
2973 case clang::Type::Auto:
2974 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2975 ->getDeducedType()
2976 .getAsOpaquePtr(),
2977 element_type_ptr, size, is_incomplete);
2978 case clang::Type::Elaborated:
2979 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2980 ->getNamedType()
2981 .getAsOpaquePtr(),
2982 element_type_ptr, size, is_incomplete);
2983 case clang::Type::Paren:
2984 return IsArrayType(
2985 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2986 element_type_ptr, size, is_incomplete);
2987 }
2988 if (element_type_ptr)
2989 element_type_ptr->Clear();
2990 if (size)
2991 *size = 0;
2992 if (is_incomplete)
2993 *is_incomplete = false;
2994 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002995}
2996
Kate Stoneb9c1b512016-09-06 20:57:50 +00002997bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
2998 CompilerType *element_type, uint64_t *size) {
2999 clang::QualType qual_type(GetCanonicalQualType(type));
3000
3001 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3002 switch (type_class) {
3003 case clang::Type::Vector: {
3004 const clang::VectorType *vector_type =
3005 qual_type->getAs<clang::VectorType>();
3006 if (vector_type) {
3007 if (size)
3008 *size = vector_type->getNumElements();
3009 if (element_type)
3010 *element_type =
3011 CompilerType(getASTContext(), vector_type->getElementType());
3012 }
3013 return true;
3014 } break;
3015 case clang::Type::ExtVector: {
3016 const clang::ExtVectorType *ext_vector_type =
3017 qual_type->getAs<clang::ExtVectorType>();
3018 if (ext_vector_type) {
3019 if (size)
3020 *size = ext_vector_type->getNumElements();
3021 if (element_type)
3022 *element_type =
3023 CompilerType(getASTContext(), ext_vector_type->getElementType());
3024 }
3025 return true;
3026 }
3027 default:
3028 break;
3029 }
3030 return false;
3031}
3032
3033bool ClangASTContext::IsRuntimeGeneratedType(
3034 lldb::opaque_compiler_type_t type) {
3035 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
3036 ->GetDeclContextForType(GetQualType(type));
3037 if (!decl_ctx)
3038 return false;
3039
3040 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
3041 return false;
3042
3043 clang::ObjCInterfaceDecl *result_iface_decl =
3044 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
3045
3046 ClangASTMetadata *ast_metadata =
3047 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
3048 if (!ast_metadata)
3049 return false;
3050 return (ast_metadata->GetISAPtr() != 0);
3051}
3052
3053bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
3054 return GetQualType(type).getUnqualifiedType()->isCharType();
3055}
3056
3057bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
3058 const bool allow_completion = false;
3059 return GetCompleteQualType(getASTContext(), GetQualType(type),
3060 allow_completion);
3061}
3062
3063bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
3064 return GetQualType(type).isConstQualified();
3065}
3066
3067bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
3068 uint32_t &length) {
3069 CompilerType pointee_or_element_clang_type;
3070 length = 0;
3071 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
3072
3073 if (!pointee_or_element_clang_type.IsValid())
3074 return false;
3075
3076 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
3077 if (pointee_or_element_clang_type.IsCharType()) {
3078 if (type_flags.Test(eTypeIsArray)) {
Adrian Prantl05097242018-04-30 16:49:04 +00003079 // We know the size of the array and it could be a C string since it is
3080 // an array of characters
Kate Stoneb9c1b512016-09-06 20:57:50 +00003081 length = llvm::cast<clang::ConstantArrayType>(
3082 GetCanonicalQualType(type).getTypePtr())
3083 ->getSize()
3084 .getLimitedValue();
3085 }
3086 return true;
3087 }
3088 }
3089 return false;
3090}
3091
3092bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
3093 bool *is_variadic_ptr) {
3094 if (type) {
3095 clang::QualType qual_type(GetCanonicalQualType(type));
3096
3097 if (qual_type->isFunctionType()) {
3098 if (is_variadic_ptr) {
3099 const clang::FunctionProtoType *function_proto_type =
3100 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3101 if (function_proto_type)
3102 *is_variadic_ptr = function_proto_type->isVariadic();
3103 else
3104 *is_variadic_ptr = false;
3105 }
3106 return true;
3107 }
3108
Greg Claytond8d4a572015-08-11 21:38:15 +00003109 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003110 switch (type_class) {
3111 default:
3112 break;
3113 case clang::Type::Typedef:
3114 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
3115 ->getDecl()
3116 ->getUnderlyingType()
3117 .getAsOpaquePtr(),
3118 nullptr);
3119 case clang::Type::Auto:
3120 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
3121 ->getDeducedType()
3122 .getAsOpaquePtr(),
3123 nullptr);
3124 case clang::Type::Elaborated:
3125 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
3126 ->getNamedType()
3127 .getAsOpaquePtr(),
3128 nullptr);
3129 case clang::Type::Paren:
3130 return IsFunctionType(
3131 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3132 nullptr);
3133 case clang::Type::LValueReference:
3134 case clang::Type::RValueReference: {
3135 const clang::ReferenceType *reference_type =
3136 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3137 if (reference_type)
3138 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
3139 nullptr);
3140 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00003141 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003142 }
3143 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003144}
3145
3146// Used to detect "Homogeneous Floating-point Aggregates"
3147uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003148ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
3149 CompilerType *base_type_ptr) {
3150 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003151 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003152
3153 clang::QualType qual_type(GetCanonicalQualType(type));
3154 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3155 switch (type_class) {
3156 case clang::Type::Record:
3157 if (GetCompleteType(type)) {
3158 const clang::CXXRecordDecl *cxx_record_decl =
3159 qual_type->getAsCXXRecordDecl();
3160 if (cxx_record_decl) {
3161 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3162 return 0;
3163 }
3164 const clang::RecordType *record_type =
3165 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3166 if (record_type) {
3167 const clang::RecordDecl *record_decl = record_type->getDecl();
3168 if (record_decl) {
3169 // We are looking for a structure that contains only floating point
3170 // types
3171 clang::RecordDecl::field_iterator field_pos,
3172 field_end = record_decl->field_end();
3173 uint32_t num_fields = 0;
3174 bool is_hva = false;
3175 bool is_hfa = false;
3176 clang::QualType base_qual_type;
3177 uint64_t base_bitwidth = 0;
3178 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3179 ++field_pos) {
3180 clang::QualType field_qual_type = field_pos->getType();
3181 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
3182 if (field_qual_type->isFloatingType()) {
3183 if (field_qual_type->isComplexType())
3184 return 0;
3185 else {
3186 if (num_fields == 0)
3187 base_qual_type = field_qual_type;
3188 else {
3189 if (is_hva)
3190 return 0;
3191 is_hfa = true;
3192 if (field_qual_type.getTypePtr() !=
3193 base_qual_type.getTypePtr())
3194 return 0;
3195 }
3196 }
3197 } else if (field_qual_type->isVectorType() ||
3198 field_qual_type->isExtVectorType()) {
3199 if (num_fields == 0) {
3200 base_qual_type = field_qual_type;
3201 base_bitwidth = field_bitwidth;
3202 } else {
3203 if (is_hfa)
3204 return 0;
3205 is_hva = true;
3206 if (base_bitwidth != field_bitwidth)
3207 return 0;
3208 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3209 return 0;
3210 }
3211 } else
3212 return 0;
3213 ++num_fields;
3214 }
3215 if (base_type_ptr)
3216 *base_type_ptr = CompilerType(getASTContext(), base_qual_type);
3217 return num_fields;
3218 }
3219 }
3220 }
3221 break;
3222
3223 case clang::Type::Typedef:
3224 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3225 ->getDecl()
3226 ->getUnderlyingType()
3227 .getAsOpaquePtr(),
3228 base_type_ptr);
3229
3230 case clang::Type::Auto:
3231 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3232 ->getDeducedType()
3233 .getAsOpaquePtr(),
3234 base_type_ptr);
3235
3236 case clang::Type::Elaborated:
3237 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3238 ->getNamedType()
3239 .getAsOpaquePtr(),
3240 base_type_ptr);
3241 default:
3242 break;
3243 }
3244 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003245}
3246
Kate Stoneb9c1b512016-09-06 20:57:50 +00003247size_t ClangASTContext::GetNumberOfFunctionArguments(
3248 lldb::opaque_compiler_type_t type) {
3249 if (type) {
3250 clang::QualType qual_type(GetCanonicalQualType(type));
3251 const clang::FunctionProtoType *func =
3252 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3253 if (func)
3254 return func->getNumParams();
3255 }
3256 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003257}
3258
Greg Claytona1e5dc82015-08-11 22:53:00 +00003259CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00003260ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3261 const size_t index) {
3262 if (type) {
Greg Claytond8d4a572015-08-11 21:38:15 +00003263 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003264 const clang::FunctionProtoType *func =
3265 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3266 if (func) {
3267 if (index < func->getNumParams())
3268 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00003269 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003270 }
3271 return CompilerType();
3272}
3273
3274bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3275 if (type) {
3276 clang::QualType qual_type(GetCanonicalQualType(type));
3277
3278 if (qual_type->isFunctionPointerType())
3279 return true;
3280
3281 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3282 switch (type_class) {
3283 default:
3284 break;
3285 case clang::Type::Typedef:
3286 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3287 ->getDecl()
3288 ->getUnderlyingType()
3289 .getAsOpaquePtr());
3290 case clang::Type::Auto:
3291 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3292 ->getDeducedType()
3293 .getAsOpaquePtr());
3294 case clang::Type::Elaborated:
3295 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3296 ->getNamedType()
3297 .getAsOpaquePtr());
3298 case clang::Type::Paren:
3299 return IsFunctionPointerType(
3300 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3301
3302 case clang::Type::LValueReference:
3303 case clang::Type::RValueReference: {
3304 const clang::ReferenceType *reference_type =
3305 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3306 if (reference_type)
3307 return IsFunctionPointerType(
3308 reference_type->getPointeeType().getAsOpaquePtr());
3309 } break;
3310 }
3311 }
3312 return false;
3313}
3314
3315bool ClangASTContext::IsBlockPointerType(
3316 lldb::opaque_compiler_type_t type,
3317 CompilerType *function_pointer_type_ptr) {
3318 if (type) {
3319 clang::QualType qual_type(GetCanonicalQualType(type));
3320
3321 if (qual_type->isBlockPointerType()) {
3322 if (function_pointer_type_ptr) {
3323 const clang::BlockPointerType *block_pointer_type =
3324 qual_type->getAs<clang::BlockPointerType>();
3325 QualType pointee_type = block_pointer_type->getPointeeType();
3326 QualType function_pointer_type = m_ast_ap->getPointerType(pointee_type);
3327 *function_pointer_type_ptr =
3328 CompilerType(getASTContext(), function_pointer_type);
3329 }
3330 return true;
3331 }
3332
3333 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3334 switch (type_class) {
3335 default:
3336 break;
3337 case clang::Type::Typedef:
3338 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3339 ->getDecl()
3340 ->getUnderlyingType()
3341 .getAsOpaquePtr(),
3342 function_pointer_type_ptr);
3343 case clang::Type::Auto:
3344 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3345 ->getDeducedType()
3346 .getAsOpaquePtr(),
3347 function_pointer_type_ptr);
3348 case clang::Type::Elaborated:
3349 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3350 ->getNamedType()
3351 .getAsOpaquePtr(),
3352 function_pointer_type_ptr);
3353 case clang::Type::Paren:
3354 return IsBlockPointerType(
3355 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3356 function_pointer_type_ptr);
3357
3358 case clang::Type::LValueReference:
3359 case clang::Type::RValueReference: {
3360 const clang::ReferenceType *reference_type =
3361 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3362 if (reference_type)
3363 return IsBlockPointerType(
3364 reference_type->getPointeeType().getAsOpaquePtr(),
3365 function_pointer_type_ptr);
3366 } break;
3367 }
3368 }
3369 return false;
3370}
3371
3372bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3373 bool &is_signed) {
3374 if (!type)
3375 return false;
3376
3377 clang::QualType qual_type(GetCanonicalQualType(type));
3378 const clang::BuiltinType *builtin_type =
3379 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3380
3381 if (builtin_type) {
3382 if (builtin_type->isInteger()) {
3383 is_signed = builtin_type->isSignedInteger();
3384 return true;
3385 }
3386 }
3387
3388 return false;
3389}
3390
3391bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3392 bool &is_signed) {
3393 if (type) {
3394 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3395 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3396
3397 if (enum_type) {
3398 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3399 is_signed);
3400 return true;
3401 }
3402 }
3403
3404 return false;
3405}
3406
3407bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3408 CompilerType *pointee_type) {
3409 if (type) {
3410 clang::QualType qual_type(GetCanonicalQualType(type));
3411 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3412 switch (type_class) {
3413 case clang::Type::Builtin:
3414 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3415 default:
3416 break;
3417 case clang::BuiltinType::ObjCId:
3418 case clang::BuiltinType::ObjCClass:
3419 return true;
3420 }
3421 return false;
3422 case clang::Type::ObjCObjectPointer:
3423 if (pointee_type)
3424 pointee_type->SetCompilerType(
3425 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3426 ->getPointeeType());
3427 return true;
3428 case clang::Type::BlockPointer:
3429 if (pointee_type)
3430 pointee_type->SetCompilerType(
3431 getASTContext(),
3432 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3433 return true;
3434 case clang::Type::Pointer:
3435 if (pointee_type)
3436 pointee_type->SetCompilerType(
3437 getASTContext(),
3438 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3439 return true;
3440 case clang::Type::MemberPointer:
3441 if (pointee_type)
3442 pointee_type->SetCompilerType(
3443 getASTContext(),
3444 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3445 return true;
3446 case clang::Type::Typedef:
3447 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3448 ->getDecl()
3449 ->getUnderlyingType()
3450 .getAsOpaquePtr(),
3451 pointee_type);
3452 case clang::Type::Auto:
3453 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3454 ->getDeducedType()
3455 .getAsOpaquePtr(),
3456 pointee_type);
3457 case clang::Type::Elaborated:
3458 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3459 ->getNamedType()
3460 .getAsOpaquePtr(),
3461 pointee_type);
3462 case clang::Type::Paren:
3463 return IsPointerType(
3464 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3465 pointee_type);
3466 default:
3467 break;
3468 }
3469 }
3470 if (pointee_type)
3471 pointee_type->Clear();
3472 return false;
3473}
3474
3475bool ClangASTContext::IsPointerOrReferenceType(
3476 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3477 if (type) {
3478 clang::QualType qual_type(GetCanonicalQualType(type));
3479 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3480 switch (type_class) {
3481 case clang::Type::Builtin:
3482 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3483 default:
3484 break;
3485 case clang::BuiltinType::ObjCId:
3486 case clang::BuiltinType::ObjCClass:
3487 return true;
3488 }
3489 return false;
3490 case clang::Type::ObjCObjectPointer:
3491 if (pointee_type)
3492 pointee_type->SetCompilerType(
3493 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3494 ->getPointeeType());
3495 return true;
3496 case clang::Type::BlockPointer:
3497 if (pointee_type)
3498 pointee_type->SetCompilerType(
3499 getASTContext(),
3500 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3501 return true;
3502 case clang::Type::Pointer:
3503 if (pointee_type)
3504 pointee_type->SetCompilerType(
3505 getASTContext(),
3506 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3507 return true;
3508 case clang::Type::MemberPointer:
3509 if (pointee_type)
3510 pointee_type->SetCompilerType(
3511 getASTContext(),
3512 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3513 return true;
3514 case clang::Type::LValueReference:
3515 if (pointee_type)
3516 pointee_type->SetCompilerType(
3517 getASTContext(),
3518 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3519 return true;
3520 case clang::Type::RValueReference:
3521 if (pointee_type)
3522 pointee_type->SetCompilerType(
3523 getASTContext(),
3524 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3525 return true;
3526 case clang::Type::Typedef:
3527 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3528 ->getDecl()
3529 ->getUnderlyingType()
3530 .getAsOpaquePtr(),
3531 pointee_type);
3532 case clang::Type::Auto:
3533 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3534 ->getDeducedType()
3535 .getAsOpaquePtr(),
3536 pointee_type);
3537 case clang::Type::Elaborated:
3538 return IsPointerOrReferenceType(
3539 llvm::cast<clang::ElaboratedType>(qual_type)
3540 ->getNamedType()
3541 .getAsOpaquePtr(),
3542 pointee_type);
3543 case clang::Type::Paren:
3544 return IsPointerOrReferenceType(
3545 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3546 pointee_type);
3547 default:
3548 break;
3549 }
3550 }
3551 if (pointee_type)
3552 pointee_type->Clear();
3553 return false;
3554}
3555
3556bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3557 CompilerType *pointee_type,
3558 bool *is_rvalue) {
3559 if (type) {
3560 clang::QualType qual_type(GetCanonicalQualType(type));
3561 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3562
3563 switch (type_class) {
3564 case clang::Type::LValueReference:
3565 if (pointee_type)
3566 pointee_type->SetCompilerType(
3567 getASTContext(),
3568 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3569 if (is_rvalue)
3570 *is_rvalue = false;
3571 return true;
3572 case clang::Type::RValueReference:
3573 if (pointee_type)
3574 pointee_type->SetCompilerType(
3575 getASTContext(),
3576 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3577 if (is_rvalue)
3578 *is_rvalue = true;
3579 return true;
3580 case clang::Type::Typedef:
3581 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3582 ->getDecl()
3583 ->getUnderlyingType()
3584 .getAsOpaquePtr(),
3585 pointee_type, is_rvalue);
3586 case clang::Type::Auto:
3587 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3588 ->getDeducedType()
3589 .getAsOpaquePtr(),
3590 pointee_type, is_rvalue);
3591 case clang::Type::Elaborated:
3592 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3593 ->getNamedType()
3594 .getAsOpaquePtr(),
3595 pointee_type, is_rvalue);
3596 case clang::Type::Paren:
3597 return IsReferenceType(
3598 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3599 pointee_type, is_rvalue);
3600
3601 default:
3602 break;
3603 }
3604 }
3605 if (pointee_type)
3606 pointee_type->Clear();
3607 return false;
3608}
3609
3610bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3611 uint32_t &count, bool &is_complex) {
3612 if (type) {
3613 clang::QualType qual_type(GetCanonicalQualType(type));
3614
3615 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3616 qual_type->getCanonicalTypeInternal())) {
3617 clang::BuiltinType::Kind kind = BT->getKind();
3618 if (kind >= clang::BuiltinType::Float &&
3619 kind <= clang::BuiltinType::LongDouble) {
3620 count = 1;
3621 is_complex = false;
3622 return true;
3623 }
3624 } else if (const clang::ComplexType *CT =
3625 llvm::dyn_cast<clang::ComplexType>(
3626 qual_type->getCanonicalTypeInternal())) {
3627 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3628 is_complex)) {
3629 count = 2;
3630 is_complex = true;
3631 return true;
3632 }
3633 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3634 qual_type->getCanonicalTypeInternal())) {
3635 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3636 is_complex)) {
3637 count = VT->getNumElements();
3638 is_complex = false;
3639 return true;
3640 }
3641 }
3642 }
3643 count = 0;
3644 is_complex = false;
3645 return false;
3646}
3647
3648bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3649 if (!type)
3650 return false;
3651
3652 clang::QualType qual_type(GetQualType(type));
3653 const clang::TagType *tag_type =
3654 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3655 if (tag_type) {
3656 clang::TagDecl *tag_decl = tag_type->getDecl();
3657 if (tag_decl)
3658 return tag_decl->isCompleteDefinition();
3659 return false;
3660 } else {
3661 const clang::ObjCObjectType *objc_class_type =
3662 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3663 if (objc_class_type) {
3664 clang::ObjCInterfaceDecl *class_interface_decl =
3665 objc_class_type->getInterface();
3666 if (class_interface_decl)
3667 return class_interface_decl->getDefinition() != nullptr;
3668 return false;
3669 }
3670 }
3671 return true;
3672}
3673
3674bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
3675 if (type) {
3676 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3677
3678 const clang::ObjCObjectPointerType *obj_pointer_type =
3679 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3680
3681 if (obj_pointer_type)
3682 return obj_pointer_type->isObjCClassType();
3683 }
3684 return false;
3685}
3686
3687bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3688 if (ClangUtil::IsClangType(type))
3689 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3690 return false;
3691}
3692
3693bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3694 if (!type)
3695 return false;
3696 clang::QualType qual_type(GetCanonicalQualType(type));
3697 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3698 return (type_class == clang::Type::Record);
3699}
3700
3701bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3702 if (!type)
3703 return false;
3704 clang::QualType qual_type(GetCanonicalQualType(type));
3705 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3706 return (type_class == clang::Type::Enum);
3707}
3708
3709bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3710 if (type) {
3711 clang::QualType qual_type(GetCanonicalQualType(type));
3712 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3713 switch (type_class) {
3714 case clang::Type::Record:
3715 if (GetCompleteType(type)) {
3716 const clang::RecordType *record_type =
3717 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3718 const clang::RecordDecl *record_decl = record_type->getDecl();
3719 if (record_decl) {
3720 const clang::CXXRecordDecl *cxx_record_decl =
3721 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3722 if (cxx_record_decl)
3723 return cxx_record_decl->isPolymorphic();
Greg Claytond8d4a572015-08-11 21:38:15 +00003724 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003725 }
3726 break;
3727
3728 default:
3729 break;
3730 }
3731 }
3732 return false;
3733}
3734
3735bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3736 CompilerType *dynamic_pointee_type,
3737 bool check_cplusplus,
3738 bool check_objc) {
3739 clang::QualType pointee_qual_type;
3740 if (type) {
3741 clang::QualType qual_type(GetCanonicalQualType(type));
3742 bool success = false;
3743 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3744 switch (type_class) {
3745 case clang::Type::Builtin:
3746 if (check_objc &&
3747 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3748 clang::BuiltinType::ObjCId) {
3749 if (dynamic_pointee_type)
3750 dynamic_pointee_type->SetCompilerType(this, type);
3751 return true;
3752 }
3753 break;
3754
3755 case clang::Type::ObjCObjectPointer:
3756 if (check_objc) {
3757 if (auto objc_pointee_type =
3758 qual_type->getPointeeType().getTypePtrOrNull()) {
3759 if (auto objc_object_type =
3760 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3761 objc_pointee_type)) {
3762 if (objc_object_type->isObjCClass())
3763 return false;
3764 }
3765 }
3766 if (dynamic_pointee_type)
3767 dynamic_pointee_type->SetCompilerType(
3768 getASTContext(),
3769 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3770 ->getPointeeType());
3771 return true;
3772 }
3773 break;
3774
3775 case clang::Type::Pointer:
3776 pointee_qual_type =
3777 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3778 success = true;
3779 break;
3780
3781 case clang::Type::LValueReference:
3782 case clang::Type::RValueReference:
3783 pointee_qual_type =
3784 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3785 success = true;
3786 break;
3787
3788 case clang::Type::Typedef:
3789 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3790 ->getDecl()
3791 ->getUnderlyingType()
3792 .getAsOpaquePtr(),
3793 dynamic_pointee_type, check_cplusplus,
3794 check_objc);
3795
3796 case clang::Type::Auto:
3797 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3798 ->getDeducedType()
3799 .getAsOpaquePtr(),
3800 dynamic_pointee_type, check_cplusplus,
3801 check_objc);
3802
3803 case clang::Type::Elaborated:
3804 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3805 ->getNamedType()
3806 .getAsOpaquePtr(),
3807 dynamic_pointee_type, check_cplusplus,
3808 check_objc);
3809
3810 case clang::Type::Paren:
3811 return IsPossibleDynamicType(
3812 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3813 dynamic_pointee_type, check_cplusplus, check_objc);
3814 default:
3815 break;
3816 }
3817
3818 if (success) {
3819 // Check to make sure what we are pointing too is a possible dynamic C++
Adrian Prantl05097242018-04-30 16:49:04 +00003820 // type We currently accept any "void *" (in case we have a class that
3821 // has been watered down to an opaque pointer) and virtual C++ classes.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003822 const clang::Type::TypeClass pointee_type_class =
3823 pointee_qual_type.getCanonicalType()->getTypeClass();
3824 switch (pointee_type_class) {
3825 case clang::Type::Builtin:
3826 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3827 case clang::BuiltinType::UnknownAny:
3828 case clang::BuiltinType::Void:
3829 if (dynamic_pointee_type)
3830 dynamic_pointee_type->SetCompilerType(getASTContext(),
3831 pointee_qual_type);
3832 return true;
3833 default:
3834 break;
3835 }
3836 break;
3837
3838 case clang::Type::Record:
3839 if (check_cplusplus) {
3840 clang::CXXRecordDecl *cxx_record_decl =
3841 pointee_qual_type->getAsCXXRecordDecl();
3842 if (cxx_record_decl) {
3843 bool is_complete = cxx_record_decl->isCompleteDefinition();
3844
3845 if (is_complete)
3846 success = cxx_record_decl->isDynamicClass();
3847 else {
3848 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3849 getASTContext(), cxx_record_decl);
3850 if (metadata)
3851 success = metadata->GetIsDynamicCXXType();
3852 else {
3853 is_complete = CompilerType(getASTContext(), pointee_qual_type)
3854 .GetCompleteType();
3855 if (is_complete)
3856 success = cxx_record_decl->isDynamicClass();
3857 else
3858 success = false;
3859 }
3860 }
3861
3862 if (success) {
3863 if (dynamic_pointee_type)
3864 dynamic_pointee_type->SetCompilerType(getASTContext(),
3865 pointee_qual_type);
3866 return true;
3867 }
3868 }
3869 }
3870 break;
3871
3872 case clang::Type::ObjCObject:
3873 case clang::Type::ObjCInterface:
3874 if (check_objc) {
3875 if (dynamic_pointee_type)
3876 dynamic_pointee_type->SetCompilerType(getASTContext(),
3877 pointee_qual_type);
3878 return true;
3879 }
3880 break;
3881
3882 default:
3883 break;
3884 }
3885 }
3886 }
3887 if (dynamic_pointee_type)
3888 dynamic_pointee_type->Clear();
3889 return false;
3890}
3891
3892bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3893 if (!type)
3894 return false;
3895
3896 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3897}
3898
3899bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3900 if (!type)
3901 return false;
3902 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3903}
3904
3905bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3906 if (!type)
3907 return false;
3908 return GetCanonicalQualType(type)->isVoidType();
3909}
3910
3911bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3912 return ClangASTContextSupportsLanguage(language);
3913}
3914
3915bool ClangASTContext::GetCXXClassName(const CompilerType &type,
3916 std::string &class_name) {
3917 if (type) {
3918 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3919 if (!qual_type.isNull()) {
3920 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3921 if (cxx_record_decl) {
3922 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3923 return true;
3924 }
3925 }
3926 }
3927 class_name.clear();
3928 return false;
3929}
3930
3931bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3932 if (!type)
3933 return false;
3934
3935 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3936 if (!qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr)
3937 return true;
3938 return false;
3939}
3940
3941bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3942 if (!type)
3943 return false;
3944 clang::QualType qual_type(GetCanonicalQualType(type));
3945 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3946 if (tag_type)
3947 return tag_type->isBeingDefined();
3948 return false;
3949}
3950
3951bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3952 CompilerType *class_type_ptr) {
3953 if (!type)
3954 return false;
3955
3956 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3957
3958 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3959 if (class_type_ptr) {
3960 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3961 const clang::ObjCObjectPointerType *obj_pointer_type =
3962 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3963 if (obj_pointer_type == nullptr)
3964 class_type_ptr->Clear();
3965 else
3966 class_type_ptr->SetCompilerType(
3967 type.GetTypeSystem(),
3968 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3969 .getAsOpaquePtr());
3970 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003971 }
3972 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003973 }
3974 if (class_type_ptr)
3975 class_type_ptr->Clear();
3976 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003977}
3978
Kate Stoneb9c1b512016-09-06 20:57:50 +00003979bool ClangASTContext::GetObjCClassName(const CompilerType &type,
3980 std::string &class_name) {
3981 if (!type)
3982 return false;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003983
Kate Stoneb9c1b512016-09-06 20:57:50 +00003984 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3985
3986 const clang::ObjCObjectType *object_type =
3987 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3988 if (object_type) {
3989 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3990 if (interface) {
3991 class_name = interface->getNameAsString();
3992 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00003993 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003994 }
3995 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003996}
3997
Greg Claytond8d4a572015-08-11 21:38:15 +00003998//----------------------------------------------------------------------
3999// Type Completion
4000//----------------------------------------------------------------------
4001
Kate Stoneb9c1b512016-09-06 20:57:50 +00004002bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
4003 if (!type)
4004 return false;
4005 const bool allow_completion = true;
4006 return GetCompleteQualType(getASTContext(), GetQualType(type),
4007 allow_completion);
Greg Claytond8d4a572015-08-11 21:38:15 +00004008}
4009
Kate Stoneb9c1b512016-09-06 20:57:50 +00004010ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
4011 std::string type_name;
4012 if (type) {
4013 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
4014 clang::QualType qual_type(GetQualType(type));
4015 printing_policy.SuppressTagKeyword = true;
4016 const clang::TypedefType *typedef_type =
4017 qual_type->getAs<clang::TypedefType>();
4018 if (typedef_type) {
4019 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
4020 type_name = typedef_decl->getQualifiedNameAsString();
4021 } else {
4022 type_name = qual_type.getAsString(printing_policy);
Greg Claytond8d4a572015-08-11 21:38:15 +00004023 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004024 }
4025 return ConstString(type_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00004026}
4027
4028uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004029ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
4030 CompilerType *pointee_or_element_clang_type) {
4031 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004032 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004033
4034 if (pointee_or_element_clang_type)
4035 pointee_or_element_clang_type->Clear();
4036
4037 clang::QualType qual_type(GetQualType(type));
4038
4039 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4040 switch (type_class) {
Sean Callananddf802a2017-06-02 01:24:18 +00004041 case clang::Type::Attributed:
4042 return GetTypeInfo(
4043 qual_type->getAs<clang::AttributedType>()
4044 ->getModifiedType().getAsOpaquePtr(),
4045 pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004046 case clang::Type::Builtin: {
4047 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
4048 qual_type->getCanonicalTypeInternal());
4049
4050 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
4051 switch (builtin_type->getKind()) {
4052 case clang::BuiltinType::ObjCId:
4053 case clang::BuiltinType::ObjCClass:
4054 if (pointee_or_element_clang_type)
4055 pointee_or_element_clang_type->SetCompilerType(
4056 getASTContext(), getASTContext()->ObjCBuiltinClassTy);
4057 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
4058 break;
4059
4060 case clang::BuiltinType::ObjCSel:
4061 if (pointee_or_element_clang_type)
4062 pointee_or_element_clang_type->SetCompilerType(getASTContext(),
4063 getASTContext()->CharTy);
4064 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
4065 break;
4066
4067 case clang::BuiltinType::Bool:
4068 case clang::BuiltinType::Char_U:
4069 case clang::BuiltinType::UChar:
4070 case clang::BuiltinType::WChar_U:
4071 case clang::BuiltinType::Char16:
4072 case clang::BuiltinType::Char32:
4073 case clang::BuiltinType::UShort:
4074 case clang::BuiltinType::UInt:
4075 case clang::BuiltinType::ULong:
4076 case clang::BuiltinType::ULongLong:
4077 case clang::BuiltinType::UInt128:
4078 case clang::BuiltinType::Char_S:
4079 case clang::BuiltinType::SChar:
4080 case clang::BuiltinType::WChar_S:
4081 case clang::BuiltinType::Short:
4082 case clang::BuiltinType::Int:
4083 case clang::BuiltinType::Long:
4084 case clang::BuiltinType::LongLong:
4085 case clang::BuiltinType::Int128:
4086 case clang::BuiltinType::Float:
4087 case clang::BuiltinType::Double:
4088 case clang::BuiltinType::LongDouble:
4089 builtin_type_flags |= eTypeIsScalar;
4090 if (builtin_type->isInteger()) {
4091 builtin_type_flags |= eTypeIsInteger;
4092 if (builtin_type->isSignedInteger())
4093 builtin_type_flags |= eTypeIsSigned;
4094 } else if (builtin_type->isFloatingPoint())
4095 builtin_type_flags |= eTypeIsFloat;
4096 break;
4097 default:
4098 break;
4099 }
4100 return builtin_type_flags;
4101 }
4102
4103 case clang::Type::BlockPointer:
4104 if (pointee_or_element_clang_type)
4105 pointee_or_element_clang_type->SetCompilerType(
4106 getASTContext(), qual_type->getPointeeType());
4107 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
4108
4109 case clang::Type::Complex: {
4110 uint32_t complex_type_flags =
4111 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
4112 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
4113 qual_type->getCanonicalTypeInternal());
4114 if (complex_type) {
4115 clang::QualType complex_element_type(complex_type->getElementType());
4116 if (complex_element_type->isIntegerType())
4117 complex_type_flags |= eTypeIsFloat;
4118 else if (complex_element_type->isFloatingType())
4119 complex_type_flags |= eTypeIsInteger;
4120 }
4121 return complex_type_flags;
4122 } break;
4123
4124 case clang::Type::ConstantArray:
4125 case clang::Type::DependentSizedArray:
4126 case clang::Type::IncompleteArray:
4127 case clang::Type::VariableArray:
4128 if (pointee_or_element_clang_type)
4129 pointee_or_element_clang_type->SetCompilerType(
4130 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
4131 ->getElementType());
4132 return eTypeHasChildren | eTypeIsArray;
4133
4134 case clang::Type::DependentName:
4135 return 0;
4136 case clang::Type::DependentSizedExtVector:
4137 return eTypeHasChildren | eTypeIsVector;
4138 case clang::Type::DependentTemplateSpecialization:
4139 return eTypeIsTemplate;
4140 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004141 return CompilerType(
4142 getASTContext(),
4143 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
4144 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004145
4146 case clang::Type::Enum:
4147 if (pointee_or_element_clang_type)
4148 pointee_or_element_clang_type->SetCompilerType(
4149 getASTContext(),
4150 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
4151 return eTypeIsEnumeration | eTypeHasValue;
4152
4153 case clang::Type::Auto:
4154 return CompilerType(
4155 getASTContext(),
4156 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4157 .GetTypeInfo(pointee_or_element_clang_type);
4158 case clang::Type::Elaborated:
4159 return CompilerType(
4160 getASTContext(),
4161 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4162 .GetTypeInfo(pointee_or_element_clang_type);
4163 case clang::Type::Paren:
4164 return CompilerType(getASTContext(),
4165 llvm::cast<clang::ParenType>(qual_type)->desugar())
4166 .GetTypeInfo(pointee_or_element_clang_type);
4167
4168 case clang::Type::FunctionProto:
4169 return eTypeIsFuncPrototype | eTypeHasValue;
4170 case clang::Type::FunctionNoProto:
4171 return eTypeIsFuncPrototype | eTypeHasValue;
4172 case clang::Type::InjectedClassName:
4173 return 0;
4174
4175 case clang::Type::LValueReference:
4176 case clang::Type::RValueReference:
4177 if (pointee_or_element_clang_type)
4178 pointee_or_element_clang_type->SetCompilerType(
4179 getASTContext(),
4180 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4181 ->getPointeeType());
4182 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4183
4184 case clang::Type::MemberPointer:
4185 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4186
4187 case clang::Type::ObjCObjectPointer:
4188 if (pointee_or_element_clang_type)
4189 pointee_or_element_clang_type->SetCompilerType(
4190 getASTContext(), qual_type->getPointeeType());
4191 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4192 eTypeHasValue;
4193
4194 case clang::Type::ObjCObject:
4195 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4196 case clang::Type::ObjCInterface:
4197 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4198
4199 case clang::Type::Pointer:
4200 if (pointee_or_element_clang_type)
4201 pointee_or_element_clang_type->SetCompilerType(
4202 getASTContext(), qual_type->getPointeeType());
4203 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4204
4205 case clang::Type::Record:
4206 if (qual_type->getAsCXXRecordDecl())
4207 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4208 else
4209 return eTypeHasChildren | eTypeIsStructUnion;
4210 break;
4211 case clang::Type::SubstTemplateTypeParm:
4212 return eTypeIsTemplate;
4213 case clang::Type::TemplateTypeParm:
4214 return eTypeIsTemplate;
4215 case clang::Type::TemplateSpecialization:
4216 return eTypeIsTemplate;
4217
4218 case clang::Type::Typedef:
4219 return eTypeIsTypedef |
4220 CompilerType(getASTContext(),
4221 llvm::cast<clang::TypedefType>(qual_type)
4222 ->getDecl()
4223 ->getUnderlyingType())
4224 .GetTypeInfo(pointee_or_element_clang_type);
4225 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004226 return CompilerType(getASTContext(),
4227 llvm::cast<clang::TypeOfExprType>(qual_type)
4228 ->getUnderlyingExpr()
4229 ->getType())
4230 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004231 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004232 return CompilerType(
4233 getASTContext(),
4234 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4235 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004236 case clang::Type::UnresolvedUsing:
4237 return 0;
4238
4239 case clang::Type::ExtVector:
4240 case clang::Type::Vector: {
4241 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4242 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4243 qual_type->getCanonicalTypeInternal());
4244 if (vector_type) {
4245 if (vector_type->isIntegerType())
4246 vector_type_flags |= eTypeIsFloat;
4247 else if (vector_type->isFloatingType())
4248 vector_type_flags |= eTypeIsInteger;
4249 }
4250 return vector_type_flags;
4251 }
4252 default:
4253 return 0;
4254 }
4255 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004256}
4257
Greg Claytond8d4a572015-08-11 21:38:15 +00004258lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004259ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4260 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004261 return lldb::eLanguageTypeC;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004262
4263 // If the type is a reference, then resolve it to what it refers to first:
4264 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4265 if (qual_type->isAnyPointerType()) {
4266 if (qual_type->isObjCObjectPointerType())
4267 return lldb::eLanguageTypeObjC;
4268
4269 clang::QualType pointee_type(qual_type->getPointeeType());
4270 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
4271 return lldb::eLanguageTypeC_plus_plus;
4272 if (pointee_type->isObjCObjectOrInterfaceType())
4273 return lldb::eLanguageTypeObjC;
4274 if (pointee_type->isObjCClassType())
4275 return lldb::eLanguageTypeObjC;
4276 if (pointee_type.getTypePtr() ==
4277 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4278 return lldb::eLanguageTypeObjC;
4279 } else {
4280 if (qual_type->isObjCObjectOrInterfaceType())
4281 return lldb::eLanguageTypeObjC;
4282 if (qual_type->getAsCXXRecordDecl())
4283 return lldb::eLanguageTypeC_plus_plus;
4284 switch (qual_type->getTypeClass()) {
4285 default:
4286 break;
4287 case clang::Type::Builtin:
4288 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4289 default:
4290 case clang::BuiltinType::Void:
4291 case clang::BuiltinType::Bool:
4292 case clang::BuiltinType::Char_U:
4293 case clang::BuiltinType::UChar:
4294 case clang::BuiltinType::WChar_U:
4295 case clang::BuiltinType::Char16:
4296 case clang::BuiltinType::Char32:
4297 case clang::BuiltinType::UShort:
4298 case clang::BuiltinType::UInt:
4299 case clang::BuiltinType::ULong:
4300 case clang::BuiltinType::ULongLong:
4301 case clang::BuiltinType::UInt128:
4302 case clang::BuiltinType::Char_S:
4303 case clang::BuiltinType::SChar:
4304 case clang::BuiltinType::WChar_S:
4305 case clang::BuiltinType::Short:
4306 case clang::BuiltinType::Int:
4307 case clang::BuiltinType::Long:
4308 case clang::BuiltinType::LongLong:
4309 case clang::BuiltinType::Int128:
4310 case clang::BuiltinType::Float:
4311 case clang::BuiltinType::Double:
4312 case clang::BuiltinType::LongDouble:
4313 break;
4314
4315 case clang::BuiltinType::NullPtr:
4316 return eLanguageTypeC_plus_plus;
4317
4318 case clang::BuiltinType::ObjCId:
4319 case clang::BuiltinType::ObjCClass:
4320 case clang::BuiltinType::ObjCSel:
4321 return eLanguageTypeObjC;
4322
4323 case clang::BuiltinType::Dependent:
4324 case clang::BuiltinType::Overload:
4325 case clang::BuiltinType::BoundMember:
4326 case clang::BuiltinType::UnknownAny:
4327 break;
4328 }
4329 break;
4330 case clang::Type::Typedef:
4331 return CompilerType(getASTContext(),
4332 llvm::cast<clang::TypedefType>(qual_type)
4333 ->getDecl()
4334 ->getUnderlyingType())
4335 .GetMinimumLanguage();
4336 }
4337 }
4338 return lldb::eLanguageTypeC;
Greg Claytond8d4a572015-08-11 21:38:15 +00004339}
4340
4341lldb::TypeClass
Kate Stoneb9c1b512016-09-06 20:57:50 +00004342ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4343 if (!type)
4344 return lldb::eTypeClassInvalid;
4345
4346 clang::QualType qual_type(GetQualType(type));
4347
4348 switch (qual_type->getTypeClass()) {
4349 case clang::Type::UnaryTransform:
4350 break;
4351 case clang::Type::FunctionNoProto:
4352 return lldb::eTypeClassFunction;
4353 case clang::Type::FunctionProto:
4354 return lldb::eTypeClassFunction;
4355 case clang::Type::IncompleteArray:
4356 return lldb::eTypeClassArray;
4357 case clang::Type::VariableArray:
4358 return lldb::eTypeClassArray;
4359 case clang::Type::ConstantArray:
4360 return lldb::eTypeClassArray;
4361 case clang::Type::DependentSizedArray:
4362 return lldb::eTypeClassArray;
4363 case clang::Type::DependentSizedExtVector:
4364 return lldb::eTypeClassVector;
Fangrui Song8f284882018-07-13 22:40:40 +00004365 case clang::Type::DependentVector:
4366 return lldb::eTypeClassVector;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004367 case clang::Type::ExtVector:
4368 return lldb::eTypeClassVector;
4369 case clang::Type::Vector:
4370 return lldb::eTypeClassVector;
4371 case clang::Type::Builtin:
4372 return lldb::eTypeClassBuiltin;
4373 case clang::Type::ObjCObjectPointer:
4374 return lldb::eTypeClassObjCObjectPointer;
4375 case clang::Type::BlockPointer:
4376 return lldb::eTypeClassBlockPointer;
4377 case clang::Type::Pointer:
4378 return lldb::eTypeClassPointer;
4379 case clang::Type::LValueReference:
4380 return lldb::eTypeClassReference;
4381 case clang::Type::RValueReference:
4382 return lldb::eTypeClassReference;
4383 case clang::Type::MemberPointer:
4384 return lldb::eTypeClassMemberPointer;
4385 case clang::Type::Complex:
4386 if (qual_type->isComplexType())
4387 return lldb::eTypeClassComplexFloat;
4388 else
4389 return lldb::eTypeClassComplexInteger;
4390 case clang::Type::ObjCObject:
4391 return lldb::eTypeClassObjCObject;
4392 case clang::Type::ObjCInterface:
4393 return lldb::eTypeClassObjCInterface;
4394 case clang::Type::Record: {
4395 const clang::RecordType *record_type =
4396 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4397 const clang::RecordDecl *record_decl = record_type->getDecl();
4398 if (record_decl->isUnion())
4399 return lldb::eTypeClassUnion;
4400 else if (record_decl->isStruct())
4401 return lldb::eTypeClassStruct;
4402 else
4403 return lldb::eTypeClassClass;
4404 } break;
4405 case clang::Type::Enum:
4406 return lldb::eTypeClassEnumeration;
4407 case clang::Type::Typedef:
4408 return lldb::eTypeClassTypedef;
4409 case clang::Type::UnresolvedUsing:
4410 break;
4411 case clang::Type::Paren:
4412 return CompilerType(getASTContext(),
4413 llvm::cast<clang::ParenType>(qual_type)->desugar())
4414 .GetTypeClass();
4415 case clang::Type::Auto:
4416 return CompilerType(
4417 getASTContext(),
4418 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4419 .GetTypeClass();
4420 case clang::Type::Elaborated:
4421 return CompilerType(
4422 getASTContext(),
4423 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4424 .GetTypeClass();
4425
4426 case clang::Type::Attributed:
4427 break;
4428 case clang::Type::TemplateTypeParm:
4429 break;
4430 case clang::Type::SubstTemplateTypeParm:
4431 break;
4432 case clang::Type::SubstTemplateTypeParmPack:
4433 break;
4434 case clang::Type::InjectedClassName:
4435 break;
4436 case clang::Type::DependentName:
4437 break;
4438 case clang::Type::DependentTemplateSpecialization:
4439 break;
4440 case clang::Type::PackExpansion:
4441 break;
4442
4443 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004444 return CompilerType(getASTContext(),
4445 llvm::cast<clang::TypeOfExprType>(qual_type)
4446 ->getUnderlyingExpr()
4447 ->getType())
4448 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004449 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004450 return CompilerType(
4451 getASTContext(),
4452 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4453 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004454 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004455 return CompilerType(
4456 getASTContext(),
4457 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4458 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004459 case clang::Type::TemplateSpecialization:
4460 break;
Pavel Labath4f19fce22017-02-17 13:39:50 +00004461 case clang::Type::DeducedTemplateSpecialization:
4462 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004463 case clang::Type::Atomic:
4464 break;
4465 case clang::Type::Pipe:
4466 break;
4467
4468 // pointer type decayed from an array or function type.
4469 case clang::Type::Decayed:
4470 break;
4471 case clang::Type::Adjusted:
4472 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00004473 case clang::Type::ObjCTypeParam:
4474 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00004475
4476 case clang::Type::DependentAddressSpace:
4477 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004478 }
4479 // We don't know hot to display this type...
4480 return lldb::eTypeClassOther;
Greg Claytond8d4a572015-08-11 21:38:15 +00004481}
4482
Kate Stoneb9c1b512016-09-06 20:57:50 +00004483unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4484 if (type)
4485 return GetQualType(type).getQualifiers().getCVRQualifiers();
4486 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004487}
4488
4489//----------------------------------------------------------------------
4490// Creating related types
4491//----------------------------------------------------------------------
4492
Greg Claytona1e5dc82015-08-11 22:53:00 +00004493CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004494ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4495 uint64_t *stride) {
4496 if (type) {
4497 clang::QualType qual_type(GetCanonicalQualType(type));
4498
4499 const clang::Type *array_eletype =
4500 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4501
4502 if (!array_eletype)
4503 return CompilerType();
4504
4505 CompilerType element_type(getASTContext(),
4506 array_eletype->getCanonicalTypeUnqualified());
4507
4508 // TODO: the real stride will be >= this value.. find the real one!
4509 if (stride)
4510 *stride = element_type.GetByteSize(nullptr);
4511
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()))
5538 if (auto *dwarf_parser = ast.GetDWARFParser())
5539 return sym_file->GetDynamicArrayInfoForUID(metadata->GetUserID(),
5540 exe_ctx);
5541 return llvm::None;
5542}
5543
Kate Stoneb9c1b512016-09-06 20:57:50 +00005544uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
Adrian Prantleca07c52018-11-05 20:49:07 +00005545 bool omit_empty_base_classes,
5546 const ExecutionContext *exe_ctx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005547 if (!type)
5548 return 0;
5549
5550 uint32_t num_children = 0;
5551 clang::QualType qual_type(GetQualType(type));
5552 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5553 switch (type_class) {
5554 case clang::Type::Builtin:
5555 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5556 case clang::BuiltinType::ObjCId: // child is Class
5557 case clang::BuiltinType::ObjCClass: // child is Class
5558 num_children = 1;
5559 break;
5560
5561 default:
5562 break;
5563 }
5564 break;
5565
5566 case clang::Type::Complex:
5567 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005568 case clang::Type::Record:
5569 if (GetCompleteQualType(getASTContext(), qual_type)) {
5570 const clang::RecordType *record_type =
5571 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5572 const clang::RecordDecl *record_decl = record_type->getDecl();
5573 assert(record_decl);
5574 const clang::CXXRecordDecl *cxx_record_decl =
5575 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5576 if (cxx_record_decl) {
5577 if (omit_empty_base_classes) {
Adrian Prantl05097242018-04-30 16:49:04 +00005578 // Check each base classes to see if it or any of its base classes
5579 // contain any fields. This can help limit the noise in variable
5580 // views by not having to show base classes that contain no members.
Kate Stoneb9c1b512016-09-06 20:57:50 +00005581 clang::CXXRecordDecl::base_class_const_iterator base_class,
5582 base_class_end;
5583 for (base_class = cxx_record_decl->bases_begin(),
5584 base_class_end = cxx_record_decl->bases_end();
5585 base_class != base_class_end; ++base_class) {
5586 const clang::CXXRecordDecl *base_class_decl =
5587 llvm::cast<clang::CXXRecordDecl>(
5588 base_class->getType()
5589 ->getAs<clang::RecordType>()
5590 ->getDecl());
5591
5592 // Skip empty base classes
5593 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5594 continue;
5595
5596 num_children++;
5597 }
5598 } else {
5599 // Include all base classes
5600 num_children += cxx_record_decl->getNumBases();
5601 }
5602 }
5603 clang::RecordDecl::field_iterator field, field_end;
5604 for (field = record_decl->field_begin(),
5605 field_end = record_decl->field_end();
5606 field != field_end; ++field)
5607 ++num_children;
5608 }
5609 break;
5610
5611 case clang::Type::ObjCObject:
5612 case clang::Type::ObjCInterface:
5613 if (GetCompleteQualType(getASTContext(), qual_type)) {
5614 const clang::ObjCObjectType *objc_class_type =
5615 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5616 assert(objc_class_type);
5617 if (objc_class_type) {
5618 clang::ObjCInterfaceDecl *class_interface_decl =
5619 objc_class_type->getInterface();
5620
5621 if (class_interface_decl) {
5622
5623 clang::ObjCInterfaceDecl *superclass_interface_decl =
5624 class_interface_decl->getSuperClass();
5625 if (superclass_interface_decl) {
5626 if (omit_empty_base_classes) {
5627 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5628 ++num_children;
5629 } else
5630 ++num_children;
5631 }
5632
5633 num_children += class_interface_decl->ivar_size();
5634 }
5635 }
5636 }
5637 break;
5638
5639 case clang::Type::ObjCObjectPointer: {
5640 const clang::ObjCObjectPointerType *pointer_type =
5641 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5642 clang::QualType pointee_type = pointer_type->getPointeeType();
5643 uint32_t num_pointee_children =
5644 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005645 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005646 // If this type points to a simple type, then it has 1 child
5647 if (num_pointee_children == 0)
5648 num_children = 1;
5649 else
5650 num_children = num_pointee_children;
5651 } break;
5652
5653 case clang::Type::Vector:
5654 case clang::Type::ExtVector:
5655 num_children =
5656 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5657 break;
5658
5659 case clang::Type::ConstantArray:
5660 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5661 ->getSize()
5662 .getLimitedValue();
5663 break;
Adrian Prantleca07c52018-11-05 20:49:07 +00005664 case clang::Type::IncompleteArray:
5665 if (auto array_info =
5666 GetDynamicArrayInfo(*this, GetSymbolFile(), qual_type, exe_ctx))
5667 // Only 1-dimensional arrays are supported.
5668 num_children = array_info->element_orders.size()
5669 ? array_info->element_orders.back()
5670 : 0;
5671 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005672
5673 case clang::Type::Pointer: {
5674 const clang::PointerType *pointer_type =
5675 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5676 clang::QualType pointee_type(pointer_type->getPointeeType());
5677 uint32_t num_pointee_children =
5678 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005679 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005680 if (num_pointee_children == 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00005681 // We have a pointer to a pointee type that claims it has no children. We
5682 // will want to look at
Kate Stoneb9c1b512016-09-06 20:57:50 +00005683 num_children = GetNumPointeeChildren(pointee_type);
5684 } else
5685 num_children = num_pointee_children;
5686 } break;
5687
5688 case clang::Type::LValueReference:
5689 case clang::Type::RValueReference: {
5690 const clang::ReferenceType *reference_type =
5691 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5692 clang::QualType pointee_type = reference_type->getPointeeType();
5693 uint32_t num_pointee_children =
5694 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005695 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005696 // If this type points to a simple type, then it has 1 child
5697 if (num_pointee_children == 0)
5698 num_children = 1;
5699 else
5700 num_children = num_pointee_children;
5701 } break;
5702
5703 case clang::Type::Typedef:
5704 num_children =
5705 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5706 ->getDecl()
5707 ->getUnderlyingType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005708 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005709 break;
5710
5711 case clang::Type::Auto:
5712 num_children =
5713 CompilerType(getASTContext(),
5714 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005715 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005716 break;
5717
5718 case clang::Type::Elaborated:
5719 num_children =
5720 CompilerType(
5721 getASTContext(),
5722 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005723 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005724 break;
5725
5726 case clang::Type::Paren:
5727 num_children =
5728 CompilerType(getASTContext(),
5729 llvm::cast<clang::ParenType>(qual_type)->desugar())
Adrian Prantleca07c52018-11-05 20:49:07 +00005730 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005731 break;
5732 default:
5733 break;
5734 }
5735 return num_children;
5736}
5737
5738CompilerType ClangASTContext::GetBuiltinTypeByName(const ConstString &name) {
5739 return GetBasicType(GetBasicTypeEnumeration(name));
Greg Clayton56939cb2015-09-17 22:23:34 +00005740}
5741
Greg Claytond8d4a572015-08-11 21:38:15 +00005742lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00005743ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5744 if (type) {
5745 clang::QualType qual_type(GetQualType(type));
5746 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5747 if (type_class == clang::Type::Builtin) {
5748 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5749 case clang::BuiltinType::Void:
5750 return eBasicTypeVoid;
5751 case clang::BuiltinType::Bool:
5752 return eBasicTypeBool;
5753 case clang::BuiltinType::Char_S:
5754 return eBasicTypeSignedChar;
5755 case clang::BuiltinType::Char_U:
5756 return eBasicTypeUnsignedChar;
5757 case clang::BuiltinType::Char16:
5758 return eBasicTypeChar16;
5759 case clang::BuiltinType::Char32:
5760 return eBasicTypeChar32;
5761 case clang::BuiltinType::UChar:
5762 return eBasicTypeUnsignedChar;
5763 case clang::BuiltinType::SChar:
5764 return eBasicTypeSignedChar;
5765 case clang::BuiltinType::WChar_S:
5766 return eBasicTypeSignedWChar;
5767 case clang::BuiltinType::WChar_U:
5768 return eBasicTypeUnsignedWChar;
5769 case clang::BuiltinType::Short:
5770 return eBasicTypeShort;
5771 case clang::BuiltinType::UShort:
5772 return eBasicTypeUnsignedShort;
5773 case clang::BuiltinType::Int:
5774 return eBasicTypeInt;
5775 case clang::BuiltinType::UInt:
5776 return eBasicTypeUnsignedInt;
5777 case clang::BuiltinType::Long:
5778 return eBasicTypeLong;
5779 case clang::BuiltinType::ULong:
5780 return eBasicTypeUnsignedLong;
5781 case clang::BuiltinType::LongLong:
5782 return eBasicTypeLongLong;
5783 case clang::BuiltinType::ULongLong:
5784 return eBasicTypeUnsignedLongLong;
5785 case clang::BuiltinType::Int128:
5786 return eBasicTypeInt128;
5787 case clang::BuiltinType::UInt128:
5788 return eBasicTypeUnsignedInt128;
5789
5790 case clang::BuiltinType::Half:
5791 return eBasicTypeHalf;
5792 case clang::BuiltinType::Float:
5793 return eBasicTypeFloat;
5794 case clang::BuiltinType::Double:
5795 return eBasicTypeDouble;
5796 case clang::BuiltinType::LongDouble:
5797 return eBasicTypeLongDouble;
5798
5799 case clang::BuiltinType::NullPtr:
5800 return eBasicTypeNullPtr;
5801 case clang::BuiltinType::ObjCId:
5802 return eBasicTypeObjCID;
5803 case clang::BuiltinType::ObjCClass:
5804 return eBasicTypeObjCClass;
5805 case clang::BuiltinType::ObjCSel:
5806 return eBasicTypeObjCSel;
5807 default:
5808 return eBasicTypeOther;
5809 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005810 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005811 }
5812 return eBasicTypeInvalid;
Greg Claytond8d4a572015-08-11 21:38:15 +00005813}
5814
Kate Stoneb9c1b512016-09-06 20:57:50 +00005815void ClangASTContext::ForEachEnumerator(
5816 lldb::opaque_compiler_type_t type,
5817 std::function<bool(const CompilerType &integer_type,
5818 const ConstString &name,
5819 const llvm::APSInt &value)> const &callback) {
5820 const clang::EnumType *enum_type =
5821 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5822 if (enum_type) {
5823 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5824 if (enum_decl) {
5825 CompilerType integer_type(this,
5826 enum_decl->getIntegerType().getAsOpaquePtr());
Greg Clayton99558cc42015-08-24 23:46:31 +00005827
Kate Stoneb9c1b512016-09-06 20:57:50 +00005828 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5829 for (enum_pos = enum_decl->enumerator_begin(),
5830 enum_end_pos = enum_decl->enumerator_end();
5831 enum_pos != enum_end_pos; ++enum_pos) {
5832 ConstString name(enum_pos->getNameAsString().c_str());
5833 if (!callback(integer_type, name, enum_pos->getInitVal()))
5834 break;
5835 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005836 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005837 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005838}
5839
Greg Claytond8d4a572015-08-11 21:38:15 +00005840#pragma mark Aggregate Types
5841
Kate Stoneb9c1b512016-09-06 20:57:50 +00005842uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5843 if (!type)
5844 return 0;
Enrico Granata36f51e42015-12-18 22:41:25 +00005845
Kate Stoneb9c1b512016-09-06 20:57:50 +00005846 uint32_t count = 0;
5847 clang::QualType qual_type(GetCanonicalQualType(type));
5848 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5849 switch (type_class) {
5850 case clang::Type::Record:
5851 if (GetCompleteType(type)) {
5852 const clang::RecordType *record_type =
5853 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5854 if (record_type) {
5855 clang::RecordDecl *record_decl = record_type->getDecl();
5856 if (record_decl) {
5857 uint32_t field_idx = 0;
5858 clang::RecordDecl::field_iterator field, field_end;
5859 for (field = record_decl->field_begin(),
5860 field_end = record_decl->field_end();
5861 field != field_end; ++field)
5862 ++field_idx;
5863 count = field_idx;
5864 }
5865 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005866 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005867 break;
5868
5869 case clang::Type::Typedef:
5870 count =
5871 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5872 ->getDecl()
5873 ->getUnderlyingType())
5874 .GetNumFields();
5875 break;
5876
5877 case clang::Type::Auto:
5878 count =
5879 CompilerType(getASTContext(),
5880 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5881 .GetNumFields();
5882 break;
5883
5884 case clang::Type::Elaborated:
5885 count = CompilerType(
5886 getASTContext(),
5887 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5888 .GetNumFields();
5889 break;
5890
5891 case clang::Type::Paren:
5892 count = CompilerType(getASTContext(),
5893 llvm::cast<clang::ParenType>(qual_type)->desugar())
5894 .GetNumFields();
5895 break;
5896
Sean Callananf9c622a2016-09-30 18:44:43 +00005897 case clang::Type::ObjCObjectPointer: {
5898 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005899 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005900 const clang::ObjCInterfaceType *objc_interface_type =
5901 objc_class_type->getInterfaceType();
5902 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005903 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5904 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005905 clang::ObjCInterfaceDecl *class_interface_decl =
5906 objc_interface_type->getDecl();
5907 if (class_interface_decl) {
5908 count = class_interface_decl->ivar_size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005909 }
5910 }
5911 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005912 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005913
5914 case clang::Type::ObjCObject:
5915 case clang::Type::ObjCInterface:
5916 if (GetCompleteType(type)) {
5917 const clang::ObjCObjectType *objc_class_type =
5918 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5919 if (objc_class_type) {
5920 clang::ObjCInterfaceDecl *class_interface_decl =
5921 objc_class_type->getInterface();
5922
5923 if (class_interface_decl)
5924 count = class_interface_decl->ivar_size();
5925 }
5926 }
5927 break;
5928
5929 default:
5930 break;
5931 }
5932 return count;
Greg Claytond8d4a572015-08-11 21:38:15 +00005933}
5934
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005935static lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005936GetObjCFieldAtIndex(clang::ASTContext *ast,
5937 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5938 std::string &name, uint64_t *bit_offset_ptr,
5939 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5940 if (class_interface_decl) {
5941 if (idx < (class_interface_decl->ivar_size())) {
5942 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5943 ivar_end = class_interface_decl->ivar_end();
5944 uint32_t ivar_idx = 0;
5945
5946 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5947 ++ivar_pos, ++ivar_idx) {
5948 if (ivar_idx == idx) {
5949 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5950
5951 clang::QualType ivar_qual_type(ivar_decl->getType());
5952
5953 name.assign(ivar_decl->getNameAsString());
5954
5955 if (bit_offset_ptr) {
5956 const clang::ASTRecordLayout &interface_layout =
5957 ast->getASTObjCInterfaceLayout(class_interface_decl);
5958 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5959 }
5960
5961 const bool is_bitfield = ivar_pos->isBitField();
5962
5963 if (bitfield_bit_size_ptr) {
5964 *bitfield_bit_size_ptr = 0;
5965
5966 if (is_bitfield && ast) {
5967 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
Bill Wendlingd7656de2018-11-21 20:44:38 +00005968 clang::Expr::EvalResult result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005969 if (bitfield_bit_size_expr &&
Bill Wendlingd7656de2018-11-21 20:44:38 +00005970 bitfield_bit_size_expr->EvaluateAsInt(result, *ast)) {
5971 llvm::APSInt bitfield_apsint = result.Val.getInt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005972 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5973 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005974 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005975 }
5976 if (is_bitfield_ptr)
5977 *is_bitfield_ptr = is_bitfield;
5978
5979 return ivar_qual_type.getAsOpaquePtr();
Greg Claytond8d4a572015-08-11 21:38:15 +00005980 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005981 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005982 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005983 }
5984 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00005985}
5986
Kate Stoneb9c1b512016-09-06 20:57:50 +00005987CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5988 size_t idx, std::string &name,
5989 uint64_t *bit_offset_ptr,
5990 uint32_t *bitfield_bit_size_ptr,
5991 bool *is_bitfield_ptr) {
5992 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005993 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005994
5995 clang::QualType qual_type(GetCanonicalQualType(type));
5996 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5997 switch (type_class) {
5998 case clang::Type::Record:
5999 if (GetCompleteType(type)) {
6000 const clang::RecordType *record_type =
6001 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6002 const clang::RecordDecl *record_decl = record_type->getDecl();
6003 uint32_t field_idx = 0;
6004 clang::RecordDecl::field_iterator field, field_end;
6005 for (field = record_decl->field_begin(),
6006 field_end = record_decl->field_end();
6007 field != field_end; ++field, ++field_idx) {
6008 if (idx == field_idx) {
6009 // Print the member type if requested
6010 // Print the member name and equal sign
6011 name.assign(field->getNameAsString());
6012
6013 // Figure out the type byte size (field_type_info.first) and
6014 // alignment (field_type_info.second) from the AST context.
6015 if (bit_offset_ptr) {
6016 const clang::ASTRecordLayout &record_layout =
6017 getASTContext()->getASTRecordLayout(record_decl);
6018 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
6019 }
6020
6021 const bool is_bitfield = field->isBitField();
6022
6023 if (bitfield_bit_size_ptr) {
6024 *bitfield_bit_size_ptr = 0;
6025
6026 if (is_bitfield) {
6027 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
Bill Wendlingd7656de2018-11-21 20:44:38 +00006028 clang::Expr::EvalResult result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006029 if (bitfield_bit_size_expr &&
Bill Wendlingd7656de2018-11-21 20:44:38 +00006030 bitfield_bit_size_expr->EvaluateAsInt(result,
Kate Stoneb9c1b512016-09-06 20:57:50 +00006031 *getASTContext())) {
Bill Wendlingd7656de2018-11-21 20:44:38 +00006032 llvm::APSInt bitfield_apsint = result.Val.getInt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006033 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
6034 }
6035 }
6036 }
6037 if (is_bitfield_ptr)
6038 *is_bitfield_ptr = is_bitfield;
6039
6040 return CompilerType(getASTContext(), field->getType());
6041 }
6042 }
6043 }
6044 break;
6045
Sean Callananf9c622a2016-09-30 18:44:43 +00006046 case clang::Type::ObjCObjectPointer: {
6047 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00006048 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00006049 const clang::ObjCInterfaceType *objc_interface_type =
6050 objc_class_type->getInterfaceType();
6051 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00006052 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
6053 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00006054 clang::ObjCInterfaceDecl *class_interface_decl =
6055 objc_interface_type->getDecl();
6056 if (class_interface_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006057 return CompilerType(
6058 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
6059 idx, name, bit_offset_ptr,
6060 bitfield_bit_size_ptr, is_bitfield_ptr));
6061 }
6062 }
6063 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00006064 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006065
6066 case clang::Type::ObjCObject:
6067 case clang::Type::ObjCInterface:
6068 if (GetCompleteType(type)) {
6069 const clang::ObjCObjectType *objc_class_type =
6070 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6071 assert(objc_class_type);
6072 if (objc_class_type) {
6073 clang::ObjCInterfaceDecl *class_interface_decl =
6074 objc_class_type->getInterface();
6075 return CompilerType(
6076 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
6077 idx, name, bit_offset_ptr,
6078 bitfield_bit_size_ptr, is_bitfield_ptr));
6079 }
6080 }
6081 break;
6082
6083 case clang::Type::Typedef:
6084 return CompilerType(getASTContext(),
6085 llvm::cast<clang::TypedefType>(qual_type)
6086 ->getDecl()
6087 ->getUnderlyingType())
6088 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6089 is_bitfield_ptr);
6090
6091 case clang::Type::Auto:
6092 return CompilerType(
6093 getASTContext(),
6094 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
6095 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6096 is_bitfield_ptr);
6097
6098 case clang::Type::Elaborated:
6099 return CompilerType(
6100 getASTContext(),
6101 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
6102 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6103 is_bitfield_ptr);
6104
6105 case clang::Type::Paren:
6106 return CompilerType(getASTContext(),
6107 llvm::cast<clang::ParenType>(qual_type)->desugar())
6108 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6109 is_bitfield_ptr);
6110
6111 default:
6112 break;
6113 }
6114 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006115}
6116
Greg Clayton99558cc42015-08-24 23:46:31 +00006117uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006118ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
6119 uint32_t count = 0;
6120 clang::QualType qual_type(GetCanonicalQualType(type));
6121 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6122 switch (type_class) {
6123 case clang::Type::Record:
6124 if (GetCompleteType(type)) {
6125 const clang::CXXRecordDecl *cxx_record_decl =
6126 qual_type->getAsCXXRecordDecl();
6127 if (cxx_record_decl)
6128 count = cxx_record_decl->getNumBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006129 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006130 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006131
Kate Stoneb9c1b512016-09-06 20:57:50 +00006132 case clang::Type::ObjCObjectPointer:
6133 count = GetPointeeType(type).GetNumDirectBaseClasses();
6134 break;
6135
6136 case clang::Type::ObjCObject:
6137 if (GetCompleteType(type)) {
6138 const clang::ObjCObjectType *objc_class_type =
6139 qual_type->getAsObjCQualifiedInterfaceType();
6140 if (objc_class_type) {
6141 clang::ObjCInterfaceDecl *class_interface_decl =
6142 objc_class_type->getInterface();
6143
6144 if (class_interface_decl && class_interface_decl->getSuperClass())
6145 count = 1;
6146 }
6147 }
6148 break;
6149 case clang::Type::ObjCInterface:
6150 if (GetCompleteType(type)) {
6151 const clang::ObjCInterfaceType *objc_interface_type =
6152 qual_type->getAs<clang::ObjCInterfaceType>();
6153 if (objc_interface_type) {
6154 clang::ObjCInterfaceDecl *class_interface_decl =
6155 objc_interface_type->getInterface();
6156
6157 if (class_interface_decl && class_interface_decl->getSuperClass())
6158 count = 1;
6159 }
6160 }
6161 break;
6162
6163 case clang::Type::Typedef:
6164 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6165 ->getDecl()
6166 ->getUnderlyingType()
6167 .getAsOpaquePtr());
6168 break;
6169
6170 case clang::Type::Auto:
6171 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6172 ->getDeducedType()
6173 .getAsOpaquePtr());
6174 break;
6175
6176 case clang::Type::Elaborated:
6177 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6178 ->getNamedType()
6179 .getAsOpaquePtr());
6180 break;
6181
6182 case clang::Type::Paren:
6183 return GetNumDirectBaseClasses(
6184 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6185
6186 default:
6187 break;
6188 }
6189 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006190}
6191
6192uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006193ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
6194 uint32_t count = 0;
6195 clang::QualType qual_type(GetCanonicalQualType(type));
6196 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6197 switch (type_class) {
6198 case clang::Type::Record:
6199 if (GetCompleteType(type)) {
6200 const clang::CXXRecordDecl *cxx_record_decl =
6201 qual_type->getAsCXXRecordDecl();
6202 if (cxx_record_decl)
6203 count = cxx_record_decl->getNumVBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006204 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006205 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006206
Kate Stoneb9c1b512016-09-06 20:57:50 +00006207 case clang::Type::Typedef:
6208 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6209 ->getDecl()
6210 ->getUnderlyingType()
6211 .getAsOpaquePtr());
6212 break;
6213
6214 case clang::Type::Auto:
6215 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6216 ->getDeducedType()
6217 .getAsOpaquePtr());
6218 break;
6219
6220 case clang::Type::Elaborated:
6221 count =
6222 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6223 ->getNamedType()
6224 .getAsOpaquePtr());
6225 break;
6226
6227 case clang::Type::Paren:
6228 count = GetNumVirtualBaseClasses(
6229 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6230 break;
6231
6232 default:
6233 break;
6234 }
6235 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006236}
6237
Kate Stoneb9c1b512016-09-06 20:57:50 +00006238CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
6239 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6240 clang::QualType qual_type(GetCanonicalQualType(type));
6241 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6242 switch (type_class) {
6243 case clang::Type::Record:
6244 if (GetCompleteType(type)) {
6245 const clang::CXXRecordDecl *cxx_record_decl =
6246 qual_type->getAsCXXRecordDecl();
6247 if (cxx_record_decl) {
6248 uint32_t curr_idx = 0;
6249 clang::CXXRecordDecl::base_class_const_iterator base_class,
6250 base_class_end;
6251 for (base_class = cxx_record_decl->bases_begin(),
6252 base_class_end = cxx_record_decl->bases_end();
6253 base_class != base_class_end; ++base_class, ++curr_idx) {
6254 if (curr_idx == idx) {
6255 if (bit_offset_ptr) {
6256 const clang::ASTRecordLayout &record_layout =
6257 getASTContext()->getASTRecordLayout(cxx_record_decl);
6258 const clang::CXXRecordDecl *base_class_decl =
6259 llvm::cast<clang::CXXRecordDecl>(
6260 base_class->getType()
6261 ->getAs<clang::RecordType>()
6262 ->getDecl());
6263 if (base_class->isVirtual())
6264 *bit_offset_ptr =
6265 record_layout.getVBaseClassOffset(base_class_decl)
6266 .getQuantity() *
6267 8;
6268 else
6269 *bit_offset_ptr =
6270 record_layout.getBaseClassOffset(base_class_decl)
6271 .getQuantity() *
6272 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006273 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006274 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6275 }
6276 }
6277 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006278 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006279 break;
6280
6281 case clang::Type::ObjCObjectPointer:
6282 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6283
6284 case clang::Type::ObjCObject:
6285 if (idx == 0 && GetCompleteType(type)) {
6286 const clang::ObjCObjectType *objc_class_type =
6287 qual_type->getAsObjCQualifiedInterfaceType();
6288 if (objc_class_type) {
6289 clang::ObjCInterfaceDecl *class_interface_decl =
6290 objc_class_type->getInterface();
6291
6292 if (class_interface_decl) {
6293 clang::ObjCInterfaceDecl *superclass_interface_decl =
6294 class_interface_decl->getSuperClass();
6295 if (superclass_interface_decl) {
6296 if (bit_offset_ptr)
6297 *bit_offset_ptr = 0;
6298 return CompilerType(getASTContext(),
6299 getASTContext()->getObjCInterfaceType(
6300 superclass_interface_decl));
6301 }
6302 }
6303 }
6304 }
6305 break;
6306 case clang::Type::ObjCInterface:
6307 if (idx == 0 && GetCompleteType(type)) {
6308 const clang::ObjCObjectType *objc_interface_type =
6309 qual_type->getAs<clang::ObjCInterfaceType>();
6310 if (objc_interface_type) {
6311 clang::ObjCInterfaceDecl *class_interface_decl =
6312 objc_interface_type->getInterface();
6313
6314 if (class_interface_decl) {
6315 clang::ObjCInterfaceDecl *superclass_interface_decl =
6316 class_interface_decl->getSuperClass();
6317 if (superclass_interface_decl) {
6318 if (bit_offset_ptr)
6319 *bit_offset_ptr = 0;
6320 return CompilerType(getASTContext(),
6321 getASTContext()->getObjCInterfaceType(
6322 superclass_interface_decl));
6323 }
6324 }
6325 }
6326 }
6327 break;
6328
6329 case clang::Type::Typedef:
6330 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6331 ->getDecl()
6332 ->getUnderlyingType()
6333 .getAsOpaquePtr(),
6334 idx, bit_offset_ptr);
6335
6336 case clang::Type::Auto:
6337 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6338 ->getDeducedType()
6339 .getAsOpaquePtr(),
6340 idx, bit_offset_ptr);
6341
6342 case clang::Type::Elaborated:
6343 return GetDirectBaseClassAtIndex(
6344 llvm::cast<clang::ElaboratedType>(qual_type)
6345 ->getNamedType()
6346 .getAsOpaquePtr(),
6347 idx, bit_offset_ptr);
6348
6349 case clang::Type::Paren:
6350 return GetDirectBaseClassAtIndex(
6351 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6352 idx, bit_offset_ptr);
6353
6354 default:
6355 break;
6356 }
6357 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006358}
6359
Kate Stoneb9c1b512016-09-06 20:57:50 +00006360CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6361 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6362 clang::QualType qual_type(GetCanonicalQualType(type));
6363 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6364 switch (type_class) {
6365 case clang::Type::Record:
6366 if (GetCompleteType(type)) {
6367 const clang::CXXRecordDecl *cxx_record_decl =
6368 qual_type->getAsCXXRecordDecl();
6369 if (cxx_record_decl) {
6370 uint32_t curr_idx = 0;
6371 clang::CXXRecordDecl::base_class_const_iterator base_class,
6372 base_class_end;
6373 for (base_class = cxx_record_decl->vbases_begin(),
6374 base_class_end = cxx_record_decl->vbases_end();
6375 base_class != base_class_end; ++base_class, ++curr_idx) {
6376 if (curr_idx == idx) {
6377 if (bit_offset_ptr) {
6378 const clang::ASTRecordLayout &record_layout =
6379 getASTContext()->getASTRecordLayout(cxx_record_decl);
6380 const clang::CXXRecordDecl *base_class_decl =
6381 llvm::cast<clang::CXXRecordDecl>(
6382 base_class->getType()
6383 ->getAs<clang::RecordType>()
6384 ->getDecl());
6385 *bit_offset_ptr =
6386 record_layout.getVBaseClassOffset(base_class_decl)
6387 .getQuantity() *
6388 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006389 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006390 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6391 }
6392 }
6393 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006394 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006395 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006396
Kate Stoneb9c1b512016-09-06 20:57:50 +00006397 case clang::Type::Typedef:
6398 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6399 ->getDecl()
6400 ->getUnderlyingType()
6401 .getAsOpaquePtr(),
6402 idx, bit_offset_ptr);
6403
6404 case clang::Type::Auto:
6405 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6406 ->getDeducedType()
6407 .getAsOpaquePtr(),
6408 idx, bit_offset_ptr);
6409
6410 case clang::Type::Elaborated:
6411 return GetVirtualBaseClassAtIndex(
6412 llvm::cast<clang::ElaboratedType>(qual_type)
6413 ->getNamedType()
6414 .getAsOpaquePtr(),
6415 idx, bit_offset_ptr);
6416
6417 case clang::Type::Paren:
6418 return GetVirtualBaseClassAtIndex(
6419 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6420 idx, bit_offset_ptr);
6421
6422 default:
6423 break;
6424 }
6425 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006426}
6427
Greg Claytond8d4a572015-08-11 21:38:15 +00006428// If a pointer to a pointee type (the clang_type arg) says that it has no
6429// children, then we either need to trust it, or override it and return a
6430// different result. For example, an "int *" has one child that is an integer,
6431// but a function pointer doesn't have any children. Likewise if a Record type
6432// claims it has no children, then there really is nothing to show.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006433uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6434 if (type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00006435 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006436
6437 clang::QualType qual_type(type.getCanonicalType());
6438 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6439 switch (type_class) {
6440 case clang::Type::Builtin:
6441 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6442 case clang::BuiltinType::UnknownAny:
6443 case clang::BuiltinType::Void:
6444 case clang::BuiltinType::NullPtr:
6445 case clang::BuiltinType::OCLEvent:
6446 case clang::BuiltinType::OCLImage1dRO:
6447 case clang::BuiltinType::OCLImage1dWO:
6448 case clang::BuiltinType::OCLImage1dRW:
6449 case clang::BuiltinType::OCLImage1dArrayRO:
6450 case clang::BuiltinType::OCLImage1dArrayWO:
6451 case clang::BuiltinType::OCLImage1dArrayRW:
6452 case clang::BuiltinType::OCLImage1dBufferRO:
6453 case clang::BuiltinType::OCLImage1dBufferWO:
6454 case clang::BuiltinType::OCLImage1dBufferRW:
6455 case clang::BuiltinType::OCLImage2dRO:
6456 case clang::BuiltinType::OCLImage2dWO:
6457 case clang::BuiltinType::OCLImage2dRW:
6458 case clang::BuiltinType::OCLImage2dArrayRO:
6459 case clang::BuiltinType::OCLImage2dArrayWO:
6460 case clang::BuiltinType::OCLImage2dArrayRW:
6461 case clang::BuiltinType::OCLImage3dRO:
6462 case clang::BuiltinType::OCLImage3dWO:
6463 case clang::BuiltinType::OCLImage3dRW:
6464 case clang::BuiltinType::OCLSampler:
6465 return 0;
6466 case clang::BuiltinType::Bool:
6467 case clang::BuiltinType::Char_U:
6468 case clang::BuiltinType::UChar:
6469 case clang::BuiltinType::WChar_U:
6470 case clang::BuiltinType::Char16:
6471 case clang::BuiltinType::Char32:
6472 case clang::BuiltinType::UShort:
6473 case clang::BuiltinType::UInt:
6474 case clang::BuiltinType::ULong:
6475 case clang::BuiltinType::ULongLong:
6476 case clang::BuiltinType::UInt128:
6477 case clang::BuiltinType::Char_S:
6478 case clang::BuiltinType::SChar:
6479 case clang::BuiltinType::WChar_S:
6480 case clang::BuiltinType::Short:
6481 case clang::BuiltinType::Int:
6482 case clang::BuiltinType::Long:
6483 case clang::BuiltinType::LongLong:
6484 case clang::BuiltinType::Int128:
6485 case clang::BuiltinType::Float:
6486 case clang::BuiltinType::Double:
6487 case clang::BuiltinType::LongDouble:
6488 case clang::BuiltinType::Dependent:
6489 case clang::BuiltinType::Overload:
6490 case clang::BuiltinType::ObjCId:
6491 case clang::BuiltinType::ObjCClass:
6492 case clang::BuiltinType::ObjCSel:
6493 case clang::BuiltinType::BoundMember:
6494 case clang::BuiltinType::Half:
6495 case clang::BuiltinType::ARCUnbridgedCast:
6496 case clang::BuiltinType::PseudoObject:
6497 case clang::BuiltinType::BuiltinFn:
6498 case clang::BuiltinType::OMPArraySection:
6499 return 1;
6500 default:
6501 return 0;
6502 }
6503 break;
6504
6505 case clang::Type::Complex:
6506 return 1;
6507 case clang::Type::Pointer:
6508 return 1;
6509 case clang::Type::BlockPointer:
6510 return 0; // If block pointers don't have debug info, then no children for
6511 // them
6512 case clang::Type::LValueReference:
6513 return 1;
6514 case clang::Type::RValueReference:
6515 return 1;
6516 case clang::Type::MemberPointer:
6517 return 0;
6518 case clang::Type::ConstantArray:
6519 return 0;
6520 case clang::Type::IncompleteArray:
6521 return 0;
6522 case clang::Type::VariableArray:
6523 return 0;
6524 case clang::Type::DependentSizedArray:
6525 return 0;
6526 case clang::Type::DependentSizedExtVector:
6527 return 0;
6528 case clang::Type::Vector:
6529 return 0;
6530 case clang::Type::ExtVector:
6531 return 0;
6532 case clang::Type::FunctionProto:
6533 return 0; // When we function pointers, they have no children...
6534 case clang::Type::FunctionNoProto:
6535 return 0; // When we function pointers, they have no children...
6536 case clang::Type::UnresolvedUsing:
6537 return 0;
6538 case clang::Type::Paren:
6539 return GetNumPointeeChildren(
6540 llvm::cast<clang::ParenType>(qual_type)->desugar());
6541 case clang::Type::Typedef:
6542 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6543 ->getDecl()
6544 ->getUnderlyingType());
6545 case clang::Type::Auto:
6546 return GetNumPointeeChildren(
6547 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6548 case clang::Type::Elaborated:
6549 return GetNumPointeeChildren(
6550 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6551 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006552 return GetNumPointeeChildren(llvm::cast<clang::TypeOfExprType>(qual_type)
6553 ->getUnderlyingExpr()
6554 ->getType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006555 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006556 return GetNumPointeeChildren(
6557 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006558 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006559 return GetNumPointeeChildren(
6560 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006561 case clang::Type::Record:
6562 return 0;
6563 case clang::Type::Enum:
6564 return 1;
6565 case clang::Type::TemplateTypeParm:
6566 return 1;
6567 case clang::Type::SubstTemplateTypeParm:
6568 return 1;
6569 case clang::Type::TemplateSpecialization:
6570 return 1;
6571 case clang::Type::InjectedClassName:
6572 return 0;
6573 case clang::Type::DependentName:
6574 return 1;
6575 case clang::Type::DependentTemplateSpecialization:
6576 return 1;
6577 case clang::Type::ObjCObject:
6578 return 0;
6579 case clang::Type::ObjCInterface:
6580 return 0;
6581 case clang::Type::ObjCObjectPointer:
6582 return 1;
6583 default:
6584 break;
6585 }
6586 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006587}
6588
Kate Stoneb9c1b512016-09-06 20:57:50 +00006589CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6590 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6591 bool transparent_pointers, bool omit_empty_base_classes,
6592 bool ignore_array_bounds, std::string &child_name,
6593 uint32_t &child_byte_size, int32_t &child_byte_offset,
6594 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6595 bool &child_is_base_class, bool &child_is_deref_of_parent,
6596 ValueObject *valobj, uint64_t &language_flags) {
6597 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006598 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006599
Kate Stoneb9c1b512016-09-06 20:57:50 +00006600 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6601 const clang::Type::TypeClass parent_type_class =
6602 parent_qual_type->getTypeClass();
6603 child_bitfield_bit_size = 0;
6604 child_bitfield_bit_offset = 0;
6605 child_is_base_class = false;
6606 language_flags = 0;
6607
Adrian Prantleca07c52018-11-05 20:49:07 +00006608 const bool idx_is_valid =
6609 idx < GetNumChildren(type, omit_empty_base_classes, exe_ctx);
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00006610 int32_t bit_offset;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006611 switch (parent_type_class) {
6612 case clang::Type::Builtin:
6613 if (idx_is_valid) {
6614 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6615 case clang::BuiltinType::ObjCId:
6616 case clang::BuiltinType::ObjCClass:
6617 child_name = "isa";
6618 child_byte_size =
6619 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6620 CHAR_BIT;
6621 return CompilerType(getASTContext(),
6622 getASTContext()->ObjCBuiltinClassTy);
6623
6624 default:
6625 break;
6626 }
6627 }
6628 break;
6629
6630 case clang::Type::Record:
6631 if (idx_is_valid && GetCompleteType(type)) {
6632 const clang::RecordType *record_type =
6633 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6634 const clang::RecordDecl *record_decl = record_type->getDecl();
6635 assert(record_decl);
6636 const clang::ASTRecordLayout &record_layout =
6637 getASTContext()->getASTRecordLayout(record_decl);
6638 uint32_t child_idx = 0;
6639
6640 const clang::CXXRecordDecl *cxx_record_decl =
6641 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6642 if (cxx_record_decl) {
6643 // We might have base classes to print out first
6644 clang::CXXRecordDecl::base_class_const_iterator base_class,
6645 base_class_end;
6646 for (base_class = cxx_record_decl->bases_begin(),
6647 base_class_end = cxx_record_decl->bases_end();
6648 base_class != base_class_end; ++base_class) {
6649 const clang::CXXRecordDecl *base_class_decl = nullptr;
6650
6651 // Skip empty base classes
6652 if (omit_empty_base_classes) {
6653 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6654 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6655 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
6656 continue;
6657 }
6658
6659 if (idx == child_idx) {
6660 if (base_class_decl == nullptr)
6661 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6662 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6663
6664 if (base_class->isVirtual()) {
6665 bool handled = false;
6666 if (valobj) {
Aleksandr Urakov1dc51db2018-11-12 16:23:50 +00006667 clang::VTableContextBase *vtable_ctx =
6668 getASTContext()->getVTableContext();
6669 if (vtable_ctx)
6670 handled = GetVBaseBitOffset(*vtable_ctx, *valobj,
6671 record_layout, cxx_record_decl,
6672 base_class_decl, bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006673 }
6674 if (!handled)
6675 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6676 .getQuantity() *
6677 8;
6678 } else
6679 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6680 .getQuantity() *
6681 8;
6682
6683 // Base classes should be a multiple of 8 bits in size
6684 child_byte_offset = bit_offset / 8;
6685 CompilerType base_class_clang_type(getASTContext(),
6686 base_class->getType());
6687 child_name = base_class_clang_type.GetTypeName().AsCString("");
6688 uint64_t base_class_clang_type_bit_size =
6689 base_class_clang_type.GetBitSize(
6690 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6691
6692 // Base classes bit sizes should be a multiple of 8 bits in size
6693 assert(base_class_clang_type_bit_size % 8 == 0);
6694 child_byte_size = base_class_clang_type_bit_size / 8;
6695 child_is_base_class = true;
6696 return base_class_clang_type;
6697 }
6698 // We don't increment the child index in the for loop since we might
6699 // be skipping empty base classes
6700 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006701 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006702 }
6703 // Make sure index is in range...
6704 uint32_t field_idx = 0;
6705 clang::RecordDecl::field_iterator field, field_end;
6706 for (field = record_decl->field_begin(),
6707 field_end = record_decl->field_end();
6708 field != field_end; ++field, ++field_idx, ++child_idx) {
6709 if (idx == child_idx) {
6710 // Print the member type if requested
6711 // Print the member name and equal sign
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006712 child_name.assign(field->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006713
6714 // Figure out the type byte size (field_type_info.first) and
6715 // alignment (field_type_info.second) from the AST context.
6716 CompilerType field_clang_type(getASTContext(), field->getType());
6717 assert(field_idx < record_layout.getFieldCount());
6718 child_byte_size = field_clang_type.GetByteSize(
6719 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6720 const uint32_t child_bit_size = child_byte_size * 8;
6721
6722 // Figure out the field offset within the current struct/union/class
6723 // type
6724 bit_offset = record_layout.getFieldOffset(field_idx);
6725 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6726 child_bitfield_bit_size)) {
6727 child_bitfield_bit_offset = bit_offset % child_bit_size;
6728 const uint32_t child_bit_offset =
6729 bit_offset - child_bitfield_bit_offset;
6730 child_byte_offset = child_bit_offset / 8;
6731 } else {
6732 child_byte_offset = bit_offset / 8;
6733 }
6734
6735 return field_clang_type;
6736 }
6737 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006738 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006739 break;
6740
6741 case clang::Type::ObjCObject:
6742 case clang::Type::ObjCInterface:
6743 if (idx_is_valid && GetCompleteType(type)) {
6744 const clang::ObjCObjectType *objc_class_type =
6745 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6746 assert(objc_class_type);
6747 if (objc_class_type) {
6748 uint32_t child_idx = 0;
6749 clang::ObjCInterfaceDecl *class_interface_decl =
6750 objc_class_type->getInterface();
6751
6752 if (class_interface_decl) {
6753
6754 const clang::ASTRecordLayout &interface_layout =
6755 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6756 clang::ObjCInterfaceDecl *superclass_interface_decl =
6757 class_interface_decl->getSuperClass();
6758 if (superclass_interface_decl) {
6759 if (omit_empty_base_classes) {
6760 CompilerType base_class_clang_type(
6761 getASTContext(), getASTContext()->getObjCInterfaceType(
6762 superclass_interface_decl));
Adrian Prantleca07c52018-11-05 20:49:07 +00006763 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes,
6764 exe_ctx) > 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006765 if (idx == 0) {
6766 clang::QualType ivar_qual_type(
6767 getASTContext()->getObjCInterfaceType(
6768 superclass_interface_decl));
6769
6770 child_name.assign(
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006771 superclass_interface_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006772
6773 clang::TypeInfo ivar_type_info =
6774 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6775
6776 child_byte_size = ivar_type_info.Width / 8;
6777 child_byte_offset = 0;
6778 child_is_base_class = true;
6779
6780 return CompilerType(getASTContext(), ivar_qual_type);
6781 }
6782
6783 ++child_idx;
6784 }
6785 } else
6786 ++child_idx;
6787 }
6788
6789 const uint32_t superclass_idx = child_idx;
6790
6791 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6792 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6793 ivar_end = class_interface_decl->ivar_end();
6794
6795 for (ivar_pos = class_interface_decl->ivar_begin();
6796 ivar_pos != ivar_end; ++ivar_pos) {
6797 if (child_idx == idx) {
6798 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6799
6800 clang::QualType ivar_qual_type(ivar_decl->getType());
6801
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006802 child_name.assign(ivar_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006803
6804 clang::TypeInfo ivar_type_info =
6805 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6806
6807 child_byte_size = ivar_type_info.Width / 8;
6808
6809 // Figure out the field offset within the current
Adrian Prantl05097242018-04-30 16:49:04 +00006810 // struct/union/class type For ObjC objects, we can't trust the
6811 // bit offset we get from the Clang AST, since that doesn't
6812 // account for the space taken up by unbacked properties, or
6813 // from the changing size of base classes that are newer than
6814 // this class. So if we have a process around that we can ask
6815 // about this object, do so.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006816 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6817 Process *process = nullptr;
6818 if (exe_ctx)
6819 process = exe_ctx->GetProcessPtr();
6820 if (process) {
6821 ObjCLanguageRuntime *objc_runtime =
6822 process->GetObjCLanguageRuntime();
6823 if (objc_runtime != nullptr) {
6824 CompilerType parent_ast_type(getASTContext(),
6825 parent_qual_type);
6826 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6827 parent_ast_type, ivar_decl->getNameAsString().c_str());
6828 }
6829 }
6830
Aleksandr Urakovff701722018-08-20 05:59:27 +00006831 // Setting this to INT32_MAX to make sure we don't compute it
Kate Stoneb9c1b512016-09-06 20:57:50 +00006832 // twice...
Aleksandr Urakov53459482018-08-17 07:28:24 +00006833 bit_offset = INT32_MAX;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006834
6835 if (child_byte_offset ==
6836 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6837 bit_offset = interface_layout.getFieldOffset(child_idx -
6838 superclass_idx);
6839 child_byte_offset = bit_offset / 8;
6840 }
6841
6842 // Note, the ObjC Ivar Byte offset is just that, it doesn't
Adrian Prantl05097242018-04-30 16:49:04 +00006843 // account for the bit offset of a bitfield within its
6844 // containing object. So regardless of where we get the byte
Kate Stoneb9c1b512016-09-06 20:57:50 +00006845 // offset from, we still need to get the bit offset for
6846 // bitfields from the layout.
6847
6848 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6849 child_bitfield_bit_size)) {
Aleksandr Urakov53459482018-08-17 07:28:24 +00006850 if (bit_offset == INT32_MAX)
Kate Stoneb9c1b512016-09-06 20:57:50 +00006851 bit_offset = interface_layout.getFieldOffset(
6852 child_idx - superclass_idx);
6853
6854 child_bitfield_bit_offset = bit_offset % 8;
6855 }
6856 return CompilerType(getASTContext(), ivar_qual_type);
6857 }
6858 ++child_idx;
6859 }
6860 }
6861 }
6862 }
6863 }
6864 break;
6865
6866 case clang::Type::ObjCObjectPointer:
6867 if (idx_is_valid) {
6868 CompilerType pointee_clang_type(GetPointeeType(type));
6869
6870 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6871 child_is_deref_of_parent = false;
6872 bool tmp_child_is_deref_of_parent = false;
6873 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6874 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6875 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6876 child_bitfield_bit_size, child_bitfield_bit_offset,
6877 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6878 language_flags);
6879 } else {
6880 child_is_deref_of_parent = true;
6881 const char *parent_name =
6882 valobj ? valobj->GetName().GetCString() : NULL;
6883 if (parent_name) {
6884 child_name.assign(1, '*');
6885 child_name += parent_name;
6886 }
6887
6888 // We have a pointer to an simple type
6889 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
6890 child_byte_size = pointee_clang_type.GetByteSize(
6891 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6892 child_byte_offset = 0;
6893 return pointee_clang_type;
6894 }
6895 }
6896 }
6897 break;
6898
6899 case clang::Type::Vector:
6900 case clang::Type::ExtVector:
6901 if (idx_is_valid) {
6902 const clang::VectorType *array =
6903 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6904 if (array) {
6905 CompilerType element_type(getASTContext(), array->getElementType());
6906 if (element_type.GetCompleteType()) {
6907 char element_name[64];
6908 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6909 static_cast<uint64_t>(idx));
6910 child_name.assign(element_name);
6911 child_byte_size = element_type.GetByteSize(
6912 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6913 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6914 return element_type;
6915 }
6916 }
6917 }
6918 break;
6919
6920 case clang::Type::ConstantArray:
6921 case clang::Type::IncompleteArray:
6922 if (ignore_array_bounds || idx_is_valid) {
6923 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6924 if (array) {
6925 CompilerType element_type(getASTContext(), array->getElementType());
6926 if (element_type.GetCompleteType()) {
Zachary Turner827d5d72016-12-16 04:27:00 +00006927 child_name = llvm::formatv("[{0}]", idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006928 child_byte_size = element_type.GetByteSize(
6929 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6930 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6931 return element_type;
6932 }
6933 }
6934 }
6935 break;
6936
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006937 case clang::Type::Pointer: {
6938 CompilerType pointee_clang_type(GetPointeeType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00006939
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006940 // Don't dereference "void *" pointers
6941 if (pointee_clang_type.IsVoidType())
6942 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006943
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006944 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6945 child_is_deref_of_parent = false;
6946 bool tmp_child_is_deref_of_parent = false;
6947 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6948 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6949 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6950 child_bitfield_bit_size, child_bitfield_bit_offset,
6951 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6952 language_flags);
6953 } else {
6954 child_is_deref_of_parent = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006955
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006956 const char *parent_name =
6957 valobj ? valobj->GetName().GetCString() : NULL;
6958 if (parent_name) {
6959 child_name.assign(1, '*');
6960 child_name += parent_name;
6961 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006962
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006963 // We have a pointer to an simple type
6964 if (idx == 0) {
6965 child_byte_size = pointee_clang_type.GetByteSize(
6966 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6967 child_byte_offset = 0;
6968 return pointee_clang_type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006969 }
6970 }
6971 break;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006972 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006973
6974 case clang::Type::LValueReference:
6975 case clang::Type::RValueReference:
6976 if (idx_is_valid) {
6977 const clang::ReferenceType *reference_type =
6978 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
6979 CompilerType pointee_clang_type(getASTContext(),
6980 reference_type->getPointeeType());
6981 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6982 child_is_deref_of_parent = false;
6983 bool tmp_child_is_deref_of_parent = false;
6984 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6985 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6986 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6987 child_bitfield_bit_size, child_bitfield_bit_offset,
6988 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6989 language_flags);
6990 } else {
6991 const char *parent_name =
6992 valobj ? valobj->GetName().GetCString() : NULL;
6993 if (parent_name) {
6994 child_name.assign(1, '&');
6995 child_name += parent_name;
6996 }
6997
6998 // We have a pointer to an simple type
6999 if (idx == 0) {
7000 child_byte_size = pointee_clang_type.GetByteSize(
7001 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
7002 child_byte_offset = 0;
7003 return pointee_clang_type;
7004 }
7005 }
7006 }
7007 break;
7008
7009 case clang::Type::Typedef: {
7010 CompilerType typedefed_clang_type(
7011 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)
7012 ->getDecl()
7013 ->getUnderlyingType());
7014 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
7015 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7016 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7017 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7018 child_is_deref_of_parent, valobj, language_flags);
7019 } break;
7020
7021 case clang::Type::Auto: {
7022 CompilerType elaborated_clang_type(
7023 getASTContext(),
7024 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
7025 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
7026 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7027 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7028 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7029 child_is_deref_of_parent, valobj, language_flags);
7030 }
7031
7032 case clang::Type::Elaborated: {
7033 CompilerType elaborated_clang_type(
7034 getASTContext(),
7035 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
7036 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
7037 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7038 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7039 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7040 child_is_deref_of_parent, valobj, language_flags);
7041 }
7042
7043 case clang::Type::Paren: {
7044 CompilerType paren_clang_type(
7045 getASTContext(),
7046 llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
7047 return paren_clang_type.GetChildCompilerTypeAtIndex(
7048 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7049 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7050 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7051 child_is_deref_of_parent, valobj, language_flags);
7052 }
7053
7054 default:
7055 break;
7056 }
7057 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007058}
7059
Kate Stoneb9c1b512016-09-06 20:57:50 +00007060static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
7061 const clang::CXXBaseSpecifier *base_spec,
7062 bool omit_empty_base_classes) {
7063 uint32_t child_idx = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007064
Kate Stoneb9c1b512016-09-06 20:57:50 +00007065 const clang::CXXRecordDecl *cxx_record_decl =
7066 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7067
7068 // const char *super_name = record_decl->getNameAsCString();
7069 // const char *base_name =
7070 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
7071 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
7072 //
7073 if (cxx_record_decl) {
7074 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
7075 for (base_class = cxx_record_decl->bases_begin(),
7076 base_class_end = cxx_record_decl->bases_end();
7077 base_class != base_class_end; ++base_class) {
7078 if (omit_empty_base_classes) {
7079 if (BaseSpecifierIsEmpty(base_class))
7080 continue;
7081 }
7082
7083 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
7084 // super_name, base_name,
7085 // child_idx,
7086 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
7087 //
7088 //
7089 if (base_class == base_spec)
7090 return child_idx;
7091 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00007092 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007093 }
7094
7095 return UINT32_MAX;
7096}
7097
7098static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
7099 clang::NamedDecl *canonical_decl,
7100 bool omit_empty_base_classes) {
7101 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
7102 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
7103 omit_empty_base_classes);
7104
7105 clang::RecordDecl::field_iterator field, field_end;
7106 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
7107 field != field_end; ++field, ++child_idx) {
7108 if (field->getCanonicalDecl() == canonical_decl)
7109 return child_idx;
7110 }
7111
7112 return UINT32_MAX;
Greg Claytond8d4a572015-08-11 21:38:15 +00007113}
7114
7115// Look for a child member (doesn't include base classes, but it does include
Adrian Prantl05097242018-04-30 16:49:04 +00007116// their members) in the type hierarchy. Returns an index path into
7117// "clang_type" on how to reach the appropriate member.
Greg Claytond8d4a572015-08-11 21:38:15 +00007118//
7119// class A
7120// {
7121// public:
7122// int m_a;
7123// int m_b;
7124// };
7125//
7126// class B
7127// {
7128// };
7129//
7130// class C :
7131// public B,
7132// public A
7133// {
7134// };
7135//
7136// If we have a clang type that describes "class C", and we wanted to looked
7137// "m_b" in it:
7138//
Kate Stoneb9c1b512016-09-06 20:57:50 +00007139// With omit_empty_base_classes == false we would get an integer array back
Adrian Prantl05097242018-04-30 16:49:04 +00007140// with: { 1, 1 } The first index 1 is the child index for "class A" within
7141// class C The second index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007142//
Adrian Prantl05097242018-04-30 16:49:04 +00007143// With omit_empty_base_classes == true we would get an integer array back
7144// with: { 0, 1 } The first index 0 is the child index for "class A" within
7145// class C (since class B doesn't have any members it doesn't count) The second
7146// index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007147
Kate Stoneb9c1b512016-09-06 20:57:50 +00007148size_t ClangASTContext::GetIndexOfChildMemberWithName(
7149 lldb::opaque_compiler_type_t type, const char *name,
7150 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
7151 if (type && name && name[0]) {
7152 clang::QualType qual_type(GetCanonicalQualType(type));
7153 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7154 switch (type_class) {
7155 case clang::Type::Record:
7156 if (GetCompleteType(type)) {
7157 const clang::RecordType *record_type =
7158 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7159 const clang::RecordDecl *record_decl = record_type->getDecl();
Enrico Granata36f51e42015-12-18 22:41:25 +00007160
Kate Stoneb9c1b512016-09-06 20:57:50 +00007161 assert(record_decl);
7162 uint32_t child_idx = 0;
7163
7164 const clang::CXXRecordDecl *cxx_record_decl =
7165 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7166
7167 // Try and find a field that matches NAME
7168 clang::RecordDecl::field_iterator field, field_end;
7169 llvm::StringRef name_sref(name);
7170 for (field = record_decl->field_begin(),
7171 field_end = record_decl->field_end();
7172 field != field_end; ++field, ++child_idx) {
7173 llvm::StringRef field_name = field->getName();
7174 if (field_name.empty()) {
7175 CompilerType field_type(getASTContext(), field->getType());
7176 child_indexes.push_back(child_idx);
7177 if (field_type.GetIndexOfChildMemberWithName(
7178 name, omit_empty_base_classes, child_indexes))
7179 return child_indexes.size();
7180 child_indexes.pop_back();
7181
7182 } else if (field_name.equals(name_sref)) {
7183 // We have to add on the number of base classes to this index!
7184 child_indexes.push_back(
7185 child_idx + ClangASTContext::GetNumBaseClasses(
7186 cxx_record_decl, omit_empty_base_classes));
7187 return child_indexes.size();
7188 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007189 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007190
Kate Stoneb9c1b512016-09-06 20:57:50 +00007191 if (cxx_record_decl) {
7192 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7193
7194 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7195
7196 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7197 // Didn't find things easily, lets let clang do its thang...
7198 clang::IdentifierInfo &ident_ref =
7199 getASTContext()->Idents.get(name_sref);
7200 clang::DeclarationName decl_name(&ident_ref);
7201
7202 clang::CXXBasePaths paths;
7203 if (cxx_record_decl->lookupInBases(
7204 [decl_name](const clang::CXXBaseSpecifier *specifier,
7205 clang::CXXBasePath &path) {
7206 return clang::CXXRecordDecl::FindOrdinaryMember(
7207 specifier, path, decl_name);
7208 },
7209 paths)) {
7210 clang::CXXBasePaths::const_paths_iterator path,
7211 path_end = paths.end();
7212 for (path = paths.begin(); path != path_end; ++path) {
7213 const size_t num_path_elements = path->size();
7214 for (size_t e = 0; e < num_path_elements; ++e) {
7215 clang::CXXBasePathElement elem = (*path)[e];
7216
7217 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7218 omit_empty_base_classes);
7219 if (child_idx == UINT32_MAX) {
7220 child_indexes.clear();
7221 return 0;
7222 } else {
7223 child_indexes.push_back(child_idx);
7224 parent_record_decl = llvm::cast<clang::RecordDecl>(
7225 elem.Base->getType()
7226 ->getAs<clang::RecordType>()
7227 ->getDecl());
7228 }
7229 }
7230 for (clang::NamedDecl *path_decl : path->Decls) {
7231 child_idx = GetIndexForRecordChild(
7232 parent_record_decl, path_decl, 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 }
7239 }
7240 }
7241 return child_indexes.size();
7242 }
7243 }
7244 }
7245 break;
7246
7247 case clang::Type::ObjCObject:
7248 case clang::Type::ObjCInterface:
7249 if (GetCompleteType(type)) {
7250 llvm::StringRef name_sref(name);
7251 const clang::ObjCObjectType *objc_class_type =
7252 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7253 assert(objc_class_type);
7254 if (objc_class_type) {
7255 uint32_t child_idx = 0;
7256 clang::ObjCInterfaceDecl *class_interface_decl =
7257 objc_class_type->getInterface();
7258
7259 if (class_interface_decl) {
7260 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7261 ivar_end = class_interface_decl->ivar_end();
7262 clang::ObjCInterfaceDecl *superclass_interface_decl =
7263 class_interface_decl->getSuperClass();
7264
7265 for (ivar_pos = class_interface_decl->ivar_begin();
7266 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7267 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7268
7269 if (ivar_decl->getName().equals(name_sref)) {
7270 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7271 (omit_empty_base_classes &&
7272 ObjCDeclHasIVars(superclass_interface_decl, true)))
7273 ++child_idx;
7274
7275 child_indexes.push_back(child_idx);
7276 return child_indexes.size();
7277 }
7278 }
7279
7280 if (superclass_interface_decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00007281 // The super class index is always zero for ObjC classes, so we
7282 // push it onto the child indexes in case we find an ivar in our
7283 // superclass...
Kate Stoneb9c1b512016-09-06 20:57:50 +00007284 child_indexes.push_back(0);
7285
7286 CompilerType superclass_clang_type(
7287 getASTContext(), getASTContext()->getObjCInterfaceType(
7288 superclass_interface_decl));
7289 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7290 name, omit_empty_base_classes, child_indexes)) {
Adrian Prantl05097242018-04-30 16:49:04 +00007291 // We did find an ivar in a superclass so just return the
7292 // results!
Kate Stoneb9c1b512016-09-06 20:57:50 +00007293 return child_indexes.size();
7294 }
7295
Adrian Prantl05097242018-04-30 16:49:04 +00007296 // We didn't find an ivar matching "name" in our superclass, pop
7297 // the superclass zero index that we pushed on above.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007298 child_indexes.pop_back();
7299 }
7300 }
7301 }
7302 }
7303 break;
7304
7305 case clang::Type::ObjCObjectPointer: {
7306 CompilerType objc_object_clang_type(
7307 getASTContext(),
7308 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7309 ->getPointeeType());
7310 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7311 name, omit_empty_base_classes, child_indexes);
7312 } break;
7313
7314 case clang::Type::ConstantArray: {
7315 // const clang::ConstantArrayType *array =
7316 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7317 // const uint64_t element_count =
7318 // array->getSize().getLimitedValue();
7319 //
7320 // if (idx < element_count)
7321 // {
7322 // std::pair<uint64_t, unsigned> field_type_info =
7323 // ast->getTypeInfo(array->getElementType());
7324 //
7325 // char element_name[32];
7326 // ::snprintf (element_name, sizeof (element_name),
7327 // "%s[%u]", parent_name ? parent_name : "", idx);
7328 //
7329 // child_name.assign(element_name);
7330 // assert(field_type_info.first % 8 == 0);
7331 // child_byte_size = field_type_info.first / 8;
7332 // child_byte_offset = idx * child_byte_size;
7333 // return array->getElementType().getAsOpaquePtr();
7334 // }
7335 } break;
7336
7337 // case clang::Type::MemberPointerType:
7338 // {
7339 // MemberPointerType *mem_ptr_type =
7340 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7341 // clang::QualType pointee_type =
7342 // mem_ptr_type->getPointeeType();
7343 //
7344 // if (ClangASTContext::IsAggregateType
7345 // (pointee_type.getAsOpaquePtr()))
7346 // {
7347 // return GetIndexOfChildWithName (ast,
7348 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7349 // name);
7350 // }
7351 // }
7352 // break;
7353 //
7354 case clang::Type::LValueReference:
7355 case clang::Type::RValueReference: {
7356 const clang::ReferenceType *reference_type =
7357 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7358 clang::QualType pointee_type(reference_type->getPointeeType());
7359 CompilerType pointee_clang_type(getASTContext(), pointee_type);
7360
7361 if (pointee_clang_type.IsAggregateType()) {
7362 return pointee_clang_type.GetIndexOfChildMemberWithName(
7363 name, omit_empty_base_classes, child_indexes);
7364 }
7365 } break;
7366
7367 case clang::Type::Pointer: {
7368 CompilerType pointee_clang_type(GetPointeeType(type));
7369
7370 if (pointee_clang_type.IsAggregateType()) {
7371 return pointee_clang_type.GetIndexOfChildMemberWithName(
7372 name, omit_empty_base_classes, child_indexes);
7373 }
7374 } break;
7375
7376 case clang::Type::Typedef:
7377 return CompilerType(getASTContext(),
7378 llvm::cast<clang::TypedefType>(qual_type)
7379 ->getDecl()
7380 ->getUnderlyingType())
7381 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7382 child_indexes);
7383
7384 case clang::Type::Auto:
7385 return CompilerType(
7386 getASTContext(),
7387 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7388 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7389 child_indexes);
7390
7391 case clang::Type::Elaborated:
7392 return CompilerType(
7393 getASTContext(),
7394 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7395 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7396 child_indexes);
7397
7398 case clang::Type::Paren:
7399 return CompilerType(getASTContext(),
7400 llvm::cast<clang::ParenType>(qual_type)->desugar())
7401 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7402 child_indexes);
7403
7404 default:
7405 break;
7406 }
7407 }
7408 return 0;
7409}
Greg Claytond8d4a572015-08-11 21:38:15 +00007410
7411// Get the index of the child of "clang_type" whose name matches. This function
7412// doesn't descend into the children, but only looks one level deep and name
7413// matches can include base class names.
7414
7415uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007416ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7417 const char *name,
7418 bool omit_empty_base_classes) {
7419 if (type && name && name[0]) {
7420 clang::QualType qual_type(GetCanonicalQualType(type));
Enrico Granata36f51e42015-12-18 22:41:25 +00007421
Kate Stoneb9c1b512016-09-06 20:57:50 +00007422 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7423
7424 switch (type_class) {
7425 case clang::Type::Record:
7426 if (GetCompleteType(type)) {
7427 const clang::RecordType *record_type =
7428 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7429 const clang::RecordDecl *record_decl = record_type->getDecl();
7430
7431 assert(record_decl);
7432 uint32_t child_idx = 0;
7433
7434 const clang::CXXRecordDecl *cxx_record_decl =
7435 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7436
7437 if (cxx_record_decl) {
7438 clang::CXXRecordDecl::base_class_const_iterator base_class,
7439 base_class_end;
7440 for (base_class = cxx_record_decl->bases_begin(),
7441 base_class_end = cxx_record_decl->bases_end();
7442 base_class != base_class_end; ++base_class) {
7443 // Skip empty base classes
7444 clang::CXXRecordDecl *base_class_decl =
7445 llvm::cast<clang::CXXRecordDecl>(
7446 base_class->getType()
7447 ->getAs<clang::RecordType>()
7448 ->getDecl());
7449 if (omit_empty_base_classes &&
7450 ClangASTContext::RecordHasFields(base_class_decl) == false)
7451 continue;
7452
7453 CompilerType base_class_clang_type(getASTContext(),
7454 base_class->getType());
7455 std::string base_class_type_name(
7456 base_class_clang_type.GetTypeName().AsCString(""));
7457 if (base_class_type_name.compare(name) == 0)
7458 return child_idx;
7459 ++child_idx;
7460 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007461 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007462
Kate Stoneb9c1b512016-09-06 20:57:50 +00007463 // Try and find a field that matches NAME
7464 clang::RecordDecl::field_iterator field, field_end;
7465 llvm::StringRef name_sref(name);
7466 for (field = record_decl->field_begin(),
7467 field_end = record_decl->field_end();
7468 field != field_end; ++field, ++child_idx) {
7469 if (field->getName().equals(name_sref))
7470 return child_idx;
7471 }
7472 }
7473 break;
7474
7475 case clang::Type::ObjCObject:
7476 case clang::Type::ObjCInterface:
7477 if (GetCompleteType(type)) {
7478 llvm::StringRef name_sref(name);
7479 const clang::ObjCObjectType *objc_class_type =
7480 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7481 assert(objc_class_type);
7482 if (objc_class_type) {
7483 uint32_t child_idx = 0;
7484 clang::ObjCInterfaceDecl *class_interface_decl =
7485 objc_class_type->getInterface();
7486
7487 if (class_interface_decl) {
7488 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7489 ivar_end = class_interface_decl->ivar_end();
7490 clang::ObjCInterfaceDecl *superclass_interface_decl =
7491 class_interface_decl->getSuperClass();
7492
7493 for (ivar_pos = class_interface_decl->ivar_begin();
7494 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7495 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7496
7497 if (ivar_decl->getName().equals(name_sref)) {
7498 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7499 (omit_empty_base_classes &&
7500 ObjCDeclHasIVars(superclass_interface_decl, true)))
7501 ++child_idx;
7502
7503 return child_idx;
7504 }
7505 }
7506
7507 if (superclass_interface_decl) {
7508 if (superclass_interface_decl->getName().equals(name_sref))
7509 return 0;
7510 }
7511 }
7512 }
7513 }
7514 break;
7515
7516 case clang::Type::ObjCObjectPointer: {
7517 CompilerType pointee_clang_type(
7518 getASTContext(),
7519 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7520 ->getPointeeType());
7521 return pointee_clang_type.GetIndexOfChildWithName(
7522 name, omit_empty_base_classes);
7523 } break;
7524
7525 case clang::Type::ConstantArray: {
7526 // const clang::ConstantArrayType *array =
7527 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7528 // const uint64_t element_count =
7529 // array->getSize().getLimitedValue();
7530 //
7531 // if (idx < element_count)
7532 // {
7533 // std::pair<uint64_t, unsigned> field_type_info =
7534 // ast->getTypeInfo(array->getElementType());
7535 //
7536 // char element_name[32];
7537 // ::snprintf (element_name, sizeof (element_name),
7538 // "%s[%u]", parent_name ? parent_name : "", idx);
7539 //
7540 // child_name.assign(element_name);
7541 // assert(field_type_info.first % 8 == 0);
7542 // child_byte_size = field_type_info.first / 8;
7543 // child_byte_offset = idx * child_byte_size;
7544 // return array->getElementType().getAsOpaquePtr();
7545 // }
7546 } break;
7547
7548 // case clang::Type::MemberPointerType:
7549 // {
7550 // MemberPointerType *mem_ptr_type =
7551 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7552 // clang::QualType pointee_type =
7553 // mem_ptr_type->getPointeeType();
7554 //
7555 // if (ClangASTContext::IsAggregateType
7556 // (pointee_type.getAsOpaquePtr()))
7557 // {
7558 // return GetIndexOfChildWithName (ast,
7559 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7560 // name);
7561 // }
7562 // }
7563 // break;
7564 //
7565 case clang::Type::LValueReference:
7566 case clang::Type::RValueReference: {
7567 const clang::ReferenceType *reference_type =
7568 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7569 CompilerType pointee_type(getASTContext(),
7570 reference_type->getPointeeType());
7571
7572 if (pointee_type.IsAggregateType()) {
7573 return pointee_type.GetIndexOfChildWithName(name,
7574 omit_empty_base_classes);
7575 }
7576 } break;
7577
7578 case clang::Type::Pointer: {
7579 const clang::PointerType *pointer_type =
7580 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7581 CompilerType pointee_type(getASTContext(),
7582 pointer_type->getPointeeType());
7583
7584 if (pointee_type.IsAggregateType()) {
7585 return pointee_type.GetIndexOfChildWithName(name,
7586 omit_empty_base_classes);
7587 } else {
7588 // if (parent_name)
7589 // {
7590 // child_name.assign(1, '*');
7591 // child_name += parent_name;
7592 // }
7593 //
7594 // // We have a pointer to an simple type
7595 // if (idx == 0)
7596 // {
7597 // std::pair<uint64_t, unsigned> clang_type_info
7598 // = ast->getTypeInfo(pointee_type);
7599 // assert(clang_type_info.first % 8 == 0);
7600 // child_byte_size = clang_type_info.first / 8;
7601 // child_byte_offset = 0;
7602 // return pointee_type.getAsOpaquePtr();
7603 // }
7604 }
7605 } break;
7606
7607 case clang::Type::Auto:
7608 return CompilerType(
7609 getASTContext(),
7610 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7611 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7612
7613 case clang::Type::Elaborated:
7614 return CompilerType(
7615 getASTContext(),
7616 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7617 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7618
7619 case clang::Type::Paren:
7620 return CompilerType(getASTContext(),
7621 llvm::cast<clang::ParenType>(qual_type)->desugar())
7622 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7623
7624 case clang::Type::Typedef:
7625 return CompilerType(getASTContext(),
7626 llvm::cast<clang::TypedefType>(qual_type)
7627 ->getDecl()
7628 ->getUnderlyingType())
7629 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7630
7631 default:
7632 break;
7633 }
7634 }
7635 return UINT32_MAX;
7636}
Greg Claytond8d4a572015-08-11 21:38:15 +00007637
7638size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007639ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7640 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007641 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007642
Kate Stoneb9c1b512016-09-06 20:57:50 +00007643 clang::QualType qual_type(GetCanonicalQualType(type));
7644 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7645 switch (type_class) {
7646 case clang::Type::Record:
7647 if (GetCompleteType(type)) {
7648 const clang::CXXRecordDecl *cxx_record_decl =
7649 qual_type->getAsCXXRecordDecl();
7650 if (cxx_record_decl) {
7651 const clang::ClassTemplateSpecializationDecl *template_decl =
7652 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7653 cxx_record_decl);
7654 if (template_decl)
7655 return template_decl->getTemplateArgs().size();
7656 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007657 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007658 break;
7659
7660 case clang::Type::Typedef:
7661 return (CompilerType(getASTContext(),
7662 llvm::cast<clang::TypedefType>(qual_type)
7663 ->getDecl()
7664 ->getUnderlyingType()))
7665 .GetNumTemplateArguments();
7666
7667 case clang::Type::Auto:
7668 return (CompilerType(
7669 getASTContext(),
7670 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7671 .GetNumTemplateArguments();
7672
7673 case clang::Type::Elaborated:
7674 return (CompilerType(
7675 getASTContext(),
7676 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7677 .GetNumTemplateArguments();
7678
7679 case clang::Type::Paren:
7680 return (CompilerType(getASTContext(),
7681 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7682 .GetNumTemplateArguments();
7683
7684 default:
7685 break;
7686 }
7687
7688 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007689}
7690
Pavel Labath769b21e2017-11-13 14:26:21 +00007691const clang::ClassTemplateSpecializationDecl *
7692ClangASTContext::GetAsTemplateSpecialization(
7693 lldb::opaque_compiler_type_t type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00007694 if (!type)
Pavel Labath769b21e2017-11-13 14:26:21 +00007695 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007696
7697 clang::QualType qual_type(GetCanonicalQualType(type));
7698 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7699 switch (type_class) {
Pavel Labath769b21e2017-11-13 14:26:21 +00007700 case clang::Type::Record: {
7701 if (! GetCompleteType(type))
7702 return nullptr;
7703 const clang::CXXRecordDecl *cxx_record_decl =
7704 qual_type->getAsCXXRecordDecl();
7705 if (!cxx_record_decl)
7706 return nullptr;
7707 return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7708 cxx_record_decl);
7709 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007710
7711 case clang::Type::Typedef:
Pavel Labath769b21e2017-11-13 14:26:21 +00007712 return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type)
7713 ->getDecl()
7714 ->getUnderlyingType()
7715 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007716
7717 case clang::Type::Auto:
Pavel Labath769b21e2017-11-13 14:26:21 +00007718 return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type)
7719 ->getDeducedType()
7720 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007721
7722 case clang::Type::Elaborated:
Pavel Labath769b21e2017-11-13 14:26:21 +00007723 return GetAsTemplateSpecialization(
7724 llvm::cast<clang::ElaboratedType>(qual_type)
7725 ->getNamedType()
7726 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007727
7728 case clang::Type::Paren:
Pavel Labath769b21e2017-11-13 14:26:21 +00007729 return GetAsTemplateSpecialization(
7730 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007731
7732 default:
Pavel Labath769b21e2017-11-13 14:26:21 +00007733 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007734 }
Pavel Labath769b21e2017-11-13 14:26:21 +00007735}
7736
7737lldb::TemplateArgumentKind
7738ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
7739 size_t arg_idx) {
7740 const clang::ClassTemplateSpecializationDecl *template_decl =
7741 GetAsTemplateSpecialization(type);
7742 if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size())
7743 return eTemplateArgumentKindNull;
7744
7745 switch (template_decl->getTemplateArgs()[arg_idx].getKind()) {
7746 case clang::TemplateArgument::Null:
7747 return eTemplateArgumentKindNull;
7748
7749 case clang::TemplateArgument::NullPtr:
7750 return eTemplateArgumentKindNullPtr;
7751
7752 case clang::TemplateArgument::Type:
7753 return eTemplateArgumentKindType;
7754
7755 case clang::TemplateArgument::Declaration:
7756 return eTemplateArgumentKindDeclaration;
7757
7758 case clang::TemplateArgument::Integral:
7759 return eTemplateArgumentKindIntegral;
7760
7761 case clang::TemplateArgument::Template:
7762 return eTemplateArgumentKindTemplate;
7763
7764 case clang::TemplateArgument::TemplateExpansion:
7765 return eTemplateArgumentKindTemplateExpansion;
7766
7767 case clang::TemplateArgument::Expression:
7768 return eTemplateArgumentKindExpression;
7769
7770 case clang::TemplateArgument::Pack:
7771 return eTemplateArgumentKindPack;
7772 }
7773 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
7774}
7775
7776CompilerType
7777ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
7778 size_t idx) {
7779 const clang::ClassTemplateSpecializationDecl *template_decl =
7780 GetAsTemplateSpecialization(type);
7781 if (!template_decl || idx >= template_decl->getTemplateArgs().size())
7782 return CompilerType();
7783
7784 const clang::TemplateArgument &template_arg =
7785 template_decl->getTemplateArgs()[idx];
7786 if (template_arg.getKind() != clang::TemplateArgument::Type)
7787 return CompilerType();
7788
7789 return CompilerType(getASTContext(), template_arg.getAsType());
7790}
7791
Pavel Labathf59056f2017-11-30 10:16:54 +00007792llvm::Optional<CompilerType::IntegralTemplateArgument>
Pavel Labath769b21e2017-11-13 14:26:21 +00007793ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
7794 size_t idx) {
7795 const clang::ClassTemplateSpecializationDecl *template_decl =
7796 GetAsTemplateSpecialization(type);
7797 if (! template_decl || idx >= template_decl->getTemplateArgs().size())
Pavel Labathf59056f2017-11-30 10:16:54 +00007798 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007799
7800 const clang::TemplateArgument &template_arg =
7801 template_decl->getTemplateArgs()[idx];
7802 if (template_arg.getKind() != clang::TemplateArgument::Integral)
Pavel Labathf59056f2017-11-30 10:16:54 +00007803 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007804
Pavel Labathf59056f2017-11-30 10:16:54 +00007805 return {{template_arg.getAsIntegral(),
7806 CompilerType(getASTContext(), template_arg.getIntegralType())}};
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007807}
7808
Kate Stoneb9c1b512016-09-06 20:57:50 +00007809CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7810 if (type)
7811 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7812 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007813}
7814
Kate Stoneb9c1b512016-09-06 20:57:50 +00007815clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7816 const clang::EnumType *enutype =
7817 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7818 if (enutype)
7819 return enutype->getDecl();
7820 return NULL;
7821}
7822
7823clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7824 const clang::RecordType *record_type =
7825 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7826 if (record_type)
7827 return record_type->getDecl();
7828 return nullptr;
7829}
7830
7831clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
7832 clang::QualType qual_type = ClangUtil::GetCanonicalQualType(type);
7833 if (qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00007834 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007835 else
7836 return qual_type->getAsTagDecl();
Greg Claytone6b36cd2015-12-08 01:02:08 +00007837}
7838
Aleksandr Urakov709426b2018-09-10 08:08:43 +00007839clang::TypedefNameDecl *
7840ClangASTContext::GetAsTypedefDecl(const CompilerType &type) {
7841 const clang::TypedefType *typedef_type =
7842 llvm::dyn_cast<clang::TypedefType>(ClangUtil::GetQualType(type));
7843 if (typedef_type)
7844 return typedef_type->getDecl();
7845 return nullptr;
7846}
7847
Greg Claytond8d4a572015-08-11 21:38:15 +00007848clang::CXXRecordDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007849ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7850 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
Greg Claytond8d4a572015-08-11 21:38:15 +00007851}
7852
7853clang::ObjCInterfaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007854ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7855 const clang::ObjCObjectType *objc_class_type =
7856 llvm::dyn_cast<clang::ObjCObjectType>(
7857 ClangUtil::GetCanonicalQualType(type));
7858 if (objc_class_type)
7859 return objc_class_type->getInterface();
7860 return nullptr;
7861}
7862
7863clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007864 const CompilerType &type, llvm::StringRef name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00007865 const CompilerType &field_clang_type, AccessType access,
7866 uint32_t bitfield_bit_size) {
7867 if (!type.IsValid() || !field_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00007868 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007869 ClangASTContext *ast =
7870 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7871 if (!ast)
7872 return nullptr;
7873 clang::ASTContext *clang_ast = ast->getASTContext();
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007874 clang::IdentifierInfo *ident = nullptr;
7875 if (!name.empty())
7876 ident = &clang_ast->Idents.get(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00007877
7878 clang::FieldDecl *field = nullptr;
7879
7880 clang::Expr *bit_width = nullptr;
7881 if (bitfield_bit_size != 0) {
7882 llvm::APInt bitfield_bit_size_apint(
7883 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7884 bit_width = new (*clang_ast)
7885 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7886 clang_ast->IntTy, clang::SourceLocation());
7887 }
7888
7889 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7890 if (record_decl) {
7891 field = clang::FieldDecl::Create(
7892 *clang_ast, record_decl, clang::SourceLocation(),
7893 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007894 ident, // Identifier
7895 ClangUtil::GetQualType(field_clang_type), // Field type
7896 nullptr, // TInfo *
7897 bit_width, // BitWidth
7898 false, // Mutable
7899 clang::ICIS_NoInit); // HasInit
Kate Stoneb9c1b512016-09-06 20:57:50 +00007900
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007901 if (name.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +00007902 // Determine whether this field corresponds to an anonymous struct or
7903 // union.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007904 if (const clang::TagType *TagT =
7905 field->getType()->getAs<clang::TagType>()) {
7906 if (clang::RecordDecl *Rec =
7907 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7908 if (!Rec->getDeclName()) {
7909 Rec->setAnonymousStructOrUnion(true);
7910 field->setImplicit();
7911 }
7912 }
7913 }
7914
7915 if (field) {
7916 field->setAccess(
7917 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7918
7919 record_decl->addDecl(field);
7920
7921#ifdef LLDB_CONFIGURATION_DEBUG
7922 VerifyDecl(field);
7923#endif
7924 }
7925 } else {
7926 clang::ObjCInterfaceDecl *class_interface_decl =
7927 ast->GetAsObjCInterfaceDecl(type);
7928
7929 if (class_interface_decl) {
7930 const bool is_synthesized = false;
7931
7932 field_clang_type.GetCompleteType();
7933
7934 field = clang::ObjCIvarDecl::Create(
7935 *clang_ast, class_interface_decl, clang::SourceLocation(),
7936 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007937 ident, // Identifier
7938 ClangUtil::GetQualType(field_clang_type), // Field type
7939 nullptr, // TypeSourceInfo *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007940 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7941 is_synthesized);
7942
7943 if (field) {
7944 class_interface_decl->addDecl(field);
7945
7946#ifdef LLDB_CONFIGURATION_DEBUG
7947 VerifyDecl(field);
7948#endif
7949 }
7950 }
7951 }
7952 return field;
Greg Claytond8d4a572015-08-11 21:38:15 +00007953}
7954
Kate Stoneb9c1b512016-09-06 20:57:50 +00007955void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7956 if (!type)
7957 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007958
Kate Stoneb9c1b512016-09-06 20:57:50 +00007959 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7960 if (!ast)
7961 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007962
Kate Stoneb9c1b512016-09-06 20:57:50 +00007963 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007964
Kate Stoneb9c1b512016-09-06 20:57:50 +00007965 if (!record_decl)
7966 return;
7967
7968 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7969
7970 IndirectFieldVector indirect_fields;
7971 clang::RecordDecl::field_iterator field_pos;
7972 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7973 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7974 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7975 last_field_pos = field_pos++) {
7976 if (field_pos->isAnonymousStructOrUnion()) {
7977 clang::QualType field_qual_type = field_pos->getType();
7978
7979 const clang::RecordType *field_record_type =
7980 field_qual_type->getAs<clang::RecordType>();
7981
7982 if (!field_record_type)
7983 continue;
7984
7985 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7986
7987 if (!field_record_decl)
7988 continue;
7989
7990 for (clang::RecordDecl::decl_iterator
7991 di = field_record_decl->decls_begin(),
7992 de = field_record_decl->decls_end();
7993 di != de; ++di) {
7994 if (clang::FieldDecl *nested_field_decl =
7995 llvm::dyn_cast<clang::FieldDecl>(*di)) {
7996 clang::NamedDecl **chain =
7997 new (*ast->getASTContext()) clang::NamedDecl *[2];
7998 chain[0] = *field_pos;
7999 chain[1] = nested_field_decl;
8000 clang::IndirectFieldDecl *indirect_field =
8001 clang::IndirectFieldDecl::Create(
8002 *ast->getASTContext(), record_decl, clang::SourceLocation(),
8003 nested_field_decl->getIdentifier(),
8004 nested_field_decl->getType(), {chain, 2});
8005
8006 indirect_field->setImplicit();
8007
8008 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
8009 field_pos->getAccess(), nested_field_decl->getAccess()));
8010
8011 indirect_fields.push_back(indirect_field);
8012 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
8013 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
8014 size_t nested_chain_size =
8015 nested_indirect_field_decl->getChainingSize();
8016 clang::NamedDecl **chain = new (*ast->getASTContext())
8017 clang::NamedDecl *[nested_chain_size + 1];
8018 chain[0] = *field_pos;
8019
8020 int chain_index = 1;
8021 for (clang::IndirectFieldDecl::chain_iterator
8022 nci = nested_indirect_field_decl->chain_begin(),
8023 nce = nested_indirect_field_decl->chain_end();
8024 nci < nce; ++nci) {
8025 chain[chain_index] = *nci;
8026 chain_index++;
8027 }
8028
8029 clang::IndirectFieldDecl *indirect_field =
8030 clang::IndirectFieldDecl::Create(
8031 *ast->getASTContext(), record_decl, clang::SourceLocation(),
8032 nested_indirect_field_decl->getIdentifier(),
8033 nested_indirect_field_decl->getType(),
8034 {chain, nested_chain_size + 1});
8035
8036 indirect_field->setImplicit();
8037
8038 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
8039 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
8040
8041 indirect_fields.push_back(indirect_field);
Greg Claytond8d4a572015-08-11 21:38:15 +00008042 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008043 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008044 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008045 }
8046
Adrian Prantl05097242018-04-30 16:49:04 +00008047 // Check the last field to see if it has an incomplete array type as its last
8048 // member and if it does, the tell the record decl about it
Kate Stoneb9c1b512016-09-06 20:57:50 +00008049 if (last_field_pos != field_end_pos) {
8050 if (last_field_pos->getType()->isIncompleteArrayType())
8051 record_decl->hasFlexibleArrayMember();
8052 }
8053
8054 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
8055 ife = indirect_fields.end();
8056 ifi < ife; ++ifi) {
8057 record_decl->addDecl(*ifi);
8058 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008059}
8060
Kate Stoneb9c1b512016-09-06 20:57:50 +00008061void ClangASTContext::SetIsPacked(const CompilerType &type) {
8062 if (type) {
8063 ClangASTContext *ast =
8064 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8065 if (ast) {
8066 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
8067
8068 if (!record_decl)
Greg Claytonf73034f2015-09-08 18:15:05 +00008069 return;
8070
Kate Stoneb9c1b512016-09-06 20:57:50 +00008071 record_decl->addAttr(
8072 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
Greg Claytond8d4a572015-08-11 21:38:15 +00008073 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008074 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008075}
8076
Kate Stoneb9c1b512016-09-06 20:57:50 +00008077clang::VarDecl *ClangASTContext::AddVariableToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008078 const CompilerType &type, llvm::StringRef name,
8079 const CompilerType &var_type, AccessType access) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00008080 if (!type.IsValid() || !var_type.IsValid())
8081 return nullptr;
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008082
Kate Stoneb9c1b512016-09-06 20:57:50 +00008083 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8084 if (!ast)
8085 return nullptr;
8086
8087 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008088 if (!record_decl)
8089 return nullptr;
8090
8091 clang::VarDecl *var_decl = nullptr;
8092 clang::IdentifierInfo *ident = nullptr;
8093 if (!name.empty())
8094 ident = &ast->getASTContext()->Idents.get(name);
8095
8096 var_decl = clang::VarDecl::Create(
8097 *ast->getASTContext(), // ASTContext &
8098 record_decl, // DeclContext *
8099 clang::SourceLocation(), // clang::SourceLocation StartLoc
8100 clang::SourceLocation(), // clang::SourceLocation IdLoc
8101 ident, // clang::IdentifierInfo *
8102 ClangUtil::GetQualType(var_type), // Variable clang::QualType
8103 nullptr, // TypeSourceInfo *
8104 clang::SC_Static); // StorageClass
8105 if (!var_decl)
8106 return nullptr;
8107
8108 var_decl->setAccess(
8109 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
8110 record_decl->addDecl(var_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008111
Greg Claytond8d4a572015-08-11 21:38:15 +00008112#ifdef LLDB_CONFIGURATION_DEBUG
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008113 VerifyDecl(var_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008114#endif
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008115
Kate Stoneb9c1b512016-09-06 20:57:50 +00008116 return var_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008117}
8118
Kate Stoneb9c1b512016-09-06 20:57:50 +00008119clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
Davide Italiano675767a2018-03-27 19:40:50 +00008120 lldb::opaque_compiler_type_t type, const char *name, const char *mangled_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008121 const CompilerType &method_clang_type, lldb::AccessType access,
8122 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
8123 bool is_attr_used, bool is_artificial) {
8124 if (!type || !method_clang_type.IsValid() || name == nullptr ||
8125 name[0] == '\0')
8126 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008127
Kate Stoneb9c1b512016-09-06 20:57:50 +00008128 clang::QualType record_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008129
Kate Stoneb9c1b512016-09-06 20:57:50 +00008130 clang::CXXRecordDecl *cxx_record_decl =
8131 record_qual_type->getAsCXXRecordDecl();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008132
Kate Stoneb9c1b512016-09-06 20:57:50 +00008133 if (cxx_record_decl == nullptr)
8134 return nullptr;
8135
8136 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8137
8138 clang::CXXMethodDecl *cxx_method_decl = nullptr;
8139
8140 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
8141
8142 const clang::FunctionType *function_type =
8143 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
8144
8145 if (function_type == nullptr)
8146 return nullptr;
8147
8148 const clang::FunctionProtoType *method_function_prototype(
8149 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
8150
8151 if (!method_function_prototype)
8152 return nullptr;
8153
8154 unsigned int num_params = method_function_prototype->getNumParams();
8155
8156 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
8157 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
8158
8159 if (is_artificial)
8160 return nullptr; // skip everything artificial
8161
8162 if (name[0] == '~') {
8163 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
8164 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8165 clang::DeclarationNameInfo(
8166 getASTContext()->DeclarationNames.getCXXDestructorName(
8167 getASTContext()->getCanonicalType(record_qual_type)),
8168 clang::SourceLocation()),
8169 method_qual_type, nullptr, is_inline, is_artificial);
8170 cxx_method_decl = cxx_dtor_decl;
8171 } else if (decl_name == cxx_record_decl->getDeclName()) {
8172 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
8173 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8174 clang::DeclarationNameInfo(
8175 getASTContext()->DeclarationNames.getCXXConstructorName(
8176 getASTContext()->getCanonicalType(record_qual_type)),
8177 clang::SourceLocation()),
8178 method_qual_type,
8179 nullptr, // TypeSourceInfo *
8180 is_explicit, is_inline, is_artificial, false /*is_constexpr*/);
8181 cxx_method_decl = cxx_ctor_decl;
8182 } else {
8183 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
8184 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
8185
8186 if (IsOperator(name, op_kind)) {
8187 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
Adrian Prantl05097242018-04-30 16:49:04 +00008188 // Check the number of operator parameters. Sometimes we have seen bad
8189 // DWARF that doesn't correctly describe operators and if we try to
8190 // create a method and add it to the class, clang will assert and
8191 // crash, so we need to make sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00008192 const bool is_method = true;
8193 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
8194 is_method, op_kind, num_params))
8195 return nullptr;
8196 cxx_method_decl = clang::CXXMethodDecl::Create(
8197 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8198 clang::DeclarationNameInfo(
8199 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
8200 clang::SourceLocation()),
8201 method_qual_type,
8202 nullptr, // TypeSourceInfo *
8203 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
8204 } else if (num_params == 0) {
8205 // Conversion operators don't take params...
8206 cxx_method_decl = clang::CXXConversionDecl::Create(
8207 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8208 clang::DeclarationNameInfo(
8209 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
8210 getASTContext()->getCanonicalType(
8211 function_type->getReturnType())),
8212 clang::SourceLocation()),
8213 method_qual_type,
8214 nullptr, // TypeSourceInfo *
8215 is_inline, is_explicit, false /*is_constexpr*/,
8216 clang::SourceLocation());
8217 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008218 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008219
8220 if (cxx_method_decl == nullptr) {
8221 cxx_method_decl = clang::CXXMethodDecl::Create(
8222 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8223 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8224 method_qual_type,
8225 nullptr, // TypeSourceInfo *
8226 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
Greg Claytond8d4a572015-08-11 21:38:15 +00008227 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008228 }
8229
8230 clang::AccessSpecifier access_specifier =
8231 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8232
8233 cxx_method_decl->setAccess(access_specifier);
8234 cxx_method_decl->setVirtualAsWritten(is_virtual);
8235
8236 if (is_attr_used)
8237 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8238
Davide Italiano675767a2018-03-27 19:40:50 +00008239 if (mangled_name != NULL) {
8240 cxx_method_decl->addAttr(
8241 clang::AsmLabelAttr::CreateImplicit(*getASTContext(), mangled_name));
8242 }
8243
Kate Stoneb9c1b512016-09-06 20:57:50 +00008244 // Populate the method decl with parameter decls
8245
8246 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8247
8248 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8249 params.push_back(clang::ParmVarDecl::Create(
8250 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8251 clang::SourceLocation(),
8252 nullptr, // anonymous
8253 method_function_prototype->getParamType(param_index), nullptr,
8254 clang::SC_None, nullptr));
8255 }
8256
8257 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8258
8259 cxx_record_decl->addDecl(cxx_method_decl);
8260
8261 // Sometimes the debug info will mention a constructor (default/copy/move),
8262 // destructor, or assignment operator (copy/move) but there won't be any
8263 // version of this in the code. So we check if the function was artificially
8264 // generated and if it is trivial and this lets the compiler/backend know
8265 // that it can inline the IR for these when it needs to and we can avoid a
8266 // "missing function" error when running expressions.
8267
8268 if (is_artificial) {
8269 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8270 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8271 (cxx_ctor_decl->isCopyConstructor() &&
8272 cxx_record_decl->hasTrivialCopyConstructor()) ||
8273 (cxx_ctor_decl->isMoveConstructor() &&
8274 cxx_record_decl->hasTrivialMoveConstructor()))) {
8275 cxx_ctor_decl->setDefaulted();
8276 cxx_ctor_decl->setTrivial(true);
8277 } else if (cxx_dtor_decl) {
8278 if (cxx_record_decl->hasTrivialDestructor()) {
8279 cxx_dtor_decl->setDefaulted();
8280 cxx_dtor_decl->setTrivial(true);
8281 }
8282 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8283 cxx_record_decl->hasTrivialCopyAssignment()) ||
8284 (cxx_method_decl->isMoveAssignmentOperator() &&
8285 cxx_record_decl->hasTrivialMoveAssignment())) {
8286 cxx_method_decl->setDefaulted();
8287 cxx_method_decl->setTrivial(true);
Greg Claytond8d4a572015-08-11 21:38:15 +00008288 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008289 }
8290
Greg Claytond8d4a572015-08-11 21:38:15 +00008291#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008292 VerifyDecl(cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008293#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008294
Kate Stoneb9c1b512016-09-06 20:57:50 +00008295 // printf ("decl->isPolymorphic() = %i\n",
8296 // cxx_record_decl->isPolymorphic());
8297 // printf ("decl->isAggregate() = %i\n",
8298 // cxx_record_decl->isAggregate());
8299 // printf ("decl->isPOD() = %i\n",
8300 // cxx_record_decl->isPOD());
8301 // printf ("decl->isEmpty() = %i\n",
8302 // cxx_record_decl->isEmpty());
8303 // printf ("decl->isAbstract() = %i\n",
8304 // cxx_record_decl->isAbstract());
8305 // printf ("decl->hasTrivialConstructor() = %i\n",
8306 // cxx_record_decl->hasTrivialConstructor());
8307 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8308 // cxx_record_decl->hasTrivialCopyConstructor());
8309 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8310 // cxx_record_decl->hasTrivialCopyAssignment());
8311 // printf ("decl->hasTrivialDestructor() = %i\n",
8312 // cxx_record_decl->hasTrivialDestructor());
8313 return cxx_method_decl;
8314}
Greg Claytond8d4a572015-08-11 21:38:15 +00008315
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00008316void ClangASTContext::AddMethodOverridesForCXXRecordType(
8317 lldb::opaque_compiler_type_t type) {
8318 if (auto *record = GetAsCXXRecordDecl(type))
8319 for (auto *method : record->methods())
8320 addOverridesForMethod(method);
8321}
8322
Greg Claytond8d4a572015-08-11 21:38:15 +00008323#pragma mark C++ Base Classes
8324
Zachary Turner970f38e2018-10-25 20:44:56 +00008325std::unique_ptr<clang::CXXBaseSpecifier>
Kate Stoneb9c1b512016-09-06 20:57:50 +00008326ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8327 AccessType access, bool is_virtual,
8328 bool base_of_class) {
Zachary Turner970f38e2018-10-25 20:44:56 +00008329 if (!type)
8330 return nullptr;
8331
8332 return llvm::make_unique<clang::CXXBaseSpecifier>(
8333 clang::SourceRange(), is_virtual, base_of_class,
8334 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8335 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8336 clang::SourceLocation());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008337}
8338
Zachary Turner970f38e2018-10-25 20:44:56 +00008339bool ClangASTContext::TransferBaseClasses(
Kate Stoneb9c1b512016-09-06 20:57:50 +00008340 lldb::opaque_compiler_type_t type,
Zachary Turner970f38e2018-10-25 20:44:56 +00008341 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases) {
8342 if (!type)
8343 return false;
8344 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8345 if (!cxx_record_decl)
8346 return false;
8347 std::vector<clang::CXXBaseSpecifier *> raw_bases;
8348 raw_bases.reserve(bases.size());
8349
8350 // Clang will make a copy of them, so it's ok that we pass pointers that we're
8351 // about to destroy.
8352 for (auto &b : bases)
8353 raw_bases.push_back(b.get());
8354 cxx_record_decl->setBases(raw_bases.data(), raw_bases.size());
8355 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008356}
8357
8358bool ClangASTContext::SetObjCSuperClass(
8359 const CompilerType &type, const CompilerType &superclass_clang_type) {
8360 ClangASTContext *ast =
8361 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8362 if (!ast)
8363 return false;
8364 clang::ASTContext *clang_ast = ast->getASTContext();
8365
8366 if (type && superclass_clang_type.IsValid() &&
8367 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8368 clang::ObjCInterfaceDecl *class_interface_decl =
8369 GetAsObjCInterfaceDecl(type);
8370 clang::ObjCInterfaceDecl *super_interface_decl =
8371 GetAsObjCInterfaceDecl(superclass_clang_type);
8372 if (class_interface_decl && super_interface_decl) {
8373 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8374 clang_ast->getObjCInterfaceType(super_interface_decl)));
8375 return true;
8376 }
8377 }
8378 return false;
8379}
8380
8381bool ClangASTContext::AddObjCClassProperty(
8382 const CompilerType &type, const char *property_name,
8383 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8384 const char *property_setter_name, const char *property_getter_name,
8385 uint32_t property_attributes, ClangASTMetadata *metadata) {
8386 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8387 property_name[0] == '\0')
8388 return false;
8389 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8390 if (!ast)
8391 return false;
8392 clang::ASTContext *clang_ast = ast->getASTContext();
8393
8394 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8395
8396 if (class_interface_decl) {
8397 CompilerType property_clang_type_to_access;
8398
8399 if (property_clang_type.IsValid())
8400 property_clang_type_to_access = property_clang_type;
8401 else if (ivar_decl)
8402 property_clang_type_to_access =
8403 CompilerType(clang_ast, ivar_decl->getType());
8404
8405 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8406 clang::TypeSourceInfo *prop_type_source;
8407 if (ivar_decl)
8408 prop_type_source =
8409 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8410 else
8411 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8412 ClangUtil::GetQualType(property_clang_type));
8413
8414 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8415 *clang_ast, class_interface_decl,
8416 clang::SourceLocation(), // Source Location
8417 &clang_ast->Idents.get(property_name),
8418 clang::SourceLocation(), // Source Location for AT
8419 clang::SourceLocation(), // Source location for (
8420 ivar_decl ? ivar_decl->getType()
8421 : ClangUtil::GetQualType(property_clang_type),
8422 prop_type_source);
8423
8424 if (property_decl) {
8425 if (metadata)
8426 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8427
8428 class_interface_decl->addDecl(property_decl);
8429
8430 clang::Selector setter_sel, getter_sel;
8431
8432 if (property_setter_name != nullptr) {
8433 std::string property_setter_no_colon(
8434 property_setter_name, strlen(property_setter_name) - 1);
8435 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008436 &clang_ast->Idents.get(property_setter_no_colon);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008437 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8438 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8439 std::string setter_sel_string("set");
8440 setter_sel_string.push_back(::toupper(property_name[0]));
8441 setter_sel_string.append(&property_name[1]);
8442 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008443 &clang_ast->Idents.get(setter_sel_string);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008444 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8445 }
8446 property_decl->setSetterName(setter_sel);
8447 property_decl->setPropertyAttributes(
8448 clang::ObjCPropertyDecl::OBJC_PR_setter);
8449
8450 if (property_getter_name != nullptr) {
8451 clang::IdentifierInfo *getter_ident =
8452 &clang_ast->Idents.get(property_getter_name);
8453 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8454 } else {
8455 clang::IdentifierInfo *getter_ident =
8456 &clang_ast->Idents.get(property_name);
8457 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8458 }
8459 property_decl->setGetterName(getter_sel);
8460 property_decl->setPropertyAttributes(
8461 clang::ObjCPropertyDecl::OBJC_PR_getter);
8462
8463 if (ivar_decl)
8464 property_decl->setPropertyIvarDecl(ivar_decl);
8465
8466 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8467 property_decl->setPropertyAttributes(
8468 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8469 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8470 property_decl->setPropertyAttributes(
8471 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8472 if (property_attributes & DW_APPLE_PROPERTY_assign)
8473 property_decl->setPropertyAttributes(
8474 clang::ObjCPropertyDecl::OBJC_PR_assign);
8475 if (property_attributes & DW_APPLE_PROPERTY_retain)
8476 property_decl->setPropertyAttributes(
8477 clang::ObjCPropertyDecl::OBJC_PR_retain);
8478 if (property_attributes & DW_APPLE_PROPERTY_copy)
8479 property_decl->setPropertyAttributes(
8480 clang::ObjCPropertyDecl::OBJC_PR_copy);
8481 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8482 property_decl->setPropertyAttributes(
8483 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8484 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8485 property_decl->setPropertyAttributes(
8486 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8487 if (property_attributes &
8488 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8489 property_decl->setPropertyAttributes(
8490 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8491 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8492 property_decl->setPropertyAttributes(
8493 clang::ObjCPropertyDecl::OBJC_PR_class);
8494
8495 const bool isInstance =
8496 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8497
8498 if (!getter_sel.isNull() &&
8499 !(isInstance
8500 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8501 : class_interface_decl->lookupClassMethod(getter_sel))) {
8502 const bool isVariadic = false;
8503 const bool isSynthesized = false;
8504 const bool isImplicitlyDeclared = true;
8505 const bool isDefined = false;
8506 const clang::ObjCMethodDecl::ImplementationControl impControl =
8507 clang::ObjCMethodDecl::None;
8508 const bool HasRelatedResultType = false;
8509
8510 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8511 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8512 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8513 nullptr, class_interface_decl, isInstance, isVariadic,
8514 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8515 HasRelatedResultType);
8516
8517 if (getter && metadata)
8518 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8519
8520 if (getter) {
8521 getter->setMethodParams(*clang_ast,
8522 llvm::ArrayRef<clang::ParmVarDecl *>(),
8523 llvm::ArrayRef<clang::SourceLocation>());
8524
8525 class_interface_decl->addDecl(getter);
8526 }
8527 }
8528
8529 if (!setter_sel.isNull() &&
8530 !(isInstance
8531 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8532 : class_interface_decl->lookupClassMethod(setter_sel))) {
8533 clang::QualType result_type = clang_ast->VoidTy;
8534 const bool isVariadic = false;
8535 const bool isSynthesized = false;
8536 const bool isImplicitlyDeclared = true;
8537 const bool isDefined = false;
8538 const clang::ObjCMethodDecl::ImplementationControl impControl =
8539 clang::ObjCMethodDecl::None;
8540 const bool HasRelatedResultType = false;
8541
8542 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8543 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8544 setter_sel, result_type, nullptr, class_interface_decl,
8545 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8546 isDefined, impControl, HasRelatedResultType);
8547
8548 if (setter && metadata)
8549 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8550
8551 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8552
8553 params.push_back(clang::ParmVarDecl::Create(
8554 *clang_ast, setter, clang::SourceLocation(),
8555 clang::SourceLocation(),
8556 nullptr, // anonymous
8557 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8558 clang::SC_Auto, nullptr));
8559
8560 if (setter) {
8561 setter->setMethodParams(
8562 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8563 llvm::ArrayRef<clang::SourceLocation>());
8564
8565 class_interface_decl->addDecl(setter);
8566 }
8567 }
8568
8569 return true;
8570 }
8571 }
8572 }
8573 return false;
8574}
8575
8576bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8577 bool check_superclass) {
8578 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8579 if (class_interface_decl)
8580 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8581 return false;
8582}
8583
8584clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8585 const CompilerType &type,
8586 const char *name, // the full symbol name as seen in the symbol table
8587 // (lldb::opaque_compiler_type_t type, "-[NString
8588 // stringWithCString:]")
8589 const CompilerType &method_clang_type, lldb::AccessType access,
8590 bool is_artificial, bool is_variadic) {
8591 if (!type || !method_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008592 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008593
Kate Stoneb9c1b512016-09-06 20:57:50 +00008594 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8595
8596 if (class_interface_decl == nullptr)
8597 return nullptr;
8598 ClangASTContext *lldb_ast =
8599 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8600 if (lldb_ast == nullptr)
8601 return nullptr;
8602 clang::ASTContext *ast = lldb_ast->getASTContext();
8603
8604 const char *selector_start = ::strchr(name, ' ');
8605 if (selector_start == nullptr)
8606 return nullptr;
8607
8608 selector_start++;
8609 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8610
8611 size_t len = 0;
8612 const char *start;
8613 // printf ("name = '%s'\n", name);
8614
8615 unsigned num_selectors_with_args = 0;
8616 for (start = selector_start; start && *start != '\0' && *start != ']';
8617 start += len) {
8618 len = ::strcspn(start, ":]");
8619 bool has_arg = (start[len] == ':');
8620 if (has_arg)
8621 ++num_selectors_with_args;
8622 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8623 if (has_arg)
8624 len += 1;
8625 }
8626
8627 if (selector_idents.size() == 0)
8628 return nullptr;
8629
8630 clang::Selector method_selector = ast->Selectors.getSelector(
8631 num_selectors_with_args ? selector_idents.size() : 0,
8632 selector_idents.data());
8633
8634 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8635
8636 // Populate the method decl with parameter decls
8637 const clang::Type *method_type(method_qual_type.getTypePtr());
8638
8639 if (method_type == nullptr)
8640 return nullptr;
8641
8642 const clang::FunctionProtoType *method_function_prototype(
8643 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8644
8645 if (!method_function_prototype)
8646 return nullptr;
8647
8648 bool is_synthesized = false;
8649 bool is_defined = false;
8650 clang::ObjCMethodDecl::ImplementationControl imp_control =
8651 clang::ObjCMethodDecl::None;
8652
8653 const unsigned num_args = method_function_prototype->getNumParams();
8654
8655 if (num_args != num_selectors_with_args)
8656 return nullptr; // some debug information is corrupt. We are not going to
8657 // deal with it.
8658
8659 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8660 *ast,
8661 clang::SourceLocation(), // beginLoc,
8662 clang::SourceLocation(), // endLoc,
8663 method_selector, method_function_prototype->getReturnType(),
8664 nullptr, // TypeSourceInfo *ResultTInfo,
8665 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8666 ClangUtil::GetQualType(type)),
8667 name[0] == '-', is_variadic, is_synthesized,
8668 true, // is_implicitly_declared; we force this to true because we don't
8669 // have source locations
8670 is_defined, imp_control, false /*has_related_result_type*/);
8671
8672 if (objc_method_decl == nullptr)
8673 return nullptr;
8674
8675 if (num_args > 0) {
8676 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8677
8678 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8679 params.push_back(clang::ParmVarDecl::Create(
8680 *ast, objc_method_decl, clang::SourceLocation(),
8681 clang::SourceLocation(),
8682 nullptr, // anonymous
8683 method_function_prototype->getParamType(param_index), nullptr,
8684 clang::SC_Auto, nullptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00008685 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008686
Kate Stoneb9c1b512016-09-06 20:57:50 +00008687 objc_method_decl->setMethodParams(
8688 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8689 llvm::ArrayRef<clang::SourceLocation>());
8690 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008691
Kate Stoneb9c1b512016-09-06 20:57:50 +00008692 class_interface_decl->addDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008693
Greg Claytond8d4a572015-08-11 21:38:15 +00008694#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008695 VerifyDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008696#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00008697
8698 return objc_method_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008699}
8700
Kate Stoneb9c1b512016-09-06 20:57:50 +00008701bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8702 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00008703 return false;
Greg Claytone6b36cd2015-12-08 01:02:08 +00008704
Kate Stoneb9c1b512016-09-06 20:57:50 +00008705 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008706
Kate Stoneb9c1b512016-09-06 20:57:50 +00008707 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8708 switch (type_class) {
8709 case clang::Type::Record: {
8710 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8711 if (cxx_record_decl)
8712 return cxx_record_decl->hasExternalLexicalStorage() ||
8713 cxx_record_decl->hasExternalVisibleStorage();
8714 } break;
Enrico Granata36f51e42015-12-18 22:41:25 +00008715
Kate Stoneb9c1b512016-09-06 20:57:50 +00008716 case clang::Type::Enum: {
8717 clang::EnumDecl *enum_decl =
8718 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8719 if (enum_decl)
8720 return enum_decl->hasExternalLexicalStorage() ||
8721 enum_decl->hasExternalVisibleStorage();
8722 } break;
8723
8724 case clang::Type::ObjCObject:
8725 case clang::Type::ObjCInterface: {
8726 const clang::ObjCObjectType *objc_class_type =
8727 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8728 assert(objc_class_type);
8729 if (objc_class_type) {
8730 clang::ObjCInterfaceDecl *class_interface_decl =
8731 objc_class_type->getInterface();
8732
8733 if (class_interface_decl)
8734 return class_interface_decl->hasExternalLexicalStorage() ||
8735 class_interface_decl->hasExternalVisibleStorage();
Greg Claytond8d4a572015-08-11 21:38:15 +00008736 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008737 } break;
8738
8739 case clang::Type::Typedef:
8740 return GetHasExternalStorage(CompilerType(
8741 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8742 ->getDecl()
8743 ->getUnderlyingType()
8744 .getAsOpaquePtr()));
8745
8746 case clang::Type::Auto:
8747 return GetHasExternalStorage(CompilerType(
8748 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8749 ->getDeducedType()
8750 .getAsOpaquePtr()));
8751
8752 case clang::Type::Elaborated:
8753 return GetHasExternalStorage(CompilerType(
8754 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8755 ->getNamedType()
8756 .getAsOpaquePtr()));
8757
8758 case clang::Type::Paren:
8759 return GetHasExternalStorage(CompilerType(
8760 type.GetTypeSystem(),
8761 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8762
8763 default:
8764 break;
8765 }
8766 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008767}
8768
Kate Stoneb9c1b512016-09-06 20:57:50 +00008769bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8770 bool has_extern) {
8771 if (!type)
8772 return false;
8773
8774 clang::QualType qual_type(GetCanonicalQualType(type));
8775
8776 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8777 switch (type_class) {
8778 case clang::Type::Record: {
8779 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8780 if (cxx_record_decl) {
8781 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8782 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8783 return true;
8784 }
8785 } break;
8786
8787 case clang::Type::Enum: {
8788 clang::EnumDecl *enum_decl =
8789 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8790 if (enum_decl) {
8791 enum_decl->setHasExternalLexicalStorage(has_extern);
8792 enum_decl->setHasExternalVisibleStorage(has_extern);
8793 return true;
8794 }
8795 } break;
8796
8797 case clang::Type::ObjCObject:
8798 case clang::Type::ObjCInterface: {
8799 const clang::ObjCObjectType *objc_class_type =
8800 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8801 assert(objc_class_type);
8802 if (objc_class_type) {
8803 clang::ObjCInterfaceDecl *class_interface_decl =
8804 objc_class_type->getInterface();
8805
8806 if (class_interface_decl) {
8807 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8808 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8809 return true;
8810 }
8811 }
8812 } break;
8813
8814 case clang::Type::Typedef:
8815 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8816 ->getDecl()
8817 ->getUnderlyingType()
8818 .getAsOpaquePtr(),
8819 has_extern);
8820
8821 case clang::Type::Auto:
8822 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8823 ->getDeducedType()
8824 .getAsOpaquePtr(),
8825 has_extern);
8826
8827 case clang::Type::Elaborated:
8828 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8829 ->getNamedType()
8830 .getAsOpaquePtr(),
8831 has_extern);
8832
8833 case clang::Type::Paren:
8834 return SetHasExternalStorage(
8835 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8836 has_extern);
8837
8838 default:
8839 break;
8840 }
8841 return false;
8842}
Greg Claytond8d4a572015-08-11 21:38:15 +00008843
8844#pragma mark TagDecl
8845
Kate Stoneb9c1b512016-09-06 20:57:50 +00008846bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8847 clang::QualType qual_type(ClangUtil::GetQualType(type));
8848 if (!qual_type.isNull()) {
8849 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8850 if (tag_type) {
8851 clang::TagDecl *tag_decl = tag_type->getDecl();
8852 if (tag_decl) {
8853 tag_decl->startDefinition();
8854 return true;
8855 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008856 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008857
8858 const clang::ObjCObjectType *object_type =
8859 qual_type->getAs<clang::ObjCObjectType>();
8860 if (object_type) {
8861 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8862 if (interface_decl) {
8863 interface_decl->startDefinition();
8864 return true;
8865 }
8866 }
8867 }
8868 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008869}
8870
Kate Stoneb9c1b512016-09-06 20:57:50 +00008871bool ClangASTContext::CompleteTagDeclarationDefinition(
8872 const CompilerType &type) {
8873 clang::QualType qual_type(ClangUtil::GetQualType(type));
8874 if (!qual_type.isNull()) {
8875 // Make sure we use the same methodology as
Adrian Prantl05097242018-04-30 16:49:04 +00008876 // ClangASTContext::StartTagDeclarationDefinition() as to how we start/end
8877 // the definition. Previously we were calling
Kate Stoneb9c1b512016-09-06 20:57:50 +00008878 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8879 if (tag_type) {
8880 clang::TagDecl *tag_decl = tag_type->getDecl();
8881 if (tag_decl) {
8882 clang::CXXRecordDecl *cxx_record_decl =
8883 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8884
8885 if (cxx_record_decl) {
8886 if (!cxx_record_decl->isCompleteDefinition())
8887 cxx_record_decl->completeDefinition();
8888 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8889 cxx_record_decl->setHasExternalLexicalStorage(false);
8890 cxx_record_decl->setHasExternalVisibleStorage(false);
8891 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008892 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008893 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008894 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008895
8896 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8897
8898 if (enutype) {
8899 clang::EnumDecl *enum_decl = enutype->getDecl();
8900
8901 if (enum_decl) {
8902 if (!enum_decl->isCompleteDefinition()) {
8903 ClangASTContext *lldb_ast =
8904 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8905 if (lldb_ast == nullptr)
8906 return false;
8907 clang::ASTContext *ast = lldb_ast->getASTContext();
8908
8909 /// TODO This really needs to be fixed.
8910
8911 QualType integer_type(enum_decl->getIntegerType());
8912 if (!integer_type.isNull()) {
8913 unsigned NumPositiveBits = 1;
8914 unsigned NumNegativeBits = 0;
8915
8916 clang::QualType promotion_qual_type;
8917 // If the enum integer type is less than an integer in bit width,
8918 // then we must promote it to an integer size.
8919 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8920 ast->getTypeSize(ast->IntTy)) {
8921 if (enum_decl->getIntegerType()->isSignedIntegerType())
8922 promotion_qual_type = ast->IntTy;
8923 else
8924 promotion_qual_type = ast->UnsignedIntTy;
8925 } else
8926 promotion_qual_type = enum_decl->getIntegerType();
8927
8928 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8929 promotion_qual_type, NumPositiveBits,
8930 NumNegativeBits);
8931 }
8932 }
8933 return true;
8934 }
8935 }
8936 }
8937 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008938}
8939
Aleksandr Urakov709426b2018-09-10 08:08:43 +00008940clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008941 const CompilerType &enum_type, const Declaration &decl, const char *name,
8942 int64_t enum_value, uint32_t enum_value_bit_size) {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008943
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008944 if (!enum_type || ConstString(name).IsEmpty())
8945 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008946
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008947 lldbassert(enum_type.GetTypeSystem() == static_cast<TypeSystem *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +00008948
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008949 lldb::opaque_compiler_type_t enum_opaque_compiler_type =
8950 enum_type.GetOpaqueQualType();
8951
8952 if (!enum_opaque_compiler_type)
8953 return nullptr;
8954
8955 CompilerType underlying_type =
8956 GetEnumerationIntegerType(enum_type.GetOpaqueQualType());
8957
8958 clang::QualType enum_qual_type(
8959 GetCanonicalQualType(enum_opaque_compiler_type));
8960
8961 bool is_signed = false;
8962 underlying_type.IsIntegerType(is_signed);
8963 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8964
8965 if (!clang_type)
8966 return nullptr;
8967
8968 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8969
8970 if (!enutype)
8971 return nullptr;
8972
8973 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
8974 enum_llvm_apsint = enum_value;
8975 clang::EnumConstantDecl *enumerator_decl = clang::EnumConstantDecl::Create(
8976 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8977 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier
8978 clang::QualType(enutype, 0), nullptr, enum_llvm_apsint);
8979
8980 if (!enumerator_decl)
8981 return nullptr;
8982
8983 enutype->getDecl()->addDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008984
8985#ifdef LLDB_CONFIGURATION_DEBUG
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008986 VerifyDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008987#endif
8988
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008989 return enumerator_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008990}
8991
Greg Claytona1e5dc82015-08-11 22:53:00 +00008992CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00008993ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
8994 clang::QualType enum_qual_type(GetCanonicalQualType(type));
8995 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8996 if (clang_type) {
8997 const clang::EnumType *enutype =
8998 llvm::dyn_cast<clang::EnumType>(clang_type);
8999 if (enutype) {
9000 clang::EnumDecl *enum_decl = enutype->getDecl();
9001 if (enum_decl)
9002 return CompilerType(getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00009003 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009004 }
9005 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00009006}
9007
Kate Stoneb9c1b512016-09-06 20:57:50 +00009008CompilerType
9009ClangASTContext::CreateMemberPointerType(const CompilerType &type,
9010 const CompilerType &pointee_type) {
9011 if (type && pointee_type.IsValid() &&
9012 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
9013 ClangASTContext *ast =
9014 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
9015 if (!ast)
9016 return CompilerType();
9017 return CompilerType(ast->getASTContext(),
9018 ast->getASTContext()->getMemberPointerType(
9019 ClangUtil::GetQualType(pointee_type),
9020 ClangUtil::GetQualType(type).getTypePtr()));
9021 }
9022 return CompilerType();
9023}
Greg Claytond8d4a572015-08-11 21:38:15 +00009024
9025size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00009026ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
9027 const char *s, uint8_t *dst,
9028 size_t dst_size) {
9029 if (type) {
9030 clang::QualType qual_type(GetCanonicalQualType(type));
9031 uint32_t count = 0;
9032 bool is_complex = false;
9033 if (IsFloatingPointType(type, count, is_complex)) {
9034 // TODO: handle complex and vector types
9035 if (count != 1)
9036 return false;
9037
9038 llvm::StringRef s_sref(s);
9039 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type),
9040 s_sref);
9041
9042 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type);
9043 const uint64_t byte_size = bit_size / 8;
9044 if (dst_size >= byte_size) {
9045 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(
9046 llvm::NextPowerOf2(byte_size) * 8);
Zachary Turner97206d52017-05-12 04:51:55 +00009047 lldb_private::Status get_data_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009048 if (scalar.GetAsMemoryData(dst, byte_size,
9049 lldb_private::endian::InlHostByteOrder(),
9050 get_data_error))
9051 return byte_size;
9052 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009053 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009054 }
9055 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00009056}
9057
Greg Claytond8d4a572015-08-11 21:38:15 +00009058//----------------------------------------------------------------------
9059// Dumping types
9060//----------------------------------------------------------------------
9061#define DEPTH_INCREMENT 2
9062
Zachary Turner49110232018-11-05 17:40:28 +00009063void ClangASTContext::Dump(Stream &s) {
Zachary Turner115209e2018-11-05 19:25:39 +00009064 Decl *tu = Decl::castFromDeclContext(GetTranslationUnitDecl());
Zachary Turner49110232018-11-05 17:40:28 +00009065 tu->dump(s.AsRawOstream());
9066}
9067
Kate Stoneb9c1b512016-09-06 20:57:50 +00009068void ClangASTContext::DumpValue(
9069 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
Zachary Turner29cb8682017-03-03 20:57:05 +00009070 lldb::Format format, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009071 lldb::offset_t data_byte_offset, size_t data_byte_size,
9072 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
9073 bool show_summary, bool verbose, uint32_t depth) {
9074 if (!type)
9075 return;
9076
9077 clang::QualType qual_type(GetQualType(type));
9078 switch (qual_type->getTypeClass()) {
9079 case clang::Type::Record:
9080 if (GetCompleteType(type)) {
9081 const clang::RecordType *record_type =
9082 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9083 const clang::RecordDecl *record_decl = record_type->getDecl();
9084 assert(record_decl);
9085 uint32_t field_bit_offset = 0;
9086 uint32_t field_byte_offset = 0;
9087 const clang::ASTRecordLayout &record_layout =
9088 getASTContext()->getASTRecordLayout(record_decl);
9089 uint32_t child_idx = 0;
9090
9091 const clang::CXXRecordDecl *cxx_record_decl =
9092 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9093 if (cxx_record_decl) {
9094 // We might have base classes to print out first
9095 clang::CXXRecordDecl::base_class_const_iterator base_class,
9096 base_class_end;
9097 for (base_class = cxx_record_decl->bases_begin(),
9098 base_class_end = cxx_record_decl->bases_end();
9099 base_class != base_class_end; ++base_class) {
9100 const clang::CXXRecordDecl *base_class_decl =
9101 llvm::cast<clang::CXXRecordDecl>(
9102 base_class->getType()->getAs<clang::RecordType>()->getDecl());
9103
9104 // Skip empty base classes
9105 if (verbose == false &&
9106 ClangASTContext::RecordHasFields(base_class_decl) == false)
9107 continue;
9108
9109 if (base_class->isVirtual())
9110 field_bit_offset =
9111 record_layout.getVBaseClassOffset(base_class_decl)
9112 .getQuantity() *
9113 8;
9114 else
9115 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
9116 .getQuantity() *
9117 8;
9118 field_byte_offset = field_bit_offset / 8;
9119 assert(field_bit_offset % 8 == 0);
9120 if (child_idx == 0)
9121 s->PutChar('{');
9122 else
9123 s->PutChar(',');
9124
9125 clang::QualType base_class_qual_type = base_class->getType();
9126 std::string base_class_type_name(base_class_qual_type.getAsString());
9127
9128 // Indent and print the base class type name
Zachary Turner827d5d72016-12-16 04:27:00 +00009129 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
9130 base_class_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009131
9132 clang::TypeInfo base_class_type_info =
9133 getASTContext()->getTypeInfo(base_class_qual_type);
9134
9135 // Dump the value of the member
9136 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
9137 base_clang_type.DumpValue(
9138 exe_ctx,
9139 s, // Stream to dump to
9140 base_clang_type
9141 .GetFormat(), // The format with which to display the member
9142 data, // Data buffer containing all bytes for this type
9143 data_byte_offset + field_byte_offset, // Offset into "data" where
9144 // to grab value from
9145 base_class_type_info.Width / 8, // Size of this type in bytes
9146 0, // Bitfield bit size
9147 0, // Bitfield bit offset
9148 show_types, // Boolean indicating if we should show the variable
9149 // types
9150 show_summary, // Boolean indicating if we should show a summary
9151 // for the current type
9152 verbose, // Verbose output?
9153 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9154 // children
9155
9156 ++child_idx;
9157 }
9158 }
9159 uint32_t field_idx = 0;
9160 clang::RecordDecl::field_iterator field, field_end;
9161 for (field = record_decl->field_begin(),
9162 field_end = record_decl->field_end();
9163 field != field_end; ++field, ++field_idx, ++child_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009164 // Print the starting squiggly bracket (if this is the first member) or
9165 // comma (for member 2 and beyond) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009166 if (child_idx == 0)
9167 s->PutChar('{');
9168 else
9169 s->PutChar(',');
9170
9171 // Indent
9172 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
9173
9174 clang::QualType field_type = field->getType();
9175 // Print the member type if requested
Adrian Prantl05097242018-04-30 16:49:04 +00009176 // Figure out the type byte size (field_type_info.first) and alignment
9177 // (field_type_info.second) from the AST context.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009178 clang::TypeInfo field_type_info =
9179 getASTContext()->getTypeInfo(field_type);
9180 assert(field_idx < record_layout.getFieldCount());
9181 // Figure out the field offset within the current struct/union/class
9182 // type
9183 field_bit_offset = record_layout.getFieldOffset(field_idx);
9184 field_byte_offset = field_bit_offset / 8;
9185 uint32_t field_bitfield_bit_size = 0;
9186 uint32_t field_bitfield_bit_offset = 0;
9187 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
9188 field_bitfield_bit_size))
9189 field_bitfield_bit_offset = field_bit_offset % 8;
9190
9191 if (show_types) {
9192 std::string field_type_name(field_type.getAsString());
9193 if (field_bitfield_bit_size > 0)
9194 s->Printf("(%s:%u) ", field_type_name.c_str(),
9195 field_bitfield_bit_size);
9196 else
9197 s->Printf("(%s) ", field_type_name.c_str());
9198 }
9199 // Print the member name and equal sign
9200 s->Printf("%s = ", field->getNameAsString().c_str());
9201
9202 // Dump the value of the member
9203 CompilerType field_clang_type(getASTContext(), field_type);
9204 field_clang_type.DumpValue(
9205 exe_ctx,
9206 s, // Stream to dump to
9207 field_clang_type
9208 .GetFormat(), // The format with which to display the member
9209 data, // Data buffer containing all bytes for this type
9210 data_byte_offset + field_byte_offset, // Offset into "data" where to
9211 // grab value from
9212 field_type_info.Width / 8, // Size of this type in bytes
9213 field_bitfield_bit_size, // Bitfield bit size
9214 field_bitfield_bit_offset, // Bitfield bit offset
9215 show_types, // Boolean indicating if we should show the variable
9216 // types
9217 show_summary, // Boolean indicating if we should show a summary for
9218 // the current type
9219 verbose, // Verbose output?
9220 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9221 // children
9222 }
9223
9224 // Indent the trailing squiggly bracket
9225 if (child_idx > 0)
9226 s->Printf("\n%*s}", depth, "");
9227 }
9228 return;
9229
9230 case clang::Type::Enum:
9231 if (GetCompleteType(type)) {
9232 const clang::EnumType *enutype =
9233 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9234 const clang::EnumDecl *enum_decl = enutype->getDecl();
9235 assert(enum_decl);
9236 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9237 lldb::offset_t offset = data_byte_offset;
9238 const int64_t enum_value = data.GetMaxU64Bitfield(
9239 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
9240 for (enum_pos = enum_decl->enumerator_begin(),
9241 enum_end_pos = enum_decl->enumerator_end();
9242 enum_pos != enum_end_pos; ++enum_pos) {
9243 if (enum_pos->getInitVal() == enum_value) {
9244 s->Printf("%s", enum_pos->getNameAsString().c_str());
9245 return;
9246 }
9247 }
Adrian Prantl05097242018-04-30 16:49:04 +00009248 // If we have gotten here we didn't get find the enumerator in the enum
9249 // decl, so just print the integer.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009250 s->Printf("%" PRIi64, enum_value);
9251 }
9252 return;
9253
9254 case clang::Type::ConstantArray: {
9255 const clang::ConstantArrayType *array =
9256 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9257 bool is_array_of_characters = false;
9258 clang::QualType element_qual_type = array->getElementType();
9259
9260 const clang::Type *canonical_type =
9261 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9262 if (canonical_type)
9263 is_array_of_characters = canonical_type->isCharType();
9264
9265 const uint64_t element_count = array->getSize().getLimitedValue();
9266
9267 clang::TypeInfo field_type_info =
9268 getASTContext()->getTypeInfo(element_qual_type);
9269
9270 uint32_t element_idx = 0;
9271 uint32_t element_offset = 0;
9272 uint64_t element_byte_size = field_type_info.Width / 8;
9273 uint32_t element_stride = element_byte_size;
9274
9275 if (is_array_of_characters) {
9276 s->PutChar('"');
Zachary Turner29cb8682017-03-03 20:57:05 +00009277 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9278 element_byte_size, element_count, UINT32_MAX,
9279 LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009280 s->PutChar('"');
9281 return;
9282 } else {
9283 CompilerType element_clang_type(getASTContext(), element_qual_type);
9284 lldb::Format element_format = element_clang_type.GetFormat();
9285
9286 for (element_idx = 0; element_idx < element_count; ++element_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009287 // Print the starting squiggly bracket (if this is the first member) or
9288 // comman (for member 2 and beyong) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009289 if (element_idx == 0)
9290 s->PutChar('{');
9291 else
9292 s->PutChar(',');
9293
9294 // Indent and print the index
9295 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9296
9297 // Figure out the field offset within the current struct/union/class
9298 // type
9299 element_offset = element_idx * element_stride;
9300
9301 // Dump the value of the member
9302 element_clang_type.DumpValue(
9303 exe_ctx,
9304 s, // Stream to dump to
9305 element_format, // The format with which to display the element
9306 data, // Data buffer containing all bytes for this type
9307 data_byte_offset +
9308 element_offset, // Offset into "data" where to grab value from
9309 element_byte_size, // Size of this type in bytes
9310 0, // Bitfield bit size
9311 0, // Bitfield bit offset
9312 show_types, // Boolean indicating if we should show the variable
9313 // types
9314 show_summary, // Boolean indicating if we should show a summary for
9315 // the current type
9316 verbose, // Verbose output?
9317 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9318 // children
9319 }
9320
9321 // Indent the trailing squiggly bracket
9322 if (element_idx > 0)
9323 s->Printf("\n%*s}", depth, "");
9324 }
9325 }
9326 return;
9327
9328 case clang::Type::Typedef: {
9329 clang::QualType typedef_qual_type =
9330 llvm::cast<clang::TypedefType>(qual_type)
9331 ->getDecl()
9332 ->getUnderlyingType();
9333
9334 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9335 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9336 clang::TypeInfo typedef_type_info =
9337 getASTContext()->getTypeInfo(typedef_qual_type);
9338 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9339
9340 return typedef_clang_type.DumpValue(
9341 exe_ctx,
9342 s, // Stream to dump to
9343 typedef_format, // The format with which to display the element
9344 data, // Data buffer containing all bytes for this type
9345 data_byte_offset, // Offset into "data" where to grab value from
9346 typedef_byte_size, // Size of this type in bytes
9347 bitfield_bit_size, // Bitfield bit size
9348 bitfield_bit_offset, // Bitfield bit offset
9349 show_types, // Boolean indicating if we should show the variable types
9350 show_summary, // Boolean indicating if we should show a summary for the
9351 // current type
9352 verbose, // Verbose output?
9353 depth); // Scope depth for any types that have children
9354 } break;
9355
9356 case clang::Type::Auto: {
9357 clang::QualType elaborated_qual_type =
9358 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9359 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9360 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9361 clang::TypeInfo elaborated_type_info =
9362 getASTContext()->getTypeInfo(elaborated_qual_type);
9363 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9364
9365 return elaborated_clang_type.DumpValue(
9366 exe_ctx,
9367 s, // Stream to dump to
9368 elaborated_format, // The format with which to display the element
9369 data, // Data buffer containing all bytes for this type
9370 data_byte_offset, // Offset into "data" where to grab value from
9371 elaborated_byte_size, // Size of this type in bytes
9372 bitfield_bit_size, // Bitfield bit size
9373 bitfield_bit_offset, // Bitfield bit offset
9374 show_types, // Boolean indicating if we should show the variable types
9375 show_summary, // Boolean indicating if we should show a summary for the
9376 // current type
9377 verbose, // Verbose output?
9378 depth); // Scope depth for any types that have children
9379 } break;
9380
9381 case clang::Type::Elaborated: {
9382 clang::QualType elaborated_qual_type =
9383 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9384 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9385 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9386 clang::TypeInfo elaborated_type_info =
9387 getASTContext()->getTypeInfo(elaborated_qual_type);
9388 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9389
9390 return elaborated_clang_type.DumpValue(
9391 exe_ctx,
9392 s, // Stream to dump to
9393 elaborated_format, // The format with which to display the element
9394 data, // Data buffer containing all bytes for this type
9395 data_byte_offset, // Offset into "data" where to grab value from
9396 elaborated_byte_size, // Size of this type in bytes
9397 bitfield_bit_size, // Bitfield bit size
9398 bitfield_bit_offset, // Bitfield bit offset
9399 show_types, // Boolean indicating if we should show the variable types
9400 show_summary, // Boolean indicating if we should show a summary for the
9401 // current type
9402 verbose, // Verbose output?
9403 depth); // Scope depth for any types that have children
9404 } break;
9405
9406 case clang::Type::Paren: {
9407 clang::QualType desugar_qual_type =
9408 llvm::cast<clang::ParenType>(qual_type)->desugar();
9409 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type);
9410
9411 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9412 clang::TypeInfo desugar_type_info =
9413 getASTContext()->getTypeInfo(desugar_qual_type);
9414 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9415
9416 return desugar_clang_type.DumpValue(
9417 exe_ctx,
9418 s, // Stream to dump to
9419 desugar_format, // The format with which to display the element
9420 data, // Data buffer containing all bytes for this type
9421 data_byte_offset, // Offset into "data" where to grab value from
9422 desugar_byte_size, // Size of this type in bytes
9423 bitfield_bit_size, // Bitfield bit size
9424 bitfield_bit_offset, // Bitfield bit offset
9425 show_types, // Boolean indicating if we should show the variable types
9426 show_summary, // Boolean indicating if we should show a summary for the
9427 // current type
9428 verbose, // Verbose output?
9429 depth); // Scope depth for any types that have children
9430 } break;
9431
9432 default:
9433 // We are down to a scalar type that we just need to display.
Zachary Turner29cb8682017-03-03 20:57:05 +00009434 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9435 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9436 bitfield_bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009437
9438 if (show_summary)
9439 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9440 break;
9441 }
9442}
9443
9444bool ClangASTContext::DumpTypeValue(
9445 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
Zachary Turner29cb8682017-03-03 20:57:05 +00009446 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9447 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009448 ExecutionContextScope *exe_scope) {
9449 if (!type)
9450 return false;
9451 if (IsAggregateType(type)) {
9452 return false;
9453 } else {
Greg Claytond8d4a572015-08-11 21:38:15 +00009454 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009455
9456 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9457 switch (type_class) {
9458 case clang::Type::Typedef: {
9459 clang::QualType typedef_qual_type =
9460 llvm::cast<clang::TypedefType>(qual_type)
9461 ->getDecl()
9462 ->getUnderlyingType();
9463 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9464 if (format == eFormatDefault)
9465 format = typedef_clang_type.GetFormat();
9466 clang::TypeInfo typedef_type_info =
9467 getASTContext()->getTypeInfo(typedef_qual_type);
9468 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9469
9470 return typedef_clang_type.DumpTypeValue(
9471 s,
9472 format, // The format with which to display the element
9473 data, // Data buffer containing all bytes for this type
9474 byte_offset, // Offset into "data" where to grab value from
9475 typedef_byte_size, // Size of this type in bytes
9476 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9477 // treat as a bitfield
9478 bitfield_bit_offset, // Offset in bits of a bitfield value if
9479 // bitfield_bit_size != 0
9480 exe_scope);
9481 } break;
9482
9483 case clang::Type::Enum:
Adrian Prantl05097242018-04-30 16:49:04 +00009484 // If our format is enum or default, show the enumeration value as its
9485 // enumeration string value, else just display it as requested.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009486 if ((format == eFormatEnum || format == eFormatDefault) &&
9487 GetCompleteType(type)) {
9488 const clang::EnumType *enutype =
9489 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9490 const clang::EnumDecl *enum_decl = enutype->getDecl();
9491 assert(enum_decl);
9492 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9493 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9494 lldb::offset_t offset = byte_offset;
9495 if (is_signed) {
9496 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9497 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9498 for (enum_pos = enum_decl->enumerator_begin(),
9499 enum_end_pos = enum_decl->enumerator_end();
9500 enum_pos != enum_end_pos; ++enum_pos) {
9501 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009502 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009503 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009504 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009505 }
9506 // If we have gotten here we didn't get find the enumerator in the
9507 // enum decl, so just print the integer.
9508 s->Printf("%" PRIi64, enum_svalue);
9509 } else {
9510 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9511 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9512 for (enum_pos = enum_decl->enumerator_begin(),
9513 enum_end_pos = enum_decl->enumerator_end();
9514 enum_pos != enum_end_pos; ++enum_pos) {
9515 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009516 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009517 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009518 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009519 }
9520 // If we have gotten here we didn't get find the enumerator in the
9521 // enum decl, so just print the integer.
9522 s->Printf("%" PRIu64, enum_uvalue);
Greg Claytond8d4a572015-08-11 21:38:15 +00009523 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009524 return true;
9525 }
9526 // format was not enum, just fall through and dump the value as
9527 // requested....
9528 LLVM_FALLTHROUGH;
9529
9530 default:
9531 // We are down to a scalar type that we just need to display.
9532 {
9533 uint32_t item_count = 1;
9534 // A few formats, we might need to modify our size and count for
9535 // depending
9536 // on how we are trying to display the value...
9537 switch (format) {
Greg Claytond8d4a572015-08-11 21:38:15 +00009538 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00009539 case eFormatBoolean:
9540 case eFormatBinary:
9541 case eFormatComplex:
9542 case eFormatCString: // NULL terminated C strings
9543 case eFormatDecimal:
9544 case eFormatEnum:
9545 case eFormatHex:
9546 case eFormatHexUppercase:
9547 case eFormatFloat:
9548 case eFormatOctal:
9549 case eFormatOSType:
9550 case eFormatUnsigned:
9551 case eFormatPointer:
9552 case eFormatVectorOfChar:
9553 case eFormatVectorOfSInt8:
9554 case eFormatVectorOfUInt8:
9555 case eFormatVectorOfSInt16:
9556 case eFormatVectorOfUInt16:
9557 case eFormatVectorOfSInt32:
9558 case eFormatVectorOfUInt32:
9559 case eFormatVectorOfSInt64:
9560 case eFormatVectorOfUInt64:
9561 case eFormatVectorOfFloat32:
9562 case eFormatVectorOfFloat64:
9563 case eFormatVectorOfUInt128:
9564 break;
9565
9566 case eFormatChar:
9567 case eFormatCharPrintable:
9568 case eFormatCharArray:
9569 case eFormatBytes:
9570 case eFormatBytesWithASCII:
9571 item_count = byte_size;
9572 byte_size = 1;
9573 break;
9574
9575 case eFormatUnicode16:
9576 item_count = byte_size / 2;
9577 byte_size = 2;
9578 break;
9579
9580 case eFormatUnicode32:
9581 item_count = byte_size / 4;
9582 byte_size = 4;
9583 break;
9584 }
Zachary Turner29cb8682017-03-03 20:57:05 +00009585 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9586 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
9587 bitfield_bit_size, bitfield_bit_offset,
9588 exe_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009589 }
9590 break;
9591 }
9592 }
9593 return 0;
9594}
9595
9596void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9597 ExecutionContext *exe_ctx, Stream *s,
9598 const lldb_private::DataExtractor &data,
9599 lldb::offset_t data_byte_offset,
9600 size_t data_byte_size) {
9601 uint32_t length = 0;
9602 if (IsCStringType(type, length)) {
9603 if (exe_ctx) {
9604 Process *process = exe_ctx->GetProcessPtr();
9605 if (process) {
9606 lldb::offset_t offset = data_byte_offset;
9607 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9608 std::vector<uint8_t> buf;
9609 if (length > 0)
9610 buf.resize(length);
9611 else
9612 buf.resize(256);
9613
Zachary Turner29cb8682017-03-03 20:57:05 +00009614 DataExtractor cstr_data(&buf.front(), buf.size(),
9615 process->GetByteOrder(), 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009616 buf.back() = '\0';
9617 size_t bytes_read;
9618 size_t total_cstr_len = 0;
Zachary Turner97206d52017-05-12 04:51:55 +00009619 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009620 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9621 buf.size(), error)) > 0) {
9622 const size_t len = strlen((const char *)&buf.front());
9623 if (len == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00009624 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009625 if (total_cstr_len == 0)
9626 s->PutCString(" \"");
Zachary Turner29cb8682017-03-03 20:57:05 +00009627 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9628 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009629 total_cstr_len += len;
9630 if (len < buf.size())
9631 break;
9632 pointer_address += total_cstr_len;
Greg Claytond8d4a572015-08-11 21:38:15 +00009633 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009634 if (total_cstr_len > 0)
9635 s->PutChar('"');
9636 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009637 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009638 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009639}
9640
Kate Stoneb9c1b512016-09-06 20:57:50 +00009641void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9642 StreamFile s(stdout, false);
9643 DumpTypeDescription(type, &s);
9644 ClangASTMetadata *metadata =
9645 ClangASTContext::GetMetadata(getASTContext(), type);
9646 if (metadata) {
9647 metadata->Dump(&s);
9648 }
9649}
Greg Claytond8d4a572015-08-11 21:38:15 +00009650
Kate Stoneb9c1b512016-09-06 20:57:50 +00009651void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9652 Stream *s) {
9653 if (type) {
9654 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00009655
Kate Stoneb9c1b512016-09-06 20:57:50 +00009656 llvm::SmallVector<char, 1024> buf;
9657 llvm::raw_svector_ostream llvm_ostrm(buf);
9658
9659 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9660 switch (type_class) {
9661 case clang::Type::ObjCObject:
9662 case clang::Type::ObjCInterface: {
9663 GetCompleteType(type);
9664
9665 const clang::ObjCObjectType *objc_class_type =
9666 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9667 assert(objc_class_type);
9668 if (objc_class_type) {
9669 clang::ObjCInterfaceDecl *class_interface_decl =
9670 objc_class_type->getInterface();
9671 if (class_interface_decl) {
9672 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9673 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
Greg Claytond8d4a572015-08-11 21:38:15 +00009674 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009675 }
9676 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00009677
Kate Stoneb9c1b512016-09-06 20:57:50 +00009678 case clang::Type::Typedef: {
9679 const clang::TypedefType *typedef_type =
9680 qual_type->getAs<clang::TypedefType>();
9681 if (typedef_type) {
9682 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9683 std::string clang_typedef_name(
9684 typedef_decl->getQualifiedNameAsString());
9685 if (!clang_typedef_name.empty()) {
9686 s->PutCString("typedef ");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009687 s->PutCString(clang_typedef_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00009688 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009689 }
9690 } break;
9691
9692 case clang::Type::Auto:
9693 CompilerType(getASTContext(),
9694 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
9695 .DumpTypeDescription(s);
9696 return;
9697
9698 case clang::Type::Elaborated:
9699 CompilerType(getASTContext(),
9700 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
9701 .DumpTypeDescription(s);
9702 return;
9703
9704 case clang::Type::Paren:
9705 CompilerType(getASTContext(),
9706 llvm::cast<clang::ParenType>(qual_type)->desugar())
9707 .DumpTypeDescription(s);
9708 return;
9709
9710 case clang::Type::Record: {
9711 GetCompleteType(type);
9712
9713 const clang::RecordType *record_type =
9714 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9715 const clang::RecordDecl *record_decl = record_type->getDecl();
9716 const clang::CXXRecordDecl *cxx_record_decl =
9717 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9718
9719 if (cxx_record_decl)
9720 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9721 s->GetIndentLevel());
9722 else
9723 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9724 s->GetIndentLevel());
9725 } break;
9726
9727 default: {
9728 const clang::TagType *tag_type =
9729 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9730 if (tag_type) {
9731 clang::TagDecl *tag_decl = tag_type->getDecl();
9732 if (tag_decl)
9733 tag_decl->print(llvm_ostrm, 0);
9734 } else {
9735 std::string clang_type_name(qual_type.getAsString());
9736 if (!clang_type_name.empty())
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009737 s->PutCString(clang_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009738 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009739 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00009740 }
9741
Kate Stoneb9c1b512016-09-06 20:57:50 +00009742 if (buf.size() > 0) {
9743 s->Write(buf.data(), buf.size());
Greg Clayton8b4edba2015-08-14 20:02:05 +00009744 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009745 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00009746}
9747
Kate Stoneb9c1b512016-09-06 20:57:50 +00009748void ClangASTContext::DumpTypeName(const CompilerType &type) {
9749 if (ClangUtil::IsClangType(type)) {
9750 clang::QualType qual_type(
9751 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9752
9753 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9754 switch (type_class) {
9755 case clang::Type::Record: {
9756 const clang::CXXRecordDecl *cxx_record_decl =
9757 qual_type->getAsCXXRecordDecl();
9758 if (cxx_record_decl)
9759 printf("class %s", cxx_record_decl->getName().str().c_str());
9760 } break;
9761
9762 case clang::Type::Enum: {
9763 clang::EnumDecl *enum_decl =
9764 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9765 if (enum_decl) {
9766 printf("enum %s", enum_decl->getName().str().c_str());
9767 }
9768 } break;
9769
9770 case clang::Type::ObjCObject:
9771 case clang::Type::ObjCInterface: {
9772 const clang::ObjCObjectType *objc_class_type =
9773 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9774 if (objc_class_type) {
9775 clang::ObjCInterfaceDecl *class_interface_decl =
9776 objc_class_type->getInterface();
Adrian Prantl05097242018-04-30 16:49:04 +00009777 // We currently can't complete objective C types through the newly
9778 // added ASTContext because it only supports TagDecl objects right
9779 // now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00009780 if (class_interface_decl)
9781 printf("@class %s", class_interface_decl->getName().str().c_str());
9782 }
9783 } break;
9784
9785 case clang::Type::Typedef:
9786 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9787 ->getDecl()
9788 ->getName()
9789 .str()
9790 .c_str());
9791 break;
9792
9793 case clang::Type::Auto:
9794 printf("auto ");
9795 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9796 llvm::cast<clang::AutoType>(qual_type)
9797 ->getDeducedType()
9798 .getAsOpaquePtr()));
9799
9800 case clang::Type::Elaborated:
9801 printf("elaborated ");
9802 return DumpTypeName(CompilerType(
9803 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9804 ->getNamedType()
9805 .getAsOpaquePtr()));
9806
9807 case clang::Type::Paren:
9808 printf("paren ");
9809 return DumpTypeName(CompilerType(
9810 type.GetTypeSystem(),
9811 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9812
9813 default:
9814 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9815 break;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009816 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009817 }
Greg Clayton6dc8d582015-08-18 22:32:36 +00009818}
9819
Kate Stoneb9c1b512016-09-06 20:57:50 +00009820clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9821 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9822 const char *parent_name, int tag_decl_kind,
9823 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9824 if (template_param_infos.IsValid()) {
9825 std::string template_basename(parent_name);
9826 template_basename.erase(template_basename.find('<'));
9827
9828 return CreateClassTemplateDecl(decl_ctx, access_type,
9829 template_basename.c_str(), tag_decl_kind,
9830 template_param_infos);
9831 }
9832 return NULL;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009833}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009834
Kate Stoneb9c1b512016-09-06 20:57:50 +00009835void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9836 ClangASTContext *ast = (ClangASTContext *)baton;
9837 SymbolFile *sym_file = ast->GetSymbolFile();
9838 if (sym_file) {
9839 CompilerType clang_type = GetTypeForDecl(decl);
9840 if (clang_type)
9841 sym_file->CompleteType(clang_type);
9842 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00009843}
9844
Kate Stoneb9c1b512016-09-06 20:57:50 +00009845void ClangASTContext::CompleteObjCInterfaceDecl(
9846 void *baton, clang::ObjCInterfaceDecl *decl) {
9847 ClangASTContext *ast = (ClangASTContext *)baton;
9848 SymbolFile *sym_file = ast->GetSymbolFile();
9849 if (sym_file) {
9850 CompilerType clang_type = GetTypeForDecl(decl);
9851 if (clang_type)
9852 sym_file->CompleteType(clang_type);
9853 }
Zachary Turner42dff792016-04-15 00:21:26 +00009854}
Greg Clayton261ac3f2015-08-28 01:01:03 +00009855
Kate Stoneb9c1b512016-09-06 20:57:50 +00009856DWARFASTParser *ClangASTContext::GetDWARFParser() {
9857 if (!m_dwarf_ast_parser_ap)
9858 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
9859 return m_dwarf_ast_parser_ap.get();
9860}
9861
9862PDBASTParser *ClangASTContext::GetPDBParser() {
9863 if (!m_pdb_ast_parser_ap)
9864 m_pdb_ast_parser_ap.reset(new PDBASTParser(*this));
9865 return m_pdb_ast_parser_ap.get();
9866}
9867
9868bool ClangASTContext::LayoutRecordType(
9869 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9870 uint64_t &alignment,
9871 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9872 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9873 &base_offsets,
9874 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9875 &vbase_offsets) {
9876 ClangASTContext *ast = (ClangASTContext *)baton;
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00009877 lldb_private::ClangASTImporter *importer = nullptr;
9878 if (ast->m_dwarf_ast_parser_ap)
9879 importer = &ast->m_dwarf_ast_parser_ap->GetClangASTImporter();
9880 if (!importer && ast->m_pdb_ast_parser_ap)
9881 importer = &ast->m_pdb_ast_parser_ap->GetClangASTImporter();
9882 if (!importer)
9883 return false;
9884
9885 return importer->LayoutRecordType(record_decl, bit_size, alignment,
9886 field_offsets, base_offsets, vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009887}
9888
Greg Clayton99558cc42015-08-24 23:46:31 +00009889//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009890// CompilerDecl override functions
9891//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009892
Kate Stoneb9c1b512016-09-06 20:57:50 +00009893ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9894 if (opaque_decl) {
9895 clang::NamedDecl *nd =
9896 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9897 if (nd != nullptr)
9898 return ConstString(nd->getDeclName().getAsString());
9899 }
9900 return ConstString();
Paul Hermand628cbb2015-09-15 23:44:17 +00009901}
9902
Kate Stoneb9c1b512016-09-06 20:57:50 +00009903ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9904 if (opaque_decl) {
9905 clang::NamedDecl *nd =
9906 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9907 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9908 clang::MangleContext *mc = getMangleContext();
9909 if (mc && mc->shouldMangleCXXName(nd)) {
9910 llvm::SmallVector<char, 1024> buf;
9911 llvm::raw_svector_ostream llvm_ostrm(buf);
9912 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9913 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9914 Ctor_Complete, llvm_ostrm);
9915 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9916 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9917 Dtor_Complete, llvm_ostrm);
9918 } else {
9919 mc->mangleName(nd, llvm_ostrm);
Greg Claytonfe689042015-11-10 17:47:04 +00009920 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009921 if (buf.size() > 0)
9922 return ConstString(buf.data(), buf.size());
9923 }
Greg Claytonfe689042015-11-10 17:47:04 +00009924 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009925 }
9926 return ConstString();
Greg Claytonfe689042015-11-10 17:47:04 +00009927}
9928
Kate Stoneb9c1b512016-09-06 20:57:50 +00009929CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9930 if (opaque_decl)
9931 return CompilerDeclContext(this,
9932 ((clang::Decl *)opaque_decl)->getDeclContext());
9933 else
9934 return CompilerDeclContext();
Greg Claytonfe689042015-11-10 17:47:04 +00009935}
9936
Kate Stoneb9c1b512016-09-06 20:57:50 +00009937CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9938 if (clang::FunctionDecl *func_decl =
9939 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9940 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9941 if (clang::ObjCMethodDecl *objc_method =
9942 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9943 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9944 else
Greg Claytonfe689042015-11-10 17:47:04 +00009945 return CompilerType();
9946}
9947
Kate Stoneb9c1b512016-09-06 20:57:50 +00009948size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9949 if (clang::FunctionDecl *func_decl =
9950 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9951 return func_decl->param_size();
9952 if (clang::ObjCMethodDecl *objc_method =
9953 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9954 return objc_method->param_size();
9955 else
9956 return 0;
9957}
9958
9959CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
9960 size_t idx) {
9961 if (clang::FunctionDecl *func_decl =
9962 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
9963 if (idx < func_decl->param_size()) {
9964 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9965 if (var_decl)
9966 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
9967 }
9968 } else if (clang::ObjCMethodDecl *objc_method =
9969 llvm::dyn_cast<clang::ObjCMethodDecl>(
9970 (clang::Decl *)opaque_decl)) {
9971 if (idx < objc_method->param_size())
9972 return CompilerType(
9973 this,
9974 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9975 }
9976 return CompilerType();
9977}
9978
Paul Hermand628cbb2015-09-15 23:44:17 +00009979//----------------------------------------------------------------------
Greg Clayton99558cc42015-08-24 23:46:31 +00009980// CompilerDeclContext functions
9981//----------------------------------------------------------------------
9982
Kate Stoneb9c1b512016-09-06 20:57:50 +00009983std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
9984 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
9985 std::vector<CompilerDecl> found_decls;
9986 if (opaque_decl_ctx) {
9987 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
9988 std::set<DeclContext *> searched;
9989 std::multimap<DeclContext *, DeclContext *> search_queue;
9990 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +00009991
Kate Stoneb9c1b512016-09-06 20:57:50 +00009992 for (clang::DeclContext *decl_context = root_decl_ctx;
9993 decl_context != nullptr && found_decls.empty();
9994 decl_context = decl_context->getParent()) {
9995 search_queue.insert(std::make_pair(decl_context, decl_context));
Paul Hermand628cbb2015-09-15 23:44:17 +00009996
Kate Stoneb9c1b512016-09-06 20:57:50 +00009997 for (auto it = search_queue.find(decl_context); it != search_queue.end();
9998 it++) {
9999 if (!searched.insert(it->second).second)
10000 continue;
10001 symbol_file->ParseDeclsForContext(
10002 CompilerDeclContext(this, it->second));
Paul Hermanea188fc2015-09-16 18:48:30 +000010003
Kate Stoneb9c1b512016-09-06 20:57:50 +000010004 for (clang::Decl *child : it->second->decls()) {
10005 if (clang::UsingDirectiveDecl *ud =
10006 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10007 if (ignore_using_decls)
10008 continue;
10009 clang::DeclContext *from = ud->getCommonAncestor();
10010 if (searched.find(ud->getNominatedNamespace()) == searched.end())
10011 search_queue.insert(
10012 std::make_pair(from, ud->getNominatedNamespace()));
10013 } else if (clang::UsingDecl *ud =
10014 llvm::dyn_cast<clang::UsingDecl>(child)) {
10015 if (ignore_using_decls)
10016 continue;
10017 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10018 clang::Decl *target = usd->getTargetDecl();
10019 if (clang::NamedDecl *nd =
10020 llvm::dyn_cast<clang::NamedDecl>(target)) {
10021 IdentifierInfo *ii = nd->getIdentifier();
10022 if (ii != nullptr &&
10023 ii->getName().equals(name.AsCString(nullptr)))
10024 found_decls.push_back(CompilerDecl(this, nd));
10025 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010026 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010027 } else if (clang::NamedDecl *nd =
10028 llvm::dyn_cast<clang::NamedDecl>(child)) {
10029 IdentifierInfo *ii = nd->getIdentifier();
10030 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
10031 found_decls.push_back(CompilerDecl(this, nd));
10032 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010033 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010034 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010035 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010036 }
10037 return found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +000010038}
10039
Dawn Perchikb5925782015-12-12 19:31:41 +000010040// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010041// and return the number of levels it took to find it, or
Adrian Prantl05097242018-04-30 16:49:04 +000010042// LLDB_INVALID_DECL_LEVEL if not found. If the decl was imported via a using
10043// declaration, its name and/or type, if set, will be used to check that the
10044// decl found in the scope is a match.
Dawn Perchikb5925782015-12-12 19:31:41 +000010045//
Kate Stoneb9c1b512016-09-06 20:57:50 +000010046// The optional name is required by languages (like C++) to handle using
Adrian Prantl05097242018-04-30 16:49:04 +000010047// declarations like:
Dawn Perchikb5925782015-12-12 19:31:41 +000010048//
10049// void poo();
10050// namespace ns {
10051// void foo();
10052// void goo();
10053// }
10054// void bar() {
10055// using ns::foo;
10056// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
10057// // LLDB_INVALID_DECL_LEVEL for 'goo'.
10058// }
10059//
10060// The optional type is useful in the case that there's a specific overload
10061// that we're looking for that might otherwise be shadowed, like:
10062//
10063// void foo(int);
10064// namespace ns {
10065// void foo();
10066// }
10067// void bar() {
10068// using ns::foo;
10069// // CountDeclLevels returns 0 for { 'foo', void() },
10070// // 1 for { 'foo', void(int) }, and
10071// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
10072// }
10073//
10074// NOTE: Because file statics are at the TranslationUnit along with globals, a
Kate Stoneb9c1b512016-09-06 20:57:50 +000010075// function at file scope will return the same level as a function at global
Adrian Prantl05097242018-04-30 16:49:04 +000010076// scope. Ideally we'd like to treat the file scope as an additional scope just
10077// below the global scope. More work needs to be done to recognise that, if
10078// the decl we're trying to look up is static, we should compare its source
10079// file with that of the current scope and return a lower number for it.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010080uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
10081 clang::DeclContext *child_decl_ctx,
10082 ConstString *child_name,
10083 CompilerType *child_type) {
10084 if (frame_decl_ctx) {
10085 std::set<DeclContext *> searched;
10086 std::multimap<DeclContext *, DeclContext *> search_queue;
10087 SymbolFile *symbol_file = GetSymbolFile();
Dawn Perchikb5925782015-12-12 19:31:41 +000010088
Kate Stoneb9c1b512016-09-06 20:57:50 +000010089 // Get the lookup scope for the decl we're trying to find.
10090 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
Dawn Perchikb5925782015-12-12 19:31:41 +000010091
Kate Stoneb9c1b512016-09-06 20:57:50 +000010092 // Look for it in our scope's decl context and its parents.
10093 uint32_t level = 0;
10094 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
10095 decl_ctx = decl_ctx->getParent()) {
10096 if (!decl_ctx->isLookupContext())
10097 continue;
10098 if (decl_ctx == parent_decl_ctx)
10099 // Found it!
10100 return level;
10101 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
10102 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
10103 it++) {
10104 if (searched.find(it->second) != searched.end())
10105 continue;
10106
10107 // Currently DWARF has one shared translation unit for all Decls at top
Adrian Prantl05097242018-04-30 16:49:04 +000010108 // level, so this would erroneously find using statements anywhere. So
10109 // don't look at the top-level translation unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010110 // TODO fix this and add a testcase that depends on it.
10111
10112 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
10113 continue;
10114
10115 searched.insert(it->second);
10116 symbol_file->ParseDeclsForContext(
10117 CompilerDeclContext(this, it->second));
10118
10119 for (clang::Decl *child : it->second->decls()) {
10120 if (clang::UsingDirectiveDecl *ud =
10121 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10122 clang::DeclContext *ns = ud->getNominatedNamespace();
10123 if (ns == parent_decl_ctx)
10124 // Found it!
10125 return level;
10126 clang::DeclContext *from = ud->getCommonAncestor();
10127 if (searched.find(ns) == searched.end())
10128 search_queue.insert(std::make_pair(from, ns));
10129 } else if (child_name) {
10130 if (clang::UsingDecl *ud =
10131 llvm::dyn_cast<clang::UsingDecl>(child)) {
10132 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10133 clang::Decl *target = usd->getTargetDecl();
10134 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
10135 if (!nd)
10136 continue;
10137 // Check names.
10138 IdentifierInfo *ii = nd->getIdentifier();
10139 if (ii == nullptr ||
10140 !ii->getName().equals(child_name->AsCString(nullptr)))
10141 continue;
10142 // Check types, if one was provided.
10143 if (child_type) {
10144 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
10145 if (!AreTypesSame(clang_type, *child_type,
10146 /*ignore_qualifiers=*/true))
10147 continue;
10148 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010149 // Found it!
10150 return level;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010151 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010152 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010153 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010154 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010155 }
10156 ++level;
Dawn Perchikb5925782015-12-12 19:31:41 +000010157 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010158 }
10159 return LLDB_INVALID_DECL_LEVEL;
Dawn Perchikb5925782015-12-12 19:31:41 +000010160}
10161
Kate Stoneb9c1b512016-09-06 20:57:50 +000010162bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
10163 if (opaque_decl_ctx)
10164 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
10165 else
Greg Clayton99558cc42015-08-24 23:46:31 +000010166 return false;
10167}
10168
Kate Stoneb9c1b512016-09-06 20:57:50 +000010169ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
10170 if (opaque_decl_ctx) {
10171 clang::NamedDecl *named_decl =
10172 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10173 if (named_decl)
10174 return ConstString(named_decl->getName());
10175 }
10176 return ConstString();
Greg Clayton99558cc42015-08-24 23:46:31 +000010177}
10178
Kate Stoneb9c1b512016-09-06 20:57:50 +000010179ConstString
10180ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
10181 if (opaque_decl_ctx) {
10182 clang::NamedDecl *named_decl =
10183 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10184 if (named_decl)
10185 return ConstString(
10186 llvm::StringRef(named_decl->getQualifiedNameAsString()));
10187 }
10188 return ConstString();
10189}
10190
10191bool ClangASTContext::DeclContextIsClassMethod(
10192 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
10193 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
10194 if (opaque_decl_ctx) {
10195 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10196 if (ObjCMethodDecl *objc_method =
10197 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
10198 if (is_instance_method_ptr)
10199 *is_instance_method_ptr = objc_method->isInstanceMethod();
10200 if (language_ptr)
10201 *language_ptr = eLanguageTypeObjC;
10202 if (language_object_name_ptr)
10203 language_object_name_ptr->SetCString("self");
10204 return true;
10205 } else if (CXXMethodDecl *cxx_method =
10206 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
10207 if (is_instance_method_ptr)
10208 *is_instance_method_ptr = cxx_method->isInstance();
10209 if (language_ptr)
10210 *language_ptr = eLanguageTypeC_plus_plus;
10211 if (language_object_name_ptr)
10212 language_object_name_ptr->SetCString("this");
10213 return true;
10214 } else if (clang::FunctionDecl *function_decl =
10215 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
10216 ClangASTMetadata *metadata =
10217 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
10218 if (metadata && metadata->HasObjectPtr()) {
10219 if (is_instance_method_ptr)
10220 *is_instance_method_ptr = true;
10221 if (language_ptr)
10222 *language_ptr = eLanguageTypeObjC;
10223 if (language_object_name_ptr)
10224 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
10225 return true;
10226 }
10227 }
10228 }
10229 return false;
10230}
10231
10232clang::DeclContext *
10233ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
10234 if (dc.IsClang())
10235 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
10236 return nullptr;
10237}
Greg Clayton99558cc42015-08-24 23:46:31 +000010238
10239ObjCMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010240ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
10241 if (dc.IsClang())
10242 return llvm::dyn_cast<clang::ObjCMethodDecl>(
10243 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10244 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010245}
10246
10247CXXMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010248ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10249 if (dc.IsClang())
10250 return llvm::dyn_cast<clang::CXXMethodDecl>(
10251 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10252 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010253}
10254
10255clang::FunctionDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010256ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10257 if (dc.IsClang())
10258 return llvm::dyn_cast<clang::FunctionDecl>(
10259 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10260 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010261}
10262
10263clang::NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010264ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10265 if (dc.IsClang())
10266 return llvm::dyn_cast<clang::NamespaceDecl>(
10267 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10268 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010269}
10270
10271ClangASTMetadata *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010272ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10273 const void *object) {
10274 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10275 if (ast)
10276 return ClangASTContext::GetMetadata(ast, object);
10277 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010278}
10279
10280clang::ASTContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010281ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10282 ClangASTContext *ast =
10283 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10284 if (ast)
10285 return ast->getASTContext();
10286 return nullptr;
10287}
10288
10289ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10290 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10291 m_target_wp(target.shared_from_this()),
10292 m_persistent_variables(new ClangPersistentVariables) {}
10293
10294UserExpression *ClangASTContextForExpressions::GetUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010295 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010296 Expression::ResultType desired_type,
10297 const EvaluateExpressionOptions &options) {
10298 TargetSP target_sp = m_target_wp.lock();
10299 if (!target_sp)
Greg Clayton99558cc42015-08-24 23:46:31 +000010300 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010301
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010302 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010303 desired_type, options);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010304}
10305
Kate Stoneb9c1b512016-09-06 20:57:50 +000010306FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10307 const CompilerType &return_type, const Address &function_address,
10308 const ValueList &arg_value_list, const char *name) {
10309 TargetSP target_sp = m_target_wp.lock();
10310 if (!target_sp)
10311 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010312
Kate Stoneb9c1b512016-09-06 20:57:50 +000010313 Process *process = target_sp->GetProcessSP().get();
10314 if (!process)
10315 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010316
Kate Stoneb9c1b512016-09-06 20:57:50 +000010317 return new ClangFunctionCaller(*process, return_type, function_address,
10318 arg_value_list, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010319}
10320
10321UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010322ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10323 const char *name) {
10324 TargetSP target_sp = m_target_wp.lock();
10325 if (!target_sp)
10326 return nullptr;
10327
10328 return new ClangUtilityFunction(*target_sp.get(), text, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010329}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010330
10331PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010332ClangASTContextForExpressions::GetPersistentExpressionState() {
10333 return m_persistent_variables.get();
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010334}
Sean Callanan68e44232017-09-28 20:20:25 +000010335
10336clang::ExternalASTMerger &
10337ClangASTContextForExpressions::GetMergerUnchecked() {
Eugene Zemtsova9d928c2017-09-29 03:15:08 +000010338 lldbassert(m_scratch_ast_source_ap != nullptr);
Sean Callanan68e44232017-09-28 20:20:25 +000010339 return m_scratch_ast_source_ap->GetMergerUnchecked();
10340}