blob: aafbe2ec1c3ad608a376e684d71985c73fff483c [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ClangASTContext.cpp -------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Eli Friedman932197d2010-06-13 19:06:42 +00009#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010
Zachary Turner827d5d72016-12-16 04:27:00 +000011#include "llvm/Support/FormatAdapters.h"
12#include "llvm/Support/FormatVariadic.h"
13
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000014#include <mutex>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include <string>
Sean Callananfe38c852015-10-08 23:07:53 +000016#include <vector>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017
Greg Clayton6beaaa62011-01-17 03:46:26 +000018
Kate Stoneb9c1b512016-09-06 20:57:50 +000019// Clang headers like to use NDEBUG inside of them to enable/disable debug
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +000020// related features using "#ifndef NDEBUG" preprocessor blocks to do one thing
Greg Clayton6beaaa62011-01-17 03:46:26 +000021// or another. This is bad because it means that if clang was built in release
22// mode, it assumes that you are building in release mode which is not always
23// the case. You can end up with functions that are defined as empty in header
24// files when NDEBUG is not defined, and this can cause link errors with the
25// clang .a files that you have since you might be missing functions in the .a
26// file. So we have to define NDEBUG when including clang headers to avoid any
27// mismatches. This is covered by rdar://problem/8691220
28
Sean Callanan3b1d4f62011-10-26 17:46:51 +000029#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
Greg Clayton6beaaa62011-01-17 03:46:26 +000030#define LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000031#define NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000032// Need to include assert.h so it is as clang would expect it to be (disabled)
33#include <assert.h>
34#endif
35
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "clang/AST/ASTContext.h"
37#include "clang/AST/ASTImporter.h"
Greg Claytonf74c4032012-12-03 18:29:55 +000038#include "clang/AST/Attr.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "clang/AST/CXXInheritance.h"
Greg Clayton8cf05932010-07-22 18:30:50 +000040#include "clang/AST/DeclObjC.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000041#include "clang/AST/DeclTemplate.h"
Greg Claytonfe689042015-11-10 17:47:04 +000042#include "clang/AST/Mangle.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "clang/AST/RecordLayout.h"
44#include "clang/AST/Type.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000045#include "clang/AST/VTableBuilder.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046#include "clang/Basic/Builtins.h"
Sean Callanan7e2863b2012-02-06 21:28:03 +000047#include "clang/Basic/Diagnostic.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048#include "clang/Basic/FileManager.h"
Sean Callanan79439e82010-11-18 02:56:27 +000049#include "clang/Basic/FileSystemOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050#include "clang/Basic/SourceManager.h"
51#include "clang/Basic/TargetInfo.h"
52#include "clang/Basic/TargetOptions.h"
53#include "clang/Frontend/FrontendOptions.h"
54#include "clang/Frontend/LangStandard.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000055
56#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000057#undef NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000058#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
59// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
60#include <assert.h>
61#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062
Greg Claytond8d4a572015-08-11 21:38:15 +000063#include "llvm/Support/Signals.h"
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000064#include "llvm/Support/Threading.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000065
Zachary Turnerd133f6a2016-03-28 22:53:41 +000066#include "Plugins/ExpressionParser/Clang/ClangFunctionCaller.h"
67#include "Plugins/ExpressionParser/Clang/ClangUserExpression.h"
68#include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000069#include "lldb/Utility/ArchSpec.h"
Zachary Turner01c32432017-02-14 19:06:07 +000070#include "lldb/Utility/Flags.h"
71
Zachary Turner29cb8682017-03-03 20:57:05 +000072#include "lldb/Core/DumpDataExtractor.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000073#include "lldb/Core/Module.h"
74#include "lldb/Core/PluginManager.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000075#include "lldb/Core/StreamFile.h"
Enrico Granata2267ad42014-09-16 17:28:40 +000076#include "lldb/Core/ThreadSafeDenseMap.h"
Greg Clayton57ee3062013-07-11 22:46:58 +000077#include "lldb/Core/UniqueCStringMap.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000078#include "lldb/Symbol/ClangASTContext.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000079#include "lldb/Symbol/ClangASTImporter.h"
Greg Clayton6dc8d582015-08-18 22:32:36 +000080#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000081#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000082#include "lldb/Symbol/ClangUtil.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000083#include "lldb/Symbol/ObjectFile.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000084#include "lldb/Symbol/SymbolFile.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000085#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000086#include "lldb/Target/ExecutionContext.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000087#include "lldb/Target/Language.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000088#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham151c0322015-09-15 21:13:50 +000089#include "lldb/Target/Process.h"
90#include "lldb/Target/Target.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000091#include "lldb/Utility/DataExtractor.h"
Sean Callananc530ba92016-05-02 21:15:31 +000092#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000093#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000094#include "lldb/Utility/RegularExpression.h"
Pavel Labathd821c992018-08-07 11:07:21 +000095#include "lldb/Utility/Scalar.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000096
Greg Clayton261ac3f2015-08-28 01:01:03 +000097#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
Zachary Turner42dff792016-04-15 00:21:26 +000098#include "Plugins/SymbolFile/PDB/PDBASTParser.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000099
Eli Friedman932197d2010-06-13 19:06:42 +0000100#include <stdio.h>
101
Greg Clayton1341baf2013-07-11 23:36:31 +0000102#include <mutex>
103
Greg Claytonc86103d2010-08-05 01:57:25 +0000104using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105using namespace lldb_private;
106using namespace llvm;
107using namespace clang;
108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109namespace {
110static inline bool
111ClangASTContextSupportsLanguage(lldb::LanguageType language) {
112 return language == eLanguageTypeUnknown || // Clang is the default type system
113 Language::LanguageIsC(language) ||
114 Language::LanguageIsCPlusPlus(language) ||
115 Language::LanguageIsObjC(language) ||
116 Language::LanguageIsPascal(language) ||
117 // Use Clang for Rust until there is a proper language plugin for it
118 language == eLanguageTypeRust ||
Johan Engelen04799572016-11-25 11:01:12 +0000119 language == eLanguageTypeExtRenderScript ||
120 // Use Clang for D until there is a proper language plugin for it
Bruce Mitchenerb8233f82018-11-27 05:37:27 +0000121 language == eLanguageTypeD ||
122 // Open Dylan compiler debug info is designed to be Clang-compatible
123 language == eLanguageTypeDylan;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124}
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000125
126// Checks whether m1 is an overload of m2 (as opposed to an override). This is
127// called by addOverridesForMethod to distinguish overrides (which share a
128// vtable entry) from overloads (which require distinct entries).
129bool isOverload(clang::CXXMethodDecl *m1, clang::CXXMethodDecl *m2) {
130 // FIXME: This should detect covariant return types, but currently doesn't.
131 lldbassert(&m1->getASTContext() == &m2->getASTContext() &&
132 "Methods should have the same AST context");
133 clang::ASTContext &context = m1->getASTContext();
134
135 const auto *m1Type = llvm::cast<clang::FunctionProtoType>(
136 context.getCanonicalType(m1->getType()));
137
138 const auto *m2Type = llvm::cast<clang::FunctionProtoType>(
139 context.getCanonicalType(m2->getType()));
140
141 auto compareArgTypes = [&context](const clang::QualType &m1p,
142 const clang::QualType &m2p) {
143 return context.hasSameType(m1p.getUnqualifiedType(),
144 m2p.getUnqualifiedType());
145 };
146
147 // FIXME: In C++14 and later, we can just pass m2Type->param_type_end()
148 // as a fourth parameter to std::equal().
149 return (m1->getNumParams() != m2->getNumParams()) ||
150 !std::equal(m1Type->param_type_begin(), m1Type->param_type_end(),
151 m2Type->param_type_begin(), compareArgTypes);
152}
153
154// If decl is a virtual method, walk the base classes looking for methods that
155// decl overrides. This table of overridden methods is used by IRGen to
156// determine the vtable layout for decl's parent class.
157void addOverridesForMethod(clang::CXXMethodDecl *decl) {
158 if (!decl->isVirtual())
159 return;
160
161 clang::CXXBasePaths paths;
162
163 auto find_overridden_methods =
164 [decl](const clang::CXXBaseSpecifier *specifier,
165 clang::CXXBasePath &path) {
166 if (auto *base_record = llvm::dyn_cast<clang::CXXRecordDecl>(
167 specifier->getType()->getAs<clang::RecordType>()->getDecl())) {
168
169 clang::DeclarationName name = decl->getDeclName();
170
171 // If this is a destructor, check whether the base class destructor is
172 // virtual.
173 if (name.getNameKind() == clang::DeclarationName::CXXDestructorName)
174 if (auto *baseDtorDecl = base_record->getDestructor()) {
175 if (baseDtorDecl->isVirtual()) {
176 path.Decls = baseDtorDecl;
177 return true;
178 } else
179 return false;
180 }
181
182 // Otherwise, search for name in the base class.
183 for (path.Decls = base_record->lookup(name); !path.Decls.empty();
184 path.Decls = path.Decls.slice(1)) {
185 if (auto *method_decl =
186 llvm::dyn_cast<clang::CXXMethodDecl>(path.Decls.front()))
187 if (method_decl->isVirtual() && !isOverload(decl, method_decl)) {
188 path.Decls = method_decl;
189 return true;
190 }
191 }
192 }
193
194 return false;
195 };
196
197 if (decl->getParent()->lookupInBases(find_overridden_methods, paths)) {
198 for (auto *overridden_decl : paths.found_decls())
199 decl->addOverriddenMethod(
200 llvm::cast<clang::CXXMethodDecl>(overridden_decl));
201 }
202}
Greg Clayton56939cb2015-09-17 22:23:34 +0000203}
204
Aleksandr Urakov1dc51db2018-11-12 16:23:50 +0000205static lldb::addr_t GetVTableAddress(Process &process,
206 VTableContextBase &vtable_ctx,
207 ValueObject &valobj,
208 const ASTRecordLayout &record_layout) {
209 // Retrieve type info
210 CompilerType pointee_type;
211 CompilerType this_type(valobj.GetCompilerType());
212 uint32_t type_info = this_type.GetTypeInfo(&pointee_type);
213 if (!type_info)
214 return LLDB_INVALID_ADDRESS;
215
216 // Check if it's a pointer or reference
217 bool ptr_or_ref = false;
218 if (type_info & (eTypeIsPointer | eTypeIsReference)) {
219 ptr_or_ref = true;
220 type_info = pointee_type.GetTypeInfo();
221 }
222
223 // We process only C++ classes
224 const uint32_t cpp_class = eTypeIsClass | eTypeIsCPlusPlus;
225 if ((type_info & cpp_class) != cpp_class)
226 return LLDB_INVALID_ADDRESS;
227
228 // Calculate offset to VTable pointer
229 lldb::offset_t vbtable_ptr_offset =
230 vtable_ctx.isMicrosoft() ? record_layout.getVBPtrOffset().getQuantity()
231 : 0;
232
233 if (ptr_or_ref) {
234 // We have a pointer / ref to object, so read
235 // VTable pointer from process memory
236
237 if (valobj.GetAddressTypeOfChildren() != eAddressTypeLoad)
238 return LLDB_INVALID_ADDRESS;
239
240 auto vbtable_ptr_addr = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
241 if (vbtable_ptr_addr == LLDB_INVALID_ADDRESS)
242 return LLDB_INVALID_ADDRESS;
243
244 vbtable_ptr_addr += vbtable_ptr_offset;
245
246 Status err;
247 return process.ReadPointerFromMemory(vbtable_ptr_addr, err);
248 }
249
250 // We have an object already read from process memory,
251 // so just extract VTable pointer from it
252
253 DataExtractor data;
254 Status err;
255 auto size = valobj.GetData(data, err);
256 if (err.Fail() || vbtable_ptr_offset + data.GetAddressByteSize() > size)
257 return LLDB_INVALID_ADDRESS;
258
259 return data.GetPointer(&vbtable_ptr_offset);
260}
261
262static int64_t ReadVBaseOffsetFromVTable(Process &process,
263 VTableContextBase &vtable_ctx,
264 lldb::addr_t vtable_ptr,
265 const CXXRecordDecl *cxx_record_decl,
266 const CXXRecordDecl *base_class_decl) {
267 if (vtable_ctx.isMicrosoft()) {
268 clang::MicrosoftVTableContext &msoft_vtable_ctx =
269 static_cast<clang::MicrosoftVTableContext &>(vtable_ctx);
270
271 // Get the index into the virtual base table. The
272 // index is the index in uint32_t from vbtable_ptr
273 const unsigned vbtable_index =
274 msoft_vtable_ctx.getVBTableIndex(cxx_record_decl, base_class_decl);
275 const lldb::addr_t base_offset_addr = vtable_ptr + vbtable_index * 4;
276 Status err;
277 return process.ReadSignedIntegerFromMemory(base_offset_addr, 4, INT64_MAX,
278 err);
279 }
280
281 clang::ItaniumVTableContext &itanium_vtable_ctx =
282 static_cast<clang::ItaniumVTableContext &>(vtable_ctx);
283
284 clang::CharUnits base_offset_offset =
285 itanium_vtable_ctx.getVirtualBaseOffsetOffset(cxx_record_decl,
286 base_class_decl);
287 const lldb::addr_t base_offset_addr =
288 vtable_ptr + base_offset_offset.getQuantity();
289 const uint32_t base_offset_size = process.GetAddressByteSize();
290 Status err;
291 return process.ReadSignedIntegerFromMemory(base_offset_addr, base_offset_size,
292 INT64_MAX, err);
293}
294
295static bool GetVBaseBitOffset(VTableContextBase &vtable_ctx,
296 ValueObject &valobj,
297 const ASTRecordLayout &record_layout,
298 const CXXRecordDecl *cxx_record_decl,
299 const CXXRecordDecl *base_class_decl,
300 int32_t &bit_offset) {
301 ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
302 Process *process = exe_ctx.GetProcessPtr();
303 if (!process)
304 return false;
305
306 lldb::addr_t vtable_ptr =
307 GetVTableAddress(*process, vtable_ctx, valobj, record_layout);
308 if (vtable_ptr == LLDB_INVALID_ADDRESS)
309 return false;
310
311 auto base_offset = ReadVBaseOffsetFromVTable(
312 *process, vtable_ctx, vtable_ptr, cxx_record_decl, base_class_decl);
313 if (base_offset == INT64_MAX)
314 return false;
315
316 bit_offset = base_offset * 8;
317
318 return true;
319}
320
Kate Stoneb9c1b512016-09-06 20:57:50 +0000321typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext *>
322 ClangASTMap;
Enrico Granata5d84a692014-08-19 21:46:37 +0000323
Kate Stoneb9c1b512016-09-06 20:57:50 +0000324static ClangASTMap &GetASTMap() {
325 static ClangASTMap *g_map_ptr = nullptr;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000326 static llvm::once_flag g_once_flag;
327 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
329 });
330 return *g_map_ptr;
Enrico Granata5d84a692014-08-19 21:46:37 +0000331}
332
Davide Italiano7e3ef4d2018-03-20 19:46:32 +0000333bool ClangASTContext::IsOperator(const char *name,
334 clang::OverloadedOperatorKind &op_kind) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335 if (name == nullptr || name[0] == '\0')
336 return false;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000337
338#define OPERATOR_PREFIX "operator"
339#define OPERATOR_PREFIX_LENGTH (sizeof(OPERATOR_PREFIX) - 1)
340
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341 const char *post_op_name = nullptr;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000342
Kate Stoneb9c1b512016-09-06 20:57:50 +0000343 bool no_space = true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000344
Kate Stoneb9c1b512016-09-06 20:57:50 +0000345 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
346 return false;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000347
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 post_op_name = name + OPERATOR_PREFIX_LENGTH;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000349
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 if (post_op_name[0] == ' ') {
351 post_op_name++;
352 no_space = false;
353 }
Pavel Labath1ac2b202016-08-15 14:32:32 +0000354
355#undef OPERATOR_PREFIX
356#undef OPERATOR_PREFIX_LENGTH
357
Adrian Prantl05097242018-04-30 16:49:04 +0000358 // This is an operator, set the overloaded operator kind to invalid in case
359 // this is a conversion operator...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000360 op_kind = clang::NUM_OVERLOADED_OPERATORS;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000361
Kate Stoneb9c1b512016-09-06 20:57:50 +0000362 switch (post_op_name[0]) {
363 default:
364 if (no_space)
365 return false;
366 break;
367 case 'n':
368 if (no_space)
369 return false;
370 if (strcmp(post_op_name, "new") == 0)
371 op_kind = clang::OO_New;
372 else if (strcmp(post_op_name, "new[]") == 0)
373 op_kind = clang::OO_Array_New;
374 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000375
Kate Stoneb9c1b512016-09-06 20:57:50 +0000376 case 'd':
377 if (no_space)
378 return false;
379 if (strcmp(post_op_name, "delete") == 0)
380 op_kind = clang::OO_Delete;
381 else if (strcmp(post_op_name, "delete[]") == 0)
382 op_kind = clang::OO_Array_Delete;
383 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000384
Kate Stoneb9c1b512016-09-06 20:57:50 +0000385 case '+':
386 if (post_op_name[1] == '\0')
387 op_kind = clang::OO_Plus;
388 else if (post_op_name[2] == '\0') {
389 if (post_op_name[1] == '=')
390 op_kind = clang::OO_PlusEqual;
391 else if (post_op_name[1] == '+')
392 op_kind = clang::OO_PlusPlus;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000393 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000394 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000395
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396 case '-':
397 if (post_op_name[1] == '\0')
398 op_kind = clang::OO_Minus;
399 else if (post_op_name[2] == '\0') {
400 switch (post_op_name[1]) {
401 case '=':
402 op_kind = clang::OO_MinusEqual;
403 break;
404 case '-':
405 op_kind = clang::OO_MinusMinus;
406 break;
407 case '>':
408 op_kind = clang::OO_Arrow;
409 break;
410 }
411 } else if (post_op_name[3] == '\0') {
412 if (post_op_name[2] == '*')
413 op_kind = clang::OO_ArrowStar;
414 break;
415 }
416 break;
417
418 case '*':
419 if (post_op_name[1] == '\0')
420 op_kind = clang::OO_Star;
421 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
422 op_kind = clang::OO_StarEqual;
423 break;
424
425 case '/':
426 if (post_op_name[1] == '\0')
427 op_kind = clang::OO_Slash;
428 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
429 op_kind = clang::OO_SlashEqual;
430 break;
431
432 case '%':
433 if (post_op_name[1] == '\0')
434 op_kind = clang::OO_Percent;
435 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
436 op_kind = clang::OO_PercentEqual;
437 break;
438
439 case '^':
440 if (post_op_name[1] == '\0')
441 op_kind = clang::OO_Caret;
442 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
443 op_kind = clang::OO_CaretEqual;
444 break;
445
446 case '&':
447 if (post_op_name[1] == '\0')
448 op_kind = clang::OO_Amp;
449 else if (post_op_name[2] == '\0') {
450 switch (post_op_name[1]) {
451 case '=':
452 op_kind = clang::OO_AmpEqual;
453 break;
454 case '&':
455 op_kind = clang::OO_AmpAmp;
456 break;
457 }
458 }
459 break;
460
461 case '|':
462 if (post_op_name[1] == '\0')
463 op_kind = clang::OO_Pipe;
464 else if (post_op_name[2] == '\0') {
465 switch (post_op_name[1]) {
466 case '=':
467 op_kind = clang::OO_PipeEqual;
468 break;
469 case '|':
470 op_kind = clang::OO_PipePipe;
471 break;
472 }
473 }
474 break;
475
476 case '~':
477 if (post_op_name[1] == '\0')
478 op_kind = clang::OO_Tilde;
479 break;
480
481 case '!':
482 if (post_op_name[1] == '\0')
483 op_kind = clang::OO_Exclaim;
484 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
485 op_kind = clang::OO_ExclaimEqual;
486 break;
487
488 case '=':
489 if (post_op_name[1] == '\0')
490 op_kind = clang::OO_Equal;
491 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
492 op_kind = clang::OO_EqualEqual;
493 break;
494
495 case '<':
496 if (post_op_name[1] == '\0')
497 op_kind = clang::OO_Less;
498 else if (post_op_name[2] == '\0') {
499 switch (post_op_name[1]) {
500 case '<':
501 op_kind = clang::OO_LessLess;
502 break;
503 case '=':
504 op_kind = clang::OO_LessEqual;
505 break;
506 }
507 } else if (post_op_name[3] == '\0') {
508 if (post_op_name[2] == '=')
509 op_kind = clang::OO_LessLessEqual;
510 }
511 break;
512
513 case '>':
514 if (post_op_name[1] == '\0')
515 op_kind = clang::OO_Greater;
516 else if (post_op_name[2] == '\0') {
517 switch (post_op_name[1]) {
518 case '>':
519 op_kind = clang::OO_GreaterGreater;
520 break;
521 case '=':
522 op_kind = clang::OO_GreaterEqual;
523 break;
524 }
525 } else if (post_op_name[1] == '>' && post_op_name[2] == '=' &&
526 post_op_name[3] == '\0') {
527 op_kind = clang::OO_GreaterGreaterEqual;
528 }
529 break;
530
531 case ',':
532 if (post_op_name[1] == '\0')
533 op_kind = clang::OO_Comma;
534 break;
535
536 case '(':
537 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
538 op_kind = clang::OO_Call;
539 break;
540
541 case '[':
542 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
543 op_kind = clang::OO_Subscript;
544 break;
545 }
546
547 return true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000548}
Enrico Granata5d84a692014-08-19 21:46:37 +0000549
Greg Clayton57ee3062013-07-11 22:46:58 +0000550clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +0000551ClangASTContext::ConvertAccessTypeToAccessSpecifier(AccessType access) {
552 switch (access) {
553 default:
554 break;
555 case eAccessNone:
Greg Clayton8cf05932010-07-22 18:30:50 +0000556 return AS_none;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000557 case eAccessPublic:
558 return AS_public;
559 case eAccessPrivate:
560 return AS_private;
561 case eAccessProtected:
562 return AS_protected;
563 }
564 return AS_none;
Greg Clayton8cf05932010-07-22 18:30:50 +0000565}
566
Kate Stoneb9c1b512016-09-06 20:57:50 +0000567static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) {
568 // FIXME: Cleanup per-file based stuff.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000569
Adrian Prantl05097242018-04-30 16:49:04 +0000570 // Set some properties which depend solely on the input kind; it would be
571 // nice to move these to the language standard, and have the driver resolve
572 // the input kind + language standard.
Richard Smith8186cd42017-04-26 22:10:53 +0000573 if (IK.getLanguage() == InputKind::Asm) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000574 Opts.AsmPreprocessor = 1;
Richard Smith8186cd42017-04-26 22:10:53 +0000575 } else if (IK.isObjectiveC()) {
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000576 Opts.ObjC = 1;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000577 }
578
579 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
580
581 if (LangStd == LangStandard::lang_unspecified) {
582 // Based on the base language, pick one.
Richard Smith8186cd42017-04-26 22:10:53 +0000583 switch (IK.getLanguage()) {
584 case InputKind::Unknown:
585 case InputKind::LLVM_IR:
586 case InputKind::RenderScript:
David Blaikiea322f362017-01-06 00:38:06 +0000587 llvm_unreachable("Invalid input kind!");
Richard Smith8186cd42017-04-26 22:10:53 +0000588 case InputKind::OpenCL:
Pavel Labath47168542017-04-27 08:49:19 +0000589 LangStd = LangStandard::lang_opencl10;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000590 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000591 case InputKind::CUDA:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000592 LangStd = LangStandard::lang_cuda;
593 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000594 case InputKind::Asm:
595 case InputKind::C:
596 case InputKind::ObjC:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000597 LangStd = LangStandard::lang_gnu99;
598 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000599 case InputKind::CXX:
600 case InputKind::ObjCXX:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000601 LangStd = LangStandard::lang_gnucxx98;
602 break;
Benjamin Kramer0d97c222018-04-25 13:22:47 +0000603 case InputKind::HIP:
604 LangStd = LangStandard::lang_hip;
605 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000606 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000607 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000608
Kate Stoneb9c1b512016-09-06 20:57:50 +0000609 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
610 Opts.LineComment = Std.hasLineComments();
611 Opts.C99 = Std.isC99();
612 Opts.CPlusPlus = Std.isCPlusPlus();
613 Opts.CPlusPlus11 = Std.isCPlusPlus11();
614 Opts.Digraphs = Std.hasDigraphs();
615 Opts.GNUMode = Std.isGNUMode();
616 Opts.GNUInline = !Std.isC99();
617 Opts.HexFloats = Std.hasHexFloats();
618 Opts.ImplicitInt = Std.hasImplicitInt();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000619
Kate Stoneb9c1b512016-09-06 20:57:50 +0000620 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000621
Kate Stoneb9c1b512016-09-06 20:57:50 +0000622 // OpenCL has some additional defaults.
Pavel Labath47168542017-04-27 08:49:19 +0000623 if (LangStd == LangStandard::lang_opencl10) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000624 Opts.OpenCL = 1;
625 Opts.AltiVec = 1;
626 Opts.CXXOperatorNames = 1;
627 Opts.LaxVectorConversions = 1;
628 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629
Kate Stoneb9c1b512016-09-06 20:57:50 +0000630 // OpenCL and C++ both have bool, true, false keywords.
631 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000632
Kate Stoneb9c1b512016-09-06 20:57:50 +0000633 Opts.setValueVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000634
Adrian Prantl05097242018-04-30 16:49:04 +0000635 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs is
636 // specified, or -std is set to a conforming mode.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000637 Opts.Trigraphs = !Opts.GNUMode;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000638 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000639 Opts.OptimizeSize = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000640
Kate Stoneb9c1b512016-09-06 20:57:50 +0000641 // FIXME: Eliminate this dependency.
642 // unsigned Opt =
643 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
644 // Opts.Optimize = Opt != 0;
645 unsigned Opt = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000646
Kate Stoneb9c1b512016-09-06 20:57:50 +0000647 // This is the __NO_INLINE__ define, which just depends on things like the
648 // optimization level and -fno-inline, not actually whether the backend has
649 // inlining enabled.
650 //
651 // FIXME: This is affected by other options (-fno-inline).
652 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000653}
654
Kate Stoneb9c1b512016-09-06 20:57:50 +0000655ClangASTContext::ClangASTContext(const char *target_triple)
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000656 : TypeSystem(TypeSystem::eKindClang), m_target_triple(), m_ast_up(),
657 m_language_options_up(), m_source_manager_up(), m_diagnostics_engine_up(),
658 m_target_options_rp(), m_target_info_up(), m_identifier_table_up(),
659 m_selector_table_up(), m_builtins_up(), m_callback_tag_decl(nullptr),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000660 m_callback_objc_decl(nullptr), m_callback_baton(nullptr),
661 m_pointer_byte_size(0), m_ast_owned(false) {
662 if (target_triple && target_triple[0])
663 SetTargetTriple(target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000664}
665
666//----------------------------------------------------------------------
667// Destructor
668//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000669ClangASTContext::~ClangASTContext() { Finalize(); }
670
671ConstString ClangASTContext::GetPluginNameStatic() {
672 return ConstString("clang");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000673}
674
Kate Stoneb9c1b512016-09-06 20:57:50 +0000675ConstString ClangASTContext::GetPluginName() {
676 return ClangASTContext::GetPluginNameStatic();
Greg Clayton56939cb2015-09-17 22:23:34 +0000677}
678
Kate Stoneb9c1b512016-09-06 20:57:50 +0000679uint32_t ClangASTContext::GetPluginVersion() { return 1; }
Greg Clayton56939cb2015-09-17 22:23:34 +0000680
Kate Stoneb9c1b512016-09-06 20:57:50 +0000681lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
682 lldb_private::Module *module,
683 Target *target) {
684 if (ClangASTContextSupportsLanguage(language)) {
685 ArchSpec arch;
686 if (module)
687 arch = module->GetArchitecture();
688 else if (target)
689 arch = target->GetArchitecture();
Greg Clayton56939cb2015-09-17 22:23:34 +0000690
Kate Stoneb9c1b512016-09-06 20:57:50 +0000691 if (arch.IsValid()) {
692 ArchSpec fixed_arch = arch;
693 // LLVM wants this to be set to iOS or MacOSX; if we're working on
694 // a bare-boards type image, change the triple for llvm's benefit.
695 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
696 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) {
697 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
698 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
699 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) {
700 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
701 } else {
702 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
Greg Clayton56939cb2015-09-17 22:23:34 +0000703 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000704 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000705
Kate Stoneb9c1b512016-09-06 20:57:50 +0000706 if (module) {
707 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
708 if (ast_sp) {
709 ast_sp->SetArchitecture(fixed_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000710 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000711 return ast_sp;
712 } else if (target && target->IsValid()) {
713 std::shared_ptr<ClangASTContextForExpressions> ast_sp(
714 new ClangASTContextForExpressions(*target));
715 if (ast_sp) {
716 ast_sp->SetArchitecture(fixed_arch);
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000717 ast_sp->m_scratch_ast_source_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000718 new ClangASTSource(target->shared_from_this()));
Sean Callanan68e44232017-09-28 20:20:25 +0000719 lldbassert(ast_sp->getFileManager());
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000720 ast_sp->m_scratch_ast_source_up->InstallASTContext(
Sean Callanan68e44232017-09-28 20:20:25 +0000721 *ast_sp->getASTContext(), *ast_sp->getFileManager(), true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000722 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000723 ast_sp->m_scratch_ast_source_up->CreateProxy());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000724 ast_sp->SetExternalSource(proxy_ast_source);
725 return ast_sp;
726 }
727 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000729 }
730 return lldb::TypeSystemSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000731}
732
Kate Stoneb9c1b512016-09-06 20:57:50 +0000733void ClangASTContext::EnumerateSupportedLanguages(
734 std::set<lldb::LanguageType> &languages_for_types,
735 std::set<lldb::LanguageType> &languages_for_expressions) {
736 static std::vector<lldb::LanguageType> s_supported_languages_for_types(
737 {lldb::eLanguageTypeC89, lldb::eLanguageTypeC, lldb::eLanguageTypeC11,
738 lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeC99,
739 lldb::eLanguageTypeObjC, lldb::eLanguageTypeObjC_plus_plus,
740 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
741 lldb::eLanguageTypeC11, lldb::eLanguageTypeC_plus_plus_14});
742
743 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions(
744 {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC_plus_plus,
745 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
746 lldb::eLanguageTypeC_plus_plus_14});
747
748 languages_for_types.insert(s_supported_languages_for_types.begin(),
749 s_supported_languages_for_types.end());
750 languages_for_expressions.insert(
751 s_supported_languages_for_expressions.begin(),
752 s_supported_languages_for_expressions.end());
Enrico Granata5d84a692014-08-19 21:46:37 +0000753}
754
Kate Stoneb9c1b512016-09-06 20:57:50 +0000755void ClangASTContext::Initialize() {
756 PluginManager::RegisterPlugin(GetPluginNameStatic(),
757 "clang base AST context plug-in",
758 CreateInstance, EnumerateSupportedLanguages);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000759}
760
Kate Stoneb9c1b512016-09-06 20:57:50 +0000761void ClangASTContext::Terminate() {
762 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000763}
764
Kate Stoneb9c1b512016-09-06 20:57:50 +0000765void ClangASTContext::Finalize() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000766 if (m_ast_up) {
767 GetASTMap().Erase(m_ast_up.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000768 if (!m_ast_owned)
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000769 m_ast_up.release();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000770 }
771
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000772 m_builtins_up.reset();
773 m_selector_table_up.reset();
774 m_identifier_table_up.reset();
775 m_target_info_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000776 m_target_options_rp.reset();
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000777 m_diagnostics_engine_up.reset();
778 m_source_manager_up.reset();
779 m_language_options_up.reset();
780 m_ast_up.reset();
781 m_scratch_ast_source_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000782}
783
784void ClangASTContext::Clear() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000785 m_ast_up.reset();
786 m_language_options_up.reset();
787 m_source_manager_up.reset();
788 m_diagnostics_engine_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000789 m_target_options_rp.reset();
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000790 m_target_info_up.reset();
791 m_identifier_table_up.reset();
792 m_selector_table_up.reset();
793 m_builtins_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000794 m_pointer_byte_size = 0;
795}
796
797const char *ClangASTContext::GetTargetTriple() {
798 return m_target_triple.c_str();
799}
800
801void ClangASTContext::SetTargetTriple(const char *target_triple) {
802 Clear();
803 m_target_triple.assign(target_triple);
804}
805
806void ClangASTContext::SetArchitecture(const ArchSpec &arch) {
807 SetTargetTriple(arch.GetTriple().str().c_str());
808}
809
810bool ClangASTContext::HasExternalSource() {
811 ASTContext *ast = getASTContext();
812 if (ast)
813 return ast->getExternalSource() != nullptr;
814 return false;
815}
816
817void ClangASTContext::SetExternalSource(
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000818 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000819 ASTContext *ast = getASTContext();
820 if (ast) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000821 ast->setExternalSource(ast_source_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000822 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000823 }
824}
825
826void ClangASTContext::RemoveExternalSource() {
827 ASTContext *ast = getASTContext();
828
829 if (ast) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000830 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_up;
831 ast->setExternalSource(empty_ast_source_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000832 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000833 }
834}
835
836void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) {
837 if (!m_ast_owned) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000838 m_ast_up.release();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000839 }
840 m_ast_owned = false;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000841 m_ast_up.reset(ast_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000842 GetASTMap().Insert(ast_ctx, this);
843}
844
845ASTContext *ClangASTContext::getASTContext() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000846 if (m_ast_up == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000847 m_ast_owned = true;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000848 m_ast_up.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000849 *getIdentifierTable(), *getSelectorTable(),
850 *getBuiltinContext()));
851
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000852 m_ast_up->getDiagnostics().setClient(getDiagnosticConsumer(), false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000853
854 // This can be NULL if we don't know anything about the architecture or if
Adrian Prantl05097242018-04-30 16:49:04 +0000855 // the target for an architecture isn't enabled in the llvm/clang that we
856 // built
Kate Stoneb9c1b512016-09-06 20:57:50 +0000857 TargetInfo *target_info = getTargetInfo();
858 if (target_info)
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000859 m_ast_up->InitBuiltinTypes(*target_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000860
861 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000862 m_ast_up->getTranslationUnitDecl()->setHasExternalLexicalStorage();
863 // m_ast_up->getTranslationUnitDecl()->setHasExternalVisibleStorage();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000864 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000865
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000866 GetASTMap().Insert(m_ast_up.get(), this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000867
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000868 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_up(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000869 new ClangExternalASTSourceCallbacks(
870 ClangASTContext::CompleteTagDecl,
871 ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
872 ClangASTContext::LayoutRecordType, this));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000873 SetExternalSource(ast_source_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000874 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000875 return m_ast_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000876}
877
Kate Stoneb9c1b512016-09-06 20:57:50 +0000878ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
879 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
880 return clang_ast;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000881}
882
Kate Stoneb9c1b512016-09-06 20:57:50 +0000883Builtin::Context *ClangASTContext::getBuiltinContext() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000884 if (m_builtins_up == nullptr)
885 m_builtins_up.reset(new Builtin::Context());
886 return m_builtins_up.get();
Sean Callanan79439e82010-11-18 02:56:27 +0000887}
888
Kate Stoneb9c1b512016-09-06 20:57:50 +0000889IdentifierTable *ClangASTContext::getIdentifierTable() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000890 if (m_identifier_table_up == nullptr)
891 m_identifier_table_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000892 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000893 return m_identifier_table_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000894}
895
Kate Stoneb9c1b512016-09-06 20:57:50 +0000896LangOptions *ClangASTContext::getLanguageOptions() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000897 if (m_language_options_up == nullptr) {
898 m_language_options_up.reset(new LangOptions());
899 ParseLangArgs(*m_language_options_up, InputKind::ObjCXX, GetTargetTriple());
900 // InitializeLangOptions(*m_language_options_up, InputKind::ObjCXX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000901 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000902 return m_language_options_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000903}
904
Kate Stoneb9c1b512016-09-06 20:57:50 +0000905SelectorTable *ClangASTContext::getSelectorTable() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000906 if (m_selector_table_up == nullptr)
907 m_selector_table_up.reset(new SelectorTable());
908 return m_selector_table_up.get();
Greg Claytonfe689042015-11-10 17:47:04 +0000909}
910
Kate Stoneb9c1b512016-09-06 20:57:50 +0000911clang::FileManager *ClangASTContext::getFileManager() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000912 if (m_file_manager_up == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000913 clang::FileSystemOptions file_system_options;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000914 m_file_manager_up.reset(new clang::FileManager(file_system_options));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000915 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000916 return m_file_manager_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000917}
918
919clang::SourceManager *ClangASTContext::getSourceManager() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000920 if (m_source_manager_up == nullptr)
921 m_source_manager_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000922 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000923 return m_source_manager_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000924}
925
926clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000927 if (m_diagnostics_engine_up == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000928 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000929 m_diagnostics_engine_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000930 new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
931 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000932 return m_diagnostics_engine_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000933}
934
935clang::MangleContext *ClangASTContext::getMangleContext() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000936 if (m_mangle_ctx_up == nullptr)
937 m_mangle_ctx_up.reset(getASTContext()->createMangleContext());
938 return m_mangle_ctx_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000939}
940
941class NullDiagnosticConsumer : public DiagnosticConsumer {
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000942public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000943 NullDiagnosticConsumer() {
944 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
945 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000946
Kate Stoneb9c1b512016-09-06 20:57:50 +0000947 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
948 const clang::Diagnostic &info) {
949 if (m_log) {
950 llvm::SmallVector<char, 32> diag_str(10);
951 info.FormatDiagnostic(diag_str);
952 diag_str.push_back('\0');
953 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000954 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000955 }
956
957 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
958 return new NullDiagnosticConsumer();
959 }
960
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000961private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000962 Log *m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000963};
964
Kate Stoneb9c1b512016-09-06 20:57:50 +0000965DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000966 if (m_diagnostic_consumer_up == nullptr)
967 m_diagnostic_consumer_up.reset(new NullDiagnosticConsumer);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000968
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000969 return m_diagnostic_consumer_up.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000970}
971
Kate Stoneb9c1b512016-09-06 20:57:50 +0000972std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000973 if (m_target_options_rp == nullptr && !m_target_triple.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000974 m_target_options_rp = std::make_shared<clang::TargetOptions>();
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000975 if (m_target_options_rp != nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000976 m_target_options_rp->Triple = m_target_triple;
977 }
978 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000979}
980
Kate Stoneb9c1b512016-09-06 20:57:50 +0000981TargetInfo *ClangASTContext::getTargetInfo() {
982 // target_triple should be something like "x86_64-apple-macosx"
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000983 if (m_target_info_up == nullptr && !m_target_triple.empty())
984 m_target_info_up.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000985 getTargetOptions()));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000986 return m_target_info_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000987}
988
989#pragma mark Basic Types
990
Kate Stoneb9c1b512016-09-06 20:57:50 +0000991static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
992 ASTContext *ast, QualType qual_type) {
993 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000994 return qual_type_bit_size == bit_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000995}
Greg Clayton56939cb2015-09-17 22:23:34 +0000996
Greg Claytona1e5dc82015-08-11 22:53:00 +0000997CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000998ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
999 size_t bit_size) {
1000 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
1001 getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001002}
1003
Kate Stoneb9c1b512016-09-06 20:57:50 +00001004CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
1005 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
1006 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001007 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001008 switch (encoding) {
1009 case eEncodingInvalid:
1010 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1011 return CompilerType(ast, ast->VoidPtrTy);
1012 break;
1013
1014 case eEncodingUint:
1015 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1016 return CompilerType(ast, ast->UnsignedCharTy);
1017 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1018 return CompilerType(ast, ast->UnsignedShortTy);
1019 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1020 return CompilerType(ast, ast->UnsignedIntTy);
1021 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1022 return CompilerType(ast, ast->UnsignedLongTy);
1023 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1024 return CompilerType(ast, ast->UnsignedLongLongTy);
1025 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1026 return CompilerType(ast, ast->UnsignedInt128Ty);
1027 break;
1028
1029 case eEncodingSint:
1030 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1031 return CompilerType(ast, ast->SignedCharTy);
1032 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1033 return CompilerType(ast, ast->ShortTy);
1034 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1035 return CompilerType(ast, ast->IntTy);
1036 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1037 return CompilerType(ast, ast->LongTy);
1038 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1039 return CompilerType(ast, ast->LongLongTy);
1040 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1041 return CompilerType(ast, ast->Int128Ty);
1042 break;
1043
1044 case eEncodingIEEE754:
1045 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1046 return CompilerType(ast, ast->FloatTy);
1047 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1048 return CompilerType(ast, ast->DoubleTy);
1049 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1050 return CompilerType(ast, ast->LongDoubleTy);
1051 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1052 return CompilerType(ast, ast->HalfTy);
1053 break;
1054
1055 case eEncodingVector:
1056 // Sanity check that bit_size is a multiple of 8's.
1057 if (bit_size && !(bit_size & 0x7u))
1058 return CompilerType(
1059 ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8));
1060 break;
1061 }
1062
1063 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001064}
1065
Greg Clayton57ee3062013-07-11 22:46:58 +00001066lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00001067ClangASTContext::GetBasicTypeEnumeration(const ConstString &name) {
1068 if (name) {
1069 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
1070 static TypeNameToBasicTypeMap g_type_map;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +00001071 static llvm::once_flag g_once_flag;
1072 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001073 // "void"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001074 g_type_map.Append(ConstString("void"), eBasicTypeVoid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001075
1076 // "char"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001077 g_type_map.Append(ConstString("char"), eBasicTypeChar);
1078 g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar);
1079 g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar);
1080 g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar);
1081 g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar);
1082 g_type_map.Append(ConstString("unsigned wchar_t"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001083 eBasicTypeUnsignedWChar);
1084 // "short"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001085 g_type_map.Append(ConstString("short"), eBasicTypeShort);
1086 g_type_map.Append(ConstString("short int"), eBasicTypeShort);
1087 g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort);
1088 g_type_map.Append(ConstString("unsigned short int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001089 eBasicTypeUnsignedShort);
1090
1091 // "int"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001092 g_type_map.Append(ConstString("int"), eBasicTypeInt);
1093 g_type_map.Append(ConstString("signed int"), eBasicTypeInt);
1094 g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt);
1095 g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001096
1097 // "long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001098 g_type_map.Append(ConstString("long"), eBasicTypeLong);
1099 g_type_map.Append(ConstString("long int"), eBasicTypeLong);
1100 g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong);
1101 g_type_map.Append(ConstString("unsigned long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102 eBasicTypeUnsignedLong);
1103
1104 // "long long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001105 g_type_map.Append(ConstString("long long"), eBasicTypeLongLong);
1106 g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong);
1107 g_type_map.Append(ConstString("unsigned long long"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001108 eBasicTypeUnsignedLongLong);
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001109 g_type_map.Append(ConstString("unsigned long long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001110 eBasicTypeUnsignedLongLong);
1111
1112 // "int128"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001113 g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128);
1114 g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001115
1116 // Miscellaneous
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001117 g_type_map.Append(ConstString("bool"), eBasicTypeBool);
1118 g_type_map.Append(ConstString("float"), eBasicTypeFloat);
1119 g_type_map.Append(ConstString("double"), eBasicTypeDouble);
1120 g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble);
1121 g_type_map.Append(ConstString("id"), eBasicTypeObjCID);
1122 g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel);
1123 g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001124 g_type_map.Sort();
1125 });
1126
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001127 return g_type_map.Find(name, eBasicTypeInvalid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001128 }
1129 return eBasicTypeInvalid;
Greg Clayton57ee3062013-07-11 22:46:58 +00001130}
1131
Kate Stoneb9c1b512016-09-06 20:57:50 +00001132CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1133 const ConstString &name) {
1134 if (ast) {
1135 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
1136 return ClangASTContext::GetBasicType(ast, basic_type);
1137 }
1138 return CompilerType();
1139}
1140
1141uint32_t ClangASTContext::GetPointerByteSize() {
1142 if (m_pointer_byte_size == 0)
Adrian Prantld963a7c2019-01-15 18:07:52 +00001143 if (auto size = GetBasicType(lldb::eBasicTypeVoid)
1144 .GetPointerType()
1145 .GetByteSize(nullptr))
1146 m_pointer_byte_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001147 return m_pointer_byte_size;
1148}
1149
1150CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
1151 return GetBasicType(getASTContext(), basic_type);
1152}
1153
1154CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1155 lldb::BasicType basic_type) {
1156 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001157 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001158 lldb::opaque_compiler_type_t clang_type =
1159 GetOpaqueCompilerType(ast, basic_type);
1160
1161 if (clang_type)
1162 return CompilerType(GetASTContext(ast), clang_type);
1163 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +00001164}
1165
Kate Stoneb9c1b512016-09-06 20:57:50 +00001166CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
1167 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
1168 ASTContext *ast = getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001169
Kate Stoneb9c1b512016-09-06 20:57:50 +00001170#define streq(a, b) strcmp(a, b) == 0
1171 assert(ast != nullptr);
1172 if (ast) {
1173 switch (dw_ate) {
1174 default:
1175 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001176
Kate Stoneb9c1b512016-09-06 20:57:50 +00001177 case DW_ATE_address:
1178 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1179 return CompilerType(ast, ast->VoidPtrTy);
1180 break;
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001181
Kate Stoneb9c1b512016-09-06 20:57:50 +00001182 case DW_ATE_boolean:
1183 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
1184 return CompilerType(ast, ast->BoolTy);
1185 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1186 return CompilerType(ast, ast->UnsignedCharTy);
1187 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1188 return CompilerType(ast, ast->UnsignedShortTy);
1189 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1190 return CompilerType(ast, ast->UnsignedIntTy);
1191 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001192
Kate Stoneb9c1b512016-09-06 20:57:50 +00001193 case DW_ATE_lo_user:
1194 // This has been seen to mean DW_AT_complex_integer
1195 if (type_name) {
1196 if (::strstr(type_name, "complex")) {
1197 CompilerType complex_int_clang_type =
1198 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1199 bit_size / 2);
1200 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1201 complex_int_clang_type)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001202 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001203 }
1204 break;
1205
1206 case DW_ATE_complex_float:
1207 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
1208 return CompilerType(ast, ast->FloatComplexTy);
1209 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
1210 return CompilerType(ast, ast->DoubleComplexTy);
1211 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
1212 return CompilerType(ast, ast->LongDoubleComplexTy);
1213 else {
1214 CompilerType complex_float_clang_type =
1215 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1216 bit_size / 2);
1217 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1218 complex_float_clang_type)));
1219 }
1220 break;
1221
1222 case DW_ATE_float:
1223 if (streq(type_name, "float") &&
1224 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1225 return CompilerType(ast, ast->FloatTy);
1226 if (streq(type_name, "double") &&
1227 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1228 return CompilerType(ast, ast->DoubleTy);
1229 if (streq(type_name, "long double") &&
1230 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1231 return CompilerType(ast, ast->LongDoubleTy);
1232 // Fall back to not requiring a name match
1233 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1234 return CompilerType(ast, ast->FloatTy);
1235 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1236 return CompilerType(ast, ast->DoubleTy);
1237 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1238 return CompilerType(ast, ast->LongDoubleTy);
1239 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1240 return CompilerType(ast, ast->HalfTy);
1241 break;
1242
1243 case DW_ATE_signed:
1244 if (type_name) {
1245 if (streq(type_name, "wchar_t") &&
1246 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1247 (getTargetInfo() &&
1248 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1249 return CompilerType(ast, ast->WCharTy);
1250 if (streq(type_name, "void") &&
1251 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
1252 return CompilerType(ast, ast->VoidTy);
1253 if (strstr(type_name, "long long") &&
1254 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1255 return CompilerType(ast, ast->LongLongTy);
1256 if (strstr(type_name, "long") &&
1257 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1258 return CompilerType(ast, ast->LongTy);
1259 if (strstr(type_name, "short") &&
1260 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1261 return CompilerType(ast, ast->ShortTy);
1262 if (strstr(type_name, "char")) {
1263 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1264 return CompilerType(ast, ast->CharTy);
1265 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1266 return CompilerType(ast, ast->SignedCharTy);
1267 }
1268 if (strstr(type_name, "int")) {
1269 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1270 return CompilerType(ast, ast->IntTy);
1271 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1272 return CompilerType(ast, ast->Int128Ty);
1273 }
1274 }
1275 // We weren't able to match up a type name, just search by size
1276 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1277 return CompilerType(ast, ast->CharTy);
1278 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1279 return CompilerType(ast, ast->ShortTy);
1280 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1281 return CompilerType(ast, ast->IntTy);
1282 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1283 return CompilerType(ast, ast->LongTy);
1284 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1285 return CompilerType(ast, ast->LongLongTy);
1286 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1287 return CompilerType(ast, ast->Int128Ty);
1288 break;
1289
1290 case DW_ATE_signed_char:
1291 if (ast->getLangOpts().CharIsSigned && type_name &&
1292 streq(type_name, "char")) {
1293 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1294 return CompilerType(ast, ast->CharTy);
1295 }
1296 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1297 return CompilerType(ast, ast->SignedCharTy);
1298 break;
1299
1300 case DW_ATE_unsigned:
1301 if (type_name) {
1302 if (streq(type_name, "wchar_t")) {
1303 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1304 if (!(getTargetInfo() &&
1305 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1306 return CompilerType(ast, ast->WCharTy);
1307 }
1308 }
1309 if (strstr(type_name, "long long")) {
1310 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1311 return CompilerType(ast, ast->UnsignedLongLongTy);
1312 } else if (strstr(type_name, "long")) {
1313 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1314 return CompilerType(ast, ast->UnsignedLongTy);
1315 } else if (strstr(type_name, "short")) {
1316 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1317 return CompilerType(ast, ast->UnsignedShortTy);
1318 } else if (strstr(type_name, "char")) {
1319 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1320 return CompilerType(ast, ast->UnsignedCharTy);
1321 } else if (strstr(type_name, "int")) {
1322 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1323 return CompilerType(ast, ast->UnsignedIntTy);
1324 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1325 return CompilerType(ast, ast->UnsignedInt128Ty);
1326 }
1327 }
1328 // We weren't able to match up a type name, just search by size
1329 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1330 return CompilerType(ast, ast->UnsignedCharTy);
1331 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1332 return CompilerType(ast, ast->UnsignedShortTy);
1333 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1334 return CompilerType(ast, ast->UnsignedIntTy);
1335 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1336 return CompilerType(ast, ast->UnsignedLongTy);
1337 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1338 return CompilerType(ast, ast->UnsignedLongLongTy);
1339 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1340 return CompilerType(ast, ast->UnsignedInt128Ty);
1341 break;
1342
1343 case DW_ATE_unsigned_char:
1344 if (!ast->getLangOpts().CharIsSigned && type_name &&
1345 streq(type_name, "char")) {
1346 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1347 return CompilerType(ast, ast->CharTy);
1348 }
1349 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1350 return CompilerType(ast, ast->UnsignedCharTy);
1351 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1352 return CompilerType(ast, ast->UnsignedShortTy);
1353 break;
1354
1355 case DW_ATE_imaginary_float:
1356 break;
1357
1358 case DW_ATE_UTF:
1359 if (type_name) {
1360 if (streq(type_name, "char16_t")) {
1361 return CompilerType(ast, ast->Char16Ty);
1362 } else if (streq(type_name, "char32_t")) {
1363 return CompilerType(ast, ast->Char32Ty);
1364 }
1365 }
1366 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001367 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001368 }
1369 // This assert should fire for anything that we don't catch above so we know
1370 // to fix any issues we run into.
1371 if (type_name) {
1372 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1373 "DW_TAG_base_type '%s' encoded with "
1374 "DW_ATE = 0x%x, bit_size = %u\n",
1375 type_name, dw_ate, bit_size);
1376 } else {
1377 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1378 "DW_TAG_base_type encoded with "
1379 "DW_ATE = 0x%x, bit_size = %u\n",
1380 dw_ate, bit_size);
1381 }
1382 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001383}
1384
Kate Stoneb9c1b512016-09-06 20:57:50 +00001385CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) {
1386 if (ast)
1387 return CompilerType(ast, ast->UnknownAnyTy);
1388 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001389}
1390
Kate Stoneb9c1b512016-09-06 20:57:50 +00001391CompilerType ClangASTContext::GetCStringType(bool is_const) {
1392 ASTContext *ast = getASTContext();
1393 QualType char_type(ast->CharTy);
1394
1395 if (is_const)
1396 char_type.addConst();
1397
1398 return CompilerType(ast, ast->getPointerType(char_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001399}
1400
Zachary Turner115209e2018-11-05 19:25:39 +00001401clang::DeclContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001402ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1403 return ast->getTranslationUnitDecl();
Sean Callanan09ab4b72011-11-30 22:11:59 +00001404}
1405
Kate Stoneb9c1b512016-09-06 20:57:50 +00001406clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1407 clang::Decl *source_decl) {
1408 FileSystemOptions file_system_options;
1409 FileManager file_manager(file_system_options);
1410 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1411
1412 return importer.Import(source_decl);
Greg Clayton526e5af2010-11-13 03:52:47 +00001413}
1414
Kate Stoneb9c1b512016-09-06 20:57:50 +00001415bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1416 bool ignore_qualifiers) {
1417 ClangASTContext *ast =
1418 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1419 if (!ast || ast != type2.GetTypeSystem())
1420 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001421
Kate Stoneb9c1b512016-09-06 20:57:50 +00001422 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1423 return true;
Greg Clayton55995eb2012-04-06 17:38:55 +00001424
Kate Stoneb9c1b512016-09-06 20:57:50 +00001425 QualType type1_qual = ClangUtil::GetQualType(type1);
1426 QualType type2_qual = ClangUtil::GetQualType(type2);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001427
Kate Stoneb9c1b512016-09-06 20:57:50 +00001428 if (ignore_qualifiers) {
1429 type1_qual = type1_qual.getUnqualifiedType();
1430 type2_qual = type2_qual.getUnqualifiedType();
1431 }
1432
1433 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001434}
1435
Kate Stoneb9c1b512016-09-06 20:57:50 +00001436CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1437 if (clang::ObjCInterfaceDecl *interface_decl =
1438 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1439 return GetTypeForDecl(interface_decl);
1440 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1441 return GetTypeForDecl(tag_decl);
1442 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001443}
1444
Kate Stoneb9c1b512016-09-06 20:57:50 +00001445CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001446 // No need to call the getASTContext() accessor (which can create the AST if
1447 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001448 // AST if our AST didn't already exist...
1449 ASTContext *ast = &decl->getASTContext();
1450 if (ast)
1451 return CompilerType(ast, ast->getTagDeclType(decl));
1452 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001453}
1454
Kate Stoneb9c1b512016-09-06 20:57:50 +00001455CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001456 // No need to call the getASTContext() accessor (which can create the AST if
1457 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001458 // AST if our AST didn't already exist...
1459 ASTContext *ast = &decl->getASTContext();
1460 if (ast)
1461 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1462 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001463}
1464
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001465#pragma mark Structure, Unions, Classes
1466
Kate Stoneb9c1b512016-09-06 20:57:50 +00001467CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx,
1468 AccessType access_type,
1469 const char *name, int kind,
1470 LanguageType language,
1471 ClangASTMetadata *metadata) {
1472 ASTContext *ast = getASTContext();
1473 assert(ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001474
Kate Stoneb9c1b512016-09-06 20:57:50 +00001475 if (decl_ctx == nullptr)
1476 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton9e409562010-07-28 02:04:09 +00001477
Kate Stoneb9c1b512016-09-06 20:57:50 +00001478 if (language == eLanguageTypeObjC ||
1479 language == eLanguageTypeObjC_plus_plus) {
1480 bool isForwardDecl = true;
1481 bool isInternal = false;
1482 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1483 }
Greg Clayton9e409562010-07-28 02:04:09 +00001484
Kate Stoneb9c1b512016-09-06 20:57:50 +00001485 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
Adrian Prantl05097242018-04-30 16:49:04 +00001486 // we will need to update this code. I was told to currently always use the
1487 // CXXRecordDecl class since we often don't know from debug information if
1488 // something is struct or a class, so we default to always use the more
Kate Stoneb9c1b512016-09-06 20:57:50 +00001489 // complete definition just in case.
Greg Claytonc4ffd662013-03-08 01:37:30 +00001490
Kate Stoneb9c1b512016-09-06 20:57:50 +00001491 bool is_anonymous = (!name) || (!name[0]);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001492
Kate Stoneb9c1b512016-09-06 20:57:50 +00001493 CXXRecordDecl *decl = CXXRecordDecl::Create(
1494 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1495 SourceLocation(), is_anonymous ? nullptr : &ast->Idents.get(name));
1496
1497 if (is_anonymous)
1498 decl->setAnonymousStructOrUnion(true);
1499
1500 if (decl) {
1501 if (metadata)
1502 SetMetadata(ast, decl, *metadata);
1503
1504 if (access_type != eAccessNone)
1505 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1506
1507 if (decl_ctx)
1508 decl_ctx->addDecl(decl);
1509
1510 return CompilerType(ast, ast->getTagDeclType(decl));
1511 }
1512 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001513}
1514
Sean Callanan09e91ac2017-05-11 22:08:05 +00001515namespace {
1516 bool IsValueParam(const clang::TemplateArgument &argument) {
1517 return argument.getKind() == TemplateArgument::Integral;
1518 }
1519}
1520
Kate Stoneb9c1b512016-09-06 20:57:50 +00001521static TemplateParameterList *CreateTemplateParameterList(
1522 ASTContext *ast,
1523 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1524 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1525 const bool parameter_pack = false;
1526 const bool is_typename = false;
1527 const unsigned depth = 0;
Sean Callanan09e91ac2017-05-11 22:08:05 +00001528 const size_t num_template_params = template_param_infos.args.size();
1529 DeclContext *const decl_context =
1530 ast->getTranslationUnitDecl(); // Is this the right decl context?,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001531 for (size_t i = 0; i < num_template_params; ++i) {
1532 const char *name = template_param_infos.names[i];
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001533
Kate Stoneb9c1b512016-09-06 20:57:50 +00001534 IdentifierInfo *identifier_info = nullptr;
1535 if (name && name[0])
1536 identifier_info = &ast->Idents.get(name);
Sean Callanan09e91ac2017-05-11 22:08:05 +00001537 if (IsValueParam(template_param_infos.args[i])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001538 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001539 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001540 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1541 template_param_infos.args[i].getIntegralType(), parameter_pack,
1542 nullptr));
1543
1544 } else {
1545 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001546 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001547 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1548 is_typename, parameter_pack));
1549 }
1550 }
Eugene Zemtsova9d928c2017-09-29 03:15:08 +00001551
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001552 if (template_param_infos.packed_args) {
Sean Callanan09e91ac2017-05-11 22:08:05 +00001553 IdentifierInfo *identifier_info = nullptr;
1554 if (template_param_infos.pack_name && template_param_infos.pack_name[0])
1555 identifier_info = &ast->Idents.get(template_param_infos.pack_name);
1556 const bool parameter_pack_true = true;
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001557
1558 if (!template_param_infos.packed_args->args.empty() &&
1559 IsValueParam(template_param_infos.packed_args->args[0])) {
Sean Callanan09e91ac2017-05-11 22:08:05 +00001560 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001561 *ast, decl_context, SourceLocation(), SourceLocation(), depth,
1562 num_template_params, identifier_info,
Sean Callanan09e91ac2017-05-11 22:08:05 +00001563 template_param_infos.packed_args->args[0].getIntegralType(),
1564 parameter_pack_true, nullptr));
1565 } else {
1566 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001567 *ast, decl_context, SourceLocation(), SourceLocation(), depth,
1568 num_template_params, identifier_info, is_typename,
1569 parameter_pack_true));
Sean Callanan09e91ac2017-05-11 22:08:05 +00001570 }
1571 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001572 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1573 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1574 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1575 SourceLocation(), requires_clause);
1576 return template_param_list;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001577}
1578
Kate Stoneb9c1b512016-09-06 20:57:50 +00001579clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1580 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1581 const char *name, const TemplateParameterInfos &template_param_infos) {
Adrian Prantld8f460e2018-05-02 16:55:16 +00001582 // /// Create a function template node.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001583 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001584
Kate Stoneb9c1b512016-09-06 20:57:50 +00001585 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001586
Kate Stoneb9c1b512016-09-06 20:57:50 +00001587 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1588 ast, template_param_infos, template_param_decls);
1589 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1590 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1591 template_param_list, func_decl);
1592
1593 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1594 i < template_param_decl_count; ++i) {
1595 // TODO: verify which decl context we should put template_param_decls into..
1596 template_param_decls[i]->setDeclContext(func_decl);
1597 }
1598
1599 return func_tmpl_decl;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001600}
1601
Kate Stoneb9c1b512016-09-06 20:57:50 +00001602void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1603 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1604 const TemplateParameterInfos &infos) {
1605 TemplateArgumentList template_args(TemplateArgumentList::OnStack, infos.args);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001606
Kate Stoneb9c1b512016-09-06 20:57:50 +00001607 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, &template_args,
1608 nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001609}
1610
Kate Stoneb9c1b512016-09-06 20:57:50 +00001611ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1612 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1613 int kind, const TemplateParameterInfos &template_param_infos) {
1614 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001615
Kate Stoneb9c1b512016-09-06 20:57:50 +00001616 ClassTemplateDecl *class_template_decl = nullptr;
1617 if (decl_ctx == nullptr)
1618 decl_ctx = ast->getTranslationUnitDecl();
Greg Claytonf0705c82011-10-22 03:33:13 +00001619
Kate Stoneb9c1b512016-09-06 20:57:50 +00001620 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1621 DeclarationName decl_name(&identifier_info);
Greg Claytonf0705c82011-10-22 03:33:13 +00001622
Kate Stoneb9c1b512016-09-06 20:57:50 +00001623 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Greg Claytonf0705c82011-10-22 03:33:13 +00001624
Kate Stoneb9c1b512016-09-06 20:57:50 +00001625 for (NamedDecl *decl : result) {
1626 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001627 if (class_template_decl)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001628 return class_template_decl;
1629 }
1630
1631 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1632
1633 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1634 ast, template_param_infos, template_param_decls);
1635
1636 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1637 *ast, (TagDecl::TagKind)kind,
1638 decl_ctx, // What decl context do we use here? TU? The actual decl
1639 // context?
1640 SourceLocation(), SourceLocation(), &identifier_info);
1641
1642 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1643 i < template_param_decl_count; ++i) {
1644 template_param_decls[i]->setDeclContext(template_cxx_decl);
1645 }
1646
1647 // With templated classes, we say that a class is templated with
1648 // specializations, but that the bare class has no functions.
1649 // template_cxx_decl->startDefinition();
1650 // template_cxx_decl->completeDefinition();
1651
1652 class_template_decl = ClassTemplateDecl::Create(
1653 *ast,
1654 decl_ctx, // What decl context do we use here? TU? The actual decl
1655 // context?
Pavel Labath4294de32017-01-12 10:44:16 +00001656 SourceLocation(), decl_name, template_param_list, template_cxx_decl);
Richard Smith35b007e2019-02-15 21:48:09 +00001657 template_cxx_decl->setDescribedClassTemplate(class_template_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001658
1659 if (class_template_decl) {
1660 if (access_type != eAccessNone)
1661 class_template_decl->setAccess(
1662 ConvertAccessTypeToAccessSpecifier(access_type));
1663
1664 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1665 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1666
1667 decl_ctx->addDecl(class_template_decl);
1668
Sean Callanan5e9e1992011-10-26 01:06:27 +00001669#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001670 VerifyDecl(class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001671#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001672 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001673
Kate Stoneb9c1b512016-09-06 20:57:50 +00001674 return class_template_decl;
Greg Claytonf0705c82011-10-22 03:33:13 +00001675}
1676
Frederic Rissf4e7e522018-04-02 16:18:32 +00001677TemplateTemplateParmDecl *
1678ClangASTContext::CreateTemplateTemplateParmDecl(const char *template_name) {
1679 ASTContext *ast = getASTContext();
1680
1681 auto *decl_ctx = ast->getTranslationUnitDecl();
1682
1683 IdentifierInfo &identifier_info = ast->Idents.get(template_name);
1684 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1685
1686 ClangASTContext::TemplateParameterInfos template_param_infos;
1687 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1688 ast, template_param_infos, template_param_decls);
1689
1690 // LLDB needs to create those decls only to be able to display a
Adrian Prantl05097242018-04-30 16:49:04 +00001691 // type that includes a template template argument. Only the name matters for
1692 // this purpose, so we use dummy values for the other characterisitcs of the
1693 // type.
Frederic Rissf4e7e522018-04-02 16:18:32 +00001694 return TemplateTemplateParmDecl::Create(
1695 *ast, decl_ctx, SourceLocation(),
1696 /*Depth*/ 0, /*Position*/ 0,
1697 /*IsParameterPack*/ false, &identifier_info, template_param_list);
1698}
1699
Greg Claytonf0705c82011-10-22 03:33:13 +00001700ClassTemplateSpecializationDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001701ClangASTContext::CreateClassTemplateSpecializationDecl(
1702 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1703 const TemplateParameterInfos &template_param_infos) {
1704 ASTContext *ast = getASTContext();
Sean Callanan09e91ac2017-05-11 22:08:05 +00001705 llvm::SmallVector<clang::TemplateArgument, 2> args(
1706 template_param_infos.args.size() +
1707 (template_param_infos.packed_args ? 1 : 0));
1708 std::copy(template_param_infos.args.begin(), template_param_infos.args.end(),
1709 args.begin());
1710 if (template_param_infos.packed_args) {
1711 args[args.size() - 1] = TemplateArgument::CreatePackCopy(
1712 *ast, template_param_infos.packed_args->args);
1713 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001714 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1715 ClassTemplateSpecializationDecl::Create(
1716 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
Sean Callanan09e91ac2017-05-11 22:08:05 +00001717 SourceLocation(), class_template_decl, args,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001718 nullptr);
1719
1720 class_template_specialization_decl->setSpecializationKind(
1721 TSK_ExplicitSpecialization);
1722
1723 return class_template_specialization_decl;
1724}
1725
1726CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1727 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1728 if (class_template_specialization_decl) {
Greg Claytonf0705c82011-10-22 03:33:13 +00001729 ASTContext *ast = getASTContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001730 if (ast)
1731 return CompilerType(
1732 ast, ast->getTagDeclType(class_template_specialization_decl));
1733 }
1734 return CompilerType();
Greg Claytonf0705c82011-10-22 03:33:13 +00001735}
1736
Kate Stoneb9c1b512016-09-06 20:57:50 +00001737static inline bool check_op_param(bool is_method,
1738 clang::OverloadedOperatorKind op_kind,
1739 bool unary, bool binary,
1740 uint32_t num_params) {
1741 // Special-case call since it can take any number of operands
1742 if (op_kind == OO_Call)
1743 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001744
Kate Stoneb9c1b512016-09-06 20:57:50 +00001745 // The parameter count doesn't include "this"
1746 if (is_method)
1747 ++num_params;
1748 if (num_params == 1)
1749 return unary;
1750 if (num_params == 2)
1751 return binary;
1752 else
Greg Clayton090d0982011-06-19 03:43:27 +00001753 return false;
1754}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001755
Kate Stoneb9c1b512016-09-06 20:57:50 +00001756bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1757 bool is_method, clang::OverloadedOperatorKind op_kind,
1758 uint32_t num_params) {
1759 switch (op_kind) {
1760 default:
1761 break;
1762 // C++ standard allows any number of arguments to new/delete
1763 case OO_New:
1764 case OO_Array_New:
1765 case OO_Delete:
1766 case OO_Array_Delete:
1767 return true;
1768 }
Pavel Labath1ac2b202016-08-15 14:32:32 +00001769
Kate Stoneb9c1b512016-09-06 20:57:50 +00001770#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1771 case OO_##Name: \
1772 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1773 switch (op_kind) {
Greg Clayton090d0982011-06-19 03:43:27 +00001774#include "clang/Basic/OperatorKinds.def"
Kate Stoneb9c1b512016-09-06 20:57:50 +00001775 default:
1776 break;
1777 }
1778 return false;
Greg Clayton090d0982011-06-19 03:43:27 +00001779}
1780
Greg Clayton57ee3062013-07-11 22:46:58 +00001781clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +00001782ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1783 clang::AccessSpecifier rhs) {
1784 // Make the access equal to the stricter of the field and the nested field's
1785 // access
1786 if (lhs == AS_none || rhs == AS_none)
1787 return AS_none;
1788 if (lhs == AS_private || rhs == AS_private)
1789 return AS_private;
1790 if (lhs == AS_protected || rhs == AS_protected)
1791 return AS_protected;
1792 return AS_public;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001793}
1794
Kate Stoneb9c1b512016-09-06 20:57:50 +00001795bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1796 uint32_t &bitfield_bit_size) {
1797 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001798}
1799
Kate Stoneb9c1b512016-09-06 20:57:50 +00001800bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field,
1801 uint32_t &bitfield_bit_size) {
1802 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001803 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001804
Kate Stoneb9c1b512016-09-06 20:57:50 +00001805 if (field->isBitField()) {
1806 Expr *bit_width_expr = field->getBitWidth();
1807 if (bit_width_expr) {
1808 llvm::APSInt bit_width_apsint;
1809 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1810 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001811 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001812 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001813 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001814 }
1815 return false;
1816}
1817
1818bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1819 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001820 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001821
1822 if (!record_decl->field_empty())
1823 return true;
1824
1825 // No fields, lets check this is a CXX record and check the base classes
1826 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1827 if (cxx_record_decl) {
1828 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1829 for (base_class = cxx_record_decl->bases_begin(),
1830 base_class_end = cxx_record_decl->bases_end();
1831 base_class != base_class_end; ++base_class) {
1832 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1833 base_class->getType()->getAs<RecordType>()->getDecl());
1834 if (RecordHasFields(base_class_decl))
1835 return true;
1836 }
1837 }
1838 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001839}
1840
Adrian Prantl4e8be2c2018-06-13 16:21:24 +00001841#pragma mark Objective-C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001842
Kate Stoneb9c1b512016-09-06 20:57:50 +00001843CompilerType ClangASTContext::CreateObjCClass(const char *name,
1844 DeclContext *decl_ctx,
1845 bool isForwardDecl,
1846 bool isInternal,
1847 ClangASTMetadata *metadata) {
1848 ASTContext *ast = getASTContext();
1849 assert(ast != nullptr);
1850 assert(name && name[0]);
1851 if (decl_ctx == nullptr)
1852 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001853
Kate Stoneb9c1b512016-09-06 20:57:50 +00001854 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1855 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1856 nullptr, SourceLocation(),
1857 /*isForwardDecl,*/
1858 isInternal);
1859
1860 if (decl && metadata)
1861 SetMetadata(ast, decl, *metadata);
1862
1863 return CompilerType(ast, ast->getObjCInterfaceType(decl));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001864}
1865
Kate Stoneb9c1b512016-09-06 20:57:50 +00001866static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001867 return !ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001868}
1869
Greg Clayton57ee3062013-07-11 22:46:58 +00001870uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001871ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1872 bool omit_empty_base_classes) {
1873 uint32_t num_bases = 0;
1874 if (cxx_record_decl) {
1875 if (omit_empty_base_classes) {
1876 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1877 for (base_class = cxx_record_decl->bases_begin(),
1878 base_class_end = cxx_record_decl->bases_end();
1879 base_class != base_class_end; ++base_class) {
1880 // Skip empty base classes
1881 if (omit_empty_base_classes) {
1882 if (BaseSpecifierIsEmpty(base_class))
1883 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001884 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001885 ++num_bases;
1886 }
1887 } else
1888 num_bases = cxx_record_decl->getNumBases();
1889 }
1890 return num_bases;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001891}
1892
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001893#pragma mark Namespace Declarations
1894
1895NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001896ClangASTContext::GetUniqueNamespaceDeclaration(const char *name,
1897 DeclContext *decl_ctx) {
1898 NamespaceDecl *namespace_decl = nullptr;
1899 ASTContext *ast = getASTContext();
1900 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1901 if (decl_ctx == nullptr)
1902 decl_ctx = translation_unit_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00001903
Kate Stoneb9c1b512016-09-06 20:57:50 +00001904 if (name) {
1905 IdentifierInfo &identifier_info = ast->Idents.get(name);
1906 DeclarationName decl_name(&identifier_info);
1907 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1908 for (NamedDecl *decl : result) {
1909 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1910 if (namespace_decl)
1911 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001912 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001913
1914 namespace_decl =
1915 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1916 SourceLocation(), &identifier_info, nullptr);
1917
1918 decl_ctx->addDecl(namespace_decl);
1919 } else {
1920 if (decl_ctx == translation_unit_decl) {
1921 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1922 if (namespace_decl)
1923 return namespace_decl;
1924
1925 namespace_decl =
1926 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1927 SourceLocation(), nullptr, nullptr);
1928 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1929 translation_unit_decl->addDecl(namespace_decl);
1930 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
1931 } else {
1932 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1933 if (parent_namespace_decl) {
1934 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1935 if (namespace_decl)
1936 return namespace_decl;
1937 namespace_decl =
1938 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1939 SourceLocation(), nullptr, nullptr);
1940 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1941 parent_namespace_decl->addDecl(namespace_decl);
1942 assert(namespace_decl ==
1943 parent_namespace_decl->getAnonymousNamespace());
1944 } else {
1945 // BAD!!!
1946 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001947 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001948 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001949#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001950 VerifyDecl(namespace_decl);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001951#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001952 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001953}
1954
Kate Stoneb9c1b512016-09-06 20:57:50 +00001955NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1956 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx) {
1957 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1958 if (ast_ctx == nullptr)
1959 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +00001960
Kate Stoneb9c1b512016-09-06 20:57:50 +00001961 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx);
Siva Chandra03ff5c82016-02-05 19:10:04 +00001962}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001963
Paul Hermand628cbb2015-09-15 23:44:17 +00001964clang::BlockDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001965ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1966 if (ctx != nullptr) {
1967 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1968 clang::SourceLocation());
1969 ctx->addDecl(decl);
1970 return decl;
1971 }
1972 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001973}
1974
Kate Stoneb9c1b512016-09-06 20:57:50 +00001975clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1976 clang::DeclContext *right,
1977 clang::DeclContext *root) {
1978 if (root == nullptr)
Paul Hermanea188fc2015-09-16 18:48:30 +00001979 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001980
1981 std::set<clang::DeclContext *> path_left;
1982 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1983 path_left.insert(d);
1984
1985 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1986 if (path_left.find(d) != path_left.end())
1987 return d;
1988
1989 return nullptr;
Paul Hermanea188fc2015-09-16 18:48:30 +00001990}
1991
Kate Stoneb9c1b512016-09-06 20:57:50 +00001992clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
1993 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
1994 if (decl_ctx != nullptr && ns_decl != nullptr) {
1995 clang::TranslationUnitDecl *translation_unit =
1996 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
1997 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
1998 *getASTContext(), decl_ctx, clang::SourceLocation(),
1999 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
2000 clang::SourceLocation(), ns_decl,
2001 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
2002 decl_ctx->addDecl(using_decl);
2003 return using_decl;
2004 }
2005 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002006}
2007
2008clang::UsingDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00002009ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
2010 clang::NamedDecl *target) {
2011 if (current_decl_ctx != nullptr && target != nullptr) {
2012 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
2013 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
2014 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
2015 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
2016 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
2017 target);
2018 using_decl->addShadowDecl(shadow_decl);
2019 current_decl_ctx->addDecl(using_decl);
2020 return using_decl;
2021 }
2022 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002023}
2024
Kate Stoneb9c1b512016-09-06 20:57:50 +00002025clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
2026 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
2027 if (decl_context != nullptr) {
2028 clang::VarDecl *var_decl = clang::VarDecl::Create(
2029 *getASTContext(), decl_context, clang::SourceLocation(),
2030 clang::SourceLocation(),
2031 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
2032 nullptr, clang::SC_None);
2033 var_decl->setAccess(clang::AS_public);
2034 decl_context->addDecl(var_decl);
2035 return var_decl;
2036 }
2037 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00002038}
2039
Zachary Turner9d8a97e2016-04-01 23:20:35 +00002040lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00002041ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
2042 lldb::BasicType basic_type) {
2043 switch (basic_type) {
2044 case eBasicTypeVoid:
2045 return ast->VoidTy.getAsOpaquePtr();
2046 case eBasicTypeChar:
2047 return ast->CharTy.getAsOpaquePtr();
2048 case eBasicTypeSignedChar:
2049 return ast->SignedCharTy.getAsOpaquePtr();
2050 case eBasicTypeUnsignedChar:
2051 return ast->UnsignedCharTy.getAsOpaquePtr();
2052 case eBasicTypeWChar:
2053 return ast->getWCharType().getAsOpaquePtr();
2054 case eBasicTypeSignedWChar:
2055 return ast->getSignedWCharType().getAsOpaquePtr();
2056 case eBasicTypeUnsignedWChar:
2057 return ast->getUnsignedWCharType().getAsOpaquePtr();
2058 case eBasicTypeChar16:
2059 return ast->Char16Ty.getAsOpaquePtr();
2060 case eBasicTypeChar32:
2061 return ast->Char32Ty.getAsOpaquePtr();
2062 case eBasicTypeShort:
2063 return ast->ShortTy.getAsOpaquePtr();
2064 case eBasicTypeUnsignedShort:
2065 return ast->UnsignedShortTy.getAsOpaquePtr();
2066 case eBasicTypeInt:
2067 return ast->IntTy.getAsOpaquePtr();
2068 case eBasicTypeUnsignedInt:
2069 return ast->UnsignedIntTy.getAsOpaquePtr();
2070 case eBasicTypeLong:
2071 return ast->LongTy.getAsOpaquePtr();
2072 case eBasicTypeUnsignedLong:
2073 return ast->UnsignedLongTy.getAsOpaquePtr();
2074 case eBasicTypeLongLong:
2075 return ast->LongLongTy.getAsOpaquePtr();
2076 case eBasicTypeUnsignedLongLong:
2077 return ast->UnsignedLongLongTy.getAsOpaquePtr();
2078 case eBasicTypeInt128:
2079 return ast->Int128Ty.getAsOpaquePtr();
2080 case eBasicTypeUnsignedInt128:
2081 return ast->UnsignedInt128Ty.getAsOpaquePtr();
2082 case eBasicTypeBool:
2083 return ast->BoolTy.getAsOpaquePtr();
2084 case eBasicTypeHalf:
2085 return ast->HalfTy.getAsOpaquePtr();
2086 case eBasicTypeFloat:
2087 return ast->FloatTy.getAsOpaquePtr();
2088 case eBasicTypeDouble:
2089 return ast->DoubleTy.getAsOpaquePtr();
2090 case eBasicTypeLongDouble:
2091 return ast->LongDoubleTy.getAsOpaquePtr();
2092 case eBasicTypeFloatComplex:
2093 return ast->FloatComplexTy.getAsOpaquePtr();
2094 case eBasicTypeDoubleComplex:
2095 return ast->DoubleComplexTy.getAsOpaquePtr();
2096 case eBasicTypeLongDoubleComplex:
2097 return ast->LongDoubleComplexTy.getAsOpaquePtr();
2098 case eBasicTypeObjCID:
2099 return ast->getObjCIdType().getAsOpaquePtr();
2100 case eBasicTypeObjCClass:
2101 return ast->getObjCClassType().getAsOpaquePtr();
2102 case eBasicTypeObjCSel:
2103 return ast->getObjCSelType().getAsOpaquePtr();
2104 case eBasicTypeNullPtr:
2105 return ast->NullPtrTy.getAsOpaquePtr();
2106 default:
2107 return nullptr;
2108 }
Zachary Turner9d8a97e2016-04-01 23:20:35 +00002109}
2110
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002111#pragma mark Function Types
2112
Pavel Labath1ac2b202016-08-15 14:32:32 +00002113clang::DeclarationName
Kate Stoneb9c1b512016-09-06 20:57:50 +00002114ClangASTContext::GetDeclarationName(const char *name,
2115 const CompilerType &function_clang_type) {
2116 if (!name || !name[0])
2117 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002118
Kate Stoneb9c1b512016-09-06 20:57:50 +00002119 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
2120 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
2121 return DeclarationName(&getASTContext()->Idents.get(
2122 name)); // Not operator, but a regular function.
Pavel Labath1ac2b202016-08-15 14:32:32 +00002123
Adrian Prantl05097242018-04-30 16:49:04 +00002124 // Check the number of operator parameters. Sometimes we have seen bad DWARF
2125 // that doesn't correctly describe operators and if we try to create a method
2126 // and add it to the class, clang will assert and crash, so we need to make
2127 // sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002128 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
2129 const clang::FunctionProtoType *function_type =
2130 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
2131 if (function_type == nullptr)
2132 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002133
Kate Stoneb9c1b512016-09-06 20:57:50 +00002134 const bool is_method = false;
2135 const unsigned int num_params = function_type->getNumParams();
2136 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
2137 is_method, op_kind, num_params))
2138 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002139
Kate Stoneb9c1b512016-09-06 20:57:50 +00002140 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
Pavel Labath1ac2b202016-08-15 14:32:32 +00002141}
2142
Kate Stoneb9c1b512016-09-06 20:57:50 +00002143FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
2144 DeclContext *decl_ctx, const char *name,
2145 const CompilerType &function_clang_type, int storage, bool is_inline) {
2146 FunctionDecl *func_decl = nullptr;
2147 ASTContext *ast = getASTContext();
2148 if (decl_ctx == nullptr)
2149 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002150
Kate Stoneb9c1b512016-09-06 20:57:50 +00002151 const bool hasWrittenPrototype = true;
2152 const bool isConstexprSpecified = false;
Greg Clayton0d551042013-06-28 21:08:47 +00002153
Kate Stoneb9c1b512016-09-06 20:57:50 +00002154 clang::DeclarationName declarationName =
2155 GetDeclarationName(name, function_clang_type);
2156 func_decl = FunctionDecl::Create(
2157 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
2158 ClangUtil::GetQualType(function_clang_type), nullptr,
2159 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
2160 isConstexprSpecified);
2161 if (func_decl)
2162 decl_ctx->addDecl(func_decl);
2163
Sean Callanan5e9e1992011-10-26 01:06:27 +00002164#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00002165 VerifyDecl(func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002166#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002167
2168 return func_decl;
2169}
2170
2171CompilerType ClangASTContext::CreateFunctionType(
2172 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002173 unsigned num_args, bool is_variadic, unsigned type_quals,
2174 clang::CallingConv cc) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002175 if (ast == nullptr)
2176 return CompilerType(); // invalid AST
2177
2178 if (!result_type || !ClangUtil::IsClangType(result_type))
2179 return CompilerType(); // invalid return type
2180
2181 std::vector<QualType> qual_type_args;
2182 if (num_args > 0 && args == nullptr)
2183 return CompilerType(); // invalid argument array passed in
2184
2185 // Verify that all arguments are valid and the right type
2186 for (unsigned i = 0; i < num_args; ++i) {
2187 if (args[i]) {
2188 // Make sure we have a clang type in args[i] and not a type from another
2189 // language whose name might match
2190 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2191 lldbassert(is_clang_type);
2192 if (is_clang_type)
2193 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2194 else
2195 return CompilerType(); // invalid argument type (must be a clang type)
2196 } else
2197 return CompilerType(); // invalid argument type (empty)
2198 }
2199
2200 // TODO: Detect calling convention in DWARF?
2201 FunctionProtoType::ExtProtoInfo proto_info;
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002202 proto_info.ExtInfo = cc;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002203 proto_info.Variadic = is_variadic;
2204 proto_info.ExceptionSpec = EST_None;
Mikael Nilsson8b3bf6c2018-12-13 10:17:26 +00002205 proto_info.TypeQuals = clang::Qualifiers::fromFastMask(type_quals);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002206 proto_info.RefQualifier = RQ_None;
2207
2208 return CompilerType(ast,
2209 ast->getFunctionType(ClangUtil::GetQualType(result_type),
2210 qual_type_args, proto_info));
2211}
2212
2213ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
Zachary Turner6753d2d2018-12-12 17:17:53 +00002214 clang::DeclContext *decl_ctx, const char *name,
2215 const CompilerType &param_type, int storage) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002216 ASTContext *ast = getASTContext();
2217 assert(ast != nullptr);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00002218 auto *decl =
2219 ParmVarDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(),
2220 name && name[0] ? &ast->Idents.get(name) : nullptr,
2221 ClangUtil::GetQualType(param_type), nullptr,
2222 (clang::StorageClass)storage, nullptr);
2223 decl_ctx->addDecl(decl);
2224 return decl;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002225}
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) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00002236 QualType block_type = m_ast_up->getBlockPointerType(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002237 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();
Jonas Devlieghered5b44032019-02-13 06:25:41 +00003326 QualType function_pointer_type = m_ast_up->getPointerType(pointee_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003327 *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));
Jonas Devliegherea6682a42018-12-15 00:15:33 +00003936 return !qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003937}
3938
3939bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3940 if (!type)
3941 return false;
3942 clang::QualType qual_type(GetCanonicalQualType(type));
3943 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3944 if (tag_type)
3945 return tag_type->isBeingDefined();
3946 return false;
3947}
3948
3949bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3950 CompilerType *class_type_ptr) {
3951 if (!type)
3952 return false;
3953
3954 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3955
3956 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3957 if (class_type_ptr) {
3958 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3959 const clang::ObjCObjectPointerType *obj_pointer_type =
3960 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3961 if (obj_pointer_type == nullptr)
3962 class_type_ptr->Clear();
3963 else
3964 class_type_ptr->SetCompilerType(
3965 type.GetTypeSystem(),
3966 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3967 .getAsOpaquePtr());
3968 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003969 }
3970 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003971 }
3972 if (class_type_ptr)
3973 class_type_ptr->Clear();
3974 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003975}
3976
Kate Stoneb9c1b512016-09-06 20:57:50 +00003977bool ClangASTContext::GetObjCClassName(const CompilerType &type,
3978 std::string &class_name) {
3979 if (!type)
3980 return false;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003981
Kate Stoneb9c1b512016-09-06 20:57:50 +00003982 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3983
3984 const clang::ObjCObjectType *object_type =
3985 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3986 if (object_type) {
3987 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3988 if (interface) {
3989 class_name = interface->getNameAsString();
3990 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00003991 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003992 }
3993 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003994}
3995
Greg Claytond8d4a572015-08-11 21:38:15 +00003996//----------------------------------------------------------------------
3997// Type Completion
3998//----------------------------------------------------------------------
3999
Kate Stoneb9c1b512016-09-06 20:57:50 +00004000bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
4001 if (!type)
4002 return false;
4003 const bool allow_completion = true;
4004 return GetCompleteQualType(getASTContext(), GetQualType(type),
4005 allow_completion);
Greg Claytond8d4a572015-08-11 21:38:15 +00004006}
4007
Kate Stoneb9c1b512016-09-06 20:57:50 +00004008ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
4009 std::string type_name;
4010 if (type) {
4011 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
4012 clang::QualType qual_type(GetQualType(type));
4013 printing_policy.SuppressTagKeyword = true;
4014 const clang::TypedefType *typedef_type =
4015 qual_type->getAs<clang::TypedefType>();
4016 if (typedef_type) {
4017 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
4018 type_name = typedef_decl->getQualifiedNameAsString();
4019 } else {
4020 type_name = qual_type.getAsString(printing_policy);
Greg Claytond8d4a572015-08-11 21:38:15 +00004021 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004022 }
4023 return ConstString(type_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00004024}
4025
4026uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004027ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
4028 CompilerType *pointee_or_element_clang_type) {
4029 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004030 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004031
4032 if (pointee_or_element_clang_type)
4033 pointee_or_element_clang_type->Clear();
4034
4035 clang::QualType qual_type(GetQualType(type));
4036
4037 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4038 switch (type_class) {
Sean Callananddf802a2017-06-02 01:24:18 +00004039 case clang::Type::Attributed:
4040 return GetTypeInfo(
4041 qual_type->getAs<clang::AttributedType>()
4042 ->getModifiedType().getAsOpaquePtr(),
4043 pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004044 case clang::Type::Builtin: {
4045 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
4046 qual_type->getCanonicalTypeInternal());
4047
4048 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
4049 switch (builtin_type->getKind()) {
4050 case clang::BuiltinType::ObjCId:
4051 case clang::BuiltinType::ObjCClass:
4052 if (pointee_or_element_clang_type)
4053 pointee_or_element_clang_type->SetCompilerType(
4054 getASTContext(), getASTContext()->ObjCBuiltinClassTy);
4055 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
4056 break;
4057
4058 case clang::BuiltinType::ObjCSel:
4059 if (pointee_or_element_clang_type)
4060 pointee_or_element_clang_type->SetCompilerType(getASTContext(),
4061 getASTContext()->CharTy);
4062 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
4063 break;
4064
4065 case clang::BuiltinType::Bool:
4066 case clang::BuiltinType::Char_U:
4067 case clang::BuiltinType::UChar:
4068 case clang::BuiltinType::WChar_U:
4069 case clang::BuiltinType::Char16:
4070 case clang::BuiltinType::Char32:
4071 case clang::BuiltinType::UShort:
4072 case clang::BuiltinType::UInt:
4073 case clang::BuiltinType::ULong:
4074 case clang::BuiltinType::ULongLong:
4075 case clang::BuiltinType::UInt128:
4076 case clang::BuiltinType::Char_S:
4077 case clang::BuiltinType::SChar:
4078 case clang::BuiltinType::WChar_S:
4079 case clang::BuiltinType::Short:
4080 case clang::BuiltinType::Int:
4081 case clang::BuiltinType::Long:
4082 case clang::BuiltinType::LongLong:
4083 case clang::BuiltinType::Int128:
4084 case clang::BuiltinType::Float:
4085 case clang::BuiltinType::Double:
4086 case clang::BuiltinType::LongDouble:
4087 builtin_type_flags |= eTypeIsScalar;
4088 if (builtin_type->isInteger()) {
4089 builtin_type_flags |= eTypeIsInteger;
4090 if (builtin_type->isSignedInteger())
4091 builtin_type_flags |= eTypeIsSigned;
4092 } else if (builtin_type->isFloatingPoint())
4093 builtin_type_flags |= eTypeIsFloat;
4094 break;
4095 default:
4096 break;
4097 }
4098 return builtin_type_flags;
4099 }
4100
4101 case clang::Type::BlockPointer:
4102 if (pointee_or_element_clang_type)
4103 pointee_or_element_clang_type->SetCompilerType(
4104 getASTContext(), qual_type->getPointeeType());
4105 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
4106
4107 case clang::Type::Complex: {
4108 uint32_t complex_type_flags =
4109 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
4110 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
4111 qual_type->getCanonicalTypeInternal());
4112 if (complex_type) {
4113 clang::QualType complex_element_type(complex_type->getElementType());
4114 if (complex_element_type->isIntegerType())
4115 complex_type_flags |= eTypeIsFloat;
4116 else if (complex_element_type->isFloatingType())
4117 complex_type_flags |= eTypeIsInteger;
4118 }
4119 return complex_type_flags;
4120 } break;
4121
4122 case clang::Type::ConstantArray:
4123 case clang::Type::DependentSizedArray:
4124 case clang::Type::IncompleteArray:
4125 case clang::Type::VariableArray:
4126 if (pointee_or_element_clang_type)
4127 pointee_or_element_clang_type->SetCompilerType(
4128 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
4129 ->getElementType());
4130 return eTypeHasChildren | eTypeIsArray;
4131
4132 case clang::Type::DependentName:
4133 return 0;
4134 case clang::Type::DependentSizedExtVector:
4135 return eTypeHasChildren | eTypeIsVector;
4136 case clang::Type::DependentTemplateSpecialization:
4137 return eTypeIsTemplate;
4138 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004139 return CompilerType(
4140 getASTContext(),
4141 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
4142 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004143
4144 case clang::Type::Enum:
4145 if (pointee_or_element_clang_type)
4146 pointee_or_element_clang_type->SetCompilerType(
4147 getASTContext(),
4148 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
4149 return eTypeIsEnumeration | eTypeHasValue;
4150
4151 case clang::Type::Auto:
4152 return CompilerType(
4153 getASTContext(),
4154 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4155 .GetTypeInfo(pointee_or_element_clang_type);
4156 case clang::Type::Elaborated:
4157 return CompilerType(
4158 getASTContext(),
4159 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4160 .GetTypeInfo(pointee_or_element_clang_type);
4161 case clang::Type::Paren:
4162 return CompilerType(getASTContext(),
4163 llvm::cast<clang::ParenType>(qual_type)->desugar())
4164 .GetTypeInfo(pointee_or_element_clang_type);
4165
4166 case clang::Type::FunctionProto:
4167 return eTypeIsFuncPrototype | eTypeHasValue;
4168 case clang::Type::FunctionNoProto:
4169 return eTypeIsFuncPrototype | eTypeHasValue;
4170 case clang::Type::InjectedClassName:
4171 return 0;
4172
4173 case clang::Type::LValueReference:
4174 case clang::Type::RValueReference:
4175 if (pointee_or_element_clang_type)
4176 pointee_or_element_clang_type->SetCompilerType(
4177 getASTContext(),
4178 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4179 ->getPointeeType());
4180 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4181
4182 case clang::Type::MemberPointer:
4183 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4184
4185 case clang::Type::ObjCObjectPointer:
4186 if (pointee_or_element_clang_type)
4187 pointee_or_element_clang_type->SetCompilerType(
4188 getASTContext(), qual_type->getPointeeType());
4189 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4190 eTypeHasValue;
4191
4192 case clang::Type::ObjCObject:
4193 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4194 case clang::Type::ObjCInterface:
4195 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4196
4197 case clang::Type::Pointer:
4198 if (pointee_or_element_clang_type)
4199 pointee_or_element_clang_type->SetCompilerType(
4200 getASTContext(), qual_type->getPointeeType());
4201 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4202
4203 case clang::Type::Record:
4204 if (qual_type->getAsCXXRecordDecl())
4205 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4206 else
4207 return eTypeHasChildren | eTypeIsStructUnion;
4208 break;
4209 case clang::Type::SubstTemplateTypeParm:
4210 return eTypeIsTemplate;
4211 case clang::Type::TemplateTypeParm:
4212 return eTypeIsTemplate;
4213 case clang::Type::TemplateSpecialization:
4214 return eTypeIsTemplate;
4215
4216 case clang::Type::Typedef:
4217 return eTypeIsTypedef |
4218 CompilerType(getASTContext(),
4219 llvm::cast<clang::TypedefType>(qual_type)
4220 ->getDecl()
4221 ->getUnderlyingType())
4222 .GetTypeInfo(pointee_or_element_clang_type);
4223 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004224 return CompilerType(getASTContext(),
4225 llvm::cast<clang::TypeOfExprType>(qual_type)
4226 ->getUnderlyingExpr()
4227 ->getType())
4228 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004229 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004230 return CompilerType(
4231 getASTContext(),
4232 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4233 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004234 case clang::Type::UnresolvedUsing:
4235 return 0;
4236
4237 case clang::Type::ExtVector:
4238 case clang::Type::Vector: {
4239 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4240 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4241 qual_type->getCanonicalTypeInternal());
4242 if (vector_type) {
4243 if (vector_type->isIntegerType())
4244 vector_type_flags |= eTypeIsFloat;
4245 else if (vector_type->isFloatingType())
4246 vector_type_flags |= eTypeIsInteger;
4247 }
4248 return vector_type_flags;
4249 }
4250 default:
4251 return 0;
4252 }
4253 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004254}
4255
Greg Claytond8d4a572015-08-11 21:38:15 +00004256lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004257ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4258 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004259 return lldb::eLanguageTypeC;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004260
4261 // If the type is a reference, then resolve it to what it refers to first:
4262 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4263 if (qual_type->isAnyPointerType()) {
4264 if (qual_type->isObjCObjectPointerType())
4265 return lldb::eLanguageTypeObjC;
4266
4267 clang::QualType pointee_type(qual_type->getPointeeType());
4268 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
4269 return lldb::eLanguageTypeC_plus_plus;
4270 if (pointee_type->isObjCObjectOrInterfaceType())
4271 return lldb::eLanguageTypeObjC;
4272 if (pointee_type->isObjCClassType())
4273 return lldb::eLanguageTypeObjC;
4274 if (pointee_type.getTypePtr() ==
4275 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4276 return lldb::eLanguageTypeObjC;
4277 } else {
4278 if (qual_type->isObjCObjectOrInterfaceType())
4279 return lldb::eLanguageTypeObjC;
4280 if (qual_type->getAsCXXRecordDecl())
4281 return lldb::eLanguageTypeC_plus_plus;
4282 switch (qual_type->getTypeClass()) {
4283 default:
4284 break;
4285 case clang::Type::Builtin:
4286 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4287 default:
4288 case clang::BuiltinType::Void:
4289 case clang::BuiltinType::Bool:
4290 case clang::BuiltinType::Char_U:
4291 case clang::BuiltinType::UChar:
4292 case clang::BuiltinType::WChar_U:
4293 case clang::BuiltinType::Char16:
4294 case clang::BuiltinType::Char32:
4295 case clang::BuiltinType::UShort:
4296 case clang::BuiltinType::UInt:
4297 case clang::BuiltinType::ULong:
4298 case clang::BuiltinType::ULongLong:
4299 case clang::BuiltinType::UInt128:
4300 case clang::BuiltinType::Char_S:
4301 case clang::BuiltinType::SChar:
4302 case clang::BuiltinType::WChar_S:
4303 case clang::BuiltinType::Short:
4304 case clang::BuiltinType::Int:
4305 case clang::BuiltinType::Long:
4306 case clang::BuiltinType::LongLong:
4307 case clang::BuiltinType::Int128:
4308 case clang::BuiltinType::Float:
4309 case clang::BuiltinType::Double:
4310 case clang::BuiltinType::LongDouble:
4311 break;
4312
4313 case clang::BuiltinType::NullPtr:
4314 return eLanguageTypeC_plus_plus;
4315
4316 case clang::BuiltinType::ObjCId:
4317 case clang::BuiltinType::ObjCClass:
4318 case clang::BuiltinType::ObjCSel:
4319 return eLanguageTypeObjC;
4320
4321 case clang::BuiltinType::Dependent:
4322 case clang::BuiltinType::Overload:
4323 case clang::BuiltinType::BoundMember:
4324 case clang::BuiltinType::UnknownAny:
4325 break;
4326 }
4327 break;
4328 case clang::Type::Typedef:
4329 return CompilerType(getASTContext(),
4330 llvm::cast<clang::TypedefType>(qual_type)
4331 ->getDecl()
4332 ->getUnderlyingType())
4333 .GetMinimumLanguage();
4334 }
4335 }
4336 return lldb::eLanguageTypeC;
Greg Claytond8d4a572015-08-11 21:38:15 +00004337}
4338
4339lldb::TypeClass
Kate Stoneb9c1b512016-09-06 20:57:50 +00004340ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4341 if (!type)
4342 return lldb::eTypeClassInvalid;
4343
4344 clang::QualType qual_type(GetQualType(type));
4345
4346 switch (qual_type->getTypeClass()) {
4347 case clang::Type::UnaryTransform:
4348 break;
4349 case clang::Type::FunctionNoProto:
4350 return lldb::eTypeClassFunction;
4351 case clang::Type::FunctionProto:
4352 return lldb::eTypeClassFunction;
4353 case clang::Type::IncompleteArray:
4354 return lldb::eTypeClassArray;
4355 case clang::Type::VariableArray:
4356 return lldb::eTypeClassArray;
4357 case clang::Type::ConstantArray:
4358 return lldb::eTypeClassArray;
4359 case clang::Type::DependentSizedArray:
4360 return lldb::eTypeClassArray;
4361 case clang::Type::DependentSizedExtVector:
4362 return lldb::eTypeClassVector;
Fangrui Song8f284882018-07-13 22:40:40 +00004363 case clang::Type::DependentVector:
4364 return lldb::eTypeClassVector;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004365 case clang::Type::ExtVector:
4366 return lldb::eTypeClassVector;
4367 case clang::Type::Vector:
4368 return lldb::eTypeClassVector;
4369 case clang::Type::Builtin:
4370 return lldb::eTypeClassBuiltin;
4371 case clang::Type::ObjCObjectPointer:
4372 return lldb::eTypeClassObjCObjectPointer;
4373 case clang::Type::BlockPointer:
4374 return lldb::eTypeClassBlockPointer;
4375 case clang::Type::Pointer:
4376 return lldb::eTypeClassPointer;
4377 case clang::Type::LValueReference:
4378 return lldb::eTypeClassReference;
4379 case clang::Type::RValueReference:
4380 return lldb::eTypeClassReference;
4381 case clang::Type::MemberPointer:
4382 return lldb::eTypeClassMemberPointer;
4383 case clang::Type::Complex:
4384 if (qual_type->isComplexType())
4385 return lldb::eTypeClassComplexFloat;
4386 else
4387 return lldb::eTypeClassComplexInteger;
4388 case clang::Type::ObjCObject:
4389 return lldb::eTypeClassObjCObject;
4390 case clang::Type::ObjCInterface:
4391 return lldb::eTypeClassObjCInterface;
4392 case clang::Type::Record: {
4393 const clang::RecordType *record_type =
4394 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4395 const clang::RecordDecl *record_decl = record_type->getDecl();
4396 if (record_decl->isUnion())
4397 return lldb::eTypeClassUnion;
4398 else if (record_decl->isStruct())
4399 return lldb::eTypeClassStruct;
4400 else
4401 return lldb::eTypeClassClass;
4402 } break;
4403 case clang::Type::Enum:
4404 return lldb::eTypeClassEnumeration;
4405 case clang::Type::Typedef:
4406 return lldb::eTypeClassTypedef;
4407 case clang::Type::UnresolvedUsing:
4408 break;
4409 case clang::Type::Paren:
4410 return CompilerType(getASTContext(),
4411 llvm::cast<clang::ParenType>(qual_type)->desugar())
4412 .GetTypeClass();
4413 case clang::Type::Auto:
4414 return CompilerType(
4415 getASTContext(),
4416 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4417 .GetTypeClass();
4418 case clang::Type::Elaborated:
4419 return CompilerType(
4420 getASTContext(),
4421 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4422 .GetTypeClass();
4423
4424 case clang::Type::Attributed:
4425 break;
4426 case clang::Type::TemplateTypeParm:
4427 break;
4428 case clang::Type::SubstTemplateTypeParm:
4429 break;
4430 case clang::Type::SubstTemplateTypeParmPack:
4431 break;
4432 case clang::Type::InjectedClassName:
4433 break;
4434 case clang::Type::DependentName:
4435 break;
4436 case clang::Type::DependentTemplateSpecialization:
4437 break;
4438 case clang::Type::PackExpansion:
4439 break;
4440
4441 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004442 return CompilerType(getASTContext(),
4443 llvm::cast<clang::TypeOfExprType>(qual_type)
4444 ->getUnderlyingExpr()
4445 ->getType())
4446 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004447 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004448 return CompilerType(
4449 getASTContext(),
4450 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4451 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004452 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004453 return CompilerType(
4454 getASTContext(),
4455 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4456 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004457 case clang::Type::TemplateSpecialization:
4458 break;
Pavel Labath4f19fce22017-02-17 13:39:50 +00004459 case clang::Type::DeducedTemplateSpecialization:
4460 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004461 case clang::Type::Atomic:
4462 break;
4463 case clang::Type::Pipe:
4464 break;
4465
4466 // pointer type decayed from an array or function type.
4467 case clang::Type::Decayed:
4468 break;
4469 case clang::Type::Adjusted:
4470 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00004471 case clang::Type::ObjCTypeParam:
4472 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00004473
4474 case clang::Type::DependentAddressSpace:
4475 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004476 }
4477 // We don't know hot to display this type...
4478 return lldb::eTypeClassOther;
Greg Claytond8d4a572015-08-11 21:38:15 +00004479}
4480
Kate Stoneb9c1b512016-09-06 20:57:50 +00004481unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4482 if (type)
4483 return GetQualType(type).getQualifiers().getCVRQualifiers();
4484 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004485}
4486
4487//----------------------------------------------------------------------
4488// Creating related types
4489//----------------------------------------------------------------------
4490
Greg Claytona1e5dc82015-08-11 22:53:00 +00004491CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004492ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4493 uint64_t *stride) {
4494 if (type) {
4495 clang::QualType qual_type(GetCanonicalQualType(type));
4496
4497 const clang::Type *array_eletype =
4498 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4499
4500 if (!array_eletype)
4501 return CompilerType();
4502
4503 CompilerType element_type(getASTContext(),
4504 array_eletype->getCanonicalTypeUnqualified());
4505
4506 // TODO: the real stride will be >= this value.. find the real one!
4507 if (stride)
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00004508 if (Optional<uint64_t> size = element_type.GetByteSize(nullptr))
Adrian Prantld963a7c2019-01-15 18:07:52 +00004509 *stride = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004510
4511 return element_type;
4512 }
4513 return CompilerType();
4514}
4515
4516CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4517 uint64_t size) {
4518 if (type) {
4519 clang::QualType qual_type(GetCanonicalQualType(type));
4520 if (clang::ASTContext *ast_ctx = getASTContext()) {
4521 if (size != 0)
4522 return CompilerType(
4523 ast_ctx, ast_ctx->getConstantArrayType(
4524 qual_type, llvm::APInt(64, size),
4525 clang::ArrayType::ArraySizeModifier::Normal, 0));
4526 else
4527 return CompilerType(
4528 ast_ctx,
4529 ast_ctx->getIncompleteArrayType(
4530 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
Greg Claytond8d4a572015-08-11 21:38:15 +00004531 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004532 }
4533
4534 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004535}
4536
Greg Claytona1e5dc82015-08-11 22:53:00 +00004537CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004538ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4539 if (type)
4540 return CompilerType(getASTContext(), GetCanonicalQualType(type));
4541 return CompilerType();
4542}
4543
4544static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4545 clang::QualType qual_type) {
4546 if (qual_type->isPointerType())
4547 qual_type = ast->getPointerType(
4548 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4549 else
4550 qual_type = qual_type.getUnqualifiedType();
4551 qual_type.removeLocalConst();
4552 qual_type.removeLocalRestrict();
4553 qual_type.removeLocalVolatile();
4554 return qual_type;
Enrico Granata639392f2016-08-30 20:39:58 +00004555}
4556
4557CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004558ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4559 if (type)
4560 return CompilerType(
4561 getASTContext(),
4562 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4563 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004564}
4565
Kate Stoneb9c1b512016-09-06 20:57:50 +00004566int ClangASTContext::GetFunctionArgumentCount(
4567 lldb::opaque_compiler_type_t type) {
4568 if (type) {
4569 const clang::FunctionProtoType *func =
4570 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4571 if (func)
4572 return func->getNumParams();
4573 }
4574 return -1;
4575}
4576
4577CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4578 lldb::opaque_compiler_type_t type, size_t idx) {
4579 if (type) {
4580 const clang::FunctionProtoType *func =
4581 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4582 if (func) {
4583 const uint32_t num_args = func->getNumParams();
4584 if (idx < num_args)
4585 return CompilerType(getASTContext(), func->getParamType(idx));
4586 }
4587 }
4588 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004589}
4590
Greg Claytona1e5dc82015-08-11 22:53:00 +00004591CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004592ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4593 if (type) {
4594 clang::QualType qual_type(GetQualType(type));
4595 const clang::FunctionProtoType *func =
4596 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4597 if (func)
4598 return CompilerType(getASTContext(), func->getReturnType());
4599 }
4600 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004601}
4602
4603size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004604ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4605 size_t num_functions = 0;
4606 if (type) {
4607 clang::QualType qual_type(GetCanonicalQualType(type));
4608 switch (qual_type->getTypeClass()) {
4609 case clang::Type::Record:
4610 if (GetCompleteQualType(getASTContext(), qual_type)) {
4611 const clang::RecordType *record_type =
4612 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4613 const clang::RecordDecl *record_decl = record_type->getDecl();
4614 assert(record_decl);
4615 const clang::CXXRecordDecl *cxx_record_decl =
4616 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4617 if (cxx_record_decl)
4618 num_functions = std::distance(cxx_record_decl->method_begin(),
4619 cxx_record_decl->method_end());
4620 }
4621 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004622
Sean Callananf9c622a2016-09-30 18:44:43 +00004623 case clang::Type::ObjCObjectPointer: {
4624 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004625 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004626 const clang::ObjCInterfaceType *objc_interface_type =
4627 objc_class_type->getInterfaceType();
4628 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004629 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4630 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004631 clang::ObjCInterfaceDecl *class_interface_decl =
4632 objc_interface_type->getDecl();
4633 if (class_interface_decl) {
4634 num_functions = std::distance(class_interface_decl->meth_begin(),
4635 class_interface_decl->meth_end());
Greg Claytond8d4a572015-08-11 21:38:15 +00004636 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004637 }
4638 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004639 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004640
4641 case clang::Type::ObjCObject:
4642 case clang::Type::ObjCInterface:
4643 if (GetCompleteType(type)) {
4644 const clang::ObjCObjectType *objc_class_type =
4645 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4646 if (objc_class_type) {
4647 clang::ObjCInterfaceDecl *class_interface_decl =
4648 objc_class_type->getInterface();
4649 if (class_interface_decl)
4650 num_functions = std::distance(class_interface_decl->meth_begin(),
4651 class_interface_decl->meth_end());
4652 }
4653 }
4654 break;
4655
4656 case clang::Type::Typedef:
4657 return CompilerType(getASTContext(),
4658 llvm::cast<clang::TypedefType>(qual_type)
4659 ->getDecl()
4660 ->getUnderlyingType())
4661 .GetNumMemberFunctions();
4662
4663 case clang::Type::Auto:
4664 return CompilerType(
4665 getASTContext(),
4666 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4667 .GetNumMemberFunctions();
4668
4669 case clang::Type::Elaborated:
4670 return CompilerType(
4671 getASTContext(),
4672 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4673 .GetNumMemberFunctions();
4674
4675 case clang::Type::Paren:
4676 return CompilerType(getASTContext(),
4677 llvm::cast<clang::ParenType>(qual_type)->desugar())
4678 .GetNumMemberFunctions();
4679
4680 default:
4681 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004682 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004683 }
4684 return num_functions;
Greg Claytond8d4a572015-08-11 21:38:15 +00004685}
4686
4687TypeMemberFunctionImpl
Kate Stoneb9c1b512016-09-06 20:57:50 +00004688ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4689 size_t idx) {
4690 std::string name;
4691 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4692 CompilerType clang_type;
4693 CompilerDecl clang_decl;
4694 if (type) {
4695 clang::QualType qual_type(GetCanonicalQualType(type));
4696 switch (qual_type->getTypeClass()) {
4697 case clang::Type::Record:
4698 if (GetCompleteQualType(getASTContext(), qual_type)) {
4699 const clang::RecordType *record_type =
4700 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4701 const clang::RecordDecl *record_decl = record_type->getDecl();
4702 assert(record_decl);
4703 const clang::CXXRecordDecl *cxx_record_decl =
4704 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4705 if (cxx_record_decl) {
4706 auto method_iter = cxx_record_decl->method_begin();
4707 auto method_end = cxx_record_decl->method_end();
4708 if (idx <
4709 static_cast<size_t>(std::distance(method_iter, method_end))) {
4710 std::advance(method_iter, idx);
4711 clang::CXXMethodDecl *cxx_method_decl =
4712 method_iter->getCanonicalDecl();
4713 if (cxx_method_decl) {
4714 name = cxx_method_decl->getDeclName().getAsString();
4715 if (cxx_method_decl->isStatic())
4716 kind = lldb::eMemberFunctionKindStaticMethod;
4717 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4718 kind = lldb::eMemberFunctionKindConstructor;
4719 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4720 kind = lldb::eMemberFunctionKindDestructor;
4721 else
4722 kind = lldb::eMemberFunctionKindInstanceMethod;
4723 clang_type = CompilerType(
4724 this, cxx_method_decl->getType().getAsOpaquePtr());
4725 clang_decl = CompilerDecl(this, cxx_method_decl);
4726 }
4727 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004728 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004729 }
4730 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004731
Sean Callananf9c622a2016-09-30 18:44:43 +00004732 case clang::Type::ObjCObjectPointer: {
4733 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004734 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004735 const clang::ObjCInterfaceType *objc_interface_type =
4736 objc_class_type->getInterfaceType();
4737 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004738 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4739 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004740 clang::ObjCInterfaceDecl *class_interface_decl =
4741 objc_interface_type->getDecl();
4742 if (class_interface_decl) {
4743 auto method_iter = class_interface_decl->meth_begin();
4744 auto method_end = class_interface_decl->meth_end();
4745 if (idx <
4746 static_cast<size_t>(std::distance(method_iter, method_end))) {
4747 std::advance(method_iter, idx);
4748 clang::ObjCMethodDecl *objc_method_decl =
4749 method_iter->getCanonicalDecl();
4750 if (objc_method_decl) {
4751 clang_decl = CompilerDecl(this, objc_method_decl);
4752 name = objc_method_decl->getSelector().getAsString();
4753 if (objc_method_decl->isClassMethod())
4754 kind = lldb::eMemberFunctionKindStaticMethod;
4755 else
4756 kind = lldb::eMemberFunctionKindInstanceMethod;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004757 }
4758 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004759 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004760 }
4761 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004762 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004763
Kate Stoneb9c1b512016-09-06 20:57:50 +00004764 case clang::Type::ObjCObject:
4765 case clang::Type::ObjCInterface:
4766 if (GetCompleteType(type)) {
4767 const clang::ObjCObjectType *objc_class_type =
4768 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4769 if (objc_class_type) {
4770 clang::ObjCInterfaceDecl *class_interface_decl =
4771 objc_class_type->getInterface();
4772 if (class_interface_decl) {
4773 auto method_iter = class_interface_decl->meth_begin();
4774 auto method_end = class_interface_decl->meth_end();
4775 if (idx <
4776 static_cast<size_t>(std::distance(method_iter, method_end))) {
4777 std::advance(method_iter, idx);
4778 clang::ObjCMethodDecl *objc_method_decl =
4779 method_iter->getCanonicalDecl();
4780 if (objc_method_decl) {
4781 clang_decl = CompilerDecl(this, objc_method_decl);
4782 name = objc_method_decl->getSelector().getAsString();
4783 if (objc_method_decl->isClassMethod())
4784 kind = lldb::eMemberFunctionKindStaticMethod;
4785 else
4786 kind = lldb::eMemberFunctionKindInstanceMethod;
4787 }
4788 }
4789 }
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004790 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004791 }
4792 break;
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004793
Kate Stoneb9c1b512016-09-06 20:57:50 +00004794 case clang::Type::Typedef:
4795 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4796 ->getDecl()
4797 ->getUnderlyingType()
4798 .getAsOpaquePtr(),
4799 idx);
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004800
Kate Stoneb9c1b512016-09-06 20:57:50 +00004801 case clang::Type::Auto:
4802 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4803 ->getDeducedType()
4804 .getAsOpaquePtr(),
4805 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004806
Kate Stoneb9c1b512016-09-06 20:57:50 +00004807 case clang::Type::Elaborated:
4808 return GetMemberFunctionAtIndex(
4809 llvm::cast<clang::ElaboratedType>(qual_type)
4810 ->getNamedType()
4811 .getAsOpaquePtr(),
4812 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004813
Kate Stoneb9c1b512016-09-06 20:57:50 +00004814 case clang::Type::Paren:
4815 return GetMemberFunctionAtIndex(
4816 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4817 idx);
4818
4819 default:
4820 break;
Greg Clayton56939cb2015-09-17 22:23:34 +00004821 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004822 }
Greg Clayton56939cb2015-09-17 22:23:34 +00004823
Kate Stoneb9c1b512016-09-06 20:57:50 +00004824 if (kind == eMemberFunctionKindUnknown)
4825 return TypeMemberFunctionImpl();
4826 else
4827 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
Greg Clayton56939cb2015-09-17 22:23:34 +00004828}
4829
Greg Claytona1e5dc82015-08-11 22:53:00 +00004830CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004831ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4832 if (type)
4833 return CompilerType(getASTContext(),
4834 GetQualType(type).getNonReferenceType());
4835 return CompilerType();
4836}
4837
4838CompilerType ClangASTContext::CreateTypedefType(
4839 const CompilerType &type, const char *typedef_name,
4840 const CompilerDeclContext &compiler_decl_ctx) {
4841 if (type && typedef_name && typedef_name[0]) {
4842 ClangASTContext *ast =
4843 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4844 if (!ast)
4845 return CompilerType();
4846 clang::ASTContext *clang_ast = ast->getASTContext();
4847 clang::QualType qual_type(ClangUtil::GetQualType(type));
4848
4849 clang::DeclContext *decl_ctx =
4850 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4851 if (decl_ctx == nullptr)
4852 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4853
4854 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4855 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4856 &clang_ast->Idents.get(typedef_name),
4857 clang_ast->getTrivialTypeSourceInfo(qual_type));
4858
4859 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4860
Aleksandr Urakov709426b2018-09-10 08:08:43 +00004861 decl_ctx->addDecl(decl);
4862
Kate Stoneb9c1b512016-09-06 20:57:50 +00004863 // Get a uniqued clang::QualType for the typedef decl type
4864 return CompilerType(clang_ast, clang_ast->getTypedefType(decl));
4865 }
4866 return CompilerType();
4867}
4868
4869CompilerType
4870ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4871 if (type) {
4872 clang::QualType qual_type(GetQualType(type));
4873 return CompilerType(getASTContext(),
4874 qual_type.getTypePtr()->getPointeeType());
4875 }
4876 return CompilerType();
4877}
4878
4879CompilerType
4880ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4881 if (type) {
4882 clang::QualType qual_type(GetQualType(type));
4883
4884 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4885 switch (type_class) {
4886 case clang::Type::ObjCObject:
4887 case clang::Type::ObjCInterface:
4888 return CompilerType(getASTContext(),
4889 getASTContext()->getObjCObjectPointerType(qual_type));
4890
4891 default:
4892 return CompilerType(getASTContext(),
4893 getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004894 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004895 }
4896 return CompilerType();
4897}
4898
4899CompilerType
4900ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4901 if (type)
4902 return CompilerType(this, getASTContext()
4903 ->getLValueReferenceType(GetQualType(type))
4904 .getAsOpaquePtr());
4905 else
Greg Claytona1e5dc82015-08-11 22:53:00 +00004906 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004907}
4908
Kate Stoneb9c1b512016-09-06 20:57:50 +00004909CompilerType
4910ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4911 if (type)
4912 return CompilerType(this, getASTContext()
4913 ->getRValueReferenceType(GetQualType(type))
4914 .getAsOpaquePtr());
4915 else
4916 return CompilerType();
4917}
4918
4919CompilerType
4920ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4921 if (type) {
4922 clang::QualType result(GetQualType(type));
4923 result.addConst();
4924 return CompilerType(this, result.getAsOpaquePtr());
4925 }
4926 return CompilerType();
4927}
4928
4929CompilerType
4930ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4931 if (type) {
4932 clang::QualType result(GetQualType(type));
4933 result.addVolatile();
4934 return CompilerType(this, result.getAsOpaquePtr());
4935 }
4936 return CompilerType();
4937}
4938
4939CompilerType
4940ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4941 if (type) {
4942 clang::QualType result(GetQualType(type));
4943 result.addRestrict();
4944 return CompilerType(this, result.getAsOpaquePtr());
4945 }
4946 return CompilerType();
4947}
4948
4949CompilerType
4950ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4951 const char *typedef_name,
4952 const CompilerDeclContext &compiler_decl_ctx) {
4953 if (type) {
4954 clang::ASTContext *clang_ast = getASTContext();
4955 clang::QualType qual_type(GetQualType(type));
4956
4957 clang::DeclContext *decl_ctx =
4958 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4959 if (decl_ctx == nullptr)
4960 decl_ctx = getASTContext()->getTranslationUnitDecl();
4961
4962 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4963 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4964 &clang_ast->Idents.get(typedef_name),
4965 clang_ast->getTrivialTypeSourceInfo(qual_type));
4966
4967 clang::TagDecl *tdecl = nullptr;
4968 if (!qual_type.isNull()) {
4969 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4970 tdecl = rt->getDecl();
4971 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4972 tdecl = et->getDecl();
4973 }
4974
4975 // Check whether this declaration is an anonymous struct, union, or enum,
Adrian Prantl05097242018-04-30 16:49:04 +00004976 // hidden behind a typedef. If so, we try to check whether we have a
4977 // typedef tag to attach to the original record declaration
Kate Stoneb9c1b512016-09-06 20:57:50 +00004978 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4979 tdecl->setTypedefNameForAnonDecl(decl);
4980
4981 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4982
4983 // Get a uniqued clang::QualType for the typedef decl type
4984 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4985 }
4986 return CompilerType();
4987}
4988
4989CompilerType
4990ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4991 if (type) {
4992 const clang::TypedefType *typedef_type =
4993 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4994 if (typedef_type)
4995 return CompilerType(getASTContext(),
4996 typedef_type->getDecl()->getUnderlyingType());
4997 }
4998 return CompilerType();
4999}
Greg Claytond8d4a572015-08-11 21:38:15 +00005000
5001//----------------------------------------------------------------------
5002// Create related types using the current type's AST
5003//----------------------------------------------------------------------
5004
Kate Stoneb9c1b512016-09-06 20:57:50 +00005005CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
5006 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005007}
5008//----------------------------------------------------------------------
5009// Exploring the type
5010//----------------------------------------------------------------------
5011
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005012Optional<uint64_t>
5013ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
5014 ExecutionContextScope *exe_scope) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005015 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
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005023 return None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005024 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);
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005068 // Function types actually have a size of 0, that's not an error.
5069 if (qual_type->isFunctionProtoType())
5070 return bit_size;
5071 if (bit_size)
5072 return bit_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005073 }
5074 }
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005075 return None;
Greg Claytond8d4a572015-08-11 21:38:15 +00005076}
5077
Kate Stoneb9c1b512016-09-06 20:57:50 +00005078size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
5079 if (GetCompleteType(type))
5080 return getASTContext()->getTypeAlign(GetQualType(type));
5081 return 0;
5082}
5083
5084lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
5085 uint64_t &count) {
5086 if (!type)
5087 return lldb::eEncodingInvalid;
5088
5089 count = 1;
5090 clang::QualType qual_type(GetCanonicalQualType(type));
5091
5092 switch (qual_type->getTypeClass()) {
5093 case clang::Type::UnaryTransform:
5094 break;
5095
5096 case clang::Type::FunctionNoProto:
5097 case clang::Type::FunctionProto:
5098 break;
5099
5100 case clang::Type::IncompleteArray:
5101 case clang::Type::VariableArray:
5102 break;
5103
5104 case clang::Type::ConstantArray:
5105 break;
5106
Fangrui Song8f284882018-07-13 22:40:40 +00005107 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005108 case clang::Type::ExtVector:
5109 case clang::Type::Vector:
5110 // TODO: Set this to more than one???
5111 break;
5112
5113 case clang::Type::Builtin:
5114 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5115 case clang::BuiltinType::Void:
5116 break;
5117
5118 case clang::BuiltinType::Bool:
5119 case clang::BuiltinType::Char_S:
5120 case clang::BuiltinType::SChar:
5121 case clang::BuiltinType::WChar_S:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005122 case clang::BuiltinType::Short:
5123 case clang::BuiltinType::Int:
5124 case clang::BuiltinType::Long:
5125 case clang::BuiltinType::LongLong:
5126 case clang::BuiltinType::Int128:
5127 return lldb::eEncodingSint;
5128
5129 case clang::BuiltinType::Char_U:
5130 case clang::BuiltinType::UChar:
5131 case clang::BuiltinType::WChar_U:
Richard Smith51d12d82018-05-02 02:43:22 +00005132 case clang::BuiltinType::Char8:
5133 case clang::BuiltinType::Char16:
5134 case clang::BuiltinType::Char32:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005135 case clang::BuiltinType::UShort:
5136 case clang::BuiltinType::UInt:
5137 case clang::BuiltinType::ULong:
5138 case clang::BuiltinType::ULongLong:
5139 case clang::BuiltinType::UInt128:
5140 return lldb::eEncodingUint;
5141
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005142 // Fixed point types. Note that they are currently ignored.
5143 case clang::BuiltinType::ShortAccum:
5144 case clang::BuiltinType::Accum:
5145 case clang::BuiltinType::LongAccum:
5146 case clang::BuiltinType::UShortAccum:
5147 case clang::BuiltinType::UAccum:
5148 case clang::BuiltinType::ULongAccum:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005149 case clang::BuiltinType::ShortFract:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005150 case clang::BuiltinType::Fract:
5151 case clang::BuiltinType::LongFract:
5152 case clang::BuiltinType::UShortFract:
5153 case clang::BuiltinType::UFract:
5154 case clang::BuiltinType::ULongFract:
5155 case clang::BuiltinType::SatShortAccum:
5156 case clang::BuiltinType::SatAccum:
5157 case clang::BuiltinType::SatLongAccum:
5158 case clang::BuiltinType::SatUShortAccum:
5159 case clang::BuiltinType::SatUAccum:
5160 case clang::BuiltinType::SatULongAccum:
5161 case clang::BuiltinType::SatShortFract:
5162 case clang::BuiltinType::SatFract:
5163 case clang::BuiltinType::SatLongFract:
5164 case clang::BuiltinType::SatUShortFract:
5165 case clang::BuiltinType::SatUFract:
5166 case clang::BuiltinType::SatULongFract:
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005167 break;
5168
Kate Stoneb9c1b512016-09-06 20:57:50 +00005169 case clang::BuiltinType::Half:
5170 case clang::BuiltinType::Float:
Ted Woodward4355c7c2017-09-20 19:16:53 +00005171 case clang::BuiltinType::Float16:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005172 case clang::BuiltinType::Float128:
5173 case clang::BuiltinType::Double:
5174 case clang::BuiltinType::LongDouble:
5175 return lldb::eEncodingIEEE754;
5176
5177 case clang::BuiltinType::ObjCClass:
5178 case clang::BuiltinType::ObjCId:
5179 case clang::BuiltinType::ObjCSel:
5180 return lldb::eEncodingUint;
5181
5182 case clang::BuiltinType::NullPtr:
5183 return lldb::eEncodingUint;
5184
5185 case clang::BuiltinType::Kind::ARCUnbridgedCast:
5186 case clang::BuiltinType::Kind::BoundMember:
5187 case clang::BuiltinType::Kind::BuiltinFn:
5188 case clang::BuiltinType::Kind::Dependent:
5189 case clang::BuiltinType::Kind::OCLClkEvent:
5190 case clang::BuiltinType::Kind::OCLEvent:
5191 case clang::BuiltinType::Kind::OCLImage1dRO:
5192 case clang::BuiltinType::Kind::OCLImage1dWO:
5193 case clang::BuiltinType::Kind::OCLImage1dRW:
5194 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
5195 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
5196 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
5197 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
5198 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
5199 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
5200 case clang::BuiltinType::Kind::OCLImage2dRO:
5201 case clang::BuiltinType::Kind::OCLImage2dWO:
5202 case clang::BuiltinType::Kind::OCLImage2dRW:
5203 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
5204 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
5205 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
5206 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
5207 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
5208 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
5209 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5210 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5211 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5212 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5213 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5214 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5215 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5216 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5217 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5218 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5219 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5220 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5221 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5222 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5223 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5224 case clang::BuiltinType::Kind::OCLImage3dRO:
5225 case clang::BuiltinType::Kind::OCLImage3dWO:
5226 case clang::BuiltinType::Kind::OCLImage3dRW:
5227 case clang::BuiltinType::Kind::OCLQueue:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005228 case clang::BuiltinType::Kind::OCLReserveID:
5229 case clang::BuiltinType::Kind::OCLSampler:
5230 case clang::BuiltinType::Kind::OMPArraySection:
5231 case clang::BuiltinType::Kind::Overload:
5232 case clang::BuiltinType::Kind::PseudoObject:
5233 case clang::BuiltinType::Kind::UnknownAny:
5234 break;
Jorge Gorbe Moyaa6e6c182018-11-08 22:04:58 +00005235
5236 case clang::BuiltinType::OCLIntelSubgroupAVCMcePayload:
5237 case clang::BuiltinType::OCLIntelSubgroupAVCImePayload:
5238 case clang::BuiltinType::OCLIntelSubgroupAVCRefPayload:
5239 case clang::BuiltinType::OCLIntelSubgroupAVCSicPayload:
5240 case clang::BuiltinType::OCLIntelSubgroupAVCMceResult:
5241 case clang::BuiltinType::OCLIntelSubgroupAVCImeResult:
5242 case clang::BuiltinType::OCLIntelSubgroupAVCRefResult:
5243 case clang::BuiltinType::OCLIntelSubgroupAVCSicResult:
5244 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultSingleRefStreamout:
5245 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultDualRefStreamout:
5246 case clang::BuiltinType::OCLIntelSubgroupAVCImeSingleRefStreamin:
5247 case clang::BuiltinType::OCLIntelSubgroupAVCImeDualRefStreamin:
5248 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005249 }
5250 break;
Adrian Prantl05097242018-04-30 16:49:04 +00005251 // All pointer types are represented as unsigned integer encodings. We may
5252 // nee to add a eEncodingPointer if we ever need to know the difference
Kate Stoneb9c1b512016-09-06 20:57:50 +00005253 case clang::Type::ObjCObjectPointer:
5254 case clang::Type::BlockPointer:
5255 case clang::Type::Pointer:
5256 case clang::Type::LValueReference:
5257 case clang::Type::RValueReference:
5258 case clang::Type::MemberPointer:
5259 return lldb::eEncodingUint;
5260 case clang::Type::Complex: {
5261 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5262 if (qual_type->isComplexType())
5263 encoding = lldb::eEncodingIEEE754;
5264 else {
5265 const clang::ComplexType *complex_type =
5266 qual_type->getAsComplexIntegerType();
5267 if (complex_type)
5268 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5269 .GetEncoding(count);
5270 else
5271 encoding = lldb::eEncodingSint;
5272 }
5273 count = 2;
5274 return encoding;
5275 }
5276
5277 case clang::Type::ObjCInterface:
5278 break;
5279 case clang::Type::Record:
5280 break;
5281 case clang::Type::Enum:
5282 return lldb::eEncodingSint;
5283 case clang::Type::Typedef:
5284 return CompilerType(getASTContext(),
5285 llvm::cast<clang::TypedefType>(qual_type)
5286 ->getDecl()
5287 ->getUnderlyingType())
5288 .GetEncoding(count);
5289
5290 case clang::Type::Auto:
5291 return CompilerType(
5292 getASTContext(),
5293 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5294 .GetEncoding(count);
5295
5296 case clang::Type::Elaborated:
5297 return CompilerType(
5298 getASTContext(),
5299 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5300 .GetEncoding(count);
5301
5302 case clang::Type::Paren:
5303 return CompilerType(getASTContext(),
5304 llvm::cast<clang::ParenType>(qual_type)->desugar())
5305 .GetEncoding(count);
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005306 case clang::Type::TypeOfExpr:
5307 return CompilerType(getASTContext(),
5308 llvm::cast<clang::TypeOfExprType>(qual_type)
5309 ->getUnderlyingExpr()
5310 ->getType())
5311 .GetEncoding(count);
5312 case clang::Type::TypeOf:
5313 return CompilerType(
5314 getASTContext(),
5315 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5316 .GetEncoding(count);
5317 case clang::Type::Decltype:
5318 return CompilerType(
5319 getASTContext(),
5320 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5321 .GetEncoding(count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005322 case clang::Type::DependentSizedArray:
5323 case clang::Type::DependentSizedExtVector:
5324 case clang::Type::UnresolvedUsing:
5325 case clang::Type::Attributed:
5326 case clang::Type::TemplateTypeParm:
5327 case clang::Type::SubstTemplateTypeParm:
5328 case clang::Type::SubstTemplateTypeParmPack:
5329 case clang::Type::InjectedClassName:
5330 case clang::Type::DependentName:
5331 case clang::Type::DependentTemplateSpecialization:
5332 case clang::Type::PackExpansion:
5333 case clang::Type::ObjCObject:
5334
Kate Stoneb9c1b512016-09-06 20:57:50 +00005335 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005336 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005337 case clang::Type::Atomic:
5338 case clang::Type::Adjusted:
5339 case clang::Type::Pipe:
5340 break;
5341
5342 // pointer type decayed from an array or function type.
5343 case clang::Type::Decayed:
5344 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005345 case clang::Type::ObjCTypeParam:
5346 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005347
5348 case clang::Type::DependentAddressSpace:
5349 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005350 }
5351 count = 0;
5352 return lldb::eEncodingInvalid;
5353}
5354
5355lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5356 if (!type)
5357 return lldb::eFormatDefault;
5358
5359 clang::QualType qual_type(GetCanonicalQualType(type));
5360
5361 switch (qual_type->getTypeClass()) {
5362 case clang::Type::UnaryTransform:
5363 break;
5364
5365 case clang::Type::FunctionNoProto:
5366 case clang::Type::FunctionProto:
5367 break;
5368
5369 case clang::Type::IncompleteArray:
5370 case clang::Type::VariableArray:
5371 break;
5372
5373 case clang::Type::ConstantArray:
5374 return lldb::eFormatVoid; // no value
5375
Fangrui Song8f284882018-07-13 22:40:40 +00005376 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005377 case clang::Type::ExtVector:
5378 case clang::Type::Vector:
5379 break;
5380
5381 case clang::Type::Builtin:
5382 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5383 // default: assert(0 && "Unknown builtin type!");
5384 case clang::BuiltinType::UnknownAny:
5385 case clang::BuiltinType::Void:
5386 case clang::BuiltinType::BoundMember:
5387 break;
5388
5389 case clang::BuiltinType::Bool:
5390 return lldb::eFormatBoolean;
5391 case clang::BuiltinType::Char_S:
5392 case clang::BuiltinType::SChar:
5393 case clang::BuiltinType::WChar_S:
5394 case clang::BuiltinType::Char_U:
5395 case clang::BuiltinType::UChar:
5396 case clang::BuiltinType::WChar_U:
5397 return lldb::eFormatChar;
5398 case clang::BuiltinType::Char16:
5399 return lldb::eFormatUnicode16;
5400 case clang::BuiltinType::Char32:
5401 return lldb::eFormatUnicode32;
5402 case clang::BuiltinType::UShort:
5403 return lldb::eFormatUnsigned;
5404 case clang::BuiltinType::Short:
5405 return lldb::eFormatDecimal;
5406 case clang::BuiltinType::UInt:
5407 return lldb::eFormatUnsigned;
5408 case clang::BuiltinType::Int:
5409 return lldb::eFormatDecimal;
5410 case clang::BuiltinType::ULong:
5411 return lldb::eFormatUnsigned;
5412 case clang::BuiltinType::Long:
5413 return lldb::eFormatDecimal;
5414 case clang::BuiltinType::ULongLong:
5415 return lldb::eFormatUnsigned;
5416 case clang::BuiltinType::LongLong:
5417 return lldb::eFormatDecimal;
5418 case clang::BuiltinType::UInt128:
5419 return lldb::eFormatUnsigned;
5420 case clang::BuiltinType::Int128:
5421 return lldb::eFormatDecimal;
5422 case clang::BuiltinType::Half:
5423 case clang::BuiltinType::Float:
5424 case clang::BuiltinType::Double:
5425 case clang::BuiltinType::LongDouble:
5426 return lldb::eFormatFloat;
5427 default:
5428 return lldb::eFormatHex;
5429 }
5430 break;
5431 case clang::Type::ObjCObjectPointer:
5432 return lldb::eFormatHex;
5433 case clang::Type::BlockPointer:
5434 return lldb::eFormatHex;
5435 case clang::Type::Pointer:
5436 return lldb::eFormatHex;
5437 case clang::Type::LValueReference:
5438 case clang::Type::RValueReference:
5439 return lldb::eFormatHex;
5440 case clang::Type::MemberPointer:
5441 break;
5442 case clang::Type::Complex: {
5443 if (qual_type->isComplexType())
5444 return lldb::eFormatComplex;
5445 else
5446 return lldb::eFormatComplexInteger;
5447 }
5448 case clang::Type::ObjCInterface:
5449 break;
5450 case clang::Type::Record:
5451 break;
5452 case clang::Type::Enum:
5453 return lldb::eFormatEnum;
5454 case clang::Type::Typedef:
5455 return CompilerType(getASTContext(),
5456 llvm::cast<clang::TypedefType>(qual_type)
5457 ->getDecl()
5458 ->getUnderlyingType())
5459 .GetFormat();
5460 case clang::Type::Auto:
5461 return CompilerType(getASTContext(),
5462 llvm::cast<clang::AutoType>(qual_type)->desugar())
5463 .GetFormat();
5464 case clang::Type::Paren:
5465 return CompilerType(getASTContext(),
5466 llvm::cast<clang::ParenType>(qual_type)->desugar())
5467 .GetFormat();
5468 case clang::Type::Elaborated:
5469 return CompilerType(
5470 getASTContext(),
5471 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5472 .GetFormat();
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005473 case clang::Type::TypeOfExpr:
5474 return CompilerType(getASTContext(),
5475 llvm::cast<clang::TypeOfExprType>(qual_type)
5476 ->getUnderlyingExpr()
5477 ->getType())
5478 .GetFormat();
5479 case clang::Type::TypeOf:
5480 return CompilerType(
5481 getASTContext(),
5482 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5483 .GetFormat();
5484 case clang::Type::Decltype:
5485 return CompilerType(
5486 getASTContext(),
5487 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5488 .GetFormat();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005489 case clang::Type::DependentSizedArray:
5490 case clang::Type::DependentSizedExtVector:
5491 case clang::Type::UnresolvedUsing:
5492 case clang::Type::Attributed:
5493 case clang::Type::TemplateTypeParm:
5494 case clang::Type::SubstTemplateTypeParm:
5495 case clang::Type::SubstTemplateTypeParmPack:
5496 case clang::Type::InjectedClassName:
5497 case clang::Type::DependentName:
5498 case clang::Type::DependentTemplateSpecialization:
5499 case clang::Type::PackExpansion:
5500 case clang::Type::ObjCObject:
5501
Kate Stoneb9c1b512016-09-06 20:57:50 +00005502 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005503 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005504 case clang::Type::Atomic:
5505 case clang::Type::Adjusted:
5506 case clang::Type::Pipe:
5507 break;
5508
5509 // pointer type decayed from an array or function type.
5510 case clang::Type::Decayed:
5511 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005512 case clang::Type::ObjCTypeParam:
5513 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005514
5515 case clang::Type::DependentAddressSpace:
5516 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005517 }
5518 // We don't know hot to display this type...
5519 return lldb::eFormatBytes;
5520}
5521
5522static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5523 bool check_superclass) {
5524 while (class_interface_decl) {
5525 if (class_interface_decl->ivar_size() > 0)
5526 return true;
5527
5528 if (check_superclass)
5529 class_interface_decl = class_interface_decl->getSuperClass();
5530 else
5531 break;
5532 }
5533 return false;
5534}
5535
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00005536static Optional<SymbolFile::ArrayInfo>
Adrian Prantleca07c52018-11-05 20:49:07 +00005537GetDynamicArrayInfo(ClangASTContext &ast, SymbolFile *sym_file,
5538 clang::QualType qual_type,
5539 const ExecutionContext *exe_ctx) {
5540 if (qual_type->isIncompleteArrayType())
5541 if (auto *metadata = ast.GetMetadata(qual_type.getAsOpaquePtr()))
Pavel Labathffec31e2018-12-28 13:34:44 +00005542 return sym_file->GetDynamicArrayInfoForUID(metadata->GetUserID(),
5543 exe_ctx);
Adrian Prantleca07c52018-11-05 20:49:07 +00005544 return llvm::None;
5545}
5546
Kate Stoneb9c1b512016-09-06 20:57:50 +00005547uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
Adrian Prantleca07c52018-11-05 20:49:07 +00005548 bool omit_empty_base_classes,
5549 const ExecutionContext *exe_ctx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005550 if (!type)
5551 return 0;
5552
5553 uint32_t num_children = 0;
5554 clang::QualType qual_type(GetQualType(type));
5555 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5556 switch (type_class) {
5557 case clang::Type::Builtin:
5558 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5559 case clang::BuiltinType::ObjCId: // child is Class
5560 case clang::BuiltinType::ObjCClass: // child is Class
5561 num_children = 1;
5562 break;
5563
5564 default:
5565 break;
5566 }
5567 break;
5568
5569 case clang::Type::Complex:
5570 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005571 case clang::Type::Record:
5572 if (GetCompleteQualType(getASTContext(), qual_type)) {
5573 const clang::RecordType *record_type =
5574 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5575 const clang::RecordDecl *record_decl = record_type->getDecl();
5576 assert(record_decl);
5577 const clang::CXXRecordDecl *cxx_record_decl =
5578 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5579 if (cxx_record_decl) {
5580 if (omit_empty_base_classes) {
Adrian Prantl05097242018-04-30 16:49:04 +00005581 // Check each base classes to see if it or any of its base classes
5582 // contain any fields. This can help limit the noise in variable
5583 // views by not having to show base classes that contain no members.
Kate Stoneb9c1b512016-09-06 20:57:50 +00005584 clang::CXXRecordDecl::base_class_const_iterator base_class,
5585 base_class_end;
5586 for (base_class = cxx_record_decl->bases_begin(),
5587 base_class_end = cxx_record_decl->bases_end();
5588 base_class != base_class_end; ++base_class) {
5589 const clang::CXXRecordDecl *base_class_decl =
5590 llvm::cast<clang::CXXRecordDecl>(
5591 base_class->getType()
5592 ->getAs<clang::RecordType>()
5593 ->getDecl());
5594
5595 // Skip empty base classes
Jonas Devliegherea6682a42018-12-15 00:15:33 +00005596 if (!ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00005597 continue;
5598
5599 num_children++;
5600 }
5601 } else {
5602 // Include all base classes
5603 num_children += cxx_record_decl->getNumBases();
5604 }
5605 }
5606 clang::RecordDecl::field_iterator field, field_end;
5607 for (field = record_decl->field_begin(),
5608 field_end = record_decl->field_end();
5609 field != field_end; ++field)
5610 ++num_children;
5611 }
5612 break;
5613
5614 case clang::Type::ObjCObject:
5615 case clang::Type::ObjCInterface:
5616 if (GetCompleteQualType(getASTContext(), qual_type)) {
5617 const clang::ObjCObjectType *objc_class_type =
5618 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5619 assert(objc_class_type);
5620 if (objc_class_type) {
5621 clang::ObjCInterfaceDecl *class_interface_decl =
5622 objc_class_type->getInterface();
5623
5624 if (class_interface_decl) {
5625
5626 clang::ObjCInterfaceDecl *superclass_interface_decl =
5627 class_interface_decl->getSuperClass();
5628 if (superclass_interface_decl) {
5629 if (omit_empty_base_classes) {
5630 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5631 ++num_children;
5632 } else
5633 ++num_children;
5634 }
5635
5636 num_children += class_interface_decl->ivar_size();
5637 }
5638 }
5639 }
5640 break;
5641
5642 case clang::Type::ObjCObjectPointer: {
5643 const clang::ObjCObjectPointerType *pointer_type =
5644 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5645 clang::QualType pointee_type = pointer_type->getPointeeType();
5646 uint32_t num_pointee_children =
5647 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005648 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005649 // If this type points to a simple type, then it has 1 child
5650 if (num_pointee_children == 0)
5651 num_children = 1;
5652 else
5653 num_children = num_pointee_children;
5654 } break;
5655
5656 case clang::Type::Vector:
5657 case clang::Type::ExtVector:
5658 num_children =
5659 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5660 break;
5661
5662 case clang::Type::ConstantArray:
5663 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5664 ->getSize()
5665 .getLimitedValue();
5666 break;
Adrian Prantleca07c52018-11-05 20:49:07 +00005667 case clang::Type::IncompleteArray:
5668 if (auto array_info =
5669 GetDynamicArrayInfo(*this, GetSymbolFile(), qual_type, exe_ctx))
5670 // Only 1-dimensional arrays are supported.
5671 num_children = array_info->element_orders.size()
5672 ? array_info->element_orders.back()
5673 : 0;
5674 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005675
5676 case clang::Type::Pointer: {
5677 const clang::PointerType *pointer_type =
5678 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5679 clang::QualType pointee_type(pointer_type->getPointeeType());
5680 uint32_t num_pointee_children =
5681 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005682 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005683 if (num_pointee_children == 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00005684 // We have a pointer to a pointee type that claims it has no children. We
5685 // will want to look at
Kate Stoneb9c1b512016-09-06 20:57:50 +00005686 num_children = GetNumPointeeChildren(pointee_type);
5687 } else
5688 num_children = num_pointee_children;
5689 } break;
5690
5691 case clang::Type::LValueReference:
5692 case clang::Type::RValueReference: {
5693 const clang::ReferenceType *reference_type =
5694 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5695 clang::QualType pointee_type = reference_type->getPointeeType();
5696 uint32_t num_pointee_children =
5697 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005698 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005699 // If this type points to a simple type, then it has 1 child
5700 if (num_pointee_children == 0)
5701 num_children = 1;
5702 else
5703 num_children = num_pointee_children;
5704 } break;
5705
5706 case clang::Type::Typedef:
5707 num_children =
5708 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5709 ->getDecl()
5710 ->getUnderlyingType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005711 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005712 break;
5713
5714 case clang::Type::Auto:
5715 num_children =
5716 CompilerType(getASTContext(),
5717 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005718 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005719 break;
5720
5721 case clang::Type::Elaborated:
5722 num_children =
5723 CompilerType(
5724 getASTContext(),
5725 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005726 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005727 break;
5728
5729 case clang::Type::Paren:
5730 num_children =
5731 CompilerType(getASTContext(),
5732 llvm::cast<clang::ParenType>(qual_type)->desugar())
Adrian Prantleca07c52018-11-05 20:49:07 +00005733 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005734 break;
5735 default:
5736 break;
5737 }
5738 return num_children;
5739}
5740
5741CompilerType ClangASTContext::GetBuiltinTypeByName(const ConstString &name) {
5742 return GetBasicType(GetBasicTypeEnumeration(name));
Greg Clayton56939cb2015-09-17 22:23:34 +00005743}
5744
Greg Claytond8d4a572015-08-11 21:38:15 +00005745lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00005746ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5747 if (type) {
5748 clang::QualType qual_type(GetQualType(type));
5749 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5750 if (type_class == clang::Type::Builtin) {
5751 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5752 case clang::BuiltinType::Void:
5753 return eBasicTypeVoid;
5754 case clang::BuiltinType::Bool:
5755 return eBasicTypeBool;
5756 case clang::BuiltinType::Char_S:
5757 return eBasicTypeSignedChar;
5758 case clang::BuiltinType::Char_U:
5759 return eBasicTypeUnsignedChar;
5760 case clang::BuiltinType::Char16:
5761 return eBasicTypeChar16;
5762 case clang::BuiltinType::Char32:
5763 return eBasicTypeChar32;
5764 case clang::BuiltinType::UChar:
5765 return eBasicTypeUnsignedChar;
5766 case clang::BuiltinType::SChar:
5767 return eBasicTypeSignedChar;
5768 case clang::BuiltinType::WChar_S:
5769 return eBasicTypeSignedWChar;
5770 case clang::BuiltinType::WChar_U:
5771 return eBasicTypeUnsignedWChar;
5772 case clang::BuiltinType::Short:
5773 return eBasicTypeShort;
5774 case clang::BuiltinType::UShort:
5775 return eBasicTypeUnsignedShort;
5776 case clang::BuiltinType::Int:
5777 return eBasicTypeInt;
5778 case clang::BuiltinType::UInt:
5779 return eBasicTypeUnsignedInt;
5780 case clang::BuiltinType::Long:
5781 return eBasicTypeLong;
5782 case clang::BuiltinType::ULong:
5783 return eBasicTypeUnsignedLong;
5784 case clang::BuiltinType::LongLong:
5785 return eBasicTypeLongLong;
5786 case clang::BuiltinType::ULongLong:
5787 return eBasicTypeUnsignedLongLong;
5788 case clang::BuiltinType::Int128:
5789 return eBasicTypeInt128;
5790 case clang::BuiltinType::UInt128:
5791 return eBasicTypeUnsignedInt128;
5792
5793 case clang::BuiltinType::Half:
5794 return eBasicTypeHalf;
5795 case clang::BuiltinType::Float:
5796 return eBasicTypeFloat;
5797 case clang::BuiltinType::Double:
5798 return eBasicTypeDouble;
5799 case clang::BuiltinType::LongDouble:
5800 return eBasicTypeLongDouble;
5801
5802 case clang::BuiltinType::NullPtr:
5803 return eBasicTypeNullPtr;
5804 case clang::BuiltinType::ObjCId:
5805 return eBasicTypeObjCID;
5806 case clang::BuiltinType::ObjCClass:
5807 return eBasicTypeObjCClass;
5808 case clang::BuiltinType::ObjCSel:
5809 return eBasicTypeObjCSel;
5810 default:
5811 return eBasicTypeOther;
5812 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005813 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005814 }
5815 return eBasicTypeInvalid;
Greg Claytond8d4a572015-08-11 21:38:15 +00005816}
5817
Kate Stoneb9c1b512016-09-06 20:57:50 +00005818void ClangASTContext::ForEachEnumerator(
5819 lldb::opaque_compiler_type_t type,
5820 std::function<bool(const CompilerType &integer_type,
5821 const ConstString &name,
5822 const llvm::APSInt &value)> const &callback) {
5823 const clang::EnumType *enum_type =
5824 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5825 if (enum_type) {
5826 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5827 if (enum_decl) {
5828 CompilerType integer_type(this,
5829 enum_decl->getIntegerType().getAsOpaquePtr());
Greg Clayton99558cc42015-08-24 23:46:31 +00005830
Kate Stoneb9c1b512016-09-06 20:57:50 +00005831 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5832 for (enum_pos = enum_decl->enumerator_begin(),
5833 enum_end_pos = enum_decl->enumerator_end();
5834 enum_pos != enum_end_pos; ++enum_pos) {
5835 ConstString name(enum_pos->getNameAsString().c_str());
5836 if (!callback(integer_type, name, enum_pos->getInitVal()))
5837 break;
5838 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005839 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005840 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005841}
5842
Greg Claytond8d4a572015-08-11 21:38:15 +00005843#pragma mark Aggregate Types
5844
Kate Stoneb9c1b512016-09-06 20:57:50 +00005845uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5846 if (!type)
5847 return 0;
Enrico Granata36f51e42015-12-18 22:41:25 +00005848
Kate Stoneb9c1b512016-09-06 20:57:50 +00005849 uint32_t count = 0;
5850 clang::QualType qual_type(GetCanonicalQualType(type));
5851 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5852 switch (type_class) {
5853 case clang::Type::Record:
5854 if (GetCompleteType(type)) {
5855 const clang::RecordType *record_type =
5856 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5857 if (record_type) {
5858 clang::RecordDecl *record_decl = record_type->getDecl();
5859 if (record_decl) {
5860 uint32_t field_idx = 0;
5861 clang::RecordDecl::field_iterator field, field_end;
5862 for (field = record_decl->field_begin(),
5863 field_end = record_decl->field_end();
5864 field != field_end; ++field)
5865 ++field_idx;
5866 count = field_idx;
5867 }
5868 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005869 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005870 break;
5871
5872 case clang::Type::Typedef:
5873 count =
5874 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5875 ->getDecl()
5876 ->getUnderlyingType())
5877 .GetNumFields();
5878 break;
5879
5880 case clang::Type::Auto:
5881 count =
5882 CompilerType(getASTContext(),
5883 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5884 .GetNumFields();
5885 break;
5886
5887 case clang::Type::Elaborated:
5888 count = CompilerType(
5889 getASTContext(),
5890 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5891 .GetNumFields();
5892 break;
5893
5894 case clang::Type::Paren:
5895 count = CompilerType(getASTContext(),
5896 llvm::cast<clang::ParenType>(qual_type)->desugar())
5897 .GetNumFields();
5898 break;
5899
Sean Callananf9c622a2016-09-30 18:44:43 +00005900 case clang::Type::ObjCObjectPointer: {
5901 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005902 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005903 const clang::ObjCInterfaceType *objc_interface_type =
5904 objc_class_type->getInterfaceType();
5905 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005906 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5907 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005908 clang::ObjCInterfaceDecl *class_interface_decl =
5909 objc_interface_type->getDecl();
5910 if (class_interface_decl) {
5911 count = class_interface_decl->ivar_size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005912 }
5913 }
5914 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005915 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005916
5917 case clang::Type::ObjCObject:
5918 case clang::Type::ObjCInterface:
5919 if (GetCompleteType(type)) {
5920 const clang::ObjCObjectType *objc_class_type =
5921 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5922 if (objc_class_type) {
5923 clang::ObjCInterfaceDecl *class_interface_decl =
5924 objc_class_type->getInterface();
5925
5926 if (class_interface_decl)
5927 count = class_interface_decl->ivar_size();
5928 }
5929 }
5930 break;
5931
5932 default:
5933 break;
5934 }
5935 return count;
Greg Claytond8d4a572015-08-11 21:38:15 +00005936}
5937
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005938static lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005939GetObjCFieldAtIndex(clang::ASTContext *ast,
5940 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5941 std::string &name, uint64_t *bit_offset_ptr,
5942 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5943 if (class_interface_decl) {
5944 if (idx < (class_interface_decl->ivar_size())) {
5945 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5946 ivar_end = class_interface_decl->ivar_end();
5947 uint32_t ivar_idx = 0;
5948
5949 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5950 ++ivar_pos, ++ivar_idx) {
5951 if (ivar_idx == idx) {
5952 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5953
5954 clang::QualType ivar_qual_type(ivar_decl->getType());
5955
5956 name.assign(ivar_decl->getNameAsString());
5957
5958 if (bit_offset_ptr) {
5959 const clang::ASTRecordLayout &interface_layout =
5960 ast->getASTObjCInterfaceLayout(class_interface_decl);
5961 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5962 }
5963
5964 const bool is_bitfield = ivar_pos->isBitField();
5965
5966 if (bitfield_bit_size_ptr) {
5967 *bitfield_bit_size_ptr = 0;
5968
5969 if (is_bitfield && ast) {
5970 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
Hans Wennborg30ce9622018-11-28 14:30:18 +00005971 clang::Expr::EvalResult result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005972 if (bitfield_bit_size_expr &&
Hans Wennborg30ce9622018-11-28 14:30:18 +00005973 bitfield_bit_size_expr->EvaluateAsInt(result, *ast)) {
5974 llvm::APSInt bitfield_apsint = result.Val.getInt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005975 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5976 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005977 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005978 }
5979 if (is_bitfield_ptr)
5980 *is_bitfield_ptr = is_bitfield;
5981
5982 return ivar_qual_type.getAsOpaquePtr();
Greg Claytond8d4a572015-08-11 21:38:15 +00005983 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005984 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005985 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005986 }
5987 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00005988}
5989
Kate Stoneb9c1b512016-09-06 20:57:50 +00005990CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5991 size_t idx, std::string &name,
5992 uint64_t *bit_offset_ptr,
5993 uint32_t *bitfield_bit_size_ptr,
5994 bool *is_bitfield_ptr) {
5995 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005996 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005997
5998 clang::QualType qual_type(GetCanonicalQualType(type));
5999 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6000 switch (type_class) {
6001 case clang::Type::Record:
6002 if (GetCompleteType(type)) {
6003 const clang::RecordType *record_type =
6004 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6005 const clang::RecordDecl *record_decl = record_type->getDecl();
6006 uint32_t field_idx = 0;
6007 clang::RecordDecl::field_iterator field, field_end;
6008 for (field = record_decl->field_begin(),
6009 field_end = record_decl->field_end();
6010 field != field_end; ++field, ++field_idx) {
6011 if (idx == field_idx) {
6012 // Print the member type if requested
6013 // Print the member name and equal sign
6014 name.assign(field->getNameAsString());
6015
6016 // Figure out the type byte size (field_type_info.first) and
6017 // alignment (field_type_info.second) from the AST context.
6018 if (bit_offset_ptr) {
6019 const clang::ASTRecordLayout &record_layout =
6020 getASTContext()->getASTRecordLayout(record_decl);
6021 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
6022 }
6023
6024 const bool is_bitfield = field->isBitField();
6025
6026 if (bitfield_bit_size_ptr) {
6027 *bitfield_bit_size_ptr = 0;
6028
6029 if (is_bitfield) {
6030 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
Hans Wennborg30ce9622018-11-28 14:30:18 +00006031 clang::Expr::EvalResult result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006032 if (bitfield_bit_size_expr &&
Hans Wennborg30ce9622018-11-28 14:30:18 +00006033 bitfield_bit_size_expr->EvaluateAsInt(result,
Kate Stoneb9c1b512016-09-06 20:57:50 +00006034 *getASTContext())) {
Hans Wennborg30ce9622018-11-28 14:30:18 +00006035 llvm::APSInt bitfield_apsint = result.Val.getInt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006036 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
6037 }
6038 }
6039 }
6040 if (is_bitfield_ptr)
6041 *is_bitfield_ptr = is_bitfield;
6042
6043 return CompilerType(getASTContext(), field->getType());
6044 }
6045 }
6046 }
6047 break;
6048
Sean Callananf9c622a2016-09-30 18:44:43 +00006049 case clang::Type::ObjCObjectPointer: {
6050 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00006051 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00006052 const clang::ObjCInterfaceType *objc_interface_type =
6053 objc_class_type->getInterfaceType();
6054 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00006055 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
6056 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00006057 clang::ObjCInterfaceDecl *class_interface_decl =
6058 objc_interface_type->getDecl();
6059 if (class_interface_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006060 return CompilerType(
6061 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
6062 idx, name, bit_offset_ptr,
6063 bitfield_bit_size_ptr, is_bitfield_ptr));
6064 }
6065 }
6066 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00006067 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006068
6069 case clang::Type::ObjCObject:
6070 case clang::Type::ObjCInterface:
6071 if (GetCompleteType(type)) {
6072 const clang::ObjCObjectType *objc_class_type =
6073 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6074 assert(objc_class_type);
6075 if (objc_class_type) {
6076 clang::ObjCInterfaceDecl *class_interface_decl =
6077 objc_class_type->getInterface();
6078 return CompilerType(
6079 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
6080 idx, name, bit_offset_ptr,
6081 bitfield_bit_size_ptr, is_bitfield_ptr));
6082 }
6083 }
6084 break;
6085
6086 case clang::Type::Typedef:
6087 return CompilerType(getASTContext(),
6088 llvm::cast<clang::TypedefType>(qual_type)
6089 ->getDecl()
6090 ->getUnderlyingType())
6091 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6092 is_bitfield_ptr);
6093
6094 case clang::Type::Auto:
6095 return CompilerType(
6096 getASTContext(),
6097 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
6098 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6099 is_bitfield_ptr);
6100
6101 case clang::Type::Elaborated:
6102 return CompilerType(
6103 getASTContext(),
6104 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
6105 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6106 is_bitfield_ptr);
6107
6108 case clang::Type::Paren:
6109 return CompilerType(getASTContext(),
6110 llvm::cast<clang::ParenType>(qual_type)->desugar())
6111 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6112 is_bitfield_ptr);
6113
6114 default:
6115 break;
6116 }
6117 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006118}
6119
Greg Clayton99558cc42015-08-24 23:46:31 +00006120uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006121ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
6122 uint32_t count = 0;
6123 clang::QualType qual_type(GetCanonicalQualType(type));
6124 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6125 switch (type_class) {
6126 case clang::Type::Record:
6127 if (GetCompleteType(type)) {
6128 const clang::CXXRecordDecl *cxx_record_decl =
6129 qual_type->getAsCXXRecordDecl();
6130 if (cxx_record_decl)
6131 count = cxx_record_decl->getNumBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006132 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006133 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006134
Kate Stoneb9c1b512016-09-06 20:57:50 +00006135 case clang::Type::ObjCObjectPointer:
6136 count = GetPointeeType(type).GetNumDirectBaseClasses();
6137 break;
6138
6139 case clang::Type::ObjCObject:
6140 if (GetCompleteType(type)) {
6141 const clang::ObjCObjectType *objc_class_type =
6142 qual_type->getAsObjCQualifiedInterfaceType();
6143 if (objc_class_type) {
6144 clang::ObjCInterfaceDecl *class_interface_decl =
6145 objc_class_type->getInterface();
6146
6147 if (class_interface_decl && class_interface_decl->getSuperClass())
6148 count = 1;
6149 }
6150 }
6151 break;
6152 case clang::Type::ObjCInterface:
6153 if (GetCompleteType(type)) {
6154 const clang::ObjCInterfaceType *objc_interface_type =
6155 qual_type->getAs<clang::ObjCInterfaceType>();
6156 if (objc_interface_type) {
6157 clang::ObjCInterfaceDecl *class_interface_decl =
6158 objc_interface_type->getInterface();
6159
6160 if (class_interface_decl && class_interface_decl->getSuperClass())
6161 count = 1;
6162 }
6163 }
6164 break;
6165
6166 case clang::Type::Typedef:
6167 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6168 ->getDecl()
6169 ->getUnderlyingType()
6170 .getAsOpaquePtr());
6171 break;
6172
6173 case clang::Type::Auto:
6174 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6175 ->getDeducedType()
6176 .getAsOpaquePtr());
6177 break;
6178
6179 case clang::Type::Elaborated:
6180 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6181 ->getNamedType()
6182 .getAsOpaquePtr());
6183 break;
6184
6185 case clang::Type::Paren:
6186 return GetNumDirectBaseClasses(
6187 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6188
6189 default:
6190 break;
6191 }
6192 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006193}
6194
6195uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006196ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
6197 uint32_t count = 0;
6198 clang::QualType qual_type(GetCanonicalQualType(type));
6199 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6200 switch (type_class) {
6201 case clang::Type::Record:
6202 if (GetCompleteType(type)) {
6203 const clang::CXXRecordDecl *cxx_record_decl =
6204 qual_type->getAsCXXRecordDecl();
6205 if (cxx_record_decl)
6206 count = cxx_record_decl->getNumVBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006207 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006208 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006209
Kate Stoneb9c1b512016-09-06 20:57:50 +00006210 case clang::Type::Typedef:
6211 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6212 ->getDecl()
6213 ->getUnderlyingType()
6214 .getAsOpaquePtr());
6215 break;
6216
6217 case clang::Type::Auto:
6218 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6219 ->getDeducedType()
6220 .getAsOpaquePtr());
6221 break;
6222
6223 case clang::Type::Elaborated:
6224 count =
6225 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6226 ->getNamedType()
6227 .getAsOpaquePtr());
6228 break;
6229
6230 case clang::Type::Paren:
6231 count = GetNumVirtualBaseClasses(
6232 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6233 break;
6234
6235 default:
6236 break;
6237 }
6238 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006239}
6240
Kate Stoneb9c1b512016-09-06 20:57:50 +00006241CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
6242 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6243 clang::QualType qual_type(GetCanonicalQualType(type));
6244 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6245 switch (type_class) {
6246 case clang::Type::Record:
6247 if (GetCompleteType(type)) {
6248 const clang::CXXRecordDecl *cxx_record_decl =
6249 qual_type->getAsCXXRecordDecl();
6250 if (cxx_record_decl) {
6251 uint32_t curr_idx = 0;
6252 clang::CXXRecordDecl::base_class_const_iterator base_class,
6253 base_class_end;
6254 for (base_class = cxx_record_decl->bases_begin(),
6255 base_class_end = cxx_record_decl->bases_end();
6256 base_class != base_class_end; ++base_class, ++curr_idx) {
6257 if (curr_idx == idx) {
6258 if (bit_offset_ptr) {
6259 const clang::ASTRecordLayout &record_layout =
6260 getASTContext()->getASTRecordLayout(cxx_record_decl);
6261 const clang::CXXRecordDecl *base_class_decl =
6262 llvm::cast<clang::CXXRecordDecl>(
6263 base_class->getType()
6264 ->getAs<clang::RecordType>()
6265 ->getDecl());
6266 if (base_class->isVirtual())
6267 *bit_offset_ptr =
6268 record_layout.getVBaseClassOffset(base_class_decl)
6269 .getQuantity() *
6270 8;
6271 else
6272 *bit_offset_ptr =
6273 record_layout.getBaseClassOffset(base_class_decl)
6274 .getQuantity() *
6275 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006276 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006277 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6278 }
6279 }
6280 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006281 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006282 break;
6283
6284 case clang::Type::ObjCObjectPointer:
6285 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6286
6287 case clang::Type::ObjCObject:
6288 if (idx == 0 && GetCompleteType(type)) {
6289 const clang::ObjCObjectType *objc_class_type =
6290 qual_type->getAsObjCQualifiedInterfaceType();
6291 if (objc_class_type) {
6292 clang::ObjCInterfaceDecl *class_interface_decl =
6293 objc_class_type->getInterface();
6294
6295 if (class_interface_decl) {
6296 clang::ObjCInterfaceDecl *superclass_interface_decl =
6297 class_interface_decl->getSuperClass();
6298 if (superclass_interface_decl) {
6299 if (bit_offset_ptr)
6300 *bit_offset_ptr = 0;
6301 return CompilerType(getASTContext(),
6302 getASTContext()->getObjCInterfaceType(
6303 superclass_interface_decl));
6304 }
6305 }
6306 }
6307 }
6308 break;
6309 case clang::Type::ObjCInterface:
6310 if (idx == 0 && GetCompleteType(type)) {
6311 const clang::ObjCObjectType *objc_interface_type =
6312 qual_type->getAs<clang::ObjCInterfaceType>();
6313 if (objc_interface_type) {
6314 clang::ObjCInterfaceDecl *class_interface_decl =
6315 objc_interface_type->getInterface();
6316
6317 if (class_interface_decl) {
6318 clang::ObjCInterfaceDecl *superclass_interface_decl =
6319 class_interface_decl->getSuperClass();
6320 if (superclass_interface_decl) {
6321 if (bit_offset_ptr)
6322 *bit_offset_ptr = 0;
6323 return CompilerType(getASTContext(),
6324 getASTContext()->getObjCInterfaceType(
6325 superclass_interface_decl));
6326 }
6327 }
6328 }
6329 }
6330 break;
6331
6332 case clang::Type::Typedef:
6333 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6334 ->getDecl()
6335 ->getUnderlyingType()
6336 .getAsOpaquePtr(),
6337 idx, bit_offset_ptr);
6338
6339 case clang::Type::Auto:
6340 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6341 ->getDeducedType()
6342 .getAsOpaquePtr(),
6343 idx, bit_offset_ptr);
6344
6345 case clang::Type::Elaborated:
6346 return GetDirectBaseClassAtIndex(
6347 llvm::cast<clang::ElaboratedType>(qual_type)
6348 ->getNamedType()
6349 .getAsOpaquePtr(),
6350 idx, bit_offset_ptr);
6351
6352 case clang::Type::Paren:
6353 return GetDirectBaseClassAtIndex(
6354 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6355 idx, bit_offset_ptr);
6356
6357 default:
6358 break;
6359 }
6360 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006361}
6362
Kate Stoneb9c1b512016-09-06 20:57:50 +00006363CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6364 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6365 clang::QualType qual_type(GetCanonicalQualType(type));
6366 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6367 switch (type_class) {
6368 case clang::Type::Record:
6369 if (GetCompleteType(type)) {
6370 const clang::CXXRecordDecl *cxx_record_decl =
6371 qual_type->getAsCXXRecordDecl();
6372 if (cxx_record_decl) {
6373 uint32_t curr_idx = 0;
6374 clang::CXXRecordDecl::base_class_const_iterator base_class,
6375 base_class_end;
6376 for (base_class = cxx_record_decl->vbases_begin(),
6377 base_class_end = cxx_record_decl->vbases_end();
6378 base_class != base_class_end; ++base_class, ++curr_idx) {
6379 if (curr_idx == idx) {
6380 if (bit_offset_ptr) {
6381 const clang::ASTRecordLayout &record_layout =
6382 getASTContext()->getASTRecordLayout(cxx_record_decl);
6383 const clang::CXXRecordDecl *base_class_decl =
6384 llvm::cast<clang::CXXRecordDecl>(
6385 base_class->getType()
6386 ->getAs<clang::RecordType>()
6387 ->getDecl());
6388 *bit_offset_ptr =
6389 record_layout.getVBaseClassOffset(base_class_decl)
6390 .getQuantity() *
6391 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006392 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006393 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6394 }
6395 }
6396 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006397 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006398 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006399
Kate Stoneb9c1b512016-09-06 20:57:50 +00006400 case clang::Type::Typedef:
6401 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6402 ->getDecl()
6403 ->getUnderlyingType()
6404 .getAsOpaquePtr(),
6405 idx, bit_offset_ptr);
6406
6407 case clang::Type::Auto:
6408 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6409 ->getDeducedType()
6410 .getAsOpaquePtr(),
6411 idx, bit_offset_ptr);
6412
6413 case clang::Type::Elaborated:
6414 return GetVirtualBaseClassAtIndex(
6415 llvm::cast<clang::ElaboratedType>(qual_type)
6416 ->getNamedType()
6417 .getAsOpaquePtr(),
6418 idx, bit_offset_ptr);
6419
6420 case clang::Type::Paren:
6421 return GetVirtualBaseClassAtIndex(
6422 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6423 idx, bit_offset_ptr);
6424
6425 default:
6426 break;
6427 }
6428 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006429}
6430
Greg Claytond8d4a572015-08-11 21:38:15 +00006431// If a pointer to a pointee type (the clang_type arg) says that it has no
6432// children, then we either need to trust it, or override it and return a
6433// different result. For example, an "int *" has one child that is an integer,
6434// but a function pointer doesn't have any children. Likewise if a Record type
6435// claims it has no children, then there really is nothing to show.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006436uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6437 if (type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00006438 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006439
6440 clang::QualType qual_type(type.getCanonicalType());
6441 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6442 switch (type_class) {
6443 case clang::Type::Builtin:
6444 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6445 case clang::BuiltinType::UnknownAny:
6446 case clang::BuiltinType::Void:
6447 case clang::BuiltinType::NullPtr:
6448 case clang::BuiltinType::OCLEvent:
6449 case clang::BuiltinType::OCLImage1dRO:
6450 case clang::BuiltinType::OCLImage1dWO:
6451 case clang::BuiltinType::OCLImage1dRW:
6452 case clang::BuiltinType::OCLImage1dArrayRO:
6453 case clang::BuiltinType::OCLImage1dArrayWO:
6454 case clang::BuiltinType::OCLImage1dArrayRW:
6455 case clang::BuiltinType::OCLImage1dBufferRO:
6456 case clang::BuiltinType::OCLImage1dBufferWO:
6457 case clang::BuiltinType::OCLImage1dBufferRW:
6458 case clang::BuiltinType::OCLImage2dRO:
6459 case clang::BuiltinType::OCLImage2dWO:
6460 case clang::BuiltinType::OCLImage2dRW:
6461 case clang::BuiltinType::OCLImage2dArrayRO:
6462 case clang::BuiltinType::OCLImage2dArrayWO:
6463 case clang::BuiltinType::OCLImage2dArrayRW:
6464 case clang::BuiltinType::OCLImage3dRO:
6465 case clang::BuiltinType::OCLImage3dWO:
6466 case clang::BuiltinType::OCLImage3dRW:
6467 case clang::BuiltinType::OCLSampler:
6468 return 0;
6469 case clang::BuiltinType::Bool:
6470 case clang::BuiltinType::Char_U:
6471 case clang::BuiltinType::UChar:
6472 case clang::BuiltinType::WChar_U:
6473 case clang::BuiltinType::Char16:
6474 case clang::BuiltinType::Char32:
6475 case clang::BuiltinType::UShort:
6476 case clang::BuiltinType::UInt:
6477 case clang::BuiltinType::ULong:
6478 case clang::BuiltinType::ULongLong:
6479 case clang::BuiltinType::UInt128:
6480 case clang::BuiltinType::Char_S:
6481 case clang::BuiltinType::SChar:
6482 case clang::BuiltinType::WChar_S:
6483 case clang::BuiltinType::Short:
6484 case clang::BuiltinType::Int:
6485 case clang::BuiltinType::Long:
6486 case clang::BuiltinType::LongLong:
6487 case clang::BuiltinType::Int128:
6488 case clang::BuiltinType::Float:
6489 case clang::BuiltinType::Double:
6490 case clang::BuiltinType::LongDouble:
6491 case clang::BuiltinType::Dependent:
6492 case clang::BuiltinType::Overload:
6493 case clang::BuiltinType::ObjCId:
6494 case clang::BuiltinType::ObjCClass:
6495 case clang::BuiltinType::ObjCSel:
6496 case clang::BuiltinType::BoundMember:
6497 case clang::BuiltinType::Half:
6498 case clang::BuiltinType::ARCUnbridgedCast:
6499 case clang::BuiltinType::PseudoObject:
6500 case clang::BuiltinType::BuiltinFn:
6501 case clang::BuiltinType::OMPArraySection:
6502 return 1;
6503 default:
6504 return 0;
6505 }
6506 break;
6507
6508 case clang::Type::Complex:
6509 return 1;
6510 case clang::Type::Pointer:
6511 return 1;
6512 case clang::Type::BlockPointer:
6513 return 0; // If block pointers don't have debug info, then no children for
6514 // them
6515 case clang::Type::LValueReference:
6516 return 1;
6517 case clang::Type::RValueReference:
6518 return 1;
6519 case clang::Type::MemberPointer:
6520 return 0;
6521 case clang::Type::ConstantArray:
6522 return 0;
6523 case clang::Type::IncompleteArray:
6524 return 0;
6525 case clang::Type::VariableArray:
6526 return 0;
6527 case clang::Type::DependentSizedArray:
6528 return 0;
6529 case clang::Type::DependentSizedExtVector:
6530 return 0;
6531 case clang::Type::Vector:
6532 return 0;
6533 case clang::Type::ExtVector:
6534 return 0;
6535 case clang::Type::FunctionProto:
6536 return 0; // When we function pointers, they have no children...
6537 case clang::Type::FunctionNoProto:
6538 return 0; // When we function pointers, they have no children...
6539 case clang::Type::UnresolvedUsing:
6540 return 0;
6541 case clang::Type::Paren:
6542 return GetNumPointeeChildren(
6543 llvm::cast<clang::ParenType>(qual_type)->desugar());
6544 case clang::Type::Typedef:
6545 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6546 ->getDecl()
6547 ->getUnderlyingType());
6548 case clang::Type::Auto:
6549 return GetNumPointeeChildren(
6550 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6551 case clang::Type::Elaborated:
6552 return GetNumPointeeChildren(
6553 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6554 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006555 return GetNumPointeeChildren(llvm::cast<clang::TypeOfExprType>(qual_type)
6556 ->getUnderlyingExpr()
6557 ->getType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006558 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006559 return GetNumPointeeChildren(
6560 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006561 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006562 return GetNumPointeeChildren(
6563 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006564 case clang::Type::Record:
6565 return 0;
6566 case clang::Type::Enum:
6567 return 1;
6568 case clang::Type::TemplateTypeParm:
6569 return 1;
6570 case clang::Type::SubstTemplateTypeParm:
6571 return 1;
6572 case clang::Type::TemplateSpecialization:
6573 return 1;
6574 case clang::Type::InjectedClassName:
6575 return 0;
6576 case clang::Type::DependentName:
6577 return 1;
6578 case clang::Type::DependentTemplateSpecialization:
6579 return 1;
6580 case clang::Type::ObjCObject:
6581 return 0;
6582 case clang::Type::ObjCInterface:
6583 return 0;
6584 case clang::Type::ObjCObjectPointer:
6585 return 1;
6586 default:
6587 break;
6588 }
6589 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006590}
6591
Kate Stoneb9c1b512016-09-06 20:57:50 +00006592CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6593 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6594 bool transparent_pointers, bool omit_empty_base_classes,
6595 bool ignore_array_bounds, std::string &child_name,
6596 uint32_t &child_byte_size, int32_t &child_byte_offset,
6597 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6598 bool &child_is_base_class, bool &child_is_deref_of_parent,
6599 ValueObject *valobj, uint64_t &language_flags) {
6600 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006601 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006602
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006603 auto get_exe_scope = [&exe_ctx]() {
6604 return exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr;
6605 };
6606
Kate Stoneb9c1b512016-09-06 20:57:50 +00006607 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6608 const clang::Type::TypeClass parent_type_class =
6609 parent_qual_type->getTypeClass();
6610 child_bitfield_bit_size = 0;
6611 child_bitfield_bit_offset = 0;
6612 child_is_base_class = false;
6613 language_flags = 0;
6614
Adrian Prantleca07c52018-11-05 20:49:07 +00006615 const bool idx_is_valid =
6616 idx < GetNumChildren(type, omit_empty_base_classes, exe_ctx);
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00006617 int32_t bit_offset;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006618 switch (parent_type_class) {
6619 case clang::Type::Builtin:
6620 if (idx_is_valid) {
6621 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6622 case clang::BuiltinType::ObjCId:
6623 case clang::BuiltinType::ObjCClass:
6624 child_name = "isa";
6625 child_byte_size =
6626 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6627 CHAR_BIT;
6628 return CompilerType(getASTContext(),
6629 getASTContext()->ObjCBuiltinClassTy);
6630
6631 default:
6632 break;
6633 }
6634 }
6635 break;
6636
6637 case clang::Type::Record:
6638 if (idx_is_valid && GetCompleteType(type)) {
6639 const clang::RecordType *record_type =
6640 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6641 const clang::RecordDecl *record_decl = record_type->getDecl();
6642 assert(record_decl);
6643 const clang::ASTRecordLayout &record_layout =
6644 getASTContext()->getASTRecordLayout(record_decl);
6645 uint32_t child_idx = 0;
6646
6647 const clang::CXXRecordDecl *cxx_record_decl =
6648 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6649 if (cxx_record_decl) {
6650 // We might have base classes to print out first
6651 clang::CXXRecordDecl::base_class_const_iterator base_class,
6652 base_class_end;
6653 for (base_class = cxx_record_decl->bases_begin(),
6654 base_class_end = cxx_record_decl->bases_end();
6655 base_class != base_class_end; ++base_class) {
6656 const clang::CXXRecordDecl *base_class_decl = nullptr;
6657
6658 // Skip empty base classes
6659 if (omit_empty_base_classes) {
6660 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6661 base_class->getType()->getAs<clang::RecordType>()->getDecl());
Jonas Devliegherea6682a42018-12-15 00:15:33 +00006662 if (!ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00006663 continue;
6664 }
6665
6666 if (idx == child_idx) {
6667 if (base_class_decl == nullptr)
6668 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6669 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6670
6671 if (base_class->isVirtual()) {
6672 bool handled = false;
6673 if (valobj) {
Aleksandr Urakov1dc51db2018-11-12 16:23:50 +00006674 clang::VTableContextBase *vtable_ctx =
6675 getASTContext()->getVTableContext();
6676 if (vtable_ctx)
6677 handled = GetVBaseBitOffset(*vtable_ctx, *valobj,
6678 record_layout, cxx_record_decl,
6679 base_class_decl, bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006680 }
6681 if (!handled)
6682 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6683 .getQuantity() *
6684 8;
6685 } else
6686 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6687 .getQuantity() *
6688 8;
6689
6690 // Base classes should be a multiple of 8 bits in size
6691 child_byte_offset = bit_offset / 8;
6692 CompilerType base_class_clang_type(getASTContext(),
6693 base_class->getType());
6694 child_name = base_class_clang_type.GetTypeName().AsCString("");
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006695 Optional<uint64_t> size =
6696 base_class_clang_type.GetBitSize(get_exe_scope());
Adrian Prantld963a7c2019-01-15 18:07:52 +00006697 if (!size)
6698 return {};
6699 uint64_t base_class_clang_type_bit_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006700
6701 // Base classes bit sizes should be a multiple of 8 bits in size
6702 assert(base_class_clang_type_bit_size % 8 == 0);
6703 child_byte_size = base_class_clang_type_bit_size / 8;
6704 child_is_base_class = true;
6705 return base_class_clang_type;
6706 }
6707 // We don't increment the child index in the for loop since we might
6708 // be skipping empty base classes
6709 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006710 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006711 }
6712 // Make sure index is in range...
6713 uint32_t field_idx = 0;
6714 clang::RecordDecl::field_iterator field, field_end;
6715 for (field = record_decl->field_begin(),
6716 field_end = record_decl->field_end();
6717 field != field_end; ++field, ++field_idx, ++child_idx) {
6718 if (idx == child_idx) {
6719 // Print the member type if requested
6720 // Print the member name and equal sign
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006721 child_name.assign(field->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006722
6723 // Figure out the type byte size (field_type_info.first) and
6724 // alignment (field_type_info.second) from the AST context.
6725 CompilerType field_clang_type(getASTContext(), field->getType());
6726 assert(field_idx < record_layout.getFieldCount());
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006727 Optional<uint64_t> size =
6728 field_clang_type.GetByteSize(get_exe_scope());
Adrian Prantld963a7c2019-01-15 18:07:52 +00006729 if (!size)
6730 return {};
6731 child_byte_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006732 const uint32_t child_bit_size = child_byte_size * 8;
6733
6734 // Figure out the field offset within the current struct/union/class
6735 // type
6736 bit_offset = record_layout.getFieldOffset(field_idx);
6737 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6738 child_bitfield_bit_size)) {
6739 child_bitfield_bit_offset = bit_offset % child_bit_size;
6740 const uint32_t child_bit_offset =
6741 bit_offset - child_bitfield_bit_offset;
6742 child_byte_offset = child_bit_offset / 8;
6743 } else {
6744 child_byte_offset = bit_offset / 8;
6745 }
6746
6747 return field_clang_type;
6748 }
6749 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006750 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006751 break;
6752
6753 case clang::Type::ObjCObject:
6754 case clang::Type::ObjCInterface:
6755 if (idx_is_valid && GetCompleteType(type)) {
6756 const clang::ObjCObjectType *objc_class_type =
6757 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6758 assert(objc_class_type);
6759 if (objc_class_type) {
6760 uint32_t child_idx = 0;
6761 clang::ObjCInterfaceDecl *class_interface_decl =
6762 objc_class_type->getInterface();
6763
6764 if (class_interface_decl) {
6765
6766 const clang::ASTRecordLayout &interface_layout =
6767 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6768 clang::ObjCInterfaceDecl *superclass_interface_decl =
6769 class_interface_decl->getSuperClass();
6770 if (superclass_interface_decl) {
6771 if (omit_empty_base_classes) {
6772 CompilerType base_class_clang_type(
6773 getASTContext(), getASTContext()->getObjCInterfaceType(
6774 superclass_interface_decl));
Adrian Prantleca07c52018-11-05 20:49:07 +00006775 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes,
6776 exe_ctx) > 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006777 if (idx == 0) {
6778 clang::QualType ivar_qual_type(
6779 getASTContext()->getObjCInterfaceType(
6780 superclass_interface_decl));
6781
6782 child_name.assign(
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006783 superclass_interface_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006784
6785 clang::TypeInfo ivar_type_info =
6786 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6787
6788 child_byte_size = ivar_type_info.Width / 8;
6789 child_byte_offset = 0;
6790 child_is_base_class = true;
6791
6792 return CompilerType(getASTContext(), ivar_qual_type);
6793 }
6794
6795 ++child_idx;
6796 }
6797 } else
6798 ++child_idx;
6799 }
6800
6801 const uint32_t superclass_idx = child_idx;
6802
6803 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6804 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6805 ivar_end = class_interface_decl->ivar_end();
6806
6807 for (ivar_pos = class_interface_decl->ivar_begin();
6808 ivar_pos != ivar_end; ++ivar_pos) {
6809 if (child_idx == idx) {
6810 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6811
6812 clang::QualType ivar_qual_type(ivar_decl->getType());
6813
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006814 child_name.assign(ivar_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006815
6816 clang::TypeInfo ivar_type_info =
6817 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6818
6819 child_byte_size = ivar_type_info.Width / 8;
6820
6821 // Figure out the field offset within the current
Adrian Prantl05097242018-04-30 16:49:04 +00006822 // struct/union/class type For ObjC objects, we can't trust the
6823 // bit offset we get from the Clang AST, since that doesn't
6824 // account for the space taken up by unbacked properties, or
6825 // from the changing size of base classes that are newer than
6826 // this class. So if we have a process around that we can ask
6827 // about this object, do so.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006828 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6829 Process *process = nullptr;
6830 if (exe_ctx)
6831 process = exe_ctx->GetProcessPtr();
6832 if (process) {
6833 ObjCLanguageRuntime *objc_runtime =
6834 process->GetObjCLanguageRuntime();
6835 if (objc_runtime != nullptr) {
6836 CompilerType parent_ast_type(getASTContext(),
6837 parent_qual_type);
6838 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6839 parent_ast_type, ivar_decl->getNameAsString().c_str());
6840 }
6841 }
6842
Aleksandr Urakovff701722018-08-20 05:59:27 +00006843 // Setting this to INT32_MAX to make sure we don't compute it
Kate Stoneb9c1b512016-09-06 20:57:50 +00006844 // twice...
Aleksandr Urakov53459482018-08-17 07:28:24 +00006845 bit_offset = INT32_MAX;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006846
6847 if (child_byte_offset ==
6848 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6849 bit_offset = interface_layout.getFieldOffset(child_idx -
6850 superclass_idx);
6851 child_byte_offset = bit_offset / 8;
6852 }
6853
6854 // Note, the ObjC Ivar Byte offset is just that, it doesn't
Adrian Prantl05097242018-04-30 16:49:04 +00006855 // account for the bit offset of a bitfield within its
6856 // containing object. So regardless of where we get the byte
Kate Stoneb9c1b512016-09-06 20:57:50 +00006857 // offset from, we still need to get the bit offset for
6858 // bitfields from the layout.
6859
6860 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6861 child_bitfield_bit_size)) {
Aleksandr Urakov53459482018-08-17 07:28:24 +00006862 if (bit_offset == INT32_MAX)
Kate Stoneb9c1b512016-09-06 20:57:50 +00006863 bit_offset = interface_layout.getFieldOffset(
6864 child_idx - superclass_idx);
6865
6866 child_bitfield_bit_offset = bit_offset % 8;
6867 }
6868 return CompilerType(getASTContext(), ivar_qual_type);
6869 }
6870 ++child_idx;
6871 }
6872 }
6873 }
6874 }
6875 }
6876 break;
6877
6878 case clang::Type::ObjCObjectPointer:
6879 if (idx_is_valid) {
6880 CompilerType pointee_clang_type(GetPointeeType(type));
6881
6882 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6883 child_is_deref_of_parent = false;
6884 bool tmp_child_is_deref_of_parent = false;
6885 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6886 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6887 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6888 child_bitfield_bit_size, child_bitfield_bit_offset,
6889 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6890 language_flags);
6891 } else {
6892 child_is_deref_of_parent = true;
6893 const char *parent_name =
6894 valobj ? valobj->GetName().GetCString() : NULL;
6895 if (parent_name) {
6896 child_name.assign(1, '*');
6897 child_name += parent_name;
6898 }
6899
6900 // We have a pointer to an simple type
6901 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006902 if (Optional<uint64_t> size =
6903 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006904 child_byte_size = *size;
6905 child_byte_offset = 0;
6906 return pointee_clang_type;
6907 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006908 }
6909 }
6910 }
6911 break;
6912
6913 case clang::Type::Vector:
6914 case clang::Type::ExtVector:
6915 if (idx_is_valid) {
6916 const clang::VectorType *array =
6917 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6918 if (array) {
6919 CompilerType element_type(getASTContext(), array->getElementType());
6920 if (element_type.GetCompleteType()) {
6921 char element_name[64];
6922 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6923 static_cast<uint64_t>(idx));
6924 child_name.assign(element_name);
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006925 if (Optional<uint64_t> size =
6926 element_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006927 child_byte_size = *size;
6928 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6929 return element_type;
6930 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006931 }
6932 }
6933 }
6934 break;
6935
6936 case clang::Type::ConstantArray:
6937 case clang::Type::IncompleteArray:
6938 if (ignore_array_bounds || idx_is_valid) {
6939 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6940 if (array) {
6941 CompilerType element_type(getASTContext(), array->getElementType());
6942 if (element_type.GetCompleteType()) {
Zachary Turner827d5d72016-12-16 04:27:00 +00006943 child_name = llvm::formatv("[{0}]", idx);
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006944 if (Optional<uint64_t> size =
6945 element_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006946 child_byte_size = *size;
6947 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6948 return element_type;
6949 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006950 }
6951 }
6952 }
6953 break;
6954
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006955 case clang::Type::Pointer: {
6956 CompilerType pointee_clang_type(GetPointeeType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00006957
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006958 // Don't dereference "void *" pointers
6959 if (pointee_clang_type.IsVoidType())
6960 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006961
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006962 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6963 child_is_deref_of_parent = false;
6964 bool tmp_child_is_deref_of_parent = false;
6965 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6966 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6967 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6968 child_bitfield_bit_size, child_bitfield_bit_offset,
6969 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6970 language_flags);
6971 } else {
6972 child_is_deref_of_parent = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006973
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006974 const char *parent_name =
6975 valobj ? valobj->GetName().GetCString() : NULL;
6976 if (parent_name) {
6977 child_name.assign(1, '*');
6978 child_name += parent_name;
6979 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006980
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006981 // We have a pointer to an simple type
6982 if (idx == 0) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006983 if (Optional<uint64_t> size =
6984 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006985 child_byte_size = *size;
6986 child_byte_offset = 0;
6987 return pointee_clang_type;
6988 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006989 }
6990 }
6991 break;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006992 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006993
6994 case clang::Type::LValueReference:
6995 case clang::Type::RValueReference:
6996 if (idx_is_valid) {
6997 const clang::ReferenceType *reference_type =
6998 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
6999 CompilerType pointee_clang_type(getASTContext(),
7000 reference_type->getPointeeType());
7001 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
7002 child_is_deref_of_parent = false;
7003 bool tmp_child_is_deref_of_parent = false;
7004 return pointee_clang_type.GetChildCompilerTypeAtIndex(
7005 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7006 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7007 child_bitfield_bit_size, child_bitfield_bit_offset,
7008 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
7009 language_flags);
7010 } else {
7011 const char *parent_name =
7012 valobj ? valobj->GetName().GetCString() : NULL;
7013 if (parent_name) {
7014 child_name.assign(1, '&');
7015 child_name += parent_name;
7016 }
7017
7018 // We have a pointer to an simple type
7019 if (idx == 0) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00007020 if (Optional<uint64_t> size =
7021 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00007022 child_byte_size = *size;
7023 child_byte_offset = 0;
7024 return pointee_clang_type;
7025 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007026 }
7027 }
7028 }
7029 break;
7030
7031 case clang::Type::Typedef: {
7032 CompilerType typedefed_clang_type(
7033 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)
7034 ->getDecl()
7035 ->getUnderlyingType());
7036 return typedefed_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 } break;
7042
7043 case clang::Type::Auto: {
7044 CompilerType elaborated_clang_type(
7045 getASTContext(),
7046 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
7047 return elaborated_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 case clang::Type::Elaborated: {
7055 CompilerType elaborated_clang_type(
7056 getASTContext(),
7057 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
7058 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
7059 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7060 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7061 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7062 child_is_deref_of_parent, valobj, language_flags);
7063 }
7064
7065 case clang::Type::Paren: {
7066 CompilerType paren_clang_type(
7067 getASTContext(),
7068 llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
7069 return paren_clang_type.GetChildCompilerTypeAtIndex(
7070 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7071 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7072 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7073 child_is_deref_of_parent, valobj, language_flags);
7074 }
7075
7076 default:
7077 break;
7078 }
7079 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007080}
7081
Kate Stoneb9c1b512016-09-06 20:57:50 +00007082static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
7083 const clang::CXXBaseSpecifier *base_spec,
7084 bool omit_empty_base_classes) {
7085 uint32_t child_idx = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007086
Kate Stoneb9c1b512016-09-06 20:57:50 +00007087 const clang::CXXRecordDecl *cxx_record_decl =
7088 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7089
7090 // const char *super_name = record_decl->getNameAsCString();
7091 // const char *base_name =
7092 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
7093 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
7094 //
7095 if (cxx_record_decl) {
7096 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
7097 for (base_class = cxx_record_decl->bases_begin(),
7098 base_class_end = cxx_record_decl->bases_end();
7099 base_class != base_class_end; ++base_class) {
7100 if (omit_empty_base_classes) {
7101 if (BaseSpecifierIsEmpty(base_class))
7102 continue;
7103 }
7104
7105 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
7106 // super_name, base_name,
7107 // child_idx,
7108 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
7109 //
7110 //
7111 if (base_class == base_spec)
7112 return child_idx;
7113 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00007114 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007115 }
7116
7117 return UINT32_MAX;
7118}
7119
7120static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
7121 clang::NamedDecl *canonical_decl,
7122 bool omit_empty_base_classes) {
7123 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
7124 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
7125 omit_empty_base_classes);
7126
7127 clang::RecordDecl::field_iterator field, field_end;
7128 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
7129 field != field_end; ++field, ++child_idx) {
7130 if (field->getCanonicalDecl() == canonical_decl)
7131 return child_idx;
7132 }
7133
7134 return UINT32_MAX;
Greg Claytond8d4a572015-08-11 21:38:15 +00007135}
7136
7137// Look for a child member (doesn't include base classes, but it does include
Adrian Prantl05097242018-04-30 16:49:04 +00007138// their members) in the type hierarchy. Returns an index path into
7139// "clang_type" on how to reach the appropriate member.
Greg Claytond8d4a572015-08-11 21:38:15 +00007140//
7141// class A
7142// {
7143// public:
7144// int m_a;
7145// int m_b;
7146// };
7147//
7148// class B
7149// {
7150// };
7151//
7152// class C :
7153// public B,
7154// public A
7155// {
7156// };
7157//
7158// If we have a clang type that describes "class C", and we wanted to looked
7159// "m_b" in it:
7160//
Kate Stoneb9c1b512016-09-06 20:57:50 +00007161// With omit_empty_base_classes == false we would get an integer array back
Adrian Prantl05097242018-04-30 16:49:04 +00007162// with: { 1, 1 } The first index 1 is the child index for "class A" within
7163// class C The second index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007164//
Adrian Prantl05097242018-04-30 16:49:04 +00007165// With omit_empty_base_classes == true we would get an integer array back
7166// with: { 0, 1 } The first index 0 is the child index for "class A" within
7167// class C (since class B doesn't have any members it doesn't count) The second
7168// index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007169
Kate Stoneb9c1b512016-09-06 20:57:50 +00007170size_t ClangASTContext::GetIndexOfChildMemberWithName(
7171 lldb::opaque_compiler_type_t type, const char *name,
7172 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
7173 if (type && name && name[0]) {
7174 clang::QualType qual_type(GetCanonicalQualType(type));
7175 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7176 switch (type_class) {
7177 case clang::Type::Record:
7178 if (GetCompleteType(type)) {
7179 const clang::RecordType *record_type =
7180 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7181 const clang::RecordDecl *record_decl = record_type->getDecl();
Enrico Granata36f51e42015-12-18 22:41:25 +00007182
Kate Stoneb9c1b512016-09-06 20:57:50 +00007183 assert(record_decl);
7184 uint32_t child_idx = 0;
7185
7186 const clang::CXXRecordDecl *cxx_record_decl =
7187 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7188
7189 // Try and find a field that matches NAME
7190 clang::RecordDecl::field_iterator field, field_end;
7191 llvm::StringRef name_sref(name);
7192 for (field = record_decl->field_begin(),
7193 field_end = record_decl->field_end();
7194 field != field_end; ++field, ++child_idx) {
7195 llvm::StringRef field_name = field->getName();
7196 if (field_name.empty()) {
7197 CompilerType field_type(getASTContext(), field->getType());
7198 child_indexes.push_back(child_idx);
7199 if (field_type.GetIndexOfChildMemberWithName(
7200 name, omit_empty_base_classes, child_indexes))
7201 return child_indexes.size();
7202 child_indexes.pop_back();
7203
7204 } else if (field_name.equals(name_sref)) {
7205 // We have to add on the number of base classes to this index!
7206 child_indexes.push_back(
7207 child_idx + ClangASTContext::GetNumBaseClasses(
7208 cxx_record_decl, omit_empty_base_classes));
7209 return child_indexes.size();
7210 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007211 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007212
Kate Stoneb9c1b512016-09-06 20:57:50 +00007213 if (cxx_record_decl) {
7214 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7215
7216 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7217
7218 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7219 // Didn't find things easily, lets let clang do its thang...
7220 clang::IdentifierInfo &ident_ref =
7221 getASTContext()->Idents.get(name_sref);
7222 clang::DeclarationName decl_name(&ident_ref);
7223
7224 clang::CXXBasePaths paths;
7225 if (cxx_record_decl->lookupInBases(
7226 [decl_name](const clang::CXXBaseSpecifier *specifier,
7227 clang::CXXBasePath &path) {
7228 return clang::CXXRecordDecl::FindOrdinaryMember(
7229 specifier, path, decl_name);
7230 },
7231 paths)) {
7232 clang::CXXBasePaths::const_paths_iterator path,
7233 path_end = paths.end();
7234 for (path = paths.begin(); path != path_end; ++path) {
7235 const size_t num_path_elements = path->size();
7236 for (size_t e = 0; e < num_path_elements; ++e) {
7237 clang::CXXBasePathElement elem = (*path)[e];
7238
7239 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7240 omit_empty_base_classes);
7241 if (child_idx == UINT32_MAX) {
7242 child_indexes.clear();
7243 return 0;
7244 } else {
7245 child_indexes.push_back(child_idx);
7246 parent_record_decl = llvm::cast<clang::RecordDecl>(
7247 elem.Base->getType()
7248 ->getAs<clang::RecordType>()
7249 ->getDecl());
7250 }
7251 }
7252 for (clang::NamedDecl *path_decl : path->Decls) {
7253 child_idx = GetIndexForRecordChild(
7254 parent_record_decl, path_decl, omit_empty_base_classes);
7255 if (child_idx == UINT32_MAX) {
7256 child_indexes.clear();
7257 return 0;
7258 } else {
7259 child_indexes.push_back(child_idx);
7260 }
7261 }
7262 }
7263 return child_indexes.size();
7264 }
7265 }
7266 }
7267 break;
7268
7269 case clang::Type::ObjCObject:
7270 case clang::Type::ObjCInterface:
7271 if (GetCompleteType(type)) {
7272 llvm::StringRef name_sref(name);
7273 const clang::ObjCObjectType *objc_class_type =
7274 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7275 assert(objc_class_type);
7276 if (objc_class_type) {
7277 uint32_t child_idx = 0;
7278 clang::ObjCInterfaceDecl *class_interface_decl =
7279 objc_class_type->getInterface();
7280
7281 if (class_interface_decl) {
7282 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7283 ivar_end = class_interface_decl->ivar_end();
7284 clang::ObjCInterfaceDecl *superclass_interface_decl =
7285 class_interface_decl->getSuperClass();
7286
7287 for (ivar_pos = class_interface_decl->ivar_begin();
7288 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7289 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7290
7291 if (ivar_decl->getName().equals(name_sref)) {
7292 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7293 (omit_empty_base_classes &&
7294 ObjCDeclHasIVars(superclass_interface_decl, true)))
7295 ++child_idx;
7296
7297 child_indexes.push_back(child_idx);
7298 return child_indexes.size();
7299 }
7300 }
7301
7302 if (superclass_interface_decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00007303 // The super class index is always zero for ObjC classes, so we
7304 // push it onto the child indexes in case we find an ivar in our
7305 // superclass...
Kate Stoneb9c1b512016-09-06 20:57:50 +00007306 child_indexes.push_back(0);
7307
7308 CompilerType superclass_clang_type(
7309 getASTContext(), getASTContext()->getObjCInterfaceType(
7310 superclass_interface_decl));
7311 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7312 name, omit_empty_base_classes, child_indexes)) {
Adrian Prantl05097242018-04-30 16:49:04 +00007313 // We did find an ivar in a superclass so just return the
7314 // results!
Kate Stoneb9c1b512016-09-06 20:57:50 +00007315 return child_indexes.size();
7316 }
7317
Adrian Prantl05097242018-04-30 16:49:04 +00007318 // We didn't find an ivar matching "name" in our superclass, pop
7319 // the superclass zero index that we pushed on above.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007320 child_indexes.pop_back();
7321 }
7322 }
7323 }
7324 }
7325 break;
7326
7327 case clang::Type::ObjCObjectPointer: {
7328 CompilerType objc_object_clang_type(
7329 getASTContext(),
7330 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7331 ->getPointeeType());
7332 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7333 name, omit_empty_base_classes, child_indexes);
7334 } break;
7335
7336 case clang::Type::ConstantArray: {
7337 // const clang::ConstantArrayType *array =
7338 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7339 // const uint64_t element_count =
7340 // array->getSize().getLimitedValue();
7341 //
7342 // if (idx < element_count)
7343 // {
7344 // std::pair<uint64_t, unsigned> field_type_info =
7345 // ast->getTypeInfo(array->getElementType());
7346 //
7347 // char element_name[32];
7348 // ::snprintf (element_name, sizeof (element_name),
7349 // "%s[%u]", parent_name ? parent_name : "", idx);
7350 //
7351 // child_name.assign(element_name);
7352 // assert(field_type_info.first % 8 == 0);
7353 // child_byte_size = field_type_info.first / 8;
7354 // child_byte_offset = idx * child_byte_size;
7355 // return array->getElementType().getAsOpaquePtr();
7356 // }
7357 } break;
7358
7359 // case clang::Type::MemberPointerType:
7360 // {
7361 // MemberPointerType *mem_ptr_type =
7362 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7363 // clang::QualType pointee_type =
7364 // mem_ptr_type->getPointeeType();
7365 //
7366 // if (ClangASTContext::IsAggregateType
7367 // (pointee_type.getAsOpaquePtr()))
7368 // {
7369 // return GetIndexOfChildWithName (ast,
7370 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7371 // name);
7372 // }
7373 // }
7374 // break;
7375 //
7376 case clang::Type::LValueReference:
7377 case clang::Type::RValueReference: {
7378 const clang::ReferenceType *reference_type =
7379 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7380 clang::QualType pointee_type(reference_type->getPointeeType());
7381 CompilerType pointee_clang_type(getASTContext(), pointee_type);
7382
7383 if (pointee_clang_type.IsAggregateType()) {
7384 return pointee_clang_type.GetIndexOfChildMemberWithName(
7385 name, omit_empty_base_classes, child_indexes);
7386 }
7387 } break;
7388
7389 case clang::Type::Pointer: {
7390 CompilerType pointee_clang_type(GetPointeeType(type));
7391
7392 if (pointee_clang_type.IsAggregateType()) {
7393 return pointee_clang_type.GetIndexOfChildMemberWithName(
7394 name, omit_empty_base_classes, child_indexes);
7395 }
7396 } break;
7397
7398 case clang::Type::Typedef:
7399 return CompilerType(getASTContext(),
7400 llvm::cast<clang::TypedefType>(qual_type)
7401 ->getDecl()
7402 ->getUnderlyingType())
7403 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7404 child_indexes);
7405
7406 case clang::Type::Auto:
7407 return CompilerType(
7408 getASTContext(),
7409 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7410 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7411 child_indexes);
7412
7413 case clang::Type::Elaborated:
7414 return CompilerType(
7415 getASTContext(),
7416 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7417 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7418 child_indexes);
7419
7420 case clang::Type::Paren:
7421 return CompilerType(getASTContext(),
7422 llvm::cast<clang::ParenType>(qual_type)->desugar())
7423 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7424 child_indexes);
7425
7426 default:
7427 break;
7428 }
7429 }
7430 return 0;
7431}
Greg Claytond8d4a572015-08-11 21:38:15 +00007432
7433// Get the index of the child of "clang_type" whose name matches. This function
7434// doesn't descend into the children, but only looks one level deep and name
7435// matches can include base class names.
7436
7437uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007438ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7439 const char *name,
7440 bool omit_empty_base_classes) {
7441 if (type && name && name[0]) {
7442 clang::QualType qual_type(GetCanonicalQualType(type));
Enrico Granata36f51e42015-12-18 22:41:25 +00007443
Kate Stoneb9c1b512016-09-06 20:57:50 +00007444 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7445
7446 switch (type_class) {
7447 case clang::Type::Record:
7448 if (GetCompleteType(type)) {
7449 const clang::RecordType *record_type =
7450 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7451 const clang::RecordDecl *record_decl = record_type->getDecl();
7452
7453 assert(record_decl);
7454 uint32_t child_idx = 0;
7455
7456 const clang::CXXRecordDecl *cxx_record_decl =
7457 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7458
7459 if (cxx_record_decl) {
7460 clang::CXXRecordDecl::base_class_const_iterator base_class,
7461 base_class_end;
7462 for (base_class = cxx_record_decl->bases_begin(),
7463 base_class_end = cxx_record_decl->bases_end();
7464 base_class != base_class_end; ++base_class) {
7465 // Skip empty base classes
7466 clang::CXXRecordDecl *base_class_decl =
7467 llvm::cast<clang::CXXRecordDecl>(
7468 base_class->getType()
7469 ->getAs<clang::RecordType>()
7470 ->getDecl());
7471 if (omit_empty_base_classes &&
Jonas Devliegherea6682a42018-12-15 00:15:33 +00007472 !ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00007473 continue;
7474
7475 CompilerType base_class_clang_type(getASTContext(),
7476 base_class->getType());
7477 std::string base_class_type_name(
7478 base_class_clang_type.GetTypeName().AsCString(""));
Jonas Devlieghere8d20cfd2018-12-21 22:46:10 +00007479 if (base_class_type_name == name)
Kate Stoneb9c1b512016-09-06 20:57:50 +00007480 return child_idx;
7481 ++child_idx;
7482 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007483 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007484
Kate Stoneb9c1b512016-09-06 20:57:50 +00007485 // Try and find a field that matches NAME
7486 clang::RecordDecl::field_iterator field, field_end;
7487 llvm::StringRef name_sref(name);
7488 for (field = record_decl->field_begin(),
7489 field_end = record_decl->field_end();
7490 field != field_end; ++field, ++child_idx) {
7491 if (field->getName().equals(name_sref))
7492 return child_idx;
7493 }
7494 }
7495 break;
7496
7497 case clang::Type::ObjCObject:
7498 case clang::Type::ObjCInterface:
7499 if (GetCompleteType(type)) {
7500 llvm::StringRef name_sref(name);
7501 const clang::ObjCObjectType *objc_class_type =
7502 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7503 assert(objc_class_type);
7504 if (objc_class_type) {
7505 uint32_t child_idx = 0;
7506 clang::ObjCInterfaceDecl *class_interface_decl =
7507 objc_class_type->getInterface();
7508
7509 if (class_interface_decl) {
7510 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7511 ivar_end = class_interface_decl->ivar_end();
7512 clang::ObjCInterfaceDecl *superclass_interface_decl =
7513 class_interface_decl->getSuperClass();
7514
7515 for (ivar_pos = class_interface_decl->ivar_begin();
7516 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7517 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7518
7519 if (ivar_decl->getName().equals(name_sref)) {
7520 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7521 (omit_empty_base_classes &&
7522 ObjCDeclHasIVars(superclass_interface_decl, true)))
7523 ++child_idx;
7524
7525 return child_idx;
7526 }
7527 }
7528
7529 if (superclass_interface_decl) {
7530 if (superclass_interface_decl->getName().equals(name_sref))
7531 return 0;
7532 }
7533 }
7534 }
7535 }
7536 break;
7537
7538 case clang::Type::ObjCObjectPointer: {
7539 CompilerType pointee_clang_type(
7540 getASTContext(),
7541 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7542 ->getPointeeType());
7543 return pointee_clang_type.GetIndexOfChildWithName(
7544 name, omit_empty_base_classes);
7545 } break;
7546
7547 case clang::Type::ConstantArray: {
7548 // const clang::ConstantArrayType *array =
7549 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7550 // const uint64_t element_count =
7551 // array->getSize().getLimitedValue();
7552 //
7553 // if (idx < element_count)
7554 // {
7555 // std::pair<uint64_t, unsigned> field_type_info =
7556 // ast->getTypeInfo(array->getElementType());
7557 //
7558 // char element_name[32];
7559 // ::snprintf (element_name, sizeof (element_name),
7560 // "%s[%u]", parent_name ? parent_name : "", idx);
7561 //
7562 // child_name.assign(element_name);
7563 // assert(field_type_info.first % 8 == 0);
7564 // child_byte_size = field_type_info.first / 8;
7565 // child_byte_offset = idx * child_byte_size;
7566 // return array->getElementType().getAsOpaquePtr();
7567 // }
7568 } break;
7569
7570 // case clang::Type::MemberPointerType:
7571 // {
7572 // MemberPointerType *mem_ptr_type =
7573 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7574 // clang::QualType pointee_type =
7575 // mem_ptr_type->getPointeeType();
7576 //
7577 // if (ClangASTContext::IsAggregateType
7578 // (pointee_type.getAsOpaquePtr()))
7579 // {
7580 // return GetIndexOfChildWithName (ast,
7581 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7582 // name);
7583 // }
7584 // }
7585 // break;
7586 //
7587 case clang::Type::LValueReference:
7588 case clang::Type::RValueReference: {
7589 const clang::ReferenceType *reference_type =
7590 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7591 CompilerType pointee_type(getASTContext(),
7592 reference_type->getPointeeType());
7593
7594 if (pointee_type.IsAggregateType()) {
7595 return pointee_type.GetIndexOfChildWithName(name,
7596 omit_empty_base_classes);
7597 }
7598 } break;
7599
7600 case clang::Type::Pointer: {
7601 const clang::PointerType *pointer_type =
7602 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7603 CompilerType pointee_type(getASTContext(),
7604 pointer_type->getPointeeType());
7605
7606 if (pointee_type.IsAggregateType()) {
7607 return pointee_type.GetIndexOfChildWithName(name,
7608 omit_empty_base_classes);
7609 } else {
7610 // if (parent_name)
7611 // {
7612 // child_name.assign(1, '*');
7613 // child_name += parent_name;
7614 // }
7615 //
7616 // // We have a pointer to an simple type
7617 // if (idx == 0)
7618 // {
7619 // std::pair<uint64_t, unsigned> clang_type_info
7620 // = ast->getTypeInfo(pointee_type);
7621 // assert(clang_type_info.first % 8 == 0);
7622 // child_byte_size = clang_type_info.first / 8;
7623 // child_byte_offset = 0;
7624 // return pointee_type.getAsOpaquePtr();
7625 // }
7626 }
7627 } break;
7628
7629 case clang::Type::Auto:
7630 return CompilerType(
7631 getASTContext(),
7632 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7633 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7634
7635 case clang::Type::Elaborated:
7636 return CompilerType(
7637 getASTContext(),
7638 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7639 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7640
7641 case clang::Type::Paren:
7642 return CompilerType(getASTContext(),
7643 llvm::cast<clang::ParenType>(qual_type)->desugar())
7644 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7645
7646 case clang::Type::Typedef:
7647 return CompilerType(getASTContext(),
7648 llvm::cast<clang::TypedefType>(qual_type)
7649 ->getDecl()
7650 ->getUnderlyingType())
7651 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7652
7653 default:
7654 break;
7655 }
7656 }
7657 return UINT32_MAX;
7658}
Greg Claytond8d4a572015-08-11 21:38:15 +00007659
7660size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007661ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7662 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007663 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007664
Kate Stoneb9c1b512016-09-06 20:57:50 +00007665 clang::QualType qual_type(GetCanonicalQualType(type));
7666 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7667 switch (type_class) {
7668 case clang::Type::Record:
7669 if (GetCompleteType(type)) {
7670 const clang::CXXRecordDecl *cxx_record_decl =
7671 qual_type->getAsCXXRecordDecl();
7672 if (cxx_record_decl) {
7673 const clang::ClassTemplateSpecializationDecl *template_decl =
7674 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7675 cxx_record_decl);
7676 if (template_decl)
7677 return template_decl->getTemplateArgs().size();
7678 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007679 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007680 break;
7681
7682 case clang::Type::Typedef:
7683 return (CompilerType(getASTContext(),
7684 llvm::cast<clang::TypedefType>(qual_type)
7685 ->getDecl()
7686 ->getUnderlyingType()))
7687 .GetNumTemplateArguments();
7688
7689 case clang::Type::Auto:
7690 return (CompilerType(
7691 getASTContext(),
7692 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7693 .GetNumTemplateArguments();
7694
7695 case clang::Type::Elaborated:
7696 return (CompilerType(
7697 getASTContext(),
7698 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7699 .GetNumTemplateArguments();
7700
7701 case clang::Type::Paren:
7702 return (CompilerType(getASTContext(),
7703 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7704 .GetNumTemplateArguments();
7705
7706 default:
7707 break;
7708 }
7709
7710 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007711}
7712
Pavel Labath769b21e2017-11-13 14:26:21 +00007713const clang::ClassTemplateSpecializationDecl *
7714ClangASTContext::GetAsTemplateSpecialization(
7715 lldb::opaque_compiler_type_t type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00007716 if (!type)
Pavel Labath769b21e2017-11-13 14:26:21 +00007717 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007718
7719 clang::QualType qual_type(GetCanonicalQualType(type));
7720 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7721 switch (type_class) {
Pavel Labath769b21e2017-11-13 14:26:21 +00007722 case clang::Type::Record: {
7723 if (! GetCompleteType(type))
7724 return nullptr;
7725 const clang::CXXRecordDecl *cxx_record_decl =
7726 qual_type->getAsCXXRecordDecl();
7727 if (!cxx_record_decl)
7728 return nullptr;
7729 return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7730 cxx_record_decl);
7731 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007732
7733 case clang::Type::Typedef:
Pavel Labath769b21e2017-11-13 14:26:21 +00007734 return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type)
7735 ->getDecl()
7736 ->getUnderlyingType()
7737 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007738
7739 case clang::Type::Auto:
Pavel Labath769b21e2017-11-13 14:26:21 +00007740 return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type)
7741 ->getDeducedType()
7742 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007743
7744 case clang::Type::Elaborated:
Pavel Labath769b21e2017-11-13 14:26:21 +00007745 return GetAsTemplateSpecialization(
7746 llvm::cast<clang::ElaboratedType>(qual_type)
7747 ->getNamedType()
7748 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007749
7750 case clang::Type::Paren:
Pavel Labath769b21e2017-11-13 14:26:21 +00007751 return GetAsTemplateSpecialization(
7752 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007753
7754 default:
Pavel Labath769b21e2017-11-13 14:26:21 +00007755 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007756 }
Pavel Labath769b21e2017-11-13 14:26:21 +00007757}
7758
7759lldb::TemplateArgumentKind
7760ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
7761 size_t arg_idx) {
7762 const clang::ClassTemplateSpecializationDecl *template_decl =
7763 GetAsTemplateSpecialization(type);
7764 if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size())
7765 return eTemplateArgumentKindNull;
7766
7767 switch (template_decl->getTemplateArgs()[arg_idx].getKind()) {
7768 case clang::TemplateArgument::Null:
7769 return eTemplateArgumentKindNull;
7770
7771 case clang::TemplateArgument::NullPtr:
7772 return eTemplateArgumentKindNullPtr;
7773
7774 case clang::TemplateArgument::Type:
7775 return eTemplateArgumentKindType;
7776
7777 case clang::TemplateArgument::Declaration:
7778 return eTemplateArgumentKindDeclaration;
7779
7780 case clang::TemplateArgument::Integral:
7781 return eTemplateArgumentKindIntegral;
7782
7783 case clang::TemplateArgument::Template:
7784 return eTemplateArgumentKindTemplate;
7785
7786 case clang::TemplateArgument::TemplateExpansion:
7787 return eTemplateArgumentKindTemplateExpansion;
7788
7789 case clang::TemplateArgument::Expression:
7790 return eTemplateArgumentKindExpression;
7791
7792 case clang::TemplateArgument::Pack:
7793 return eTemplateArgumentKindPack;
7794 }
7795 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
7796}
7797
7798CompilerType
7799ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
7800 size_t idx) {
7801 const clang::ClassTemplateSpecializationDecl *template_decl =
7802 GetAsTemplateSpecialization(type);
7803 if (!template_decl || idx >= template_decl->getTemplateArgs().size())
7804 return CompilerType();
7805
7806 const clang::TemplateArgument &template_arg =
7807 template_decl->getTemplateArgs()[idx];
7808 if (template_arg.getKind() != clang::TemplateArgument::Type)
7809 return CompilerType();
7810
7811 return CompilerType(getASTContext(), template_arg.getAsType());
7812}
7813
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00007814Optional<CompilerType::IntegralTemplateArgument>
Pavel Labath769b21e2017-11-13 14:26:21 +00007815ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
7816 size_t idx) {
7817 const clang::ClassTemplateSpecializationDecl *template_decl =
7818 GetAsTemplateSpecialization(type);
7819 if (! template_decl || idx >= template_decl->getTemplateArgs().size())
Pavel Labathf59056f2017-11-30 10:16:54 +00007820 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007821
7822 const clang::TemplateArgument &template_arg =
7823 template_decl->getTemplateArgs()[idx];
7824 if (template_arg.getKind() != clang::TemplateArgument::Integral)
Pavel Labathf59056f2017-11-30 10:16:54 +00007825 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007826
Pavel Labathf59056f2017-11-30 10:16:54 +00007827 return {{template_arg.getAsIntegral(),
7828 CompilerType(getASTContext(), template_arg.getIntegralType())}};
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007829}
7830
Kate Stoneb9c1b512016-09-06 20:57:50 +00007831CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7832 if (type)
7833 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7834 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007835}
7836
Kate Stoneb9c1b512016-09-06 20:57:50 +00007837clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7838 const clang::EnumType *enutype =
7839 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7840 if (enutype)
7841 return enutype->getDecl();
7842 return NULL;
7843}
7844
7845clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7846 const clang::RecordType *record_type =
7847 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7848 if (record_type)
7849 return record_type->getDecl();
7850 return nullptr;
7851}
7852
7853clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
Zachary Turner1639c6b2018-12-17 16:15:13 +00007854 return ClangUtil::GetAsTagDecl(type);
Greg Claytone6b36cd2015-12-08 01:02:08 +00007855}
7856
Aleksandr Urakov709426b2018-09-10 08:08:43 +00007857clang::TypedefNameDecl *
7858ClangASTContext::GetAsTypedefDecl(const CompilerType &type) {
7859 const clang::TypedefType *typedef_type =
7860 llvm::dyn_cast<clang::TypedefType>(ClangUtil::GetQualType(type));
7861 if (typedef_type)
7862 return typedef_type->getDecl();
7863 return nullptr;
7864}
7865
Greg Claytond8d4a572015-08-11 21:38:15 +00007866clang::CXXRecordDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007867ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7868 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
Greg Claytond8d4a572015-08-11 21:38:15 +00007869}
7870
7871clang::ObjCInterfaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007872ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7873 const clang::ObjCObjectType *objc_class_type =
7874 llvm::dyn_cast<clang::ObjCObjectType>(
7875 ClangUtil::GetCanonicalQualType(type));
7876 if (objc_class_type)
7877 return objc_class_type->getInterface();
7878 return nullptr;
7879}
7880
7881clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007882 const CompilerType &type, llvm::StringRef name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00007883 const CompilerType &field_clang_type, AccessType access,
7884 uint32_t bitfield_bit_size) {
7885 if (!type.IsValid() || !field_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00007886 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007887 ClangASTContext *ast =
7888 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7889 if (!ast)
7890 return nullptr;
7891 clang::ASTContext *clang_ast = ast->getASTContext();
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007892 clang::IdentifierInfo *ident = nullptr;
7893 if (!name.empty())
7894 ident = &clang_ast->Idents.get(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00007895
7896 clang::FieldDecl *field = nullptr;
7897
7898 clang::Expr *bit_width = nullptr;
7899 if (bitfield_bit_size != 0) {
7900 llvm::APInt bitfield_bit_size_apint(
7901 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7902 bit_width = new (*clang_ast)
7903 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7904 clang_ast->IntTy, clang::SourceLocation());
7905 }
7906
7907 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7908 if (record_decl) {
7909 field = clang::FieldDecl::Create(
7910 *clang_ast, record_decl, clang::SourceLocation(),
7911 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007912 ident, // Identifier
7913 ClangUtil::GetQualType(field_clang_type), // Field type
7914 nullptr, // TInfo *
7915 bit_width, // BitWidth
7916 false, // Mutable
7917 clang::ICIS_NoInit); // HasInit
Kate Stoneb9c1b512016-09-06 20:57:50 +00007918
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007919 if (name.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +00007920 // Determine whether this field corresponds to an anonymous struct or
7921 // union.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007922 if (const clang::TagType *TagT =
7923 field->getType()->getAs<clang::TagType>()) {
7924 if (clang::RecordDecl *Rec =
7925 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7926 if (!Rec->getDeclName()) {
7927 Rec->setAnonymousStructOrUnion(true);
7928 field->setImplicit();
7929 }
7930 }
7931 }
7932
7933 if (field) {
7934 field->setAccess(
7935 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7936
7937 record_decl->addDecl(field);
7938
7939#ifdef LLDB_CONFIGURATION_DEBUG
7940 VerifyDecl(field);
7941#endif
7942 }
7943 } else {
7944 clang::ObjCInterfaceDecl *class_interface_decl =
7945 ast->GetAsObjCInterfaceDecl(type);
7946
7947 if (class_interface_decl) {
7948 const bool is_synthesized = false;
7949
7950 field_clang_type.GetCompleteType();
7951
7952 field = clang::ObjCIvarDecl::Create(
7953 *clang_ast, class_interface_decl, clang::SourceLocation(),
7954 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007955 ident, // Identifier
7956 ClangUtil::GetQualType(field_clang_type), // Field type
7957 nullptr, // TypeSourceInfo *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007958 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7959 is_synthesized);
7960
7961 if (field) {
7962 class_interface_decl->addDecl(field);
7963
7964#ifdef LLDB_CONFIGURATION_DEBUG
7965 VerifyDecl(field);
7966#endif
7967 }
7968 }
7969 }
7970 return field;
Greg Claytond8d4a572015-08-11 21:38:15 +00007971}
7972
Kate Stoneb9c1b512016-09-06 20:57:50 +00007973void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7974 if (!type)
7975 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007976
Kate Stoneb9c1b512016-09-06 20:57:50 +00007977 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7978 if (!ast)
7979 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007980
Kate Stoneb9c1b512016-09-06 20:57:50 +00007981 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007982
Kate Stoneb9c1b512016-09-06 20:57:50 +00007983 if (!record_decl)
7984 return;
7985
7986 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7987
7988 IndirectFieldVector indirect_fields;
7989 clang::RecordDecl::field_iterator field_pos;
7990 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7991 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7992 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7993 last_field_pos = field_pos++) {
7994 if (field_pos->isAnonymousStructOrUnion()) {
7995 clang::QualType field_qual_type = field_pos->getType();
7996
7997 const clang::RecordType *field_record_type =
7998 field_qual_type->getAs<clang::RecordType>();
7999
8000 if (!field_record_type)
8001 continue;
8002
8003 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
8004
8005 if (!field_record_decl)
8006 continue;
8007
8008 for (clang::RecordDecl::decl_iterator
8009 di = field_record_decl->decls_begin(),
8010 de = field_record_decl->decls_end();
8011 di != de; ++di) {
8012 if (clang::FieldDecl *nested_field_decl =
8013 llvm::dyn_cast<clang::FieldDecl>(*di)) {
8014 clang::NamedDecl **chain =
8015 new (*ast->getASTContext()) clang::NamedDecl *[2];
8016 chain[0] = *field_pos;
8017 chain[1] = nested_field_decl;
8018 clang::IndirectFieldDecl *indirect_field =
8019 clang::IndirectFieldDecl::Create(
8020 *ast->getASTContext(), record_decl, clang::SourceLocation(),
8021 nested_field_decl->getIdentifier(),
8022 nested_field_decl->getType(), {chain, 2});
8023
8024 indirect_field->setImplicit();
8025
8026 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
8027 field_pos->getAccess(), nested_field_decl->getAccess()));
8028
8029 indirect_fields.push_back(indirect_field);
8030 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
8031 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
8032 size_t nested_chain_size =
8033 nested_indirect_field_decl->getChainingSize();
8034 clang::NamedDecl **chain = new (*ast->getASTContext())
8035 clang::NamedDecl *[nested_chain_size + 1];
8036 chain[0] = *field_pos;
8037
8038 int chain_index = 1;
8039 for (clang::IndirectFieldDecl::chain_iterator
8040 nci = nested_indirect_field_decl->chain_begin(),
8041 nce = nested_indirect_field_decl->chain_end();
8042 nci < nce; ++nci) {
8043 chain[chain_index] = *nci;
8044 chain_index++;
8045 }
8046
8047 clang::IndirectFieldDecl *indirect_field =
8048 clang::IndirectFieldDecl::Create(
8049 *ast->getASTContext(), record_decl, clang::SourceLocation(),
8050 nested_indirect_field_decl->getIdentifier(),
8051 nested_indirect_field_decl->getType(),
8052 {chain, nested_chain_size + 1});
8053
8054 indirect_field->setImplicit();
8055
8056 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
8057 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
8058
8059 indirect_fields.push_back(indirect_field);
Greg Claytond8d4a572015-08-11 21:38:15 +00008060 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008061 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008062 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008063 }
8064
Adrian Prantl05097242018-04-30 16:49:04 +00008065 // Check the last field to see if it has an incomplete array type as its last
8066 // member and if it does, the tell the record decl about it
Kate Stoneb9c1b512016-09-06 20:57:50 +00008067 if (last_field_pos != field_end_pos) {
8068 if (last_field_pos->getType()->isIncompleteArrayType())
8069 record_decl->hasFlexibleArrayMember();
8070 }
8071
8072 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
8073 ife = indirect_fields.end();
8074 ifi < ife; ++ifi) {
8075 record_decl->addDecl(*ifi);
8076 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008077}
8078
Kate Stoneb9c1b512016-09-06 20:57:50 +00008079void ClangASTContext::SetIsPacked(const CompilerType &type) {
8080 if (type) {
8081 ClangASTContext *ast =
8082 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8083 if (ast) {
8084 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
8085
8086 if (!record_decl)
Greg Claytonf73034f2015-09-08 18:15:05 +00008087 return;
8088
Kate Stoneb9c1b512016-09-06 20:57:50 +00008089 record_decl->addAttr(
8090 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
Greg Claytond8d4a572015-08-11 21:38:15 +00008091 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008092 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008093}
8094
Kate Stoneb9c1b512016-09-06 20:57:50 +00008095clang::VarDecl *ClangASTContext::AddVariableToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008096 const CompilerType &type, llvm::StringRef name,
8097 const CompilerType &var_type, AccessType access) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00008098 if (!type.IsValid() || !var_type.IsValid())
8099 return nullptr;
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008100
Kate Stoneb9c1b512016-09-06 20:57:50 +00008101 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8102 if (!ast)
8103 return nullptr;
8104
8105 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008106 if (!record_decl)
8107 return nullptr;
8108
8109 clang::VarDecl *var_decl = nullptr;
8110 clang::IdentifierInfo *ident = nullptr;
8111 if (!name.empty())
8112 ident = &ast->getASTContext()->Idents.get(name);
8113
8114 var_decl = clang::VarDecl::Create(
8115 *ast->getASTContext(), // ASTContext &
8116 record_decl, // DeclContext *
8117 clang::SourceLocation(), // clang::SourceLocation StartLoc
8118 clang::SourceLocation(), // clang::SourceLocation IdLoc
8119 ident, // clang::IdentifierInfo *
8120 ClangUtil::GetQualType(var_type), // Variable clang::QualType
8121 nullptr, // TypeSourceInfo *
8122 clang::SC_Static); // StorageClass
8123 if (!var_decl)
8124 return nullptr;
8125
8126 var_decl->setAccess(
8127 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
8128 record_decl->addDecl(var_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008129
Greg Claytond8d4a572015-08-11 21:38:15 +00008130#ifdef LLDB_CONFIGURATION_DEBUG
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008131 VerifyDecl(var_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008132#endif
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008133
Kate Stoneb9c1b512016-09-06 20:57:50 +00008134 return var_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008135}
8136
Kate Stoneb9c1b512016-09-06 20:57:50 +00008137clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
Davide Italiano675767a2018-03-27 19:40:50 +00008138 lldb::opaque_compiler_type_t type, const char *name, const char *mangled_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008139 const CompilerType &method_clang_type, lldb::AccessType access,
8140 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
8141 bool is_attr_used, bool is_artificial) {
8142 if (!type || !method_clang_type.IsValid() || name == nullptr ||
8143 name[0] == '\0')
8144 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008145
Kate Stoneb9c1b512016-09-06 20:57:50 +00008146 clang::QualType record_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008147
Kate Stoneb9c1b512016-09-06 20:57:50 +00008148 clang::CXXRecordDecl *cxx_record_decl =
8149 record_qual_type->getAsCXXRecordDecl();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008150
Kate Stoneb9c1b512016-09-06 20:57:50 +00008151 if (cxx_record_decl == nullptr)
8152 return nullptr;
8153
8154 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8155
8156 clang::CXXMethodDecl *cxx_method_decl = nullptr;
8157
8158 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
8159
8160 const clang::FunctionType *function_type =
8161 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
8162
8163 if (function_type == nullptr)
8164 return nullptr;
8165
8166 const clang::FunctionProtoType *method_function_prototype(
8167 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
8168
8169 if (!method_function_prototype)
8170 return nullptr;
8171
8172 unsigned int num_params = method_function_prototype->getNumParams();
8173
8174 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
8175 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
8176
8177 if (is_artificial)
8178 return nullptr; // skip everything artificial
8179
8180 if (name[0] == '~') {
8181 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
8182 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8183 clang::DeclarationNameInfo(
8184 getASTContext()->DeclarationNames.getCXXDestructorName(
8185 getASTContext()->getCanonicalType(record_qual_type)),
8186 clang::SourceLocation()),
8187 method_qual_type, nullptr, is_inline, is_artificial);
8188 cxx_method_decl = cxx_dtor_decl;
8189 } else if (decl_name == cxx_record_decl->getDeclName()) {
8190 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
8191 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8192 clang::DeclarationNameInfo(
8193 getASTContext()->DeclarationNames.getCXXConstructorName(
8194 getASTContext()->getCanonicalType(record_qual_type)),
8195 clang::SourceLocation()),
8196 method_qual_type,
8197 nullptr, // TypeSourceInfo *
8198 is_explicit, is_inline, is_artificial, false /*is_constexpr*/);
8199 cxx_method_decl = cxx_ctor_decl;
8200 } else {
8201 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
8202 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
8203
8204 if (IsOperator(name, op_kind)) {
8205 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
Adrian Prantl05097242018-04-30 16:49:04 +00008206 // Check the number of operator parameters. Sometimes we have seen bad
8207 // DWARF that doesn't correctly describe operators and if we try to
8208 // create a method and add it to the class, clang will assert and
8209 // crash, so we need to make sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00008210 const bool is_method = true;
8211 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
8212 is_method, op_kind, num_params))
8213 return nullptr;
8214 cxx_method_decl = clang::CXXMethodDecl::Create(
8215 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8216 clang::DeclarationNameInfo(
8217 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
8218 clang::SourceLocation()),
8219 method_qual_type,
8220 nullptr, // TypeSourceInfo *
8221 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
8222 } else if (num_params == 0) {
8223 // Conversion operators don't take params...
8224 cxx_method_decl = clang::CXXConversionDecl::Create(
8225 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8226 clang::DeclarationNameInfo(
8227 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
8228 getASTContext()->getCanonicalType(
8229 function_type->getReturnType())),
8230 clang::SourceLocation()),
8231 method_qual_type,
8232 nullptr, // TypeSourceInfo *
8233 is_inline, is_explicit, false /*is_constexpr*/,
8234 clang::SourceLocation());
8235 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008236 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008237
8238 if (cxx_method_decl == nullptr) {
8239 cxx_method_decl = clang::CXXMethodDecl::Create(
8240 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8241 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8242 method_qual_type,
8243 nullptr, // TypeSourceInfo *
8244 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
Greg Claytond8d4a572015-08-11 21:38:15 +00008245 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008246 }
8247
8248 clang::AccessSpecifier access_specifier =
8249 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8250
8251 cxx_method_decl->setAccess(access_specifier);
8252 cxx_method_decl->setVirtualAsWritten(is_virtual);
8253
8254 if (is_attr_used)
8255 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8256
Davide Italiano675767a2018-03-27 19:40:50 +00008257 if (mangled_name != NULL) {
8258 cxx_method_decl->addAttr(
8259 clang::AsmLabelAttr::CreateImplicit(*getASTContext(), mangled_name));
8260 }
8261
Kate Stoneb9c1b512016-09-06 20:57:50 +00008262 // Populate the method decl with parameter decls
8263
8264 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8265
8266 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8267 params.push_back(clang::ParmVarDecl::Create(
8268 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8269 clang::SourceLocation(),
8270 nullptr, // anonymous
8271 method_function_prototype->getParamType(param_index), nullptr,
8272 clang::SC_None, nullptr));
8273 }
8274
8275 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8276
8277 cxx_record_decl->addDecl(cxx_method_decl);
8278
8279 // Sometimes the debug info will mention a constructor (default/copy/move),
8280 // destructor, or assignment operator (copy/move) but there won't be any
8281 // version of this in the code. So we check if the function was artificially
8282 // generated and if it is trivial and this lets the compiler/backend know
8283 // that it can inline the IR for these when it needs to and we can avoid a
8284 // "missing function" error when running expressions.
8285
8286 if (is_artificial) {
8287 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8288 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8289 (cxx_ctor_decl->isCopyConstructor() &&
8290 cxx_record_decl->hasTrivialCopyConstructor()) ||
8291 (cxx_ctor_decl->isMoveConstructor() &&
8292 cxx_record_decl->hasTrivialMoveConstructor()))) {
8293 cxx_ctor_decl->setDefaulted();
8294 cxx_ctor_decl->setTrivial(true);
8295 } else if (cxx_dtor_decl) {
8296 if (cxx_record_decl->hasTrivialDestructor()) {
8297 cxx_dtor_decl->setDefaulted();
8298 cxx_dtor_decl->setTrivial(true);
8299 }
8300 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8301 cxx_record_decl->hasTrivialCopyAssignment()) ||
8302 (cxx_method_decl->isMoveAssignmentOperator() &&
8303 cxx_record_decl->hasTrivialMoveAssignment())) {
8304 cxx_method_decl->setDefaulted();
8305 cxx_method_decl->setTrivial(true);
Greg Claytond8d4a572015-08-11 21:38:15 +00008306 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008307 }
8308
Greg Claytond8d4a572015-08-11 21:38:15 +00008309#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008310 VerifyDecl(cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008311#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008312
Kate Stoneb9c1b512016-09-06 20:57:50 +00008313 // printf ("decl->isPolymorphic() = %i\n",
8314 // cxx_record_decl->isPolymorphic());
8315 // printf ("decl->isAggregate() = %i\n",
8316 // cxx_record_decl->isAggregate());
8317 // printf ("decl->isPOD() = %i\n",
8318 // cxx_record_decl->isPOD());
8319 // printf ("decl->isEmpty() = %i\n",
8320 // cxx_record_decl->isEmpty());
8321 // printf ("decl->isAbstract() = %i\n",
8322 // cxx_record_decl->isAbstract());
8323 // printf ("decl->hasTrivialConstructor() = %i\n",
8324 // cxx_record_decl->hasTrivialConstructor());
8325 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8326 // cxx_record_decl->hasTrivialCopyConstructor());
8327 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8328 // cxx_record_decl->hasTrivialCopyAssignment());
8329 // printf ("decl->hasTrivialDestructor() = %i\n",
8330 // cxx_record_decl->hasTrivialDestructor());
8331 return cxx_method_decl;
8332}
Greg Claytond8d4a572015-08-11 21:38:15 +00008333
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00008334void ClangASTContext::AddMethodOverridesForCXXRecordType(
8335 lldb::opaque_compiler_type_t type) {
8336 if (auto *record = GetAsCXXRecordDecl(type))
8337 for (auto *method : record->methods())
8338 addOverridesForMethod(method);
8339}
8340
Greg Claytond8d4a572015-08-11 21:38:15 +00008341#pragma mark C++ Base Classes
8342
Zachary Turner970f38e2018-10-25 20:44:56 +00008343std::unique_ptr<clang::CXXBaseSpecifier>
Kate Stoneb9c1b512016-09-06 20:57:50 +00008344ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8345 AccessType access, bool is_virtual,
8346 bool base_of_class) {
Zachary Turner970f38e2018-10-25 20:44:56 +00008347 if (!type)
8348 return nullptr;
8349
8350 return llvm::make_unique<clang::CXXBaseSpecifier>(
8351 clang::SourceRange(), is_virtual, base_of_class,
8352 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8353 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8354 clang::SourceLocation());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008355}
8356
Zachary Turner970f38e2018-10-25 20:44:56 +00008357bool ClangASTContext::TransferBaseClasses(
Kate Stoneb9c1b512016-09-06 20:57:50 +00008358 lldb::opaque_compiler_type_t type,
Zachary Turner970f38e2018-10-25 20:44:56 +00008359 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases) {
8360 if (!type)
8361 return false;
8362 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8363 if (!cxx_record_decl)
8364 return false;
8365 std::vector<clang::CXXBaseSpecifier *> raw_bases;
8366 raw_bases.reserve(bases.size());
8367
8368 // Clang will make a copy of them, so it's ok that we pass pointers that we're
8369 // about to destroy.
8370 for (auto &b : bases)
8371 raw_bases.push_back(b.get());
8372 cxx_record_decl->setBases(raw_bases.data(), raw_bases.size());
8373 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008374}
8375
8376bool ClangASTContext::SetObjCSuperClass(
8377 const CompilerType &type, const CompilerType &superclass_clang_type) {
8378 ClangASTContext *ast =
8379 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8380 if (!ast)
8381 return false;
8382 clang::ASTContext *clang_ast = ast->getASTContext();
8383
8384 if (type && superclass_clang_type.IsValid() &&
8385 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8386 clang::ObjCInterfaceDecl *class_interface_decl =
8387 GetAsObjCInterfaceDecl(type);
8388 clang::ObjCInterfaceDecl *super_interface_decl =
8389 GetAsObjCInterfaceDecl(superclass_clang_type);
8390 if (class_interface_decl && super_interface_decl) {
8391 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8392 clang_ast->getObjCInterfaceType(super_interface_decl)));
8393 return true;
8394 }
8395 }
8396 return false;
8397}
8398
8399bool ClangASTContext::AddObjCClassProperty(
8400 const CompilerType &type, const char *property_name,
8401 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8402 const char *property_setter_name, const char *property_getter_name,
8403 uint32_t property_attributes, ClangASTMetadata *metadata) {
8404 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8405 property_name[0] == '\0')
8406 return false;
8407 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8408 if (!ast)
8409 return false;
8410 clang::ASTContext *clang_ast = ast->getASTContext();
8411
8412 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8413
8414 if (class_interface_decl) {
8415 CompilerType property_clang_type_to_access;
8416
8417 if (property_clang_type.IsValid())
8418 property_clang_type_to_access = property_clang_type;
8419 else if (ivar_decl)
8420 property_clang_type_to_access =
8421 CompilerType(clang_ast, ivar_decl->getType());
8422
8423 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8424 clang::TypeSourceInfo *prop_type_source;
8425 if (ivar_decl)
8426 prop_type_source =
8427 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8428 else
8429 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8430 ClangUtil::GetQualType(property_clang_type));
8431
8432 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8433 *clang_ast, class_interface_decl,
8434 clang::SourceLocation(), // Source Location
8435 &clang_ast->Idents.get(property_name),
8436 clang::SourceLocation(), // Source Location for AT
8437 clang::SourceLocation(), // Source location for (
8438 ivar_decl ? ivar_decl->getType()
8439 : ClangUtil::GetQualType(property_clang_type),
8440 prop_type_source);
8441
8442 if (property_decl) {
8443 if (metadata)
8444 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8445
8446 class_interface_decl->addDecl(property_decl);
8447
8448 clang::Selector setter_sel, getter_sel;
8449
8450 if (property_setter_name != nullptr) {
8451 std::string property_setter_no_colon(
8452 property_setter_name, strlen(property_setter_name) - 1);
8453 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008454 &clang_ast->Idents.get(property_setter_no_colon);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008455 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8456 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8457 std::string setter_sel_string("set");
8458 setter_sel_string.push_back(::toupper(property_name[0]));
8459 setter_sel_string.append(&property_name[1]);
8460 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008461 &clang_ast->Idents.get(setter_sel_string);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008462 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8463 }
8464 property_decl->setSetterName(setter_sel);
8465 property_decl->setPropertyAttributes(
8466 clang::ObjCPropertyDecl::OBJC_PR_setter);
8467
8468 if (property_getter_name != nullptr) {
8469 clang::IdentifierInfo *getter_ident =
8470 &clang_ast->Idents.get(property_getter_name);
8471 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8472 } else {
8473 clang::IdentifierInfo *getter_ident =
8474 &clang_ast->Idents.get(property_name);
8475 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8476 }
8477 property_decl->setGetterName(getter_sel);
8478 property_decl->setPropertyAttributes(
8479 clang::ObjCPropertyDecl::OBJC_PR_getter);
8480
8481 if (ivar_decl)
8482 property_decl->setPropertyIvarDecl(ivar_decl);
8483
8484 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8485 property_decl->setPropertyAttributes(
8486 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8487 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8488 property_decl->setPropertyAttributes(
8489 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8490 if (property_attributes & DW_APPLE_PROPERTY_assign)
8491 property_decl->setPropertyAttributes(
8492 clang::ObjCPropertyDecl::OBJC_PR_assign);
8493 if (property_attributes & DW_APPLE_PROPERTY_retain)
8494 property_decl->setPropertyAttributes(
8495 clang::ObjCPropertyDecl::OBJC_PR_retain);
8496 if (property_attributes & DW_APPLE_PROPERTY_copy)
8497 property_decl->setPropertyAttributes(
8498 clang::ObjCPropertyDecl::OBJC_PR_copy);
8499 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8500 property_decl->setPropertyAttributes(
8501 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8502 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8503 property_decl->setPropertyAttributes(
8504 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8505 if (property_attributes &
8506 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8507 property_decl->setPropertyAttributes(
8508 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8509 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8510 property_decl->setPropertyAttributes(
8511 clang::ObjCPropertyDecl::OBJC_PR_class);
8512
8513 const bool isInstance =
8514 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8515
8516 if (!getter_sel.isNull() &&
8517 !(isInstance
8518 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8519 : class_interface_decl->lookupClassMethod(getter_sel))) {
8520 const bool isVariadic = false;
8521 const bool isSynthesized = false;
8522 const bool isImplicitlyDeclared = true;
8523 const bool isDefined = false;
8524 const clang::ObjCMethodDecl::ImplementationControl impControl =
8525 clang::ObjCMethodDecl::None;
8526 const bool HasRelatedResultType = false;
8527
8528 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8529 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8530 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8531 nullptr, class_interface_decl, isInstance, isVariadic,
8532 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8533 HasRelatedResultType);
8534
8535 if (getter && metadata)
8536 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8537
8538 if (getter) {
8539 getter->setMethodParams(*clang_ast,
8540 llvm::ArrayRef<clang::ParmVarDecl *>(),
8541 llvm::ArrayRef<clang::SourceLocation>());
8542
8543 class_interface_decl->addDecl(getter);
8544 }
8545 }
8546
8547 if (!setter_sel.isNull() &&
8548 !(isInstance
8549 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8550 : class_interface_decl->lookupClassMethod(setter_sel))) {
8551 clang::QualType result_type = clang_ast->VoidTy;
8552 const bool isVariadic = false;
8553 const bool isSynthesized = false;
8554 const bool isImplicitlyDeclared = true;
8555 const bool isDefined = false;
8556 const clang::ObjCMethodDecl::ImplementationControl impControl =
8557 clang::ObjCMethodDecl::None;
8558 const bool HasRelatedResultType = false;
8559
8560 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8561 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8562 setter_sel, result_type, nullptr, class_interface_decl,
8563 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8564 isDefined, impControl, HasRelatedResultType);
8565
8566 if (setter && metadata)
8567 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8568
8569 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8570
8571 params.push_back(clang::ParmVarDecl::Create(
8572 *clang_ast, setter, clang::SourceLocation(),
8573 clang::SourceLocation(),
8574 nullptr, // anonymous
8575 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8576 clang::SC_Auto, nullptr));
8577
8578 if (setter) {
8579 setter->setMethodParams(
8580 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8581 llvm::ArrayRef<clang::SourceLocation>());
8582
8583 class_interface_decl->addDecl(setter);
8584 }
8585 }
8586
8587 return true;
8588 }
8589 }
8590 }
8591 return false;
8592}
8593
8594bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8595 bool check_superclass) {
8596 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8597 if (class_interface_decl)
8598 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8599 return false;
8600}
8601
8602clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8603 const CompilerType &type,
8604 const char *name, // the full symbol name as seen in the symbol table
8605 // (lldb::opaque_compiler_type_t type, "-[NString
8606 // stringWithCString:]")
8607 const CompilerType &method_clang_type, lldb::AccessType access,
8608 bool is_artificial, bool is_variadic) {
8609 if (!type || !method_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008610 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008611
Kate Stoneb9c1b512016-09-06 20:57:50 +00008612 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8613
8614 if (class_interface_decl == nullptr)
8615 return nullptr;
8616 ClangASTContext *lldb_ast =
8617 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8618 if (lldb_ast == nullptr)
8619 return nullptr;
8620 clang::ASTContext *ast = lldb_ast->getASTContext();
8621
8622 const char *selector_start = ::strchr(name, ' ');
8623 if (selector_start == nullptr)
8624 return nullptr;
8625
8626 selector_start++;
8627 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8628
8629 size_t len = 0;
8630 const char *start;
8631 // printf ("name = '%s'\n", name);
8632
8633 unsigned num_selectors_with_args = 0;
8634 for (start = selector_start; start && *start != '\0' && *start != ']';
8635 start += len) {
8636 len = ::strcspn(start, ":]");
8637 bool has_arg = (start[len] == ':');
8638 if (has_arg)
8639 ++num_selectors_with_args;
8640 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8641 if (has_arg)
8642 len += 1;
8643 }
8644
8645 if (selector_idents.size() == 0)
8646 return nullptr;
8647
8648 clang::Selector method_selector = ast->Selectors.getSelector(
8649 num_selectors_with_args ? selector_idents.size() : 0,
8650 selector_idents.data());
8651
8652 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8653
8654 // Populate the method decl with parameter decls
8655 const clang::Type *method_type(method_qual_type.getTypePtr());
8656
8657 if (method_type == nullptr)
8658 return nullptr;
8659
8660 const clang::FunctionProtoType *method_function_prototype(
8661 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8662
8663 if (!method_function_prototype)
8664 return nullptr;
8665
8666 bool is_synthesized = false;
8667 bool is_defined = false;
8668 clang::ObjCMethodDecl::ImplementationControl imp_control =
8669 clang::ObjCMethodDecl::None;
8670
8671 const unsigned num_args = method_function_prototype->getNumParams();
8672
8673 if (num_args != num_selectors_with_args)
8674 return nullptr; // some debug information is corrupt. We are not going to
8675 // deal with it.
8676
8677 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8678 *ast,
8679 clang::SourceLocation(), // beginLoc,
8680 clang::SourceLocation(), // endLoc,
8681 method_selector, method_function_prototype->getReturnType(),
8682 nullptr, // TypeSourceInfo *ResultTInfo,
8683 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8684 ClangUtil::GetQualType(type)),
8685 name[0] == '-', is_variadic, is_synthesized,
8686 true, // is_implicitly_declared; we force this to true because we don't
8687 // have source locations
8688 is_defined, imp_control, false /*has_related_result_type*/);
8689
8690 if (objc_method_decl == nullptr)
8691 return nullptr;
8692
8693 if (num_args > 0) {
8694 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8695
8696 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8697 params.push_back(clang::ParmVarDecl::Create(
8698 *ast, objc_method_decl, clang::SourceLocation(),
8699 clang::SourceLocation(),
8700 nullptr, // anonymous
8701 method_function_prototype->getParamType(param_index), nullptr,
8702 clang::SC_Auto, nullptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00008703 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008704
Kate Stoneb9c1b512016-09-06 20:57:50 +00008705 objc_method_decl->setMethodParams(
8706 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8707 llvm::ArrayRef<clang::SourceLocation>());
8708 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008709
Kate Stoneb9c1b512016-09-06 20:57:50 +00008710 class_interface_decl->addDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008711
Greg Claytond8d4a572015-08-11 21:38:15 +00008712#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008713 VerifyDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008714#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00008715
8716 return objc_method_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008717}
8718
Kate Stoneb9c1b512016-09-06 20:57:50 +00008719bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8720 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00008721 return false;
Greg Claytone6b36cd2015-12-08 01:02:08 +00008722
Kate Stoneb9c1b512016-09-06 20:57:50 +00008723 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008724
Kate Stoneb9c1b512016-09-06 20:57:50 +00008725 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8726 switch (type_class) {
8727 case clang::Type::Record: {
8728 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8729 if (cxx_record_decl)
8730 return cxx_record_decl->hasExternalLexicalStorage() ||
8731 cxx_record_decl->hasExternalVisibleStorage();
8732 } break;
Enrico Granata36f51e42015-12-18 22:41:25 +00008733
Kate Stoneb9c1b512016-09-06 20:57:50 +00008734 case clang::Type::Enum: {
8735 clang::EnumDecl *enum_decl =
8736 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8737 if (enum_decl)
8738 return enum_decl->hasExternalLexicalStorage() ||
8739 enum_decl->hasExternalVisibleStorage();
8740 } break;
8741
8742 case clang::Type::ObjCObject:
8743 case clang::Type::ObjCInterface: {
8744 const clang::ObjCObjectType *objc_class_type =
8745 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8746 assert(objc_class_type);
8747 if (objc_class_type) {
8748 clang::ObjCInterfaceDecl *class_interface_decl =
8749 objc_class_type->getInterface();
8750
8751 if (class_interface_decl)
8752 return class_interface_decl->hasExternalLexicalStorage() ||
8753 class_interface_decl->hasExternalVisibleStorage();
Greg Claytond8d4a572015-08-11 21:38:15 +00008754 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008755 } break;
8756
8757 case clang::Type::Typedef:
8758 return GetHasExternalStorage(CompilerType(
8759 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8760 ->getDecl()
8761 ->getUnderlyingType()
8762 .getAsOpaquePtr()));
8763
8764 case clang::Type::Auto:
8765 return GetHasExternalStorage(CompilerType(
8766 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8767 ->getDeducedType()
8768 .getAsOpaquePtr()));
8769
8770 case clang::Type::Elaborated:
8771 return GetHasExternalStorage(CompilerType(
8772 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8773 ->getNamedType()
8774 .getAsOpaquePtr()));
8775
8776 case clang::Type::Paren:
8777 return GetHasExternalStorage(CompilerType(
8778 type.GetTypeSystem(),
8779 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8780
8781 default:
8782 break;
8783 }
8784 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008785}
8786
Kate Stoneb9c1b512016-09-06 20:57:50 +00008787bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8788 bool has_extern) {
8789 if (!type)
8790 return false;
8791
8792 clang::QualType qual_type(GetCanonicalQualType(type));
8793
8794 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8795 switch (type_class) {
8796 case clang::Type::Record: {
8797 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8798 if (cxx_record_decl) {
8799 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8800 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8801 return true;
8802 }
8803 } break;
8804
8805 case clang::Type::Enum: {
8806 clang::EnumDecl *enum_decl =
8807 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8808 if (enum_decl) {
8809 enum_decl->setHasExternalLexicalStorage(has_extern);
8810 enum_decl->setHasExternalVisibleStorage(has_extern);
8811 return true;
8812 }
8813 } break;
8814
8815 case clang::Type::ObjCObject:
8816 case clang::Type::ObjCInterface: {
8817 const clang::ObjCObjectType *objc_class_type =
8818 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8819 assert(objc_class_type);
8820 if (objc_class_type) {
8821 clang::ObjCInterfaceDecl *class_interface_decl =
8822 objc_class_type->getInterface();
8823
8824 if (class_interface_decl) {
8825 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8826 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8827 return true;
8828 }
8829 }
8830 } break;
8831
8832 case clang::Type::Typedef:
8833 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8834 ->getDecl()
8835 ->getUnderlyingType()
8836 .getAsOpaquePtr(),
8837 has_extern);
8838
8839 case clang::Type::Auto:
8840 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8841 ->getDeducedType()
8842 .getAsOpaquePtr(),
8843 has_extern);
8844
8845 case clang::Type::Elaborated:
8846 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8847 ->getNamedType()
8848 .getAsOpaquePtr(),
8849 has_extern);
8850
8851 case clang::Type::Paren:
8852 return SetHasExternalStorage(
8853 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8854 has_extern);
8855
8856 default:
8857 break;
8858 }
8859 return false;
8860}
Greg Claytond8d4a572015-08-11 21:38:15 +00008861
8862#pragma mark TagDecl
8863
Kate Stoneb9c1b512016-09-06 20:57:50 +00008864bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8865 clang::QualType qual_type(ClangUtil::GetQualType(type));
8866 if (!qual_type.isNull()) {
8867 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8868 if (tag_type) {
8869 clang::TagDecl *tag_decl = tag_type->getDecl();
8870 if (tag_decl) {
8871 tag_decl->startDefinition();
8872 return true;
8873 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008874 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008875
8876 const clang::ObjCObjectType *object_type =
8877 qual_type->getAs<clang::ObjCObjectType>();
8878 if (object_type) {
8879 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8880 if (interface_decl) {
8881 interface_decl->startDefinition();
8882 return true;
8883 }
8884 }
8885 }
8886 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008887}
8888
Kate Stoneb9c1b512016-09-06 20:57:50 +00008889bool ClangASTContext::CompleteTagDeclarationDefinition(
8890 const CompilerType &type) {
8891 clang::QualType qual_type(ClangUtil::GetQualType(type));
8892 if (!qual_type.isNull()) {
8893 // Make sure we use the same methodology as
Adrian Prantl05097242018-04-30 16:49:04 +00008894 // ClangASTContext::StartTagDeclarationDefinition() as to how we start/end
8895 // the definition. Previously we were calling
Kate Stoneb9c1b512016-09-06 20:57:50 +00008896 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8897 if (tag_type) {
8898 clang::TagDecl *tag_decl = tag_type->getDecl();
8899 if (tag_decl) {
8900 clang::CXXRecordDecl *cxx_record_decl =
8901 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8902
8903 if (cxx_record_decl) {
8904 if (!cxx_record_decl->isCompleteDefinition())
8905 cxx_record_decl->completeDefinition();
8906 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8907 cxx_record_decl->setHasExternalLexicalStorage(false);
8908 cxx_record_decl->setHasExternalVisibleStorage(false);
8909 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008910 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008911 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008912 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008913
8914 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8915
8916 if (enutype) {
8917 clang::EnumDecl *enum_decl = enutype->getDecl();
8918
8919 if (enum_decl) {
8920 if (!enum_decl->isCompleteDefinition()) {
8921 ClangASTContext *lldb_ast =
8922 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8923 if (lldb_ast == nullptr)
8924 return false;
8925 clang::ASTContext *ast = lldb_ast->getASTContext();
8926
8927 /// TODO This really needs to be fixed.
8928
8929 QualType integer_type(enum_decl->getIntegerType());
8930 if (!integer_type.isNull()) {
8931 unsigned NumPositiveBits = 1;
8932 unsigned NumNegativeBits = 0;
8933
8934 clang::QualType promotion_qual_type;
8935 // If the enum integer type is less than an integer in bit width,
8936 // then we must promote it to an integer size.
8937 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8938 ast->getTypeSize(ast->IntTy)) {
8939 if (enum_decl->getIntegerType()->isSignedIntegerType())
8940 promotion_qual_type = ast->IntTy;
8941 else
8942 promotion_qual_type = ast->UnsignedIntTy;
8943 } else
8944 promotion_qual_type = enum_decl->getIntegerType();
8945
8946 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8947 promotion_qual_type, NumPositiveBits,
8948 NumNegativeBits);
8949 }
8950 }
8951 return true;
8952 }
8953 }
8954 }
8955 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008956}
8957
Aleksandr Urakov709426b2018-09-10 08:08:43 +00008958clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008959 const CompilerType &enum_type, const Declaration &decl, const char *name,
Zachary Turner1639c6b2018-12-17 16:15:13 +00008960 const llvm::APSInt &value) {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008961
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008962 if (!enum_type || ConstString(name).IsEmpty())
8963 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008964
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008965 lldbassert(enum_type.GetTypeSystem() == static_cast<TypeSystem *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +00008966
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008967 lldb::opaque_compiler_type_t enum_opaque_compiler_type =
8968 enum_type.GetOpaqueQualType();
8969
8970 if (!enum_opaque_compiler_type)
8971 return nullptr;
8972
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008973 clang::QualType enum_qual_type(
8974 GetCanonicalQualType(enum_opaque_compiler_type));
8975
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008976 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8977
8978 if (!clang_type)
8979 return nullptr;
8980
8981 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8982
8983 if (!enutype)
8984 return nullptr;
8985
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008986 clang::EnumConstantDecl *enumerator_decl = clang::EnumConstantDecl::Create(
8987 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8988 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier
Zachary Turner1639c6b2018-12-17 16:15:13 +00008989 clang::QualType(enutype, 0), nullptr, value);
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008990
8991 if (!enumerator_decl)
8992 return nullptr;
8993
8994 enutype->getDecl()->addDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008995
8996#ifdef LLDB_CONFIGURATION_DEBUG
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008997 VerifyDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008998#endif
8999
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00009000 return enumerator_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00009001}
9002
Zachary Turner1639c6b2018-12-17 16:15:13 +00009003clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
9004 const CompilerType &enum_type, const Declaration &decl, const char *name,
9005 int64_t enum_value, uint32_t enum_value_bit_size) {
9006 CompilerType underlying_type =
9007 GetEnumerationIntegerType(enum_type.GetOpaqueQualType());
9008 bool is_signed = false;
9009 underlying_type.IsIntegerType(is_signed);
9010
9011 llvm::APSInt value(enum_value_bit_size, is_signed);
9012 value = enum_value;
9013
9014 return AddEnumerationValueToEnumerationType(enum_type, decl, name, value);
9015}
9016
Greg Claytona1e5dc82015-08-11 22:53:00 +00009017CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00009018ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
9019 clang::QualType enum_qual_type(GetCanonicalQualType(type));
9020 const clang::Type *clang_type = enum_qual_type.getTypePtr();
9021 if (clang_type) {
9022 const clang::EnumType *enutype =
9023 llvm::dyn_cast<clang::EnumType>(clang_type);
9024 if (enutype) {
9025 clang::EnumDecl *enum_decl = enutype->getDecl();
9026 if (enum_decl)
9027 return CompilerType(getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00009028 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009029 }
9030 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00009031}
9032
Kate Stoneb9c1b512016-09-06 20:57:50 +00009033CompilerType
9034ClangASTContext::CreateMemberPointerType(const CompilerType &type,
9035 const CompilerType &pointee_type) {
9036 if (type && pointee_type.IsValid() &&
9037 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
9038 ClangASTContext *ast =
9039 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
9040 if (!ast)
9041 return CompilerType();
9042 return CompilerType(ast->getASTContext(),
9043 ast->getASTContext()->getMemberPointerType(
9044 ClangUtil::GetQualType(pointee_type),
9045 ClangUtil::GetQualType(type).getTypePtr()));
9046 }
9047 return CompilerType();
9048}
Greg Claytond8d4a572015-08-11 21:38:15 +00009049
9050size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00009051ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
9052 const char *s, uint8_t *dst,
9053 size_t dst_size) {
9054 if (type) {
9055 clang::QualType qual_type(GetCanonicalQualType(type));
9056 uint32_t count = 0;
9057 bool is_complex = false;
9058 if (IsFloatingPointType(type, count, is_complex)) {
9059 // TODO: handle complex and vector types
9060 if (count != 1)
9061 return false;
9062
9063 llvm::StringRef s_sref(s);
9064 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type),
9065 s_sref);
9066
9067 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type);
9068 const uint64_t byte_size = bit_size / 8;
9069 if (dst_size >= byte_size) {
9070 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(
9071 llvm::NextPowerOf2(byte_size) * 8);
Zachary Turner97206d52017-05-12 04:51:55 +00009072 lldb_private::Status get_data_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009073 if (scalar.GetAsMemoryData(dst, byte_size,
9074 lldb_private::endian::InlHostByteOrder(),
9075 get_data_error))
9076 return byte_size;
9077 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009078 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009079 }
9080 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00009081}
9082
Greg Claytond8d4a572015-08-11 21:38:15 +00009083//----------------------------------------------------------------------
9084// Dumping types
9085//----------------------------------------------------------------------
9086#define DEPTH_INCREMENT 2
9087
Zachary Turner49110232018-11-05 17:40:28 +00009088void ClangASTContext::Dump(Stream &s) {
Zachary Turner115209e2018-11-05 19:25:39 +00009089 Decl *tu = Decl::castFromDeclContext(GetTranslationUnitDecl());
Zachary Turner49110232018-11-05 17:40:28 +00009090 tu->dump(s.AsRawOstream());
9091}
9092
Kate Stoneb9c1b512016-09-06 20:57:50 +00009093void ClangASTContext::DumpValue(
9094 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
Zachary Turner29cb8682017-03-03 20:57:05 +00009095 lldb::Format format, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009096 lldb::offset_t data_byte_offset, size_t data_byte_size,
9097 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
9098 bool show_summary, bool verbose, uint32_t depth) {
9099 if (!type)
9100 return;
9101
9102 clang::QualType qual_type(GetQualType(type));
9103 switch (qual_type->getTypeClass()) {
9104 case clang::Type::Record:
9105 if (GetCompleteType(type)) {
9106 const clang::RecordType *record_type =
9107 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9108 const clang::RecordDecl *record_decl = record_type->getDecl();
9109 assert(record_decl);
9110 uint32_t field_bit_offset = 0;
9111 uint32_t field_byte_offset = 0;
9112 const clang::ASTRecordLayout &record_layout =
9113 getASTContext()->getASTRecordLayout(record_decl);
9114 uint32_t child_idx = 0;
9115
9116 const clang::CXXRecordDecl *cxx_record_decl =
9117 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9118 if (cxx_record_decl) {
9119 // We might have base classes to print out first
9120 clang::CXXRecordDecl::base_class_const_iterator base_class,
9121 base_class_end;
9122 for (base_class = cxx_record_decl->bases_begin(),
9123 base_class_end = cxx_record_decl->bases_end();
9124 base_class != base_class_end; ++base_class) {
9125 const clang::CXXRecordDecl *base_class_decl =
9126 llvm::cast<clang::CXXRecordDecl>(
9127 base_class->getType()->getAs<clang::RecordType>()->getDecl());
9128
9129 // Skip empty base classes
Jonas Devliegherea6682a42018-12-15 00:15:33 +00009130 if (!verbose && !ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00009131 continue;
9132
9133 if (base_class->isVirtual())
9134 field_bit_offset =
9135 record_layout.getVBaseClassOffset(base_class_decl)
9136 .getQuantity() *
9137 8;
9138 else
9139 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
9140 .getQuantity() *
9141 8;
9142 field_byte_offset = field_bit_offset / 8;
9143 assert(field_bit_offset % 8 == 0);
9144 if (child_idx == 0)
9145 s->PutChar('{');
9146 else
9147 s->PutChar(',');
9148
9149 clang::QualType base_class_qual_type = base_class->getType();
9150 std::string base_class_type_name(base_class_qual_type.getAsString());
9151
9152 // Indent and print the base class type name
Zachary Turner827d5d72016-12-16 04:27:00 +00009153 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
9154 base_class_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009155
9156 clang::TypeInfo base_class_type_info =
9157 getASTContext()->getTypeInfo(base_class_qual_type);
9158
9159 // Dump the value of the member
9160 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
9161 base_clang_type.DumpValue(
9162 exe_ctx,
9163 s, // Stream to dump to
9164 base_clang_type
9165 .GetFormat(), // The format with which to display the member
9166 data, // Data buffer containing all bytes for this type
9167 data_byte_offset + field_byte_offset, // Offset into "data" where
9168 // to grab value from
9169 base_class_type_info.Width / 8, // Size of this type in bytes
9170 0, // Bitfield bit size
9171 0, // Bitfield bit offset
9172 show_types, // Boolean indicating if we should show the variable
9173 // types
9174 show_summary, // Boolean indicating if we should show a summary
9175 // for the current type
9176 verbose, // Verbose output?
9177 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9178 // children
9179
9180 ++child_idx;
9181 }
9182 }
9183 uint32_t field_idx = 0;
9184 clang::RecordDecl::field_iterator field, field_end;
9185 for (field = record_decl->field_begin(),
9186 field_end = record_decl->field_end();
9187 field != field_end; ++field, ++field_idx, ++child_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009188 // Print the starting squiggly bracket (if this is the first member) or
9189 // comma (for member 2 and beyond) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009190 if (child_idx == 0)
9191 s->PutChar('{');
9192 else
9193 s->PutChar(',');
9194
9195 // Indent
9196 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
9197
9198 clang::QualType field_type = field->getType();
9199 // Print the member type if requested
Adrian Prantl05097242018-04-30 16:49:04 +00009200 // Figure out the type byte size (field_type_info.first) and alignment
9201 // (field_type_info.second) from the AST context.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009202 clang::TypeInfo field_type_info =
9203 getASTContext()->getTypeInfo(field_type);
9204 assert(field_idx < record_layout.getFieldCount());
9205 // Figure out the field offset within the current struct/union/class
9206 // type
9207 field_bit_offset = record_layout.getFieldOffset(field_idx);
9208 field_byte_offset = field_bit_offset / 8;
9209 uint32_t field_bitfield_bit_size = 0;
9210 uint32_t field_bitfield_bit_offset = 0;
9211 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
9212 field_bitfield_bit_size))
9213 field_bitfield_bit_offset = field_bit_offset % 8;
9214
9215 if (show_types) {
9216 std::string field_type_name(field_type.getAsString());
9217 if (field_bitfield_bit_size > 0)
9218 s->Printf("(%s:%u) ", field_type_name.c_str(),
9219 field_bitfield_bit_size);
9220 else
9221 s->Printf("(%s) ", field_type_name.c_str());
9222 }
9223 // Print the member name and equal sign
9224 s->Printf("%s = ", field->getNameAsString().c_str());
9225
9226 // Dump the value of the member
9227 CompilerType field_clang_type(getASTContext(), field_type);
9228 field_clang_type.DumpValue(
9229 exe_ctx,
9230 s, // Stream to dump to
9231 field_clang_type
9232 .GetFormat(), // The format with which to display the member
9233 data, // Data buffer containing all bytes for this type
9234 data_byte_offset + field_byte_offset, // Offset into "data" where to
9235 // grab value from
9236 field_type_info.Width / 8, // Size of this type in bytes
9237 field_bitfield_bit_size, // Bitfield bit size
9238 field_bitfield_bit_offset, // Bitfield bit offset
9239 show_types, // Boolean indicating if we should show the variable
9240 // types
9241 show_summary, // Boolean indicating if we should show a summary for
9242 // the current type
9243 verbose, // Verbose output?
9244 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9245 // children
9246 }
9247
9248 // Indent the trailing squiggly bracket
9249 if (child_idx > 0)
9250 s->Printf("\n%*s}", depth, "");
9251 }
9252 return;
9253
9254 case clang::Type::Enum:
9255 if (GetCompleteType(type)) {
9256 const clang::EnumType *enutype =
9257 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9258 const clang::EnumDecl *enum_decl = enutype->getDecl();
9259 assert(enum_decl);
9260 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9261 lldb::offset_t offset = data_byte_offset;
9262 const int64_t enum_value = data.GetMaxU64Bitfield(
9263 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
9264 for (enum_pos = enum_decl->enumerator_begin(),
9265 enum_end_pos = enum_decl->enumerator_end();
9266 enum_pos != enum_end_pos; ++enum_pos) {
9267 if (enum_pos->getInitVal() == enum_value) {
9268 s->Printf("%s", enum_pos->getNameAsString().c_str());
9269 return;
9270 }
9271 }
Adrian Prantl05097242018-04-30 16:49:04 +00009272 // If we have gotten here we didn't get find the enumerator in the enum
9273 // decl, so just print the integer.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009274 s->Printf("%" PRIi64, enum_value);
9275 }
9276 return;
9277
9278 case clang::Type::ConstantArray: {
9279 const clang::ConstantArrayType *array =
9280 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9281 bool is_array_of_characters = false;
9282 clang::QualType element_qual_type = array->getElementType();
9283
9284 const clang::Type *canonical_type =
9285 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9286 if (canonical_type)
9287 is_array_of_characters = canonical_type->isCharType();
9288
9289 const uint64_t element_count = array->getSize().getLimitedValue();
9290
9291 clang::TypeInfo field_type_info =
9292 getASTContext()->getTypeInfo(element_qual_type);
9293
9294 uint32_t element_idx = 0;
9295 uint32_t element_offset = 0;
9296 uint64_t element_byte_size = field_type_info.Width / 8;
9297 uint32_t element_stride = element_byte_size;
9298
9299 if (is_array_of_characters) {
9300 s->PutChar('"');
Zachary Turner29cb8682017-03-03 20:57:05 +00009301 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9302 element_byte_size, element_count, UINT32_MAX,
9303 LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009304 s->PutChar('"');
9305 return;
9306 } else {
9307 CompilerType element_clang_type(getASTContext(), element_qual_type);
9308 lldb::Format element_format = element_clang_type.GetFormat();
9309
9310 for (element_idx = 0; element_idx < element_count; ++element_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009311 // Print the starting squiggly bracket (if this is the first member) or
9312 // comman (for member 2 and beyong) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009313 if (element_idx == 0)
9314 s->PutChar('{');
9315 else
9316 s->PutChar(',');
9317
9318 // Indent and print the index
9319 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9320
9321 // Figure out the field offset within the current struct/union/class
9322 // type
9323 element_offset = element_idx * element_stride;
9324
9325 // Dump the value of the member
9326 element_clang_type.DumpValue(
9327 exe_ctx,
9328 s, // Stream to dump to
9329 element_format, // The format with which to display the element
9330 data, // Data buffer containing all bytes for this type
9331 data_byte_offset +
9332 element_offset, // Offset into "data" where to grab value from
9333 element_byte_size, // Size of this type in bytes
9334 0, // Bitfield bit size
9335 0, // Bitfield bit offset
9336 show_types, // Boolean indicating if we should show the variable
9337 // types
9338 show_summary, // Boolean indicating if we should show a summary for
9339 // the current type
9340 verbose, // Verbose output?
9341 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9342 // children
9343 }
9344
9345 // Indent the trailing squiggly bracket
9346 if (element_idx > 0)
9347 s->Printf("\n%*s}", depth, "");
9348 }
9349 }
9350 return;
9351
9352 case clang::Type::Typedef: {
9353 clang::QualType typedef_qual_type =
9354 llvm::cast<clang::TypedefType>(qual_type)
9355 ->getDecl()
9356 ->getUnderlyingType();
9357
9358 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9359 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9360 clang::TypeInfo typedef_type_info =
9361 getASTContext()->getTypeInfo(typedef_qual_type);
9362 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9363
9364 return typedef_clang_type.DumpValue(
9365 exe_ctx,
9366 s, // Stream to dump to
9367 typedef_format, // The format with which to display the element
9368 data, // Data buffer containing all bytes for this type
9369 data_byte_offset, // Offset into "data" where to grab value from
9370 typedef_byte_size, // Size of this type in bytes
9371 bitfield_bit_size, // Bitfield bit size
9372 bitfield_bit_offset, // Bitfield bit offset
9373 show_types, // Boolean indicating if we should show the variable types
9374 show_summary, // Boolean indicating if we should show a summary for the
9375 // current type
9376 verbose, // Verbose output?
9377 depth); // Scope depth for any types that have children
9378 } break;
9379
9380 case clang::Type::Auto: {
9381 clang::QualType elaborated_qual_type =
9382 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9383 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9384 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9385 clang::TypeInfo elaborated_type_info =
9386 getASTContext()->getTypeInfo(elaborated_qual_type);
9387 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9388
9389 return elaborated_clang_type.DumpValue(
9390 exe_ctx,
9391 s, // Stream to dump to
9392 elaborated_format, // The format with which to display the element
9393 data, // Data buffer containing all bytes for this type
9394 data_byte_offset, // Offset into "data" where to grab value from
9395 elaborated_byte_size, // Size of this type in bytes
9396 bitfield_bit_size, // Bitfield bit size
9397 bitfield_bit_offset, // Bitfield bit offset
9398 show_types, // Boolean indicating if we should show the variable types
9399 show_summary, // Boolean indicating if we should show a summary for the
9400 // current type
9401 verbose, // Verbose output?
9402 depth); // Scope depth for any types that have children
9403 } break;
9404
9405 case clang::Type::Elaborated: {
9406 clang::QualType elaborated_qual_type =
9407 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9408 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9409 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9410 clang::TypeInfo elaborated_type_info =
9411 getASTContext()->getTypeInfo(elaborated_qual_type);
9412 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9413
9414 return elaborated_clang_type.DumpValue(
9415 exe_ctx,
9416 s, // Stream to dump to
9417 elaborated_format, // The format with which to display the element
9418 data, // Data buffer containing all bytes for this type
9419 data_byte_offset, // Offset into "data" where to grab value from
9420 elaborated_byte_size, // Size of this type in bytes
9421 bitfield_bit_size, // Bitfield bit size
9422 bitfield_bit_offset, // Bitfield bit offset
9423 show_types, // Boolean indicating if we should show the variable types
9424 show_summary, // Boolean indicating if we should show a summary for the
9425 // current type
9426 verbose, // Verbose output?
9427 depth); // Scope depth for any types that have children
9428 } break;
9429
9430 case clang::Type::Paren: {
9431 clang::QualType desugar_qual_type =
9432 llvm::cast<clang::ParenType>(qual_type)->desugar();
9433 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type);
9434
9435 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9436 clang::TypeInfo desugar_type_info =
9437 getASTContext()->getTypeInfo(desugar_qual_type);
9438 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9439
9440 return desugar_clang_type.DumpValue(
9441 exe_ctx,
9442 s, // Stream to dump to
9443 desugar_format, // The format with which to display the element
9444 data, // Data buffer containing all bytes for this type
9445 data_byte_offset, // Offset into "data" where to grab value from
9446 desugar_byte_size, // Size of this type in bytes
9447 bitfield_bit_size, // Bitfield bit size
9448 bitfield_bit_offset, // Bitfield bit offset
9449 show_types, // Boolean indicating if we should show the variable types
9450 show_summary, // Boolean indicating if we should show a summary for the
9451 // current type
9452 verbose, // Verbose output?
9453 depth); // Scope depth for any types that have children
9454 } break;
9455
9456 default:
9457 // We are down to a scalar type that we just need to display.
Zachary Turner29cb8682017-03-03 20:57:05 +00009458 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9459 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9460 bitfield_bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009461
9462 if (show_summary)
9463 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9464 break;
9465 }
9466}
9467
9468bool ClangASTContext::DumpTypeValue(
9469 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
Zachary Turner29cb8682017-03-03 20:57:05 +00009470 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9471 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009472 ExecutionContextScope *exe_scope) {
9473 if (!type)
9474 return false;
9475 if (IsAggregateType(type)) {
9476 return false;
9477 } else {
Greg Claytond8d4a572015-08-11 21:38:15 +00009478 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009479
9480 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9481 switch (type_class) {
9482 case clang::Type::Typedef: {
9483 clang::QualType typedef_qual_type =
9484 llvm::cast<clang::TypedefType>(qual_type)
9485 ->getDecl()
9486 ->getUnderlyingType();
9487 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9488 if (format == eFormatDefault)
9489 format = typedef_clang_type.GetFormat();
9490 clang::TypeInfo typedef_type_info =
9491 getASTContext()->getTypeInfo(typedef_qual_type);
9492 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9493
9494 return typedef_clang_type.DumpTypeValue(
9495 s,
9496 format, // The format with which to display the element
9497 data, // Data buffer containing all bytes for this type
9498 byte_offset, // Offset into "data" where to grab value from
9499 typedef_byte_size, // Size of this type in bytes
9500 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9501 // treat as a bitfield
9502 bitfield_bit_offset, // Offset in bits of a bitfield value if
9503 // bitfield_bit_size != 0
9504 exe_scope);
9505 } break;
9506
9507 case clang::Type::Enum:
Adrian Prantl05097242018-04-30 16:49:04 +00009508 // If our format is enum or default, show the enumeration value as its
9509 // enumeration string value, else just display it as requested.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009510 if ((format == eFormatEnum || format == eFormatDefault) &&
9511 GetCompleteType(type)) {
9512 const clang::EnumType *enutype =
9513 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9514 const clang::EnumDecl *enum_decl = enutype->getDecl();
9515 assert(enum_decl);
9516 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9517 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9518 lldb::offset_t offset = byte_offset;
9519 if (is_signed) {
9520 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9521 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9522 for (enum_pos = enum_decl->enumerator_begin(),
9523 enum_end_pos = enum_decl->enumerator_end();
9524 enum_pos != enum_end_pos; ++enum_pos) {
9525 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009526 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009527 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009528 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009529 }
9530 // If we have gotten here we didn't get find the enumerator in the
9531 // enum decl, so just print the integer.
9532 s->Printf("%" PRIi64, enum_svalue);
9533 } else {
9534 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9535 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9536 for (enum_pos = enum_decl->enumerator_begin(),
9537 enum_end_pos = enum_decl->enumerator_end();
9538 enum_pos != enum_end_pos; ++enum_pos) {
9539 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009540 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009541 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009542 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009543 }
9544 // If we have gotten here we didn't get find the enumerator in the
9545 // enum decl, so just print the integer.
9546 s->Printf("%" PRIu64, enum_uvalue);
Greg Claytond8d4a572015-08-11 21:38:15 +00009547 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009548 return true;
9549 }
9550 // format was not enum, just fall through and dump the value as
9551 // requested....
9552 LLVM_FALLTHROUGH;
9553
9554 default:
9555 // We are down to a scalar type that we just need to display.
9556 {
9557 uint32_t item_count = 1;
9558 // A few formats, we might need to modify our size and count for
9559 // depending
9560 // on how we are trying to display the value...
9561 switch (format) {
Greg Claytond8d4a572015-08-11 21:38:15 +00009562 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00009563 case eFormatBoolean:
9564 case eFormatBinary:
9565 case eFormatComplex:
9566 case eFormatCString: // NULL terminated C strings
9567 case eFormatDecimal:
9568 case eFormatEnum:
9569 case eFormatHex:
9570 case eFormatHexUppercase:
9571 case eFormatFloat:
9572 case eFormatOctal:
9573 case eFormatOSType:
9574 case eFormatUnsigned:
9575 case eFormatPointer:
9576 case eFormatVectorOfChar:
9577 case eFormatVectorOfSInt8:
9578 case eFormatVectorOfUInt8:
9579 case eFormatVectorOfSInt16:
9580 case eFormatVectorOfUInt16:
9581 case eFormatVectorOfSInt32:
9582 case eFormatVectorOfUInt32:
9583 case eFormatVectorOfSInt64:
9584 case eFormatVectorOfUInt64:
9585 case eFormatVectorOfFloat32:
9586 case eFormatVectorOfFloat64:
9587 case eFormatVectorOfUInt128:
9588 break;
9589
9590 case eFormatChar:
9591 case eFormatCharPrintable:
9592 case eFormatCharArray:
9593 case eFormatBytes:
9594 case eFormatBytesWithASCII:
9595 item_count = byte_size;
9596 byte_size = 1;
9597 break;
9598
9599 case eFormatUnicode16:
9600 item_count = byte_size / 2;
9601 byte_size = 2;
9602 break;
9603
9604 case eFormatUnicode32:
9605 item_count = byte_size / 4;
9606 byte_size = 4;
9607 break;
9608 }
Zachary Turner29cb8682017-03-03 20:57:05 +00009609 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9610 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
9611 bitfield_bit_size, bitfield_bit_offset,
9612 exe_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009613 }
9614 break;
9615 }
9616 }
9617 return 0;
9618}
9619
9620void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9621 ExecutionContext *exe_ctx, Stream *s,
9622 const lldb_private::DataExtractor &data,
9623 lldb::offset_t data_byte_offset,
9624 size_t data_byte_size) {
9625 uint32_t length = 0;
9626 if (IsCStringType(type, length)) {
9627 if (exe_ctx) {
9628 Process *process = exe_ctx->GetProcessPtr();
9629 if (process) {
9630 lldb::offset_t offset = data_byte_offset;
9631 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9632 std::vector<uint8_t> buf;
9633 if (length > 0)
9634 buf.resize(length);
9635 else
9636 buf.resize(256);
9637
Zachary Turner29cb8682017-03-03 20:57:05 +00009638 DataExtractor cstr_data(&buf.front(), buf.size(),
9639 process->GetByteOrder(), 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009640 buf.back() = '\0';
9641 size_t bytes_read;
9642 size_t total_cstr_len = 0;
Zachary Turner97206d52017-05-12 04:51:55 +00009643 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009644 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9645 buf.size(), error)) > 0) {
9646 const size_t len = strlen((const char *)&buf.front());
9647 if (len == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00009648 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009649 if (total_cstr_len == 0)
9650 s->PutCString(" \"");
Zachary Turner29cb8682017-03-03 20:57:05 +00009651 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9652 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009653 total_cstr_len += len;
9654 if (len < buf.size())
9655 break;
9656 pointer_address += total_cstr_len;
Greg Claytond8d4a572015-08-11 21:38:15 +00009657 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009658 if (total_cstr_len > 0)
9659 s->PutChar('"');
9660 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009661 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009662 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009663}
9664
Kate Stoneb9c1b512016-09-06 20:57:50 +00009665void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9666 StreamFile s(stdout, false);
9667 DumpTypeDescription(type, &s);
9668 ClangASTMetadata *metadata =
9669 ClangASTContext::GetMetadata(getASTContext(), type);
9670 if (metadata) {
9671 metadata->Dump(&s);
9672 }
9673}
Greg Claytond8d4a572015-08-11 21:38:15 +00009674
Kate Stoneb9c1b512016-09-06 20:57:50 +00009675void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9676 Stream *s) {
9677 if (type) {
9678 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00009679
Kate Stoneb9c1b512016-09-06 20:57:50 +00009680 llvm::SmallVector<char, 1024> buf;
9681 llvm::raw_svector_ostream llvm_ostrm(buf);
9682
9683 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9684 switch (type_class) {
9685 case clang::Type::ObjCObject:
9686 case clang::Type::ObjCInterface: {
9687 GetCompleteType(type);
9688
9689 const clang::ObjCObjectType *objc_class_type =
9690 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9691 assert(objc_class_type);
9692 if (objc_class_type) {
9693 clang::ObjCInterfaceDecl *class_interface_decl =
9694 objc_class_type->getInterface();
9695 if (class_interface_decl) {
9696 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9697 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
Greg Claytond8d4a572015-08-11 21:38:15 +00009698 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009699 }
9700 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00009701
Kate Stoneb9c1b512016-09-06 20:57:50 +00009702 case clang::Type::Typedef: {
9703 const clang::TypedefType *typedef_type =
9704 qual_type->getAs<clang::TypedefType>();
9705 if (typedef_type) {
9706 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9707 std::string clang_typedef_name(
9708 typedef_decl->getQualifiedNameAsString());
9709 if (!clang_typedef_name.empty()) {
9710 s->PutCString("typedef ");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009711 s->PutCString(clang_typedef_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00009712 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009713 }
9714 } break;
9715
9716 case clang::Type::Auto:
9717 CompilerType(getASTContext(),
9718 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
9719 .DumpTypeDescription(s);
9720 return;
9721
9722 case clang::Type::Elaborated:
9723 CompilerType(getASTContext(),
9724 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
9725 .DumpTypeDescription(s);
9726 return;
9727
9728 case clang::Type::Paren:
9729 CompilerType(getASTContext(),
9730 llvm::cast<clang::ParenType>(qual_type)->desugar())
9731 .DumpTypeDescription(s);
9732 return;
9733
9734 case clang::Type::Record: {
9735 GetCompleteType(type);
9736
9737 const clang::RecordType *record_type =
9738 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9739 const clang::RecordDecl *record_decl = record_type->getDecl();
9740 const clang::CXXRecordDecl *cxx_record_decl =
9741 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9742
9743 if (cxx_record_decl)
9744 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9745 s->GetIndentLevel());
9746 else
9747 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9748 s->GetIndentLevel());
9749 } break;
9750
9751 default: {
9752 const clang::TagType *tag_type =
9753 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9754 if (tag_type) {
9755 clang::TagDecl *tag_decl = tag_type->getDecl();
9756 if (tag_decl)
9757 tag_decl->print(llvm_ostrm, 0);
9758 } else {
9759 std::string clang_type_name(qual_type.getAsString());
9760 if (!clang_type_name.empty())
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009761 s->PutCString(clang_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009762 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009763 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00009764 }
9765
Kate Stoneb9c1b512016-09-06 20:57:50 +00009766 if (buf.size() > 0) {
9767 s->Write(buf.data(), buf.size());
Greg Clayton8b4edba2015-08-14 20:02:05 +00009768 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009769 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00009770}
9771
Kate Stoneb9c1b512016-09-06 20:57:50 +00009772void ClangASTContext::DumpTypeName(const CompilerType &type) {
9773 if (ClangUtil::IsClangType(type)) {
9774 clang::QualType qual_type(
9775 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9776
9777 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9778 switch (type_class) {
9779 case clang::Type::Record: {
9780 const clang::CXXRecordDecl *cxx_record_decl =
9781 qual_type->getAsCXXRecordDecl();
9782 if (cxx_record_decl)
9783 printf("class %s", cxx_record_decl->getName().str().c_str());
9784 } break;
9785
9786 case clang::Type::Enum: {
9787 clang::EnumDecl *enum_decl =
9788 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9789 if (enum_decl) {
9790 printf("enum %s", enum_decl->getName().str().c_str());
9791 }
9792 } break;
9793
9794 case clang::Type::ObjCObject:
9795 case clang::Type::ObjCInterface: {
9796 const clang::ObjCObjectType *objc_class_type =
9797 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9798 if (objc_class_type) {
9799 clang::ObjCInterfaceDecl *class_interface_decl =
9800 objc_class_type->getInterface();
Adrian Prantl05097242018-04-30 16:49:04 +00009801 // We currently can't complete objective C types through the newly
9802 // added ASTContext because it only supports TagDecl objects right
9803 // now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00009804 if (class_interface_decl)
9805 printf("@class %s", class_interface_decl->getName().str().c_str());
9806 }
9807 } break;
9808
9809 case clang::Type::Typedef:
9810 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9811 ->getDecl()
9812 ->getName()
9813 .str()
9814 .c_str());
9815 break;
9816
9817 case clang::Type::Auto:
9818 printf("auto ");
9819 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9820 llvm::cast<clang::AutoType>(qual_type)
9821 ->getDeducedType()
9822 .getAsOpaquePtr()));
9823
9824 case clang::Type::Elaborated:
9825 printf("elaborated ");
9826 return DumpTypeName(CompilerType(
9827 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9828 ->getNamedType()
9829 .getAsOpaquePtr()));
9830
9831 case clang::Type::Paren:
9832 printf("paren ");
9833 return DumpTypeName(CompilerType(
9834 type.GetTypeSystem(),
9835 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9836
9837 default:
9838 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9839 break;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009840 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009841 }
Greg Clayton6dc8d582015-08-18 22:32:36 +00009842}
9843
Kate Stoneb9c1b512016-09-06 20:57:50 +00009844clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9845 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9846 const char *parent_name, int tag_decl_kind,
9847 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9848 if (template_param_infos.IsValid()) {
9849 std::string template_basename(parent_name);
9850 template_basename.erase(template_basename.find('<'));
9851
9852 return CreateClassTemplateDecl(decl_ctx, access_type,
9853 template_basename.c_str(), tag_decl_kind,
9854 template_param_infos);
9855 }
9856 return NULL;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009857}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009858
Kate Stoneb9c1b512016-09-06 20:57:50 +00009859void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9860 ClangASTContext *ast = (ClangASTContext *)baton;
9861 SymbolFile *sym_file = ast->GetSymbolFile();
9862 if (sym_file) {
9863 CompilerType clang_type = GetTypeForDecl(decl);
9864 if (clang_type)
9865 sym_file->CompleteType(clang_type);
9866 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00009867}
9868
Kate Stoneb9c1b512016-09-06 20:57:50 +00009869void ClangASTContext::CompleteObjCInterfaceDecl(
9870 void *baton, clang::ObjCInterfaceDecl *decl) {
9871 ClangASTContext *ast = (ClangASTContext *)baton;
9872 SymbolFile *sym_file = ast->GetSymbolFile();
9873 if (sym_file) {
9874 CompilerType clang_type = GetTypeForDecl(decl);
9875 if (clang_type)
9876 sym_file->CompleteType(clang_type);
9877 }
Zachary Turner42dff792016-04-15 00:21:26 +00009878}
Greg Clayton261ac3f2015-08-28 01:01:03 +00009879
Kate Stoneb9c1b512016-09-06 20:57:50 +00009880DWARFASTParser *ClangASTContext::GetDWARFParser() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009881 if (!m_dwarf_ast_parser_up)
9882 m_dwarf_ast_parser_up.reset(new DWARFASTParserClang(*this));
9883 return m_dwarf_ast_parser_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00009884}
9885
9886PDBASTParser *ClangASTContext::GetPDBParser() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009887 if (!m_pdb_ast_parser_up)
9888 m_pdb_ast_parser_up.reset(new PDBASTParser(*this));
9889 return m_pdb_ast_parser_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00009890}
9891
9892bool ClangASTContext::LayoutRecordType(
9893 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9894 uint64_t &alignment,
9895 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9896 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9897 &base_offsets,
9898 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9899 &vbase_offsets) {
9900 ClangASTContext *ast = (ClangASTContext *)baton;
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00009901 lldb_private::ClangASTImporter *importer = nullptr;
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009902 if (ast->m_dwarf_ast_parser_up)
9903 importer = &ast->m_dwarf_ast_parser_up->GetClangASTImporter();
9904 if (!importer && ast->m_pdb_ast_parser_up)
9905 importer = &ast->m_pdb_ast_parser_up->GetClangASTImporter();
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00009906 if (!importer)
9907 return false;
9908
9909 return importer->LayoutRecordType(record_decl, bit_size, alignment,
9910 field_offsets, base_offsets, vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009911}
9912
Greg Clayton99558cc42015-08-24 23:46:31 +00009913//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009914// CompilerDecl override functions
9915//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009916
Kate Stoneb9c1b512016-09-06 20:57:50 +00009917ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9918 if (opaque_decl) {
9919 clang::NamedDecl *nd =
9920 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9921 if (nd != nullptr)
9922 return ConstString(nd->getDeclName().getAsString());
9923 }
9924 return ConstString();
Paul Hermand628cbb2015-09-15 23:44:17 +00009925}
9926
Kate Stoneb9c1b512016-09-06 20:57:50 +00009927ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9928 if (opaque_decl) {
9929 clang::NamedDecl *nd =
9930 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9931 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9932 clang::MangleContext *mc = getMangleContext();
9933 if (mc && mc->shouldMangleCXXName(nd)) {
9934 llvm::SmallVector<char, 1024> buf;
9935 llvm::raw_svector_ostream llvm_ostrm(buf);
9936 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9937 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9938 Ctor_Complete, llvm_ostrm);
9939 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9940 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9941 Dtor_Complete, llvm_ostrm);
9942 } else {
9943 mc->mangleName(nd, llvm_ostrm);
Greg Claytonfe689042015-11-10 17:47:04 +00009944 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009945 if (buf.size() > 0)
9946 return ConstString(buf.data(), buf.size());
9947 }
Greg Claytonfe689042015-11-10 17:47:04 +00009948 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009949 }
9950 return ConstString();
Greg Claytonfe689042015-11-10 17:47:04 +00009951}
9952
Kate Stoneb9c1b512016-09-06 20:57:50 +00009953CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9954 if (opaque_decl)
9955 return CompilerDeclContext(this,
9956 ((clang::Decl *)opaque_decl)->getDeclContext());
9957 else
9958 return CompilerDeclContext();
Greg Claytonfe689042015-11-10 17:47:04 +00009959}
9960
Kate Stoneb9c1b512016-09-06 20:57:50 +00009961CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9962 if (clang::FunctionDecl *func_decl =
9963 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9964 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9965 if (clang::ObjCMethodDecl *objc_method =
9966 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9967 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9968 else
Greg Claytonfe689042015-11-10 17:47:04 +00009969 return CompilerType();
9970}
9971
Kate Stoneb9c1b512016-09-06 20:57:50 +00009972size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9973 if (clang::FunctionDecl *func_decl =
9974 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9975 return func_decl->param_size();
9976 if (clang::ObjCMethodDecl *objc_method =
9977 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9978 return objc_method->param_size();
9979 else
9980 return 0;
9981}
9982
9983CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
9984 size_t idx) {
9985 if (clang::FunctionDecl *func_decl =
9986 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
9987 if (idx < func_decl->param_size()) {
9988 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9989 if (var_decl)
9990 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
9991 }
9992 } else if (clang::ObjCMethodDecl *objc_method =
9993 llvm::dyn_cast<clang::ObjCMethodDecl>(
9994 (clang::Decl *)opaque_decl)) {
9995 if (idx < objc_method->param_size())
9996 return CompilerType(
9997 this,
9998 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9999 }
10000 return CompilerType();
10001}
10002
Paul Hermand628cbb2015-09-15 23:44:17 +000010003//----------------------------------------------------------------------
Greg Clayton99558cc42015-08-24 23:46:31 +000010004// CompilerDeclContext functions
10005//----------------------------------------------------------------------
10006
Kate Stoneb9c1b512016-09-06 20:57:50 +000010007std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
10008 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
10009 std::vector<CompilerDecl> found_decls;
10010 if (opaque_decl_ctx) {
10011 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
10012 std::set<DeclContext *> searched;
10013 std::multimap<DeclContext *, DeclContext *> search_queue;
10014 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +000010015
Kate Stoneb9c1b512016-09-06 20:57:50 +000010016 for (clang::DeclContext *decl_context = root_decl_ctx;
10017 decl_context != nullptr && found_decls.empty();
10018 decl_context = decl_context->getParent()) {
10019 search_queue.insert(std::make_pair(decl_context, decl_context));
Paul Hermand628cbb2015-09-15 23:44:17 +000010020
Kate Stoneb9c1b512016-09-06 20:57:50 +000010021 for (auto it = search_queue.find(decl_context); it != search_queue.end();
10022 it++) {
10023 if (!searched.insert(it->second).second)
10024 continue;
10025 symbol_file->ParseDeclsForContext(
10026 CompilerDeclContext(this, it->second));
Paul Hermanea188fc2015-09-16 18:48:30 +000010027
Kate Stoneb9c1b512016-09-06 20:57:50 +000010028 for (clang::Decl *child : it->second->decls()) {
10029 if (clang::UsingDirectiveDecl *ud =
10030 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10031 if (ignore_using_decls)
10032 continue;
10033 clang::DeclContext *from = ud->getCommonAncestor();
10034 if (searched.find(ud->getNominatedNamespace()) == searched.end())
10035 search_queue.insert(
10036 std::make_pair(from, ud->getNominatedNamespace()));
10037 } else if (clang::UsingDecl *ud =
10038 llvm::dyn_cast<clang::UsingDecl>(child)) {
10039 if (ignore_using_decls)
10040 continue;
10041 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10042 clang::Decl *target = usd->getTargetDecl();
10043 if (clang::NamedDecl *nd =
10044 llvm::dyn_cast<clang::NamedDecl>(target)) {
10045 IdentifierInfo *ii = nd->getIdentifier();
10046 if (ii != nullptr &&
10047 ii->getName().equals(name.AsCString(nullptr)))
10048 found_decls.push_back(CompilerDecl(this, nd));
10049 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010050 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010051 } else if (clang::NamedDecl *nd =
10052 llvm::dyn_cast<clang::NamedDecl>(child)) {
10053 IdentifierInfo *ii = nd->getIdentifier();
10054 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
10055 found_decls.push_back(CompilerDecl(this, nd));
10056 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010057 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010058 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010059 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010060 }
10061 return found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +000010062}
10063
Dawn Perchikb5925782015-12-12 19:31:41 +000010064// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010065// and return the number of levels it took to find it, or
Adrian Prantl05097242018-04-30 16:49:04 +000010066// LLDB_INVALID_DECL_LEVEL if not found. If the decl was imported via a using
10067// declaration, its name and/or type, if set, will be used to check that the
10068// decl found in the scope is a match.
Dawn Perchikb5925782015-12-12 19:31:41 +000010069//
Kate Stoneb9c1b512016-09-06 20:57:50 +000010070// The optional name is required by languages (like C++) to handle using
Adrian Prantl05097242018-04-30 16:49:04 +000010071// declarations like:
Dawn Perchikb5925782015-12-12 19:31:41 +000010072//
10073// void poo();
10074// namespace ns {
10075// void foo();
10076// void goo();
10077// }
10078// void bar() {
10079// using ns::foo;
10080// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
10081// // LLDB_INVALID_DECL_LEVEL for 'goo'.
10082// }
10083//
10084// The optional type is useful in the case that there's a specific overload
10085// that we're looking for that might otherwise be shadowed, like:
10086//
10087// void foo(int);
10088// namespace ns {
10089// void foo();
10090// }
10091// void bar() {
10092// using ns::foo;
10093// // CountDeclLevels returns 0 for { 'foo', void() },
10094// // 1 for { 'foo', void(int) }, and
10095// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
10096// }
10097//
10098// NOTE: Because file statics are at the TranslationUnit along with globals, a
Kate Stoneb9c1b512016-09-06 20:57:50 +000010099// function at file scope will return the same level as a function at global
Adrian Prantl05097242018-04-30 16:49:04 +000010100// scope. Ideally we'd like to treat the file scope as an additional scope just
10101// below the global scope. More work needs to be done to recognise that, if
10102// the decl we're trying to look up is static, we should compare its source
10103// file with that of the current scope and return a lower number for it.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010104uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
10105 clang::DeclContext *child_decl_ctx,
10106 ConstString *child_name,
10107 CompilerType *child_type) {
10108 if (frame_decl_ctx) {
10109 std::set<DeclContext *> searched;
10110 std::multimap<DeclContext *, DeclContext *> search_queue;
10111 SymbolFile *symbol_file = GetSymbolFile();
Dawn Perchikb5925782015-12-12 19:31:41 +000010112
Kate Stoneb9c1b512016-09-06 20:57:50 +000010113 // Get the lookup scope for the decl we're trying to find.
10114 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
Dawn Perchikb5925782015-12-12 19:31:41 +000010115
Kate Stoneb9c1b512016-09-06 20:57:50 +000010116 // Look for it in our scope's decl context and its parents.
10117 uint32_t level = 0;
10118 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
10119 decl_ctx = decl_ctx->getParent()) {
10120 if (!decl_ctx->isLookupContext())
10121 continue;
10122 if (decl_ctx == parent_decl_ctx)
10123 // Found it!
10124 return level;
10125 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
10126 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
10127 it++) {
10128 if (searched.find(it->second) != searched.end())
10129 continue;
10130
10131 // Currently DWARF has one shared translation unit for all Decls at top
Adrian Prantl05097242018-04-30 16:49:04 +000010132 // level, so this would erroneously find using statements anywhere. So
10133 // don't look at the top-level translation unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010134 // TODO fix this and add a testcase that depends on it.
10135
10136 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
10137 continue;
10138
10139 searched.insert(it->second);
10140 symbol_file->ParseDeclsForContext(
10141 CompilerDeclContext(this, it->second));
10142
10143 for (clang::Decl *child : it->second->decls()) {
10144 if (clang::UsingDirectiveDecl *ud =
10145 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10146 clang::DeclContext *ns = ud->getNominatedNamespace();
10147 if (ns == parent_decl_ctx)
10148 // Found it!
10149 return level;
10150 clang::DeclContext *from = ud->getCommonAncestor();
10151 if (searched.find(ns) == searched.end())
10152 search_queue.insert(std::make_pair(from, ns));
10153 } else if (child_name) {
10154 if (clang::UsingDecl *ud =
10155 llvm::dyn_cast<clang::UsingDecl>(child)) {
10156 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10157 clang::Decl *target = usd->getTargetDecl();
10158 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
10159 if (!nd)
10160 continue;
10161 // Check names.
10162 IdentifierInfo *ii = nd->getIdentifier();
10163 if (ii == nullptr ||
10164 !ii->getName().equals(child_name->AsCString(nullptr)))
10165 continue;
10166 // Check types, if one was provided.
10167 if (child_type) {
10168 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
10169 if (!AreTypesSame(clang_type, *child_type,
10170 /*ignore_qualifiers=*/true))
10171 continue;
10172 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010173 // Found it!
10174 return level;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010175 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010176 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010177 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010178 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010179 }
10180 ++level;
Dawn Perchikb5925782015-12-12 19:31:41 +000010181 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010182 }
10183 return LLDB_INVALID_DECL_LEVEL;
Dawn Perchikb5925782015-12-12 19:31:41 +000010184}
10185
Kate Stoneb9c1b512016-09-06 20:57:50 +000010186bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
10187 if (opaque_decl_ctx)
10188 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
10189 else
Greg Clayton99558cc42015-08-24 23:46:31 +000010190 return false;
10191}
10192
Kate Stoneb9c1b512016-09-06 20:57:50 +000010193ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
10194 if (opaque_decl_ctx) {
10195 clang::NamedDecl *named_decl =
10196 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10197 if (named_decl)
10198 return ConstString(named_decl->getName());
10199 }
10200 return ConstString();
Greg Clayton99558cc42015-08-24 23:46:31 +000010201}
10202
Kate Stoneb9c1b512016-09-06 20:57:50 +000010203ConstString
10204ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
10205 if (opaque_decl_ctx) {
10206 clang::NamedDecl *named_decl =
10207 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10208 if (named_decl)
10209 return ConstString(
10210 llvm::StringRef(named_decl->getQualifiedNameAsString()));
10211 }
10212 return ConstString();
10213}
10214
10215bool ClangASTContext::DeclContextIsClassMethod(
10216 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
10217 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
10218 if (opaque_decl_ctx) {
10219 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10220 if (ObjCMethodDecl *objc_method =
10221 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
10222 if (is_instance_method_ptr)
10223 *is_instance_method_ptr = objc_method->isInstanceMethod();
10224 if (language_ptr)
10225 *language_ptr = eLanguageTypeObjC;
10226 if (language_object_name_ptr)
10227 language_object_name_ptr->SetCString("self");
10228 return true;
10229 } else if (CXXMethodDecl *cxx_method =
10230 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
10231 if (is_instance_method_ptr)
10232 *is_instance_method_ptr = cxx_method->isInstance();
10233 if (language_ptr)
10234 *language_ptr = eLanguageTypeC_plus_plus;
10235 if (language_object_name_ptr)
10236 language_object_name_ptr->SetCString("this");
10237 return true;
10238 } else if (clang::FunctionDecl *function_decl =
10239 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
10240 ClangASTMetadata *metadata =
10241 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
10242 if (metadata && metadata->HasObjectPtr()) {
10243 if (is_instance_method_ptr)
10244 *is_instance_method_ptr = true;
10245 if (language_ptr)
10246 *language_ptr = eLanguageTypeObjC;
10247 if (language_object_name_ptr)
10248 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
10249 return true;
10250 }
10251 }
10252 }
10253 return false;
10254}
10255
10256clang::DeclContext *
10257ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
10258 if (dc.IsClang())
10259 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
10260 return nullptr;
10261}
Greg Clayton99558cc42015-08-24 23:46:31 +000010262
10263ObjCMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010264ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
10265 if (dc.IsClang())
10266 return llvm::dyn_cast<clang::ObjCMethodDecl>(
10267 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10268 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010269}
10270
10271CXXMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010272ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10273 if (dc.IsClang())
10274 return llvm::dyn_cast<clang::CXXMethodDecl>(
10275 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10276 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010277}
10278
10279clang::FunctionDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010280ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10281 if (dc.IsClang())
10282 return llvm::dyn_cast<clang::FunctionDecl>(
10283 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10284 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010285}
10286
10287clang::NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010288ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10289 if (dc.IsClang())
10290 return llvm::dyn_cast<clang::NamespaceDecl>(
10291 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10292 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010293}
10294
10295ClangASTMetadata *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010296ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10297 const void *object) {
10298 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10299 if (ast)
10300 return ClangASTContext::GetMetadata(ast, object);
10301 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010302}
10303
10304clang::ASTContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010305ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10306 ClangASTContext *ast =
10307 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10308 if (ast)
10309 return ast->getASTContext();
10310 return nullptr;
10311}
10312
10313ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10314 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10315 m_target_wp(target.shared_from_this()),
10316 m_persistent_variables(new ClangPersistentVariables) {}
10317
10318UserExpression *ClangASTContextForExpressions::GetUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010319 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010320 Expression::ResultType desired_type,
Aleksandr Urakov40624a02019-02-05 09:14:36 +000010321 const EvaluateExpressionOptions &options,
10322 ValueObject *ctx_obj) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000010323 TargetSP target_sp = m_target_wp.lock();
10324 if (!target_sp)
Greg Clayton99558cc42015-08-24 23:46:31 +000010325 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010326
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010327 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
Aleksandr Urakov40624a02019-02-05 09:14:36 +000010328 desired_type, options, ctx_obj);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010329}
10330
Kate Stoneb9c1b512016-09-06 20:57:50 +000010331FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10332 const CompilerType &return_type, const Address &function_address,
10333 const ValueList &arg_value_list, const char *name) {
10334 TargetSP target_sp = m_target_wp.lock();
10335 if (!target_sp)
10336 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010337
Kate Stoneb9c1b512016-09-06 20:57:50 +000010338 Process *process = target_sp->GetProcessSP().get();
10339 if (!process)
10340 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010341
Kate Stoneb9c1b512016-09-06 20:57:50 +000010342 return new ClangFunctionCaller(*process, return_type, function_address,
10343 arg_value_list, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010344}
10345
10346UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010347ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10348 const char *name) {
10349 TargetSP target_sp = m_target_wp.lock();
10350 if (!target_sp)
10351 return nullptr;
10352
10353 return new ClangUtilityFunction(*target_sp.get(), text, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010354}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010355
10356PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010357ClangASTContextForExpressions::GetPersistentExpressionState() {
10358 return m_persistent_variables.get();
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010359}
Sean Callanan68e44232017-09-28 20:20:25 +000010360
10361clang::ExternalASTMerger &
10362ClangASTContextForExpressions::GetMergerUnchecked() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +000010363 lldbassert(m_scratch_ast_source_up != nullptr);
10364 return m_scratch_ast_source_up->GetMergerUnchecked();
Sean Callanan68e44232017-09-28 20:20:25 +000010365}